bpf: prevent offloaded programs from running on host via tcx/netkit
Subsystems (5)
v2 cover bpf: prevent offloaded programs from running on host via tcx/netkit
Yinhao reported a splat [1] when attaching a BPF program loaded with prog_ifindex (targeted at an offload-capable device such as netdevsim) to the software path via BPF_TCX_EGRESS. The program's bpf_func had already been replaced by bpf_prog_warn_on_exec() during offload compile, so the first packet that reaches tcx_run() trips the WARN:
[ 19.592982] ------------[ cut here ]------------ [ 19.594654] attempt to execute device eBPF program on the host! [ 19.594659] WARNING: kernel/bpf/offload.c:420 at 0x0, CPU#0: poc/337 [ 19.599906] Modules linked in: [ 19.600680] CPU: 0 UID: 0 PID: 337 Comm: poc Not tainted 6.18.0-rc7-next-20251125 #10 PREEMPT(none) [ 19.601659] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 19.602684] RIP: 0010:bpf_prog_warn_on_exec+0xc/0x20 [ 19.603241] Code: 28 00 48 89 ef e8 74 44 2f 00 eb d7 66 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 48 8d 3d a4 eb 95 06 <67> 48 0f b9 3a 31 c0 e9 83 76 44 ff 0f 1f 84 00 00 00 00 00 90 90 [ 19.605093] RSP: 0018:ffff8881066e73d8 EFLAGS: 00010246 [ 19.605663] RAX: ffffffff81cbca70 RBX: ffff8881013c4210 RCX: 0000000000000004 [ 19.606378] RDX: 1ffff11020278842 RSI: ffffc90000563060 RDI: ffffffff8861b620 [ 19.607107] RBP: ffff8881010d0640 R08: ffff8881013c4210 R09: ffff8881010d06b0 [ 19.607827] R10: ffff8881010d06c3 R11: ffffc90000563000 R12: ffffc90000563000 [ 19.608751] R13: ffff8881010d06b4 R14: ffff888115eb1a34 R15: dffffc0000000000 [ 19.609478] FS: 000000000294c380(0000) GS:ffff8881911e9000(0000) knlGS:0000000000000000 [ 19.610316] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 19.610943] CR2: 000057f6b9eb38c0 CR3: 00000001010ea000 CR4: 0000000000750ef0 [ 19.611712] PKRU: 55555554 [ 19.612006] Call Trace: [ 19.612281] <TASK> [ 19.612523] __dev_queue_xmit+0x22cb/0x3530 [ 19.617607] ip_finish_output2+0x621/0x1a60 [ 19.621371] ip_output+0x170/0x2e0 [ 19.624586] ip_send_skb+0x129/0x180 [ 19.624940] udp_send_skb+0x65d/0x1300 [ 19.625316] udp_sendmsg+0x13bf/0x2000 [ 19.629960] __sys_sendto+0x396/0x470 [ 19.633720] __x64_sys_sendto+0xdc/0x1b0 [ 19.635066] do_syscall_64+0x76/0x10a0 [ 19.641701] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 19.642240] RIP: 0033:0x4240d7 [ 19.642597] Code: 00 89 01 e9 c1 fe ff ff e8 f6 03 00 00 66 0f 1f 44 00 00 f3 0f 1e fa 80 3d 8d 3f 09 00 00 41 89 ca 74 10 b8 2c 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 69 c3 55 48 89 e5 53 48 83 ec 38 44 89 4d d0 [ 19.646088] RSP: 002b:00007fffcb9ecb68 EFLAGS: 00000202 ORIG_RAX: 000000000000002c [ 19.648938] RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00000000004240d7 [ 19.652116] RDX: 0000000000000008 RSI: 00007fffcb9ecce0 RDI: 0000000000000005 [ 19.653148] RBP: 00007fffcb9eccf0 R08: 00007fffcb9ecbb0 R09: 0000000000000010 [ 19.653951] R10: 0000000000000000 R11: 0000000000000202 R12: 00007fffcb9ece08 [ 19.654760] R13: 00007fffcb9ece18 R14: 00000000004b2868 R15: 0000000000000001 [ 19.657462] </TASK> [ 19.657703] ---[ end trace 0000000000000000 ]---
The reason is that tcx and netkit never checked for offloaded programs on attach, unlike XDP which already rejects this in dev_xdp_attach(). While auditing the other link entry points, bpf_xdp_link_update() turned out to have the same gap: it calls dev_xdp_install() directly and skips the dev_xdp_attach() offload check, so an XDP link created in SKB/native mode can still be swapped to an offloaded program via BPF_LINK_UPDATE.
This series adds the same guard to all three so offloaded programs can't be attached to (or swapped into) the software path.
v1 -> v2:
- tcx/netkit: also reject offloaded progs in the link update callback (tcx_link_update/netkit_link_update), not just attach; pointed out by the AI review on v1.
- Add patch 3/3 for the same hole in bpf_xdp_link_update().
v1: https://lore.kernel.org/bpf/[email protected]/
[1]: https://lore.kernel.org/bpf/[email protected]/
Jiayuan Chen (3):
bpf, tcx: reject offloaded programs on attach bpf, netkit: reject offloaded programs on attach bpf, xdp: reject offloaded programs on link update
drivers/net/netkit.c | 9 +++++++++ kernel/bpf/tcx.c | 9 +++++++++ net/core/dev.c | 5 +++++ 3 files changed, 23 insertions(+)
Signature · 1 line
2.43.0
PATCH v2 1/3 bpf, tcx: reject offloaded programs on attach
An offloaded prog's bpf_func is replaced by bpf_prog_warn_on_exec(), since it's supposed to run on the NIC, not the host. But tcx doesn't check this and happily attaches it to the software path, so the first packet hits the WARN.
XDP already guards this in dev_xdp_attach(); tcx just never got the same check. Add it to tcx_prog_attach(), tcx_link_attach() and also tcx_link_update() so the fix cannot be bypassed by loading a normal program and then swapping it out via BPF_LINK_UPDATE.
Use bpf_prog_is_offloaded() rather than bpf_prog_is_dev_bound() + bpf_offload_dev_match() (as XDP does): bpf_prog_dev_bound_init() already rejects BPF_F_XDP_DEV_BOUND_ONLY for BPF_PROG_TYPE_SCHED_CLS, so a dev-bound SCHED_CLS program is always offloaded. The simpler check is sufficient and also rejects attaching a program offloaded to device A onto device B.
--- kernel/bpf/tcx.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/bpf/tcx.c +9 -0
@@ -16,6 +16,9 @@ int tcx_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)struct net_device *dev;int ret; +if (bpf_prog_is_offloaded (prog->aux)) +return -EINVAL ; +rtnl_lock (); dev =__dev_get_by_index (net, attr->target_ifindex);if (!dev) {
@@ -209,6 +212,9 @@ static int tcx_link_update(struct bpf_link *link, struct bpf_prog *nprog,struct net_device *dev;int ret =0 ; +if (bpf_prog_is_offloaded (nprog->aux)) +return -EINVAL ; +rtnl_lock (); dev = tcx->dev;if (!dev) {
@@ -315,6 +321,9 @@ int tcx_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)struct tcx_link *tcx;int ret; +if (bpf_prog_is_offloaded (prog->aux)) +return -EINVAL ; +rtnl_lock (); dev =__dev_get_by_index (net, attr->link_create.target_ifindex);if (!dev) {
Signature · 1 line
2.43.0
PATCH v2 2/3 bpf, netkit: reject offloaded programs on attach
In reply to Jiayuan ChenSame issue as the tcx fix: netkit accepts SCHED_CLS programs but never checks if they were loaded for hardware offload. If someone loads a program with prog_ifindex pointing to an offload-capable device and then attaches it to a netkit peer, the bpf_func is bpf_prog_warn_on_exec() and the first packet triggers the WARN.
Reject offloaded programs in netkit_prog_attach(), netkit_link_attach() and netkit_link_update().
--- drivers/net/netkit.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/net/netkit.c +9 -0
@@ -533,6 +533,9 @@ int netkit_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)struct net_device *dev;int ret; +if (bpf_prog_is_offloaded (prog->aux)) +return -EINVAL ; +rtnl_lock (); dev =netkit_dev_fetch (current->nsproxy->net_ns, attr->target_ifindex, attr->attach_type);
@@ -683,6 +686,9 @@ static int netkit_link_update(struct bpf_link *link, struct bpf_prog *nprog,struct net_device *dev;int ret =0 ; +if (bpf_prog_is_offloaded (nprog->aux)) +return -EINVAL ; +rtnl_lock (); dev = nkl->dev;if (!dev) {
@@ -788,6 +794,9 @@ int netkit_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)struct net_device *dev;int ret; +if (bpf_prog_is_offloaded (prog->aux)) +return -EINVAL ; +rtnl_lock (); dev = netkit_dev_fetch(current->nsproxy->net_ns, attr->link_create.target_ifindex,
Signature · 1 line
2.43.0
PATCH v2 3/3 bpf, xdp: reject offloaded programs on link update
In reply to Jiayuan ChenSame class of bug as the tcx/netkit fixes: bpf_xdp_link_update() calls dev_xdp_install() directly and bypasses dev_xdp_attach(), so the offload check in dev_xdp_attach() is skipped. A user can create an XDP link in SKB or native mode with a regular program and then replace it via BPF_LINK_UPDATE with an offloaded program, whose bpf_func is bpf_prog_warn_on_exec(), tripping the WARN on the first packet.
Mirror the check from dev_xdp_attach(): reject when the link is not in HW mode and the new program is offloaded.
--- net/core/dev.c | 5 +++++ 1 file changed, 5 insertions(+)
net/core/dev.c +5 -0
@@ -10648,6 +10648,11 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,netdev_lock_ops (xdp_link->dev); mode =dev_xdp_mode (xdp_link->dev, xdp_link->flags); +if (mode !=XDP_MODE_HW &&bpf_prog_is_offloaded (new_prog->aux)) { +netdev_unlock_ops (xdp_link->dev); + err = -EINVAL ; + goto out_unlock; + } bpf_op =dev_xdp_bpf_op (xdp_link->dev, mode); err =dev_xdp_install (xdp_link->dev, mode, bpf_op,NULL , xdp_link->flags, new_prog);
Signature · 1 line
2.43.0
Re: [PATCH bpf v2 1/3] bpf, tcx: reject offloaded programs on attach
In reply to Jiayuan ChenQuoted 32 lines · Jiayuan Chen
An offloaded prog's
bpf_funcis replaced bybpf_prog_warn_on_exec(), since it's supposed to run on the NIC, not the host. But tcx doesn't check this and happily attaches it to the software path, so the first packet hits the WARN.XDP already guards this in
dev_xdp_attach(); tcx just never got the same check. Add it totcx_prog_attach(),tcx_link_attach()and alsotcx_link_update()so the fix cannot be bypassed by loading a normal program and then swapping it out viaBPF_LINK_UPDATE.Use
bpf_prog_is_offloaded()rather thanbpf_prog_is_dev_bound()+bpf_offload_dev_match()(as XDP does):bpf_prog_dev_bound_init()already rejectsBPF_F_XDP_DEV_BOUND_ONLYforBPF_PROG_TYPE_SCHED_CLS, so a dev-boundSCHED_CLSprogram is always offloaded. The simpler check is sufficient and also rejects attaching a program offloaded to device A onto device B.Fixes e420bed025071 ("bpf: Add fd-based tcx multi-prog infra with link support")Reported-by Yinhao Hu <[email protected]>Reported-by Kaiyan Mei <[email protected]>Reported-by Dongliang Mu <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>--- kernel/bpf/tcx.c | 9 +++++++++ 1 file changed, 9 insertions(+)kernel/bpf/tcx.c +0
-0
kernel/bpf/tcx.c int tcx_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 21
Quoted 6 lines
kernel/bpf/tcx.c +3
-0@@ -16,6 +16,9 @@ int tcx_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)struct net_device *dev;int ret; +if (bpf_prog_is_offloaded (prog->aux)) +return -EINVAL ; +
instead of sprinkling the check everywhere do it at the source of the bug.
pw-bot: cr
[email protected]
AI reviewRe: [PATCH bpf v2 1/3] bpf, tcx: reject offloaded programs on attach
In reply to Jiayuan ChenSashiko AI review found 1 potential issue(s): - [Low] The `bpf_prog_is_offloaded()` check added to `tcx_prog_attach()` is unreachable (dead code). --
Sashiko AI review found 1 potential issue(s):
- [Low] The bpf_prog_is_offloaded() check added to tcx_prog_attach() is unreachable (dead code). --
commit 4bde7a6b1e8fdca118910a3c0f4b35254f1c6e42
Author: Jiayuan Chen <[email protected]>
bpf, tcx: reject offloaded programs on attach
This commit adds checks to prevent offloaded BPF programs from being attached via tcx, as they are meant to run on the NIC rather than the host. The guards are added to tcx_prog_attach(), tcx_link_attach(), and tcx_link_update().
kernel/bpf/tcx.c int tcx_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog) · line 21
Quoted 11 lines
kernel/bpf/tcx.c +3
-0@@ -16,6 +16,9 @@ int tcx_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)struct net_device *dev;int ret; +if (bpf_prog_is_offloaded (prog->aux)) +return -EINVAL ; +
Is it possible for an offloaded program to actually reach this check in tcx_prog_attach()?
Looking at the prog attach syscall handler, it acquires the program using bpf_prog_get_type(). This function hardcodes attach_drv = false when calling __bpf_prog_get().
As a result, bpf_prog_get_ok() explicitly rejects any offloaded program with -EINVAL before tcx_prog_attach() is ever called.
While the checks added to tcx_link_attach() and tcx_link_update() lower in this patch appear necessary (since link creation uses bpf_prog_get(), which skips attach-type validation and bypasses bpf_prog_get_ok()), does the caller environment make this specific check in tcx_prog_attach() unreachable?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH bpf v2 3/3] bpf, xdp: reject offloaded programs on link update
In reply to Jiayuan ChenSashiko AI review found 1 potential issue(s): - [Critical] Incomplete validation mirroring in `bpf_xdp_link_update()` allows device-bound programs to bypass security checks, leading to type…
Sashiko AI review found 1 potential issue(s):
- [Critical] Incomplete validation mirroring in bpf_xdp_link_update() allows device-bound programs to bypass security checks, leading to type confusion and memory corruption. --
commit 32f74494cb77525b641fbd462cda3a2dfccc0147
Author: Jiayuan Chen <[email protected]>
bpf, xdp: reject offloaded programs on link update
This commit mirrors the offload check from dev_xdp_attach to bpf_xdp_link_update to reject offloaded programs when the link is not in HW mode. This prevents a user from replacing a regular XDP link with an offloaded program via BPF_LINK_UPDATE.
Quoted 3 lines
net/core/dev.c +0
-0
[ ... ]
net/core/dev.c static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, · line 10681
Quoted 5 lines
net/core/dev.c +1
-0@@ -10678,6 +10678,11 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,netdev_lock_ops (xdp_link->dev); mode =dev_xdp_mode (xdp_link->dev, xdp_link->flags); +if (mode !=XDP_MODE_HW &&bpf_prog_is_offloaded (new_prog->aux)) {
Does this check omit the dev_bound validations that are already present in dev_xdp_attach?
If a program is loaded with BPF_F_XDP_DEV_BOUND_ONLY, it has dev_bound set to true but offload_requested set to false, meaning bpf_prog_is_offloaded will return false. Could such a program bypass this check and be attached to an SKB-mode link, potentially causing the generic XDP runner to execute device-specific kfuncs that cast the generic xdp_md context to driver-specific structures?
Additionally, if the link is in HW mode, this condition evaluates to false and the validation is skipped. Does this allow a program offloaded to a different device to be attached to an HW link on the current device? If so, the driver might receive XDP_SETUP_PROG_HW and incorrectly cast the new program's aux->offload->dev_priv to its own internal structure, resulting in memory corruption.
Quoted 7 lines
net/core/dev.c +4
-0+netdev_unlock_ops (xdp_link->dev); + err = -EINVAL ; + goto out_unlock; + } bpf_op =dev_xdp_bpf_op (xdp_link->dev, mode); err =dev_xdp_install (xdp_link->dev, mode, bpf_op,NULL , xdp_link->flags, new_prog);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3