Skip to contentSkip to conversation

x86/bug: Add printf() validation to x86's custom WARNs

PATCH Applied Sean Christopherson 3 participants 9 messages Apr 23 – Apr 27 x86

lore
Share & tools
lore
#0 lore raw

v2 cover x86/bug: Add printf() validation to x86's custom WARNs

In x86's custom HAVE_ARCH_BUG_FORMAT_ARGS WARNs, invoke a dummy function with __printf() annotation to validate the formatting+arguments of any provided messages.

Yan reported a bug where I botched the formatting of a WARN_ONCE() argument, but none of my builds (with W=1 and -Werror) detected the issue, nor did any of the build bots (AFAIK). Turns out that Yan found it via CONFIG_BUG=n, which due to the code being 64-bit-only, was the only way to detect the malformed message.

v2:
- Drop the dummy macro for assembly code. [Yan]
- Extend the #ifdeffery to hide the WARN macros themselves from assembly code. [Yan]

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (2):
x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs x86/bug: Put HAVE_ARCH_BUG_FORMAT_ARGS WARN definitions inside

    __ASSEMBLER__
 arch/x86/include/asm/bug.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

base-commit: 59287e6ad4a9e5d13519b783d6bbc1015b94d63e

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#1 lore raw

PATCH v2 1/2 x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

Add explicit printf() validation for x86-64's newfangled WARN implementation, as most (all?) compilers fail to detect basic formatting issues without the annotation. E.g. even goofs like printing a u64 as a string aren't detected:

  WARN_ONCE(1, "Bad message, %s", vcpu->arch.last_guest_tsc);

Lack of validation is especially problematic for code that is 64-bit-only, as blatant goofs can easily go unnoticed, as they (somewhat ironically) will only be noticed by CONFIG_BUG=n builds.

Cc Yan Zhao <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Fixes 5b472b6e5bd9 ("x86_64/bug: Implement __WARN_printf()")
Fixes 11bb4944f014 ("x86/bug: Implement WARN_ONCE()")
---
 arch/x86/include/asm/bug.h | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/include/asm/bug.h +2 -0
index 80c1696d8d59..bf3c802654d1 100644
@@ -153,6 +153,7 @@ struct arch_va_list {
 	struct sysv_va_list args;
 };
 extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
+static __always_inline __printf(1, 2) void __WARN_validate_printf(const char *fmt, ...) { }
 #endif /* __ASSEMBLER__ */
 
 #define __WARN_bug_entry(flags, format) ({				\
