Skip to content

Commit 3cdf5b4

Browse files
committed
userns: Ignore suid and sgid on binaries if the uid or gid can not be mapped
When performing an exec where the binary lives in one user namespace and the execing process lives in another usre namespace there is the possibility that the target uids can not be represented. Instead of failing the exec simply ignore the suid/sgid bits and run the binary with lower privileges. We already do this in the case of MNT_NOSUID so this should be a well tested code path. As the user and group are not changed this should not introduce any security issues. Acked-by: Serge Hallyn <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent ae11e0f commit 3cdf5b4

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

fs/exec.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,14 +1266,13 @@ int prepare_binprm(struct linux_binprm *bprm)
12661266
bprm->cred->egid = current_egid();
12671267

12681268
if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) &&
1269-
!current->no_new_privs) {
1269+
!current->no_new_privs &&
1270+
kuid_has_mapping(bprm->cred->user_ns, inode->i_uid) &&
1271+
kgid_has_mapping(bprm->cred->user_ns, inode->i_gid)) {
12701272
/* Set-uid? */
12711273
if (mode & S_ISUID) {
1272-
if (!kuid_has_mapping(bprm->cred->user_ns, inode->i_uid))
1273-
return -EPERM;
12741274
bprm->per_clear |= PER_CLEAR_ON_SETID;
12751275
bprm->cred->euid = inode->i_uid;
1276-
12771276
}
12781277

12791278
/* Set-gid? */
@@ -1283,8 +1282,6 @@ int prepare_binprm(struct linux_binprm *bprm)
12831282
* executable.
12841283
*/
12851284
if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1286-
if (!kgid_has_mapping(bprm->cred->user_ns, inode->i_gid))
1287-
return -EPERM;
12881285
bprm->per_clear |= PER_CLEAR_ON_SETID;
12891286
bprm->cred->egid = inode->i_gid;
12901287
}

0 commit comments

Comments
 (0)