mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Davidlohr Bueso <davidlohr@hp.com>
To: Manfred Spraul <manfred@colorfullife.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Rik van Riel <riel@redhat.com>, Mike Galbraith <efault@gmx.de>,
	sedat.dilek@gmail.com,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Davidlohr Bueso <davidlohr@hp.com>,
	stable@vger.kernel.org, #@domain.invalid, for@domain.invalid,
	3.11@domain.invalid
Subject: [PATCH 4/4] ipc,msg: prevent race with rmid in msgsnd,msgrcv
Date: Sun, 15 Sep 2013 20:04:37 -0700	[thread overview]
Message-ID: <1379300677-24188-5-git-send-email-davidlohr@hp.com> (raw)
In-Reply-To: <1379300677-24188-1-git-send-email-davidlohr@hp.com>

This fixes a race in both msgrcv() and msgsnd() between finding the msg and
actually dealing with the queue, as another thread can delete shmid
underneath us if we are preempted before acquiring the kern_ipc_perm.lock.

Manfred illustrates this nicely:

Assume a preemptible kernel that is preempted just after
> msq = msq_obtain_object_check(ns, msqid)
in do_msgrcv().
The only lock that is held is rcu_read_lock().

Now the other thread processes IPC_RMID.
When the first task is resumed, then it will happily wait for messages on a
deleted queue.

Fix this by checking for if the queue has been deleted after taking the lock.

Reported-by: Manfred Spraul <manfred@colorfullife.com>
Cc: stable@vger.kernel.org # for 3.11
Signed-off-by: Davidlohr Bueso <davidlohr@hp.com>
---
 ipc/msg.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/ipc/msg.c b/ipc/msg.c
index 06e8aae..4e7d95a 100644
--- a/ipc/msg.c
+++ b/ipc/msg.c
@@ -690,6 +690,12 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
 		if (ipcperms(ns, &msq->q_perm, S_IWUGO))
 			goto out_unlock0;
 
+		/* raced with RMID? */
+		if (msq->q_perm.deleted) {
+			err = -EIDRM;
+			goto out_unlock0;
+		}
+
 		err = security_msg_queue_msgsnd(msq, msg, msgflg);
 		if (err)
 			goto out_unlock0;
@@ -896,6 +902,13 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgfl
 			goto out_unlock1;
 
 		ipc_lock_object(&msq->q_perm);
+
+		/* raced with RMID? */
+		if (msq->q_perm.deleted) {
+			msg = ERR_PTR(-EIDRM);
+			goto out_unlock0;
+		}
+
 		msg = find_msg(msq, &msgtyp, mode);
 		if (!IS_ERR(msg)) {
 			/*
-- 
1.7.11.7


  parent reply	other threads:[~2013-09-16  3:05 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-16  3:04 [PATCH 0/4] ipc: shm and msg fixes Davidlohr Bueso
2013-09-16  3:04 ` [PATCH 1/4] ipc,shm: fix race with selinux Davidlohr Bueso
2013-09-16  9:23   ` Manfred Spraul
2013-09-16  3:04 ` [PATCH 2/4] ipc,shm: prevent race with rmid in shmat(2) Davidlohr Bueso
2013-09-27  5:45   ` Manfred Spraul
2013-09-27 23:40     ` Davidlohr Bueso
2013-09-16  3:04 ` [PATCH 3/4] ipc,msg: fix race with selinux Davidlohr Bueso
2013-09-16  3:04 ` Davidlohr Bueso [this message]
2013-09-27  5:50   ` [PATCH 4/4] ipc,msg: prevent race with rmid in msgsnd,msgrcv Manfred Spraul
2013-09-19 21:22 ` [PATCH 0/4] ipc: shm and msg fixes Davidlohr Bueso
2013-09-20 18:08   ` Eric Paris
2013-09-21 18:30     ` Davidlohr Bueso
2013-09-21 18:58       ` Linus Torvalds
2013-09-23  6:42         ` Davidlohr Bueso
2013-09-23 16:54           ` Linus Torvalds
2013-09-24  0:04             ` Davidlohr Bueso
2013-09-24  1:22               ` Linus Torvalds
2013-09-24  8:49                 ` Manfred Spraul
2013-09-24  9:05               ` Manfred Spraul

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1379300677-24188-5-git-send-email-davidlohr@hp.com \
    --to=davidlohr@hp.com \
    --cc=#@domain.invalid \
    --cc=3.11@domain.invalid \
    --cc=akpm@linux-foundation.org \
    --cc=efault@gmx.de \
    --cc=for@domain.invalid \
    --cc=linux-kernel@vger.kernel.org \
    --cc=manfred@colorfullife.com \
    --cc=riel@redhat.com \
    --cc=sedat.dilek@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®