net_sched: red: fix a race in __red_change()
authorEric Dumazet <edumazet@google.com>
Wed, 11 Jun 2025 11:15:12 +0000 (11:15 +0000)
committerJakub Kicinski <kuba@kernel.org>
Thu, 12 Jun 2025 15:05:49 +0000 (08:05 -0700)
Gerrard Tai reported a race condition in RED, whenever SFQ perturb timer
fires at the wrong time.

The race is as follows:

CPU 0                                 CPU 1
[1]: lock root
[2]: qdisc_tree_flush_backlog()
[3]: unlock root
 |
 |                                    [5]: lock root
 |                                    [6]: rehash
 |                                    [7]: qdisc_tree_reduce_backlog()
 |
[4]: qdisc_put()

This can be abused to underflow a parent's qlen.

Calling qdisc_purge_queue() instead of qdisc_tree_flush_backlog()
should fix the race, because all packets will be purged from the qdisc
before releasing the lock.

Fixes: 0c8d13ac9607 ("net: sched: red: delay destroying child qdisc on replace")
Reported-by: Gerrard Tai <gerrard.tai@starlabs.sg>
Suggested-by: Gerrard Tai <gerrard.tai@starlabs.sg>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20250611111515.1983366-3-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/sched/sch_red.c

index 1ba3e0bba54f0cc8d797ab9a7ffab529481b2d4e..4696c893cf553c4c6ecd2da75b47b3d40c80e812 100644 (file)
@@ -285,7 +285,7 @@ static int __red_change(struct Qdisc *sch, struct nlattr **tb,
        q->userbits = userbits;
        q->limit = ctl->limit;
        if (child) {
-               qdisc_tree_flush_backlog(q->qdisc);
+               qdisc_purge_queue(q->qdisc);
                old_child = q->qdisc;
                q->qdisc = child;
        }