[PATCH v8 2/5] lib: fix memparse() to handle overflow

Dmitry Antipov posted 5 patches 1 month, 2 weeks ago
[PATCH v8 2/5] lib: fix memparse() to handle overflow
Posted by Dmitry Antipov 1 month, 2 weeks ago
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.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v8: do not use temporary in check_shl_overflow()
v7: do not double-adjust endptr and drop
    redundant check against ULLONG_MAX
v6: handle valid-suffix-only string like "k"
    as unrecognized, minor style adjustments
v5: initial version to join the series
---
 lib/cmdline.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/lib/cmdline.c b/lib/cmdline.c
index 90ed997d9570..f6e4b113ca9f 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -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;
 
-- 
2.53.0
Re: [PATCH v8 2/5] lib: fix memparse() to handle overflow
Posted by Rodrigo Alencar 1 month ago
On 26/02/12 07:44PM, Dmitry Antipov wrote:
> 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.

is returning ULLONG_MAX the proper way to indicate overflow with
simple_strtoull()?

...

>  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);

... or a new wrapper for _parse_integer_limit() is needed?

I am interested on having _parse_integer_limit() overflow check
exposed to kernel modules.
...

-- 
Kind regards,

Rodrigo Alencar
Re: [PATCH v8 2/5] lib: fix memparse() to handle overflow
Posted by Rodrigo Alencar 1 month ago
On 26/02/24 12:32PM, Rodrigo Alencar wrote:
> On 26/02/12 07:44PM, Dmitry Antipov wrote:
> > 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.
> 
> is returning ULLONG_MAX the proper way to indicate overflow with
> simple_strtoull()?
> 
> ...
> 
> >  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);
> 
> ... or a new wrapper for _parse_integer_limit() is needed?
> 
> I am interested on having _parse_integer_limit() overflow check
> exposed to kernel modules.
> ...

I could propose the exposure of simple_strntoull() which is private
to lib/vsprintf.c, slightly changing its prototype:

* from:
unsigned long long simple_strntoull(const char *startp,
				    char **endp,
				    unsigned int base,
				    size_t max_chars)
* to:
int simple_strntoull(const char *startp,
		     char **endp,
		     unsigned long long *result,
		     unsigned int base,
		     size_t max_chars)

That would allow to tackle the FIXME tag. I suppose it is there
because the overflow flag is being ignored, so the returned integer
can be set to -ERANGE or -EOVERFLOW when overflow happens.

-- 
Kind regards,

Rodrigo Alencar
Re: [PATCH v8 2/5] lib: fix memparse() to handle overflow
Posted by Dmitry Antipov 1 month ago
On Tue, 2026-02-24 at 14:15 +0000, Rodrigo Alencar wrote:

> > I am interested on having _parse_integer_limit() overflow check
> > exposed to kernel modules.

Hm... _parse_integer_limit() may be EXPORT_SYMBOL()ed of course.
But are you sure that current API is not enough for your task?

> > ...
> 
> I could propose the exposure of simple_strntoull() which is private
> to lib/vsprintf.c, slightly changing its prototype:
> 
> * from:
> unsigned long long simple_strntoull(const char *startp,
> 				    char **endp,
> 				    unsigned int base,
> 				    size_t max_chars)
> * to:
> int simple_strntoull(const char *startp,
> 		     char **endp,
> 		     unsigned long long *result,
> 		     unsigned int base,
> 		     size_t max_chars)
> 
> That would allow to tackle the FIXME tag. I suppose it is there
> because the overflow flag is being ignored, so the returned integer
> can be set to -ERANGE or -EOVERFLOW when overflow happens.

Well, removal of KSTRTOX_OVERFLOW quirk may be a subject for the next series.

Dmitry