[PATCH v3 3/5] lib/vsprintf: Validate spinlock context during restricted pointer formatting
From: Thomas Weißschuh
Date: Wed May 20 2026 - 05:20:36 EST
Depending on the system configuration, the restricted pointer formatting
might call into the security subsystem which takes spinlocks, which
might sleep under PREEMPT_RT. As %pK is intended to be only used from
read handlers of virtual files, which always run in task context,
this should not be a problem in practice.
However, developers have used %pK before from atomic context without
realizing this restriction. While all existing user of %pK through
printk() have been removed, new ones might be reintroduced accidentally
in the future.
Add a lockdep annotation to unconditionally introduce a fake spinlock in
restricted_pointer(), so lockdep can detect misuse even if the current
test system configuration would not exhibit the issue.
Link: https://lore.kernel.org/lkml/20250113171731-dc10e3c1-da64-4af0-b767-7c7070468023@xxxxxxxxxxxxx/
Link: https://lore.kernel.org/lkml/20241217142032.55793-1-acarmina@xxxxxxxxxx/
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@xxxxxxxxxxxxx>
---
lib/vsprintf.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 9f359b31c8d1..021db95087fe 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -29,6 +29,7 @@
#include <linux/hex.h>
#include <linux/kernel.h>
#include <linux/kallsyms.h>
+#include <linux/lockdep.h>
#include <linux/math64.h>
#include <linux/uaccess.h>
#include <linux/ioport.h>
@@ -862,6 +863,14 @@ static noinline_for_stack
char *restricted_pointer(char *buf, char *end, const void *ptr,
struct printf_spec spec)
{
+ /*
+ * has_capability_noaudit() may use spinlocks.
+ * Make sure %pK is only used from valid contexts.
+ */
+ static DEFINE_WAIT_ASSERT_MAP(vsprintf_restricted_pointer_map, LD_WAIT_CONFIG);
+
+ guard(lock_map_acquire)(&vsprintf_restricted_pointer_map);
+
switch (kptr_restrict) {
case 0:
/* Handle as %p, hash and do _not_ leak addresses. */
--
2.54.0