mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pu Lehui <pulehui@huawei.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Pu Lehui <pulehui@huaweicloud.com>
Cc: <bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	Amery Hung <ameryhung@gmail.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>,
	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>
Subject: Re: [PATCH bpf v6 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog
Date: Tue, 28 Jul 2026 09:44:33 +0800	[thread overview]
Message-ID: <0646cb86-45dc-42bc-a381-07688b822acc@huawei.com> (raw)
In-Reply-To: <CAEf4BzZQo9vidTkr9kORbft9Lhx56JJujWQit40xem40TjSS1A@mail.gmail.com>



On 2026/7/28 7:01, Andrii Nakryiko wrote:
> On Wed, Jul 22, 2026 at 12:19 AM Pu Lehui <pulehui@huaweicloud.com> 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>
>> Reviewed-by: Amery Hung <ameryhung@gmail.com>
>> Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
>> ---
>>   kernel/bpf/net_namespace.c | 14 +++++++++-----
>>   1 file changed, 9 insertions(+), 5 deletions(-)
>>
> 
> the change is good, all other link types do take lock first and then
> do all the checking, netns is the only oddball, but see implementation
> nit below
> 
>> 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;
> 
> can we use guard() and avoid all the goto jumping?

ok, will update next

> 
> pw-bot: cr
> 
> 
>> +       }
>> +
>>          net = net_link->net;
>>          if (!net || !check_net(net)) {
>>                  /* Link auto-detached or netns dying */
>> --
>> 2.34.1
>>

  reply	other threads:[~2026-07-28  1:44 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22  7:23 [PATCH bpf v6 0/4] Fixes for bpf link update Pu Lehui
2026-07-22  7:23 ` [PATCH bpf v6 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog Pu Lehui
2026-07-27 23:01   ` Andrii Nakryiko
2026-07-28  1:44     ` Pu Lehui [this message]
2026-07-22  7:23 ` [PATCH bpf v6 2/4] bpf: Fix UAF due to missing link type check in mprog Pu Lehui
2026-07-22 16:25   ` Emil Tsalapatis
2026-07-23  3:14     ` Pu Lehui
2026-07-28  1:56       ` Pu Lehui
2026-07-28  5:50         ` Emil Tsalapatis
2026-07-22  7:23 ` [PATCH bpf v6 3/4] bpf: Fix potential UAF when reading bpf link info Pu Lehui
2026-07-27 23:15   ` Andrii Nakryiko
2026-07-28  1:45     ` Pu Lehui
2026-07-22  7:23 ` [PATCH bpf v6 4/4] bpf, cgroup: Fix storage null-ptr-deref after replacing prog Pu Lehui
2026-07-27 23:17 ` [PATCH bpf v6 0/4] Fixes for bpf link update Andrii Nakryiko
2026-07-28  1:54   ` Pu Lehui

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=0646cb86-45dc-42bc-a381-07688b822acc@huawei.com \
    --to=pulehui@huawei.com \
    --cc=ameryhung@gmail.com \
    --cc=andrii.nakryiko@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=mykyta.yatsenko5@gmail.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

all inboxes | Powered by JetHome®