Skip to contentSkip to conversation

lib: vsprintf: scanf: Negative number must have field width > 1

PATCH RESEND In review Richard Fitzgerald 2 participants 5 messages May 14, 2021 – May 20, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND v8 1/4 lib: vsprintf: scanf: Negative number must have field width > 1

If a signed number field starts with a '-' the field width must be > 1, or unlimited, to allow at least one digit after the '-'.

This patch adds a check for this. If a signed field starts with '-' and field_width == 1 the scanf will quit.

It is ok for a signed number field to have a field width of 1 if it starts with a digit. In that case the single digit can be converted.

---
 lib/vsprintf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/vsprintf.c +5 -1
index 41ddc353ebb8..f78651e9b030 100644
@@ -3466,8 +3466,12 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
 		str = skip_spaces(str);
 
 		digit = *str;
-		if (is_sign && digit == '-')
+		if (is_sign && digit == '-') {
+			if (field_width == 1)
+				break;
+
 			digit = *(str + 1);
+		}
 
 		if (!digit
 		    || (base == 16 && !isxdigit(digit))
Signature · 1 line
2.20.1
#1 lore raw

PATCH RESEND v8 2/4 lib: vsprintf: Fix handling of number field widths in vsscanf

The existing code attempted to handle numbers by doing a strto[u]l(), ignoring the field width, and then repeatedly dividing to extract the field out of the full converted value. If the string contains a run of valid digits longer than will fit in a long or long long, this would overflow and no amount of dividing can recover the correct value.

This patch fixes vsscanf() to obey number field widths when parsing the number.

A new _parse_integer_limit() is added that takes a limit for the number of characters to parse. The number field conversion in vsscanf is changed to use this new function.

If a number starts with a radix prefix, the field width must be long enough for at last one digit after the prefix. If not, it will be handled like this:

sscanf("0x4", "%1i", &i): i=0, scanning continues with the 'x' sscanf("0x4", "%2i", &i): i=0, scanning continues with the '4'

This is consistent with the observed behaviour of userland sscanf.

Note that this patch does NOT fix the problem of a single field value overflowing the target type. So for example:

  sscanf("123456789abcdef", "%x", &i);

Will not produce the correct result because the value obviously overflows INT_MAX. But sscanf will report a successful conversion.

Note that where a very large number is used to mean "unlimited", the value INT_MAX is used for consistency with the behaviour of vsnprintf().

---
Changed since v6/v7:
Reverted unnecessary change in simple_strtoul(). The existing code that calls simple_strtoull() is ok.

This is a trivial change so I've kept Petr's Reviewed-by.

---
 lib/kstrtox.c  | 13 ++++++--
 lib/kstrtox.h  |  2 ++
 lib/vsprintf.c | 86 +++++++++++++++++++++++++++++---------------------
 3 files changed, 62 insertions(+), 39 deletions(-)
lib/kstrtox.c +10 -3
index a118b0b1e9b2..0b5fe8b41173 100644
@@ -39,20 +39,22 @@ const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
 
 /*
  * Convert non-negative integer string representation in explicitly given radix
- * to an integer.
+ * to an integer. A maximum of max_chars characters will be converted.
+ *
  * Return number of characters consumed maybe or-ed with overflow bit.
  * If overflow occurs, result integer (incorrect) is still returned.
  *
  * Don't you dare use this function.
  */
-unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
+unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *p,
+				  size_t max_chars)
 {
 	unsigned long long res;
 	unsigned int rv;
 
 	res = 0;
 	rv = 0;
-	while (1) {
+	while (max_chars--) {
 		unsigned int c = *s;
 		unsigned int lc = c | 0x20; /* don't tolower() this line */
 		unsigned int val;
@@ -82,6 +84,11 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
 	return rv;
 }
 
+unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
+{
+	return _parse_integer_limit(s, base, p, INT_MAX);
+}
+
 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
 {
 	unsigned long long _res;
lib/kstrtox.h +2 -0
index 3b4637bcd254..158c400ca865 100644
@@ -4,6 +4,8 @@
 
 #define KSTRTOX_OVERFLOW	(1U << 31)
 const char *_parse_integer_fixup_radix(const char *s, unsigned int *base);
+unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *res,
+				  size_t max_chars);
 unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *res);
 
 #endif
lib/vsprintf.c +50 -36

120 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

PATCH RESEND v8 3/4 lib: test_scanf: Add tests for sscanf number conversion

In reply to Richard Fitzgerald

Adds test_sscanf to test various number conversion cases, as number conversion was previously broken.

This also tests the simple_strtoxxx() functions exported from vsprintf.c.

---
Changed since v6:
Use the KSTM_MODULE_GLOBALS define:

	-static unsigned total_tests __initdata;
	-static unsigned failed_tests __initdata;
	+KSTM_MODULE_GLOBALS();

As this is a trivial change I have kept Andy Shevchenko's ack.

---
 MAINTAINERS       |   1 +
 lib/Kconfig.debug |   3 +
 lib/Makefile      |   1 +
 lib/test_scanf.c  | 751 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 756 insertions(+)
 create mode 100644 lib/test_scanf.c
MAINTAINERS +1 -0
index fb2a3633b719..712812b2142b 100644
@@ -19225,6 +19225,7 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/pmladek/printk.git
 F:	Documentation/core-api/printk-formats.rst
 F:	lib/test_printf.c
+F:	lib/test_scanf.c
 F:	lib/vsprintf.c
 
 VT1211 HARDWARE MONITOR DRIVER
lib/Kconfig.debug +3 -0
index 2779c29d9981..4cfaf2a817f9 100644
@@ -2123,6 +2123,9 @@ config TEST_KSTRTOX
 config TEST_PRINTF
 	tristate "Test printf() family of functions at runtime"
 
+config TEST_SCANF
+	tristate "Test scanf() family of functions at runtime"
+
 config TEST_BITMAP
 	tristate "Test bitmap_*() family of functions at runtime"
 	help
lib/Makefile +1 -0
index b5307d3eec1a..5433d3f43361 100644
@@ -83,6 +83,7 @@ obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
+obj-$(CONFIG_TEST_SCANF) += test_scanf.o
 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
 obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
 obj-$(CONFIG_TEST_UUID) += test_uuid.o
lib/test_scanf.c new file +751 -0

751 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw

PATCH RESEND v8 4/4 selftests: lib: Add wrapper script for test_scanf

In reply to Richard Fitzgerald

Adds a wrapper shell script for the test_scanf module.

---
Changed since v6:
Fixed typo in tools/testing/selftests/lib/config:

-CONFIG_TEST_SCANTF=m
+CONFIG_TEST_SCANF=m

As this is a trivial change I have kept Petr Mladek's Reviewed-by and Andy Shevchenko's ack.

---
 tools/testing/selftests/lib/Makefile | 2 +-
 tools/testing/selftests/lib/config   | 1 +
 tools/testing/selftests/lib/scanf.sh | 4 ++++
 3 files changed, 6 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/lib/scanf.sh
tools/testing/selftests/lib/Makefile +1 -1
index a105f094676e..ee71fc99d5b5 100644
@@ -4,6 +4,6 @@
 # No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
 all:
 
-TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh strscpy.sh
+TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh scanf.sh strscpy.sh
 
 include ../lib.mk
tools/testing/selftests/lib/config +1 -0
index b80ee3f6e265..645839b50b0a 100644
@@ -1,4 +1,5 @@
 CONFIG_TEST_PRINTF=m
+CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_PRIME_NUMBERS=m
 CONFIG_TEST_STRSCPY=m
tools/testing/selftests/lib/scanf.sh new file +4 -0
new file mode 100755
index 000000000000..b59b8ba561c3
@@ -0,0 +1,4 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# Tests the scanf infrastructure using test_scanf kernel module.
+$(dirname $0)/../kselftest/module.sh "scanf" test_scanf
Signature · 1 line
2.20.1
maintainer
#4 lore raw
In reply to Richard Fitzgerald
Quoted 6 lines · Richard Fitzgerald

If a signed number field starts with a '-' the field width must be > 1, or unlimited, to allow at least one digit after the '-'.

This patch adds a check for this. If a signed field starts with '-' and field_width == 1 the scanf will quit.

Quoted 6 lines

It is ok for a signed number field to have a field width of 1 if it starts with a digit. In that case the single digit can be converted.

The entrire patchset has been committed into print/linux.git, branch for-5.14-vsprintf-scanf.

Thanks for reminder. I am sorry. It somehow fallen from my radar.

Best Regards,
Petr