All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2] lib, ip, tc, arpd: refuse out-of-range parsed values
@ 2026-12-01 18:59 Andrii Tereshchenko
  2026-12-02  6:54 ` Stephen Hemminger
  0 siblings, 1 reply; 2+ messages in thread
From: Andrii Tereshchenko @ 2026-12-01 18:59 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: David Ahern, netdev

Several parsers check a value before its unit is applied, or not at
all, so one that does not fit wraps: tc turns "4295s" into 32704us and
accepts "nan". ip route's rtt and rttvar scaling and tbf's limit from
"latency" wrap too, and hfsc gets d wrong for umax above 4294 bytes.
ip's encaplimit check (get_u8() < -1) never fires, so "encaplimit 300"
sends an uninitialised byte.

Check once the unit is applied. The lib parsers return -1 for
malformed input and NaN, -ERANGE for negative or too-large values.
Refuse an overflowing rtt, rttvar or tbf limit, "%" without digits and
trailing characters in an sfb probability. Fix the encaplimit test and
hfsc's 32-bit multiply, and check arpd -p before storing it.

Fixes: aba5acdfdb34 ("(Logical change 1.3)")
Fixes: 9447a0d344bf ("TUNNEL: Import ip6tunnel.c.")
Fixes: f0bda7e5a52d ("Introduce TIME_UNITS_PER_SEC to represent internal clock resolution")
Fixes: 54e0b2e71d72 ("rto support for ip command")
Fixes: d7f3299d591e ("tc : SFB flow scheduler")
Fixes: 3c7950af598b ("netem: add support for 4 state and GE loss model")
Fixes: 0df7db3cf431 ("arpd: allow configuring polling interval")
Fixes: 9d0efc10487b ("ip: add support of 'ip link type ip6tnl'")
Fixes: 8334bb325d51 ("htb: support 64bit rates")
Fixes: abf70ef494fd ("tc: support conversions to or from 64 bit nanosecond-based time")
Fixes: 8798ab4c4c02 ("tc: Add get_size64 and get_size64_and_cell")
Suggested-by: Stephen Hemminger <stephen@networkplumber.org>
Link: https://lore.kernel.org/netdev/20261130090749.593496fd@phoenix.local/
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
An AI coding assistant was used; I reviewed and tested the result.

Stephen Hemminger asked for the get_time() fix in the review linked
above; the rest came from reading the other parsers with the
assistant. This replaces the pre-unit checks of c99a85a7c8eb.

Parsers tested before and after on fixed inputs, also under UBSan,
get_time_rtt() and get_size() also in 32-bit builds: valid values
parse as before. iproute2's testsuite, tc-testing and the kernel's net
selftests show no change. tc and ip were not built with UBSan.

Unmodified (was) and patched (now) tc and ip, sends and ioctls blocked:

  $ tc qdisc add dev lo root codel interval 4295s
    was: sends interval 32704us
    now: Illegal "interval"
  $ tc qdisc add dev lo root netem delay 9223372037s
    was: sends latency64 -9223372036854775808ns
    now: Illegal "latency"
  $ tc qdisc add dev lo root tbf rate 2e20bit burst 10k latency 1ms
    was: tbf: the "rate" parameter is mandatory.
    now: tbf: illegal value for "rate": "2e20bit"
  $ tc qdisc add dev lo root tbf rate 1gbit burst 10k latency 35s
    was: sends limit 80042944
    now: tbf: limit out of range
  $ tc class add dev lo parent 1: classid 1:1 hfsc sc umax 9000b dmax 100ms rate 1mbit
    was: sends d 96720us
    now: sends d 28000us
  $ tc qdisc add dev lo root sfb increment nan
    was: sends increment 0
    now: Illegal "increment"
  $ ip route add 192.0.2.0/24 dev lo rtt 4294968s
    was: sends rtt 705ms
    now: Error: argument "4294968s" is wrong: "rtt" value is invalid
  $ ip route add 192.0.2.0/24 dev lo rtt 536870912ms
    was: sends rtt 0 (536870912 * 8 wrapped)
    now: Error: argument "536870912ms" is wrong: "rtt" value is invalid
  $ ip link add t0 type ip6tnl remote 2001:db8::1 encaplimit 300
    was: sends an uninitialised byte (255 in one run)
    now: Error: argument "300" is wrong: invalid ELIM

 ip/ip6tunnel.c   |  2 +-
 ip/iproute.c     |  7 +++++--
 ip/link_ip6tnl.c |  2 +-
 lib/utils.c      | 39 +++++++++++++++------------------------
 lib/utils_math.c | 46 ++++++++++++++++++----------------------------
 misc/arpd.c      | 10 ++++++++--
 tc/q_hfsc.c      |  2 +-
 tc/q_sfb.c       |  6 +++++-
 tc/q_tbf.c       |  5 +++++
 tc/tc_util.c     | 10 +++++++---
 10 files changed, 66 insertions(+), 63 deletions(-)

diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c
index 347bd46a..68517b40 100644
--- a/ip/ip6tunnel.c
+++ b/ip/ip6tunnel.c
@@ -181,7 +181,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
 			} else {
 				__u8 uval;
 
-				if (get_u8(&uval, *argv, 0) < -1)
+				if (get_u8(&uval, *argv, 0))
 					invarg("invalid ELIM", *argv);
 				p->encap_limit = uval;
 				p->flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
diff --git a/ip/iproute.c b/ip/iproute.c
index 067569c2..06449a16 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -19,6 +19,7 @@
 #include <linux/in_route.h>
 #include <linux/icmpv6.h>
 #include <errno.h>
+#include <limits.h>
 
 #include "rt_names.h"
 #include "utils.h"
@@ -1332,7 +1333,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 +1434,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);
diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c
index c2a05cee..1809c8c1 100644
--- a/ip/link_ip6tnl.c
+++ b/ip/link_ip6tnl.c
@@ -195,7 +195,7 @@ static int ip6tunnel_parse_opt(struct link_util *lu, int argc, char **argv,
 			} else {
 				__u8 uval;
 
-				if (get_u8(&uval, *argv, 0) < -1)
+				if (get_u8(&uval, *argv, 0))
 					invarg("invalid ELIM", *argv);
 				encap_limit = uval;
 				flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
diff --git a/lib/utils.c b/lib/utils.c
index f4bd53ff..62e4887e 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -260,32 +260,22 @@ int get_float_min_max(float *val, const char *arg, float min, float max)
 int get_time_rtt(unsigned int *val, const char *arg, int *raw)
 {
 	double t;
-	unsigned long res;
+	long long res;
 	char *p;
 
 	if (strchr(arg, '.') != NULL) {
 		t = strtod(arg, &p);
-		if (t < 0.0)
-			return -1;
 
 		/* no digits? */
 		if (!p || p == arg)
 			return -1;
-
-		/* over/underflow */
-		if ((t == HUGE_VALF || t == HUGE_VALL) && errno == ERANGE)
-			return -1;
 	} else {
-		res = strtoul(arg, &p, 0);
+		res = strtoll(arg, &p, 0);
 
 		/* empty string? */
 		if (!p || p == arg)
 			return -1;
 
-		/* overflow */
-		if (res == ULONG_MAX && errno == ERANGE)
-			return -1;
-
 		t = (double)res;
 	}
 
@@ -307,6 +297,9 @@ int get_time_rtt(unsigned int *val, const char *arg, int *raw)
 			return -1;
 	}
 
+	if (t < 0 || t > UINT_MAX)
+		return -ERANGE;
+
 	/* emulate ceil() without having to bring-in -lm and always be >= 1 */
 	*val = t;
 	if (*val < t)
@@ -1702,12 +1695,6 @@ int get_time(unsigned int *time, const char *str)
 	if (p == str)
 		return -1;	/* empty string */
 
-	if (t < 0)
-		return -1;	/* negative value */
-
-	if (t == HUGE_VAL && errno == ERANGE)
-		return -1;	/* out of range */
-
 	if (*p) {
 		if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 ||
 		    strcasecmp(p, "secs") == 0)
@@ -1722,6 +1709,11 @@ int get_time(unsigned int *time, const char *str)
 			return -1;
 	}
 
+	if (isnan(t))
+		return -1;
+	if (t < 0 || t >= (double)UINT_MAX + 1)
+		return -ERANGE;
+
 	*time = t;
 	return 0;
 }
@@ -1754,12 +1746,6 @@ int get_time64(__s64 *time, const char *str)
 	if (p == str)
 		return -1;	/* empty string */
 
-	if (nsec < 0)
-		return -1;	/* negative value */
-
-	if (nsec == HUGE_VAL && errno == ERANGE)
-		return -1;	/* out of range */
-
 	if (*p) {
 		if (strcasecmp(p, "s") == 0 ||
 		    strcasecmp(p, "sec") == 0 ||
@@ -1781,6 +1767,11 @@ int get_time64(__s64 *time, const char *str)
 			return -1;
 	}
 
+	if (isnan(nsec))
+		return -1;
+	if (nsec < 0 || nsec >= (double)INT64_MAX + 1)
+		return -ERANGE;
+
 	*time = nsec;
 	return 0;
 }
diff --git a/lib/utils_math.c b/lib/utils_math.c
index 4b0ac266..2b823e12 100644
--- a/lib/utils_math.c
+++ b/lib/utils_math.c
@@ -45,12 +45,6 @@ int get_rate(unsigned int *rate, const char *str)
 	if (p == str)
 		return -1;	/* empty string */
 
-	if (bps < 0)
-		return -1;	/* negative */
-
-	if (bps == HUGE_VAL && errno == ERANGE)
-		return -1;	/* out of range */
-
 	for (s = suffixes; s->name; ++s) {
 		if (strcasecmp(s->name, p) == 0) {
 			bps *= s->scale;
@@ -63,10 +57,12 @@ 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))
 		return -1;
+	if (bps < 0 || bps >= (double)UINT_MAX + 1)
+		return -ERANGE;
+
+	*rate = bps;
 	return 0;
 }
 
@@ -79,12 +75,6 @@ int get_rate64(__u64 *rate, const char *str)
 	if (p == str)
 		return -1;	/* empty string */
 
-	if (bps < 0)
-		return -1;	/* negative */
-
-	if (bps == HUGE_VAL && errno == ERANGE)
-		return -1;	/* out of range */
-
 	for (s = suffixes; s->name; ++s) {
 		if (strcasecmp(s->name, p) == 0) {
 			bps *= s->scale;
@@ -97,6 +87,11 @@ int get_rate64(__u64 *rate, const char *str)
 		return -1; /* unknown suffix */
 
 	bps /= 8; /* -> bytes per second */
+	if (isnan(bps))
+		return -1;
+	if (bps < 0 || bps >= (double)UINT64_MAX + 1)
+		return -ERANGE;
+
 	*rate = bps;
 	return 0;
 }
@@ -110,12 +105,6 @@ int get_size64(__u64 *size, const char *str)
 	if (p == str)
 		return -1;	/* empty string */
 
-	if (sz < 0)
-		return -1;	/* negative */
-
-	if (sz == HUGE_VAL && errno == ERANGE)
-		return -1;	/* out of range */
-
 	if (*p) {
 		if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0)
 			sz *= 1024;
@@ -133,11 +122,12 @@ int get_size64(__u64 *size, const char *str)
 			return -1;
 	}
 
-	*size = sz;
-
-	/* detect if an overflow happened */
-	if (*size != floor(sz))
+	if (isnan(sz))
 		return -1;
+	if (sz < 0 || sz >= (double)UINT64_MAX + 1)
+		return -ERANGE;
+
+	*size = sz;
 
 	return 0;
 }
@@ -148,13 +138,13 @@ 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;
+		return -ERANGE;
+
+	*size = sz64;
 
 	return 0;
 }
diff --git a/misc/arpd.c b/misc/arpd.c
index a42603f6..96dc7edc 100644
--- a/misc/arpd.c
+++ b/misc/arpd.c
@@ -16,6 +16,8 @@
 #include <sys/ioctl.h>
 #include <poll.h>
 #include <errno.h>
+#include <limits.h>
+#include <math.h>
 #include <fcntl.h>
 #include <sys/uio.h>
 #include <sys/socket.h>
@@ -610,12 +612,16 @@ int main(int argc, char **argv)
 		case 'k':
 			no_kernel_broadcasts = 1;
 			break;
-		case 'p':
-			if ((poll_timeout = 1000 * strtod(optarg, NULL)) < 100) {
+		case 'p': {
+			double timeout = 1000 * strtod(optarg, NULL);
+
+			if (isnan(timeout) || timeout < 100 || timeout > INT_MAX) {
 				fprintf(stderr, "Invalid poll timeout\n");
 				exit(-1);
 			}
+			poll_timeout = timeout;
 			break;
+		}
 		case 'R':
 			if ((broadcast_rate = atoi(optarg)) <= 0 ||
 			    (broadcast_rate = 1000/broadcast_rate) <= 0) {
diff --git a/tc/q_hfsc.c b/tc/q_hfsc.c
index f68e8351..ff80c1fe 100644
--- a/tc/q_hfsc.c
+++ b/tc/q_hfsc.c
@@ -393,7 +393,7 @@ hfsc_get_sc2(int *argcp, char ***argvp, struct tc_service_curve *sc, const char
 		 * is at dmax - umax / rate
 		 */
 		sc->m1 = 0;
-		sc->d  = tc_core_time2ktime(ceil(dmax - umax * TIME_UNITS_PER_SEC / rate));
+		sc->d  = tc_core_time2ktime(ceil(dmax - (__u64)umax * TIME_UNITS_PER_SEC / rate));
 		sc->m2 = rate;
 	}
 
diff --git a/tc/q_sfb.c b/tc/q_sfb.c
index 6a26b71a..9ea7cc28 100644
--- a/tc/q_sfb.c
+++ b/tc/q_sfb.c
@@ -13,6 +13,8 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <math.h>
+#include <errno.h>
 
 #include "utils.h"
 #include "tc_util.h"
@@ -34,8 +36,10 @@ static int get_prob(__u32 *val, const char *arg)
 	if (!arg || !*arg)
 		return -1;
 	d = strtod(arg, &ptr);
-	if (!ptr || ptr == arg || d < 0.0 || d > 1.0)
+	if (!ptr || ptr == arg || *ptr || isnan(d))
 		return -1;
+	if (d < 0.0 || d > 1.0)
+		return -ERANGE;
 	*val = (__u32)(d * SFB_MAX_PROB + 0.5);
 	return 0;
 }
diff --git a/tc/q_tbf.c b/tc/q_tbf.c
index cbe11025..60c0b6d5 100644
--- a/tc/q_tbf.c
+++ b/tc/q_tbf.c
@@ -13,6 +13,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <errno.h>
 
 #include "utils.h"
 #include "tc_util.h"
@@ -208,6 +209,10 @@ static int tbf_parse_opt(const struct qdisc_util *qu, int argc, char **argv,
 			if (lim2 < lim)
 				lim = lim2;
 		}
+		if (lim >= (double)UINT_MAX + 1) {
+			fprintf(stderr, "tbf: limit out of range\n");
+			return -ERANGE;
+		}
 		opt.limit = lim;
 	}
 
diff --git a/tc/tc_util.c b/tc/tc_util.c
index 45d76e75..27e64d0e 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -175,6 +175,8 @@ int parse_percent(double *val, const char *str)
 	char *p;
 
 	*val = strtod(str, &p) / 100.;
+	if (p == str || isnan(*val))
+		return -1;
 	if (*val > 1.0 || *val < 0.0)
 		return 1;
 	if (*p && strcmp(p, "%"))
@@ -260,12 +262,14 @@ tc_print_rate(enum output_type t, const char *key, const char *fmt,
 int get_size64_and_cell(__u64 *size, int *cell_log, char *str)
 {
 	char *slash = strchr(str, '/');
+	int rv;
 
 	if (slash)
 		*slash = 0;
 
-	if (get_size64(size, str))
-		return -1;
+	rv = get_size64(size, str);
+	if (rv)
+		return rv;
 
 	if (slash) {
 		int cell;
@@ -296,7 +300,7 @@ int get_size_and_cell(unsigned int *size, int *cell_log, char *str)
 		return rv;
 
 	if (size64 > UINT32_MAX)
-		return -1;
+		return -ERANGE;
 
 	*size = size64;
 

base-commit: 3f4b36e5b10a2e83fbddd9471c10d2260a1532ed
-- 
2.53.0


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

* Re: [PATCH iproute2] lib, ip, tc, arpd: refuse out-of-range parsed values
  2026-12-01 18:59 [PATCH iproute2] lib, ip, tc, arpd: refuse out-of-range parsed values Andrii Tereshchenko
@ 2026-12-02  6:54 ` Stephen Hemminger
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Hemminger @ 2026-12-02  6:54 UTC (permalink / raw)
  To: Andrii Tereshchenko; +Cc: David Ahern, netdev

On Tue,  1 Dec 2026 10:59:50 -0800
Andrii Tereshchenko <opensource@andrii.net> wrote:

> Several parsers check a value before its unit is applied, or not at
> all, so one that does not fit wraps: tc turns "4295s" into 32704us and
> accepts "nan". ip route's rtt and rttvar scaling and tbf's limit from
> "latency" wrap too, and hfsc gets d wrong for umax above 4294 bytes.
> ip's encaplimit check (get_u8() < -1) never fires, so "encaplimit 300"
> sends an uninitialised byte.
> 
> Check once the unit is applied. The lib parsers return -1 for
> malformed input and NaN, -ERANGE for negative or too-large values.
> Refuse an overflowing rtt, rttvar or tbf limit, "%" without digits and
> trailing characters in an sfb probability. Fix the encaplimit test and
> hfsc's 32-bit multiply, and check arpd -p before storing it.

This is iproute2 next material and arpd has been removed.

This needs to be split into much smaller patches for human review.


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 18:59 [PATCH iproute2] lib, ip, tc, arpd: refuse out-of-range parsed values Andrii Tereshchenko
2026-12-02  6:54 ` Stephen Hemminger

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.