Skip to content

Commit 7fb8fbc

Browse files
a-nogikhgregkh
authored andcommitted
netem: fix zero division in tabledist
[ Upstream commit eadd1be ] Currently it is possible to craft a special netlink RTM_NEWQDISC command that can result in jitter being equal to 0x80000000. It is enough to set the 32 bit jitter to 0x02000000 (it will later be multiplied by 2^6) or just set the 64 bit jitter via TCA_NETEM_JITTER64. This causes an overflow during the generation of uniformly distributed numbers in tabledist(), which in turn leads to division by zero (sigma != 0, but sigma * 2 is 0). The related fragment of code needs 32-bit division - see commit 9b0ed89 ("netem: remove unnecessary 64 bit modulus"), so switching to 64 bit is not an option. Fix the issue by keeping the value of jitter within the range that can be adequately handled by tabledist() - [0;INT_MAX]. As negative std deviation makes no sense, take the absolute value of the passed value and cap it at INT_MAX. Inside tabledist(), switch to unsigned 32 bit arithmetic in order to prevent overflows. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Aleksandr Nogikh <[email protected]> Reported-by: [email protected] Acked-by: Stephen Hemminger <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 2525993 commit 7fb8fbc

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

net/sched/sch_netem.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static s64 tabledist(s64 mu, s32 sigma,
330330

331331
/* default uniform distribution */
332332
if (dist == NULL)
333-
return ((rnd % (2 * sigma)) + mu) - sigma;
333+
return ((rnd % (2 * (u32)sigma)) + mu) - sigma;
334334

335335
t = dist->table[rnd % dist->size];
336336
x = (sigma % NETEM_DIST_SCALE) * t;
@@ -812,6 +812,10 @@ static void get_slot(struct netem_sched_data *q, const struct nlattr *attr)
812812
q->slot_config.max_packets = INT_MAX;
813813
if (q->slot_config.max_bytes == 0)
814814
q->slot_config.max_bytes = INT_MAX;
815+
816+
/* capping dist_jitter to the range acceptable by tabledist() */
817+
q->slot_config.dist_jitter = min_t(__s64, INT_MAX, abs(q->slot_config.dist_jitter));
818+
815819
q->slot.packets_left = q->slot_config.max_packets;
816820
q->slot.bytes_left = q->slot_config.max_bytes;
817821
if (q->slot_config.min_delay | q->slot_config.max_delay |
@@ -1037,6 +1041,9 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,
10371041
if (tb[TCA_NETEM_SLOT])
10381042
get_slot(q, tb[TCA_NETEM_SLOT]);
10391043

1044+
/* capping jitter to the range acceptable by tabledist() */
1045+
q->jitter = min_t(s64, abs(q->jitter), INT_MAX);
1046+
10401047
return ret;
10411048

10421049
get_table_failure:

0 commit comments

Comments
 (0)