Skip to contentSkip to conversation

sch_cake: avoid possible divide by zero in cake_enqueue()

PATCH In review Wen Yang 4 participants 4 messages Jan 2, 2020 – Jan 3, 2020

Subsystems (4)
lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 sch_cake: avoid possible divide by zero in cake_enqueue()

The variables 'window_interval' is u64 and do_div() truncates it to 32 bits, which means it can test non-zero and be truncated to zero for division. The unit of window_interval is nanoseconds, so its lower 32-bit is relatively easy to exceed. Fix this issue by using div64_u64() instead.

Fixes 7298de9cd725 ("sch_cake: Add ingress mode")
Cc Kevin Darbyshire-Bryant <[email protected]>
Cc Toke Høiland-Jørgensen <[email protected]>
Cc David S. Miller <[email protected]>
Cc Cong Wang <[email protected]>
---
 net/sched/sch_cake.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_cake.c +1 -1
index 6cc3ab1..90ef7cc 100644
@@ -1768,7 +1768,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 						      q->avg_window_begin));
 			u64 b = q->avg_window_bytes * (u64)NSEC_PER_SEC;
 
-			do_div(b, window_interval);
+			b = div64_u64(b, window_interval);
 			q->avg_peak_bandwidth =
 				cake_ewma(q->avg_peak_bandwidth, b,
 					  b > q->avg_peak_bandwidth ? 2 : 8);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 8 lines · Wen Yang

The variables 'window_interval' is u64 and do_div() truncates it to 32 bits, which means it can test non-zero and be truncated to zero for division. The unit of window_interval is nanoseconds, so its lower 32-bit is relatively easy to exceed. Fix this issue by using div64_u64() instead.

That might actually explain a few things. I approve.

Honestly the *correct* fix is for the compiler to implement division in a way that doesn't require substituting it with function calls. As this shows, it's error-prone to do this manually.

- Jonathan Morton

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Wen Yang
Quoted 7 lines · Wen Yang

The variables 'window_interval' is u64 and do_div() truncates it to 32 bits, which means it can test non-zero and be truncated to zero for division. The unit of window_interval is nanoseconds, so its lower 32-bit is relatively easy to exceed. Fix this issue by using div64_u64() instead.

Quoted 9 lines
Fixes 7298de9cd725 ("sch_cake: Add ingress mode")
Cc Kevin Darbyshire-Bryant <[email protected]>
Cc Toke Høiland-Jørgensen <[email protected]>
Cc David S. Miller <[email protected]>
Cc Cong Wang <[email protected]>
maintainer
#3 lore raw
In reply to Wen Yang

From: Wen Yang <[email protected]>
Date: Thu, 2 Jan 2020 17:21:43 +0800

Quoted 9 lines

The variables 'window_interval' is u64 and do_div() truncates it to 32 bits, which means it can test non-zero and be truncated to zero for division. The unit of window_interval is nanoseconds, so its lower 32-bit is relatively easy to exceed. Fix this issue by using div64_u64() instead.

Fixes 7298de9cd725 ("sch_cake: Add ingress mode")

Applied and queued up for -stable.