@@ -172,6 +173,7 @@ extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
 #define __WARN_print_arg(flags, format, arg...)				\
 do {									\
 	int __flags = (flags) | BUGFLAG_WARNING | BUGFLAG_ARGS ;	\
+	__WARN_validate_printf(format, ## arg);				\
 	static_call_mod(WARN_trap)(__WARN_bug_entry(__flags, format), ## arg); \
 	asm (""); /* inhibit tail-call optimization */			\
 } while (0)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#2 lore raw

Re: [PATCH v2 1/2] x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

Quoted 3 lines · Sean Christopherson

Lack of validation is especially problematic for code that is 64-bit-only, as blatant goofs can easily go unnoticed, as they (somewhat ironically) will only be noticed by CONFIG_BUG=n builds.

This took me a minute to piece together.

CONFIG_BUG=n builds use the asm-generic/bug.h implementations which have:

	no_printk(format);

and do their own printk validation. Right?

Also, what do you mean about 64-bit-only code?

I'm also debating if we should stick these in x86/urgent and get them to Linus sooner rather than later so folks aren't bitten by this for a whole development cycle.

#3 lore raw

Re: [PATCH v2 1/2] x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

Quoted 5 lines · Dave Hansen
Quoted 3 lines · Sean Christopherson

Lack of validation is especially problematic for code that is 64-bit-only, as blatant goofs can easily go unnoticed, as they (somewhat ironically) will only be noticed by CONFIG_BUG=n builds.

Quoted 7 lines

This took me a minute to piece together.

CONFIG_BUG=n builds use the asm-generic/bug.h implementations which have:

	no_printk(format);

and do their own printk validation. Right?

Ya.

Quoted 1 line

Also, what do you mean about 64-bit-only code?

32-bit x86 doesn't support HAVE_ARCH_BUG_FORMAT_ARGS, and so it too uses generic implementations that provide printk validation. I.e. the blind spot is code that is strictly x86-64, because code that builds on other architectures and on 32-bit x86 will be detected by those other builds, and unlike CONFIG_BUG=n, people and bots regularly test those configurations.

Quoted 3 lines

I'm also debating if we should stick these in x86/urgent and get them to Linus sooner rather than later so folks aren't bitten by this for a whole development cycle.

Acked-by
#4 lore raw

Re: [PATCH v2 1/2] x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

Quoted 6 lines · Sean Christopherson
Quoted 1 line

Also, what do you mean about 64-bit-only code?

32-bit x86 doesn't support HAVE_ARCH_BUG_FORMAT_ARGS, and so it too uses generic implementations that provide printk validation. I.e. the blind spot is code that is strictly x86-64, because code that builds on other architectures and on 32-bit x86 will be detected by those other builds, and unlike CONFIG_BUG=n, people and bots regularly test those configurations.

Ahhh, thanks for the explanation. In any case:

I'll plan to suck these in after -rc1 if someone else doesn't beat me to it.

#5 lore raw

[tip: x86/misc] x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

In reply to Sean Christopherson
The following commit has been merged into the x86/misc branch of tip:

The following commit has been merged into the x86/misc branch of tip:

Commit-ID:     40c4b47f41b95dff743c841536cb64014e65ef0c
Gitweb:        https://git.kernel.org/tip/40c4b47f41b95dff743c841536cb64014e65ef0c
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Thu, 23 Apr 2026 07:54:17 -07:00
Committer:     Dave Hansen <[email protected]>
CommitterDate: Mon, 27 Apr 2026 12:02:40 -07:00

x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

Add explicit printf() validation for x86-64's newfangled WARN implementation, as most (all?) compilers fail to detect basic formatting issues without the annotation. E.g. even goofs like printing a u64 as a string aren't detected:

  WARN_ONCE(1, "Bad message, %s", vcpu->arch.last_guest_tsc);

32-bit x86 doesn't support HAVE_ARCH_BUG_FORMAT_ARGS and uses generic implementations that provide printf() validation. This means there's now a big blind spot is code that is strictly x86-64. Inconveniently, new features are also frequently x86-64-only.

Fix the blind 64-bit blind spot.

[ dhansen: changelog tweaks to flesh out the 64-bit-only details ]

Fixes 5b472b6e5bd9 ("x86_64/bug: Implement __WARN_printf()")
Fixes 11bb4944f014 ("x86/bug: Implement WARN_ONCE()")
---
 arch/x86/include/asm/bug.h | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/include/asm/bug.h +2 -0
index 80c1696..bf3c802 100644
@@ -153,6 +153,7 @@ struct arch_va_list {
 	struct sysv_va_list args;
 };
 extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
+static __always_inline __printf(1, 2) void __WARN_validate_printf(const char *fmt, ...) { }
 #endif /* __ASSEMBLER__ */
 
 #define __WARN_bug_entry(flags, format) ({				\
@@ -172,6 +173,7 @@ extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
 #define __WARN_print_arg(flags, format, arg...)				\
 do {									\
 	int __flags = (flags) | BUGFLAG_WARNING | BUGFLAG_ARGS ;	\
+	__WARN_validate_printf(format, ## arg);				\
 	static_call_mod(WARN_trap)(__WARN_bug_entry(__flags, format), ## arg); \
 	asm (""); /* inhibit tail-call optimization */			\
 } while (0)
#6 lore raw

Re: [tip: x86/misc] x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

Quoted 18 lines · On Mon, Apr 27, 2026, tip-bot2 for Sean Christopherson wrote:

The following commit has been merged into the x86/misc branch of tip:

Commit-ID:     40c4b47f41b95dff743c841536cb64014e65ef0c
Gitweb:        https://git.kernel.org/tip/40c4b47f41b95dff743c841536cb64014e65ef0c
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Thu, 23 Apr 2026 07:54:17 -07:00
Committer:     Dave Hansen <[email protected]>
CommitterDate: Mon, 27 Apr 2026 12:02:40 -07:00

x86/bug: Add printf() validation to HAVE_ARCH_BUG_FORMAT_ARGS WARNs

Add explicit printf() validation for x86-64's newfangled WARN implementation, as most (all?) compilers fail to detect basic formatting issues without the annotation. E.g. even goofs like printing a u64 as a string aren't detected:

  WARN_ONCE(1, "Bad message, %s", vcpu->arch.last_guest_tsc);
Quoted 8 lines

32-bit x86 doesn't support HAVE_ARCH_BUG_FORMAT_ARGS and uses generic implementations that provide printf() validation. This means there's now a big blind spot is code that is strictly x86-64. Inconveniently, new features are also frequently x86-64-only.

Fix the blind 64-bit blind spot.

[ dhansen: changelog tweaks to flesh out the 64-bit-only details ]

Much better than what I wrote, thanks Dave!

#7 lore raw

PATCH v2 2/2 x86/bug: Put HAVE_ARCH_BUG_FORMAT_ARGS WARN definitions inside __ASSEMBLER__

In reply to Sean Christopherson

Extend the !assembly #ifdef guarding x86's custom WARN helpers to cover the WARN macros themselves, as they aren't assembly friendly. This helps make it clear that things like __WARN_validate_printf() don't need a dummy definition for assembly code.

No functional change intended.

---
 arch/x86/include/asm/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bug.h +1 -1
index bf3c802654d1..23ab05438269 100644
@@ -154,7 +154,6 @@ struct arch_va_list {
 };
 extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
 static __always_inline __printf(1, 2) void __WARN_validate_printf(const char *fmt, ...) { }
-#endif /* __ASSEMBLER__ */
 
 #define __WARN_bug_entry(flags, format) ({				\
 	struct bug_entry *bug;						\
@@ -189,6 +188,7 @@ do {									\
 	}								\
 	__ret_warn_on;							\
 })
+#endif /* __ASSEMBLER__ */
 
 #endif /* HAVE_ARCH_BUG_FORMAT_ARGS */
 
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#8 lore raw

[tip: x86/misc] x86/bug: Put HAVE_ARCH_BUG_FORMAT_ARGS WARN definitions inside __ASSEMBLER__

The following commit has been merged into the x86/misc branch of tip:

The following commit has been merged into the x86/misc branch of tip:

Commit-ID:     23aea3c539a62ab97ca3aecf41d590d91f2911fc
Gitweb:        https://git.kernel.org/tip/23aea3c539a62ab97ca3aecf41d590d91f2911fc
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Thu, 23 Apr 2026 07:54:18 -07:00
Committer:     Dave Hansen <[email protected]>
CommitterDate: Mon, 27 Apr 2026 12:02:57 -07:00

x86/bug: Put HAVE_ARCH_BUG_FORMAT_ARGS WARN definitions inside __ASSEMBLER__

Extend the !assembly #ifdef guarding x86's custom WARN helpers to cover the WARN macros themselves, as they aren't assembly friendly. This helps make it clear that things like __WARN_validate_printf() don't need a dummy definition for assembly code.

No functional change intended.

---
 arch/x86/include/asm/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bug.h +1 -1
index bf3c802..23ab054 100644
@@ -154,7 +154,6 @@ struct arch_va_list {
 };
 extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
 static __always_inline __printf(1, 2) void __WARN_validate_printf(const char *fmt, ...) { }
-#endif /* __ASSEMBLER__ */
 
 #define __WARN_bug_entry(flags, format) ({				\
 	struct bug_entry *bug;						\
@@ -189,6 +188,7 @@ do {									\
 	}								\
 	__ret_warn_on;							\
 })
+#endif /* __ASSEMBLER__ */
 
 #endif /* HAVE_ARCH_BUG_FORMAT_ARGS */