linux-stable/ipc
Jann Horn a1504a9e20 ipc/sem: Fix dangling sem_array access in semtimedop race
[ Upstream commit b52be557e2 ]

When __do_semtimedop() goes to sleep because it has to wait for a
semaphore value becoming zero or becoming bigger than some threshold, it
links the on-stack sem_queue to the sem_array, then goes to sleep
without holding a reference on the sem_array.

When __do_semtimedop() comes back out of sleep, one of two things must
happen:

 a) We prove that the on-stack sem_queue has been disconnected from the
    (possibly freed) sem_array, making it safe to return from the stack
    frame that the sem_queue exists in.

 b) We stabilize our reference to the sem_array, lock the sem_array, and
    detach the sem_queue from the sem_array ourselves.

sem_array has RCU lifetime, so for case (b), the reference can be
stabilized inside an RCU read-side critical section by locklessly
checking whether the sem_queue is still connected to the sem_array.

However, the current code does the lockless check on sem_queue before
starting an RCU read-side critical section, so the result of the
lockless check immediately becomes useless.

Fix it by doing rcu_read_lock() before the lockless check.  Now RCU
ensures that if we observe the object being on our queue, the object
can't be freed until rcu_read_unlock().

This bug is only hittable on kernel builds with full preemption support
(either CONFIG_PREEMPT or PREEMPT_DYNAMIC with preempt=full).

Fixes: 370b262c89 ("ipc/sem: avoid idr tree lookup for interrupted semop")
Cc: stable@vger.kernel.org
Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-12-08 11:18:35 +01:00
..
Makefile
compat.c
ipc_sysctl.c
mq_sysctl.c
mqueue.c ipc/mqueue.c: change __do_notify() to bypass check_kill_permission() 2020-05-14 07:57:23 +02:00
msg.c ipc/msg.c: consolidate all xxxctl_down() functions 2020-02-11 04:33:55 -08:00
msgutil.c ipc: prevent lockup on alloc_msg and free_msg 2019-06-15 11:54:00 +02:00
namespace.c ipc: simplify ipc initialization 2018-08-22 10:52:52 -07:00
sem.c ipc/sem: Fix dangling sem_array access in semtimedop race 2022-12-08 11:18:35 +01:00
shm.c shm: extend forced shm destroy to support objects from several IPC nses 2021-12-08 08:50:11 +01:00
syscall.c y2038: ipc: Redirect ipc(SEMTIMEDOP, ...) to compat_ksys_semtimedop 2018-04-20 16:20:30 +02:00
util.c ipc: WARN if trying to remove ipc object which is absent 2021-11-26 11:36:23 +01:00
util.h ipc/util.c: update return value of ipc_getref from int to bool 2018-08-22 10:52:52 -07:00