Skip to content

Commit 112b714

Browse files
committed
exec: Convert security_bprm_set_creds into security_bprm_repopulate_creds
Rename bprm->cap_elevated to bprm->active_secureexec and initialize it in prepare_binprm instead of in cap_bprm_set_creds. Initializing bprm->active_secureexec in prepare_binprm allows multiple implementations of security_bprm_repopulate_creds to play nicely with each other. Rename security_bprm_set_creds to security_bprm_reopulate_creds to emphasize that this path recomputes part of bprm->cred. This recomputation avoids the time of check vs time of use problems that are inherent in unix #! interpreters. In short two renames and a move in the location of initializing bprm->active_secureexec. Link: https://lkml.kernel.org/r/[email protected] Acked-by: Linus Torvalds <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent b8bff59 commit 112b714

File tree

7 files changed

+19
-20
lines changed

7 files changed

+19
-20
lines changed

fs/exec.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ int begin_new_exec(struct linux_binprm * bprm)
13661366
* the final state of setuid/setgid/fscaps can be merged into the
13671367
* secureexec flag.
13681368
*/
1369-
bprm->secureexec |= bprm->cap_elevated;
1369+
bprm->secureexec |= bprm->active_secureexec;
13701370

13711371
if (bprm->secureexec) {
13721372
/* Make sure parent cannot signal privileged process. */
@@ -1634,10 +1634,10 @@ int prepare_binprm(struct linux_binprm *bprm)
16341634
int retval;
16351635
loff_t pos = 0;
16361636

1637+
/* Recompute parts of bprm->cred based on bprm->file */
1638+
bprm->active_secureexec = 0;
16371639
bprm_fill_uid(bprm);
1638-
1639-
/* fill in binprm security blob */
1640-
retval = security_bprm_set_creds(bprm);
1640+
retval = security_bprm_repopulate_creds(bprm);
16411641
if (retval)
16421642
return retval;
16431643

include/linux/binfmts.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ struct linux_binprm {
2727
unsigned long argmin; /* rlimit marker for copy_strings() */
2828
unsigned int
2929
/*
30-
* True if most recent call to cap_bprm_set_creds
30+
* True if most recent call to security_bprm_set_creds
3131
* resulted in elevated privileges.
3232
*/
33-
cap_elevated:1,
33+
active_secureexec:1,
3434
/*
3535
* Set by bprm_creds_for_exec hook to indicate a
3636
* privilege-gaining exec has happened. Used to set

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ LSM_HOOK(int, 0, settime, const struct timespec64 *ts,
5050
const struct timezone *tz)
5151
LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages)
5252
LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm)
53-
LSM_HOOK(int, 0, bprm_set_creds, struct linux_binprm *bprm)
53+
LSM_HOOK(int, 0, bprm_repopulate_creds, struct linux_binprm *bprm)
5454
LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
5555
LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
5656
LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm)

include/linux/lsm_hooks.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* request libc enable secure mode.
4545
* @bprm contains the linux_binprm structure.
4646
* Return 0 if the hook is successful and permission is granted.
47-
* @bprm_set_creds:
47+
* @bprm_repopulate_creds:
4848
* Assuming that the relevant bits of @bprm->cred->security have been
4949
* previously set, examine @bprm->file and regenerate them. This is
5050
* so that the credentials derived from the interpreter the code is
@@ -53,7 +53,7 @@
5353
* reopen script, and may end up opening something completely different.
5454
* This hook may also optionally check permissions (e.g. for
5555
* transitions between security domains).
56-
* The hook must set @bprm->cap_elevated to 1 if AT_SECURE should be set to
56+
* The hook must set @bprm->active_secureexec to 1 if AT_SECURE should be set to
5757
* request libc enable secure mode.
5858
* @bprm contains the linux_binprm structure.
5959
* Return 0 if the hook is successful and permission is granted.

include/linux/security.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ extern int cap_capset(struct cred *new, const struct cred *old,
140140
const kernel_cap_t *effective,
141141
const kernel_cap_t *inheritable,
142142
const kernel_cap_t *permitted);
143-
extern int cap_bprm_set_creds(struct linux_binprm *bprm);
143+
extern int cap_bprm_repopulate_creds(struct linux_binprm *bprm);
144144
extern int cap_inode_setxattr(struct dentry *dentry, const char *name,
145145
const void *value, size_t size, int flags);
146146
extern int cap_inode_removexattr(struct dentry *dentry, const char *name);
@@ -277,7 +277,7 @@ int security_syslog(int type);
277277
int security_settime64(const struct timespec64 *ts, const struct timezone *tz);
278278
int security_vm_enough_memory_mm(struct mm_struct *mm, long pages);
279279
int security_bprm_creds_for_exec(struct linux_binprm *bprm);
280-
int security_bprm_set_creds(struct linux_binprm *bprm);
280+
int security_bprm_repopulate_creds(struct linux_binprm *bprm);
281281
int security_bprm_check(struct linux_binprm *bprm);
282282
void security_bprm_committing_creds(struct linux_binprm *bprm);
283283
void security_bprm_committed_creds(struct linux_binprm *bprm);
@@ -575,9 +575,9 @@ static inline int security_bprm_creds_for_exec(struct linux_binprm *bprm)
575575
return 0;
576576
}
577577

578-
static inline int security_bprm_set_creds(struct linux_binprm *bprm)
578+
static inline int security_bprm_repopulate_creds(struct linux_binprm *bprm)
579579
{
580-
return cap_bprm_set_creds(bprm);
580+
return cap_bprm_repopulate_creds(bprm);
581581
}
582582

583583
static inline int security_bprm_check(struct linux_binprm *bprm)

security/commoncap.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -797,14 +797,14 @@ static inline bool nonroot_raised_pE(struct cred *new, const struct cred *old,
797797
}
798798

799799
/**
800-
* cap_bprm_set_creds - Set up the proposed credentials for execve().
800+
* cap_bprm_repopulate_creds - Set up the proposed credentials for execve().
801801
* @bprm: The execution parameters, including the proposed creds
802802
*
803803
* Set up the proposed credentials for a new execution context being
804804
* constructed by execve(). The proposed creds in @bprm->cred is altered,
805805
* which won't take effect immediately. Returns 0 if successful, -ve on error.
806806
*/
807-
int cap_bprm_set_creds(struct linux_binprm *bprm)
807+
int cap_bprm_repopulate_creds(struct linux_binprm *bprm)
808808
{
809809
const struct cred *old = current_cred();
810810
struct cred *new = bprm->cred;
@@ -884,12 +884,11 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
884884
return -EPERM;
885885

886886
/* Check for privilege-elevated exec. */
887-
bprm->cap_elevated = 0;
888887
if (is_setid ||
889888
(!__is_real(root_uid, new) &&
890889
(effective ||
891890
__cap_grew(permitted, ambient, new))))
892-
bprm->cap_elevated = 1;
891+
bprm->active_secureexec = 1;
893892

894893
return 0;
895894
}
@@ -1346,7 +1345,7 @@ static struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
13461345
LSM_HOOK_INIT(ptrace_traceme, cap_ptrace_traceme),
13471346
LSM_HOOK_INIT(capget, cap_capget),
13481347
LSM_HOOK_INIT(capset, cap_capset),
1349-
LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
1348+
LSM_HOOK_INIT(bprm_repopulate_creds, cap_bprm_repopulate_creds),
13501349
LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
13511350
LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
13521351
LSM_HOOK_INIT(inode_getsecurity, cap_inode_getsecurity),

security/security.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,9 @@ int security_bprm_creds_for_exec(struct linux_binprm *bprm)
828828
return call_int_hook(bprm_creds_for_exec, 0, bprm);
829829
}
830830

831-
int security_bprm_set_creds(struct linux_binprm *bprm)
831+
int security_bprm_repopulate_creds(struct linux_binprm *bprm)
832832
{
833-
return call_int_hook(bprm_set_creds, 0, bprm);
833+
return call_int_hook(bprm_repopulate_creds, 0, bprm);
834834
}
835835

836836
int security_bprm_check(struct linux_binprm *bprm)

0 commit comments

Comments
 (0)