lib/vsprintf: Validate spinlock context during restricted pointer formatting
Subsystems (4)
v2 cover lib/vsprintf: Validate spinlock context during restricted pointer formatting
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.
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.
---
Changes in v2:
- Use custom lock_map over might_sleep()
- Also assert IRQ context
- Link to v1: https://lore.kernel.org/r/20260317-restricted-pointers-final-v1-1-b4dca0ed6483@linutronix.de
To: Andrew Morton <[email protected]>
To: Petr Mladek <[email protected]>
To: Steven Rostedt <[email protected]>
To: Andy Shevchenko <[email protected]>
To: Rasmus Villemoes <[email protected]>
To: Sergey Senozhatsky <[email protected]>
To: Peter Zijlstra <[email protected]>
To: Ingo Molnar <[email protected]>
To: Will Deacon <[email protected]>
To: Boqun Feng <[email protected]>
To: Waiman Long <[email protected]>
To: Sebastian Andrzej Siewior <[email protected]>
To: Clark Williams <[email protected]>
To: Kees Cook <[email protected]>
---
Thomas Weißschuh (4):
locking/lockdep: Add a helper to validate the locking context without a lock locking/lockdep: Add a guard for lock_map_acquire() lib/vsprintf: Validate spinlock context during restricted pointer formatting lib/vsprintf: Always check interrupt context restrictions
include/linux/lockdep.h | 11 +++++++++++ lib/vsprintf.c | 13 +++++++++++++ 2 files changed, 24 insertions(+)
---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260107-restricted-pointers-final-cd24979fd752
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
PATCH v2 1/4 locking/lockdep: Add a helper to validate the locking context without a lock
In some cases the specific codepath and its locking operations depend on the runtime configuration of the system. lockdep will only detect lock misuse if the system is configured in the right way by chance.
To make lockdep more reliable in these cases, introduce a helper macro to define a lockdep map without any corresponding lock.
This differs from the related DEFINE_WAIT_OVERRIDE_MAP() as the context of the map is checked against the current locking context.
--- include/linux/lockdep.h | 8 ++++++++ 1 file changed, 8 insertions(+)
include/linux/lockdep.h +8 -0
@@ -315,6 +315,11 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie); .wait_type_inner = _wait_type, \ .lock_type = LD_LOCK_WAIT_OVERRIDE, } +#define DEFINE_WAIT_ASSERT_MAP (_name, _wait_type) \ + struct lockdep_map _name = { \ + .name = #_name "-wait-type-assert", \ + .wait_type_inner = _wait_type, } +#else /* !CONFIG_LOCKDEP */ static inline void lockdep_init_task (struct task_struct *task)
@@ -407,6 +412,9 @@ extern int lockdep_is_held(const void *);#define DEFINE_WAIT_OVERRIDE_MAP (_name, _wait_type) \ struct lockdep_map __maybe_unused _name = {} +#define DEFINE_WAIT_ASSERT_MAP (_name, _wait_type) \ + struct lockdep_map __maybe_unused _name = {} +#endif /* !LOCKDEP */ #ifdef CONFIG_PROVE_LOCKING
Signature · 1 line
2.53.0
PATCH v2 2/4 locking/lockdep: Add a guard for lock_map_acquire()
In reply to Thomas WeißschuhMake it easy to acquire a lock map based on source code structure.
--- include/linux/lockdep.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/lockdep.h +3 -0
@@ -10,6 +10,7 @@#ifndef __LINUX_LOCKDEP_H#define __LINUX_LOCKDEP_H +#include <linux/cleanup.h> #include <linux/lockdep_types.h> #include <linux/smp.h> #include <asm/percpu.h>
@@ -553,6 +554,8 @@ do { \#define lock_map_release (l) lock_release(l, _THIS_IP_)#define lock_map_sync (l) lock_sync(l, 0, 0, 1, NULL, _THIS_IP_) +DEFINE_GUARD(lock_map_acquire, struct lockdep_map *, lock_map_acquire(_T), lock_map_release(_T)) +#ifdef CONFIG_PROVE_LOCKING # define might_lock (lock) \ do { \
Signature · 1 line
2.53.0
PATCH v2 3/4 lib/vsprintf: Validate spinlock context during restricted pointer formatting
In reply to Thomas WeißschuhDepending 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.
--- lib/vsprintf.c | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/vsprintf.c +9 -0
@@ -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_stackchar *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. */
Signature · 1 line
2.53.0
PATCH v2 4/4 lib/vsprintf: Always check interrupt context restrictions
In reply to Thomas WeißschuhWhen kptr_restrict is set to '1' restricted pointers can not be used in IRQ context. As kptr_restrict can change at any time at runtime, this means that restricted pointers can not be used from IRQ context in general.
Add some assertions to detect misuse early, independently of the runtime configuration of the test system.
--- lib/vsprintf.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/vsprintf.c +4 -0
@@ -871,6 +871,10 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,guard (lock_map_acquire)(&vsprintf_restricted_pointer_map); +lockdep_assert (!in_hardirq ()); +lockdep_assert (!in_serving_softirq ()); +lockdep_assert (!in_nmi ()); +switch (kptr_restrict) {case 0 :/* Handle as %p, hash and do _not_ leak addresses. */
Signature · 1 line
2.53.0
Peter Zijlstra
maintainerRe: [PATCH v2 4/4] lib/vsprintf: Always check interrupt context restrictions
Quoted 18 lines · Thomas Weißschuh
When
kptr_restrictis set to '1' restricted pointers can not be used in IRQ context. Askptr_restrictcan change at any time at runtime, this means that restricted pointers can not be used from IRQ context in general.Add some assertions to detect misuse early, independently of the runtime configuration of the test system.
Signed-off-by Thomas Weißschuh <[email protected]>--- lib/vsprintf.c | 4 ++++ 1 file changed, 4 insertions(+)lib/vsprintf.c +0
-0
lib/vsprintf.c char *restricted_pointer(char *buf, char *end, const void *ptr, · line 877
Quoted 7 lines
lib/vsprintf.c +4
-0@@ -871,6 +871,10 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,guard (lock_map_acquire)(&vsprintf_restricted_pointer_map); +lockdep_assert (!in_hardirq ()); +lockdep_assert (!in_serving_softirq ()); +lockdep_assert (!in_nmi ()); +
did that want to be:
lockdep_assert(in_task()); ?
Sebastian Andrzej Siewior
maintainerRe: [PATCH v2 4/4] lib/vsprintf: Always check interrupt context restrictions
Quoted 13 lines · Peter Zijlstra
lib/vsprintf.c char *restricted_pointer(char *buf, char *end, const void *ptr, · line 877
Quoted 10 lines
lib/vsprintf.c +4
-0@@ -871,6 +871,10 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,guard (lock_map_acquire)(&vsprintf_restricted_pointer_map); +lockdep_assert (!in_hardirq ()); +lockdep_assert (!in_serving_softirq ()); +lockdep_assert (!in_nmi ()); +did that want to be:
Quoted 3 lines
lockdep_assert(in_task()); ?
I remember halfway suggesting that. But I also wanted to poke networking folks to get rid of this which I haven't done so far :/
Sebastian
Re: [PATCH v2 4/4] lib/vsprintf: Always check interrupt context restrictions
In reply to Peter ZijlstraQuoted 22 lines · Peter Zijlstra
Quoted 21 lines · Thomas Weißschuh
When
kptr_restrictis set to '1' restricted pointers can not be used in IRQ context. Askptr_restrictcan change at any time at runtime, this means that restricted pointers can not be used from IRQ context in general.Add some assertions to detect misuse early, independently of the runtime configuration of the test system.
Signed-off-by Thomas Weißschuh <[email protected]>--- lib/vsprintf.c | 4 ++++ 1 file changed, 4 insertions(+)lib/vsprintf.c +0
-0@@ -871,6 +871,10 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,guard (lock_map_acquire)(&vsprintf_restricted_pointer_map);
Quoted 8 lines
lib/vsprintf.c char *restricted_pointer(char *buf, char *end, const void *ptr, · line 877
Quoted 4 lines
lib/vsprintf.c +4
-0+lockdep_assert (!in_hardirq ()); +lockdep_assert (!in_serving_softirq ()); +lockdep_assert (!in_nmi ()); +did that want to be:
lockdep_assert(in_task());
Primarily it wants to be the inverse of the check further down the function:
if (in_hardirq() || in_serving_softirq() || in_nmi()) {
if (spec.field_width == -1)
spec.field_width = 2 * sizeof(ptr);
return error_string(buf, end, "pK-error", spec);But in_task() looks like the better choice indeed, thanks! I'll switch both locations to that for the next revision.
Unless Sebastian manages to get rid of it all, that is.
Thomas