Buffer overruns in printf
Subsystems (2)
v5 cover Buffer overruns in printf
The printf family of functions in U-Boot cannot deal with a situation where the caller provides a buffer which turns out to be too small for the format string. This can result in buffer overflows, stack overflows and other bad behavior.
This patch series tidies this up in the common vsprintf.c code.
You can find a discussion of the Linux / U-Boot licensing issues here:
http://patchwork.ozlabs.org/patch/116161/
Code Size Impact
----------------
(From Simon Glass <[email protected]>)
With my ARMv7 compiler (gcc-4.4.3_cos_gg_53174) the code size increase is 312 bytes, about 10% increase to code size vsprintf.o.
With the CONFIG_SYS_NO_VSNPRINT option defined, the code size impact is 4 bytes.
Changes in v2:
- Use sizeof(printbuffer) instead of CONFIG_SYS_PBSIZE
- Drop patch which changes network code to use snprintf()
Changes in v3:
- Move prototypes from common.h to vsprintf.h
- Add CONFIG_SYS_VSNPRINT option to enable vsnprintf() functions
- Update README with CONFIG_SYS_VSNPRINT docs
- Use ADDCH macro to support checking/not checking end pointer
- Move function documentation into header file
Changes in v4:
- Add these changes in unless CONFIG_NO_SYS_VSNPRINT is defined
- Reduce code size overhead if disabled to only 4 bytes on ARM
- Remove the ugly #ifdef patch from series since it only saves 4 bytes
Changes in v5:
- Define INT_MAX locally within vsprintf.c
- Drop limits.h as it is used in only two places in U-Boot
Simon Glass (2):
Move vsprintf functions into their own header
vsprintf: Move function documentation into header file
Sonny Rao (2):
Add safe vsnprintf and snprintf library functions Make printf and vprintf safe from buffer overruns
README | 7 ++ common/console.c | 10 +- include/common.h | 10 +-- include/vsprintf.h | 180 +++++++++++++++++++++++++++++++++++++++ lib/vsprintf.c | 237 ++++++++++++++++++++++++++++++++------------------- 5 files changed, 342 insertions(+), 102 deletions(-) create mode 100644 include/vsprintf.h
Signature · 1 line
1.7.3.1
PATCH v5 1/4 Move vsprintf functions into their own header
common.h is very large, so before changing the vsprintf functions, move the prototypes into their own header file.
---
Changes in v3:
- Move prototypes from common.h to vsprintf.h
include/common.h | 10 +--------- include/vsprintf.h | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 include/vsprintf.h
include/common.h +1 -9
@@ -714,15 +714,7 @@ void uuid_str_to_bin(const char *uuid, unsigned char *out);int uuid_str_valid (const char *uuid);/* lib/vsprintf.c */ -ulong simple_strtoul (const char *cp,char **endp,unsigned int base); -int strict_strtoul (const char *cp,unsigned int base,unsigned long *res); -unsigned long long simple_strtoull (const char *cp,char **endp,unsigned int base); -long simple_strtol (const char *cp,char **endp,unsigned int base); -void panic (const char *fmt, ...) - __attribute__ ((format (__printf__,1 ,2 ), noreturn)); -int sprintf (char * buf,const char *fmt, ...) - __attribute__ ((format (__printf__,2 ,3 ))); -int vsprintf (char *buf,const char *fmt,va_list args); +#include <vsprintf.h> /* lib/strmhz.c */ char *strmhz (char *buf,unsigned long hz);
include/vsprintf.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* (C) Copyright 2000-2009 +* Wolfgang Denk, DENX Software Engineering, wd at denx.de. +* +* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA +*/ + +#ifndef __VSPRINTF_H +#define __VSPRINTF_H + +ulong simple_strtoul (const char *cp,char **endp,unsigned int base); +int strict_strtoul (const char *cp,unsigned int base,unsigned long *res); +unsigned long long simple_strtoull (const char *cp,char **endp, +unsigned int base); +long simple_strtol (const char *cp,char **endp,unsigned int base); +void panic (const char *fmt, ...) + __attribute__ ((format (__printf__,1 ,2 ), noreturn)); +int sprintf (char *buf,const char *fmt, ...) + __attribute__ ((format (__printf__,2 ,3 ))); +int vsprintf (char *buf,const char *fmt,va_list args); + +#endif
Signature · 1 line
1.7.3.1
PATCH v5 2/4 Add safe vsnprintf and snprintf library functions
In reply to Simon GlassFrom: Sonny Rao <[email protected]>
From: Sonny Rao <[email protected]>
These functions are useful in U-Boot because they allow a graceful failure rather than an unpredictable stack overflow when printf() buffers are exceeded.
Mostly copied from the Linux kernel. I copied vscnprintf and scnprintf so we can change printf and vprintf to use the safe implementation but still return the correct values.
(Simon Glass <[email protected]> modified this commit a little)
---
Changes in v3:
- Add CONFIG_SYS_VSNPRINT option to enable vsnprintf() functions
- Update README with CONFIG_SYS_VSNPRINT docs
- Use ADDCH macro to support checking/not checking end pointer
Changes in v4:
- Add these changes in unless CONFIG_NO_SYS_VSNPRINT is defined
- Reduce code size overhead if disabled to only 4 bytes on ARM
- Remove the ugly #ifdef patch from series since it only saves 4 bytes
Changes in v5:
- Define INT_MAX locally within vsprintf.c
README | 7 ++ include/vsprintf.h | 19 ++++ lib/vsprintf.c | 265 +++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 239 insertions(+), 52 deletions(-)
README +7 -0
@@ -638,6 +638,13 @@ The following options need to be configured: 'Sane' compilers will generate smaller code if CONFIG_PRE_CON_BUF_SZ is a power of 2 +- Safe printf() functions + Define CONFIG_SYS_NO_VSNPRINT to avoiding compilinf in + safe versions of the printf() functions. These are defined + in include/vsprintf.h and include snprintf(), vsnprintf() + and so on. Code size reduction is approximately 300-500 + bytes. + - Boot Delay: CONFIG_BOOTDELAY - in seconds Delay before automatically booting the default image; set to -1 to disable autoboot.
include/vsprintf.h +19 -0
@@ -35,4 +35,23 @@ int sprintf(char *buf, const char *fmt, ...) __attribute__ ((format (__printf__, 2, 3)));int vsprintf (char *buf,const char *fmt,va_list args); +#ifdef CONFIG_SYS_NO_VSNPRINT +/* +* Use macros to silently drop the size parameter. Note that the 'cn' +* versions are the same as the 'n' versions since the functions assume +* there is always enough buffer space when !CONFIG_SYS_VSNPRINT +*/ +#define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args) +#define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args) +#define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args) +#define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args) +#else +int snprintf (char *buf,size_t size,const char *fmt, ...) + __attribute__ ((format (__printf__,3 ,4 ))); +int scnprintf (char *buf,size_t size,const char *fmt, ...) + __attribute__ ((format (__printf__,3 ,4 ))); +int vsnprintf (char *buf,size_t size,const char *fmt,va_list args); +int vscnprintf (char *buf,size_t size,const char *fmt,va_list args); +#endif +#endif
lib/vsprintf.c +213 -52
423 lines not on this page · raw message
Signature · 1 line
1.7.3.1
PATCH v5 3/4 vsprintf: Move function documentation into header file
In reply to Simon GlassNow that this is not in common.h, perhaps it is acceptable to move this documentation into the header file.
---
Changes in v3:
- Move function documentation into header file
include/vsprintf.h | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++- lib/vsprintf.c | 100 ----------------------------------------- 2 files changed, 124 insertions(+), 101 deletions(-)
include/vsprintf.h +124 -1
@@ -25,14 +25,69 @@#define __VSPRINTF_Hulong simple_strtoul (const char *cp,char **endp,unsigned int base); + +/** +* strict_strtoul - convert a string to an unsigned long strictly +* @param cp The string to be converted +* @param base The number base to use +* @param res The converted result value +* @return 0 if conversion is successful and *res is set to the converted +* value, otherwise it returns -EINVAL and *res is set to 0. +* +* strict_strtoul converts a string to an unsigned long only if the +* string is really an unsigned long string, any string containing +* any invalid char at the tail will be rejected and -EINVAL is returned, +* only a newline char at the tail is acceptible because people generally +* change a module parameter in the following way: +* +* echo 1024 > /sys/module/e1000/parameters/copybreak +* +* echo will append a newline to the tail. +* +* simple_strtoul just ignores the successive invalid characters and +* return the converted value of prefix part of the string. +* +* Copied this function from Linux 2.6.38 commit ID: +* 521cb40b0c44418a4fd36dc633f575813d59a43d +* +*/ int strict_strtoul (const char *cp,unsigned int base,unsigned long *res);unsigned long long simple_strtoull (const char *cp,char **endp,unsigned int base);long simple_strtol (const char *cp,char **endp,unsigned int base);void panic (const char *fmt, ...) __attribute__ ((format (__printf__,1 ,2 ), noreturn)); + +/** +* Format a string and place it in a buffer +* +* @param buf The buffer to place the result into +* @param fmt The format string to use +* @param ... Arguments for the format string +* +* The function returns the number of characters written +* into @buf. +* +* See the vsprintf() documentation for format string extensions over C99. +*/ int sprintf (char *buf,const char *fmt, ...) __attribute__ ((format (__printf__,2 ,3 ))); + +/** +* Format a string and place it in a buffer (va_list version) +* +* @param buf The buffer to place the result into +* @param size The size of the buffer, including the trailing null space +* @param fmt The format string to use +* @param args Arguments for the format string +* @return the number of characters which have been written into +* the @buf not including the trailing '\0'. If @size is == 0 the function +* returns 0. +* +* If you're not already dealing with a va_list consider using scnprintf(). +* +* See the vsprintf() documentation for format string extensions over C99. +*/ int vsprintf (char *buf,const char *fmt,va_list args);#ifdef CONFIG_SYS_NO_VSNPRINT
@@ -46,12 +101,80 @@ int vsprintf(char *buf, const char *fmt, va_list args); #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args) #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)#else +/** +* Format a string and place it in a buffer +* +* @param buf The buffer to place the result into +* @param size The size of the buffer, including the trailing null space +* @param fmt The format string to use +* @param ... Arguments for the format string +* @return the number of characters which would be +* generated for the given input, excluding the trailing null, +* as per ISO C99. If the return is greater than or equal to +* @size, the resulting string is truncated. +* +* See the vsprintf() documentation for format string extensions over C99. +*/ int snprintf (char *buf,size_t size,const char *fmt, ...) __attribute__ ((format (__printf__,3 ,4 ))); + +/** +* Format a string and place it in a buffer +* +* @param buf The buffer to place the result into +* @param size The size of the buffer, including the trailing null space +* @param fmt The format string to use +* @param ... Arguments for the format string +* +* The return value is the number of characters written into @buf not including +* the trailing '\0'. If @size is == 0 the function returns 0. +* +* See the vsprintf() documentation for format string extensions over C99. +*/ int scnprintf (char *buf,size_t size,const char *fmt, ...) __attribute__ ((format (__printf__,3 ,4 ))); + +/** +* Format a string and place it in a buffer (base function) +* +* @param buf The buffer to place the result into +* @param size The size of the buffer, including the trailing null space +* @param fmt The format string to use +* @param args Arguments for the format string +* @return The number characters which would be generated for the given +* input, excluding the trailing '\0', as per ISO C99. Note that fewer +* characters may be written if this number of characters is >= size. +* +* This function follows C99 vsnprintf, but has some extensions: +* %pS output the name of a text symbol +* %pF output the name of a function pointer +* %pR output the address range in a struct resource +* +* The function returns the number of characters which would be +* generated for the given input, excluding the trailing '\0', +* as per ISO C99. +* +* Call this function if you are already dealing with a va_list. +* You probably want snprintf() instead. +*/ int vsnprintf (char *buf,size_t size,const char *fmt,va_list args); + +/** +* Format a string and place it in a buffer (va_list version) +* +* @param buf The buffer to place the result into +* @param size The size of the buffer, including the trailing null space +* @param fmt The format string to use +* @param args Arguments for the format string +* @return the number of characters which have been written into +* the @buf not including the trailing '\0'. If @size is == 0 the function +* returns 0. +* +* If you're not already dealing with a va_list consider using scnprintf(). +* +* See the vsprintf() documentation for format string extensions over C99. +*/ int vscnprintf (char *buf,size_t size,const char *fmt,va_list args); -#endif +#endif /* CONFIG_SYS_NO_VSNPRINT */ #endif
lib/vsprintf.c +0 -100
136 lines not on this page · raw message
Signature · 1 line
1.7.3.1
PATCH v5 4/4 Make printf and vprintf safe from buffer overruns
In reply to Simon GlassFrom: Sonny Rao <[email protected]>
From: Sonny Rao <[email protected]>
utilize the added vscnprintf functions to avoid buffer overruns The implementation is fairly dumb in that it doesn't detect that the buffer is too small, but at least will not cause crashes.
---
Changes in v2:
- Use sizeof(printbuffer) instead of CONFIG_SYS_PBSIZE
- Drop patch which changes network code to use snprintf()
Changes in v5:
- Drop limits.h as it is used in only two places in U-Boot
common/console.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
common/console.c +5 -5
@@ -212,7 +212,7 @@ int serial_printf(const char *fmt, ...)/* For this to work, printbuffer must be larger than * anything we ever want to print. */ - i =vsprintf (printbuffer, fmt, args); + i =vscnprintf (printbuffer,sizeof (printbuffer), fmt, args);va_end (args);serial_puts (printbuffer);
@@ -281,7 +281,7 @@ int fprintf(int file, const char *fmt, ...)/* For this to work, printbuffer must be larger than * anything we ever want to print. */ - i =vsprintf (printbuffer, fmt, args); + i =vscnprintf (printbuffer,sizeof (printbuffer), fmt, args);va_end (args);/* Send to desired file */
@@ -426,7 +426,7 @@ int printf(const char *fmt, ...)/* For this to work, printbuffer must be larger than * anything we ever want to print. */ - i =vsprintf (printbuffer, fmt, args); + i =vscnprintf (printbuffer,sizeof (printbuffer), fmt, args);va_end (args);/* Print the string */
@@ -447,7 +447,7 @@ int vprintf(const char *fmt, va_list args)/* For this to work, printbuffer must be larger than * anything we ever want to print. */ - i =vsprintf (printbuffer, fmt, args); + i =vscnprintf (printbuffer,sizeof (printbuffer), fmt, args);/* Print the string */ puts (printbuffer);
@@ -514,7 +514,7 @@ inline void dbg(const char *fmt, ...)/* For this to work, printbuffer must be larger than * anything we ever want to print. */ - i =vsprintf (printbuffer, fmt, args); + i =vsnprintf (printbuffer,sizeof (printbuffer), fmt, args);va_end (args); if ((screen + sizeof(screen) - 1 - cursor)
Signature · 1 line
1.7.3.1
v5 cover Buffer overruns in printf
In reply to Simon GlassHi,
Quoted 42 lines · Simon Glass
The printf family of functions in U-Boot cannot deal with a situation where the caller provides a buffer which turns out to be too small for the format string. This can result in buffer overflows, stack overflows and other bad behavior.
This patch series tidies this up in the common
vsprintf.ccode.You can find a discussion of the Linux / U-Boot licensing issues here:
http://patchwork.ozlabs.org/patch/116161/
Code Size Impact
----------------(From Simon Glass <[email protected]>)
With my ARMv7 compiler (gcc-4.4.3_cos_gg_53174) the code size increase is 312 bytes, about 10% increase to code size vsprintf.o.With the
CONFIG_SYS_NO_VSNPRINToption defined, the code size impact is 4 bytes.Changes in v2:
- Use sizeof(printbuffer) instead ofCONFIG_SYS_PBSIZE
- Drop patch which changes network code to usesnprintf()Changes in v3:
- Move prototypes fromcommon.htovsprintf.h
- AddCONFIG_SYS_VSNPRINToption to enablevsnprintf()functions
- Update README withCONFIG_SYS_VSNPRINTdocs
- Use ADDCH macro to support checking/not checking end pointer
- Move function documentation into header fileChanges in v4:
- Add these changes in unlessCONFIG_NO_SYS_VSNPRINTis defined
- Reduce code size overhead if disabled to only 4 bytes on ARM
- Remove the ugly #ifdef patch from series since it only saves 4 bytesChanges in v5:
- DefineINT_MAXlocally withinvsprintf.c
- Droplimits.has it is used in only two places in U-Boot
Quoted 8 lines
Simon Glass (2):
?Move vsprintf functions into their own header ?vsprintf: Move function documentation into header fileSonny Rao (2):
?Add safe vsnprintf and snprintf library functions ?Make printf and vprintf safe from buffer overruns
Any more comments on this?
I have suggested making these functions available by default, and having CONFIG_NO_SYS_VSNPRINT to remove them. The rationale is that it if CONFIG_NO_SYS_VSNPRINT is not defined, then snprintf() will not check the buffer length (all the code to do it is removed), and this is unexpected. In this case snprintf() is #defined to sprintf().
There is therefore a code size increase by default with this series. We can instead use CONFIG_SYS_VSNPRINT if people think it is safe.
Regards,
Simon
Quoted 12 lines
?README ? ? ? ? ? ? | ? ?7 ++ ?common/console.c ? | ? 10 +- ?include/common.h ? | ? 10 +-- ?include/vsprintf.h | ?180 +++++++++++++++++++++++++++++++++++++++ ?lib/vsprintf.c ? ? | ?237 ++++++++++++++++++++++++++++++++-------------------?5 files changed, 342 insertions(+), 102 deletions(-) ?create mode 100644
include/vsprintf.h--
1.7.3.1
v5 cover Buffer overruns in printf
Dear Simon Glass,
Quoted 3 lines · In message <CAPnjgZ2wW3tvGjxWgkVr0PHLr2Ttj8abtPc4aiyk=tgnpsSk3g@mail.gmail.com>…
I have suggested making these functions available by default, and having
CONFIG_NO_SYS_VSNPRINTto remove them. The rationale is that it
This makes little sense to me. We don't need an opt out here, but an opt in instead. Otherwise you willprobably see a number of builds breaking due to the increased code size.
Quoted 2 lines
There is therefore a code size increase by default with this series. We can instead use
CONFIG_SYS_VSNPRINTif people think it is safe.
Which boards were build tested with these patches applied?
Best regards,
Wolfgang Denk
Signature · 4 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Sex is like air. It's only a big deal if you can't get any.
v5 cover Buffer overruns in printf
Hi Wolfgang,
Quoted 10 lines · Wolfgang Denk
Dear Simon Glass,
Quoted 3 lines · In message <CAPnjgZ2wW3tvGjxWgkVr0PHLr2Ttj8abtPc4aiyk=tgnpsSk3g@mail.gmail.com>…
I have suggested making these functions available by default, and having
CONFIG_NO_SYS_VSNPRINTto remove them. The rationale is that itThis makes little sense to me. ?We don't need an opt out here, but an opt in instead. ?Otherwise you willprobably see a number of builds breaking due to the increased code size.
OK I can change this to CONFIG_SYS_VSNPRINT easily enough.
Quoted 5 lines
Quoted 3 lines
There is therefore a code size increase by default with this series. We can instead use
CONFIG_SYS_VSNPRINTif people think it is safe.Which boards were build tested with these patches applied?
I didn't see much difference with MAKEALL (but a lot of boards give warnings still). It would be great if I could get a clean build with no USB warnings, etc. At present I see about 180 with warnings out of 249, but I didn't see any that seemed to be affected by this patch. Perhaps they don't use printf() very early in the init when code size matters?
I am going to try to apply a few fix-up patches and see if I can get a clearer picture.
Regards,
Simon
Quoted 11 lines
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, ? ? MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Sex is like air. ?It's only a big deal if you can't get any.
v5 cover Buffer overruns in printf
Hi Wolfgang,
Quoted 7 lines · Simon Glass
Hi Wolfgang,
Quoted 4 lines · Wolfgang Denk
Dear Simon Glass,
Quoted 1 line · In message <CAPnjgZ2wW3tvGjxWgkVr0PHLr2Ttj8abtPc4aiyk=tgnpsSk3g@mail.gmail.com>…
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
I have suggested making these functions available by default, and having
CONFIG_NO_SYS_VSNPRINTto remove them. The rationale is that itThis makes little sense to me. ?We don't need an opt out here, but an opt in instead. ?Otherwise you willprobably see a number of builds breaking due to the increased code size.
OK I can change this to
CONFIG_SYS_VSNPRINTeasily enough.
I have done this and sent a new patch series (v6). It does not break any boards so far as I can tell. The code size increase on ARM is about 4 bytes and on PPC is 32 bytes. We can reduce this to closer to zero but it involves #ifdef hassle - please see the patch here:
http://patchwork.ozlabs.org/patch/120352/
Regards,
Simon