Netdev List
 help / color / mirror / Atom feed
From: Dave Seddon <dave.seddon.ca@gmail.com>
To: netdev@vger.kernel.org
Cc: Dave Seddon <dave.seddon.ca@gmail.com>,
	Stephen Hemminger <stephen@networkplumber.org>,
	David Ahern <dsahern@kernel.org>,
	Jamal Hadi Salim <jhs@mojatatu.com>,
	Eric Dumazet <edumazet@kernel.org>, Odin Ugedal <odin@uged.al>
Subject: [PATCH iproute2 3/5] lib: test for overflow before converting a rate or size
Date: Wed,  2 Dec 2026 22:40:12 -0800	[thread overview]
Message-ID: <20261203064014.2272012-4-dave.seddon.ca@gmail.com> (raw)
In-Reply-To: <20261203064014.2272012-1-dave.seddon.ca@gmail.com>

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


  parent reply	other threads:[~2026-12-03  6:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203064014.2272012-4-dave.seddon.ca@gmail.com \
    --to=dave.seddon.ca@gmail.com \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=jhs@mojatatu.com \
    --cc=netdev@vger.kernel.org \
    --cc=odin@uged.al \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox