Skip to contentSkip to conversation

lib: parser: reject out-of-range values in match_number()

PATCH In review Shashank Mohan Jain 2 participants 4 messages Oct 5 – Oct 6 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 lib: parser: reject out-of-range values in match_number()

match_int(), match_octal() and match_hex() store the result in an int and return -EINVAL or -ERANGE on failure. match_number() checks the range by parsing into a long with simple_strtol() and comparing against INT_MIN/INT_MAX, a check added by commit 77dd3b0bd17a ("lib/parser.c:
avoid overflow in match_number()"). That does not catch every out-of-range input:

- simple_strtoull() saturates to ULLONG_MAX on overflow and simple_strtol() simply converts its result to long, so any value of at least 2^64 - 2^31, and anything that overflows 64 bits, ends up inside the int range. On 64-bit, match_int() returns 0 and sets the result to -1 for "18446744073709551615" or "99999999999999999999", match_hex() does the same for "ffffffffffffffff", and "-18446744073709551615" gives 1.

- On 32-bit, long has the same width as int, so the range check can never fail: "2147483648" gives INT_MIN and "4294967295" gives -1.

These helpers parse mount options and similar user-supplied strings, so an out-of-range number is silently accepted as a different value instead of being rejected.

Use kstrtol(), as the comment above simple_strtol() recommends. It returns -ERANGE for values that don't fit in a long, which on 32-bit is the whole int range check, and the INT_MIN/INT_MAX check covers 64-bit. The substrings passed in come from match_one(), which ends a %d, %o or %x argument where simple_strtol()/simple_strtoul() stops, so kstrtol() sees the same characters and accepts the same values in the int range as before.

Fixes 77dd3b0bd17a ("lib/parser.c: avoid overflow in match_number()")
Assisted-by LLM

---
These patches were prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5): the analysis, the Lean models used to find and check the bugs, the fix, the tests, and the check of the in-tree callers for v3.

Changes in v3:
- Use kstrtol() as suggested by Alex Elder, instead of open-coding the parse with _parse_integer(); the changelog says why it accepts the same values for the callers. The existing "int ret" and "long val" declarations are kept, so the diff only replaces the parse. Patch 2/2 (the KUnit test) is unchanged.

v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

 lib/parser.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
lib/parser.c +7 -10
index 62da0ac..5e3abf0 100644
@@ -137,22 +137,19 @@ EXPORT_SYMBOL(match_token);
  */
 static int match_number(substring_t *s, int *result, int base)
 {
-	char *endp;
 	char buf[NUMBER_BUF_LEN];
 	int ret;
 	long val;
 
 	if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >= NUMBER_BUF_LEN)
 		return -ERANGE;
