All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] kernel/panic: Filter out a potential trailing newline
@ 2018-10-09 20:50 Borislav Petkov
  2018-10-09 21:13 ` Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Borislav Petkov @ 2018-10-09 20:50 UTC (permalink / raw)
  To: LKML; +Cc: Andrew Morton, Kees Cook, Steven Rostedt (VMware), x86

From: Borislav Petkov <bp@suse.de>

If a call to panic() terminates the string with a \n, the result puts
the closing brace ']---' on a newline because panic() itself adds \n
too.

Now, if one goes and removes the newline chars from all panic()
invocations - and the stats right now look like this:

~300 calls with an \n
~500 calls without a \n

one is destined to a neverending game of whack-a-mole because the usual
thing to do is add a newline at the end of a string a function is
supposed to print.

Therefore, simply zap any \n at the end of the panic string to avoid
touching so many places in the kernel.

Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
Cc: x86@kernel.org
---
 kernel/panic.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/panic.c b/kernel/panic.c
index 8b2e002d52eb..5776d2879650 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -136,7 +136,7 @@ void panic(const char *fmt, ...)
 {
 	static char buf[1024];
 	va_list args;
-	long i, i_next = 0;
+	long i, i_next = 0, len;
 	int state = 0;
 	int old_cpu, this_cpu;
 	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
@@ -173,8 +173,12 @@ void panic(const char *fmt, ...)
 	console_verbose();
 	bust_spinlocks(1);
 	va_start(args, fmt);
-	vsnprintf(buf, sizeof(buf), fmt, args);
+	len = vscnprintf(buf, sizeof(buf), fmt, args);
 	va_end(args);
+
+	if (len && buf[len - 1] == '\n')
+		buf[len - 1] = '\0';
+
 	pr_emerg("Kernel panic - not syncing: %s\n", buf);
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 	/*
-- 
2.19.0.271.gfe8321ec057f


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

* Re: [RFC PATCH] kernel/panic: Filter out a potential trailing newline
  2018-10-09 20:50 [RFC PATCH] kernel/panic: Filter out a potential trailing newline Borislav Petkov
@ 2018-10-09 21:13 ` Kees Cook
  2018-10-09 21:23 ` Steven Rostedt
  2018-10-10  6:00 ` Ingo Molnar
  2 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2018-10-09 21:13 UTC (permalink / raw)
  To: Borislav Petkov; +Cc: LKML, Andrew Morton, Steven Rostedt (VMware), X86 ML

On Tue, Oct 9, 2018 at 1:50 PM, Borislav Petkov <bp@alien8.de> wrote:
> From: Borislav Petkov <bp@suse.de>
>
> If a call to panic() terminates the string with a \n, the result puts
> the closing brace ']---' on a newline because panic() itself adds \n
> too.
>
> Now, if one goes and removes the newline chars from all panic()
> invocations - and the stats right now look like this:
>
> ~300 calls with an \n
> ~500 calls without a \n
>
> one is destined to a neverending game of whack-a-mole because the usual
> thing to do is add a newline at the end of a string a function is
> supposed to print.
>
> Therefore, simply zap any \n at the end of the panic string to avoid
> touching so many places in the kernel.
>
> Signed-off-by: Borislav Petkov <bp@suse.de>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
> Cc: x86@kernel.org

Ah yes, I like this. :)

Acked-by: Kees Cook <keescook@chromium.org>

-Kees

