mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] [ipc/sem.c] fix race with concurrent semtimedop() timeouts and IPC_RMID
@ 2011-07-21  6:42 Manfred Spraul
  2011-07-21  6:58 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Manfred Spraul @ 2011-07-21  6:42 UTC (permalink / raw)
  To: LKML, Andrew Morton; +Cc: Manfred Spraul

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.

Andrew, could you please take care of merging the patch?

Reported-by: Yuriy Yevtukhov <yuriy@ucoz.com>
Reported-by: Harald Laabs <kernel@dasr.de>
Signed-off-by: Manfred Spraul <manfred@colorfullife.com>
---
 ipc/sem.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/ipc/sem.c b/ipc/sem.c
index 34193ed..add93d2 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -1456,15 +1456,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) {
-- 
1.7.6


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] [ipc/sem.c] fix race with concurrent semtimedop() timeouts and IPC_RMID
  2011-07-21  6:42 [PATCH] [ipc/sem.c] fix race with concurrent semtimedop() timeouts and IPC_RMID Manfred Spraul
@ 2011-07-21  6:58 ` Andrew Morton
  2011-07-21  8:12   ` Eric Dumazet
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2011-07-21  6:58 UTC (permalink / raw)
  To: Manfred Spraul; +Cc: LKML, Yuriy Yevtukhov

On Thu, 21 Jul 2011 08:42:53 +0200 Manfred Spraul <manfred@colorfullife.com> wrote:

> 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.
> 
> Andrew, could you please take care of merging the patch?

yup, thanks for this.  It's good to hear from you, and that you've
still got what it takes :)

Do you know how long this bug has been around for?  Somewhere between
2.6.32 and 2.6.36, but that a large interval!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] [ipc/sem.c] fix race with concurrent semtimedop() timeouts and IPC_RMID
  2011-07-21  6:58 ` Andrew Morton
@ 2011-07-21  8:12   ` Eric Dumazet
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Dumazet @ 2011-07-21  8:12 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Manfred Spraul, LKML, Yuriy Yevtukhov, Harald Laabs

Le mercredi 20 juillet 2011 à 23:58 -0700, Andrew Morton a écrit :
> On Thu, 21 Jul 2011 08:42:53 +0200 Manfred Spraul <manfred@colorfullife.com> wrote:
> 
> > 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.
> > 
> > Andrew, could you please take care of merging the patch?
> 
> yup, thanks for this.  It's good to hear from you, and that you've
> still got what it takes :)
> 
> Do you know how long this bug has been around for?  Somewhere between
> 2.6.32 and 2.6.36, but that a large interval!

As I said, bug was introduced in commit
0a2b9d4c79671b05956806ede5d054e03ae56280
(ipc/sem.c: move wake_up_process out of the spinlock section)

(so first faulty kernel version is 2.6.35)

Please also add this bugzilla reference :

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

Reported-by: Harald Laabs <kernelml@dasr.de>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-07-21  8:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-21  6:42 [PATCH] [ipc/sem.c] fix race with concurrent semtimedop() timeouts and IPC_RMID Manfred Spraul
2011-07-21  6:58 ` Andrew Morton
2011-07-21  8:12   ` Eric Dumazet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®