All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2 0/5] reject rates, sizes and times that overflow or are NaN
@ 2026-12-03  6:40 Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 1/5] lib: get_rate64: reject overflow and NaN Dave Seddon
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Dave Seddon @ 2026-12-03  6:40 UTC (permalink / raw)
  To: netdev
  Cc: Dave Seddon, Stephen Hemminger, David Ahern, Jamal Hadi Salim,
	Eric Dumazet, Odin Ugedal, Jay Vosburgh

get_rate64(), get_time(), get_time64() and get_time_rtt() convert a
parsed double to an integer with no range check, and the first three
also accept NaN. get_rate() and get_size64() have a check, but after
the conversion. Test before converting in each (patches 1, 3 and 4).

Patch 2 stops get_size() reading the size after get_size64() fails.
Patch 5 checks rtt and rttvar in ip route, which are scaled after the
parse.

Patches 1, 4 and 5 have a command that shows the problem on current
main; the one in patch 3 needs a sanitizer build. The read in patch 2
has no visible effect, as every caller tests the return value; clang's
static analyzer reports it.

Every command in the commit messages was run as written on current
main. A build with -fsanitize=undefined,float-cast-overflow reports
all six conversions on current main and none with the series applied.
The seven parsers were also compared with an independent model on
29094 generated inputs, on x86-64 and on arm64 (qemu-user): with the
series they agree on every input.

clang-tidy, the clang static analyzer, cppcheck and gcc -fanalyzer
report nothing new on the three files changed. With the series, the
parsers also ran clean under MemorySanitizer and libFuzzer.

Dave Seddon (5):
  lib: get_rate64: reject overflow and NaN
  lib: get_size: do not copy the size out when get_size64() fails
  lib: test for overflow before converting a rate or size
  lib: reject a time that does not fit the result type
  ip route: reject an rtt or rttvar that overflows when scaled

 ip/iproute.c     |  6 ++++--
 lib/utils.c      |  9 +++++++++
 lib/utils_math.c | 18 +++++++++---------
 3 files changed, 22 insertions(+), 11 deletions(-)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH iproute2 1/5] lib: get_rate64: reject overflow and NaN
  2026-12-03  6:40 [PATCH iproute2 0/5] reject rates, sizes and times that overflow or are NaN Dave Seddon
@ 2026-12-03  6:40 ` Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 2/5] lib: get_size: do not copy the size out when get_size64() fails Dave Seddon
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Dave Seddon @ 2026-12-03  6:40 UTC (permalink / raw)
  To: netdev
  Cc: Dave Seddon, Stephen Hemminger, David Ahern, Jamal Hadi Salim,
	Eric Dumazet

Check that the rate fits in 64 bits before converting it to an integer.
Without the check

  # tc qdisc add dev lo root tbf rate nan burst 10k limit 10k

is accepted and installs "rate 0bit".

ULLONG_MAX rounds to 2^64 as a double, so reject equality as well. The
explicit cast avoids clang's conversion warning.

Fixes: 8334bb325d51 ("htb: support 64bit rates")
Signed-off-by: Dave Seddon <dave.seddon.ca@gmail.com>
---
 lib/utils_math.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/utils_math.c b/lib/utils_math.c
index 4b0ac266..d082a391 100644
--- a/lib/utils_math.c
+++ b/lib/utils_math.c
@@ -97,6 +97,9 @@ int get_rate64(__u64 *rate, const char *str)
 		return -1; /* unknown suffix */
 
 	bps /= 8; /* -> bytes per second */
+	if (isnan(bps) || bps >= (double)ULLONG_MAX)
+		return -1;
+
 	*rate = bps;
 	return 0;
 }
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH iproute2 2/5] lib: get_size: do not copy the size out when get_size64() fails
  2026-12-03  6:40 [PATCH iproute2 0/5] reject rates, sizes and times that overflow or are NaN Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 1/5] lib: get_rate64: reject overflow and NaN Dave Seddon
@ 2026-12-03  6:40 ` Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 3/5] lib: test for overflow before converting a rate or size Dave Seddon
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Dave Seddon @ 2026-12-03  6:40 UTC (permalink / raw)
  To: netdev
  Cc: Dave Seddon, Stephen Hemminger, David Ahern, Jamal Hadi Salim,
	Jay Vosburgh

get_size() stores the size returned by get_size64() before testing its
return value. When get_size64() fails without writing the size (empty
string, negative, out of range, unknown suffix), an uninitialized value
is read. Store the size only on success, as get_size_and_cell() does;
every caller tests the return value, so none can see the change.

Clang's static analyzer reports the read ("Assigned value is
uninitialized").

