Skip to content

Commit 56305aa

Browse files
committed
exec: Compute file based creds only once
Move the computation of creds from prepare_binfmt into begin_new_exec so that the creds need only be computed once. This is just code reorganization no semantic changes of any kind are made. Moving the computation is safe. I have looked through the kernel and verified none of the binfmts look at bprm->cred directly, and that there are no helpers that look at bprm->cred indirectly. Which means that it is not a problem to compute the bprm->cred later in the execution flow as it is not used until it becomes current->cred. A new function bprm_creds_from_file is added to contain the work that needs to be done. bprm_creds_from_file first computes which file bprm->executable or most likely bprm->file that the bprm->creds will be computed from. The funciton bprm_fill_uid is updated to receive the file instead of accessing bprm->file. The now unnecessary work needed to reset the bprm->cred->euid, and bprm->cred->egid is removed from brpm_fill_uid. A small comment to document that bprm_fill_uid now only deals with the work to handle suid and sgid files. The default case is already heandled by prepare_exec_creds. The function security_bprm_repopulate_creds is renamed security_bprm_creds_from_file and now is explicitly passed the file from which to compute the creds. The documentation of the bprm_creds_from_file security hook is updated to explain when the hook is called and what it needs to do. The file is passed from cap_bprm_creds_from_file into get_file_caps so that the caps are computed for the appropriate file. The now unnecessary work in cap_bprm_creds_from_file to reset the ambient capabilites has been removed. A small comment to document that the work of cap_bprm_creds_from_file is to read capabilities from the files secureity attribute and derive capabilities from the fact the user had uid 0 has been added. Reviewed-by: Kees Cook <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent a786832 commit 56305aa

File tree

8 files changed

+61
-79
lines changed

8 files changed

+61
-79
lines changed

fs/binfmt_misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static int load_misc_binary(struct linux_binprm *bprm)
192192

193193
bprm->interpreter = interp_file;
194194
if (fmt->flags & MISC_FMT_CREDENTIALS)
195-
bprm->preserve_creds = 1;
195+
bprm->execfd_creds = 1;
196196

197197
retval = 0;
198198
ret:

fs/exec.c

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272

7373
#include <trace/events/sched.h>
7474

75+
static int bprm_creds_from_file(struct linux_binprm *bprm);
76+
7577
int suid_dumpable = 0;
7678

7779
static LIST_HEAD(formats);
@@ -1304,6 +1306,11 @@ int begin_new_exec(struct linux_binprm * bprm)
13041306
struct task_struct *me = current;
13051307
int retval;
13061308

1309+
/* Once we are committed compute the creds */
1310+
retval = bprm_creds_from_file(bprm);
1311+
if (retval)
1312+
return retval;
1313+
13071314
/*
13081315
* Ensure all future errors are fatal.
13091316
*/
@@ -1354,7 +1361,6 @@ int begin_new_exec(struct linux_binprm * bprm)
13541361
me->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD |
13551362
PF_NOFREEZE | PF_NO_SETAFFINITY);
13561363
flush_thread();
1357-
bprm->per_clear |= bprm->pf_per_clear;
13581364
me->personality &= ~bprm->per_clear;
13591365

13601366
/*
@@ -1365,13 +1371,6 @@ int begin_new_exec(struct linux_binprm * bprm)
13651371
*/
13661372
do_close_on_exec(me->files);
13671373

1368-
/*
1369-
* Once here, prepare_binrpm() will not be called any more, so
1370-
* the final state of setuid/setgid/fscaps can be merged into the
1371-
* secureexec flag.
1372-
*/
1373-
bprm->secureexec |= bprm->active_secureexec;
1374-
13751374
if (bprm->secureexec) {
13761375
/* Make sure parent cannot signal privileged process. */
13771376
me->pdeath_signal = 0;
@@ -1587,29 +1586,21 @@ static void check_unsafe_exec(struct linux_binprm *bprm)
15871586
spin_unlock(&p->fs->lock);
15881587
}
15891588

