Skip to contentSkip to conversation

lib/vsprintf: assorted bug fixes

PATCH Superseded Josh Law 5 participants 27 messages Mar 24 – Apr 1

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author
#10 lore raw

PATCH 3/4 lib/vsprintf: use int for field_width in vsscanf()

In reply to Josh Law

vsscanf() 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
index 72fbfe181076..2758096b6f53 100644
@@ -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
author
#20 lore raw

PATCH 4/4 lib/vsprintf: add missing (u8) cast in format_decode() lookup

In reply to Josh Law

The 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
index 2758096b6f53..3108823e8c22 100644
@@ -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

Josh Law

author
#0 lore raw

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
author
#1 lore raw

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
index 800b8ac49f53..7898fb998b21 100644
@@ -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
author
#3 lore raw

PATCH 2/4 lib/vsprintf: fix OOB write in vbin_printf() when size is zero

In reply to Josh Law

When 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
index 7898fb998b21..72fbfe181076 100644
@@ -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
#11 lore raw

Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()

In reply to Josh Law
Quoted 4 lines · Josh Law

vsscanf() 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.

Is the field_width an acceptable integer range by the specifications?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#21 lore raw

Re: [PATCH 4/4] lib/vsprintf: add missing (u8) cast in format_decode() lookup

In reply to Josh Law
Quoted 6 lines · Josh Law

The 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.

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
#25 lore raw
In reply to Josh Law
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 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.

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
author
#26 lore raw
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 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.

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

maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] lib/vsprintf: always advance args in bstr_printf() pointer path

In reply to Josh Law
Quoted 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 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.

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

maintainer Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero

In reply to Josh Law
Quoted 6 lines · Josh Law

When 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.

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

maintainer
#5 lore raw

Re: [PATCH 2/4] lib/vsprintf: fix OOB write in vbin_printf() when size is zero

In reply to Josh Law
Quoted 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 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 +0 -0
index 7898fb998b21..72fbfe181076 100644

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 */
author
#6 lore raw

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 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.

Signed-off-by Josh Law

---
lib/vsprintf.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +0 -0
index 7898fb998b21..72fbfe181076 100644
@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char

*fmt_str, va_list args)

Quoted 6 lines
lib/vsprintf.c +1 -1
                      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

            }
            /* skip all alphanumeric pointer suffixes */

author
#7 lore raw

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 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.

Signed-off-by Josh Law

---
lib/vsprintf.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +0 -0
index 7898fb998b21..72fbfe181076 100644
@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char

*fmt_str, va_list args)

Quoted 6 lines
lib/vsprintf.c +1 -1
                      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

            }
            /* skip all alphanumeric pointer suffixes */

author
#8 lore raw

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 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.

Signed-off-by Josh Law

---
lib/vsprintf.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +0 -0
index 7898fb998b21..72fbfe181076 100644
@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char

*fmt_str, va_list args)

Quoted 6 lines
lib/vsprintf.c +1 -1
                      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

            }
            /* skip all alphanumeric pointer suffixes */

author
#9 lore raw

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 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.

Signed-off-by Josh Law

---
lib/vsprintf.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +0 -0
index 7898fb998b21..72fbfe181076 100644
@@ -3234,7 +3234,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char

*fmt_str, va_list args)

Quoted 6 lines
lib/vsprintf.c +1 -1
                      spec);
                 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 */

maintainer
#12 lore raw

Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()

In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Josh Law

vsscanf() 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.

Is the field_width an 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

maintainer Reviewed-by on v1 4/4
#22 lore raw

Re: [PATCH 4/4] lib/vsprintf: add missing (u8) cast in format_decode() lookup

In reply to Josh Law
Quoted 17 lines · Josh Law

The 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 +0 -0
index 2758096b6f53..3108823e8c22 100644

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

author
#23 lore raw

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_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.

Signed-off-by Josh Law

---
lib/vsprintf.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +0 -0
index 2758096b6f53..3108823e8c22 100644
@@ -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 -1
             fmt.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 Mladek

Best Regards,
Petr

Yeah, better safe than sorry in my opinion.

Thanks for the review petr!

author
#24 lore raw

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_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.

Signed-off-by Josh Law

---
lib/vsprintf.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +0 -0
index 2758096b6f53..3108823e8c22 100644
@@ -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 -1
             fmt.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 Mladek

Best Regards,
Petr

Yeah, better safe than sorry in my opinion.

Thanks for the review petr!

#13 lore raw

Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()

In reply to Petr Mladek
Quoted 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() 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.

Is the field_width an 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_MAX which is clearly much lover than INT_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

maintainer
#14 lore raw

Re: [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() 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.

Is the field_width an 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_MAX which is clearly much lover than INT_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]/

author
#15 lore raw

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() 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.

Is the field_width an 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_MAX which is clearly much lover than INT_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

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

author
#16 lore raw

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() 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.

Is the field_width an 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_MAX which is clearly much lover than INT_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

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

author
#17 lore raw

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() 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.

Is the field_width an 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_MAX which is clearly much lover than INT_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

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

#18 lore raw

Re: [PATCH 3/4] lib/vsprintf: use int for field_width in vsscanf()

In reply to Petr Mladek
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() 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.

Is the field_width an 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_MAX instead of SHRT_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_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_MAX which is clearly much lover than INT_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]/

#19 lore raw

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() 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.

Is the field_width an 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_MAX instead of SHRT_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_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_MAX which is clearly much lover than INT_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]/