public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] coredump: fix va_list corruption
@ 2014-04-19 17:15 Eric Dumazet
  2014-04-19 18:04 ` Oleg Nesterov
  2014-04-19 23:14 ` Neil Horman
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Dumazet @ 2014-04-19 17:15 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: Oleg Nesterov, linux-kernel@vger.kernel.org, Neil Horman,
	Andrew Morton, Hugh Dickins

From: Eric Dumazet <edumazet@google.com>

A va_list needs to be copied in case it needs to be used twice.

Thanks to Hugh for debugging this issue, leading to various panics.

Tested:

lpq84:~# echo "|/foobar12345 %h %h %h %h %h %h %h %h %h %h %h %h %h %h %
h %h %h %h %h %h" >/proc/sys/kernel/core_pattern

'produce_core' is simply : main() { *(int *)0 = 1;}

lpq84:~# ./produce_core
Segmentation fault (core dumped)
lpq84:~# dmesg | tail -1
[  614.352947] Core dump to |/foobar12345 lpq84 lpq84 lpq84 lpq84 lpq84
lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84
lpq84 lpq84 (null) pipe failed

Notice the last argument was replaced by a NULL (we were lucky enough to
not crash, but do not try this on your production machine !)

After fix :

lpq83:~# echo "|/foobar12345 %h %h %h %h %h %h %h %h %h %h %h %h %h %h %
h %h %h %h %h %h" >/proc/sys/kernel/core_pattern
lpq83:~# ./produce_core
Segmentation fault
lpq83:~# dmesg | tail -1
[  740.800441] Core dump to |/foobar12345 lpq83 lpq83 lpq83 lpq83 lpq83
lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83
lpq83 lpq83 lpq83 pipe failed

Fixes: 5fe9d8ca21cc ("coredump: cn_vprintf() has no reason to call vsnprintf() twice")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Diagnosed-by: Hugh Dickins <hughd@google.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Neil Horman <nhorman@tuxdriver.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: stable@vger.kernel.org # 3.11+
---

Note: This patch applies cleanly to 3.11+ but might need some
adjustments for older linux kernels...

 fs/coredump.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/coredump.c b/fs/coredump.c
index e3ad709a4232..0b2528fb640e 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -73,10 +73,15 @@ static int expand_corename(struct core_name *cn, int size)
 static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg)
 {
 	int free, need;
+	va_list arg_copy;
 
 again:
 	free = cn->size - cn->used;
-	need = vsnprintf(cn->corename + cn->used, free, fmt, arg);
+
+	va_copy(arg_copy, arg);
+	need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
+	va_end(arg_copy);
+
 	if (need < free) {
 		cn->used += need;
 		return 0;



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

* Re: [PATCH] coredump: fix va_list corruption
  2014-04-19 17:15 [PATCH] coredump: fix va_list corruption Eric Dumazet
@ 2014-04-19 18:04 ` Oleg Nesterov
  2014-04-19 23:14 ` Neil Horman
  1 sibling, 0 replies; 3+ messages in thread
From: Oleg Nesterov @ 2014-04-19 18:04 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Linus Torvalds, linux-kernel@vger.kernel.org, Neil Horman,
	Andrew Morton, Hugh Dickins

On 04/19, Eric Dumazet wrote:
>
> A va_list needs to be copied in case it needs to be used twice.

OOPS :/

> --- a/fs/coredump.c
> +++ b/fs/coredump.c
> @@ -73,10 +73,15 @@ static int expand_corename(struct core_name *cn, int size)
>  static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg)
>  {
>  	int free, need;
> +	va_list arg_copy;
>
>  again:
>  	free = cn->size - cn->used;
> -	need = vsnprintf(cn->corename + cn->used, free, fmt, arg);
> +
> +	va_copy(arg_copy, arg);
> +	need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
> +	va_end(arg_copy);
> +

Thanks!

Acked-by: Oleg Nesterov <oleg@redhat.com>


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

* Re: [PATCH] coredump: fix va_list corruption
  2014-04-19 17:15 [PATCH] coredump: fix va_list corruption Eric Dumazet
  2014-04-19 18:04 ` Oleg Nesterov
@ 2014-04-19 23:14 ` Neil Horman
  1 sibling, 0 replies; 3+ messages in thread
From: Neil Horman @ 2014-04-19 23:14 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Linus Torvalds, Oleg Nesterov, linux-kernel@vger.kernel.org,
	Andrew Morton, Hugh Dickins

On Sat, Apr 19, 2014 at 10:15:07AM -0700, Eric Dumazet wrote:
> From: Eric Dumazet <edumazet@google.com>
> 
> A va_list needs to be copied in case it needs to be used twice.
> 
> Thanks to Hugh for debugging this issue, leading to various panics.
> 
> Tested:
> 
> lpq84:~# echo "|/foobar12345 %h %h %h %h %h %h %h %h %h %h %h %h %h %h %
> h %h %h %h %h %h" >/proc/sys/kernel/core_pattern
> 
> 'produce_core' is simply : main() { *(int *)0 = 1;}
> 
> lpq84:~# ./produce_core
> Segmentation fault (core dumped)
> lpq84:~# dmesg | tail -1
> [  614.352947] Core dump to |/foobar12345 lpq84 lpq84 lpq84 lpq84 lpq84
> lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84 lpq84
> lpq84 lpq84 (null) pipe failed
> 
> Notice the last argument was replaced by a NULL (we were lucky enough to
> not crash, but do not try this on your production machine !)
> 
> After fix :
> 
> lpq83:~# echo "|/foobar12345 %h %h %h %h %h %h %h %h %h %h %h %h %h %h %
> h %h %h %h %h %h" >/proc/sys/kernel/core_pattern
> lpq83:~# ./produce_core
> Segmentation fault
> lpq83:~# dmesg | tail -1
> [  740.800441] Core dump to |/foobar12345 lpq83 lpq83 lpq83 lpq83 lpq83
> lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83 lpq83
> lpq83 lpq83 lpq83 pipe failed
> 
> Fixes: 5fe9d8ca21cc ("coredump: cn_vprintf() has no reason to call vsnprintf() twice")
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Diagnosed-by: Hugh Dickins <hughd@google.com>
> Cc: Oleg Nesterov <oleg@redhat.com>
> Cc: Neil Horman <nhorman@tuxdriver.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: stable@vger.kernel.org # 3.11+
> ---
> 
> Note: This patch applies cleanly to 3.11+ but might need some
> adjustments for older linux kernels...
> 
>  fs/coredump.c |    7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/coredump.c b/fs/coredump.c
> index e3ad709a4232..0b2528fb640e 100644
> --- a/fs/coredump.c
> +++ b/fs/coredump.c
> @@ -73,10 +73,15 @@ static int expand_corename(struct core_name *cn, int size)
>  static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg)
>  {
>  	int free, need;
> +	va_list arg_copy;
>  
>  again:
>  	free = cn->size - cn->used;
> -	need = vsnprintf(cn->corename + cn->used, free, fmt, arg);
> +
> +	va_copy(arg_copy, arg);
> +	need = vsnprintf(cn->corename + cn->used, free, fmt, arg_copy);
> +	va_end(arg_copy);
> +
>  	if (need < free) {
>  		cn->used += need;
>  		return 0;
> 
> 
> 

Thanks Eric!
Acked-by: Neil Horman <nhorman@tuxdriver.com>


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

end of thread, other threads:[~2014-04-19 23:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-19 17:15 [PATCH] coredump: fix va_list corruption Eric Dumazet
2014-04-19 18:04 ` Oleg Nesterov
2014-04-19 23:14 ` Neil Horman

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