From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B95101D798E for ; Mon, 6 Apr 2026 03:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.193 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775445199; cv=none; b=CEux4HT9krRIPk5xDbLLRDpAqScqrApXUvRxTn3iHWqg4SZ1Kc09OXMmpzFVgvjHcUsjQgrm9Q36tZOqLm/3sxRUFNNlvZW5LlzZ96Hfp1GDZrb9IiGuncDQ/W7WBv2UV6aEMdqGPIcU0kSpWlCuvQ3G9373zRWa+r/PIJ3sq/g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775445199; c=relaxed/simple; bh=yQGImmrjwLYeGHdVX82wTBnt/uEq+A862pqBtuQLX+E=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=B9vnnoZRrgBqBw4Rj2oSq1is2utrUq30n6xXiO3jhEIoeKAPmpTO75Abz/8zM60gvOfEI7ZUyMEB5UkIgg71cQncSZrkW9tuhz1MQEVzKO6qQ45Vpdo7b9IeZMI2UK5kvgr/KkFwrtHs5Elxh89GhCcqcZ4z9Z/8CM4buLf73aM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com; spf=pass smtp.mailfrom=etsalapatis.com; dkim=pass (2048-bit key) header.d=etsalapatis-com.20251104.gappssmtp.com header.i=@etsalapatis-com.20251104.gappssmtp.com header.b=CNdGFAji; arc=none smtp.client-ip=209.85.215.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=etsalapatis.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=etsalapatis-com.20251104.gappssmtp.com header.i=@etsalapatis-com.20251104.gappssmtp.com header.b="CNdGFAji" Received: by mail-pg1-f193.google.com with SMTP id 41be03b00d2f7-c76c60c7502so1253956a12.0 for ; Sun, 05 Apr 2026 20:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=etsalapatis-com.20251104.gappssmtp.com; s=20251104; t=1775445197; x=1776049997; darn=vger.kernel.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=WikSyXf56QgGRlPo6LAXPYCmBwFl+zDorUSabvFaPsQ=; b=CNdGFAjiwyrUvwB+aqH/zLS0WZQHfTuwaJZHQWqX3cMG1WQWWIlTZ6Z8Xue/HuUO/y d/J09JaQjUpm3usDcTCS0Q4GQU5GzxU7fBbo8yxAMow7ykx2UgDlStCzaxNR9ZtgCdoW mtVxqePweiSZ734IYp7b8VvCDMhfqTKgOxpzvEI6tkZFunc11xU1GX3LSakZeU9Qv+og DelJRxldSSyq05LS1LgcOWRaRRndsZXcs6pHXCROck1OM/ahJvqkrJg8BGxF24dLn4jY NOgm6Vh8DJSCE+zP6W2cDmSmtr/r31YWgNsAD2uqNujzqg2iG9Y4qamHiMQC57eBdeJj Z5Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775445197; x=1776049997; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=WikSyXf56QgGRlPo6LAXPYCmBwFl+zDorUSabvFaPsQ=; b=Od6Gxq5u/Cf56h1llPrzLP3SZPI3i/mcFHXStZoj/LV09WlFZ3VMbm3Av+kQZ1kYvK JNios/OkiBRqDOFjuZ0Bcrjs8/TrLm4sKeVRTI2ZWCVKFwo8UYZ1AlViBQ2DLMm1wya+ T9FJ0piwaimEL1O1T8LfO4yexS8g96Hza5+A4pTpSNX2W1UiiWQwmsp9/Yba3mW4Jzo4 ajUeCLSWUwZaSfF0UnJV6+i4b0p/nZl71KWjkpqh15r7x85C3G5KjAn7TFiiUo8Efu21 Z7mFC9E1wj/pejeMdpqDdRXfCWkp4vhH0H3yr1BxKXYmuCJb4tybi9ZYaxzkYsrnXcYW Kijg== X-Forwarded-Encrypted: i=1; AJvYcCW7TmuPnASNLMHpdCapp+gESyduIgdsfEeF9LDDY2Bo4TLxmhtVg3JikvcP/36TibEka40/wrtQZRCbCto=@vger.kernel.org X-Gm-Message-State: AOJu0YzI0wcw4glzoRhVI+zVfQxR+Af4l9DmgaZX9MRhrJIHQug4H72i m8I2G9IuDta+4flo132aJVbdR2OTGYrF7sTdp60EwAHs1D2Bh9LN/K6RHfte8cXipKc= X-Gm-Gg: AeBDievZfKfPyHmwoQyml807g+V2jWLt7bg/deZ4wfpCO7N56df1lbjSFSeIAYsWa00 ZuGCFKjH3zCbxH6CF7QTkYHzn37v6aSNPFLQy6+jHOPh7n4/WvKvjQbrYOOa9njQSPGFAzkF3TC eEnJYrnkOLN84HYgU2XV72VMuIe+fonR9kYFi2eVY4gjQluCLvDlZbzNAUXQnF8cLS2MZMtvPL2 vBTToR9jYrExK5eOqHhTzWYIV3fnnMZuK/4vY4syS0McLObIrRQsjJKaluRetJD9ByzIp/LLA1x vzHANtMEFFeCyvCEzVtPbaD6C+8B0xCML5c+vFAwK22HRhvfNZblF0Z6MyqJO49X4hq2L0XvX8d XURtcn/qfOLAbcAE7Qtrv4/gzcaCRZQWgbtjmdVMa7XkDxotH2itaTSggYwj09wQ+YeVo+Sb8PQ Xr2LUKV6k= X-Received: by 2002:a17:903:946:b0:2b2:5293:f415 with SMTP id d9443c01a7336-2b277d8ba21mr124434045ad.7.1775445196985; Sun, 05 Apr 2026 20:13:16 -0700 (PDT) Received: from localhost ([2604:3d08:487d:cd00::5517]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2749cbd85sm119417275ad.74.2026.04.05.20.13.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 05 Apr 2026 20:13:16 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sun, 05 Apr 2026 23:13:15 -0400 Message-Id: Cc: "Quan Sun" <2022090917019@std.uestc.edu.cn>, "Yinhao Hu" , "Kaiyan Mei" , "Dongliang Mu" , "Martin KaFai Lau" , "Daniel Borkmann" , "John Fastabend" , "Stanislav Fomichev" , "Alexei Starovoitov" , "Andrii Nakryiko" , "Eduard Zingerman" , "Kumar Kartikeya Dwivedi" , "Song Liu" , "Yonghong Song" , "Jiri Olsa" , "David S. Miller" , "Eric Dumazet" , "Jakub Kicinski" , "Paolo Abeni" , "Simon Horman" , "Shuah Khan" , , , Subject: Re: [PATCH bpf v1 1/2] bpf: Fix SOCK_OPS_GET_SK same-register OOB read in sock_ops From: "Emil Tsalapatis" To: "Jiayuan Chen" , "Emil Tsalapatis" , X-Mailer: aerc 0.21.0-0-g5549850facc2 References: <20260404141010.247536-1-jiayuan.chen@linux.dev> <346597fc-1703-45d7-bcef-55f5d4a7579c@linux.dev> In-Reply-To: <346597fc-1703-45d7-bcef-55f5d4a7579c@linux.dev> On Sun Apr 5, 2026 at 10:58 PM EDT, Jiayuan Chen wrote: > > On 4/6/26 7:54 AM, Emil Tsalapatis wrote: >> On Sun Apr 5, 2026 at 7:49 PM EDT, Emil Tsalapatis wrote: >>> On Sat Apr 4, 2026 at 10:09 AM EDT, Jiayuan Chen wrote: >>>> When a BPF sock_ops program reads ctx->sk with dst_reg =3D=3D src_reg >>>> (e.g., r1 =3D *(u64 *)(r1 + offsetof(sk))), the SOCK_OPS_GET_SK() macr= o >>>> fails to zero the destination register in the is_fullsock =3D=3D 0 pat= h. >>>> >>>> The macro saves/restores a temporary register and checks is_fullsock. >>>> When is_fullsock =3D=3D 0 (e.g., TCP_NEW_SYN_RECV state with a request= _sock), >>>> it should set dst_reg =3D 0 (NULL) so the verifier's PTR_TO_SOCKET_OR_= NULL >>>> type is correct at runtime. Instead, dst_reg retains the original ctx >>>> pointer, which passes subsequent NULL checks and can be used as a bogu= s >>>> socket pointer, leading to stack-out-of-bounds access in helpers like >>>> bpf_skc_to_tcp6_sock(). >>>> >>>> Fix by: >>>> - Changing JMP_A(1) to JMP_A(2) in the fullsock path to skip the >>>> added instruction. >>>> - Adding BPF_MOV64_IMM(si->dst_reg, 0) after the temp register >>>> restore in the !fullsock path, placed after the restore because >>>> dst_reg =3D=3D src_reg means we need src_reg intact to read ctx->t= emp. >>>> >>>> Fixes: 84f44df664e9 ("bpf: sock_ops sk access may stomp registers when= dst_reg =3D src_reg") >>>> Reported-by: Quan Sun <2022090917019@std.uestc.edu.cn> >>>> Reported-by: Yinhao Hu >>>> Reported-by: Kaiyan Mei >>>> Reported-by: Dongliang Mu >>>> Closes: https://lore.kernel.org/bpf/6fe1243e-149b-4d3b-99c7-fcc9e2f757= 87@std.uestc.edu.cn/T/#u >>>> Signed-off-by: Jiayuan Chen >>> This patch only seems to fix the problem when dst_reg =3D=3D src_reg. >>> Why is this not an issue when is_fullsock =3D=3D 0, but dst_reg !=3D sr= c_reg? >>> In that case the dst_reg is unmodified by the whole macro but is still >>> marked as PTR_TO_SOCKET_OR_NULL. Isn't that a problem? Can you add >>> a test case for is_fullsock =3D=3D 0 but dst_reg !=3D src_reg in patch = 2? >> Sorry for the double post, but also check sashiko.dev: >> SOSK_OPTS_GET_FIELD seems to have the same issue as the >> SOCK_OPTS_GET_SK. Can you add the same fix to it? >> > > Thanks for the review! > > The AI reviewer's observation about SOCK_OPS_GET_FIELD() is correct =E2= =80=94 > it has the same bug when dst_reg =3D=3D src_reg and is_locked_tcp_sock = =3D=3D 0. > I've folded that fix into patch 1 in v2. > > Regarding dst_reg !=3D src_reg: this case is actually safe. When > dst_reg !=3D src_reg, fullsock_reg is dst_reg itself, and the generated > sequence is: > > LDX_MEM=C2=A0 =C2=A0dst_reg =3D is_fullsock > JEQ=C2=A0 =C2=A0 =C2=A0 =C2=A0dst_reg =3D=3D 0, +jmp > LDX_MEM=C2=A0 =C2=A0dst_reg =3D sk > Yes, I missed the dst is the is_fullsock_reg assignment. v1 looks correct then. > The JEQ only branches when dst_reg =3D=3D 0, so dst_reg is naturally > zeroed on that path =E2=80=94 no extra MOV_IMM needed. The same-register = bug > exists precisely because dst_reg =3D=3D src_reg forces the macro to borro= w > a temporary register for the is_fullsock check, leaving dst_reg (the > ctx pointer) untouched. > > I will add a get_sk_diff_reg subtest in v2. > > The other suggestions (moving the detailed comment to the BPF program > file, avoiding vague "the fix" wording) are good points =E2=80=94 address= ed > in v2 as well. With the changes, feel free to add to both patches: Reviewed-by: Emil Tsalapatis