mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
To: Andrew Morton <akpm@linux-foundation.org>,
	 Petr Mladek <pmladek@suse.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	 Sergey Senozhatsky <senozhatsky@chromium.org>,
	 Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,  Will Deacon <will@kernel.org>,
	Boqun Feng <boqun@kernel.org>,  Waiman Long <longman@redhat.com>,
	 Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	 Clark Williams <clrkwllms@kernel.org>,
	Kees Cook <kees@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
Subject: [PATCH v4 5/5] lib/vsprintf: Validate spinlock context during restricted pointer formatting
Date: Mon, 08 Jun 2026 16:21:45 +0200	[thread overview]
Message-ID: <20260608-restricted-pointers-final-v4-5-9a436615799e@linutronix.de> (raw)
In-Reply-To: <20260608-restricted-pointers-final-v4-0-9a436615799e@linutronix.de>

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.
This is intentionally a single lock instance shared by all callers,
as that mirrors what can happen in the security subsystem.

This check comes intentionally after the in_task() one, to have the
clearer diagnostic first when the function is called from IRQ context,
which will trigger both.

Link: https://lore.kernel.org/lkml/20250113171731-dc10e3c1-da64-4af0-b767-7c7070468023@linutronix.de/
Link: https://lore.kernel.org/lkml/20241217142032.55793-1-acarmina@redhat.com/
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
 lib/vsprintf.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 09e0e5194d41..728a1acd69ae 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -864,7 +864,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);
+
 	lockdep_assert(in_task());
+	guard(lock_map_acquire)(&vsprintf_restricted_pointer_map);
 
 	switch (kptr_restrict) {
 	case 0:

-- 
2.54.0


  parent reply	other threads:[~2026-06-08 14:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-08 14:21 [PATCH v4 0/5] " Thomas Weißschuh
2026-06-08 14:21 ` [PATCH v4 1/5] locking/lockdep: Add a helper to validate the locking context without a lock Thomas Weißschuh
2026-06-08 14:21 ` [PATCH v4 2/5] locking/lockdep: Add a guard for lock_map_acquire() Thomas Weißschuh
2026-06-08 14:21 ` [PATCH v4 3/5] lib/vsprintf: Use in_task() for restricted pointer context check Thomas Weißschuh
2026-06-08 14:21 ` [PATCH v4 4/5] lib/vsprintf: Always check interrupt context restrictions Thomas Weißschuh
2026-06-08 14:21 ` Thomas Weißschuh [this message]
2026-06-11 10:09   ` [PATCH v4 5/5] lib/vsprintf: Validate spinlock context during restricted pointer formatting Thomas Weißschuh
2026-06-16  9:14     ` Petr Mladek
2026-06-25 14:51     ` Sebastian Andrzej Siewior
2026-06-26 15:53       ` Petr Mladek
2026-06-26 16:06         ` Petr Mladek
2026-06-26 17:06           ` Sebastian Andrzej Siewior
2026-06-29 12:06             ` Petr Mladek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260608-restricted-pointers-final-v4-5-9a436615799e@linutronix.de \
    --to=thomas.weissschuh@linutronix.de \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bigeasy@linutronix.de \
    --cc=boqun@kernel.org \
    --cc=clrkwllms@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=linux@rasmusvillemoes.dk \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome