Use-after-free in hfsc_enqueue()
Subsystems (2)
Quoted 12 lines · Budimir Markovic
The use-after-free referenced in commit
3f981138109f("sch_hfsc: Fix qlen accounting bug when using peek inhfsc_enqueue()") is still possible. That commit ensured thatqlen_notifyis called on sch's parent class duringpeek(), but that class has not yet been added to its active list at this point. It is only added afterhfsc_enqueue()returns, and since no packets are enqueued it will never be removed. The same applies to any classes further up the hierarchy.These commands trigger the bug causing a use-after-free and often a kernel crash:
Quoted 13 lines
ip link set dev lo up
tc qdisc add dev lo root handle 1: drr
tc filter add dev lo parent 1: basic classid 1:1
tc class add dev lo parent 1: classid 1:1 drr
tc qdisc add dev lo parent 1:1 handle 2: hfsc def 1 tc class add dev lo parent 2: classid 2:1 hfsc rt m1 8 d 1 m2 0 tc qdisc add dev lo parent 2:1 handle 3: netem
tc qdisc add dev lo parent 3:1 handle 4: blackhole ping -c1 -W0.01 localhost
tc class del dev lo classid 1:1
tc filter add dev lo parent 1: basic classid 1:1
ping -c1 -W0.01 localhost
There are several approaches being discussed to do this on the list. Please come there, see this thread:
https://lore.kernel.org/netdev/[email protected]/
cheers,
jamal
Quoted 31 lines
It can be fixed with the following change:
net/sched/sch_hfsc.c +6
-1@@ -1589,8 +1589,13 @@ hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)* head drop before the first dequeue operation has no chance * to invalidate the deadline. */ -if (cl->cl_flags &HFSC_RSC ) +if (cl->cl_flags &HFSC_RSC ) { cl->qdisc->ops->peek (cl->qdisc); +if (!cl->qdisc->q.qlen) { +qdisc_tree_reduce_backlog (sch,-1 , -len); +return NET_XMIT_DROP ; + } + } }Returning
__NET_XMIT_BYPASSprevents sch's parent class from being added to its active list when no packets have been enqueued. It also prevents it from updating its queue length, so we need to do this manually withqdisc_tree_reduce_backlog(). This is necessary becausenetem_dequeue()assumes the packet has already been enqueued to its ancestor qdiscs and decreases their queue lengths when dropping the packet.
qdisc_tree_reduce_backlog()will end up callingqlen_notifyon non-active classes, but after the recent hardening of theqlen_notifymethods this should be safe.
Hi Budimir,
Quoted 53 lines · Budimir Markovic
The use-after-free referenced in commit
3f981138109f("sch_hfsc: Fix qlen accounting bug when using peek inhfsc_enqueue()") is still possible. That commit ensured thatqlen_notifyis called on sch's parent class duringpeek(), but that class has not yet been added to its active list at this point. It is only added afterhfsc_enqueue()returns, and since no packets are enqueued it will never be removed. The same applies to any classes further up the hierarchy.These commands trigger the bug causing a use-after-free and often a kernel crash:
ip link set dev lo up
tc qdisc add dev lo root handle 1: drr
tc filter add dev lo parent 1: basic classid 1:1
tc class add dev lo parent 1: classid 1:1 drr
tc qdisc add dev lo parent 1:1 handle 2: hfsc def 1 tc class add dev lo parent 2: classid 2:1 hfsc rt m1 8 d 1 m2 0 tc qdisc add dev lo parent 2:1 handle 3: netem
tc qdisc add dev lo parent 3:1 handle 4: blackhole ping -c1 -W0.01 localhost
tc class del dev lo classid 1:1
tc filter add dev lo parent 1: basic classid 1:1
ping -c1 -W0.01 localhostIt can be fixed with the following change:
net/sched/sch_hfsc.c +6
-1@@ -1589,8 +1589,13 @@ hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)* head drop before the first dequeue operation has no chance * to invalidate the deadline. */ -if (cl->cl_flags &HFSC_RSC ) +if (cl->cl_flags &HFSC_RSC ) { cl->qdisc->ops->peek (cl->qdisc); +if (!cl->qdisc->q.qlen) { +qdisc_tree_reduce_backlog (sch,-1 , -len); +return NET_XMIT_DROP ; + } + } }Returning
__NET_XMIT_BYPASSprevents sch's parent class from being added to its active list when no packets have been enqueued. It also prevents it from updating its queue length, so we need to do this manually withqdisc_tree_reduce_backlog(). This is necessary becausenetem_dequeue()assumes the packet has already been enqueued to its ancestor qdiscs and decreases their queue lengths when dropping the packet.
Quoted 3 lines
qdisc_tree_reduce_backlog()will end up callingqlen_notifyon non-active classes, but after the recent hardening of theqlen_notifymethods this should be safe.
I think this is probably the same as the one reported by Mingi a few weeks ago. Your above patch looks reasonable from my glance.
Do you mind submitting a formal patch? Ideally, send it together with a selftest (in the same patchset).
Thanks!
Hi Budimir and Mingi,
I just re-tested this. This bug has been gone after Lion's fix:
https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/commit/?id=103406b38c600fec1fe375a77b27d87e314aea09
Thanks!