* [PATCH] panic: fix va_list reuse in panic_try_force_cpu()
@ 2026-07-05 16:41 Bradley Morgan
2026-07-05 19:10 ` Andrew Morton
2026-07-07 9:18 ` Petr Mladek
0 siblings, 2 replies; 4+ messages in thread
From: Bradley Morgan @ 2026-07-05 16:41 UTC (permalink / raw)
To: Andrew Morton, Feng Tang
Cc: Petr Mladek, Jinchao Wang, linux-kernel, Bradley Morgan
vsnprintf() consumes the caller's va_list. When the redirect fails,
vpanic() reuses it for the panic message, which is undefined
behavior. Use va_copy().
Signed-off-by: Bradley Morgan <include@grrlz.net>
---
kernel/panic.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/kernel/panic.c b/kernel/panic.c
index f9ad0250da67..03f1eef07b17 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -412,7 +412,12 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
* fall back to static message for early boot panics or allocation failure.
*/
if (panic_force_buf) {
- vsnprintf(panic_force_buf, PANIC_MSG_BUFSZ, fmt, args);
+ va_list ap;
+
+ /* Do not consume args, the caller reuses it if we fail */
+ va_copy(ap, args);
+ vsnprintf(panic_force_buf, PANIC_MSG_BUFSZ, fmt, ap);
+ va_end(ap);
msg = panic_force_buf;
} else {
msg = "Redirected panic (buffer unavailable)";
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] panic: fix va_list reuse in panic_try_force_cpu()
2026-07-05 16:41 [PATCH] panic: fix va_list reuse in panic_try_force_cpu() Bradley Morgan
@ 2026-07-05 19:10 ` Andrew Morton
2026-07-07 9:28 ` Petr Mladek
2026-07-07 9:18 ` Petr Mladek
1 sibling, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2026-07-05 19:10 UTC (permalink / raw)
To: Bradley Morgan; +Cc: Feng Tang, Petr Mladek, Jinchao Wang, linux-kernel
On Sun, 5 Jul 2026 16:41:23 +0000 Bradley Morgan <include@grrlz.net> wrote:
> vsnprintf() consumes the caller's va_list. When the redirect fails,
> vpanic() reuses it for the panic message, which is undefined
> behavior. Use va_copy().
Thanks.
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -412,7 +412,12 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
> * fall back to static message for early boot panics or allocation failure.
> */
> if (panic_force_buf) {
> - vsnprintf(panic_force_buf, PANIC_MSG_BUFSZ, fmt, args);
> + va_list ap;
> +
> + /* Do not consume args, the caller reuses it if we fail */
Nice comment!
> + va_copy(ap, args);
> + vsnprintf(panic_force_buf, PANIC_MSG_BUFSZ, fmt, ap);
> + va_end(ap);
> msg = panic_force_buf;
> } else {
> msg = "Redirected panic (buffer unavailable)";
AI review found a possible pre-existing thing in there (as usual,
sigh). Seems pretty improbable:
https://sashiko.dev/#/patchset/20260705164123.18746-1-include@grrlz.net
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] panic: fix va_list reuse in panic_try_force_cpu()
2026-07-05 19:10 ` Andrew Morton
@ 2026-07-07 9:28 ` Petr Mladek
0 siblings, 0 replies; 4+ messages in thread
From: Petr Mladek @ 2026-07-07 9:28 UTC (permalink / raw)
To: Andrew Morton; +Cc: Bradley Morgan, Feng Tang, Jinchao Wang, linux-kernel
On Sun 2026-07-05 12:10:14, Andrew Morton wrote:
> On Sun, 5 Jul 2026 16:41:23 +0000 Bradley Morgan <include@grrlz.net> wrote:
>
> > vsnprintf() consumes the caller's va_list. When the redirect fails,
> > vpanic() reuses it for the panic message, which is undefined
> > behavior. Use va_copy().
>
> Thanks.
>
> > --- a/kernel/panic.c
> > +++ b/kernel/panic.c
> > @@ -412,7 +412,12 @@ static bool panic_try_force_cpu(const char *fmt, va_list args)
> > * fall back to static message for early boot panics or allocation failure.
> > */
> > if (panic_force_buf) {
> > - vsnprintf(panic_force_buf, PANIC_MSG_BUFSZ, fmt, args);
> > + va_list ap;
> > +
> > + /* Do not consume args, the caller reuses it if we fail */
>
> Nice comment!
>
> > + va_copy(ap, args);
> > + vsnprintf(panic_force_buf, PANIC_MSG_BUFSZ, fmt, ap);
> > + va_end(ap);
> > msg = panic_force_buf;
> > } else {
> > msg = "Redirected panic (buffer unavailable)";
>
> AI review found a possible pre-existing thing in there (as usual,
> sigh). Seems pretty improbable:
>
> https://sashiko.dev/#/patchset/20260705164123.18746-1-include@grrlz.net
Sashiko's comment is:
<paste>
This is a pre-existing issue, but looking at panic_try_force_cpu() in
kernel/panic.c, could concurrent panics bypass the panic_force_cpu
redirection?
If multiple CPUs enter panic() concurrently:
CPU A successfully sets panic_redirect_cpu:
if (!atomic_try_cmpxchg(&panic_redirect_cpu, &old_cpu, this_cpu))
and eventually returns true to stop itself.
CPU B fails the cmpxchg on panic_redirect_cpu and returns false. Because
the target CPU (CPU C) hasn't yet started executing panic and hasn't set
panic_cpu, CPU B falls through to panic_try_start(). CPU B successfully
sets panic_cpu and handles the panic itself.
When CPU C eventually receives the IPI, it stops itself because panic_cpu
is already claimed.
Could this result in the crash kernel executing on a CPU other than the
one specified by panic_force_cpu?
</paste>
It has a point. panic_try_force_cpu() should return true when it can't
set panic_redirect_cpu. The panic() will be finished either by
the requested CPU or by the CPU which was able to set
panic_redirect_cpu.
I could send a patch. Or Bradley, would you like to send it?
Best Regards,
Petr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] panic: fix va_list reuse in panic_try_force_cpu()
2026-07-05 16:41 [PATCH] panic: fix va_list reuse in panic_try_force_cpu() Bradley Morgan
2026-07-05 19:10 ` Andrew Morton
@ 2026-07-07 9:18 ` Petr Mladek
1 sibling, 0 replies; 4+ messages in thread
From: Petr Mladek @ 2026-07-07 9:18 UTC (permalink / raw)
To: Bradley Morgan; +Cc: Andrew Morton, Feng Tang, Jinchao Wang, linux-kernel
On Sun 2026-07-05 16:41:23, Bradley Morgan wrote:
> vsnprintf() consumes the caller's va_list. When the redirect fails,
> vpanic() reuses it for the panic message, which is undefined
> behavior. Use va_copy().
>
> Signed-off-by: Bradley Morgan <include@grrlz.net>
Great catch!
The patch looks good to me:
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-07 9:28 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-05 16:41 [PATCH] panic: fix va_list reuse in panic_try_force_cpu() Bradley Morgan
2026-07-05 19:10 ` Andrew Morton
2026-07-07 9:28 ` Petr Mladek
2026-07-07 9:18 ` Petr Mladek
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.