-	ret = 0;
-	val = simple_strtol(buf, &endp, base);
-	if (endp == buf)
-		ret = -EINVAL;
-	else if (val < (long)INT_MIN || val > (long)INT_MAX)
-		ret = -ERANGE;
-	else
-		*result = (int) val;
-	return ret;
+	ret = kstrtol(buf, base, &val);
+	if (ret)
+		return ret;
+	if (val < (long)INT_MIN || val > (long)INT_MAX)
+		return -ERANGE;
+	*result = (int) val;
+	return 0;
 }
 
 /**
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH v3 2/2 lib/tests: add KUnit test for parser number helpers

Add a small KUnit suite for match_int(), match_octal() and match_hex(), covering values at and just beyond the int range, values that overflow 64 bits, and a match_token() round trip with an out-of-range argument.

Assisted-by LLM

---
Changes in v3:
- None (the test passes with the kstrtol() based match_number() of patch 1/2).

 lib/Kconfig.debug        |  12 ++++
 lib/tests/Makefile       |   1 +
 lib/tests/parser_kunit.c | 131 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100644 lib/tests/parser_kunit.c
lib/Kconfig.debug +12 -0
index 134b15a44625..905ccec61868 100644
@@ -2918,6 +2918,18 @@ config CMDLINE_KUNIT_TEST
 
 	  If unsure, say N.
 
+config PARSER_KUNIT_TEST
+	tristate "KUnit test for parser number helpers" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the unit test for the number parsing helpers in
+	  lib/parser.c (match_int(), match_octal(), match_hex()).
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config BASE64_KUNIT
 	tristate "KUnit test for base64 decoding and encoding" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/tests/Makefile +1 -0
index 3cac3b63a752..cbabf4939ce8 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
 obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o
 CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
 obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
+obj-$(CONFIG_PARSER_KUNIT_TEST) += parser_kunit.o
 obj-$(CONFIG_PRANDOM_KUNIT_TEST) += random32_kunit.o
 obj-$(CONFIG_PRINTF_KUNIT_TEST) += printf_kunit.o
 obj-$(CONFIG_RANDSTRUCT_KUNIT_TEST) += randstruct_kunit.o
lib/tests/parser_kunit.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw
In reply to Shashank Mohan Jain
Quoted 72 lines · Shashank Mohan Jain

match_int(), match_octal() and match_hex() store the result in an int and return -EINVAL or -ERANGE on failure. match_number() checks the range by parsing into a long with simple_strtol() and comparing against INT_MIN/INT_MAX, a check added by commit 77dd3b0bd17a ("lib/parser.c:
avoid overflow in match_number()"). That does not catch every out-of-range input:

- simple_strtoull() saturates to ULLONG_MAX on overflow and simple_strtol() simply converts its result to long, so any value of at least 2^64 - 2^31, and anything that overflows 64 bits, ends up inside the int range. On 64-bit, match_int() returns 0 and sets the result to -1 for "18446744073709551615" or "99999999999999999999", match_hex() does the same for "ffffffffffffffff", and "-18446744073709551615" gives 1.

- On 32-bit, long has the same width as int, so the range check can

    never fail: "2147483648" gives INT_MIN and "4294967295" gives -1.

These helpers parse mount options and similar user-supplied strings, so an out-of-range number is silently accepted as a different value instead of being rejected.

Use kstrtol(), as the comment above simple_strtol() recommends. It returns -ERANGE for values that don't fit in a long, which on 32-bit is the whole int range check, and the INT_MIN/INT_MAX check covers 64-bit. The substrings passed in come from match_one(), which ends a %d, %o or %x argument where simple_strtol()/simple_strtoul() stops, so kstrtol() sees the same characters and accepts the same values in the int range as before.

Fixes 77dd3b0bd17a ("lib/parser.c: avoid overflow in match_number()")
Assisted-by LLM

---
These patches were prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5): the analysis, the Lean models used to find and check the bugs, the fix, the tests, and the check of the in-tree callers for v3.

Changes in v3:
- Use kstrtol() as suggested by Alex Elder, instead of open-coding the parse with _parse_integer(); the changelog says why it accepts the same values for the callers. The existing "int ret" and "long val" declarations are kept, so the diff only replaces the parse. Patch 2/2 (the KUnit test) is unchanged.

v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

  lib/parser.c | 17 +++++++----------
  1 file changed, 7 insertions(+), 10 deletions(-)
lib/parser.c +0 -6
index 62da0ac..5e3abf0 100644
@@ -137,22 +137,19 @@ EXPORT_SYMBOL(match_token);
   */
  static int match_number(substring_t *s, int *result, int base)
  {
-	char *endp;
  	char buf[NUMBER_BUF_LEN];
  	int ret;
  	long val;
 
  	if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >= NUMBER_BUF_LEN)
  		return -ERANGE;
-	ret = 0;
-	val = simple_strtol(buf, &endp, base);
-	if (endp == buf)
-		ret = -EINVAL;
-	else if (val < (long)INT_MIN || val > (long)INT_MAX)

lib/parser.c EXPORT_SYMBOL(match_token); · line 151

Quoted 10 lines
lib/parser.c +6 -4
-		ret = -ERANGE;
-	else
-		*result = (int) val;
-	return ret;
+	ret = kstrtol(buf, base, &val);
+	if (ret)
+		return ret;
+	if (val < (long)INT_MIN || val > (long)INT_MAX)
+		return -ERANGE;
+	*result = (int) val;

