Commit ad5b5576 authored by Alban Browaeys's avatar Alban Browaeys Committed by Pablo Neira Ayuso

netfilter: xt_hashlimit: Fix integer divide round to zero.

Diving the divider by the multiplier before applying to the input.
When this would "divide by zero", divide the multiplier by the divider
first then multiply the input by this value.

Currently user2creds outputs zero when input value is bigger than the
number of slices and  lower than scale.
This as then user input is applied an integer divide operation to
a number greater than itself (scale).
That rounds up to zero, then we multiply zero by the credits slice size.

  iptables -t filter -I INPUT --protocol tcp --match hashlimit
  --hashlimit 40/second --hashlimit-burst 20 --hashlimit-mode srcip
  --hashlimit-name syn-flood --jump RETURN

thus trigger the overflow detection code:

xt_hashlimit: overflow, try lower: 25000/20

(25000 as hashlimit avg and 20 the burst)

Here:
134217 slices of (HZ * CREDITS_PER_JIFFY) size.
500000 is user input value
1000000 is XT_HASHLIMIT_SCALE_v2
gives: 0 as user2creds output
Setting burst to "1" typically solve the issue ...
but setting it to "40" does too !

This is on 32bit arch calling into revision 2 of hashlimit.
Signed-off-by: default avatarAlban Browaeys <alban.browaeys@gmail.com>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent f95d7a46
......@@ -463,23 +463,16 @@ static u32 xt_hashlimit_len_to_chunks(u32 len)
/* Precision saver. */
static u64 user2credits(u64 user, int revision)
{
if (revision == 1) {
/* If multiplying would overflow... */
if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY_v1))
/* Divide first. */
return div64_u64(user, XT_HASHLIMIT_SCALE)
* HZ * CREDITS_PER_JIFFY_v1;
return div64_u64(user * HZ * CREDITS_PER_JIFFY_v1,
XT_HASHLIMIT_SCALE);
} else {
if (user > 0xFFFFFFFFFFFFFFFFULL / (HZ*CREDITS_PER_JIFFY))
return div64_u64(user, XT_HASHLIMIT_SCALE_v2)
* HZ * CREDITS_PER_JIFFY;
u64 scale = (revision == 1) ?
XT_HASHLIMIT_SCALE : XT_HASHLIMIT_SCALE_v2;
u64 cpj = (revision == 1) ?
CREDITS_PER_JIFFY_v1 : CREDITS_PER_JIFFY;
return div64_u64(user * HZ * CREDITS_PER_JIFFY,
XT_HASHLIMIT_SCALE_v2);
}
/* Avoid overflow: divide the constant operands first */
if (scale >= HZ * cpj)
return div64_u64(user, div64_u64(scale, HZ * cpj));
return user * div64_u64(HZ * cpj, scale);
}
static u32 user2credits_byte(u32 user)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment