[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260123114647.1606335-2-dmantipov@yandex.ru>
Date: Fri, 23 Jan 2026 14:46:45 +0300
From: Dmitry Antipov <dmantipov@...dex.ru>
To: Andy Shevchenko <andriy.shevchenko@...el.com>
Cc: Andrew Morton <akpm@...ux-foundation.org>,
Kees Cook <kees@...nel.org>,
"Darrick J . Wong" <djwong@...nel.org>,
linux-hardening@...r.kernel.org,
Dmitry Antipov <dmantipov@...dex.ru>
Subject: [PATCH v2 1/3] lib: fix _parse_integer_limit() to handle overflow
In '_parse_integer_limit()', replace native integer arithmetic with
'check_mul_overflow()' and 'check_add_overflow()' to check whether
an intermediate result goes out of range, and denote such a case
with ULLONG_MAX. Adjust comment to kernel-doc style as well.
Signed-off-by: Dmitry Antipov <dmantipov@...dex.ru>
---
v2: initial version to join the series
---
lib/kstrtox.c | 32 ++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index bdde40cd69d7..a416cdafee37 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -39,20 +39,26 @@ const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
return s;
}
-/*
- * Convert non-negative integer string representation in explicitly given radix
- * to an integer. A maximum of max_chars characters will be converted.
+/**
+ * _parse_integer_limit - Convert integer string representation to an integer
+ * @s: Integer string representation
+ * @base: Radix
+ * @p: Where to store result
+ * @max_chars: Maximum amount of characters to convert
+ *
+ * Convert non-negative integer string representation in explicitly given
+ * radix to an integer. If overflow occurs, value at @p is set to ULLONG_MAX.
*
- * Return number of characters consumed maybe or-ed with overflow bit.
- * If overflow occurs, result integer (incorrect) is still returned.
+ * This function is the workhorse of other string conversion functions and
+ * you do not expect to use it explicitly. Consider kstrto*() famlily instead.
*
- * Don't you dare use this function.
+ * Return: Number of characters consumed, maybe ORed with overflow bit
*/
noinline
unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *p,
size_t max_chars)
{
- unsigned long long res;
+ unsigned long long tmp, res;
unsigned int rv;
res = 0;
@@ -72,14 +78,16 @@ unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned lon
if (val >= base)
break;
/*
- * Check for overflow only if we are within range of
- * it in the max base we support (16)
+ * Accumulate result if no overflow detected.
+ * Otherwise just consume valid characters.
*/
- if (unlikely(res & (~0ull << 60))) {
- if (res > div_u64(ULLONG_MAX - val, base))
+ if (res != ULLONG_MAX) {
+ if (check_mul_overflow(res, base, &tmp) ||
+ check_add_overflow(tmp, val, &res)) {
+ res = ULLONG_MAX;
rv |= KSTRTOX_OVERFLOW;
+ }
}
- res = res * base + val;
rv++;
s++;
}
--
2.52.0
Powered by blists - more mailing lists