Skip to contentSkip to conversation

Buffer overruns in printf

PATCH Superseded Simon Glass 2 participants 12 messages Nov 2, 2011 – Dec 17, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 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_VSNPRINT option undefined, 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

Changes in v6:
- Change the config option to CONFIG_SYS_VSNPRINTF
- Make the default be to NOT include safe printf functions

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             |    9 ++
 common/console.c   |   10 +-
 include/common.h   |   11 +--
 include/vsprintf.h |  181 +++++++++++++++++++++++++++++++++++++++
 lib/vsprintf.c     |  237 ++++++++++++++++++++++++++++++++-------------------
 5 files changed, 345 insertions(+), 103 deletions(-)
 create mode 100644 include/vsprintf.h
Signature · 1 line
1.7.3.1
#1 lore raw

PATCH v6 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   |   11 +----------
 include/vsprintf.h |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 10 deletions(-)
 create mode 100644 include/vsprintf.h
include/common.h +1 -10
index 5ca8820..ce80e85 100644
@@ -725,16 +725,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);
-char *simple_itoa(ulong i);
+#include <vsprintf.h>
 
 /* lib/strmhz.c */
 char *	strmhz(char *buf, unsigned long hz);
include/vsprintf.h new file +39 -0
new file mode 100644
index 0000000..0651446
@@ -0,0 +1,39 @@
+/*
+ * (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);
+char *simple_itoa(ulong i);
+
+#endif
Signature · 1 line
1.7.3.1
#2 lore raw

PATCH v6 1/4 Move vsprintf functions into their own header

Dear Simon Glass,

Quoted 4 lines · In message <[email protected]> you wrote:

common.h is very large, so before changing the vsprintf functions, move the prototypes into their own header file.

Quoted 8 lines

---
Changes in v3:
- Move prototypes from common.h to vsprintf.h

 include/common.h   |   11 +----------
 include/vsprintf.h |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 10 deletions(-)
 create mode 100644 include/vsprintf.h

Applied to "next" branch, thanks.

Best regards,

Wolfgang Denk

Signature · 6 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
Man is the best computer we can put aboard a spacecraft ...  and  the
only one that can be mass produced with unskilled labor.
                                                 -- Wernher von Braun
#3 lore raw

PATCH v6 2/4 Add safe vsnprintf and snprintf library functions

In reply to Simon Glass

From: 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

Changes in v6:
- Change the config option to CONFIG_SYS_VSNPRINTF
- Make the default be to NOT include safe printf functions

 README             |    9 ++
 include/vsprintf.h |   19 ++++
 lib/vsprintf.c     |  265 +++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 241 insertions(+), 52 deletions(-)
README +9 -0
index c05c40a..6fb604d 100644
@@ -638,6 +638,15 @@ 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_VSNPRINTF to compile in safe versions of
+		the printf() functions. These are defined in
+		include/vsprintf.h and include snprintf(), vsnprintf() and
+		so on. Code size increase is approximately 300-500 bytes.
+		If this option is not given then these functions will
+		silently discard their buffer size argument - this means
+		you are not getting any overflow checking in this case.
+
 - Boot Delay:	CONFIG_BOOTDELAY - in seconds
 		Delay before automatically booting the default image;
 		set to -1 to disable autoboot.
include/vsprintf.h +19 -0
index 0651446..5195598 100644
@@ -36,4 +36,23 @@ int sprintf(char *buf, const char *fmt, ...)
 int vsprintf(char *buf, const char *fmt, va_list args);
 char *simple_itoa(ulong i);
 
+#ifdef CONFIG_SYS_VSNPRINTF
+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);
+#else
+/*
+ * 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_VSNPRINTF
+ */
+#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)
+#endif /* CONFIG_SYS_VSNPRINTF */
+
 #endif
lib/vsprintf.c +213 -52

423 lines not on this page · raw message

Signature · 1 line
1.7.3.1
#4 lore raw

PATCH v6 2/4 Add safe vsnprintf and snprintf library functions

Dear Simon Glass,

Quoted 29 lines · In message <[email protected]> you wrote:

From: 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

Quoted 8 lines

Changes in v6:
- Change the config option to CONFIG_SYS_VSNPRINTF
- Make the default be to NOT include safe printf functions

 README             |    9 ++
 include/vsprintf.h |   19 ++++
 lib/vsprintf.c     |  265 +++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 241 insertions(+), 52 deletions(-)

Applied to "next" branch, thanks.

Best regards,

Wolfgang Denk

