Skip to contentSkip to conversation

vsprintf: fix possible NULL pointer deref in vsnprintf()

PATCH In review Sergey Shtylyov 6 participants 7 messages Jan 5, 2023 – Jan 9, 2023

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

PATCH 1/1 vsprintf: fix possible NULL pointer deref in vsnprintf()

In vsnprintf() etc, C99 allows the 'buf' argument to be NULL when the 'size' argument equals 0. Let us treat NULL passed as if the 'buf' argument pointed to a 0-sized buffer, so that we can avoid a NULL pointer dereference and still return the # of characters that would be written if 'buf' pointed to a valid buffer...

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'master' branch of the PRINTK Group's repo...

 lib/vsprintf.c |    9 +++++++++
 1 file changed, 9 insertions(+)
lib/vsprintf.c +9 -0
Index: linux/lib/vsprintf.c
===================================================================
@@ -2738,6 +2738,15 @@ int vsnprintf(char *buf, size_t size, co
 	if (WARN_ON_ONCE(size > INT_MAX))
 		return 0;
 
+	/*
+	 * C99 allows @buf to be NULL when @size is 0. We treat such NULL as if
+	 * @buf pointed to 0-sized buffer, so we can both avoid a NULL pointer
+	 * dereference and still return # of characters that would be written
+	 * if @buf pointed to a valid buffer...
+	 */
+	if (!buf)
+		size = 0;
+
 	str = buf;
 	end = buf + size;
 
maintainer
#1 lore raw

Hi,

Adding Kees, Linus, and linux-hardening into Cc. It seems that __vsnprintf_internal() does not do this in glibc. I wonder if there is a good reason for it.

My guess is that glibc does not do this either because they do not want to quietly hide bugs or just nobody had this idea.

Quoted 5 lines · Sergey Shtylyov

In vsnprintf() etc, C99 allows the 'buf' argument to be NULL when the 'size' argument equals 0. Let us treat NULL passed as if the 'buf' argument pointed to a 0-sized buffer, so that we can avoid a NULL pointer dereference and still return the # of characters that would be written if 'buf' pointed to a valid buffer...

This is misleading. The message says how vsprintf() should behave by the standard. But it does not describe what this patch does.

The function already behaves by the standard. The change is that the vsprintf() will catch an obvious mistake and prevent failure.

Quoted 17 lines

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'master' branch of the PRINTK Group's repo...

 lib/vsprintf.c |    9 +++++++++
 1 file changed, 9 insertions(+)

Index: linux/lib/vsprintf.c
===================================================================

lib/vsprintf.c +0 -0
@@ -2738,6 +2738,15 @@ int vsnprintf(char *buf, size_t size, co
 	if (WARN_ON_ONCE(size > INT_MAX))

lib/vsprintf.c int vsnprintf(char *buf, size_t size, co · line 2748

Quoted 10 lines
lib/vsprintf.c +8 -0
 		return 0;
 
+	/*
+	 * C99 allows @buf to be NULL when @size is 0. We treat such NULL as if
+	 * @buf pointed to 0-sized buffer, so we can both avoid a NULL pointer
+	 * dereference and still return # of characters that would be written
+	 * if @buf pointed to a valid buffer...
+	 */
+	if (!buf)
+		size = 0;

It makes sense except that it would hide bugs. It should print a warning, for example:

	char *err_msg;

	err_msg = check_pointer_msg(buf);
	if (err_msg) {
		WARN_ONCE(1, "Invalid buffer passed to vsnprintf(). Trying to continue with 0 length limit\n");
		size = 0;
	}

check_pointer_msg() allows to catch even more buggy pointers. WARN() helps to locate the caller. WARN_ONCE() variant is used to prevent a potential infinite loop.

lib/vsprintf.c int vsnprintf(char *buf, size_t size, co · line 2752

Quoted 4 lines
lib/vsprintf.c +1 -0
+
 	str = buf;
 	end = buf + size;
 

Best Regards,
Petr

maintainer
#2 lore raw
Quoted 8 lines · On Fri, 6 Jan 2023 16:49:46 +0100 Petr Mladek <[email protected]> wrote:
Quoted 8 lines

Index: linux/lib/vsprintf.c
===================================================================

lib/vsprintf.c +0 -0
@@ -2738,6 +2738,15 @@ int vsnprintf(char *buf, size_t size, co
 	if (WARN_ON_ONCE(size > INT_MAX))
 		return 0;
 
Quoted 11 lines

lib/vsprintf.c int vsnprintf(char *buf, size_t size, co · line 2748

Quoted 8 lines
lib/vsprintf.c +8 -0
+	/*
+	 * C99 allows @buf to be NULL when @size is 0. We treat such NULL as if
+	 * @buf pointed to 0-sized buffer, so we can both avoid a NULL pointer
+	 * dereference and still return # of characters that would be written
+	 * if @buf pointed to a valid buffer...
+	 */
+	if (!buf)
+		size = 0;  

It makes sense except that it would hide bugs. It should print a warning, for example:

I agree. This is a bug, and should not be quietly ignored.

Quoted 8 lines
	char *err_msg;

	err_msg = check_pointer_msg(buf);
	if (err_msg) {
		WARN_ONCE(1, "Invalid buffer passed to vsnprintf(). Trying to continue with 0 length limit\n");
		size = 0;
	}
	if (WARN_ONCE(err_msg, "Invalid buffer passed to vsnprintf(). Trying to continue with 0 length limit\n"))
		size = 0;

;-)

-- Steve

Quoted 9 lines

check_pointer_msg() allows to catch even more buggy pointers. WARN() helps to locate the caller. WARN_ONCE() variant is used to prevent a potential infinite loop.

Quoted 4 lines
lib/vsprintf.c +1 -0
+
 	str = buf;
 	end = buf + size;
 
#3 lore raw
Quoted 23 lines · Steven Rostedt
Quoted 8 lines · On Fri, 6 Jan 2023 16:49:46 +0100 Petr Mladek <[email protected]> wrote:
Quoted 8 lines

Index: linux/lib/vsprintf.c
===================================================================

lib/vsprintf.c +0 -0
@@ -2738,6 +2738,15 @@ int vsnprintf(char *buf, size_t size, co
 	if (WARN_ON_ONCE(size > INT_MAX))
 		return 0;
 
Quoted 11 lines

lib/vsprintf.c int vsnprintf(char *buf, size_t size, co · line 2748

Quoted 8 lines
lib/vsprintf.c +8 -0
+	/*
+	 * C99 allows @buf to be NULL when @size is 0. We treat such NULL as if
+	 * @buf pointed to 0-sized buffer, so we can both avoid a NULL pointer
+	 * dereference and still return # of characters that would be written
+	 * if @buf pointed to a valid buffer...
+	 */
+	if (!buf)
+		size = 0;  

It makes sense except that it would hide bugs. It should print a warning, for example:

Quoted 1 line

I agree. This is a bug, and should not be quietly ignored.

Yup.

Quoted 4 lines
Quoted 4 lines

	char *err_msg;
Quoted 8 lines
Quoted 5 lines
	err_msg = check_pointer_msg(buf);
	if (err_msg) {
		WARN_ONCE(1, "Invalid buffer passed to vsnprintf(). Trying to continue with 0 length limit\n");
		size = 0;
	}
	if (WARN_ONCE(err_msg, "Invalid buffer passed to vsnprintf(). Trying to continue with 0 length limit\n"))
		size = 0;

Also good. Please CC me for an Ack when this is a full patch. :)

-Kees

Signature · 1 line
Kees Cook
#4 lore raw
In reply to Petr Mladek
Quoted 4 lines · Petr Mladek

Adding Kees, Linus, and linux-hardening into Cc. It seems that __vsnprintf_internal() does not do this in glibc. I wonder if there is a good reason for it.

I do not think that patch is valid.

And I don't like Steven Rostedt's suggestion either.

I think that code *should* take a NULL pointer dereference, exactly the same way "strlen()" would do if you pass it a NULL pointer and claim there is room there.

No silly checks for invalid cases.

There's any number of invalid things you can do in the kernel, and we don't check for those either.

I don't particularly like the "pass NULL to sprintf()" thing at all, but *if* somebody does, they had better just pass a zero size too.

And doing

git grep 'sn*printf(NULL'

seems to show that all current users do exactly that.

If somebody wants to check for this, make it a coccinelle script or something. Not a runtime check for invalid use cases.

Linus

#5 lore raw
In reply to Sergey Shtylyov
Quoted 8 lines · Sergey Shtylyov

In vsnprintf() etc, C99 allows the 'buf' argument to be NULL when the 'size' argument equals 0. Let us treat NULL passed as if the 'buf' argument pointed to a 0-sized buffer, so that we can avoid a NULL pointer dereference and still return the # of characters that would be written if 'buf' pointed to a valid buffer...

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

...

lib/vsprintf.c int vsnprintf(char *buf, size_t size, co · line 2748

Quoted 8 lines
lib/vsprintf.c +8 -0
+	/*
+	 * C99 allows @buf to be NULL when @size is 0. We treat such NULL as if
+	 * @buf pointed to 0-sized buffer, so we can both avoid a NULL pointer
+	 * dereference and still return # of characters that would be written
+	 * if @buf pointed to a valid buffer...
+	 */
+	if (!buf)
+		size = 0;

Do we have test cases for that?

And what's wrong to print "(null)" ? Have you checked if your patch makes any regressions to those cases?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 1 line · Andy Shevchenko

On Fri, Jan 06, 2023 at 12:16:31AM +0300, Sergey Shtylyov wrote:

...

Quoted 10 lines

lib/vsprintf.c int vsnprintf(char *buf, size_t size, co · line 2748

Quoted 8 lines
lib/vsprintf.c +8 -0
+	/*
+	 * C99 allows @buf to be NULL when @size is 0. We treat such NULL as if
+	 * @buf pointed to 0-sized buffer, so we can both avoid a NULL pointer
+	 * dereference and still return # of characters that would be written
+	 * if @buf pointed to a valid buffer...
+	 */
+	if (!buf)
+		size = 0;

Do we have test cases for that?

This still stays...

Quoted 2 lines

And what's wrong to print "(null)" ? Have you checked if your patch makes any regressions to those cases?

...but this paragraph is not for the case (I mixed it with the arguments to be NULL).

Signature · 2 lines
With Best Regards,
Andy Shevchenko