1590-
static void bprm_fill_uid(struct linux_binprm *bprm)
1589+
static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file)
15911590
{
1591+
/* Handle suid and sgid on files */
15921592
struct inode *inode;
15931593
unsigned int mode;
15941594
kuid_t uid;
15951595
kgid_t gid;
15961596

1597-
/*
1598-
* Since this can be called multiple times (via prepare_binprm),
1599-
* we must clear any previous work done when setting set[ug]id
1600-
* bits from any earlier bprm->file uses (for example when run
1601-
* first for a setuid script then again for its interpreter).
1602-
*/
1603-
bprm->cred->euid = current_euid();
1604-
bprm->cred->egid = current_egid();
1605-
1606-
if (!mnt_may_suid(bprm->file->f_path.mnt))
1597+
if (!mnt_may_suid(file->f_path.mnt))
16071598
return;
16081599

16091600
if (task_no_new_privs(current))
16101601
return;
16111602

1612-
inode = bprm->file->f_path.dentry->d_inode;
1603+
inode = file->f_path.dentry->d_inode;
16131604
mode = READ_ONCE(inode->i_mode);
16141605
if (!(mode & (S_ISUID|S_ISGID)))
16151606
return;
@@ -1629,40 +1620,38 @@ static void bprm_fill_uid(struct linux_binprm *bprm)
16291620
return;
16301621

16311622
if (mode & S_ISUID) {
1632-
bprm->pf_per_clear |= PER_CLEAR_ON_SETID;
1623+
bprm->per_clear |= PER_CLEAR_ON_SETID;
16331624
bprm->cred->euid = uid;
16341625
}
16351626

16361627
if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1637-
bprm->pf_per_clear |= PER_CLEAR_ON_SETID;
1628+
bprm->per_clear |= PER_CLEAR_ON_SETID;
16381629
bprm->cred->egid = gid;
16391630
}
16401631
}
16411632

1633+
/*
1634+
* Compute brpm->cred based upon the final binary.
1635+
*/
1636+
static int bprm_creds_from_file(struct linux_binprm *bprm)
1637+
{
1638+
/* Compute creds based on which file? */
1639+
struct file *file = bprm->execfd_creds ? bprm->executable : bprm->file;
1640+
1641+
bprm_fill_uid(bprm, file);
1642+
return security_bprm_creds_from_file(bprm, file);
1643+
}
1644+
16421645
/*
16431646
* Fill the binprm structure from the inode.
1644-
* Check permissions, then read the first BINPRM_BUF_SIZE bytes
1647+
* Read the first BINPRM_BUF_SIZE bytes
16451648
*
16461649
* This may be called multiple times for binary chains (scripts for example).
16471650
*/
16481651
static int prepare_binprm(struct linux_binprm *bprm)
16491652
{
16501653
loff_t pos = 0;
16511654

1652-
/* Can the interpreter get to the executable without races? */
1653-
if (!bprm->preserve_creds) {
1654-
int retval;
1655-
1656-
/* Recompute parts of bprm->cred based on bprm->file */
1657-
bprm->active_secureexec = 0;
1658-
bprm->pf_per_clear = 0;
1659-
bprm_fill_uid(bprm);
1660-
retval = security_bprm_repopulate_creds(bprm);
1661-
if (retval)
1662-
return retval;
1663-
}
1664-
bprm->preserve_creds = 0;
1665-
16661655
memset(bprm->buf, 0, BINPRM_BUF_SIZE);
16671656
return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos);
16681657
}