> ---
>  kernel/panic.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/panic.c b/kernel/panic.c
> index 8b2e002d52eb..5776d2879650 100644
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -136,7 +136,7 @@ void panic(const char *fmt, ...)
>  {
>         static char buf[1024];
>         va_list args;
> -       long i, i_next = 0;
> +       long i, i_next = 0, len;
>         int state = 0;
>         int old_cpu, this_cpu;
>         bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
> @@ -173,8 +173,12 @@ void panic(const char *fmt, ...)
>         console_verbose();
>         bust_spinlocks(1);
>         va_start(args, fmt);
> -       vsnprintf(buf, sizeof(buf), fmt, args);
> +       len = vscnprintf(buf, sizeof(buf), fmt, args);
>         va_end(args);
> +
> +       if (len && buf[len - 1] == '\n')
> +               buf[len - 1] = '\0';
> +
>         pr_emerg("Kernel panic - not syncing: %s\n", buf);
>  #ifdef CONFIG_DEBUG_BUGVERBOSE
>         /*
> --
> 2.19.0.271.gfe8321ec057f
>



-- 
Kees Cook
Pixel Security

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

* Re: [RFC PATCH] kernel/panic: Filter out a potential trailing newline
  2018-10-09 20:50 [RFC PATCH] kernel/panic: Filter out a potential trailing newline Borislav Petkov
  2018-10-09 21:13 ` Kees Cook
@ 2018-10-09 21:23 ` Steven Rostedt
  2018-10-10  6:00 ` Ingo Molnar
  2 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2018-10-09 21:23 UTC (permalink / raw)
  To: Borislav Petkov; +Cc: LKML, Andrew Morton, Kees Cook, x86

On Tue,  9 Oct 2018 22:50:19 +0200
Borislav Petkov <bp@alien8.de> wrote:

> From: Borislav Petkov <bp@suse.de>
> 
> If a call to panic() terminates the string with a \n, the result puts
> the closing brace ']---' on a newline because panic() itself adds \n
> too.
> 
> Now, if one goes and removes the newline chars from all panic()
> invocations - and the stats right now look like this:
> 
> ~300 calls with an \n
> ~500 calls without a \n
> 
> one is destined to a neverending game of whack-a-mole because the usual
> thing to do is add a newline at the end of a string a function is
> supposed to print.
> 
> Therefore, simply zap any \n at the end of the panic string to avoid
> touching so many places in the kernel.
> 
> Signed-off-by: Borislav Petkov <bp@suse.de>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
> Cc: x86@kernel.org
> ---
>  kernel/panic.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/panic.c b/kernel/panic.c
> index 8b2e002d52eb..5776d2879650 100644
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -136,7 +136,7 @@ void panic(const char *fmt, ...)
>  {
>  	static char buf[1024];
>  	va_list args;
> -	long i, i_next = 0;
> +	long i, i_next = 0, len;
>  	int state = 0;
>  	int old_cpu, this_cpu;
>  	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
> @@ -173,8 +173,12 @@ void panic(const char *fmt, ...)
>  	console_verbose();
>  	bust_spinlocks(1);
>  	va_start(args, fmt);
> -	vsnprintf(buf, sizeof(buf), fmt, args);
> +	len = vscnprintf(buf, sizeof(buf), fmt, args);
>  	va_end(args);
> +
> +	if (len && buf[len - 1] == '\n')
> +		buf[len - 1] = '\0';
> +
>  	pr_emerg("Kernel panic - not syncing: %s\n", buf);
>  #ifdef CONFIG_DEBUG_BUGVERBOSE
>  	/*

Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>

-- Steve

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

* Re: [RFC PATCH] kernel/panic: Filter out a potential trailing newline
  2018-10-09 20:50 [RFC PATCH] kernel/panic: Filter out a potential trailing newline Borislav Petkov
  2018-10-09 21:13 ` Kees Cook
  2018-10-09 21:23 ` Steven Rostedt
@ 2018-10-10  6:00 ` Ingo Molnar
  2 siblings, 0 replies; 4+ messages in thread
From: Ingo Molnar @ 2018-10-10  6:00 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: LKML, Andrew Morton, Kees Cook, Steven Rostedt (VMware), x86


* Borislav Petkov <bp@alien8.de> wrote:

> From: Borislav Petkov <bp@suse.de>
> 
> If a call to panic() terminates the string with a \n, the result puts
> the closing brace ']---' on a newline because panic() itself adds \n
> too.
> 
> Now, if one goes and removes the newline chars from all panic()
> invocations - and the stats right now look like this:
> 
> ~300 calls with an \n
> ~500 calls without a \n
> 
> one is destined to a neverending game of whack-a-mole because the usual
> thing to do is add a newline at the end of a string a function is
> supposed to print.
> 
> Therefore, simply zap any \n at the end of the panic string to avoid
> touching so many places in the kernel.
> 
> Signed-off-by: Borislav Petkov <bp@suse.de>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
> Cc: x86@kernel.org
> ---
>  kernel/panic.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/panic.c b/kernel/panic.c
> index 8b2e002d52eb..5776d2879650 100644
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -136,7 +136,7 @@ void panic(const char *fmt, ...)
>  {
>  	static char buf[1024];
>  	va_list args;
> -	long i, i_next = 0;
> +	long i, i_next = 0, len;
>  	int state = 0;
>  	int old_cpu, this_cpu;
>  	bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;
> @@ -173,8 +173,12 @@ void panic(const char *fmt, ...)
>  	console_verbose();
>  	bust_spinlocks(1);
>  	va_start(args, fmt);
> -	vsnprintf(buf, sizeof(buf), fmt, args);
> +	len = vscnprintf(buf, sizeof(buf), fmt, args);
>  	va_end(args);
> +
> +	if (len && buf[len - 1] == '\n')
> +		buf[len - 1] = '\0';

Nice, this patch saves a couple of hundred trivial upstream commits in the next 20 years:

Acked-by: Ingo Molnar <mingo@kernel.org>

Thanks,

	Ingo

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

end of thread, other threads:[~2018-10-10  6:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-09 20:50 [RFC PATCH] kernel/panic: Filter out a potential trailing newline Borislav Petkov
2018-10-09 21:13 ` Kees Cook
2018-10-09 21:23 ` Steven Rostedt
2018-10-10  6:00 ` Ingo Molnar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.