lib: vsprintf: scanf: Negative number must have field width > 1
Subsystems (5)
PATCH v3 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
@@ -3433,8 +3433,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
PATCH v3 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.
--- lib/kstrtox.c | 13 ++++++-- lib/kstrtox.h | 2 ++ lib/vsprintf.c | 88 +++++++++++++++++++++++++++++--------------------- 3 files changed, 64 insertions(+), 39 deletions(-)
lib/kstrtox.c +10 -3
@@ -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 ) { +for (; max_chars >0 ; 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 longreturn 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
@@ -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 +52 -36
129 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v3 2/4] lib: vsprintf: Fix handling of number field widths in vsscanf
Quoted 25 lines · Richard Fitzgerald
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:
Quoted 6 lines
sscanf("123456789abcdef", "%x", &i);Will not produce the correct result because the value obviously overflows
INT_MAX. But sscanf will report a successful conversion.Signed-off-by Richard Fitzgerald <[email protected]>
Best Regards,
Petr
PATCH v3 3/4 lib: test_scanf: Add tests for sscanf number conversion
In reply to Richard FitzgeraldAdds 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.
--- MAINTAINERS | 1 + lib/Kconfig.debug | 3 + lib/Makefile | 1 + lib/test_scanf.c | 747 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 752 insertions(+) create mode 100644 lib/test_scanf.c
MAINTAINERS +1 -0
@@ -18782,6 +18782,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
@@ -2054,6 +2054,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
@@ -85,6 +85,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 +747 -0
747 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v3 3/4] lib: test_scanf: Add tests for sscanf number conversion
Quoted 7 lines · Richard Fitzgerald
Adds
test_sscanfto test various number conversion cases, as number conversion was previously broken.This also tests the
simple_strtoxxx()functions exported fromvsprintf.c.Signed-off-by Richard Fitzgerald <[email protected]>
Best Regards,
Petr
PATCH v3 4/4 selftests: lib: Add wrapper script for test_scanf
In reply to Richard FitzgeraldAdds a wrapper shell script for the test_scanf module.
--- 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
@@ -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
@@ -1,4 +1,5 @@ CONFIG_TEST_PRINTF=m +CONFIG_TEST_SCANTF=m CONFIG_TEST_BITMAP=m CONFIG_PRIME_NUMBERS=m CONFIG_TEST_STRSCPY=m
tools/testing/selftests/lib/scanf.sh new file +4 -0
@@ -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
Quoted 8 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.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 change makes perfect sense. vsscanf() should always process only one character when the field width is 1.
Well, it has a potential to break existing users that rely on the broken behavior. Fortunately, there seems be only one:
drivers/net/wireless/intel/iwlegacy/3945-mac.c: if (sscanf(buf, "%1i", &ant) != 1) {It is used to set a device parameter: il3945_mod_params.antenna. There are three valid values:
enum il3945_antenna {
IL_ANTENNA_DIVERSITY,
IL_ANTENNA_MAIN,
IL_ANTENNA_AUX
};So, we should be on the safe side.
Anyway, adding people from
get_maintainer.pl drivers/net/wireless/intel/iwlegacy/3945-mac.c so that they are aware of this.
Quoted 1 line
Signed-off-by Richard Fitzgerald <[email protected]>
Best Regards,
Petr
Quoted 24 lines
--- lib/vsprintf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)lib/vsprintf.c +5
-1@@ -3433,8 +3433,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
Petr Mladek
maintainerSigh, I have just realized that Andy and Rasmus, the other vsprintf maintainers and reviewers, were not in CC. I am sorry for not noticing this earlier.
The patchset is ready for 5.12 from my POV.
Best Regards,
Petr
Quoted 34 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.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.
Signed-off-by Richard Fitzgerald <[email protected]>--- lib/vsprintf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)lib/vsprintf.c +5
-1@@ -3433,8 +3433,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
Quoted 6 lines · Petr Mladek
Sigh, I have just realized that Andy and Rasmus, the other vsprintf maintainers and reviewers, were not in CC. I am sorry for not noticing this earlier.
The patchset is ready for 5.12 from my POV.
Thanks, Petr!
I have one question, do we have a test case for that? If not, I prefer defer until a test case will be provided.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Petr Mladek
Sigh, I have just realized that Andy and Rasmus, the other vsprintf maintainers and reviewers, were not in CC. I am sorry for not noticing this earlier.
Quoted 7 lines
Quoted 1 line
The patchset is ready for 5.12 from my POV.
Thanks, Petr!
I have one question, do we have a test case for that? If not, I prefer defer until a test case will be provided.
See patch 3, numbers_prefix_overflow()
Quoted 9 lines · Richard Fitzgerald
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Petr Mladek
Sigh, I have just realized that Andy and Rasmus, the other vsprintf maintainers and reviewers, were not in CC. I am sorry for not noticing this earlier.
The patchset is ready for 5.12 from my POV.
Quoted 7 lines
Quoted 5 lines
Thanks, Petr!
I have one question, do we have a test case for that? If not, I prefer defer until a test case will be provided.
See patch 3,
numbers_prefix_overflow()
Sorry, I missed you off the original mailing so you won't have seen the other patches.
Patch 3 with the test cases is here:
https://lore.kernel.org/lkml/X%2FwnoJLEt0zQskDU@alley/T/#mf2ffba20126e438bea7af171bc78fdbebdb40027
Quoted 16 lines · Richard Fitzgerald
Quoted 15 lines · Richard Fitzgerald
Quoted 13 lines · Andy Shevchenko
Quoted 6 lines · Petr Mladek
Sigh, I have just realized that Andy and Rasmus, the other vsprintf maintainers and reviewers, were not in CC. I am sorry for not noticing this earlier.
The patchset is ready for 5.12 from my POV.
Thanks, Petr!
I have one question, do we have a test case for that? If not, I prefer defer until a test case will be provided.
Quoted 6 lines
Quoted 1 line
See patch 3,
numbers_prefix_overflow()Sorry, I missed you off the original mailing so you won't have seen the other patches.
Patch 3 with the test cases is here:
https://lore.kernel.org/lkml/X%2FwnoJLEt0zQskDU@alley/T/#mf2ffba20126e438bea7af171bc78fdbebdb40027
Good, you did it!
Couple of remarks:
- free resources in the same function where you allocated them
- use post increment where it doesn't matter (like total_test)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Petr Mladek
Sigh, I have just realized that Andy and Rasmus, the other vsprintf maintainers and reviewers, were not in CC.
Sorry, probably my fault. I sent to the maintainers and lists reported by get_maintainers.sh. I guess I missed that "reviewers" should also be directly mailed.
Quoted 42 lines
I am sorry for not noticing this earlier.
The patchset is ready for 5.12 from my POV.
Best Regards,
PetrQuoted 34 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.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.
Signed-off-by Richard Fitzgerald <[email protected]>--- lib/vsprintf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)lib/vsprintf.c +5
-1@@ -3433,8 +3433,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
Andy Shevchenko
reviewerQuoted 7 lines · Richard Fitzgerald
Quoted 2 lines · Petr Mladek
Sigh, I have just realized that Andy and Rasmus, the other vsprintf maintainers and reviewers, were not in CC.
Sorry, probably my fault. I sent to the maintainers and lists reported by
get_maintainers.sh. I guess I missed that "reviewers" should also be directly mailed.
I usually use the following (not 100% guarantee, but quite close):
scripts/get_maintainer.pl --git --git-min-percent=67
Signature · 2 lines
With Best Regards, Andy Shevchenko