include/linux/binfmts.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ struct linux_binprm {
2929
/* Should an execfd be passed to userspace? */
3030
have_execfd:1,
3131

32-
/* It is safe to use the creds of a script (see binfmt_misc) */
33-
preserve_creds:1,
34-
/*
35-
* True if most recent call to security_bprm_set_creds
36-
* resulted in elevated privileges.
37-
*/
38-
active_secureexec:1,
32+
/* Use the creds of a script (see binfmt_misc) */
33+
execfd_creds:1,
3934
/*
4035
* Set by bprm_creds_for_exec hook to indicate a
4136
* privilege-gaining exec has happened. Used to set
@@ -55,11 +50,6 @@ struct linux_binprm {
5550
struct file * file;
5651
struct cred *cred; /* new credentials */
5752
int unsafe; /* how unsafe this exec is (mask of LSM_UNSAFE_*) */
58-
/*
59-
* bits to clear in current->personality
60-
* recalculated for each bprm->file.
61-
*/
62-
unsigned int pf_per_clear;
6353
unsigned int per_clear; /* bits to clear in current->personality */
6454
int argc, envc;
6555
const char * filename; /* Name of binary as seen by procps */

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_repopulate_creds, struct linux_binprm *bprm)
53+
LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, struct file *file)
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: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@
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_repopulate_creds:
48-
* Assuming that the relevant bits of @bprm->cred->security have been
49-
* previously set, examine @bprm->file and regenerate them. This is
50-
* so that the credentials derived from the interpreter the code is
51-
* actually going to run are used rather than credentials derived
52-
* from a script. This done because the interpreter binary needs to
53-
* reopen script, and may end up opening something completely different.
54-
* This hook may also optionally check permissions (e.g. for
55-
* transitions between security domains).
56-
* The hook must set @bprm->active_secureexec to 1 if AT_SECURE should be set to
47+
* @bprm_creds_from_file:
48+
* If @file is setpcap, suid, sgid or otherwise marked to change
49+
* privilege upon exec, update @bprm->cred to reflect that change.
50+
* This is called after finding the binary that will be executed.
51+
* without an interpreter. This ensures that the credentials will not
52+
* be derived from a script that the binary will need to reopen, which
53+
* when reopend may end up being a completely different file. This
54+
* hook may also optionally check permissions (e.g. for transitions
55+
* between security domains).
56+
* The hook must set @bprm->secureexec to 1 if AT_SECURE should be set to
5757
* request libc enable secure mode.
58-
* The hook must add to @bprm->pf_per_clear any personality flags that
58+
* The hook must add to @bprm->per_clear any personality flags that
5959
* should be cleared from current->personality.
6060
* @bprm contains the linux_binprm structure.
6161
* Return 0 if the hook is successful and permission is granted.

include/linux/security.h

Lines changed: 5 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_repopulate_creds(struct linux_binprm *bprm);
143+
extern int cap_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
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_repopulate_creds(struct linux_binprm *bprm);
280+
int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
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,10 @@ static inline int security_bprm_creds_for_exec(struct linux_binprm *bprm)
575575
return 0;
576576
}
577577

578-
static inline int security_bprm_repopulate_creds(struct linux_binprm *bprm)
578+
static inline int security_bprm_creds_from_file(struct linux_binprm *bprm,
579+
struct file *file)
579580
{
580-
return cap_bprm_repopulate_creds(bprm);
581+
return cap_bprm_creds_from_file(bprm, file);
581582
}
582583

583584
static inline int security_bprm_check(struct linux_binprm *bprm)

