Skip to contentSkip to conversation

panic: fix va_list reuse in panic_try_force_cpu()

PATCH In review Bradley Morgan 3 participants 4 messages Jul 5 – Jul 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 panic: fix va_list reuse in panic_try_force_cpu()

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().

---
 kernel/panic.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/panic.c +6 -1
index f9ad0250da67..03f1eef07b17 100644
@@ -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)";
Signature · 1 line
2.53.0
#1 lore raw
Quoted 3 lines · On Sun, 5 Jul 2026 16:41:23 +0000 Bradley Morgan <[email protected]> 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.

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 417

Quoted 10 lines
kernel/panic.c +3 -1
@@ -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!

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 423

Quoted 6 lines
kernel/panic.c +3 -0
+		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/[email protected]
#2 lore raw
Quoted 31 lines · Andrew Morton
Quoted 3 lines · On Sun, 5 Jul 2026 16:41:23 +0000 Bradley Morgan <[email protected]> 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.

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 417

Quoted 10 lines
kernel/panic.c +3 -1
@@ -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!

kernel/panic.c static bool panic_try_force_cpu(const char *fmt, va_list args) · line 423

Quoted 6 lines
kernel/panic.c +3 -0
+		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:

Quoted 1 line
	https://sashiko.dev/#/patchset/[email protected]

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