Skip to content

Commit 0965d85

Browse files
congwangpaniakin-aws
authored andcommitted
sch_htb: make htb_deactivate() idempotent
commit 3769478 upstream. Alan reported a NULL pointer dereference in htb_next_rb_node() after we made htb_qlen_notify() idempotent. It turns out in the following case it introduced some regression: htb_dequeue_tree(): |-> fq_codel_dequeue() |-> qdisc_tree_reduce_backlog() |-> htb_qlen_notify() |-> htb_deactivate() |-> htb_next_rb_node() |-> htb_deactivate() For htb_next_rb_node(), after calling the 1st htb_deactivate(), the clprio[prio]->ptr could be already set to NULL, which means htb_next_rb_node() is vulnerable here. For htb_deactivate(), although we checked qlen before calling it, in case of qlen==0 after qdisc_tree_reduce_backlog(), we may call it again which triggers the warning inside. To fix the issues here, we need to: 1) Make htb_deactivate() idempotent, that is, simply return if we already call it before. 2) Make htb_next_rb_node() safe against ptr==NULL. Many thanks to Alan for testing and for the reproducer. Fixes: 5ba8b83 ("sch_htb: make htb_qlen_notify() idempotent") Reported-by: Alan J. Wylie <alan@wylie.me.uk> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Link: https://patch.msgid.link/20250428232955.1740419-2-xiyou.wangcong@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> [mheyne: fixed contextual conflicts] Signed-off-by: Maximilian Heyne <mheyne@amazon.de>
1 parent 526fc9d commit 0965d85

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

net/sched/sch_htb.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ static void htb_add_to_wait_tree(struct htb_sched *q,
334334
*/
335335
static inline void htb_next_rb_node(struct rb_node **n)
336336
{
337-
*n = rb_next(*n);
337+
if (*n)
338+
*n = rb_next(*n);
338339
}
339340

340341
/**
@@ -570,8 +571,8 @@ static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
570571
*/
571572
static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
572573
{
573-
WARN_ON(!cl->prio_activity);
574-
574+
if (!cl->prio_activity)
575+
return;
575576
htb_deactivate_prios(q, cl);
576577
cl->prio_activity = 0;
577578
list_del_init(&cl->un.leaf.drop_list);
@@ -1190,8 +1191,6 @@ static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
11901191
{
11911192
struct htb_class *cl = (struct htb_class *)arg;
11921193

1193-
if (!cl->prio_activity)
1194-
return;
11951194
htb_deactivate(qdisc_priv(sch), cl);
11961195
}
11971196

@@ -1304,8 +1303,7 @@ static int htb_delete(struct Qdisc *sch, unsigned long arg)
13041303
if (cl->parent)
13051304
cl->parent->children--;
13061305

1307-
if (cl->prio_activity)
1308-
htb_deactivate(q, cl);
1306+
htb_deactivate(q, cl);
13091307

13101308
if (cl->cmode != HTB_CAN_SEND)
13111309
htb_safe_rb_erase(&cl->pq_node,
@@ -1430,8 +1428,7 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,
14301428
qdisc_reset(parent->un.leaf.q);
14311429
qdisc_tree_reduce_backlog(parent->un.leaf.q, qlen, backlog);
14321430
qdisc_destroy(parent->un.leaf.q);
1433-
if (parent->prio_activity)
1434-
htb_deactivate(q, parent);
1431+
htb_deactivate(q, parent);
14351432

14361433
/* remove from evt list because of level change */
14371434
if (parent->cmode != HTB_CAN_SEND) {

0 commit comments

Comments
 (0)