lib/vsprintf: assorted bug fixes
Subsystems (2)
PATCH 3/4 lib/vsprintf: use int for field_width in vsscanf()
In reply to Josh Lawvsscanf() declares field_width as s16 but assigns it from skip_atoi() which returns int. Values above 32767 silently truncate to negative, causing vsscanf() to abort all remaining parsing. This is inconsistent with struct printf_spec which uses int for field_width.
Change the type to int to match.
--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +1 -1
@@ -3461,7 +3461,7 @@ int vsscanf(const char *buf, const char *fmt, va_list args)long long s;unsigned long long u; } val; -s16 field_width; +int field_width;bool is_sign;while (*fmt) {
Signature · 1 line
2.34.1
PATCH 4/4 lib/vsprintf: add missing (u8) cast in format_decode() lookup
In reply to Josh LawThe first lookup into the format_state table correctly casts to (u8) at line 2778, but the second lookup after consuming a length qualifier does not. On signed-char platforms, a byte >= 0x80 sign-extends to a negative index, reading before the array.
Add the same (u8) cast for consistency.
--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +1 -1
@@ -2783,7 +2783,7 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) fmt.str++; } fmt.str++; - p = lookup_state + *fmt.str; + p = lookup_state + (u8)*fmt.str; }if (p->state) {if (p->base)
Signature · 1 line
2.34.1
cover lib/vsprintf: assorted bug fixes
Four small fixes found during an audit of lib/vsprintf.c:
1. bstr_printf() fails to advance the args pointer past a pre-rendered pointer string when the output buffer is full, corrupting all subsequent output.
2. vbin_printf() writes end[-1] unconditionally when NUL-terminating a pointer string, which is an OOB write when size is zero.
3. vsscanf() uses s16 for field_width but assigns from skip_atoi() which returns int, silently truncating large widths to negative and aborting parsing.
4. format_decode() is missing a (u8) cast on the second lookup into the format_state table, allowing a negative array index on signed-char platforms.
Josh Law (4):
lib/vsprintf: always advance args in bstr_printf() pointer path lib/vsprintf: fix OOB write in vbin_printf() when size is zero lib/vsprintf: use int for field_width in vsscanf() lib/vsprintf: add missing (u8) cast in format_decode() lookup
lib/vsprintf.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/4 lib/vsprintf: always advance args in bstr_printf() pointer path
When the output buffer is full (str >= end), bstr_printf() skips advancing the args pointer past the pre-rendered pointer string in bin_buf. This causes all subsequent format specifiers to read from the wrong position, corrupting the rest of the output.
Always compute the string length and advance args regardless of whether there is space to copy into the output buffer.
--- lib/vsprintf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/vsprintf.c +4 -3
@@ -3389,14 +3389,15 @@ int bstr_printf(char *buf, size_t size, const char *fmt_str, const u32 *bin_buf)break ; }/* Pointer dereference was already processed */ + len =strlen (args);if (str < end) { - len = copy =strlen (args); + copy = len;if (copy > end - str) copy = end - str;memcpy (str, args, copy); - str += len; - args += len +1 ; } + str += len; + args += len +1 ; }if (process) str = pointer(fmt.str, str, end, get_arg(void *), spec);
Signature · 1 line
2.34.1
PATCH 2/4 lib/vsprintf: fix OOB write in vbin_printf() when size is zero
In reply to Josh LawWhen vbin_printf() is called with size==0, end equals bin_buf and the else branch writes end[-1], which is one byte before the buffer.
Guard the write so it only happens when the buffer is non-empty.
--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +1 -1
@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args) spec);if (str +1 < end) *str++ ='\0' ; -else +else if (end > (char *)bin_buf) end[-1 ] ='\0' ;/* Must be nul terminated */ }/* skip all alphanumeric pointer suffixes */
Signature · 1 line
2.34.1
Andy Shevchenko
reviewerRe: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
In reply to Josh LawQuoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.
Is the field_width an acceptable integer range by the specifications?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerRe: [PATCH 4/4] lib/vsprintf: add missing (u8) cast in format_decode() lookup
In reply to Josh LawQuoted 6 lines · Josh Law
The first lookup into the
format_statetable correctly casts to (u8) at line 2778, but the second lookup after consuming a length qualifier does not. On signed-char platforms, a byte >=0x80sign-extends to a negative index, reading before the array.Add the same (u8) cast for consistency.
Maybe yes, but get familiar on how the Linux kernel is built. There is no such possibility IRL with this project since a commit in the past. Feel free to find what I meant as your learning curve.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerQuoted 9 lines · Josh Law
Four small fixes found during an audit of
lib/vsprintf.c:1.
bstr_printf()fails to advance the args pointer past a pre-rendered pointer string when the output buffer is full, corrupting all subsequent output.2.
vbin_printf()writes end[-1] unconditionally when NUL-terminating a pointer string, which is an OOB write when size is zero.
Quoted 7 lines
3.
vsscanf()uses s16 forfield_widthbut assigns fromskip_atoi()which returns int, silently truncating large widths to negative and aborting parsing.4.
format_decode()is missing a (u8) cast on the second lookup into theformat_statetable, allowing a negative array index on signed-char platforms.
These all needs a good review. And I think binary printf() might have a bit different rules on how to propagate the pointer in the buffer. To me these might fix something or might break something or do nothing (like in patch 4) due to lack of expertise in the area.
So, I am skeptical about accepting that series, sorry. But I leave it to others to decide, not giving any tag here.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 18 lines · Andy Shevchenko
Quoted 9 lines · Josh Law
Four small fixes found during an audit of
lib/vsprintf.c:1.
bstr_printf()fails to advance the args pointer past a pre-rendered pointer string when the output buffer is full, corrupting all subsequent output.2.
vbin_printf()writes end[-1] unconditionally when NUL-terminating a pointer string, which is an OOB write when size is zero.Quoted 7 lines
3.
vsscanf()uses s16 forfield_widthbut assigns fromskip_atoi()which returns int, silently truncating large widths to negative and aborting parsing.4.
format_decode()is missing a (u8) cast on the second lookup into theformat_statetable, allowing a negative array index on signed-char platforms.
Quoted 8 lines
These all needs a good review. And I think binary
printf()might have a bit different rules on how to propagate the pointer in the buffer. To me these might fix something or might break something or do nothing (like in patch 4) due to lack of expertise in the area.So, I am skeptical about accepting that series, sorry. But I leave it to others to decide, not giving any tag here.
Yep! That's absolutely fine! If you would like any patches dropped, just tell me and I'll do it!
I hope I improved vsprintf with these patches, seriously haha
V/R
Josh Law
Re: [PATCH 1/4] lib/vsprintf: always advance args in bstr_printf() pointer path
In reply to Josh LawQuoted 9 lines · Josh Law
When the output buffer is full (str >= end),
bstr_printf()skips advancing the args pointer past the pre-rendered pointer string inbin_buf. This causes all subsequent format specifiers to read from the wrong position, corrupting the rest of the output.Always compute the string length and advance args regardless of whether there is space to copy into the output buffer.
Signed-off-by Josh Law <[email protected]>
It looks correct to me. It is interesting that nobody found it yet. But I guess that all users of bstr_printf() are using big enough buffers so that it is hard to hit in practice.
Best Regards,
Petr
Re: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero
In reply to Josh LawQuoted 6 lines · Josh Law
When
vbin_printf()is called with size==0, end equalsbin_bufand the else branch writes end[-1], which is one byte before the buffer.Guard the write so it only happens when the buffer is non-empty.
Signed-off-by Josh Law <[email protected]>
Great catch!
There is only one in-tree user and it never passes size=0 so there was no overflow in the reality. But better be on the safe side.
Best Regards,
Petr
Steven Rostedt
maintainerRe: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero
In reply to Josh LawQuoted 15 lines · On Tue, 24 Mar 2026 22:49:38 +0000 Josh Law <[email protected]> wrote:
When
vbin_printf()is called with size==0, end equalsbin_bufand the else branch writes end[-1], which is one byte before the buffer.Guard the write so it only happens when the buffer is non-empty.
Signed-off-by Josh Law <[email protected]>--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0
lib/vsprintf.c int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args) · line 3238
Quoted 6 lines
lib/vsprintf.c +1
-1@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args) spec);if (str +1 < end) *str++ ='\0' ; -else +else if (end > (char *)bin_buf) end[-1 ] ='\0' ;/* Must be nul terminated */
instead of doing a comparison, couldn't we just check size?
else if (size) /* do nothing if size is zero */
I'm guessing this would be slightly more efficient in the assembly.
-- Steve
Quoted 2 lines
lib/vsprintf.c +0
-0}/* skip all alphanumeric pointer suffixes */
Re: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero
---- On Mon, 30 Mar 2026 17:30:19 +0100 [email protected] wrote ----
Quoted 37 lines
lib/vsprintf.c int vbin_printf(u32 *bin_buf, size_t size, const char
Quoted 16 lines · On Tue, 24 Mar 2026 22:49:38 +0000 Josh Law wrote:
When
vbin_printf()is called with size==0, end equalsbin_bufand the else branch writes end[-1], which is one byte before the buffer.Guard the write so it only happens when the buffer is non-empty.
Signed-off-by Josh Law---
lib/vsprintf.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char
*fmt_str,va_listargs)Quoted 6 lines
lib/vsprintf.c +1
-1spec); if (str + 1 < end) *str++ = '\0'; - else + else if (end > (char *)bin_buf)end[-1] = '\0'; /* Must be nul terminated */
instead of doing a comparison, couldn't we just check size?
else if (size) /* do nothing if size is zero */
I'm guessing this would be slightly more efficient in the assembly.
-- Steve
Quoted 2 lines
}
/* skip all alphanumeric pointer suffixes */
Re: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero
In reply to Steven Rostedt---- On Mon, 30 Mar 2026 17:30:19 +0100 [email protected] wrote ----
Quoted 37 lines
lib/vsprintf.c int vbin_printf(u32 *bin_buf, size_t size, const char
Quoted 16 lines · On Tue, 24 Mar 2026 22:49:38 +0000 Josh Law wrote:
When
vbin_printf()is called with size==0, end equalsbin_bufand the else branch writes end[-1], which is one byte before the buffer.Guard the write so it only happens when the buffer is non-empty.
Signed-off-by Josh Law---
lib/vsprintf.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char
*fmt_str,va_listargs)Quoted 6 lines
lib/vsprintf.c +1
-1spec); if (str + 1 < end) *str++ = '\0'; - else + else if (end > (char *)bin_buf)end[-1] = '\0'; /* Must be nul terminated */
instead of doing a comparison, couldn't we just check size?
else if (size) /* do nothing if size is zero */
I'm guessing this would be slightly more efficient in the assembly.
-- Steve
Quoted 2 lines
}
/* skip all alphanumeric pointer suffixes */
Re: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero
In reply to Steven Rostedt---- On Mon, 30 Mar 2026 17:30:19 +0100 [email protected] wrote ----
Quoted 37 lines
lib/vsprintf.c int vbin_printf(u32 *bin_buf, size_t size, const char
Quoted 16 lines · On Tue, 24 Mar 2026 22:49:38 +0000 Josh Law wrote:
When
vbin_printf()is called with size==0, end equalsbin_bufand the else branch writes end[-1], which is one byte before the buffer.Guard the write so it only happens when the buffer is non-empty.
Signed-off-by Josh Law---
lib/vsprintf.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char
*fmt_str,va_listargs)Quoted 6 lines
lib/vsprintf.c +1
-1spec); if (str + 1 < end) *str++ = '\0'; - else + else if (end > (char *)bin_buf)end[-1] = '\0'; /* Must be nul terminated */
instead of doing a comparison, couldn't we just check size?
else if (size) /* do nothing if size is zero */
I'm guessing this would be slightly more efficient in the assembly.
-- Steve
Quoted 2 lines
}
/* skip all alphanumeric pointer suffixes */
Re: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero
In reply to Steven Rostedt---- On Mon, 30 Mar 2026 17:30:19 +0100 [email protected] wrote ----
Quoted 27 lines
lib/vsprintf.c int vbin_printf(u32 *bin_buf, size_t size, const char
Quoted 16 lines · On Tue, 24 Mar 2026 22:49:38 +0000 Josh Law wrote:
When
vbin_printf()is called with size==0, end equalsbin_bufand the else branch writes end[-1], which is one byte before the buffer.Guard the write so it only happens when the buffer is non-empty.
Signed-off-by Josh Law---
lib/vsprintf.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char
*fmt_str,va_listargs)Quoted 6 lines
lib/vsprintf.c +1
-1spec); if (str + 1 < end) *str++ = '\0'; - else + else if (end > (char *)bin_buf)end[-1] = '\0'; /* Must be nul terminated */
Quoted 3 lines
instead of doing a comparison, couldn't we just check size?
else if (size) /* do nothing if size is zero */
Good call. I'll make a V2 soon on that patch.
Quoted 7 lines
I'm guessing this would be slightly more efficient in the assembly.
-- Steve
Quoted 2 lines
}
/* skip all alphanumeric pointer suffixes */
Petr Mladek
maintainerRe: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
In reply to Andy ShevchenkoQuoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?
I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's':
{
char *s = (char *)va_arg(args, char *);
if (field_width == -1)
field_width = SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
case '[':
{
[...]
/* field width is required */
if (field_width == -1)
return num;The current code limits valid field width values to positive ones, aka SHRT_MAX which is clearly much lover than INT_MAX. And it might prevent some out of bound access.
Best Regards,
Petr
Re: [PATCH 4/4] lib/vsprintf: add missing (u8) cast in format_decode() lookup
In reply to Josh LawQuoted 17 lines · Josh Law
The first lookup into the
format_statetable correctly casts to (u8) at line 2778, but the second lookup after consuming a length qualifier does not. On signed-char platforms, a byte >=0x80sign-extends to a negative index, reading before the array.Add the same (u8) cast for consistency.
Signed-off-by Josh Law <[email protected]>--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0
lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2789
Quoted 8 lines
lib/vsprintf.c +1
-1@@ -2783,7 +2783,7 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) fmt.str++; } fmt.str++; - p = lookup_state + *fmt.str; + p = lookup_state + (u8)*fmt.str; }if (p->state) {if (p->base)
This makes sense. Even though the current code is safe as pointed out by Andy.
Best Regards,
Petr
Re: [PATCH 4/4] lib/vsprintf: add missing (u8) cast in format_decode() lookup
---- On Tue, 31 Mar 2026 15:33:53 +0100 [email protected] wrote ----
Quoted 29 lines · Josh Law
lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct
Quoted 18 lines
The first lookup into the
format_statetable correctly casts to (u8) at line 2778, but the second lookup after consuming a length qualifier does not. On signed-char platforms, a byte >=0x80sign-extends to a negative index, reading before the array.Add the same (u8) cast for consistency.
Signed-off-by Josh Law---
lib/vsprintf.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0@@ -2783,7 +2783,7 @@ struct fmt format_decode(struct fmt fmt, struct
printf_spec*spec)lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2789
Quoted 8 lines
lib/vsprintf.c +1
-1fmt.str++; } fmt.str++; - p = lookup_state + *fmt.str; + p = lookup_state + (u8)*fmt.str; } if (p->state) { if (p->base)
Quoted 7 lines
This makes sense. Even though the current code is safe as pointed out by Andy.
Reviewed-by Petr MladekBest Regards,
Petr
Yeah, better safe than sorry in my opinion.
Thanks for the review petr!
Re: [PATCH 4/4] lib/vsprintf: add missing (u8) cast in format_decode() lookup
In reply to Petr Mladek---- On Tue, 31 Mar 2026 15:33:53 +0100 [email protected] wrote ----
Quoted 29 lines · Josh Law
lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct
Quoted 18 lines
The first lookup into the
format_statetable correctly casts to (u8) at line 2778, but the second lookup after consuming a length qualifier does not. On signed-char platforms, a byte >=0x80sign-extends to a negative index, reading before the array.Add the same (u8) cast for consistency.
Signed-off-by Josh Law---
lib/vsprintf.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0@@ -2783,7 +2783,7 @@ struct fmt format_decode(struct fmt fmt, struct
printf_spec*spec)lib/vsprintf.c struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) · line 2789
Quoted 8 lines
lib/vsprintf.c +1
-1fmt.str++; } fmt.str++; - p = lookup_state + *fmt.str; + p = lookup_state + (u8)*fmt.str; } if (p->state) { if (p->base)
Quoted 7 lines
This makes sense. Even though the current code is safe as pointed out by Andy.
Reviewed-by Petr MladekBest Regards,
Petr
Yeah, better safe than sorry in my opinion.
Thanks for the review petr!
Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
In reply to Petr MladekQuoted 31 lines · On Tue, 31 Mar 2026 16:31:50 +0200 Petr Mladek <[email protected]> wrote:
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's': { char *s = (char *)va_arg(args, char *); if (field_width == -1) field_width = SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
case '[': { [...] /* field width is required */ if (field_width == -1) return num;
Quoted 8 lines
The current code limits valid field width values to positive ones, aka
SHRT_MAXwhich is clearly much lover thanINT_MAX. And it might prevent some out of bound access.Best Regards,
Petr
Notwithstanding what the code actually does there is no point defining a local variable as a 'short' unless you really want arithmetic to wrap at 16 bits.
All it does is force the compiler to keep adding code to fix the sign extension to 32 bits.
Look at the object for anything other than x86 (or m68k).
David
Petr Mladek
maintainerRe: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
Quoted 27 lines · David Laight
Quoted 24 lines · On Tue, 31 Mar 2026 16:31:50 +0200 Petr Mladek <[email protected]> wrote:
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's': { char *s = (char *)va_arg(args, char *); if (field_width == -1) field_width = SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
Quoted 8 lines
Quoted 8 lines
case '[': { [...] /* field width is required */ if (field_width == -1) return num;The current code limits valid field width values to positive ones,
I meant this code:
/* get field width */
field_width = -1;
if (isdigit(*fmt)) {
field_width = skip_atoi(&fmt);
if (field_width <= 0)
break;
}If we change the type of the local variable then the above check will suddenly accept fied_width <= INT_MAX instead of SHRT_MAX.
As a result, The above mentioned "case '[':" handling will suddely allow to iternate over INT_MAX long string instead of SHRT_MAX.
I doubt that there is any kernel code which would be affected by this. But I do not want to risk it.
Quoted 8 lines
Quoted 7 lines
aka
SHRT_MAXwhich is clearly much lover thanINT_MAX. And it might prevent some out of bound access.Best Regards,
Petr
Quoted 6 lines
Notwithstanding what the code actually does there is no point defining a local variable as a 'short' unless you really want arithmetic to wrap at 16 bits.
All it does is force the compiler to keep adding code to fix the sign extension to 32 bits.
Look at the object for anything other than x86 (or m68k).
If you think that it is important enough, feel free to send a patch.
I not taking this patch from Josh Law, definitely!
Best Regards,
Petr
PS: Note that Josh Law seems to be an AI virtual person, see
https://lore.kernel.org/all/[email protected]/
I am even not sure what to do with the other 3 patches. They look correct. But I should not take patches with an unclear origin, see
https://lore.kernel.org/all/[email protected]/
Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
---- On Tue, 31 Mar 2026 17:12:18 +0100 [email protected] wrote ----
Quoted 79 lines · David Laight
Quoted 27 lines
Quoted 24 lines · On Tue, 31 Mar 2026 16:31:50 +0200 Petr Mladek wrote:
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's':
{
char *s = (char *)va_arg(args, char *); if (field_width== -1)field_width=SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
Quoted 8 lines
Quoted 8 lines
case '[':
{
[...]
/* field width is required */
if (field_width== -1)
return num;The current code limits valid field width values to positive ones,
I meant this code:
/* get field width */
field_width = -1;
if (isdigit(*fmt)) {
field_width =skip_atoi(&fmt);
if (field_width<= 0)
break;
}If we change the type of the local variable then the above check will suddenly accept
fied_width<=INT_MAXinstead ofSHRT_MAX.As a result, The above mentioned "case '[':" handling will suddely allow to iternate over
INT_MAXlong string instead ofSHRT_MAX.I doubt that there is any kernel code which would be affected by this. But I do not want to risk it.
Quoted 8 lines
Quoted 7 lines
aka
SHRT_MAXwhich is clearly much lover thanINT_MAX. And it might prevent some out of bound access.Best Regards,
PetrQuoted 6 lines
Notwithstanding what the code actually does there is no point defining a local variable as a 'short' unless you really want arithmetic to wrap at 16 bits.
All it does is force the compiler to keep adding code to fix the sign extension to 32 bits.
Look at the object for anything other than x86 (or m68k).If you think that it is important enough, feel free to send a patch.
I not taking this patch from Josh Law, definitely!
Best Regards,
Petr
Quoted 6 lines
PS: Note that Josh Law seems to be an AI virtual person, see
https://lore.kernel.org/all/[email protected]/[https://lore.kernel.org/all/[email protected]/]I am even not sure what to do with the other 3 patches. They look correct. But I should not take patches with an unclear origin, see
https://lore.kernel.org/all/[email protected]/[https://lore.kernel.org/all/[email protected]/]
I'm gonna arrange a video call with someone to fix this
Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
In reply to Petr Mladek---- On Tue, 31 Mar 2026 17:12:18 +0100 [email protected] wrote ----
Quoted 79 lines · David Laight
Quoted 27 lines
Quoted 24 lines · On Tue, 31 Mar 2026 16:31:50 +0200 Petr Mladek wrote:
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's':
{
char *s = (char *)va_arg(args, char *); if (field_width== -1)field_width=SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
Quoted 8 lines
Quoted 8 lines
case '[':
{
[...]
/* field width is required */
if (field_width== -1)
return num;The current code limits valid field width values to positive ones,
I meant this code:
/* get field width */
field_width = -1;
if (isdigit(*fmt)) {
field_width =skip_atoi(&fmt);
if (field_width<= 0)
break;
}If we change the type of the local variable then the above check will suddenly accept
fied_width<=INT_MAXinstead ofSHRT_MAX.As a result, The above mentioned "case '[':" handling will suddely allow to iternate over
INT_MAXlong string instead ofSHRT_MAX.I doubt that there is any kernel code which would be affected by this. But I do not want to risk it.
Quoted 8 lines
Quoted 7 lines
aka
SHRT_MAXwhich is clearly much lover thanINT_MAX. And it might prevent some out of bound access.Best Regards,
PetrQuoted 6 lines
Notwithstanding what the code actually does there is no point defining a local variable as a 'short' unless you really want arithmetic to wrap at 16 bits.
All it does is force the compiler to keep adding code to fix the sign extension to 32 bits.
Look at the object for anything other than x86 (or m68k).If you think that it is important enough, feel free to send a patch.
I not taking this patch from Josh Law, definitely!
Best Regards,
Petr
Quoted 6 lines
PS: Note that Josh Law seems to be an AI virtual person, see
https://lore.kernel.org/all/[email protected]/[https://lore.kernel.org/all/[email protected]/]I am even not sure what to do with the other 3 patches. They look correct. But I should not take patches with an unclear origin, see
https://lore.kernel.org/all/[email protected]/[https://lore.kernel.org/all/[email protected]/]
I'm gonna arrange a video call with someone to fix this
Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
In reply to Petr Mladek---- On Tue, 31 Mar 2026 17:12:18 +0100 [email protected] wrote ----
Quoted 79 lines · David Laight
Quoted 27 lines
Quoted 24 lines · On Tue, 31 Mar 2026 16:31:50 +0200 Petr Mladek wrote:
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's':
{
char *s = (char *)va_arg(args, char *); if (field_width== -1)field_width=SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
Quoted 8 lines
Quoted 8 lines
case '[':
{
[...]
/* field width is required */
if (field_width== -1)
return num;The current code limits valid field width values to positive ones,
I meant this code:
/* get field width */
field_width = -1;
if (isdigit(*fmt)) {
field_width =skip_atoi(&fmt);
if (field_width<= 0)
break;
}If we change the type of the local variable then the above check will suddenly accept
fied_width<=INT_MAXinstead ofSHRT_MAX.As a result, The above mentioned "case '[':" handling will suddely allow to iternate over
INT_MAXlong string instead ofSHRT_MAX.I doubt that there is any kernel code which would be affected by this. But I do not want to risk it.
Quoted 8 lines
Quoted 7 lines
aka
SHRT_MAXwhich is clearly much lover thanINT_MAX. And it might prevent some out of bound access.Best Regards,
PetrQuoted 6 lines
Notwithstanding what the code actually does there is no point defining a local variable as a 'short' unless you really want arithmetic to wrap at 16 bits.
All it does is force the compiler to keep adding code to fix the sign extension to 32 bits.
Look at the object for anything other than x86 (or m68k).If you think that it is important enough, feel free to send a patch.
I not taking this patch from Josh Law, definitely!
Best Regards,
Petr
Quoted 6 lines
PS: Note that Josh Law seems to be an AI virtual person, see
https://lore.kernel.org/all/[email protected]/[https://lore.kernel.org/all/[email protected]/]I am even not sure what to do with the other 3 patches. They look correct. But I should not take patches with an unclear origin, see
https://lore.kernel.org/all/[email protected]/[https://lore.kernel.org/all/[email protected]/]
I'm gonna arrange a video call with someone to fix this
Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
In reply to Petr MladekQuoted 47 lines · On Tue, 31 Mar 2026 18:12:18 +0200 Petr Mladek <[email protected]> wrote:
Quoted 27 lines · David Laight
Quoted 24 lines · On Tue, 31 Mar 2026 16:31:50 +0200 Petr Mladek <[email protected]> wrote:
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's': { char *s = (char *)va_arg(args, char *); if (field_width == -1) field_width = SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
Quoted 8 lines
Quoted 8 lines
case '[': { [...] /* field width is required */ if (field_width == -1) return num;The current code limits valid field width values to positive ones,
I meant this code:
/* get field width */ field_width = -1; if (isdigit(*fmt)) { field_width = skip_atoi(&fmt); if (field_width <= 0) break; }
Quoted 2 lines
If we change the type of the local variable then the above check will suddenly accept
field_width<=INT_MAXinstead ofSHRT_MAX.
But it is all broken anyway - consider "%65537[abc]". The test needs to be:
if (field_width <= 0 || field_width > MAX_XXX)
(and I doubt 32767 is a same limit anyway).
(oh and skip_atoi() need to either saturate or stop consuming digits)
David
Quoted 37 lines
As a result, The above mentioned "case '[':" handling will suddely allow to iternate over
INT_MAXlong string instead ofSHRT_MAX.I doubt that there is any kernel code which would be affected by this. But I do not want to risk it.
Quoted 8 lines
Quoted 7 lines
aka
SHRT_MAXwhich is clearly much lover thanINT_MAX. And it might prevent some out of bound access.Best Regards,
PetrQuoted 6 lines
Notwithstanding what the code actually does there is no point defining a local variable as a 'short' unless you really want arithmetic to wrap at 16 bits.
All it does is force the compiler to keep adding code to fix the sign extension to 32 bits.
Look at the object for anything other than x86 (or m68k).If you think that it is important enough, feel free to send a patch.
I not taking this patch from Josh Law, definitely!
Best Regards,
PetrPS: Note that Josh Law seems to be an AI virtual person, see
https://lore.kernel.org/all/[email protected]/I am even not sure what to do with the other 3 patches. They look correct. But I should not take patches with an unclear origin, see
https://lore.kernel.org/all/[email protected]/
Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()
Quoted 53 lines · On Wed, 1 Apr 2026 15:22:56 +0100 David Laight <[email protected]>…
Quoted 47 lines · On Tue, 31 Mar 2026 18:12:18 +0200 Petr Mladek <[email protected]> wrote:
Quoted 27 lines · David Laight
Quoted 24 lines · On Tue, 31 Mar 2026 16:31:50 +0200 Petr Mladek <[email protected]> wrote:
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law
vsscanf()declaresfield_widthas s16 but assigns it fromskip_atoi()which returns int. Values above 32767 silently truncate to negative, causingvsscanf()to abort all remaining parsing. This is inconsistent with structprintf_specwhich uses int forfield_width.Is the
field_widthan acceptable integer range by the specifications?I am not sure what is allowed by specification. Anyway, the code is not ready for a bigger values, for example:
case 's': { char *s = (char *)va_arg(args, char *); if (field_width == -1) field_width = SHRT_MAX;clearly expects signed short int range.
I wonder if it might even open some backdoor. The code matching as sequence of characters expects a defined field width, see
Quoted 8 lines
Quoted 8 lines
case '[': { [...] /* field width is required */ if (field_width == -1) return num;The current code limits valid field width values to positive ones,
I meant this code:
/* get field width */ field_width = -1; if (isdigit(*fmt)) { field_width = skip_atoi(&fmt); if (field_width <= 0) break; }Quoted 2 lines
If we change the type of the local variable then the above check will suddenly accept
field_width<=INT_MAXinstead ofSHRT_MAX.
Quoted 6 lines
But it is all broken anyway - consider "%65537[abc]". The test needs to be:
if (field_width <= 0 || field_width > MAX_XXX)(and I doubt 32767 is a same limit anyway).
(oh and
skip_atoi()need to either saturate or stop consuming digits)
Actually I realised later this is all pointless.
All the formats are literal strings in the kernel - they are known to be ok. I think even the (field_width <= 0) test can just be deleted. The code won't loop forever, it might overrun the output buffer but than can happen with a syntactically valid length as well.
David
Quoted 43 lines
David
Quoted 37 lines
As a result, The above mentioned "case '[':" handling will suddely allow to iternate over
INT_MAXlong string instead ofSHRT_MAX.I doubt that there is any kernel code which would be affected by this. But I do not want to risk it.
Quoted 8 lines
Quoted 7 lines
aka
SHRT_MAXwhich is clearly much lover thanINT_MAX. And it might prevent some out of bound access.Best Regards,
PetrQuoted 6 lines
Notwithstanding what the code actually does there is no point defining a local variable as a 'short' unless you really want arithmetic to wrap at 16 bits.
All it does is force the compiler to keep adding code to fix the sign extension to 32 bits.
Look at the object for anything other than x86 (or m68k).If you think that it is important enough, feel free to send a patch.
I not taking this patch from Josh Law, definitely!
Best Regards,
PetrPS: Note that Josh Law seems to be an AI virtual person, see
https://lore.kernel.org/all/[email protected]/I am even not sure what to do with the other 3 patches. They look correct. But I should not take patches with an unclear origin, see
https://lore.kernel.org/all/[email protected]/