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>
Subject: [PATCH 1/4] ipc,shm: fix race with selinux
Date: Sun, 15 Sep 2013 20:04:34 -0700	[thread overview]
Message-ID: <1379300677-24188-2-git-send-email-davidlohr@hp.com> (raw)
In-Reply-To: <1379300677-24188-1-git-send-email-davidlohr@hp.com>

Currently, we check shm security only under RCU. Since selinux
can free the security structure, through selinux_sem_free_security(),
we can run into a use-after-free condition. This bug affects both
shmctl and shmat syscalls.

The fix is obvious, make sure we hold the kern_ipc_perm.lock while
performing these security checks.

Reported-by: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Davidlohr Bueso <davidlohr@hp.com>
---
 ipc/shm.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/ipc/shm.c b/ipc/shm.c
index 2821cdf..bc3e897 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -781,18 +781,17 @@ static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
 
 	shp = container_of(ipcp, struct shmid_kernel, shm_perm);
 
+	ipc_lock_object(&shp->shm_perm);
 	err = security_shm_shmctl(shp, cmd);
 	if (err)
-		goto out_unlock1;
+		goto out_unlock0;
 
 	switch (cmd) {
 	case IPC_RMID:
-		ipc_lock_object(&shp->shm_perm);
 		/* do_shm_rmid unlocks the ipc object and rcu */
 		do_shm_rmid(ns, ipcp);
 		goto out_up;
 	case IPC_SET:
-		ipc_lock_object(&shp->shm_perm);
 		err = ipc_update_perm(&shmid64.shm_perm, ipcp);
 		if (err)
 			goto out_unlock0;
@@ -800,7 +799,6 @@ static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
 		break;
 	default:
 		err = -EINVAL;
-		goto out_unlock1;
 	}
 
 out_unlock0:
@@ -895,9 +893,12 @@ static int shmctl_nolock(struct ipc_namespace *ns, int shmid,
 		if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
 			goto out_unlock;
 
+		ipc_lock_object(&shp->shm_perm);
 		err = security_shm_shmctl(shp, cmd);
-		if (err)
+		if (err) {
+			ipc_unlock_object(&shp->shm_perm);
 			goto out_unlock;
+		}
 
 		memset(&tbuf, 0, sizeof(tbuf));
 		kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
@@ -909,6 +910,7 @@ static int shmctl_nolock(struct ipc_namespace *ns, int shmid,
 		tbuf.shm_lpid	= shp->shm_lprid;
 		tbuf.shm_nattch	= shp->shm_nattch;
 		rcu_read_unlock();
+		ipc_unlock_object(&shp->shm_perm);
 
 		if (copy_shmid_to_user(buf, &tbuf, version))
 			err = -EFAULT;
@@ -960,11 +962,12 @@ SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
 		}
 
 		audit_ipc_obj(&(shp->shm_perm));
+
+		ipc_lock_object(&shp->shm_perm);
 		err = security_shm_shmctl(shp, cmd);
 		if (err)
-			goto out_unlock1;
+			goto out_unlock0;
 
-		ipc_lock_object(&shp->shm_perm);
 		if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
 			kuid_t euid = current_euid();
 			err = -EPERM;
@@ -1089,11 +1092,13 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr,
 	if (ipcperms(ns, &shp->shm_perm, acc_mode))
 		goto out_unlock;
 
+	ipc_lock_object(&shp->shm_perm);
 	err = security_shm_shmat(shp, shmaddr, shmflg);
-	if (err)
+	if (err) {
+		ipc_unlock_object(&shp->shm_perm);
 		goto out_unlock;
+	}
 
-	ipc_lock_object(&shp->shm_perm);
 	path = shp->shm_file->f_path;
 	path_get(&path);
 	shp->shm_nattch++;
-- 
1.7.11.7


  reply	other threads:[~2013-09-16  3:04 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 ` Davidlohr Bueso [this message]
2013-09-16  9:23   ` [PATCH 1/4] ipc,shm: fix race with selinux 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 ` [PATCH 4/4] ipc,msg: prevent race with rmid in msgsnd,msgrcv Davidlohr Bueso
2013-09-27  5:50   ` 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-2-git-send-email-davidlohr@hp.com \
    --to=davidlohr@hp.com \
    --cc=akpm@linux-foundation.org \
    --cc=efault@gmx.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=manfred@colorfullife.com \
    --cc=riel@redhat.com \
    --cc=sedat.dilek@gmail.com \
    --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®