security/commoncap.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
647647
* its xattrs and, if present, apply them to the proposed credentials being
648648
* constructed by execve().
649649
*/
650-
static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_fcap)
650+
static int get_file_caps(struct linux_binprm *bprm, struct file *file,
651+
bool *effective, bool *has_fcap)
651652
{
652653
int rc = 0;
653654
struct cpu_vfs_cap_data vcaps;
@@ -657,18 +658,18 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_f
657658
if (!file_caps_enabled)
658659
return 0;
659660

660-
if (!mnt_may_suid(bprm->file->f_path.mnt))
661+
if (!mnt_may_suid(file->f_path.mnt))
661662
return 0;
662663

663664
/*
664665
* This check is redundant with mnt_may_suid() but is kept to make
665666
* explicit that capability bits are limited to s_user_ns and its
666667
* descendants.
667668
*/
668-
if (!current_in_userns(bprm->file->f_path.mnt->mnt_sb->s_user_ns))
669+
if (!current_in_userns(file->f_path.mnt->mnt_sb->s_user_ns))
669670
return 0;
670671

671-
rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
672+
rc = get_vfs_caps_from_disk(file->f_path.dentry, &vcaps);
672673
if (rc < 0) {
673674
if (rc == -EINVAL)
674675
printk(KERN_NOTICE "Invalid argument reading file caps for %s\n",
@@ -797,26 +798,27 @@ static inline bool nonroot_raised_pE(struct cred *new, const struct cred *old,
797798
}
798799

799800
/**
800-
* cap_bprm_repopulate_creds - Set up the proposed credentials for execve().
801+
* cap_bprm_creds_from_file - Set up the proposed credentials for execve().
801802
* @bprm: The execution parameters, including the proposed creds
803+
* @file: The file to pull the credentials from
802804
*
803805
* Set up the proposed credentials for a new execution context being
804806
* constructed by execve(). The proposed creds in @bprm->cred is altered,
805807
* which won't take effect immediately. Returns 0 if successful, -ve on error.
806808
*/
807-
int cap_bprm_repopulate_creds(struct linux_binprm *bprm)
809+
int cap_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file)
808810
{
811+
/* Process setpcap binaries and capabilities for uid 0 */
809812
const struct cred *old = current_cred();
810813
struct cred *new = bprm->cred;
811814
bool effective = false, has_fcap = false, is_setid;
812815
int ret;
813816
kuid_t root_uid;
814817

815-
new->cap_ambient = old->cap_ambient;
816818
if (WARN_ON(!cap_ambient_invariant_ok(old)))
817819
return -EPERM;
818820

819-
ret = get_file_caps(bprm, &effective, &has_fcap);
821+
ret = get_file_caps(bprm, file, &effective, &has_fcap);
820822
if (ret < 0)
821823
return ret;
822824

@@ -826,7 +828,7 @@ int cap_bprm_repopulate_creds(struct linux_binprm *bprm)
826828

827829
/* if we have fs caps, clear dangerous personality flags */
828830
if (__cap_gained(permitted, new, old))
829-
bprm->pf_per_clear |= PER_CLEAR_ON_SETID;
831+
bprm->per_clear |= PER_CLEAR_ON_SETID;
830832

831833
/* Don't let someone trace a set[ug]id/setpcap binary with the revised
832834
* credentials unless they have the appropriate permit.
@@ -889,7 +891,7 @@ int cap_bprm_repopulate_creds(struct linux_binprm *bprm)
889891
(!__is_real(root_uid, new) &&
890892
(effective ||
891893
__cap_grew(permitted, ambient, new))))
892-
bprm->active_secureexec = 1;
894+
bprm->secureexec = 1;
893895

894896
return 0;
895897
}
@@ -1346,7 +1348,7 @@ static struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
13461348
LSM_HOOK_INIT(ptrace_traceme, cap_ptrace_traceme),
13471349
LSM_HOOK_INIT(capget, cap_capget),
13481350
LSM_HOOK_INIT(capset, cap_capset),
1349-
LSM_HOOK_INIT(bprm_repopulate_creds, cap_bprm_repopulate_creds),
1351+
LSM_HOOK_INIT(bprm_creds_from_file, cap_bprm_creds_from_file),
13501352
LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
13511353
LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
13521354
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_repopulate_creds(struct linux_binprm *bprm)
831+
int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file)
832832
{
833-
return call_int_hook(bprm_repopulate_creds, 0, bprm);
833+
return call_int_hook(bprm_creds_from_file, 0, bprm, file);
834834
}
835835

836836
int security_bprm_check(struct linux_binprm *bprm)

0 commit comments

Comments
 (0)