From: Amery Hung <ameryhung@gmail.com>
To: Pu Lehui <pulehui@huaweicloud.com>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Yonghong Song <yonghong.song@linux.dev>,
Song Liu <song@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Pu Lehui <pulehui@huawei.com>
Subject: Re: [PATCH bpf v4 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog
Date: Mon, 20 Jul 2026 10:33:50 -0700 [thread overview]
Message-ID: <4f264a16-bf79-4033-9196-603c168406e1@gmail.com> (raw)
In-Reply-To: <20260720134547.1289964-2-pulehui@huaweicloud.com>
On 7/20/26 6:45 AM, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
>
> 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 != link->prog)
> return -EPERM;
> bpf_netns_link_update_prog
> if (old_prog && old_prog != link->prog)
> ...
> old_prog = xchg(&link->prog, new_prog);
> bpf_prog_put(old_prog);
> if (new_prog->type != 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 <sashiko-bot@kernel.org>
> Signed-off-by: Pu Lehui <pulehui@huawei.com>
Reviewed-by: Amery Hung <ameryhung@gmail.com>
> ---
> 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;
>
> - if (old_prog && old_prog != link->prog)
> - return -EPERM;
> - if (new_prog->type != link->prog->type)
> - return -EINVAL;
> -
> mutex_lock(&netns_bpf_mutex);
>
> + if (old_prog && old_prog != link->prog) {
> + ret = -EPERM;
> + goto out_unlock;
> + }
> + if (new_prog->type != link->prog->type) {
> + ret = -EINVAL;
> + goto out_unlock;
> + }
> +
> net = net_link->net;
> if (!net || !check_net(net)) {
> /* Link auto-detached or netns dying */
next prev parent reply other threads:[~2026-07-20 17:33 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-20 13:45 [PATCH bpf v4 0/4] Fixes for bpf link update Pu Lehui
2026-07-20 13:45 ` [PATCH bpf v4 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog Pu Lehui
2026-07-20 17:33 ` Amery Hung [this message]
2026-07-20 13:45 ` [PATCH bpf v4 2/4] bpf: Fix UAF due to missing link type check in mprog Pu Lehui
2026-07-20 18:44 ` Amery Hung
2026-07-21 3:39 ` Pu Lehui
2026-07-20 13:45 ` [PATCH bpf v4 3/4] bpf: Fix potential UAF when reading bpf link info Pu Lehui
2026-07-20 14:16 ` Mykyta Yatsenko
2026-07-21 3:38 ` Pu Lehui
2026-07-20 13:45 ` [PATCH bpf v4 4/4] bpf, cgroup: Fix storage null-ptr-deref after replacing prog Pu Lehui
2026-07-20 18:58 ` Amery Hung
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4f264a16-bf79-4033-9196-603c168406e1@gmail.com \
--to=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=pulehui@huawei.com \
--cc=pulehui@huaweicloud.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome