Skip to content

Commit 7813430

Browse files
Waiman-LongIngo Molnar
authored andcommitted
locking/rwsem: Don't call owner_on_cpu() on read-owner
For writer, the owner value is cleared on unlock. For reader, it is left intact on unlock for providing better debugging aid on crash dump and the unlock of one reader may not mean the lock is free. As a result, the owner_on_cpu() shouldn't be used on read-owner as the task pointer value may not be valid and it might have been freed. That is the case in rwsem_spin_on_owner(), but not in rwsem_can_spin_on_owner(). This can lead to use-after-free error from KASAN. For example, BUG: KASAN: use-after-free in rwsem_down_write_slowpath (/home/miguel/kernel/linux/kernel/locking/rwsem.c:669 /home/miguel/kernel/linux/kernel/locking/rwsem.c:1125) Fix this by checking for RWSEM_READER_OWNED flag before calling owner_on_cpu(). Reported-by: Luis Henriques <[email protected]> Tested-by: Luis Henriques <[email protected]> Signed-off-by: Waiman Long <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: Jeff Layton <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Tim Chen <[email protected]> Cc: Will Deacon <[email protected]> Cc: huang ying <[email protected]> Fixes: 94a9717 ("locking/rwsem: Make rwsem->owner an atomic_long_t") Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent f9adc23 commit 7813430

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

kernel/locking/rwsem.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,11 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem,
666666
preempt_disable();
667667
rcu_read_lock();
668668
owner = rwsem_owner_flags(sem, &flags);
669-
if ((flags & nonspinnable) || (owner && !owner_on_cpu(owner)))
669+
/*
670+
* Don't check the read-owner as the entry may be stale.
671+
*/
672+
if ((flags & nonspinnable) ||
673+
(owner && !(flags & RWSEM_READER_OWNED) && !owner_on_cpu(owner)))
670674
ret = false;
671675
rcu_read_unlock();
672676
preempt_enable();

0 commit comments

Comments
 (0)