Skip to contentSkip to conversation

net/sched: clamp skb->queue_mapping before indexing the tx queue

PATCH Superseded Jamal Hadi Salim 2 participants 3 messages Oct 26 – Oct 29

Subsystems (6)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jamal Hadi Salim

author maintainer
#0 lore raw

PATCH v5 1/1 net/sched: clamp skb->queue_mapping before indexing the tx queue

This issue was caught by Sashiko (nipa). A producer-side path cap is already merged (ea4d4b5dddb5); this patch adds the matching consumer-side caps.

The issue:
The tx queue is picked and capped in netdev_core_pick_tx() before the __dev_xmit_skb() qdisc enqueue path. A tc BPF program running inside an egress qdisc (not clsact) can then change __sk_buff->queue_mapping; that store is never re-checked. bpf_convert_ctx_access() bounds it only by NO_QUEUE_MAPPING, not by the device's queue count.

Two consumers read the stale value:

- On dequeue, qdisc_restart() calls netdev_get_tx_queue(), which indexes dev->_tx[]. netdev_get_tx_queue() guards it with just a DEBUG_NET_WARN_ON_ONCE, so sch_direct_xmit() takes the out-of-bounds txq->_xmit_lock and writes txq->xmit_lock_owner past the end of the allocation on non-lltx devices such as ifb. Drivers that index their rings from skb_get_queue_mapping() (ex: ifb) go out of range the same way.

- On enqueue, taprio_enqueue() indexes q->qdiscs[] with the same value; the array is sized dev->num_tx_queues. A duplicating netem on a child qdisc clones the skb and re-enqueues the clone at the taprio root (qdisc_root_bh()) through a direct rootq->enqueue() call, so the value reaches taprio_enqueue() before any dequeue cap runs.

The Fix:
Cap the mapping at the consumers and write the capped value back, so the qdisc's _tx[] lookup, the taprio q->qdiscs[] lookup and any later driver read stay in range.

On the dequeue side the cap folds into the dequeue path instead of a separate walk over the returned list: dequeue_skb() caps the head, and each bulk helper caps the element it appends. netdev_cap_txqueue() already logs a ratelimited notice and selects queue 0.

A TCQ_F_ONETXQUEUE qdisc is bound to a single tx queue: dequeue_skb() uses q->dev_queue for the frozen check, the BQL limit and the trace point, while qdisc_restart() transmits on the skb's queue. Capping an out-of-range mapping to queue 0 would therefore send a per-queue child's skb through a different queue than the one it was dequeued for. On mq, mqprio, taprio and htb offload children, a stopped queue 0 could then park the list in q->gso_skb, and the later wake would schedule queue 0's own child instead of this qdisc. Pin a TCQ_F_ONETXQUEUE mapping to the qdisc's own queue to keep the documented invariant.

That also keeps a bulk list on one queue by construction: every element of a TCQ_F_ONETXQUEUE qdisc, including a follower changed by a filter, is pinned to q->dev_queue before it is appended. The fast bulk helper therefore needs no head comparison and no skb_bad_txq requeue; the slow helper, used only by non-ONETXQUEUE qdiscs, still compares and requeues a mismatched follower, where a wake on any queue reschedules q.

On the enqueue side taprio_enqueue() caps the mapping against real_num_tx_queues and writes it back before selecting q->qdiscs[queue], so a duplicating child cannot steer the clone out of the array.

Conditions to recreate the bug:
The dequeue path: load a SCHED_CLS or SCHED_ACT program that stores __sk_buff->queue_mapping (a tc BPF action is enough), attach it to a transmit qdisc (for example a matchall filter on a prio root), and send a packet. With a 1-queue device a store of 1 indexes one past dev->_tx[]. Under KASAN with panic_on_warn=1 the unfixed kernel faults with "BUG:

KASAN: slab-out-of-bounds in sch_direct_xmit". Loading the program needs

CAP_BPF/CAP_SYS_ADMIN, so the trigger is root.

The enqueue path: on a multi-queue device, install a software taprio root, graft a prio child with a duplicating netem on every class, and store an out-of-range queue_mapping from a matchall+bpf filter on the prio. The duplicate is re-enqueued at the taprio root and indexes q->qdiscs[] past dev->num_tx_queues; the unfixed kernel faults in taprio_enqueue_one(). Same CAP_BPF/CAP_SYS_ADMIN requirement.

---
v4 -> v5:
- Fold in the enqueue-side consumer: taprio_enqueue() indexes q->qdiscs[] with skb_get_queue_mapping(), and a duplicating netem re-enqueues the clone at the taprio root before any dequeue cap can run. Cap the mapping against real_num_tx_queues before the lookup. Reported by Sashiko (nipa).
- Call out the TCQ_F_ONETXQUEUE steering change for stable users: an in-range mapping set by a filter on a per-queue child is pinned to the child's own queue, restoring the documented invariant. Reported by Sashiko (nipa).

v3 -> v4:
- Cap a TCQ_F_ONETXQUEUE mapping to the qdisc's own tx queue instead of queue 0, so a per-queue child of mq, mqprio, taprio or htb offload keeps the documented invariant between q->dev_queue and the transmitted queue. Reported by Sashiko (nipa).
- With every element pinned to q->dev_queue, the fast bulk helper no longer needs a head comparison or a skb_bad_txq requeue; a mismatched follower there could otherwise be stranded behind a stopped queue that cannot reschedule this qdisc. Reported by Sashiko (nipa).

v2 -> v3:
- Considered Comments from Simon Horman (hunk counts did not match the diff content; patch did not apply):
https://lore.kernel.org/netdev/[email protected]/

v1 -> v2:
- try_bulk_dequeue_skb(): the fast bulk helper (TCQ_F_ONETXQUEUE) capped each follower but appended it without comparing it against the head, so a mismatched follower was still sent under the head's txq lock. Compare the capped follower with the head and requeue a mismatch to skb_bad_txq, matching try_bulk_dequeue_skb_slow(). Reported by Sashiko (nipa).

---
 net/sched/sch_generic.c | 41 ++++++++++++++++++++++++++++++++++++-----
 net/sched/sch_taprio.c  |  5 +++++
 2 files changed, 41 insertions(+), 5 deletions(-)
