Make sscanf safer
Subsystems (2)
cover Make sscanf safer
Standard sscanf isn't well designed for input validation:
* no way to detect interger overflow
* unmached tail text is ignored
* no mandatory buffer overflow checks
All these problems were found in the wild in cgroup interfaces:
https://patchwork.kernel.org/patch/10831387/
https://lore.kernel.org/patchwork/patch/1046130/
This patchset has patches for first two issues.
Unbounded "%s" is out of scope for now.
Second patch handles integer overlow as parse error:
sscanf exits without touching related argument.
Third patch adds convenient way for checking that
whole text was matched and nothing left.
Fourth adds missing standard features for %[...],
this might be useful for input validation.
Fifth adds __must_check and will generate some new warnings.
The last is a simple test module for chechking some basic and new corner-cases added by this patchset.
Code haven't chaneged much:
$ ./scripts/bloat-o-meter lib/vsprintf.o.old lib/vsprintf.o.new add/remove: 0/0 grow/shrink: 1/0 up/down: 60/0 (60) Function old new delta vsscanf 2270 2330 +60 Total: Before=16904, After=16964, chg +0.35%
Unbounded "%s" could be fixed only by making field width mandatory. There are only few users and in most cases sscanf could be eliminated.
---
Konstantin Khlebnikov (6):
lib: scanf: document features of scanf format string
lib: scanf: handle integer overflows in vsscanf
lib: scanf: add vsscanf feature for matching end of text
lib: scanf: handle character ranges in %[...]
lib: scanf: mark sscanf and vsscanf as __must_check
lib: scanf: add test module
include/linux/kernel.h | 10 ++ lib/Kconfig.debug | 3 + lib/Makefile | 1 lib/test_scanf.c | 252 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/vsprintf.c | 153 +++++++++++++++++++++-------- 5 files changed, 376 insertions(+), 43 deletions(-) create mode 100644 lib/test_scanf.c
--
Signature
PATCH 1/6 lib: scanf: document features of scanf format string
Kernel implementation of vsscanf() has some limitations.
--- lib/vsprintf.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
lib/vsprintf.c +28 -0
@@ -3004,6 +3004,28 @@ EXPORT_SYMBOL_GPL(bprintf);* @buf: input buffer * @fmt: format of buffer * @args: arguments +* +* Returns number of successfully matched arguments. +* +* This function implements only basic vsscanf features: +* +* - conversion specifiers: d, i, o, u, x, X, s, c, [, n +* - type modifiers: hh, h, l, L, ll, z +* +* Not implemented features: +* +* - floating-point numbers +* - memory-allocation "%m..." +* - pointer "%p", ptrdiff_t "%t...", intmax_t "%j..." +* - discaring of matching input "%*[...]" +* - ranges or matching ']' in "%[...]" +* - positional assignment "%n$" +* +* Non-standatd features: +* +* - "%[...]" requires field width +* - %s without field width limited with SHRT_MAX +* - "%*..." simply skips non white-space characters without conversion */ int vsscanf (const char *buf,const char *fmt,va_list args) {
@@ -3291,6 +3313,12 @@ EXPORT_SYMBOL(vsscanf);* @buf: input buffer * @fmt: formatting of buffer * @...: resulting arguments +* +* Returns number of successfully matched arguments. +* +* See format description in comment for vsscanf() above. +* +* For single integer conversion it is better to use kstrto*(). */ int sscanf (const char *buf,const char *fmt, ...) {
PATCH 2/6 lib: scanf: handle integer overflows in vsscanf
In reply to Konstantin KhlebnikovTraditional scanf implementations ignore integer overflows because C language standard allows here undefined behavior (§7.21.6.2 #10).
So, sane and safe behavior wouldn't harm anything.
This patch carefully checks integer overflows and stops matching if result does not fit into appropriate type before assigning it into argument.
--- lib/vsprintf.c | 86 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 30 deletions(-)
lib/vsprintf.c +56 -30
@@ -3026,12 +3026,13 @@ EXPORT_SYMBOL_GPL(bprintf);* - "%[...]" requires field width * - %s without field width limited with SHRT_MAX * - "%*..." simply skips non white-space characters without conversion +* - integer overflows are handled as matching failure */ int vsscanf (const char *buf,const char *fmt,va_list args) {const char *str = buf; -char *next; -char digit; +const char *next; +unsigned int rv;int num =0 ;u8 qualifier;unsigned int base;
@@ -3230,29 +3231,31 @@ int vsscanf(const char *buf, const char *fmt, va_list args)*/ str =skip_spaces (str); - digit = *str; -if (is_sign && digit =='-' ) - digit = *(str +1 ); + next = str; -if (!digit - || (base ==16 && !isxdigit (digit)) - || (base ==10 && !isdigit (digit)) - || (base ==8 && (!isdigit (digit) || digit >'7' )) - || (base ==0 && !isdigit (digit))) -break ; +/* skip leading sign */ +if (is_sign && (*str =='+' || *str =='-' )) + next++; -if (is_sign) - val.s = qualifier !='L' ? -simple_strtol (str, &next, base) : -simple_strtoll (str, &next, base); -else - val.u = qualifier !='L' ? -simple_strtoul (str, &next, base) : -simple_strtoull (str, &next, base); +/* 64-bit integer conversion, similar to _kstrtoull() */ + next =_parse_integer_fixup_radix (next, &base); + rv =_parse_integer (next, base, &val.u); +if (rv ==0 ) +break ; +if (rv &KSTRTOX_OVERFLOW ) + goto overflow; + next += rv; + +if (is_sign) { +if (*str =='-' ) { + val.s = -val.u; +if (val.s >0 ) + goto overflow; + }else if (val.s <0 ) + goto overflow; + } -if (field_width >0 && next - str > field_width) { -if (base ==0 ) -_parse_integer_fixup_radix (str, &base); +if (field_width >0 ) {while (next - str > field_width) {if (is_sign) val.s =div_s64 (val.s, base);
@@ -3264,22 +3267,37 @@ int vsscanf(const char *buf, const char *fmt, va_list args)switch (qualifier) {case 'H' :/* that's 'hh' in format */ -if (is_sign) +if (is_sign) { +if ((signed char )val.s != val.s) + goto overflow; *va_arg(args, signed char *) = val.s; -else + }else { +if ((unsigned char )val.u != val.u) + goto overflow; *va_arg(args, unsigned char *) = val.u; + }break ;case 'h' : -if (is_sign) +if (is_sign) { +if ((short )val.s != val.s) + goto overflow; *va_arg(args, short *) = val.s; -else + }else { +if ((unsigned short )val.u != val.u) + goto overflow; *va_arg(args, unsigned short *) = val.u; + }break ;case 'l' : -if (is_sign) +if (is_sign) { +if ((long )val.s != val.s) + goto overflow; *va_arg(args, long *) = val.s; -else + }else { +if ((unsigned long )val.u != val.u) + goto overflow; *va_arg(args, unsigned long *) = val.u; + }break ;case 'L' :if (is_sign)
@@ -3288,13 +3306,20 @@ int vsscanf(const char *buf, const char *fmt, va_list args) *va_arg(args, unsigned long long *) = val.u;break ;case 'z' : +if ((size_t )val.u != val.u) + goto overflow; *va_arg(args, size_t *) = val.u;break ;default : -if (is_sign) +if (is_sign) { +if ((int )val.s != val.s) + goto overflow; *va_arg(args, int *) = val.s; -else + }else { +if ((unsigned int )val.u != val.u) + goto overflow; *va_arg(args, unsigned int *) = val.u; + }break ; } num++;
@@ -3304,6 +3329,7 @@ int vsscanf(const char *buf, const char *fmt, va_list args) str = next; } +overflow:return num; }EXPORT_SYMBOL (vsscanf);
Rasmus Villemoes
reviewerRe: [PATCH v1 2/6] lib: scanf: handle integer overflows in vsscanf
Quoted 7 lines · Konstantin Khlebnikov
Traditional scanf implementations ignore integer overflows because C language standard allows here undefined behavior (§7.21.6.2 #10).
So, sane and safe behavior wouldn't harm anything.
This patch carefully checks integer overflows and stops matching if result does not fit into appropriate type before assigning it into argument.
IIRC, this has been attempted before, causing a userspace regression because some sysfs/procfs file matched with %u or %x, and somebody wrote -1 to get 0xffffffff .
I can't remember or find a reference right now, making the above a rather weak argument. However, please start the series with your test cases, before any refactoring. That makes it easier to see what behaviour you're changing (i.e., what used to be allowed is now treated as non-match, etc.).
Rasmus
Re: [PATCH v1 2/6] lib: scanf: handle integer overflows in vsscanf
Quoted 4 lines · Rasmus Villemoes
IIRC, this has been attempted before, causing a userspace regression because some sysfs/procfs file matched with %u or %x, and somebody wrote -1 to get
0xffffffff.
.. which is correct anyway. That's how scanf is supposed to work.
If somebody needs overflow checking, they shouldn't be using scanf.
Linus
Re: [PATCH v1 2/6] lib: scanf: handle integer overflows in vsscanf
Quoted 8 lines · Linus Torvalds
Quoted 4 lines · Rasmus Villemoes
IIRC, this has been attempted before, causing a userspace regression because some sysfs/procfs file matched with %u or %x, and somebody wrote -1 to get
0xffffffff... which is correct anyway. That's how scanf is supposed to work.
Well. In this case '%u' sscanf in kernel is already broken. %u does not accept minus sign and looks like never does.
'is_sign' is set only for %d and %i,
without it string couldn't pass initial check of first "digit".
More over simple_strtoul never accepted '-'.
Glibc version indeed accepts '-1' as %u and returns 0xFFFFFFFF which isn't in manpage:' optionally signed' mentioned only for %d and %i. But this explicitly mentioned in manpage for 'stroul'.
Quoted 5 lines
If somebody needs overflow checking, they shouldn't be using scanf.
Linus
PATCH 3/6 lib: scanf: add vsscanf feature for matching end of text
In reply to Konstantin KhlebnikovTraditional sscanf ignores trailing unmatched characters, because it derives from design of streaming fscanf().
Without extra care this leads to confusing parsing results:
"%d" successfully parses "0x1" as 0 and "1M" as 1.
Silent ignore of trailing input also could hide bugs and unexpectedly break backward compatibility at any change.
This patch adds end of text matching into sscanf format by ASCII End-Of-Transfer character '\004' (EOT aka ^D).
EOT stops matching and if input text haven't ended and adds flag SCANF_MORE to the result value. Result stays positive.
EOT allows to rephrase common end of text checks like:
int end;
if (sscanf(buf, fmt "%n", ..., &end) == NR && !buf[end])
char dummy;
if (sscanf(buf, fmt "%c", ..., &dummy) == NR - 1)
purely in format string without extra code or arguments:
if (sscanf(buf, fmt KERN_EOT, ...) == NR)
This checks that text ends strictly after previous matched character. For skipping trailing white spaces simply add space before EOT.
--- include/linux/kernel.h | 6 ++++++ lib/vsprintf.c | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)
include/linux/kernel.h +6 -0
@@ -476,6 +476,12 @@ char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); extern __printf(2, 0) const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); +#define KERN_EOT "\004" /* ASCII End Of Transfer */ +#define KERN_EOT_ASCII '\004' + +/* sscanf adds this to the result if EOT does not match end of text */ +#define SCANF_MORE (1 << 30) + extern __scanf(2, 3) int sscanf(const char *, const char *, ...); extern __scanf(2, 0)
lib/vsprintf.c +10 -1
@@ -3005,7 +3005,8 @@ EXPORT_SYMBOL_GPL(bprintf);* @fmt: format of buffer * @args: arguments * -* Returns number of successfully matched arguments. +* Returns number of successfully matched arguments, +* plus SCANF_MORE bit flag if EOT does not matched end of text. * * This function implements only basic vsscanf features: *
@@ -3027,6 +3028,8 @@ EXPORT_SYMBOL_GPL(bprintf);* - %s without field width limited with SHRT_MAX * - "%*..." simply skips non white-space characters without conversion * - integer overflows are handled as matching failure +* - KERN_EOT ("\004") matches end of string otherwise stops parsing and +* returns count matched arguments plus SCANF_MORE bit flag, */ int vsscanf (const char *buf,const char *fmt,va_list args) {
@@ -3055,6 +3058,12 @@ int vsscanf(const char *buf, const char *fmt, va_list args)/* anything that is not a conversion must match exactly */ if (*fmt !='%' && *fmt) { +/* EOT in format string: text must end here */ +if (*fmt ==KERN_EOT_ASCII ) { +if (*str) + num |=SCANF_MORE ; +break ; + }if (*fmt++ != *str++)break ;continue ;
PATCH 4/6 lib: scanf: handle character ranges in %[...]
In reply to Konstantin KhlebnikovComplete implementation isn't much bigger than present one.
This patch adds missing standard features:
%[a-z] - matches 'a'..'z'
%[][] - matches '[' and ']' (']' must be first)
%[_-] - matches '_' and '-' ('-' must be last)
--- lib/vsprintf.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-)
lib/vsprintf.c +20 -12
@@ -3019,7 +3019,6 @@ EXPORT_SYMBOL_GPL(bprintf);* - memory-allocation "%m..." * - pointer "%p", ptrdiff_t "%t...", intmax_t "%j..." * - discaring of matching input "%*[...]" -* - ranges or matching ']' in "%[...]" * - positional assignment "%n$" * * Non-standatd features:
@@ -3160,11 +3159,8 @@ int vsscanf(const char *buf, const char *fmt, va_list args)/* * Warning: This implementation of the '[' conversion specifier * deviates from its glibc counterpart in the following ways: -* (1) It does NOT support ranges i.e. '-' is NOT a special -* character -* (2) It cannot match the closing bracket ']' itself -* (3) A field width is required -* (4) '%*[' (discard matching input) is currently not supported +* (1) A field width is required +* (2) '%*[' (discard matching input) is currently not supported * * Example usage: * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
@@ -3176,7 +3172,6 @@ int vsscanf(const char *buf, const char *fmt, va_list args) { char *s = (char *)va_arg(args, char *); DECLARE_BITMAP(set, 256) = {0}; -unsigned int len =0 ;bool negate = (*fmt =='^' );/* field width is required */
@@ -3186,12 +3181,25 @@ int vsscanf(const char *buf, const char *fmt, va_list args)if (negate) ++fmt; -for ( ; *fmt && *fmt !=']' ; ++fmt, ++len) -set_bit ((u8)*fmt, set); +do { +/* no ']' found */ +if (!*fmt) +return num; -/* no ']' or no character set found */ -if (!*fmt || !len) -return num; +/* '-' at last position is non-special */ +if (*fmt =='-' && *(fmt +1 ) !=']' ) { +u8 a = *(fmt -1 ); +u8 b = *(fmt +1 ); + +if (a <= b) +bitmap_set (set, a, b - a +1 ); + }else +set_bit ((u8)*fmt, set); + +/* ']' at first position is non-special */ + }while (*++fmt !=']' ); + +/* skip ']' */ ++fmt;if (negate) {
PATCH 5/6 lib: scanf: mark sscanf and vsscanf as __must_check
In reply to Konstantin KhlebnikovCaller must check returned count of matched arguments.
--- include/linux/kernel.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/kernel.h +2 -2
@@ -483,9 +483,9 @@ const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);#define SCANF_MORE (1 << 30) extern __scanf(2, 3) -int sscanf(const char *, const char *, ...); +int __must_check sscanf(const char *, const char *, ...); extern __scanf(2, 0) -int vsscanf(const char *, const char *, va_list); +int __must_check vsscanf(const char *, const char *, va_list); extern int get_option(char **str, int *pint); extern char *get_options(const char *str, int nints, int *ints);
PATCH 6/6 lib: scanf: add test module
In reply to Konstantin KhlebnikovModule is under config option CONFIG_TEST_SCANF. This is just basic cases, complete coverage isn't that easy for such complicated function.
--- lib/Kconfig.debug | 3 + lib/Makefile | 1 lib/test_scanf.c | 252 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 lib/test_scanf.c
lib/Kconfig.debug +3 -0
@@ -1806,6 +1806,9 @@ config TEST_STRING_HELPERS config TEST_KSTRTOX tristate "Test kstrto*() family of functions at runtime" +config TEST_SCANF + tristate "Test scanf() family of functions at runtime" + config TEST_PRINTF tristate "Test printf() family of functions at runtime"
lib/Makefile +1 -0
@@ -68,6 +68,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_BITFIELD) += test_bitfield.o obj-$(CONFIG_TEST_UUID) += test_uuid.o
lib/test_scanf.c new file +252 -0
252 lines not on this page · raw message