* Backport of 8b01fc86b9 (take i_mutex during ...) for 3.14.y
@ 2015-04-28 19:55 Chas Williams III
2015-04-28 20:05 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: Chas Williams III @ 2015-04-28 19:55 UTC (permalink / raw)
To: stable@vger.kernel.org
Hello, this is a backport of 8b01fc86b9 for 3.14.y. Please
apply as this addresses CVE-2015-3339.
Thanks!
From: Jann Horn <jann@thejh.net>
Date: Sun, 19 Apr 2015 02:48:39 +0200
Subject: [PATCH] fs: take i_mutex during prepare_binprm for set[ug]id
executables
This prevents a race between chown() and execve(), where chowning a
setuid-user binary to root would momentarily make the binary setuid
root.
This patch was mostly written by Linus Torvalds.
Signed-off-by: Jann Horn <jann@thejh.net>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
[ciwillia@brocade.com: backport to 3.14: context]
---
fs/exec.c | 76 ++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 48 insertions(+), 28 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index ea4449d..05f1942 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1268,6 +1268,53 @@ static void check_unsafe_exec(struct linux_binprm *bprm)
spin_unlock(&p->fs->lock);
}
+static void bprm_fill_uid(struct linux_binprm *bprm)
+{
+ struct inode *inode;
+ unsigned int mode;
+ kuid_t uid;
+ kgid_t gid;
+
+ /* clear any previous set[ug]id data from a previous binary */
+ bprm->cred->euid = current_euid();
+ bprm->cred->egid = current_egid();
+
+ if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
+ return;
+
+ if (current->no_new_privs)
+ return;
+
+ inode = file_inode(bprm->file);
+ mode = ACCESS_ONCE(inode->i_mode);
+ if (!(mode & (S_ISUID|S_ISGID)))
+ return;
+
+ /* Be careful if suid/sgid is set */
+ mutex_lock(&inode->i_mutex);
+
+ /* reload atomically mode/uid/gid now that lock held */
+ mode = inode->i_mode;
+ uid = inode->i_uid;
+ gid = inode->i_gid;
+ mutex_unlock(&inode->i_mutex);
+
+ /* We ignore suid/sgid if there are no mappings for them in the ns */
+ if (!kuid_has_mapping(bprm->cred->user_ns, uid) ||
+ !kgid_has_mapping(bprm->cred->user_ns, gid))
+ return;
+
+ if (mode & S_ISUID) {
+ bprm->per_clear |= PER_CLEAR_ON_SETID;
+ bprm->cred->euid = uid;
+ }
+
+ if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
+ bprm->per_clear |= PER_CLEAR_ON_SETID;
+ bprm->cred->egid = gid;
+ }
+}
+
/*
* Fill the binprm structure from the inode.
* Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
@@ -1276,36 +1323,9 @@ static void check_unsafe_exec(struct linux_binprm *bprm)
*/
int prepare_binprm(struct linux_binprm *bprm)
{
- struct inode *inode = file_inode(bprm->file);
- umode_t mode = inode->i_mode;
int retval;
-
- /* clear any previous set[ug]id data from a previous binary */
- bprm->cred->euid = current_euid();
- bprm->cred->egid = current_egid();
-
- if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) &&
- !current->no_new_privs &&
- kuid_has_mapping(bprm->cred->user_ns, inode->i_uid) &&
- kgid_has_mapping(bprm->cred->user_ns, inode->i_gid)) {
- /* Set-uid? */
- if (mode & S_ISUID) {
- bprm->per_clear |= PER_CLEAR_ON_SETID;
- bprm->cred->euid = inode->i_uid;
- }
-
- /* Set-gid? */
- /*
- * If setgid is set but no group execute bit then this
- * is a candidate for mandatory locking, not a setgid
- * executable.
- */
- if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
- bprm->per_clear |= PER_CLEAR_ON_SETID;
- bprm->cred->egid = inode->i_gid;
- }
- }
+ bprm_fill_uid(bprm);
/* fill in binprm security blob */
retval = security_bprm_set_creds(bprm);
--
1.9.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: Backport of 8b01fc86b9 (take i_mutex during ...) for 3.14.y
2015-04-28 19:55 Backport of 8b01fc86b9 (take i_mutex during ...) for 3.14.y Chas Williams III
@ 2015-04-28 20:05 ` Greg KH
2015-04-28 20:12 ` Chas Williams III
0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2015-04-28 20:05 UTC (permalink / raw)
To: Chas Williams III; +Cc: stable@vger.kernel.org
On Tue, Apr 28, 2015 at 01:55:10PM -0600, Chas Williams III wrote:
> Hello, this is a backport of 8b01fc86b9 for 3.14.y. Please
> apply as this addresses CVE-2015-3339.
>
> Thanks!
>
> From: Jann Horn <jann@thejh.net>
> Date: Sun, 19 Apr 2015 02:48:39 +0200
> Subject: [PATCH] fs: take i_mutex during prepare_binprm for set[ug]id
> executables
>
> This prevents a race between chown() and execve(), where chowning a
> setuid-user binary to root would momentarily make the binary setuid
> root.
>
> This patch was mostly written by Linus Torvalds.
>
> Signed-off-by: Jann Horn <jann@thejh.net>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> [ciwillia@brocade.com: backport to 3.14: context]
> ---
You aren't going to sign off on your changes? Why not?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Backport of 8b01fc86b9 (take i_mutex during ...) for 3.14.y
2015-04-28 20:05 ` Greg KH
@ 2015-04-28 20:12 ` Chas Williams III
0 siblings, 0 replies; 3+ messages in thread
From: Chas Williams III @ 2015-04-28 20:12 UTC (permalink / raw)
To: Greg KH; +Cc: Chas Williams III, stable@vger.kernel.org
On Tue, 2015-04-28 at 14:05 -0600, Greg KH wrote:
> On Tue, Apr 28, 2015 at 01:55:10PM -0600, Chas Williams III wrote:
> > Hello, this is a backport of 8b01fc86b9 for 3.14.y. Please
> > apply as this addresses CVE-2015-3339.
> >
> > Thanks!
> >
> > From: Jann Horn <jann@thejh.net>
> > Date: Sun, 19 Apr 2015 02:48:39 +0200
> > Subject: [PATCH] fs: take i_mutex during prepare_binprm for set[ug]id
> > executables
> >
> > This prevents a race between chown() and execve(), where chowning a
> > setuid-user binary to root would momentarily make the binary setuid
> > root.
> >
> > This patch was mostly written by Linus Torvalds.
> >
> > Signed-off-by: Jann Horn <jann@thejh.net>
> > Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> > [ciwillia@brocade.com: backport to 3.14: context]
> > ---
>
> You aren't going to sign off on your changes? Why not?
>
I could sign-off if you like, but I didn't really make substantial
changes. If that is what you like, I can do that.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-04-28 20:13 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-28 19:55 Backport of 8b01fc86b9 (take i_mutex during ...) for 3.14.y Chas Williams III
2015-04-28 20:05 ` Greg KH
2015-04-28 20:12 ` Chas Williams III
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox