Skip to contentSkip to conversation

net_sched: Fix UAF vulnerability in HFSC qdisc

PATCH Applied Cong Wang 4 participants 7 messages Apr 17, 2025 – Apr 24, 2025

Subsystems (4)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

v2 cover net_sched: Fix UAF vulnerability in HFSC qdisc

This patchset contains two bug fixes and a selftest for the first one which we have a reliable reproducer, please check each patch description for details.

---
v2: Add a fix for hfsc_dequeue

Cong Wang (3):
net_sched: hfsc: Fix a UAF vulnerability in class handling
net_sched: hfsc: Fix a potential UAF in hfsc_dequeue() too selftests/tc-testing: Add test for HFSC queue emptying during peek
operation

 net/sched/sch_hfsc.c                          | 23 ++++++++---
 .../tc-testing/tc-tests/infra/qdiscs.json     | 39 +++++++++++++++++++
 2 files changed, 56 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/3 net_sched: hfsc: Fix a UAF vulnerability in class handling

This patch fixes a Use-After-Free vulnerability in the HFSC qdisc class handling. The issue occurs due to a time-of-check/time-of-use condition in hfsc_change_class() when working with certain child qdiscs like netem or codel.

The vulnerability works as follows:
1. hfsc_change_class() checks if a class has packets (q.qlen != 0)
2. It then calls qdisc_peek_len(), which for certain qdiscs (e.g., codel, netem) might drop packets and empty the queue
3. The code continues assuming the queue is still non-empty, adding the class to vttree
4. This breaks HFSC scheduler assumptions that only non-empty classes are in vttree
5. Later, when the class is destroyed, this can lead to a Use-After-Free

The fix adds a second queue length check after qdisc_peek_len() to verify the queue wasn't emptied.

Fixes 21f4d5cc25ec ("net_sched/hfsc: fix curve activation in hfsc_change_class()")
---
 net/sched/sch_hfsc.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/sched/sch_hfsc.c +7 -2
index ce5045eea065..b368ac0595d5 100644
@@ -961,6 +961,7 @@ hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
 
 	if (cl != NULL) {
 		int old_flags;
+		int len = 0;
 
 		if (parentid) {
 			if (cl->cl_parent &&
@@ -991,9 +992,13 @@ hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
 		if (usc != NULL)
 			hfsc_change_usc(cl, usc, cur_time);
 
+		if (cl->qdisc->q.qlen != 0)
+			len = qdisc_peek_len(cl->qdisc);
+		/* Check queue length again since some qdisc implementations
+		 * (e.g., netem/codel) might empty the queue during the peek
+		 * operation.
+		 */
 		if (cl->qdisc->q.qlen != 0) {
-			int len = qdisc_peek_len(cl->qdisc);
-
 			if (cl->cl_flags & HFSC_RSC) {
 				if (old_flags & HFSC_RSC)
 					update_ed(cl, len);
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/3 net_sched: hfsc: Fix a potential UAF in hfsc_dequeue() too

In reply to Cong Wang

Similarly to the previous patch, we need to safe guard hfsc_dequeue() too. But for this one, we don't have a reliable reproducer.

Fixes 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 ("Linux-2.6.12-rc2")
---
 net/sched/sch_hfsc.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
net/sched/sch_hfsc.c +10 -4
index b368ac0595d5..6c8ef826cec0 100644
@@ -1641,10 +1641,16 @@ hfsc_dequeue(struct Qdisc *sch)
 		if (cl->qdisc->q.qlen != 0) {
 			/* update ed */
 			next_len = qdisc_peek_len(cl->qdisc);
-			if (realtime)
-				update_ed(cl, next_len);
-			else
-				update_d(cl, next_len);
+			/* Check queue length again since some qdisc implementations
+			 * (e.g., netem/codel) might empty the queue during the peek
+			 * operation.
+			 */
+			if (cl->qdisc->q.qlen != 0) {
+				if (realtime)
+					update_ed(cl, next_len);
+				else
+					update_d(cl, next_len);
+			}
 		} else {
 			/* the class becomes passive */
 			eltree_remove(cl);
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/3 selftests/tc-testing: Add test for HFSC queue emptying during peek operation

In reply to Cong Wang

Add a selftest to exercise the condition where qdisc implementations like netem or codel might empty the queue during a peek operation. This tests the defensive code path in HFSC that checks the queue length again after peeking to handle this case.

Based on the reproducer from Gerrard, improved by Jamal.

---
 .../tc-testing/tc-tests/infra/qdiscs.json     | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
tools/testing/selftests/tc-testing/tc-tests/infra/qdiscs.json +39 -0
index d4ea9cd845a3..e26bbc169783 100644
@@ -313,5 +313,44 @@
             "$TC qdisc del dev $DUMMY handle 1: root",
             "$IP addr del 10.10.10.10/24 dev $DUMMY || true"
         ]
+    },
+    {
+        "id": "a4c3",
+        "name": "Test HFSC with netem/blackhole - queue emptying during peek operation",
+        "category": [
+            "qdisc",
+            "hfsc",
+            "netem",
+            "blackhole"
+        ],
+        "plugins": {
+            "requires": "nsPlugin"
+        },
+        "setup": [
+            "$IP link set dev $DUMMY up || true",
+            "$IP addr add 10.10.10.10/24 dev $DUMMY || true",
+            "$TC qdisc add dev $DUMMY handle 1:0 root drr",
+            "$TC class add dev $DUMMY parent 1:0 classid 1:1 drr",
+            "$TC class add dev $DUMMY parent 1:0 classid 1:2 drr",
+            "$TC qdisc add dev $DUMMY parent 1:1 handle 2:0 plug limit 1024",
+            "$TC qdisc add dev $DUMMY parent 1:2 handle 3:0 hfsc default 1",
+            "$TC class add dev $DUMMY parent 3:0 classid 3:1 hfsc rt m1 5Mbit d 10ms m2 10Mbit",
+            "$TC qdisc add dev $DUMMY parent 3:1 handle 4:0 netem delay 1ms",
+            "$TC qdisc add dev $DUMMY parent 4:1 handle 5:0 blackhole",
+            "ping -c 3 -W 0.01 -i 0.001 -s 1 10.10.10.10 -I $DUMMY > /dev/null 2>&1 || true",
+            "$TC class change dev $DUMMY parent 3:0 classid 3:1 hfsc sc m1 5Mbit d 10ms m2 10Mbit",
+            "$TC class del dev $DUMMY parent 3:0 classid 3:1",
+            "$TC class add dev $DUMMY parent 3:0 classid 3:1 hfsc rt m1 5Mbit d 10ms m2 10Mbit",
+            "ping -c 3 -W 0.01 -i 0.001 -s 1 10.10.10.10 -I $DUMMY > /dev/null 2>&1 || true"
+        ],
+        "cmdUnderTest": "$TC class change dev $DUMMY parent 3:0 classid 3:1 hfsc sc m1 5Mbit d 10ms m2 10Mbit",
+        "expExitCode": "0",
+        "verifyCmd": "$TC -s qdisc show dev $DUMMY",
+        "matchPattern": "qdisc hfsc 3:.*parent 1:2.*default 1",
+        "matchCount": "1",
+        "teardown": [
+            "$TC qdisc del dev $DUMMY handle 1:0 root",
+            "$IP addr del 10.10.10.10/24 dev $DUMMY || true"
+        ]
     }
 ]
Signature · 1 line
2.34.1
Tested-by on v2 3/3
#4 lore raw

Re: [Patch net v2 3/3] selftests/tc-testing: Add test for HFSC queue emptying during peek operation

Quoted 10 lines · Cong Wang

Add a selftest to exercise the condition where qdisc implementations like netem or codel might empty the queue during a peek operation. This tests the defensive code path in HFSC that checks the queue length again after peeking to handle this case.

Based on the reproducer from Gerrard, improved by Jamal.

maintainer Reviewed-by on the v2 series
#5 lore raw
In reply to Cong Wang
Quoted 5 lines · Cong Wang

This patchset contains two bug fixes and a selftest for the first one which we have a reliable reproducer, please check each patch description for details.

For the patchset:

cheers,
jamal

Quoted 16 lines

---
v2: Add a fix for hfsc_dequeue

Cong Wang (3):
net_sched: hfsc: Fix a UAF vulnerability in class handling
net_sched: hfsc: Fix a potential UAF in hfsc_dequeue() too selftests/tc-testing: Add test for HFSC queue emptying during peek
operation

 net/sched/sch_hfsc.c                          | 23 ++++++++---
 .../tc-testing/tc-tests/infra/qdiscs.json     | 39 +++++++++++++++++++
 2 files changed, 56 insertions(+), 6 deletions(-)

--
2.34.1

#6 lore raw
In reply to Cong Wang
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Thu, 17 Apr 2025 11:47:29 -0700 you wrote:

This patchset contains two bug fixes and a selftest for the first one which we have a reliable reproducer, please check each patch description for details.

---
v2: Add a fix for hfsc_dequeue

[...]

Here is the summary with links:
- [net,v2,1/3] net_sched: hfsc: Fix a UAF vulnerability in class handling

    https://git.kernel.org/netdev/net/c/3df275ef0a6a
  - [net,v2,2/3] net_sched: hfsc: Fix a potential UAF in hfsc_dequeue() too
    https://git.kernel.org/netdev/net/c/6ccbda44e2cc
  - [net,v2,3/3] selftests/tc-testing: Add test for HFSC queue emptying during peek operation
    https://git.kernel.org/netdev/net/c/7629d1a04ad2

You are awesome, thank you!

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