Fixes: 8798ab4c4c02 ("tc: Add get_size64 and get_size64_and_cell")
Signed-off-by: Dave Seddon <dave.seddon.ca@gmail.com>
---
 lib/utils_math.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/lib/utils_math.c b/lib/utils_math.c
index d082a391..d9bf3aa0 100644
--- a/lib/utils_math.c
+++ b/lib/utils_math.c
@@ -151,13 +151,12 @@ int get_size(unsigned int *size, const char *str)
 	int rv;
 
 	rv = get_size64(&sz64, str);
-	*size = sz64;
-
 	if (rv)
 		return rv;
 
 	if (sz64 > UINT_MAX)
 		return -1;
 
+	*size = sz64;
 	return 0;
 }
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH iproute2 3/5] lib: test for overflow before converting a rate or size
  2026-12-03  6:40 [PATCH iproute2 0/5] reject rates, sizes and times that overflow or are NaN Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 1/5] lib: get_rate64: reject overflow and NaN Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 2/5] lib: get_size: do not copy the size out when get_size64() fails Dave Seddon
@ 2026-12-03  6:40 ` Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 4/5] lib: reject a time that does not fit the result type Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 5/5] ip route: reject an rtt or rttvar that overflows when scaled Dave Seddon
  4 siblings, 0 replies; 6+ messages in thread
From: Dave Seddon @ 2026-12-03  6:40 UTC (permalink / raw)
  To: netdev
  Cc: Dave Seddon, Stephen Hemminger, David Ahern, Jamal Hadi Salim,
	Eric Dumazet, Odin Ugedal

get_rate() and get_size64() convert the value to an integer and then
test whether it survived. Converting a double that is out of range is
undefined: on arm64 it saturates, so get_size64() accepts a size of
2^64 and returns UINT64_MAX. Test before converting, as get_rate64()
now does. Built with -fsanitize=float-cast-overflow,

  # tc qdisc add dev lo root tbf rate 1mbit burst 1e30 limit 10k

reports the conversion in get_size64().

Fixes: a303853e844c ("get_rate: detect 32bit overflows")
Fixes: e07c57e94e27 ("tc_util: detect overflow in get_size")
Signed-off-by: Dave Seddon <dave.seddon.ca@gmail.com>
---
 lib/utils_math.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/lib/utils_math.c b/lib/utils_math.c
index d9bf3aa0..9cad9409 100644
--- a/lib/utils_math.c
+++ b/lib/utils_math.c
@@ -63,10 +63,10 @@ int get_rate(unsigned int *rate, const char *str)
 		return -1; /* unknown suffix */
 
 	bps /= 8; /* -> bytes per second */
-	*rate = bps;
-	/* detect if an overflow happened */
-	if (*rate != floor(bps))
+	if (isnan(bps) || floor(bps) > UINT_MAX)
 		return -1;
+
+	*rate = bps;
 	return 0;
 }
 
@@ -136,12 +136,10 @@ int get_size64(__u64 *size, const char *str)
 			return -1;
 	}
 
-	*size = sz;
-
-	/* detect if an overflow happened */
-	if (*size != floor(sz))
+	if (isnan(sz) || sz >= (double)ULLONG_MAX)
 		return -1;
 
+	*size = sz;
 	return 0;
 }
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH iproute2 4/5] lib: reject a time that does not fit the result type
  2026-12-03  6:40 [PATCH iproute2 0/5] reject rates, sizes and times that overflow or are NaN Dave Seddon
                   ` (2 preceding siblings ...)
  2026-12-03  6:40 ` [PATCH iproute2 3/5] lib: test for overflow before converting a rate or size Dave Seddon
@ 2026-12-03  6:40 ` Dave Seddon
  2026-12-03  6:40 ` [PATCH iproute2 5/5] ip route: reject an rtt or rttvar that overflows when scaled Dave Seddon
  4 siblings, 0 replies; 6+ messages in thread
From: Dave Seddon @ 2026-12-03  6:40 UTC (permalink / raw)
  To: netdev; +Cc: Dave Seddon, Stephen Hemminger, David Ahern, Jamal Hadi Salim

Return an error from get_time(), get_time64() and get_time_rtt() when
the parsed time does not fit the result, and from the first two when it
is not a number. Today

  # tc qdisc add dev lo root fq_codel target 5000s
  # ip route add 198.51.100.0/24 dev lo rto_min 5000000s

install "target 705s" and "rto_min lock 705033s", and

  # tc qdisc add dev lo root fq_codel target nan

is accepted and installs the default target.

LLONG_MAX rounds to 2^63 as a double, so get_time64() rejects equality
as well. get_time() truncates and get_time_rtt() rounds up, hence the
different 32-bit limits. The cast in get_time() is needed: UINT_MAX + 1
is 0 in unsigned arithmetic. floor() is not used there, as lib/utils.c
is linked into ip and others without -lm.

get_time_rtt() cannot see NaN: a string without a '.' is parsed by
strtoul(), and one with a '.' leaves a suffix that is not a time unit.

Fixes: 54e0b2e71d72 ("rto support for ip command")
Fixes: abf70ef494fd ("tc: support conversions to or from 64 bit nanosecond-based time")
Signed-off-by: Dave Seddon <dave.seddon.ca@gmail.com>
---
 lib/utils.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/utils.c b/lib/utils.c
index f4bd53ff..7314f5b9 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -307,6 +307,9 @@ int get_time_rtt(unsigned int *val, const char *arg, int *raw)
 			return -1;
 	}
 
+	if (t > UINT_MAX)
+		return -1;
+
 	/* emulate ceil() without having to bring-in -lm and always be >= 1 */
 	*val = t;
 	if (*val < t)
@@ -1722,6 +1725,9 @@ int get_time(unsigned int *time, const char *str)
 			return -1;
 	}
 
+	if (isnan(t) || t >= (double)UINT_MAX + 1)
+		return -1;
+
 	*time = t;
 	return 0;
 }
@@ -1781,6 +1787,9 @@ int get_time64(__s64 *time, const char *str)
 			return -1;
 	}
 
+	if (isnan(nsec) || nsec >= (double)LLONG_MAX)
+		return -1;
+
 	*time = nsec;
 	return 0;
 }
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH iproute2 5/5] ip route: reject an rtt or rttvar that overflows when scaled
  2026-12-03  6:40 [PATCH iproute2 0/5] reject rates, sizes and times that overflow or are NaN Dave Seddon
                   ` (3 preceding siblings ...)
  2026-12-03  6:40 ` [PATCH iproute2 4/5] lib: reject a time that does not fit the result type Dave Seddon
@ 2026-12-03  6:40 ` Dave Seddon
  4 siblings, 0 replies; 6+ messages in thread
