[Natty/SRU][PATCH] ipc/sem.c: fix race with concurrent semtimedop() timeouts and IPC_RMID

Herton R. Krzesinski herton.krzesinski at canonical.com
Fri Mar 2 16:18:55 UTC 2012

From: Manfred Spraul <manfred at colorfullife.com>

If a semaphore array is removed and in parallel a sleeping task is woken
up (signal or timeout, does not matter), then the woken up task does not
wait until wake_up_sem_queue_do() is completed.  This will cause crashes,
because wake_up_sem_queue_do() will read from a stale pointer.

The fix is simple: Regardless of anything, always call get_queue_result().
This function waits until wake_up_sem_queue_do() has finished it's task.

Addresses https://bugzilla.kernel.org/show_bug.cgi?id=27142

Reported-by: Yuriy Yevtukhov <yuriy at ucoz.com>
Reported-by: Harald Laabs <kernel at dasr.de>
Signed-off-by: Manfred Spraul <manfred at colorfullife.com>
Acked-by: Eric Dumazet <eric.dumazet at gmail.com>
Cc: <stable at kernel.org>		[2.6.35+]
Signed-off-by: Andrew Morton <akpm at linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
(cherry-picked from commit d694ad62bf539dbb20a0899ac2a954555f9e4a83 upstream)
BugLink: http://bugs.launchpad.net/bugs/943815
Signed-off-by: Herton Ronaldo Krzesinski <herton.krzesinski at canonical.com>
 ipc/sem.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/ipc/sem.c b/ipc/sem.c
index 0e0d49b..9769b47 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -1454,15 +1454,24 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
 	sma = sem_lock(ns, semid);
+	/*
+	 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
+	 */
+	error = get_queue_result(&queue);
+	/*
+	 * Array removed? If yes, leave without sem_unlock().
+	 */
 	if (IS_ERR(sma)) {
 		error = -EIDRM;
 		goto out_free;
-	error = get_queue_result(&queue);
-	 * If queue.status != -EINTR we are woken up by another process
+	 * If queue.status != -EINTR we are woken up by another process.
+	 * Leave without unlink_queue(), but with sem_unlock().
 	if (error != -EINTR) {

More information about the kernel-team mailing list