Commit e661bb1c authored by Jann Horn's avatar Jann Horn Committed by Zefan Li

fs: take i_mutex during prepare_binprm for set[ug]id executables

commit 8b01fc86 upstream.

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: default avatarJann Horn <jann@thejh.net>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
[lizf: Backported to 3.4:
 - adjust context
 - remove task_no_new_priv and user namespace stuff
 - open-code file_inode()
 - s/READ_ONCE/ACCESS_ONCE]
Signed-off-by: default avatarZefan Li <lizefan@huawei.com>
parent fcafa22d
...@@ -1248,6 +1248,45 @@ void install_exec_creds(struct linux_binprm *bprm) ...@@ -1248,6 +1248,45 @@ void install_exec_creds(struct linux_binprm *bprm)
} }
EXPORT_SYMBOL(install_exec_creds); EXPORT_SYMBOL(install_exec_creds);
static void bprm_fill_uid(struct linux_binprm *bprm)
{
struct inode *inode;
unsigned int mode;
uid_t uid;
gid_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;
inode = bprm->file->f_path.dentry->d_inode;
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);
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;
}
}
/* /*
* determine how safe it is to execute the proposed program * determine how safe it is to execute the proposed program
* - the caller must hold ->cred_guard_mutex to protect against * - the caller must hold ->cred_guard_mutex to protect against
...@@ -1297,36 +1336,12 @@ static int check_unsafe_exec(struct linux_binprm *bprm) ...@@ -1297,36 +1336,12 @@ static int check_unsafe_exec(struct linux_binprm *bprm)
*/ */
int prepare_binprm(struct linux_binprm *bprm) int prepare_binprm(struct linux_binprm *bprm)
{ {
umode_t mode;
struct inode * inode = bprm->file->f_path.dentry->d_inode;
int retval; int retval;
mode = inode->i_mode;
if (bprm->file->f_op == NULL) if (bprm->file->f_op == NULL)
return -EACCES; return -EACCES;
/* clear any previous set[ug]id data from a previous binary */ bprm_fill_uid(bprm);
bprm->cred->euid = current_euid();
bprm->cred->egid = current_egid();
if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
/* 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;
}
}
/* fill in binprm security blob */ /* fill in binprm security blob */
retval = security_bprm_set_creds(bprm); retval = security_bprm_set_creds(bprm);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment