Skip to content

Commit f84df2a

Browse files
committed
exec: Ensure mm->user_ns contains the execed files
When the user namespace support was merged the need to prevent ptrace from revealing the contents of an unreadable executable was overlooked. Correct this oversight by ensuring that the executed file or files are in mm->user_ns, by adjusting mm->user_ns. Use the new function privileged_wrt_inode_uidgid to see if the executable is a member of the user namespace, and as such if having CAP_SYS_PTRACE in the user namespace should allow tracing the executable. If not update mm->user_ns to the parent user namespace until an appropriate parent is found. Cc: stable@vger.kernel.org Reported-by: Jann Horn <jann@thejh.net> Fixes: 9e4a36e ("userns: Fail exec for suid and sgid binaries with ids outside our user namespace.") Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 84d77d3 commit f84df2a

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎fs/exec.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,8 +1275,22 @@ EXPORT_SYMBOL(flush_old_exec);
12751275

12761276
void would_dump(struct linux_binprm *bprm, struct file *file)
12771277
{
1278-
if (inode_permission(file_inode(file), MAY_READ) < 0)
1278+
struct inode *inode = file_inode(file);
1279+
if (inode_permission(inode, MAY_READ) < 0) {
1280+
struct user_namespace *old, *user_ns;
12791281
bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1282+
1283+
/* Ensure mm->user_ns contains the executable */
1284+
user_ns = old = bprm->mm->user_ns;
1285+
while ((user_ns != &init_user_ns) &&
1286+
!privileged_wrt_inode_uidgid(user_ns, inode))
1287+
user_ns = user_ns->parent;
1288+
1289+
if (old != user_ns) {
1290+
bprm->mm->user_ns = get_user_ns(user_ns);
1291+
put_user_ns(old);
1292+
}
1293+
}
12801294
}
12811295
EXPORT_SYMBOL(would_dump);
12821296

@@ -1306,7 +1320,6 @@ void setup_new_exec(struct linux_binprm * bprm)
13061320
!gid_eq(bprm->cred->gid, current_egid())) {
13071321
current->pdeath_signal = 0;
13081322
} else {
1309-
would_dump(bprm, bprm->file);
13101323
if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
13111324
set_dumpable(current->mm, suid_dumpable);
13121325
}
@@ -1741,6 +1754,8 @@ static int do_execveat_common(int fd, struct filename *filename,
17411754
if (retval < 0)
17421755
goto out;
17431756

1757+
would_dump(bprm, bprm->file);
1758+
17441759
retval = exec_binprm(bprm);
17451760
if (retval < 0)
17461761
goto out;

‎include/linux/capability.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
240240
return true;
241241
}
242242
#endif /* CONFIG_MULTIUSER */
243+
extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode);
243244
extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
244245
extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
245246
extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);

‎kernel/capability.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,19 @@ bool file_ns_capable(const struct file *file, struct user_namespace *ns,
456456
}
457457
EXPORT_SYMBOL(file_ns_capable);
458458

459+
/**
460+
* privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?
461+
* @ns: The user namespace in question
462+
* @inode: The inode in question
463+
*
464+
* Return true if the inode uid and gid are within the namespace.
465+
*/
466+
bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode)
467+
{
468+
return kuid_has_mapping(ns, inode->i_uid) &&
469+
kgid_has_mapping(ns, inode->i_gid);
470+
}
471+
459472
/**
460473
* capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
461474
* @inode: The inode in question
@@ -469,8 +482,7 @@ bool capable_wrt_inode_uidgid(const struct inode *inode, int cap)
469482
{
470483
struct user_namespace *ns = current_user_ns();
471484

472-
return ns_capable(ns, cap) && kuid_has_mapping(ns, inode->i_uid) &&
473-
kgid_has_mapping(ns, inode->i_gid);
485+
return ns_capable(ns, cap) && privileged_wrt_inode_uidgid(ns, inode);
474486
}
475487
EXPORT_SYMBOL(capable_wrt_inode_uidgid);
476488

0 commit comments

Comments
 (0)