lib/vsprintf: Fixes size check
Subsystems (2)
v2 cover lib/vsprintf: Fixes size check
Hi,
Here is a pair of patches to fix vsnprintf().
- Fix to limit the size of width and precision.
- Warn if the return size is over INT_MAX.
Recently we discussed snprintf() usage in bootconfig and found snprintf() design has a problem[1]. It returns the required or printed size in 'int' but the maxlen is passed by 'size_t'. The maxlen is already limited by INT_MAX, but if the expected print size becomes bigger than INT_MAX, it can return negative value. We also found width and precision size check does not work.
[1] https://lore.kernel.org/all/[email protected]/
Thank you,
---
Masami Hiramatsu (Google) (2):
lib/vsprintf: Fix to check field_width and precision lib/vsprintf: Limit the returning size to INT_MAX
lib/vsprintf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
--
Masami Hiramatsu (Google) <[email protected]>
PATCH v2 1/2 lib/vsprintf: Fix to check field_width and precision
From: Masami Hiramatsu (Google) <[email protected]>
Check the field_width and presition correctly. Previously it depends on the bitfield conversion from int to check out-of-range error. However, commit 938df695e98d ("vsprintf: associate the format state with the format pointer") changed those fields to int.
We need to check the out-of-range correctly without bitfield conversion.
---
Changes in v2:
- Fix to use logical split.
--- lib/vsprintf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
lib/vsprintf.c +4 -2
@@ -2803,7 +2803,8 @@ static void set_field_width(struct printf_spec *spec, int width) { spec->field_width = width; -if (WARN_ONCE (spec->field_width != width,"field width %d too large" , width)) { +if (WARN_ONCE (spec->field_width >FIELD_WIDTH_MAX || spec->field_width < -FIELD_WIDTH_MAX , +"field width %d too large" , width)) { spec->field_width =clamp (width, -FIELD_WIDTH_MAX ,FIELD_WIDTH_MAX ); } }
@@ -2812,7 +2813,8 @@ static void set_precision(struct printf_spec *spec, int prec) { spec->precision = prec; -if (WARN_ONCE (spec->precision != prec,"precision %d too large" , prec)) { +if (WARN_ONCE (spec->precision >PRECISION_MAX || spec->precision <0 , +"precision %d too large" , prec)) { spec->precision =clamp (prec,0 ,PRECISION_MAX ); } }
PATCH v2 2/2 lib/vsprintf: Limit the returning size to INT_MAX
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
The return value of vsnprintf() can overflow INT_MAX and return a minus value. In the @size is checked input overflow, but it does not check the output, which is expected required size.
This should never happen but it should be checked and limited.
--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +1 -1
@@ -2985,7 +2985,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) }/* the trailing null byte doesn't count towards the total */ -return str-buf; +return WARN_ON_ONCE (str - buf >INT_MAX ) ?INT_MAX : str - buf; }EXPORT_SYMBOL (vsnprintf);
Re: [PATCH v2 1/2] lib/vsprintf: Fix to check field_width and precision
In reply to Masami Hiramatsu (Google)Quoted 25 lines · On Fri, 20 Mar 2026 12:54:48 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Check the
field_widthand presition correctly. Previously it depends on the bitfield conversion from int to check out-of-range error. However, commit938df695e98d("vsprintf: associate the format state with the format pointer") changed those fields to int.
We need to check the out-of-range correctly without bitfield conversion.Fixes 938df695e98d ("vsprintf: associate the format state with the format pointer")Reported-by David Laight <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>---
Changes in v2:
- Fix to use logical split.--- lib/vsprintf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)lib/vsprintf.c +0
-0
lib/vsprintf.c static void · line 2806
Quoted 5 lines
lib/vsprintf.c +1
-1@@ -2803,7 +2803,8 @@ static void set_field_width(struct printf_spec *spec, int width) { spec->field_width = width; -if (WARN_ONCE (spec->field_width != width,"field width %d too large" , width)) { + if (WARN_ONCE(spec->field_width > FIELD_WIDTH_MAX || spec->field_width < -FIELD_WIDTH_MAX,
Check and update width before the assignment to spec->field_width.
David
Quoted 15 lines
lib/vsprintf.c +3
-1+ "field width %d too large", width)) { spec->field_width =clamp (width, -FIELD_WIDTH_MAX ,FIELD_WIDTH_MAX ); } }@@ -2812,7 +2813,8 @@ static void set_precision(struct printf_spec *spec, int prec) { spec->precision = prec; -if (WARN_ONCE (spec->precision != prec,"precision %d too large" , prec)) { +if (WARN_ONCE (spec->precision >PRECISION_MAX || spec->precision <0 , +"precision %d too large" , prec)) { spec->precision =clamp (prec,0 ,PRECISION_MAX ); } }
Petr Mladek
maintainerRe: [PATCH v2 2/2] lib/vsprintf: Limit the returning size to INT_MAX
In reply to Masami Hiramatsu (Google)Quoted 7 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
The return value of
vsnprintf()can overflowINT_MAXand return a minus value. In the @size is checked input overflow, but it does not check the output, which is expected required size.This should never happen but it should be checked and limited.
Great catch!
lib/vsprintf.c int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) · line 2988
Quoted 8 lines
lib/vsprintf.c +1
-1@@ -2985,7 +2985,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) }/* the trailing null byte doesn't count towards the total */ -return str-buf; +return WARN_ON_ONCE (str - buf >INT_MAX ) ?INT_MAX : str - buf;
Is it guaranteed that the pointer arithmetic will be a big enough unsigned number type?
I would rather do a cast to be on the safe side, for example:
return WARN_ON_ONCE((size_t)(str - buf) > INT_MAX) ? INT_MAX : str - buf;
or even use a variable to make it better readable:
size_t ret_size; ret_size = str - buf; if (WARN_ON_ONCE(ret_size > INT_MAX)) ret_size = INT_MAX; return ret_size;
Best Regards,
Petr
Re: [PATCH v2 1/2] lib/vsprintf: Fix to check field_width and precision
In reply to David LaightQuoted 36 lines · On Fri, 20 Mar 2026 09:48:58 +0000 David Laight <[email protected]>…
Quoted 25 lines · On Fri, 20 Mar 2026 12:54:48 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Check the
field_widthand presition correctly. Previously it depends on the bitfield conversion from int to check out-of-range error. However, commit938df695e98d("vsprintf: associate the format state with the format pointer") changed those fields to int.
We need to check the out-of-range correctly without bitfield conversion.Fixes 938df695e98d ("vsprintf: associate the format state with the format pointer")Reported-by David Laight <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>---
Changes in v2:
- Fix to use logical split.--- lib/vsprintf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)lib/vsprintf.c +0
-0lib/vsprintf.c static void · line 2806
Quoted 5 lines
lib/vsprintf.c +1
-1@@ -2803,7 +2803,8 @@ static void set_field_width(struct printf_spec *spec, int width) { spec->field_width = width; -if (WARN_ONCE (spec->field_width != width,"field width %d too large" , width)) { + if (WARN_ONCE(spec->field_width > FIELD_WIDTH_MAX || spec->field_width < -FIELD_WIDTH_MAX,Check and update width before the assignment to
spec->field_width.
Quoted 1 line
David
Indeed. I'll do the same for precision.
Thanks,
Quoted 17 lines
Quoted 16 lines
lib/vsprintf.c +3
-1+ "field width %d too large", width)) { spec->field_width =clamp (width, -FIELD_WIDTH_MAX ,FIELD_WIDTH_MAX ); } }@@ -2812,7 +2813,8 @@ static void set_precision(struct printf_spec *spec, int prec) { spec->precision = prec; -if (WARN_ONCE (spec->precision != prec,"precision %d too large" , prec)) { +if (WARN_ONCE (spec->precision >PRECISION_MAX || spec->precision <0 , +"precision %d too large" , prec)) { spec->precision =clamp (prec,0 ,PRECISION_MAX ); } }
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH v2 2/2] lib/vsprintf: Limit the returning size to INT_MAX
In reply to Petr MladekQuoted 23 lines · On Fri, 20 Mar 2026 17:51:48 +0100 Petr Mladek <[email protected]> wrote:
Quoted 7 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
The return value of
vsnprintf()can overflowINT_MAXand return a minus value. In the @size is checked input overflow, but it does not check the output, which is expected required size.This should never happen but it should be checked and limited.
Great catch!
lib/vsprintf.c int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) · line 2988
Quoted 8 lines
lib/vsprintf.c +1
-1@@ -2985,7 +2985,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) }/* the trailing null byte doesn't count towards the total */ -return str-buf; +return WARN_ON_ONCE (str - buf >INT_MAX ) ?INT_MAX : str - buf;Is it guaranteed that the pointer arithmetic will be a big enough unsigned number type?
Quoted 3 lines
I would rather do a cast to be on the safe side, for example:
return WARN_ON_ONCE((size_t)(str - buf) > INT_MAX) ? INT_MAX : str - buf;
OK.
Quoted 9 lines
or even use a variable to make it better readable:
size_t ret_size; ret_size = str - buf; if (WARN_ON_ONCE(ret_size > INT_MAX)) ret_size = INT_MAX; return ret_size;
Ah, Indeed. Let me do this.
Thanks!
Quoted 3 lines
Best Regards,
Petr
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>