Skip to contentSkip to conversation

net/sched: cls_bpf: reject dev-bound programs bound to a different device

PATCH Applied Jamal Hadi Salim 5 participants 8 messages Aug 9 – Aug 13

Subsystems (6)
lore
Share & tools
lore

Jamal Hadi Salim

author maintainer
#0 lore raw

PATCH 1/1 net/sched: cls_bpf: reject dev-bound programs bound to a different device

cls_bpf_prog_from_efd() obtained a SCHED_CLS program via bpf_prog_get_type_dev() but never verified that a device-bound (offloaded) program's bound netdev matches the TC netdev the classifier is being attached to. This let a program loaded with prog_ifindex for device A be attached via cls_bpf + skip_sw to device B; deleting device A then destroyed the program's offload state while it was still attached to device B, triggering a netdevsim WARN (panic with panic_on_warn=1).

Mirror the XDP attach path (net/core/dev.c) and reject the attach with -EINVAL when a dev-bound program's bound device does not match the target device.

Fixes 6c8dfe21c435 ("cls_bpf: allow attaching programs loaded for specific device")
Reported-by [email protected]
---
 net/sched/cls_bpf.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
net/sched/cls_bpf.c +16 -2
index 001d8c4ebfed..6d19155becc8 100644
@@ -374,7 +374,8 @@ static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
 }
 
 static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