Signature · 9 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
"IBM uses what I like to call the 'hole-in-the-ground  technique'  to
destroy  the  competition.....  IBM digs a big HOLE in the ground and
covers it with leaves. It then puts a big POT OF GOLD nearby. Then it
gives the call, 'Hey, look at all this gold, get over here fast.'  As
soon  as  the competitor approaches the pot, he falls into the pit"
                                                     - John C. Dvorak
#5 lore raw

PATCH v6 3/4 vsprintf: Move function documentation into header file

In reply to Simon Glass

Now 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 |  123 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/vsprintf.c     |  100 ------------------------------------------
 2 files changed, 123 insertions(+), 100 deletions(-)
include/vsprintf.h +123 -0
index 5195598..651077c 100644
@@ -25,23 +25,146 @@
 #define __VSPRINTF_H
 
 ulong 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);
 char *simple_itoa(ulong i);
 
 #ifdef CONFIG_SYS_VSNPRINTF
+/**
+ * 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);
 #else
 /*
lib/vsprintf.c +0 -100

136 lines not on this page · raw message

Signature · 1 line
1.7.3.1
#6 lore raw

PATCH v6 3/4 vsprintf: Move function documentation into header file

Dear Simon Glass,

Quoted 11 lines · In message <[email protected]> you wrote:

Now 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 |  123 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/vsprintf.c     |  100 ------------------------------------------
 2 files changed, 123 insertions(+), 100 deletions(-)

Applied, thanks.

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
Overdrawn?  But I still have checks left!
#7 lore raw

PATCH v6 4/4 Make printf and vprintf safe from buffer overruns

In reply to Simon Glass

From: 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
index f17875e..1177f7d 100644
@@ -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
#8 lore raw

PATCH v6 4/4 Make printf and vprintf safe from buffer overruns

Dear Simon Glass,

Quoted 14 lines · In message <[email protected]> you wrote:

From: 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()

Quoted 5 lines

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

Applied to "next" branch, thanks.

Best regards,

Wolfgang Denk

Signature · 6 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
There is, however, a strange, musty smell in the air that reminds  me
of something...hmm...yes...I've got it...there's a VMS nearby, or I'm
a Blit.          - Larry Wall in Configure from the perl distribution
#9 lore raw

v6 cover Buffer overruns in printf

In reply to Simon Glass

Hi,

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

Does any maintainer want to pick up this series? It passes the MAKEALL test for me.

Regards,
Simon

Quoted 62 lines

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_VSNPRINT option undefined, 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

Changes in v6:
- Change the config option to CONFIG_SYS_VSNPRINTF
- Make the default be to NOT include safe printf functions

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 ? ? ? ? ? ? | ? ?9 ++
?common/console.c ? | ? 10 +-
?include/common.h ? | ? 11 +--
?include/vsprintf.h | ?181 +++++++++++++++++++++++++++++++++++++++
?lib/vsprintf.c ? ? | ?237 ++++++++++++++++++++++++++++++++-------------------

?5 files changed, 345 insertions(+), 103 deletions(-) ?create mode 100644 include/vsprintf.h

--
1.7.3.1

#10 lore raw

v6 cover Buffer overruns in printf

Hi Wolfgang,

Quoted 10 lines · Simon Glass

Hi,

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

Does any maintainer want to pick up this series? It passes the MAKEALL test for me.

Is this series going into this month's release?

Regards,
Simon

Quoted 66 lines

Regards,
Simon

Quoted 62 lines

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_VSNPRINT option undefined, 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

Changes in v6:
- Change the config option to CONFIG_SYS_VSNPRINTF
- Make the default be to NOT include safe printf functions

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 ? ? ? ? ? ? | ? ?9 ++
?common/console.c ? | ? 10 +-
?include/common.h ? | ? 11 +--
?include/vsprintf.h | ?181 +++++++++++++++++++++++++++++++++++++++
?lib/vsprintf.c ? ? | ?237 ++++++++++++++++++++++++++++++++-------------------

?5 files changed, 345 insertions(+), 103 deletions(-) ?create mode 100644 include/vsprintf.h

--
1.7.3.1

#11 lore raw

v6 cover Buffer overruns in printf

Dear Simon Glass,

Quoted 2 lines · In message <CAPnjgZ0MFpTsLghMxEOP-nvpe=H-v2p4r3eptDpv7rV8XSrNPw@mail.gmail.com>…

Is this series going into this month's release?

No. This goes into "next".

Best regards,

Wolfgang Denk

Signature · 5 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
Do not follow where the path may lead....go instead where there is no
path and leave a trail.