[PATCH 09/11] exec: In bprm_fill_uid only set per_clear when honoring suid or sgid

From: Eric W. Biederman
Date: Thu May 28 2020 - 11:53:42 EST



It makes no sense to set active_per_clear when the kernel decides not
to honor the executables setuid or or setgid bits. Instead set
active_per_clear when the kernel actually decides to honor the suid or
sgid permission bits of an executable.

As far as I can tell this was the intended behavior but with the
ptrace logic hiding out in security/commcap.c:cap_bprm_apply_creds I
believe it was just overlooked that the setuid or setgid operation
could be cancelled.

History Tree: git://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
Fixes: 1bb0fa189c6a ("[PATCH] NX: clean up legacy binary support")
Signed-off-by: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
---
fs/exec.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index af108ecf9632..347dade4bc54 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1634,15 +1634,16 @@ static void bprm_fill_uid(struct linux_binprm *bprm)
need_cap = bprm->unsafe & LSM_UNSAFE_SHARE ||
!ptracer_capable(current, new->user_ns);

- if (mode & S_ISUID) {
+ if ((mode & S_ISUID) &&
+ (!need_cap || ns_capable(new->user_ns, CAP_SETUID))) {
bprm->per_clear = 1;
- if (!need_cap || ns_capable(new->user_ns, CAP_SETUID))
- new->suid = new->fsuid = new->euid = uid;
+ new->suid = new->fsuid = new->euid = uid;
}
- if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
+
+ if (((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) &&
+ (!need_cap || ns_capable(new->user_ns, CAP_SETGID))) {
bprm->per_clear = 1;
- if (!need_cap || ns_capable(new->user_ns, CAP_SETGID))
- new->sgid = new->fsgid = new->egid = gid;
+ new->sgid = new->fsgid = new->egid = gid;
}

after_setid:
--
2.25.0