public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] vsprintf: Remove accidental VLA usage
@ 2018-03-07 23:07 Kees Cook
  2018-03-07 23:40 ` Andrew Morton
  2018-03-07 23:42 ` Tycho Andersen
  0 siblings, 2 replies; 7+ messages in thread
From: Kees Cook @ 2018-03-07 23:07 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Tobin C. Harding, Jonathan Corbet, Pantelis Antoniou,
	Steven Rostedt (VMware), kernel-hardening, linux-kernel,
	Gustavo A. R. Silva

The "sym" calculation is actually a fixed size, but since the max()
macro uses some extensive tricks for safety, it ends up looking like a
variable size. This replaces max() with a simple max macro which is
sufficient for the calculation of the array size.

Seen with -Wvla. Fixed as part of the directive to remove all VLAs from
the kernel: https://lkml.org/lkml/2018/3/7/621

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 lib/vsprintf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index d7a708f82559..f420ab1477cb 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -744,8 +744,9 @@ char *resource_string(char *buf, char *end, struct resource *res,
 #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
 #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
 #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
-	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
-		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
+#define SIMPLE_MAX(x, y)	((x) > (y) ? (x) : (y))
+	char sym[SIMPLE_MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
+			    2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
 
 	char *p = sym, *pend = sym + sizeof(sym);
 	int decode = (fmt[0] == 'R') ? 1 : 0;
-- 
2.7.4


-- 
Kees Cook
Pixel Security

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] vsprintf: Remove accidental VLA usage
  2018-03-07 23:07 [PATCH] vsprintf: Remove accidental VLA usage Kees Cook
@ 2018-03-07 23:40 ` Andrew Morton
  2018-03-07 23:56   ` Kees Cook
  2018-03-07 23:42 ` Tycho Andersen
  1 sibling, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2018-03-07 23:40 UTC (permalink / raw)
  To: Kees Cook
  Cc: Tobin C. Harding, Jonathan Corbet, Pantelis Antoniou,
	Steven Rostedt (VMware), kernel-hardening, linux-kernel,
	Gustavo A. R. Silva

On Wed, 7 Mar 2018 15:07:14 -0800 Kees Cook <keescook@chromium.org> wrote:

> The "sym" calculation is actually a fixed size, but since the max()
> macro uses some extensive tricks for safety, it ends up looking like a
> variable size. This replaces max() with a simple max macro which is
> sufficient for the calculation of the array size.
> 
> Seen with -Wvla. Fixed as part of the directive to remove all VLAs from
> the kernel: https://lkml.org/lkml/2018/3/7/621
> 
> ...
>
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -744,8 +744,9 @@ char *resource_string(char *buf, char *end, struct resource *res,
>  #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
>  #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
>  #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
> -	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
> -		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
> +#define SIMPLE_MAX(x, y)	((x) > (y) ? (x) : (y))
> +	char sym[SIMPLE_MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
> +			    2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
>  
>  	char *p = sym, *pend = sym + sizeof(sym);
>  	int decode = (fmt[0] == 'R') ? 1 : 0;

A year from now I'll be receiving an email titled

	[patch] lib/vsprintf.c: use standard max() macro

won't I?

--- a/lib/vsprintf.c~vsprintf-remove-accidental-vla-usage-fix
+++ a/lib/vsprintf.c
@@ -754,6 +754,7 @@ char *resource_string(char *buf, char *e
 #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
 #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
 #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
+/* regular max() tricks gcc into creating a variable length array */
 #define SIMPLE_MAX(x, y)	((x) > (y) ? (x) : (y))
 	char sym[SIMPLE_MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
 			    2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
_

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] vsprintf: Remove accidental VLA usage
  2018-03-07 23:07 [PATCH] vsprintf: Remove accidental VLA usage Kees Cook
  2018-03-07 23:40 ` Andrew Morton
@ 2018-03-07 23:42 ` Tycho Andersen
  2018-03-07 23:59   ` Kees Cook
  1 sibling, 1 reply; 7+ messages in thread
From: Tycho Andersen @ 2018-03-07 23:42 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Tobin C. Harding, Jonathan Corbet,
	Pantelis Antoniou, Steven Rostedt (VMware), kernel-hardening,
	linux-kernel, Gustavo A. R. Silva

Hi Kees,

On Wed, Mar 07, 2018 at 03:07:14PM -0800, Kees Cook wrote:
> The "sym" calculation is actually a fixed size, but since the max()
> macro uses some extensive tricks for safety, it ends up looking like a
> variable size. This replaces max() with a simple max macro which is
> sufficient for the calculation of the array size.
> 
> Seen with -Wvla. Fixed as part of the directive to remove all VLAs from
> the kernel: https://lkml.org/lkml/2018/3/7/621
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  lib/vsprintf.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index d7a708f82559..f420ab1477cb 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -744,8 +744,9 @@ char *resource_string(char *buf, char *end, struct resource *res,
>  #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
>  #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
>  #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
> -	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
> -		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
> +#define SIMPLE_MAX(x, y)	((x) > (y) ? (x) : (y))

It's probably worth hoisting this out into some other header. When I
was looking at this a while ago, this problem happens in a few places,
see e.g. net/ipv4/proc.c:TCPUDP_MIB_MAX.

Cheers,

Tycho

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] vsprintf: Remove accidental VLA usage
  2018-03-07 23:40 ` Andrew Morton
@ 2018-03-07 23:56   ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2018-03-07 23:56 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Tobin C. Harding, Jonathan Corbet, Pantelis Antoniou,
	Steven Rostedt (VMware), Kernel Hardening, LKML,
	Gustavo A. R. Silva

On Wed, Mar 7, 2018 at 3:40 PM, Andrew Morton <akpm@linux-foundation.org> wrote:
> On Wed, 7 Mar 2018 15:07:14 -0800 Kees Cook <keescook@chromium.org> wrote:
>
>> The "sym" calculation is actually a fixed size, but since the max()
>> macro uses some extensive tricks for safety, it ends up looking like a
>> variable size. This replaces max() with a simple max macro which is
>> sufficient for the calculation of the array size.
>>
>> Seen with -Wvla. Fixed as part of the directive to remove all VLAs from
>> the kernel: https://lkml.org/lkml/2018/3/7/621
>>
>> ...
>>
>> --- a/lib/vsprintf.c
>> +++ b/lib/vsprintf.c
>> @@ -744,8 +744,9 @@ char *resource_string(char *buf, char *end, struct resource *res,
>>  #define FLAG_BUF_SIZE                (2 * sizeof(res->flags))
>>  #define DECODED_BUF_SIZE     sizeof("[mem - 64bit pref window disabled]")
>>  #define RAW_BUF_SIZE         sizeof("[mem - flags 0x]")
>> -     char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
>> -                  2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
>> +#define SIMPLE_MAX(x, y)     ((x) > (y) ? (x) : (y))
>> +     char sym[SIMPLE_MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
>> +                         2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
>>
>>       char *p = sym, *pend = sym + sizeof(sym);
>>       int decode = (fmt[0] == 'R') ? 1 : 0;
>
> A year from now I'll be receiving an email titled
>
>         [patch] lib/vsprintf.c: use standard max() macro
>
> won't I?

Hopefully a year from now we'll have added -Wvla to the global build
flags and no one will be able to try that. :)

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] vsprintf: Remove accidental VLA usage
  2018-03-07 23:42 ` Tycho Andersen
@ 2018-03-07 23:59   ` Kees Cook
  2018-03-08  0:03     ` Andrew Morton
  0 siblings, 1 reply; 7+ messages in thread
From: Kees Cook @ 2018-03-07 23:59 UTC (permalink / raw)
  To: Tycho Andersen
  Cc: Andrew Morton, Tobin C. Harding, Jonathan Corbet,
	Pantelis Antoniou, Steven Rostedt (VMware), Kernel Hardening,
	LKML, Gustavo A. R. Silva

On Wed, Mar 7, 2018 at 3:42 PM, Tycho Andersen <tycho@tycho.ws> wrote:
> Hi Kees,
>
> On Wed, Mar 07, 2018 at 03:07:14PM -0800, Kees Cook wrote:
>> The "sym" calculation is actually a fixed size, but since the max()
>> macro uses some extensive tricks for safety, it ends up looking like a
>> variable size. This replaces max() with a simple max macro which is
>> sufficient for the calculation of the array size.
>>
>> Seen with -Wvla. Fixed as part of the directive to remove all VLAs from
>> the kernel: https://lkml.org/lkml/2018/3/7/621
>>
>> Signed-off-by: Kees Cook <keescook@chromium.org>
>> ---
>>  lib/vsprintf.c | 5 +++--
>>  1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
>> index d7a708f82559..f420ab1477cb 100644
>> --- a/lib/vsprintf.c
>> +++ b/lib/vsprintf.c
>> @@ -744,8 +744,9 @@ char *resource_string(char *buf, char *end, struct resource *res,
>>  #define FLAG_BUF_SIZE                (2 * sizeof(res->flags))
>>  #define DECODED_BUF_SIZE     sizeof("[mem - 64bit pref window disabled]")
>>  #define RAW_BUF_SIZE         sizeof("[mem - flags 0x]")
>> -     char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
>> -                  2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
>> +#define SIMPLE_MAX(x, y)     ((x) > (y) ? (x) : (y))
>
> It's probably worth hoisting this out into some other header. When I
> was looking at this a while ago, this problem happens in a few places,
> see e.g. net/ipv4/proc.c:TCPUDP_MIB_MAX.

Hmm, good point. All of those suffer from the same "max*() is too fancy".

I didn't want to encourage a global macro that _lacked_ the safety
built into the max*() family, though... thoughts for a reasonable
approach?

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] vsprintf: Remove accidental VLA usage
  2018-03-07 23:59   ` Kees Cook
@ 2018-03-08  0:03     ` Andrew Morton
  2018-03-08  0:36       ` Kees Cook
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2018-03-08  0:03 UTC (permalink / raw)
  To: Kees Cook
  Cc: Tycho Andersen, Tobin C. Harding, Jonathan Corbet,
	Pantelis Antoniou, Steven Rostedt (VMware), Kernel Hardening,
	LKML, Gustavo A. R. Silva

On Wed, 7 Mar 2018 15:59:27 -0800 Kees Cook <keescook@chromium.org> wrote:

> I didn't want to encourage a global macro that _lacked_ the safety
> built into the max*() family, though... thoughts for a reasonable
> approach?

I think SIMPLE_MAX() is OK.    Along with one of /* these */ things ;)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] vsprintf: Remove accidental VLA usage
  2018-03-08  0:03     ` Andrew Morton
@ 2018-03-08  0:36       ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2018-03-08  0:36 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Tycho Andersen, Tobin C. Harding, Jonathan Corbet,
	Pantelis Antoniou, Steven Rostedt (VMware), Kernel Hardening,
	LKML, Gustavo A. R. Silva

On Wed, Mar 7, 2018 at 4:03 PM, Andrew Morton <akpm@linux-foundation.org> wrote:
> On Wed, 7 Mar 2018 15:59:27 -0800 Kees Cook <keescook@chromium.org> wrote:
>
>> I didn't want to encourage a global macro that _lacked_ the safety
>> built into the max*() family, though... thoughts for a reasonable
>> approach?
>
> I think SIMPLE_MAX() is OK.    Along with one of /* these */ things ;)

Sounds good. I will resend with net/ipv[46]/proc.c and one other fixed
as well...

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-03-08  0:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-07 23:07 [PATCH] vsprintf: Remove accidental VLA usage Kees Cook
2018-03-07 23:40 ` Andrew Morton
2018-03-07 23:56   ` Kees Cook
2018-03-07 23:42 ` Tycho Andersen
2018-03-07 23:59   ` Kees Cook
2018-03-08  0:03     ` Andrew Morton
2018-03-08  0:36       ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox