Skip to content

Commit 98cd7ed

Browse files
congwanggregkh
authored andcommitted
sch_htb: make htb_deactivate() idempotent
[ Upstream commit 3769478 ] 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 <[email protected]> Signed-off-by: Cong Wang <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 5c3b8f0 commit 98cd7ed

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
@@ -348,7 +348,8 @@ static void htb_add_to_wait_tree(struct htb_sched *q,
348348
*/
349349
static inline void htb_next_rb_node(struct rb_node **n)
350350
{
351-
*n = rb_next(*n);
351+
if (*n)
352+
*n = rb_next(*n);
352353
}
353354

354355
/**
@@ -609,8 +610,8 @@ static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
609610
*/
610611
static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
611612
{
612-
WARN_ON(!cl->prio_activity);
613-
613+
if (!cl->prio_activity)
614+
return;
614615
htb_deactivate_prios(q, cl);
615616
cl->prio_activity = 0;
616617
}
@@ -1485,8 +1486,6 @@ static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
14851486
{
14861487
struct htb_class *cl = (struct htb_class *)arg;
14871488

1488-
if (!cl->prio_activity)
1489-
return;
14901489
htb_deactivate(qdisc_priv(sch), cl);
14911490
}
14921491

@@ -1740,8 +1739,7 @@ static int htb_delete(struct Qdisc *sch, unsigned long arg,
17401739
if (cl->parent)
17411740
cl->parent->children--;
17421741

1743-
if (cl->prio_activity)
1744-
htb_deactivate(q, cl);
1742+
htb_deactivate(q, cl);
17451743

17461744
if (cl->cmode != HTB_CAN_SEND)
17471745
htb_safe_rb_erase(&cl->pq_node,
@@ -1949,8 +1947,7 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,
19491947
/* turn parent into inner node */
19501948
qdisc_purge_queue(parent->leaf.q);
19511949
parent_qdisc = parent->leaf.q;
1952-
if (parent->prio_activity)
1953-
htb_deactivate(q, parent);
1950+
htb_deactivate(q, parent);
19541951

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

0 commit comments

Comments
 (0)