net/sched/sch_generic.c +36 -5
index 6f6a6f0d5eb0..5dc1d47f811a 100644
@@ -96,9 +96,34 @@ static void qdisc_maybe_clear_missed(struct Qdisc *q,
 
 #define SKB_XOFF_MAGIC ((struct sk_buff *)1UL)
 
+/* Re-cap a stale queue_mapping; an ONETXQUEUE qdisc pins q->dev_queue. */
+static u16 qdisc_cap_skb_tx_queue(struct Qdisc *q, struct sk_buff *skb)
+{
+	u16 queue = skb_get_queue_mapping(skb);
+	struct net_device *dev = qdisc_dev(q);
+	u16 capped;
+
+	if (q->flags & TCQ_F_ONETXQUEUE)
+		capped = q->dev_queue - netdev_get_tx_queue(dev, 0);
+	else
+		capped = netdev_cap_txqueue(dev, queue);
+
+	if (unlikely(capped != queue))
+		skb_set_queue_mapping(skb, capped);
+
+	return capped;
+}
+
+static struct netdev_queue *skb_cap_tx_queue(struct Qdisc *q,
+					     struct sk_buff *skb)
+{
+	return netdev_get_tx_queue(qdisc_dev(q),
+				   qdisc_cap_skb_tx_queue(q, skb));
+}
+
 static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
 {
-	const struct netdev_queue *txq = q->dev_queue;
+	const struct netdev_queue *txq;
 	spinlock_t *lock = NULL;
 	struct sk_buff *skb;
 
@@ -110,7 +135,8 @@ static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
 	skb = skb_peek(&q->skb_bad_txq);
 	if (skb) {
 		/* check the reason of requeuing without tx lock first */
-		txq = skb_get_tx_queue(txq->dev, skb);
+		/* re-cap: real_num_tx_queues may have shrunk */
+		txq = skb_cap_tx_queue(q, skb);
 		if (!netif_xmit_frozen_or_stopped(txq)) {
 			skb = __skb_dequeue(&q->skb_bad_txq);
 			if (qdisc_is_percpu_stats(q)) {
@@ -216,6 +242,8 @@ static void try_bulk_dequeue_skb(struct Qdisc *q,
 		if (!nskb)
 			break;
 
+		/* Every element of an ONETXQUEUE qdisc is pinned to q->dev_queue. */
+		qdisc_cap_skb_tx_queue(q, nskb);
 		bytelimit -= nskb->len; /* covers GSO len */
 		skb->next = nskb;
 		skb = nskb;
@@ -233,7 +261,6 @@ static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
 				      struct sk_buff *skb,
 				      int *packets)
 {
-	int mapping = skb_get_queue_mapping(skb);
 	struct sk_buff *nskb;
 	int cnt = 0;
 
@@ -241,7 +268,9 @@ static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
 		nskb = q->dequeue(q);
 		if (!nskb)
 			break;
-		if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
+		/* A tc BPF store may have changed the mapping; cap it. */
+		if (unlikely(qdisc_cap_skb_tx_queue(q, nskb) !=
+			     skb_get_queue_mapping(skb))) {
 			qdisc_enqueue_skb_bad_txq(q, nskb);
 			break;
 		}
@@ -286,7 +315,7 @@ static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
 		if (xfrm_offload(skb))
 			*validate = true;
 		/* check the reason of requeuing without tx lock first */
-		txq = skb_get_tx_queue(txq->dev, skb);
+		txq = skb_cap_tx_queue(q, skb);
 		if (!netif_xmit_frozen_or_stopped(txq)) {
 			skb = __skb_dequeue(&q->gso_skb);
 			if (qdisc_is_percpu_stats(q)) {
@@ -322,6 +351,8 @@ static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
 	skb = q->dequeue(q);
 	if (skb) {
 bulk:
+		/* cap the head; the bulk helpers cap every appended element */
+		qdisc_cap_skb_tx_queue(q, skb);
 		if (qdisc_may_bulk(q))
 			try_bulk_dequeue_skb(q, skb, txq, packets, budget);
 		else
net/sched/sch_taprio.c +5 -0
index 39ac5b97aa3a..0eb0803047a1 100644
@@ -632,6 +632,11 @@ static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	int queue;
 
 	queue = skb_get_queue_mapping(skb);
+	if (unlikely(queue >= qdisc_dev(sch)->real_num_tx_queues)) {
+		/* A tc BPF program may have stored an out-of-range mapping. */
+		queue = netdev_cap_txqueue(qdisc_dev(sch), queue);
+		skb_set_queue_mapping(skb, queue);
+	}
 
 	child = q->qdiscs[queue];
 	if (unlikely(child == &noop_qdisc))
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch silently changes behaviour on stable kernels, and its commit message and docs do not describe the change accurately.

--

net/sched: clamp skb->queue_mapping before indexing the tx queue

A tc BPF store can leave skb->queue_mapping out of range. This patch caps it in the qdisc dequeue paths in sch_generic.c and in taprio_enqueue(), so it can no longer index dev->_tx[] or q->qdiscs[] out of bounds. On a TCQ_F_ONETXQUEUE qdisc the mapping is pinned to the qdisc's own q->dev_queue instead of being capped to queue 0.

Quoted 9 lines
net/sched/sch_generic.c +1 -0
index 6f6a6f0d5eb0..5dc1d47f811a 100644
@@ -96,9 +96,34 @@ static void qdisc_maybe_clear_missed(struct Qdisc *q,
 
 #define SKB_XOFF_MAGIC ((struct sk_buff *)1UL)
 
+/* Re-cap a stale queue_mapping; an ONETXQUEUE qdisc pins q->dev_queue. */

net/sched/sch_generic.c static void qdisc_maybe_clear_missed(struct Qdisc *q, · line 109

Quoted 10 lines
net/sched/sch_generic.c +10 -0
+static u16 qdisc_cap_skb_tx_queue(struct Qdisc *q, struct sk_buff *skb)
+{
+	u16 queue = skb_get_queue_mapping(skb);
+	struct net_device *dev = qdisc_dev(q);
+	u16 capped;
+
+	if (q->flags & TCQ_F_ONETXQUEUE)
+		capped = q->dev_queue - netdev_get_tx_queue(dev, 0);
+	else
+		capped = netdev_cap_txqueue(dev, queue);

[Severity: Medium]

On a TCQ_F_ONETXQUEUE qdisc, this branch rewrites the mapping of every skb, not just the out-of-range ones. Is that intended for in-range mappings that a classifier sets on purpose?

The rewrite happens in several places. dequeue_skb() applies it to the head skb and to gso_skb. try_bulk_dequeue_skb() applies it to each follower. __skb_dequeue_bad_txq() applies it to skbs taken from skb_bad_txq.

Here is one way to hit it. Take a per-queue child of mq, mqprio, taprio or htb offload whose q->dev_queue is txq i. A filter on that child can run "action skbedit queue_mapping j" with j != i and j < real_num_tx_queues. tcf_skbedit_act() accepts that value:

net/sched/act_skbedit.c:tcf_skbedit_act() {
    ...
	if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
	    skb->dev->real_num_tx_queues > params->queue_mapping) {
		if (skb_at_tc_egress(skb))
			netdev_xmit_skip_txqueue(true);
		skb_set_queue_mapping(skb, tcf_skbedit_hash(params, skb));
	}
    ...
}

A tc BPF store of an in-range value behaves the same way.

Before this patch, qdisc_restart() sent the packet on skb_get_tx_queue(dev, skb), which is queue j. With this patch, dequeue_skb()->qdisc_cap_skb_tx_queue() first rewrites j back to i. The packet then goes out on queue i, and nothing is logged.

The commit message says:

netdev_cap_txqueue() already logs a ratelimited notice and selects queue 0.

The TCQ_F_ONETXQUEUE branch never calls netdev_cap_txqueue(), though. That also covers the 1-queue reproducer in the commit message, because qdisc_create() sets the flag on every non-multiqueue device:

net/sched/sch_api.c:qdisc_create() {
    ...
		if (!netif_is_multiqueue(dev))
			sch->flags |= TCQ_F_ONETXQUEUE;
    ...
}

So in that reproducer, a store of 1 from the filter on the prio root is quietly pinned to 0, with no notice.

The only place that says in-range mappings from a filter on a per-queue child are now overridden is the v4 -> v5 note below the "---". git am drops that part, and the patch is tagged for stable.

Documentation/networking/tc-queue-filters.rst also still says:

2) TC filter directs traffic to a transmit queue with the action skbedit queue_mapping $tx_qid.

It does not limit where the filter can be attached.

Could the commit message describe the pinning of in-range mappings, and the missing log message on the TCQ_F_ONETXQUEUE path? Should the documentation be updated to match?

Quoted 6 lines
net/sched/sch_generic.c +6 -0
+
+	if (unlikely(capped != queue))
+		skb_set_queue_mapping(skb, capped);
+
+	return capped;
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-MD1X.v5.r0.20261026053226-1%40mojatatu.com
author maintainer
#2 lore raw
Quoted 6 lines · On Thu, Oct 29, 2026 at 2:16 PM <[email protected]> wrote:

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0

Quoted 9 lines

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch silently changes behaviour on stable kernels, and its commit message and docs do not describe the change accurately.

sigh. Exhausting. I will send v6 with the commit message fixed.

cheers,
jamal

Quoted 115 lines

--

net/sched: clamp skb->queue_mapping before indexing the tx queue

A tc BPF store can leave skb->queue_mapping out of range. This patch caps it in the qdisc dequeue paths in sch_generic.c and in taprio_enqueue(), so it can no longer index dev->_tx[] or q->qdiscs[] out of bounds. On a TCQ_F_ONETXQUEUE qdisc the mapping is pinned to the qdisc's own q->dev_queue instead of being capped to queue 0.

Quoted 9 lines
net/sched/sch_generic.c +1 -0
index 6f6a6f0d5eb0..5dc1d47f811a 100644
@@ -96,9 +96,34 @@ static void qdisc_maybe_clear_missed(struct Qdisc *q,
 
 #define SKB_XOFF_MAGIC ((struct sk_buff *)1UL)
 
+/* Re-cap a stale queue_mapping; an ONETXQUEUE qdisc pins q->dev_queue. */

net/sched/sch_generic.c static void qdisc_maybe_clear_missed(struct Qdisc *q, · line 109

Quoted 10 lines
net/sched/sch_generic.c +10 -0
+static u16 qdisc_cap_skb_tx_queue(struct Qdisc *q, struct sk_buff *skb)
+{
+     u16 queue = skb_get_queue_mapping(skb);
+     struct net_device *dev = qdisc_dev(q);
+     u16 capped;
+
+     if (q->flags & TCQ_F_ONETXQUEUE)
+             capped = q->dev_queue - netdev_get_tx_queue(dev, 0);
+     else
+             capped = netdev_cap_txqueue(dev, queue);

[Severity: Medium]

On a TCQ_F_ONETXQUEUE qdisc, this branch rewrites the mapping of every skb, not just the out-of-range ones. Is that intended for in-range mappings that a classifier sets on purpose?

The rewrite happens in several places. dequeue_skb() applies it to the head skb and to gso_skb. try_bulk_dequeue_skb() applies it to each follower. __skb_dequeue_bad_txq() applies it to skbs taken from skb_bad_txq.

Here is one way to hit it. Take a per-queue child of mq, mqprio, taprio or htb offload whose q->dev_queue is txq i. A filter on that child can run "action skbedit queue_mapping j" with j != i and j < real_num_tx_queues. tcf_skbedit_act() accepts that value:

net/sched/act_skbedit.c:tcf_skbedit_act() {
    ...
        if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
            skb->dev->real_num_tx_queues > params->queue_mapping) {
                if (skb_at_tc_egress(skb))
                        netdev_xmit_skip_txqueue(true);
                skb_set_queue_mapping(skb, tcf_skbedit_hash(params, skb));
        }
    ...
}

A tc BPF store of an in-range value behaves the same way.

Before this patch, qdisc_restart() sent the packet on skb_get_tx_queue(dev, skb), which is queue j. With this patch, dequeue_skb()->qdisc_cap_skb_tx_queue() first rewrites j back to i. The packet then goes out on queue i, and nothing is logged.

The commit message says:

netdev_cap_txqueue() already logs a ratelimited notice and selects queue 0.

The TCQ_F_ONETXQUEUE branch never calls netdev_cap_txqueue(), though. That also covers the 1-queue reproducer in the commit message, because qdisc_create() sets the flag on every non-multiqueue device:

net/sched/sch_api.c:qdisc_create() {
    ...
                if (!netif_is_multiqueue(dev))
                        sch->flags |= TCQ_F_ONETXQUEUE;
    ...
}

So in that reproducer, a store of 1 from the filter on the prio root is quietly pinned to 0, with no notice.

The only place that says in-range mappings from a filter on a per-queue child are now overridden is the v4 -> v5 note below the "---". git am drops that part, and the patch is tagged for stable.

Documentation/networking/tc-queue-filters.rst also still says:

2) TC filter directs traffic to a transmit queue with the action skbedit queue_mapping $tx_qid.

It does not limit where the filter can be attached.

Could the commit message describe the pinning of in-range mappings, and the missing log message on the TCQ_F_ONETXQUEUE path? Should the documentation be updated to match?

net/sched/sch_generic.c static void qdisc_maybe_clear_missed(struct Qdisc *q, · line 115

Quoted 6 lines
net/sched/sch_generic.c +6 -0
+
+     if (unlikely(capped != queue))
+             skb_set_queue_mapping(skb, capped);
+
+     return capped;
+}

[ ... ]

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-MD1X.v5.r0.20261026053226-1%40mojatatu.com