mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Weiming Shi <bestswngs@gmail.com>
To: 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>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	Stanislav Fomichev <sdf@fomichev.me>,
	co+e4b67bb653c9bb8b@bugs.sh, Xiang Mei <xmei5@asu.edu>,
	Weiming Shi <bestswngs@gmail.com>
Subject: [PATCH bpf] bpf: reject dev-bound programs on unregistering devices
Date: Thu, 17 Sep 2026 01:29:45 +0800	[thread overview]
Message-ID: <20260916172944.1337289-2-bestswngs@gmail.com> (raw)

bpf_prog_dev_bound_init() takes a reference to the target netdev with
dev_get_by_index(), then initializes the BPF offload state later under
bpf_devs_lock. If unregister_netdevice_many_notify() runs in between,
bpf_dev_bound_netdev_unregister() can observe that no bound-only ondev
exists yet and return. The loader can then create a new bound-only ondev
for a device whose unregister path has already passed the BPF cleanup
point, leaving prog->aux->offload->netdev dangling after dev_put().

Reject new dev-bound state once the device has left NETREG_REGISTERED.
The unregister path sets NETREG_UNREGISTERING before BPF cleanup, and
both initialization and cleanup hold bpf_devs_lock, so no ondev can be
created after cleanup has passed.

The race was reproduced without kernel source instrumentation by using a
tracing BPF fexit program to signal and widen the interval immediately
after dev_get_by_index() returns. KASAN reported:

  BUG: KASAN: slab-use-after-free in bpf_dev_bound_resolve_kfunc (kernel/bpf/offload.c:846)
  Read of size 8
  bpf_dev_bound_resolve_kfunc (kernel/bpf/offload.c:846)
  bpf_fixup_kfunc_call (kernel/bpf/verifier.c:20595)
  bpf_do_misc_fixups (kernel/bpf/fixups.c:1888)
  bpf_check (kernel/bpf/verifier.c:21201)
  bpf_prog_load (kernel/bpf/syscall.c:3133)
  __sys_bpf (kernel/bpf/syscall.c:6367)

The object was freed concurrently through:

  netdev_run_todo (net/core/dev.c:11818)
  rtnl_dellink (net/core/rtnetlink.c:3702)

Fixes: 2b3486bc2d23 ("bpf: Introduce device-bound XDP programs")
Reported-by: <co+e4b67bb653c9bb8b@bugs.sh>
Assisted-by: LLM
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
---
Please queue this fix for stable kernels.

 kernel/bpf/offload.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/bpf/offload.c b/kernel/bpf/offload.c
index 0d6f5569588c3..5e05517d06c7c 100644
--- a/kernel/bpf/offload.c
+++ b/kernel/bpf/offload.c
@@ -182,6 +182,9 @@ static int __bpf_prog_dev_bound_init(struct bpf_prog *prog, struct net_device *n
 	struct bpf_prog_offload *offload;
 	int err;
 
+	if (READ_ONCE(netdev->reg_state) != NETREG_REGISTERED)
+		return -ENODEV;
+
 	offload = kzalloc_obj(*offload, GFP_USER);
 	if (!offload)
 		return -ENOMEM;
-- 
2.55.0


                 reply	other threads:[~2026-09-16 17:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260916172944.1337289-2-bestswngs@gmail.com \
    --to=bestswngs@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=co+e4b67bb653c9bb8b@bugs.sh \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=xmei5@asu.edu \
    --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®