From: Dave Seddon @ 2026-12-03  6:40 UTC (permalink / raw)
  To: netdev; +Cc: Dave Seddon, Stephen Hemminger, David Ahern, Jamal Hadi Salim

Check that rtt and rttvar still fit in 32 bits after they are scaled by
8 and 4. Today

  # ip route add 198.51.100.0/24 dev lo rtt 1000000s

installs "rtt 463129s".

Fixes: 54e0b2e71d72 ("rto support for ip command")
Signed-off-by: Dave Seddon <dave.seddon.ca@gmail.com>
---
 ip/iproute.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/ip/iproute.c b/ip/iproute.c
index 067569c2..46571461 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -1332,7 +1332,8 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
 				mxlock |= (1<<RTAX_RTT);
 				NEXT_ARG();
 			}
-			if (get_time_rtt(&rtt, *argv, &raw))
+			if (get_time_rtt(&rtt, *argv, &raw) ||
+			    (!raw && rtt > UINT_MAX / 8))
 				invarg("\"rtt\" value is invalid\n", *argv);
 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT,
 				(raw) ? rtt : rtt * 8);
@@ -1432,7 +1433,8 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
 				mxlock |= (1<<RTAX_RTTVAR);
 				NEXT_ARG();
 			}
-			if (get_time_rtt(&win, *argv, &raw))
+			if (get_time_rtt(&win, *argv, &raw) ||
+			    (!raw && win > UINT_MAX / 4))
 				invarg("\"rttvar\" value is invalid\n", *argv);
 			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
 				(raw) ? win : win * 4);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-03  6:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  6:40 [PATCH iproute2 0/5] reject rates, sizes and times that overflow or are NaN Dave Seddon
2026-12-03  6:40 ` [PATCH iproute2 1/5] lib: get_rate64: reject overflow and NaN Dave Seddon
2026-12-03  6:40 ` [PATCH iproute2 2/5] lib: get_size: do not copy the size out when get_size64() fails Dave Seddon
2026-12-03  6:40 ` [PATCH iproute2 3/5] lib: test for overflow before converting a rate or size Dave Seddon
2026-12-03  6:40 ` [PATCH iproute2 4/5] lib: reject a time that does not fit the result type Dave Seddon
2026-12-03  6:40 ` [PATCH iproute2 5/5] ip route: reject an rtt or rttvar that overflows when scaled Dave Seddon

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.