]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lib: fix memparse() to handle overflow
authorDmitry Antipov <dmantipov@yandex.ru>
Tue, 19 May 2026 17:22:53 +0000 (20:22 +0300)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 29 May 2026 04:24:50 +0000 (21:24 -0700)
Since '_parse_integer_limit()' (and so 'simple_strtoull()') is now capable
to handle overflow, adjust 'memparse()' to handle overflow (denoted by
ULLONG_MAX) returned from 'simple_strtoull()'.  Also use
'check_shl_overflow()' to catch an overflow possibly caused by processing
size suffix and denote it with ULLONG_MAX as well.

Link: https://lore.kernel.org/20260519172259.908980-3-dmantipov@yandex.ru
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Charlie Jenkins <thecharlesjenkins@gmail.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
lib/cmdline.c

index 90ed997d9570169ade250a661046b7b18f4b8992..f6e4b113ca9f80a25d5c7bed55dadd97cc5e9313 100644 (file)
@@ -150,39 +150,46 @@ EXPORT_SYMBOL(get_options);
 unsigned long long memparse(const char *ptr, char **retptr)
 {
        char *endptr;   /* local pointer to end of parsed string */
-
        unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
+       unsigned int shl = 0;
 
+       /* Consume valid suffix even in case of overflow. */
        switch (*endptr) {
        case 'E':
        case 'e':
-               ret <<= 10;
+               shl += 10;
                fallthrough;
        case 'P':
        case 'p':
-               ret <<= 10;
+               shl += 10;
                fallthrough;
        case 'T':
        case 't':
-               ret <<= 10;
+               shl += 10;
                fallthrough;
        case 'G':
        case 'g':
-               ret <<= 10;
+               shl += 10;
                fallthrough;
        case 'M':
        case 'm':
-               ret <<= 10;
+               shl += 10;
                fallthrough;
        case 'K':
        case 'k':
-               ret <<= 10;
-               endptr++;
+               shl += 10;
                fallthrough;
        default:
                break;
        }
 
+       if (shl && likely(ptr != endptr)) {
+               /* Have valid suffix with preceding number. */
+               if (unlikely(check_shl_overflow(ret, shl, &ret)))
+                       ret = ULLONG_MAX;
+               endptr++;
+       }
+
        if (retptr)
                *retptr = endptr;