For consistency, there should be no space between the right parenthesis in the cast and val. This could probably be fixed by the maintainer if this gets accepted.

-Alex

Quoted 4 lines
lib/parser.c +1 -0
+	return 0;
  }
 
  /**
#3 lore raw
Quoted 3 lines · Alex Elder

For consistency, there should be no space between the right parenthesis in the cast and val. This could probably be fixed by the maintainer if this gets accepted.

Thanks, Alex. That line was carried over unchanged from the old code, but you're right that it should be "(int)val".

Andrew, if you apply this, could you drop the space when you do? Otherwise I'm happy to send a v4 with just that change.

Thanks,
Shashank

Quoted 96 lines · Alex Elder
Quoted 72 lines · Shashank Mohan Jain

match_int(), match_octal() and match_hex() store the result in an int and return -EINVAL or -ERANGE on failure. match_number() checks the range by parsing into a long with simple_strtol() and comparing against INT_MIN/INT_MAX, a check added by commit 77dd3b0bd17a ("lib/parser.c:
avoid overflow in match_number()"). That does not catch every out-of-range input:

- simple_strtoull() saturates to ULLONG_MAX on overflow and simple_strtol() simply converts its result to long, so any value of at least 2^64 - 2^31, and anything that overflows 64 bits, ends up inside the int range. On 64-bit, match_int() returns 0 and sets the result to -1 for "18446744073709551615" or "99999999999999999999", match_hex() does the same for "ffffffffffffffff", and "-18446744073709551615" gives 1.

- On 32-bit, long has the same width as int, so the range check can

    never fail: "2147483648" gives INT_MIN and "4294967295" gives -1.

These helpers parse mount options and similar user-supplied strings, so an out-of-range number is silently accepted as a different value instead of being rejected.

Use kstrtol(), as the comment above simple_strtol() recommends. It returns -ERANGE for values that don't fit in a long, which on 32-bit is the whole int range check, and the INT_MIN/INT_MAX check covers 64-bit. The substrings passed in come from match_one(), which ends a %d, %o or %x argument where simple_strtol()/simple_strtoul() stops, so kstrtol() sees the same characters and accepts the same values in the int range as before.

Fixes 77dd3b0bd17a ("lib/parser.c: avoid overflow in match_number()")
Assisted-by LLM

---
These patches were prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5): the analysis, the Lean models used to find and check the bugs, the fix, the tests, and the check of the in-tree callers for v3.

Changes in v3:
- Use kstrtol() as suggested by Alex Elder, instead of open-coding the parse with _parse_integer(); the changelog says why it accepts the same values for the callers. The existing "int ret" and "long val" declarations are kept, so the diff only replaces the parse. Patch 2/2 (the KUnit test) is unchanged.

v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

  lib/parser.c | 17 +++++++----------
  1 file changed, 7 insertions(+), 10 deletions(-)
lib/parser.c +0 -6
index 62da0ac..5e3abf0 100644
@@ -137,22 +137,19 @@ EXPORT_SYMBOL(match_token);
   */
  static int match_number(substring_t *s, int *result, int base)
  {
-     char *endp;
      char buf[NUMBER_BUF_LEN];
      int ret;
      long val;
 
      if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >= NUMBER_BUF_LEN)
              return -ERANGE;
-     ret = 0;
-     val = simple_strtol(buf, &endp, base);
-     if (endp == buf)
-             ret = -EINVAL;
-     else if (val < (long)INT_MIN || val > (long)INT_MAX)

lib/parser.c EXPORT_SYMBOL(match_token); · line 151

Quoted 10 lines
lib/parser.c +6 -4
-             ret = -ERANGE;
-     else
-             *result = (int) val;
-     return ret;
+     ret = kstrtol(buf, base, &val);
+     if (ret)
+             return ret;
+     if (val < (long)INT_MIN || val > (long)INT_MAX)
+             return -ERANGE;
+     *result = (int) val;

For consistency, there should be no space between the right parenthesis in the cast and val. This could probably be fixed by the maintainer if this gets accepted.

-Alex

Quoted 4 lines
lib/parser.c +1 -0
+     return 0;
  }
 
  /**