linux/kernel/locking
Thomas Gleixner 89038cc87d locking/rt: Fix the incorrect RCU protection in rt_spin_unlock()
rt_spin_unlock() releases the RCU protection before unlocking the
lock. That opens the door for the following UAF scenario:

 T1					T2
 spin_lock(&p->lock);		rcu_read_lock();
 invalidate(p);			p = rcu_dereference(ptr);
 rcu_assign_pointer(ptr, NULL);	if (!p) return;
 spin_unlock(&p->lock);		spin_lock(&p->lock)
 				   lock(&lock->lock);
				   rcu_read_lock();
 kfree_rcu(p);			rcu_read_unlock();
				....
				spin_unlock(&p->lock)
				  rcu_read_unlock(); // Ends grace period
 rcu_do_batch()
   kfree(p);
			    UAF ->	  rt_mutex_cmpxchg_release(&lock->lock...)

Regular spinlocks keep preemption disabled accross the unlock operation,
which provides full RCU protection, but the RT substitution fails to
resemble that. Same applies for the rwlock substitution.

Move the rcu_read_unlock() invocation past the unlock operations to match
the non-RT semantics. This makes it asymmetric vs. rt_xxx_lock(), but
that's harmless as the caller needs to hold RCU read lock across the lock
operation. The migrate_enable() call stays before the unlock operation
because there is no per CPU operation in the unlock path which would
require migration to be kept disabled.

Fixes: 0f383b6dc9 ("locking/spinlock: Provide RT variant")
Reported-by: syzbot+000c800a02097aaa10ed@syzkaller.appspotmail.com
Decoded-by: Jann Horn <jannh@google.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/87jyrud75z.ffs@fw13
2026-06-21 11:51:13 +02:00
..
irqflag-debug.c lockdep: Noinstr annotate warn_bogus_irq_restore() 2021-02-10 14:44:39 +01:00
lock_events_list.h bpf_res_spin_lock 2025-03-30 13:06:27 -07:00
lock_events.c locking/debug: Fix debugfs API return value checks to use IS_ERR() 2023-10-03 10:11:25 +02:00
lock_events.h locking/qspinlock: Always evaluate lockevent* non-event parameter once 2024-03-21 20:45:17 +01:00
lockdep_internals.h locking/lockdep: Change 'static const' variables to enum values 2025-07-14 21:57:29 -07:00
lockdep_proc.c locking/lockdep: Avoid struct return in lock_stats() 2025-07-14 21:57:20 -07:00
lockdep_states.h locking/lockdep: Rework FS_RECLAIM annotation 2017-08-10 12:29:03 +02:00
lockdep.c lockdep: Speed up lockdep_unregister_key() with expedited RCU synchronization 2025-07-14 21:57:29 -07:00
locktorture.c Convert remaining multi-line kmalloc_obj/flex GFP_KERNEL uses 2026-02-22 08:26:33 -08:00
Makefile locking/rwsem: Add context analysis 2026-03-08 11:06:53 +01:00
mcs_spinlock.h locking: Allow obtaining result of arch_mcs_spin_lock_contended 2025-03-19 08:03:04 -07:00
mutex-debug.c Scheduler changes for v7.1: 2026-04-14 13:33:36 -07:00
mutex.c Scheduler updates for v7.2: 2026-06-15 14:50:18 +05:30
mutex.h Scheduler changes for v7.1: 2026-04-14 13:33:36 -07:00
osq_lock.c locking/osq_lock: Use atomic_try_cmpxchg_release() in osq_unlock() 2024-10-25 10:01:50 +02:00
percpu-rwsem.c locking: Add contended_release tracepoint to sleepable locks 2026-06-11 13:41:25 +02:00
qrwlock.c locking: Add __lockfunc to slow path functions 2022-08-19 19:47:51 +02:00
qspinlock_paravirt.h locking/pvqspinlock: Convert fields of 'enum vcpu_state' to uppercase 2024-10-17 21:21:16 -07:00
qspinlock_stat.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157 2019-05-30 11:26:37 -07:00
qspinlock.c locking: Move common qspinlock helpers to a private header 2025-03-19 08:02:29 -07:00
qspinlock.h locking: Move common qspinlock helpers to a private header 2025-03-19 08:02:29 -07:00
rtmutex_api.c Locking updates for v7.2: 2026-06-15 14:21:14 +05:30
rtmutex_common.h locking/rtmutex: Add context analysis 2026-03-08 11:06:53 +01:00
rtmutex.c Locking updates for v7.2: 2026-06-15 14:21:14 +05:30
rwbase_rt.c locking: Add contended_release tracepoint to sleepable locks 2026-06-11 13:41:25 +02:00
rwsem.c locking: Add contended_release tracepoint to sleepable locks 2026-06-11 13:41:25 +02:00
semaphore.c locking: Add contended_release tracepoint to sleepable locks 2026-06-11 13:41:25 +02:00
spinlock_debug.c locking/spinlock/debug: Fix data-race in do_raw_write_lock 2025-10-21 12:31:55 +02:00
spinlock_rt.c locking/rt: Fix the incorrect RCU protection in rt_spin_unlock() 2026-06-21 11:51:13 +02:00
spinlock.c locking: Add lock context annotations in the spinlock implementation 2026-03-16 13:16:50 +01:00
test-ww_mutex.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
ww_mutex.h sched/proxy: Remove PROXY_WAKING 2026-06-02 12:26:09 +02:00
ww_rt_mutex.c locking/rtmutex: Add context analysis 2026-03-08 11:06:53 +01:00