From nobody Sat Jul 25 01:40:53 2026 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC4FC2D661C; Tue, 21 Jul 2026 04:06:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606798; cv=none; b=iKr8We07Ci93BpYbmZ/m3zwRn2e2eQCi01YcT9Jx4O2rOkEQdbRiKvw57XVrRfZg0XsmI5GJucZy8jlUpzzbvQUDt6TDZLGXhcwZWUcWhKYlEa277j1ln1cIUqGLF6ePS61qqttnzjoTQULlO70nUQXswMn+hnrI1wtVhhDPues= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606798; c=relaxed/simple; bh=0LsocKogIxy0NAFJaGmIS3UoSTnByu09at0uIn4mJrI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TB6u5pJfVA7rRiCa5i3JsZWou4bMJXP+K6XQAaR2qPzDBgmfarWWbEM+BnqSYNhI/4Hwc93Lu5cW0r2A0hDnweCZOtukhaDeJF1YwRy1LtnrN21NsVQc18YMMLS2zj0PFcRZNFi4emkNgK3nrA/+CNd7FTe1K4XAR2/5nZzCGoI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.177]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4h43hc0wZkzKHMP3; Tue, 21 Jul 2026 12:05:52 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id C297E405B2; Tue, 21 Jul 2026 12:06:18 +0800 (CST) Received: from ultra.huawei.com (unknown [10.90.53.71]) by APP1 (Coremail) with UTF8SMTPA id cCh0CgAHF3M58F5qwav1Bw--.64145S3; Tue, 21 Jul 2026 12:06:18 +0800 (CST) From: Pu Lehui To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Mykyta Yatsenko , Amery Hung Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Yonghong Song , Song Liu , Jiri Olsa , Emil Tsalapatis , Pu Lehui , Pu Lehui Subject: [PATCH bpf v5 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog Date: Tue, 21 Jul 2026 04:10:45 +0000 Message-Id: <20260721041048.1394085-2-pulehui@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260721041048.1394085-1-pulehui@huaweicloud.com> References: <20260721041048.1394085-1-pulehui@huaweicloud.com> 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 X-CM-TRANSID: cCh0CgAHF3M58F5qwav1Bw--.64145S3 X-Coremail-Antispam: 1UD129KBjvJXoW7tr1xJw4kCF1Utr17uF48WFg_yoW8CF1kpF y3Cr1DGw10krsF9F18X3WkuryrXFy0gr4UCr1DZ3W0qFyIqr1Fg34UurZ2vrZY9FWqgFyS qa4jgr1Yqw1jva7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_GFv_WrylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0JU4OJ5UUUUU= X-CM-SenderInfo: psxovxtxl6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Pu Lehui In bpf_netns_link_update_prog, the checks for old_prog and prog type are currently performed locklessly before acquiring netns_bpf_mutex. This creates a race condition that can lead to a UAF issue. If two threads concurrently execute BPF_LINK_UPDATE on the same netns link, the following execution path can trigger a UAF: CPU0 CPU1 bpf_netns_link_update_prog if (old_prog && old_prog !=3D link->prog) return -EPERM; bpf_netns_link_update_prog if (old_prog && old_prog != =3D link->prog) ... old_prog =3D xchg(&link->pr= og, new_prog); bpf_prog_put(old_prog); if (new_prog->type !=3D link->prog->type) <-- trigger UAF Fix this by moving the old_prog and prog->type checks inside the netns_bpf_mutex critical section. Fixes: 7f045a49fee0 ("bpf: Add link-based BPF program attachment to network= namespace") Reported-by: Sashiko Reviewed-by: Amery Hung Signed-off-by: Pu Lehui Reviewed-by: Emil Tsalapatis --- kernel/bpf/net_namespace.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c index 25f30f9edaef..9fc62db1441c 100644 --- a/kernel/bpf/net_namespace.c +++ b/kernel/bpf/net_namespace.c @@ -171,13 +171,17 @@ static int bpf_netns_link_update_prog(struct bpf_link= *link, struct net *net; int idx, ret; =20 - if (old_prog && old_prog !=3D link->prog) - return -EPERM; - if (new_prog->type !=3D link->prog->type) - return -EINVAL; - mutex_lock(&netns_bpf_mutex); =20 + if (old_prog && old_prog !=3D link->prog) { + ret =3D -EPERM; + goto out_unlock; + } + if (new_prog->type !=3D link->prog->type) { + ret =3D -EINVAL; + goto out_unlock; + } + net =3D net_link->net; if (!net || !check_net(net)) { /* Link auto-detached or netns dying */ --=20 2.34.1 From nobody Sat Jul 25 01:40:53 2026 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D5E12EF653; Tue, 21 Jul 2026 04:06:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606799; cv=none; b=izEeFaa5JSR5t5c2GjcrT+yzhS7dPSVIIN3oFn40pGhlWYdkOd26ouzMFNs1pHELK22ZzzAYVTYx2QJI/qnYX4BCVJGUR/RQImx3IqUBgEpHnq1xxe0eoQyWjK6+SUDcqAJ28S3+rUusa/NK65ZeTcH+WPoI/bthR0Zb674m3kg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606799; c=relaxed/simple; bh=H/V0jVFnZ1pQMZit3JMtoGaXSaYiPGPcKaFv6kDu8RI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mpSPhdEdry5ZF/YiwfI1lHD/1vPv30cYa/DdKsiXY0yZoR/WpiK+RggFotYdBSu09Cj6+G+colZ6r25PNYyjP5FRA666/kRCSYfkxy6Gj6gXbYuGH3WVNbcnQxeGdwJsq87AfYmXZVW60Bski896uygUoP5XXUUJeaM1dIRrXms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.177]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4h43hc1rgBzKHMPd; Tue, 21 Jul 2026 12:05:52 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id E535F405B5; Tue, 21 Jul 2026 12:06:18 +0800 (CST) Received: from ultra.huawei.com (unknown [10.90.53.71]) by APP1 (Coremail) with UTF8SMTPA id cCh0CgAHF3M58F5qwav1Bw--.64145S4; Tue, 21 Jul 2026 12:06:18 +0800 (CST) From: Pu Lehui To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Mykyta Yatsenko , Amery Hung Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Yonghong Song , Song Liu , Jiri Olsa , Emil Tsalapatis , Pu Lehui , Pu Lehui Subject: [PATCH bpf v5 2/4] bpf: Fix UAF due to missing link type check in mprog Date: Tue, 21 Jul 2026 04:10:46 +0000 Message-Id: <20260721041048.1394085-3-pulehui@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260721041048.1394085-1-pulehui@huaweicloud.com> References: <20260721041048.1394085-1-pulehui@huaweicloud.com> 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 X-CM-TRANSID: cCh0CgAHF3M58F5qwav1Bw--.64145S4 X-Coremail-Antispam: 1UD129KBjvJXoWxZr47Gr4kKry8tryxXr43Jrb_yoW5Kw4kpF s3JryvyF1Yv3y7XF4jvF1fAryY9F48Wr1jkas5Ww4vvrn0ga1vqF1fWr4Sqw15KFZ8Grsa vw1qqryDA34DXrDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmY14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1l e2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI 8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwAC jcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2Y2ka0x kIwI1lc7CjxVAaw2AFwI0_Jw0_GFyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r4a6rW5MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjfUOdgAUUUUU X-CM-SenderInfo: psxovxtxl6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Pu Lehui In bpf_mprog_link, the code does not check the link->type first before dereferencing link->prog->type. This missing validation allows a user to pass an abnormal non-netkit or non-tcx link via relative_fd. If doing BPF_LINK_UPDATE on the abnormal link, it can trigger a UAF issue. CPU0 CPU1 netkit_link_prog_attach bpf_mprog_attach bpf_mprog_tuple_relative bpf_mprog_link link =3D bpf_link_get_from_fd(id_or_fd); BPF_LINK_UPDATE ... old_prog =3D xchg(&link->link.pro= g, new_prog); bpf_prog_put(old_prog); if (type && link->prog->type !=3D type) <-- trigger UAF The reason for the UAF is that each subsystem provides its own protection for link->prog. Since there is no cross subsystem protection (if not considering the RCU of prog tear down), dereferencing the prog of an anchor link that does not belong to the current subsystem is not safe: it may have been freed. Therefore, we need to validate link->type to reject foreign anchors. Fix this by strictly validating link->type in bpf_mprog_link against the expected link type. bpf_mprog_tuple_relative is also adjusted to accept and pass down the expected link type. Fixes: 053c8e1f235d ("bpf: Add generic attach/detach/query API for multi-pr= ogs") Reported-by: Sashiko Reviewed-by: Amery Hung Signed-off-by: Pu Lehui --- kernel/bpf/mprog.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/mprog.c b/kernel/bpf/mprog.c index 1394168062e8..37ce91e0bcd2 100644 --- a/kernel/bpf/mprog.c +++ b/kernel/bpf/mprog.c @@ -6,7 +6,7 @@ =20 static int bpf_mprog_link(struct bpf_tuple *tuple, u32 id_or_fd, u32 flags, - enum bpf_prog_type type) + enum bpf_link_type type) { struct bpf_link *link =3D ERR_PTR(-EINVAL); bool id =3D flags & BPF_F_ID; @@ -17,7 +17,7 @@ static int bpf_mprog_link(struct bpf_tuple *tuple, link =3D bpf_link_get_from_fd(id_or_fd); if (IS_ERR(link)) return PTR_ERR(link); - if (type && link->prog->type !=3D type) { + if (type && link->type !=3D type) { bpf_link_put(link); return -EINVAL; } @@ -52,21 +52,22 @@ static int bpf_mprog_prog(struct bpf_tuple *tuple, =20 static int bpf_mprog_tuple_relative(struct bpf_tuple *tuple, u32 id_or_fd, u32 flags, - enum bpf_prog_type type) + enum bpf_link_type ltype, + enum bpf_prog_type ptype) { bool link =3D flags & BPF_F_LINK; bool id =3D flags & BPF_F_ID; =20 memset(tuple, 0, sizeof(*tuple)); if (link) - return bpf_mprog_link(tuple, id_or_fd, flags, type); + return bpf_mprog_link(tuple, id_or_fd, flags, ltype); /* If no relevant flag is set and no id_or_fd was passed, then * tuple link/prog is just NULLed. This is the case when before/ * after selects first/last position without passing fd. */ if (!id && !id_or_fd) return 0; - return bpf_mprog_prog(tuple, id_or_fd, flags, type); + return bpf_mprog_prog(tuple, id_or_fd, flags, ptype); } =20 static void bpf_mprog_tuple_put(struct bpf_tuple *tuple) @@ -243,6 +244,7 @@ int bpf_mprog_attach(struct bpf_mprog_entry *entry, return -EEXIST; ret =3D bpf_mprog_tuple_relative(&rtuple, id_or_fd, flags & ~BPF_F_REPLACE, + link ? link->type : BPF_LINK_TYPE_UNSPEC, prog_new->type); if (ret) return ret; @@ -343,6 +345,7 @@ int bpf_mprog_detach(struct bpf_mprog_entry *entry, if (!bpf_mprog_total(entry)) return -ENOENT; ret =3D bpf_mprog_tuple_relative(&rtuple, id_or_fd, flags, + link ? link->type : BPF_LINK_TYPE_UNSPEC, prog ? prog->type : BPF_PROG_TYPE_UNSPEC); if (ret) --=20 2.34.1 From nobody Sat Jul 25 01:40:53 2026 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AA1B27874F; Tue, 21 Jul 2026 04:06:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606795; cv=none; b=edSCsUGTQ98UeqA9Ic6s/aNsT6smTly8iLXSXdd/REHkCMr+sdaq2WpHPoCj3CzxaT7E71+fZHOp+S5PicN4O36qBy/7vh9vQOtV3AHMlhoyCjOIsqFle5Xs+2H3og8eFsZ6UVMbmtB6bceHvo6+VfqzIvTrq5GgJqUPVHTfgCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606795; c=relaxed/simple; bh=ZIGQ4mjLviXf53CrGLdTDh1mBwZyp9zMDwv1BtY4JRs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T94FpvwZuyW2DKlLGrcGrxAHzh+gmkl1Rr635x4EnggMpMcBO+b79kXaVZG278MqtIpJ+U9AnHOcr29/cc0u/uCtwvPqmM33rYn6HN58kvZNp+aRAV5I4pXYR7lfa559+Dj630/RofjPGGypkNN2x+NJg0dvwX2Q0vmM1WaOC4o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.198]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4h43hc0rhdzYQtk0; Tue, 21 Jul 2026 12:05:52 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id C8F6040579; Tue, 21 Jul 2026 12:06:18 +0800 (CST) Received: from ultra.huawei.com (unknown [10.90.53.71]) by APP1 (Coremail) with UTF8SMTPA id cCh0CgAHF3M58F5qwav1Bw--.64145S5; Tue, 21 Jul 2026 12:06:18 +0800 (CST) From: Pu Lehui To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Mykyta Yatsenko , Amery Hung Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Yonghong Song , Song Liu , Jiri Olsa , Emil Tsalapatis , Pu Lehui , Pu Lehui Subject: [PATCH bpf v5 3/4] bpf: Fix potential UAF when reading bpf link info Date: Tue, 21 Jul 2026 04:10:47 +0000 Message-Id: <20260721041048.1394085-4-pulehui@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260721041048.1394085-1-pulehui@huaweicloud.com> References: <20260721041048.1394085-1-pulehui@huaweicloud.com> 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 X-CM-TRANSID: cCh0CgAHF3M58F5qwav1Bw--.64145S5 X-Coremail-Antispam: 1UD129KBjvJXoWxJFyfGryUuF4UZF1xCFy5twb_yoW5WF17pF W3J3Z8Ca1rWr4293WUJrWUurySgF48WFyUJF97W34FyF1aqrZYgFyUWrWfZr9IkF97Gr1x X34jyry5Jw17XFDanT9S1TB71UUUUU7qnTZGka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info and bpf_link_get_info_by_fd, link->prog is accessed without holding any locks. If the prog is concurrently replaced via bpf_link_update, the old prog can be freed, leading to a potential UAF issue. Before dereferencing the prog, both normal RCU and RCU Tasks Trace read locks would normally be required, as BPF_LINK_TYPE_ITER supports both non-sleepable and sleepable progs. However, as commit 57b23c0f612d ("bpf: Retire rcu_trace_implies_rcu_gp()") clarifies, an RCU Tasks Trace grace period implies an RCU grace period, so holding only rcu_read_lock() is already sufficient. Fixes: 0c991ebc8c69 ("bpf: Implement bpf_prog replacement for an active bpf= _cgroup_link") Reported-by: Sashiko Signed-off-by: Pu Lehui Reviewed-by: Amery Hung Reviewed-by: Emil Tsalapatis --- kernel/bpf/syscall.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 6db306d23b47..cad986807d53 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3471,9 +3471,10 @@ static const char *bpf_link_type_strs[] =3D { static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) { const struct bpf_link *link =3D filp->private_data; - const struct bpf_prog *prog =3D link->prog; + const struct bpf_prog *prog; enum bpf_link_type type =3D link->type; char prog_tag[sizeof(prog->tag) * 2 + 1] =3D { }; + u32 prog_id; =20 if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) { if (link->type =3D=3D BPF_LINK_TYPE_KPROBE_MULTI) @@ -3490,13 +3491,20 @@ static void bpf_link_show_fdinfo(struct seq_file *m= , struct file *filp) } seq_printf(m, "link_id:\t%u\n", link->id); =20 + rcu_read_lock(); + prog =3D READ_ONCE(link->prog); if (prog) { bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); + prog_id =3D prog->aux->id; + } + rcu_read_unlock(); + + if (prog) { seq_printf(m, "prog_tag:\t%s\n" "prog_id:\t%u\n", prog_tag, - prog->aux->id); + prog_id); } if (link->ops->show_fdinfo) link->ops->show_fdinfo(link, m); @@ -5535,6 +5543,7 @@ static int bpf_link_get_info_by_fd(struct file *file, { struct bpf_link_info __user *uinfo =3D u64_to_user_ptr(attr->info.info); struct bpf_link_info info; + const struct bpf_prog *prog; u32 info_len =3D attr->info.info_len; int err; =20 @@ -5549,8 +5558,12 @@ static int bpf_link_get_info_by_fd(struct file *file, =20 info.type =3D link->type; info.id =3D link->id; - if (link->prog) - info.prog_id =3D link->prog->aux->id; + + rcu_read_lock(); + prog =3D READ_ONCE(link->prog); + if (prog) + info.prog_id =3D prog->aux->id; + rcu_read_unlock(); =20 if (link->ops->fill_link_info) { err =3D link->ops->fill_link_info(link, &info); --=20 2.34.1 From nobody Sat Jul 25 01:40:53 2026 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EFC02F745C; Tue, 21 Jul 2026 04:06:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606799; cv=none; b=GSfs0B6owfTZ2BOGbkfYfwu7Mb9Y315qGLSmh+x+cMJWX6YM0K4G6/+7gtDuJkcyZ6Ts1uAu+CW142z0y3zKpKl56YUUN7ECGqAS95gmzbLqDsyR70H4p7kjDo2h5AZMaDgialUFLDeADCJH1KIkapsfWvL9bz6Tb1ahnJCk1yE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784606799; c=relaxed/simple; bh=6kLDb/EkwL+x0uT2625WN0nWMVGKTy4T4ZBkAimrppQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KDMcNTWTRZcfw5EHCRgtydoDBLo2r27Xp9F4gYmfmBJVrXjlT6PnJFyK+KqaizLMMPjjoH+TbDrZP6Oz4VXPrawnk14xc6/jNGNfJL3ADtg3oZ+6ITtDM92cbb6WsA5bL3eWvhx/c59yEUlhVpEK/KRQlloxF1fdz1A2W1BkCRw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.177]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4h43hv0rn7zYQtJ3; Tue, 21 Jul 2026 12:06:07 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id E8E7A405B6; Tue, 21 Jul 2026 12:06:18 +0800 (CST) Received: from ultra.huawei.com (unknown [10.90.53.71]) by APP1 (Coremail) with UTF8SMTPA id cCh0CgAHF3M58F5qwav1Bw--.64145S6; Tue, 21 Jul 2026 12:06:18 +0800 (CST) From: Pu Lehui To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Mykyta Yatsenko , Amery Hung Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Yonghong Song , Song Liu , Jiri Olsa , Emil Tsalapatis , Pu Lehui , Pu Lehui Subject: [PATCH bpf v5 4/4] bpf, cgroup: Fix storage null-ptr-deref after replacing prog Date: Tue, 21 Jul 2026 04:10:48 +0000 Message-Id: <20260721041048.1394085-5-pulehui@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260721041048.1394085-1-pulehui@huaweicloud.com> References: <20260721041048.1394085-1-pulehui@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Enc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CI42IY6xAIw20EY4v20xvaj40_Jr0_JF 4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBI daVFxhVjvjDU0xZFpf9x0JUQFxUUUUUU= X-CM-SenderInfo: psxovxtxl6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Pu Lehui Syzkaller reported a storage null-ptr-deref issue after replacing prog. This occurs in the following scenario: 1. prog A, an empty prog, is attached to a cgrp. 2. prog B uses BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE and calls the bpf_get_local_storage helper. 3. link_update is called to replace prog A with prog B. The reason is that __cgroup_bpf_replace fails to alloc and assign the required cgrp storage for the incoming replacement prog. Consequently, the new prog inherits an uninit storage, leading to null-ptr-deref panic when kick the new prog. Fix this by properly allocating the storage and comparing the old and new storage pointers. If the storage changed, fallback to update_effective_progs which performs a RCU-safe update of the entire array. If the storage remains unchanged, we can safely retain the fast-path in-place update. Additionally, handle the error path in __cgroup_bpf_attach strictly. Although it is rare for update_effective_progs to fail in this context, proper rollbacks for storage and flags are added for code rigor. Fixes: 0c991ebc8c69 ("bpf: Implement bpf_prog replacement for an active bpf= _cgroup_link") Reviewed-by: Amery Hung Signed-off-by: Pu Lehui --- kernel/bpf/cgroup.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 4355ccb78a9c..56d538f05520 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -813,10 +813,12 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *old_prog =3D NULL; struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] =3D {}; struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] =3D {= }; + struct bpf_cgroup_storage *old_storage[MAX_BPF_CGROUP_STORAGE_TYPE] =3D {= }; struct bpf_prog *new_prog =3D prog ? : link->link.prog; enum cgroup_bpf_attach_type atype; struct bpf_prog_list *pl; struct hlist_head *progs; + u8 old_flags; int err; =20 if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) || @@ -883,7 +885,10 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp, pl->prog =3D prog; pl->link =3D link; pl->flags =3D flags; + if (old_prog) + bpf_cgroup_storages_assign(old_storage, pl->storage); bpf_cgroup_storages_assign(pl->storage, storage); + old_flags =3D cgrp->bpf.flags[atype]; cgrp->bpf.flags[atype] =3D saved_flags; =20 if (type =3D=3D BPF_LSM_CGROUP) { @@ -915,12 +920,14 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp, if (old_prog) { pl->prog =3D old_prog; pl->link =3D NULL; + bpf_cgroup_storages_assign(pl->storage, old_storage); } bpf_cgroup_storages_free(new_storage); if (!old_prog) { hlist_del(&pl->node); kfree(pl); } + cgrp->bpf.flags[atype] =3D old_flags; return err; } =20 @@ -1032,11 +1039,17 @@ static int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link, struct bpf_prog *new_prog) { + struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] =3D {= }; + struct bpf_cgroup_storage *old_storage[MAX_BPF_CGROUP_STORAGE_TYPE] =3D {= }; + struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] =3D {}; + enum bpf_cgroup_storage_type stype; enum cgroup_bpf_attach_type atype; + bool storage_changed =3D false; struct bpf_prog *old_prog; struct bpf_prog_list *pl; struct hlist_head *progs; bool found =3D false; + int err; =20 atype =3D bpf_cgroup_atype_find(link->link.attach_type, new_prog->aux->at= tach_btf_id); if (atype < 0) @@ -1056,10 +1069,39 @@ static int __cgroup_bpf_replace(struct cgroup *cgrp, if (!found) return -ENOENT; =20 + if (bpf_cgroup_storages_alloc(storage, new_storage, link->link.attach_typ= e, + new_prog, cgrp)) + return -ENOMEM; + + for_each_cgroup_storage_type(stype) { + if (storage[stype] !=3D pl->storage[stype]) { + storage_changed =3D true; + break; + } + } + cgrp->bpf.revisions[atype] +=3D 1; old_prog =3D xchg(&link->link.prog, new_prog); - replace_effective_prog(cgrp, atype, pl); + + if (!storage_changed) { + replace_effective_prog(cgrp, atype, pl); + bpf_prog_put(old_prog); + return 0; + } + + bpf_cgroup_storages_assign(old_storage, pl->storage); + bpf_cgroup_storages_assign(pl->storage, storage); + err =3D update_effective_progs(cgrp, atype); + if (err) { + xchg(&link->link.prog, old_prog); + bpf_cgroup_storages_assign(pl->storage, old_storage); + bpf_cgroup_storages_free(new_storage); + cgrp->bpf.revisions[atype] -=3D 1; + return err; + } + bpf_prog_put(old_prog); + bpf_cgroup_storages_link(new_storage, cgrp, link->link.attach_type); return 0; } =20 --=20 2.34.1