get_time() and get_time64() check the number before the unit is applied, so "4295s" becomes 32704us and "nan" is accepted. get_rate() and get_size64() store first and detect the overflow after, which is already undefined; get_rate64() has no check at all, so "1e30bit" parses as 0. Check after scaling and before the store: -1 for malformed input and NaN, -ERANGE past the limit. Pass the result up through the cell helpers. Fixes: c99a85a7c8eb ("lib: add input validation for time, rate, and size parsing functions") Suggested-by: Stephen Hemminger Link: https://lore.kernel.org/netdev/20261130090749.593496fd@phoenix.local/ Signed-off-by: Andrii Tereshchenko --- codel interval 4295s: was interval 32704us, now Illegal "interval". tbf rate 2e20bit: was 'the "rate" parameter is mandatory', now illegal value for "rate". get_rate64("1e30bit"): was 0, now refused. lib/utils.c | 22 ++++++++++------------ lib/utils_math.c | 46 ++++++++++++++++++---------------------------- tc/tc_util.c | 8 +++++--- 3 files changed, 33 insertions(+), 43 deletions(-) diff --git a/lib/utils.c b/lib/utils.c index f4bd53ff..b23d8dd5 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -1702,12 +1702,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 +1716,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 +1753,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 +1774,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/tc/tc_util.c b/tc/tc_util.c index 45d76e75..5f03f950 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -260,12 +260,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 +298,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: 06b62d179bbb5b62cba965dcda48a613dbc59416 -- 2.53.0