[PATCH net v2] net/sched: sch_netem: prevent packet length underflow with negative overhead
flat view
HOTtoday
From: Bui Viet Dung <hidden>
Date: 2026-10-12 04:43:30
Also in:
lkml, stable
Subsystem:
netem network emulator, networking [general], tc subsystem, the rest · Maintainers:
Stephen Hemminger, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Jamal Hadi Salim, Jiri Pirko, Linus Torvalds
packet_time_ns() calculates transmission time for netem rate shaping.
It adds q->packet_overhead (signed 32-bit integer) to len (unsigned
64-bit integer).
When userspace configures a negative packet overhead (via
TCA_NETEM_RATE packet_overhead attribute) and an enqueued packet's
length is smaller than the absolute overhead, (s64)len +
q->packet_overhead is non-positive. Because len is u64, the addition
wraps into an astronomical value (~2^64 - 1). When multiplied by
NSEC_PER_SEC and divided by q->rate, the calculated delay spans days,
causing enqueued packets to be frozen in the qdisc indefinitely and
stalling transmission.
A similar underflow can occur in cell length calculation when
(q->cell_size + q->cell_overhead) is non-positive.
Furthermore, computing cell_len in s64 prevents sign corruption if
cell_size >= 0x80000000 or if cell_size + cell_overhead exceeds INT_MAX.
Computing the final transmission time with mul_u64_u64_div_u64() and
clamping to S64_MAX avoids 64-bit multiplication overflow and prevents
the resulting delay from turning negative in s64 delay arithmetic.
Return 0 delay if effective packet length or cell length is non-positive
so that non-positive effective packet lengths experience no rate
throttling rather than freezing transmission.
The issue was discovered via manual code audit of sch_netem.c and
verified on a test VM using 'tc qdisc add dev lo root netem rate 100Mbit
overhead -100', which caused immediate backlog stalls on small packets.
Fixes: 7bc0f28c7a0c ("netem: rate extension")
Cc: stable@vger.kernel.org
Signed-off-by: Bui Viet Dung <redacted>
---
Link to v1: https://lore.kernel.org/netdev/20261008035656.330667-1-dungvn2345@gmail.com/ (local)
v1 -> v2:
- Calculate cell_len as s64 to avoid sign corruption when cell_size >= 0x80000000 or upon s32 overflow (Sashiko AI review).
- Use mul_u64_u64_div_u64() to prevent u64 multiplication overflow with large cell configurations, and clamp result to S64_MAX to prevent negative s64 delays (Sashiko AI review).
- Reword commit message: remove inaccurate comparisons to sch_cake and sch_tbf; clarify the clamping policy for non-positive effective length (Sashiko AI review).
- Include <linux/math64.h>.
net/sched/sch_netem.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index 8a1b89a289d8..3eccdc93c0b6 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c@@ -21,6 +21,7 @@ #include <linux/rtnetlink.h> #include <linux/reciprocal_div.h> #include <linux/rbtree.h> +#include <linux/math64.h> #include <net/gso.h> #include <net/netlink.h>
@@ -366,17 +367,26 @@ static s64 tabledist(s64 mu, s32 sigma, static u64 packet_time_ns(u64 len, const struct netem_sched_data *q) { + u64 d; + + if ((s64)len + q->packet_overhead <= 0) + return 0; + len += q->packet_overhead; if (q->cell_size) { u32 cells = reciprocal_divide(len, q->cell_size_reciprocal); + s64 cell_len = (s64)q->cell_size + q->cell_overhead; if (len > cells * q->cell_size) /* extra cell needed for remainder */ cells++; - len = cells * (q->cell_size + q->cell_overhead); + if (cell_len <= 0) + return 0; + len = (u64)cells * cell_len; } - return div64_u64(len * NSEC_PER_SEC, q->rate); + d = mul_u64_u64_div_u64(len, NSEC_PER_SEC, q->rate); + return min_t(u64, d, S64_MAX); } static void tfifo_reset(struct Qdisc *sch)
--
2.43.0