BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pu Lehui" <pulehui@huaweicloud.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf v4 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog
Date: Mon, 20 Jul 2026 13:55:23 +0000	[thread overview]
Message-ID: <20260720135523.AB3461F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260720134547.1289964-2-pulehui@huaweicloud.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Lockless access to `link->prog` in generic BPF link info retrieval functions and mprog logic can lead to a Use-After-Free when an updateable link replaces its program concurrently.
--

--- Patch [1]: [PATCH bpf v4 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog ---
commit 1e9631860a41f1ee87e0b9afa5e2e3ca3f35147e
Author: Pu Lehui <pulehui@huawei.com>

bpf: Fix potential UAF in bpf_netns_link_update_prog
    
This commit fixes a potential use-after-free race condition in
bpf_netns_link_update_prog. By moving the old_prog and prog->type checks
inside the netns_bpf_mutex critical section, it prevents concurrent
BPF_LINK_UPDATE operations from freeing the program while it is being accessed.

> diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c
> index 25f30f9edaefd..9fc62db1441c6 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;
> +	}
> +

[Severity: High]
This isn't a bug introduced by this patch, but while looking at the lockless
accesses of link->prog being fixed here, I noticed other places that might
have similar race conditions.

For example, in kernel/bpf/syscall.c, bpf_link_show_fdinfo() reads link->prog
locklessly:

kernel/bpf/syscall.c:bpf_link_show_fdinfo() {
    ...
    const struct bpf_prog *prog = link->prog;
    ...
    if (prog) {
        bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
    ...
}

Similarly, bpf_link_get_info_by_fd() in the same file accesses
link->prog->aux->id locklessly, and bpf_mprog_link() in kernel/bpf/mprog.c
checks link->prog->type without locks.

If a user-space thread calls one of these info retrieval functions and is
preempted, could a concurrent BPF_LINK_UPDATE (like tcx_link_update or
the one being fixed in this patch) swap out link->prog via xchg() and free
it?

Since the reading threads do not appear to hold rcu_read_lock() or take a
reference on the program, it seems the RCU grace period could complete,
resulting in a use-after-free when they resume and dereference prog->tag or
prog->aux->id.

Should we add RCU read locks or reference counting around these generic info
retrieval paths to prevent the program from being freed out from under them?

>  	net = net_link->net;
>  	if (!net || !check_net(net)) {
>  		/* Link auto-detached or netns dying */

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260720134547.1289964-1-pulehui@huaweicloud.com?part=1

  reply	other threads:[~2026-07-20 13:55 UTC|newest]

Thread overview: 13+ 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 13:55   ` sashiko-bot [this message]
2026-07-21  3:41     ` Pu Lehui
2026-07-20 17:33   ` Amery Hung
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=20260720135523.AB3461F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=pulehui@huaweicloud.com \
    --cc=sashiko-reviews@lists.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