-				 u32 gen_flags, const struct tcf_proto *tp)
+				 u32 gen_flags, const struct tcf_proto *tp,
+				 struct netlink_ext_ack *extack)
 {
 	struct bpf_prog *fp;
 	char *name = NULL;
@@ -388,6 +389,19 @@ static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
 	if (IS_ERR(fp))
 		return PTR_ERR(fp);
 
+	if (bpf_prog_is_dev_bound(fp->aux)) {
+		struct tcf_block *block = tp->chain->block;
+		struct net_device *dev;
+
+		dev = block->q ? qdisc_dev(block->q) : NULL;
+		if (!dev || !bpf_offload_dev_match(fp, dev)) {
+			NL_SET_ERR_MSG(extack,
+				       "Program is bound to a different device");
+			bpf_prog_put(fp);
+			return -EINVAL;
+		}
+	}
+
 	if (tb[TCA_BPF_NAME]) {
 		name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL);
 		if (!name) {
@@ -492,7 +506,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
 	prog->gen_flags = gen_flags;
 
 	ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
-		cls_bpf_prog_from_efd(tb, prog, gen_flags, tp);
+		cls_bpf_prog_from_efd(tb, prog, gen_flags, tp, extack);
 	if (ret < 0)
 		goto errout_idr;
 
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jamal Hadi Salim

cls_bpf_prog_from_efd() obtained a SCHED_CLS program via bpf_prog_get_type_dev() but never verified that a device-bound (offloaded) program's bound netdev matches the TC netdev the classifier is being attached to. This let a program loaded with prog_ifindex for device A be attached via cls_bpf + skip_sw to device B; deleting device A then destroyed the program's offload state while it was still attached to device B, triggering a netdevsim WARN (panic with panic_on_warn=1).

Quoted 8 lines

Mirror the XDP attach path (net/core/dev.c) and reject the attach with -EINVAL when a dev-bound program's bound device does not match the target device.

Fixes 6c8dfe21c435 ("cls_bpf: allow attaching programs loaded for specific device")
Reported-by [email protected]
maintainer
#2 lore raw
In reply to Jamal Hadi Salim
Quoted 7 lines · On Sun, 9 Aug 2026 05:44:18 -0400 Jamal Hadi Salim wrote:

cls_bpf_prog_from_efd() obtained a SCHED_CLS program via bpf_prog_get_type_dev() but never verified that a device-bound (offloaded) program's bound netdev matches the TC netdev the classifier is being attached to. This let a program loaded with prog_ifindex for device A be attached via cls_bpf + skip_sw to device B; deleting device A then destroyed the program's offload state while it was still attached to device B, triggering a netdevsim WARN (panic with panic_on_warn=1).

maybe netdevsim has a bug then.

Quoted 5 lines

Mirror the XDP attach path (net/core/dev.c) and reject the attach with -EINVAL when a dev-bound program's bound device does not match the target device.

Fixes 6c8dfe21c435 ("cls_bpf: allow attaching programs loaded for specific device")

This commit in itself is fine, nfp checks that the offload matches:
https://elixir.bootlin.com/linux/v7.2-rc5/source/drivers/net/ethernet/netronome/nfp/bpf/offload.c#L579

Maybe the bound-devs got extended for JIT / descriptor access, and that added some extra risk here. So either this is netdevsim-only (and not worth the Fixes tag), or the Fixes tag is wrong..

author maintainer
#3 lore raw
Quoted 10 lines · Jakub Kicinski
Quoted 7 lines · On Sun, 9 Aug 2026 05:44:18 -0400 Jamal Hadi Salim wrote:

cls_bpf_prog_from_efd() obtained a SCHED_CLS program via bpf_prog_get_type_dev() but never verified that a device-bound (offloaded) program's bound netdev matches the TC netdev the classifier is being attached to. This let a program loaded with prog_ifindex for device A be attached via cls_bpf + skip_sw to device B; deleting device A then destroyed the program's offload state while it was still attached to device B, triggering a netdevsim WARN (panic with panic_on_warn=1).

Quoted 2 lines

maybe netdevsim has a bug then.

Looking closely: You're right. nfp guards at the driver (bpf_offload_dev_match() at
while netdevsim's cls_bpf path checks boundness, not the device match.

Quoted 4 lines
Quoted 4 lines

Mirror the XDP attach path (net/core/dev.c) and reject the attach with -EINVAL when a dev-bound program's bound device does not match the target device.

Quoted 8 lines
Quoted 1 line
Fixes 6c8dfe21c435 ("cls_bpf: allow attaching programs loaded for specific device")

This commit in itself is fine, nfp checks that the offload matches:
https://elixir.bootlin.com/linux/v7.2-rc5/source/drivers/net/ethernet/netronome/nfp/bpf/offload.c#L579

Maybe the bound-devs got extended for JIT / descriptor access, and that added some extra risk here. So either this is netdevsim-only (and not worth the Fixes tag), or the Fixes tag is wrong..

Fixes tag is definetely wrong. 6c8dfe21c435 was fine for nfp; the semantic shift happened from Stan's fix in 2b3486bc2d23 (made prog->aux->offload non-NULL for dev-bound progs). c0c6bde586c7 then fixed netdevsim XDP but missed cls_bpf.

It seems a v2 may be worth it:
fix netdevsim's cls_bpf path (Fixes: 2b3486bc2d23), keep the core check as defense-in-depth without the bogus Fixes:, and walk block->ports for shared blocks (block->q is NULL there). Or drop the core change and fix only netdevsim. What says you?

cheers,
jamal

maintainer
#4 lore raw
Quoted 5 lines · On Tue, 11 Aug 2026 07:35:41 -0400 Jamal Hadi Salim wrote:

It seems a v2 may be worth it:
fix netdevsim's cls_bpf path (Fixes: 2b3486bc2d23), keep the core check as defense-in-depth without the bogus Fixes:, and walk block->ports for shared blocks (block->q is NULL there). Or drop the core change and fix only netdevsim. What says you?

Pls post the right Fixes tag in this thread (without wrapping etc) my scripts will pick that up when applying.

re: blocks - don't care, those are a switch ACL feature to me. for BPF offload - the offload mechanism itself already works at the device level, so the program can be loaded once and then bound to multiple ports. Or at least that's what I _think_ I did cause NFP wouldn't have enough program storage for all the ports it had :)

author maintainer
#5 lore raw
In reply to Jamal Hadi Salim
Quoted 9 lines · Jamal Hadi Salim

cls_bpf_prog_from_efd() obtained a SCHED_CLS program via bpf_prog_get_type_dev() but never verified that a device-bound (offloaded) program's bound netdev matches the TC netdev the classifier is being attached to. This let a program loaded with prog_ifindex for device A be attached via cls_bpf + skip_sw to device B; deleting device A then destroyed the program's offload state while it was still attached to device B, triggering a netdevsim WARN (panic with panic_on_warn=1).

Quoted 5 lines

Mirror the XDP attach path (net/core/dev.c) and reject the attach with -EINVAL when a dev-bound program's bound device does not match the target device.

Fixes 6c8dfe21c435 ("cls_bpf: allow attaching programs loaded for specific device")
Fixes 2b3486bc2d23 ("bpf: Introduce device-bound XDP programs")

cheers,
jamal

Quoted 53 lines
---
 net/sched/cls_bpf.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
net/sched/cls_bpf.c +16 -3
index 001d8c4ebfed..6d19155becc8 100644
@@ -374,7 +374,8 @@ static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
 }
 
 static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
-                                u32 gen_flags, const struct tcf_proto *tp)
+                                u32 gen_flags, const struct tcf_proto *tp,
+                                struct netlink_ext_ack *extack)
 {
        struct bpf_prog *fp;
        char *name = NULL;
@@ -388,6 +389,19 @@ static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
        if (IS_ERR(fp))
                return PTR_ERR(fp);
 
+       if (bpf_prog_is_dev_bound(fp->aux)) {
+               struct tcf_block *block = tp->chain->block;
+               struct net_device *dev;
+
+               dev = block->q ? qdisc_dev(block->q) : NULL;
+               if (!dev || !bpf_offload_dev_match(fp, dev)) {
+                       NL_SET_ERR_MSG(extack,
+                                      "Program is bound to a different device");
+                       bpf_prog_put(fp);
+                       return -EINVAL;
+               }
+       }
+
        if (tb[TCA_BPF_NAME]) {
                name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL);
                if (!name) {
@@ -492,7 +506,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
        prog->gen_flags = gen_flags;
 
        ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
-               cls_bpf_prog_from_efd(tb, prog, gen_flags, tp);
+               cls_bpf_prog_from_efd(tb, prog, gen_flags, tp, extack);
        if (ret < 0)
                goto errout_idr;
 
--

2.34.1

maintainer
#6 lore raw
In reply to Jamal Hadi Salim
Quoted 8 lines · Jamal Hadi Salim

cls_bpf_prog_from_efd() obtained a SCHED_CLS program via bpf_prog_get_type_dev() but never verified that a device-bound (offloaded) program's bound netdev matches the TC netdev the classifier is being attached to. This let a program loaded with prog_ifindex for device A be attached via cls_bpf + skip_sw to device B; deleting device A then destroyed the program's offload state while it was still attached to device B, triggering a netdevsim WARN (panic with panic_on_warn=1).

Quoted 8 lines

Mirror the XDP attach path (net/core/dev.c) and reject the attach with -EINVAL when a dev-bound program's bound device does not match the target device.

Fixes 6c8dfe21c435 ("cls_bpf: allow attaching programs loaded for specific device")
Reported-by [email protected]

I understand that Jakub prefers going with this patch (with a correct fixes tag), so waiting for such tag landing here ;)

/P

#7 lore raw
In reply to Jamal Hadi Salim
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Sun, 9 Aug 2026 05:44:18 -0400 you wrote:

cls_bpf_prog_from_efd() obtained a SCHED_CLS program via bpf_prog_get_type_dev() but never verified that a device-bound (offloaded) program's bound netdev matches the TC netdev the classifier is being attached to. This let a program loaded with prog_ifindex for device A be attached via cls_bpf + skip_sw to device B; deleting device A then destroyed the program's offload state while it was still attached to device B, triggering a netdevsim WARN (panic with panic_on_warn=1).

[...]

Here is the summary with links:
- [net] net/sched: cls_bpf: reject dev-bound programs bound to a different device

    https://git.kernel.org/netdev/net/c/120977e2c096

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html