Skip to content

Commit 828f425

Browse files
committed
Merge tag 'secureexec-v4.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull secureexec update from Kees Cook: "This series has the ultimate goal of providing a sane stack rlimit when running set*id processes. To do this, the bprm_secureexec LSM hook is collapsed into the bprm_set_creds hook so the secureexec-ness of an exec can be determined early enough to make decisions about rlimits and the resulting memory layouts. Other logic acting on the secureexec-ness of an exec is similarly consolidated. Capabilities needed some special handling, but the refactoring removed other special handling, so that was a wash" * tag 'secureexec-v4.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: exec: Consolidate pdeath_signal clearing exec: Use sane stack rlimit under secureexec exec: Consolidate dumpability logic smack: Remove redundant pdeath_signal clearing exec: Use secureexec for clearing pdeath_signal exec: Use secureexec for setting dumpability LSM: drop bprm_secureexec hook commoncap: Move cap_elevated calculation into bprm_set_creds commoncap: Refactor to remove bprm_secureexec hook smack: Refactor to remove bprm_secureexec hook selinux: Refactor to remove bprm_secureexec hook apparmor: Refactor to remove bprm_secureexec hook binfmt: Introduce secureexec flag exec: Correct comments about "point of no return" exec: Rename bprm->cred_prepared to called_set_creds
2 parents 44ccba3 + fe8993b commit 828f425

File tree

16 files changed

+91
-159
lines changed

16 files changed

+91
-159
lines changed

fs/binfmt_elf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
252252
NEW_AUX_ENT(AT_EUID, from_kuid_munged(cred->user_ns, cred->euid));
253253
NEW_AUX_ENT(AT_GID, from_kgid_munged(cred->user_ns, cred->gid));
254254
NEW_AUX_ENT(AT_EGID, from_kgid_munged(cred->user_ns, cred->egid));
255-
NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
255+
NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
256256
NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
257257
#ifdef ELF_HWCAP2
258258
NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);

fs/binfmt_elf_fdpic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ static int create_elf_fdpic_tables(struct linux_binprm *bprm,
650650
NEW_AUX_ENT(AT_EUID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
651651
NEW_AUX_ENT(AT_GID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
652652
NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
653-
NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
653+
NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
654654
NEW_AUX_ENT(AT_EXECFN, bprm->exec);
655655

656656
#ifdef ARCH_DLINFO

fs/binfmt_flat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ static int load_flat_shared_library(int id, struct lib_info *libs)
890890
* as we're past the point of no return and are dealing with shared
891891
* libraries.
892892
*/
893-
bprm.cred_prepared = 1;
893+
bprm.called_set_creds = 1;
894894

895895
res = prepare_binprm(&bprm);
896896

fs/exec.c

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,12 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
12591259
perf_event_comm(tsk, exec);
12601260
}
12611261

1262+
/*
1263+
* Calling this is the point of no return. None of the failures will be
1264+
* seen by userspace since either the process is already taking a fatal
1265+
* signal (via de_thread() or coredump), or will have SEGV raised
1266+
* (after exec_mmap()) by search_binary_handlers (see below).
1267+
*/
12621268
int flush_old_exec(struct linux_binprm * bprm)
12631269
{
12641270
int retval;
@@ -1286,7 +1292,13 @@ int flush_old_exec(struct linux_binprm * bprm)
12861292
if (retval)
12871293
goto out;
12881294

1289-
bprm->mm = NULL; /* We're using it now */
1295+
/*
1296+
* After clearing bprm->mm (to mark that current is using the
1297+
* prepared mm now), we have nothing left of the original
1298+
* process. If anything from here on returns an error, the check
1299+
* in search_binary_handler() will SEGV current.
1300+
*/
1301+
bprm->mm = NULL;
12901302

12911303
set_fs(USER_DS);
12921304
current->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD |
@@ -1331,15 +1343,38 @@ EXPORT_SYMBOL(would_dump);
13311343

13321344
void setup_new_exec(struct linux_binprm * bprm)
13331345
{
1346+
/*
1347+
* Once here, prepare_binrpm() will not be called any more, so
1348+
* the final state of setuid/setgid/fscaps can be merged into the
1349+
* secureexec flag.
1350+
*/
1351+
bprm->secureexec |= bprm->cap_elevated;
1352+
1353+
if (bprm->secureexec) {
1354+
/* Make sure parent cannot signal privileged process. */
1355+
current->pdeath_signal = 0;
1356+
1357+
/*
1358+
* For secureexec, reset the stack limit to sane default to
1359+
* avoid bad behavior from the prior rlimits. This has to
1360+
* happen before arch_pick_mmap_layout(), which examines
1361+
* RLIMIT_STACK, but after the point of no return to avoid
1362+
* needing to clean up the change on failure.
1363+
*/
1364+
if (current->signal->rlim[RLIMIT_STACK].rlim_cur > _STK_LIM)
1365+
current->signal->rlim[RLIMIT_STACK].rlim_cur = _STK_LIM;
1366+
}
1367+
13341368
arch_pick_mmap_layout(current->mm);
13351369

1336-
/* This is the point of no return */
13371370
current->sas_ss_sp = current->sas_ss_size = 0;
13381371

1339-
if (uid_eq(current_euid(), current_uid()) && gid_eq(current_egid(), current_gid()))
1340-
set_dumpable(current->mm, SUID_DUMP_USER);
1341-
else
1372+
/* Figure out dumpability. */
1373+
if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP ||
1374+
bprm->secureexec)
13421375
set_dumpable(current->mm, suid_dumpable);
1376+
else
1377+
set_dumpable(current->mm, SUID_DUMP_USER);
13431378

13441379
arch_setup_new_exec();
13451380
perf_event_exec();
@@ -1351,15 +1386,6 @@ void setup_new_exec(struct linux_binprm * bprm)
13511386
*/
13521387
current->mm->task_size = TASK_SIZE;
13531388

1354-
/* install the new credentials */
1355-
if (!uid_eq(bprm->cred->uid, current_euid()) ||
1356-
!gid_eq(bprm->cred->gid, current_egid())) {
1357-
current->pdeath_signal = 0;
1358-
} else {
1359-
if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
1360-
set_dumpable(current->mm, suid_dumpable);
1361-
}
1362-
13631389
/* An exec changes our domain. We are no longer part of the thread
13641390
group */
13651391
current->self_exec_id++;
@@ -1548,7 +1574,7 @@ int prepare_binprm(struct linux_binprm *bprm)
15481574
retval = security_bprm_set_creds(bprm);
15491575
if (retval)
15501576
return retval;
1551-
bprm->cred_prepared = 1;
1577+
bprm->called_set_creds = 1;
15521578

15531579
memset(bprm->buf, 0, BINPRM_BUF_SIZE);
15541580
return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);

include/linux/binfmts.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,25 @@ struct linux_binprm {
2525
struct mm_struct *mm;
2626
unsigned long p; /* current top of mem */
2727
unsigned int
28-
cred_prepared:1,/* true if creds already prepared (multiple
29-
* preps happen for interpreters) */
30-
cap_effective:1;/* true if has elevated effective capabilities,
31-
* false if not; except for init which inherits
32-
* its parent's caps anyway */
28+
/*
29+
* True after the bprm_set_creds hook has been called once
30+
* (multiple calls can be made via prepare_binprm() for
31+
* binfmt_script/misc).
32+
*/
33+
called_set_creds:1,
34+
/*
35+
* True if most recent call to the commoncaps bprm_set_creds
36+
* hook (due to multiple prepare_binprm() calls from the
37+
* binfmt_script/misc handlers) resulted in elevated
38+
* privileges.
39+
*/
40+
cap_elevated:1,
41+
/*
42+
* Set by bprm_set_creds hook to indicate a privilege-gaining
43+
* exec has happened. Used to sanitize execution environment
44+
* and to set AT_SECURE auxv for glibc.
45+
*/
46+
secureexec:1;
3347
#ifdef __alpha__
3448
unsigned int taso:1;
3549
#endif

include/linux/lsm_hooks.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
* interpreters. The hook can tell whether it has already been called by
4444
* checking to see if @bprm->security is non-NULL. If so, then the hook
4545
* may decide either to retain the security information saved earlier or
46-
* to replace it.
46+
* to replace it. The hook must set @bprm->secureexec to 1 if a "secure
47+
* exec" has happened as a result of this hook call. The flag is used to
48+
* indicate the need for a sanitized execution environment, and is also
49+
* passed in the ELF auxiliary table on the initial stack to indicate
50+
* whether libc should enable secure mode.
4751
* @bprm contains the linux_binprm structure.
4852
* Return 0 if the hook is successful and permission is granted.
4953
* @bprm_check_security:
@@ -71,12 +75,6 @@
7175
* linux_binprm structure. This hook is a good place to perform state
7276
* changes on the process such as clearing out non-inheritable signal
7377
* state. This is called immediately after commit_creds().
74-
* @bprm_secureexec:
75-
* Return a boolean value (0 or 1) indicating whether a "secure exec"
76-
* is required. The flag is passed in the auxiliary table
77-
* on the initial stack to the ELF interpreter to indicate whether libc
78-
* should enable secure mode.
79-
* @bprm contains the linux_binprm structure.
8078
*
8179
* Security hooks for filesystem operations.
8280
*
@@ -1388,7 +1386,6 @@ union security_list_options {
13881386

13891387
int (*bprm_set_creds)(struct linux_binprm *bprm);
13901388
int (*bprm_check_security)(struct linux_binprm *bprm);
1391-
int (*bprm_secureexec)(struct linux_binprm *bprm);
13921389
void (*bprm_committing_creds)(struct linux_binprm *bprm);
13931390
void (*bprm_committed_creds)(struct linux_binprm *bprm);
13941391

@@ -1710,7 +1707,6 @@ struct security_hook_heads {
17101707
struct list_head vm_enough_memory;
17111708
struct list_head bprm_set_creds;
17121709
struct list_head bprm_check_security;
1713-
struct list_head bprm_secureexec;
17141710
struct list_head bprm_committing_creds;
17151711
struct list_head bprm_committed_creds;
17161712
struct list_head sb_alloc_security;

include/linux/security.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ extern int cap_capset(struct cred *new, const struct cred *old,
8585
const kernel_cap_t *inheritable,
8686
const kernel_cap_t *permitted);
8787
extern int cap_bprm_set_creds(struct linux_binprm *bprm);
88-
extern int cap_bprm_secureexec(struct linux_binprm *bprm);
8988
extern int cap_inode_setxattr(struct dentry *dentry, const char *name,
9089
const void *value, size_t size, int flags);
9190
extern int cap_inode_removexattr(struct dentry *dentry, const char *name);
@@ -232,7 +231,6 @@ int security_bprm_set_creds(struct linux_binprm *bprm);
232231
int security_bprm_check(struct linux_binprm *bprm);
233232
void security_bprm_committing_creds(struct linux_binprm *bprm);
234233
void security_bprm_committed_creds(struct linux_binprm *bprm);
235-
int security_bprm_secureexec(struct linux_binprm *bprm);
236234
int security_sb_alloc(struct super_block *sb);
237235
void security_sb_free(struct super_block *sb);
238236
int security_sb_copy_data(char *orig, char *copy);
@@ -541,11 +539,6 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
541539
{
542540
}
543541

544-
static inline int security_bprm_secureexec(struct linux_binprm *bprm)
545-
{
546-
return cap_bprm_secureexec(bprm);
547-
}
548-
549542
static inline int security_sb_alloc(struct super_block *sb)
550543
{
551544
return 0;

security/apparmor/domain.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
758758
file_inode(bprm->file)->i_mode
759759
};
760760

761-
if (bprm->cred_prepared)
761+
if (bprm->called_set_creds)
762762
return 0;
763763

764764
ctx = cred_ctx(bprm->cred);
@@ -807,7 +807,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
807807
aa_label_printk(new, GFP_ATOMIC);
808808
dbg_printk("\n");
809809
}
810-
bprm->unsafe |= AA_SECURE_X_NEEDED;
810+
bprm->secureexec = 1;
811811
}
812812

813813
if (label->proxy != new->proxy) {
@@ -843,23 +843,6 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
843843
goto done;
844844
}
845845

846-
/**
847-
* apparmor_bprm_secureexec - determine if secureexec is needed
848-
* @bprm: binprm for exec (NOT NULL)
849-
*
850-
* Returns: %1 if secureexec is needed else %0
851-
*/
852-
int apparmor_bprm_secureexec(struct linux_binprm *bprm)
853-
{
854-
/* the decision to use secure exec is computed in set_creds
855-
* and stored in bprm->unsafe.
856-
*/
857-
if (bprm->unsafe & AA_SECURE_X_NEEDED)
858-
return 1;
859-
860-
return 0;
861-
}
862-
863846
/*
864847
* Functions for self directed profile change
865848
*/

security/apparmor/include/domain.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ struct aa_domain {
3030
#define AA_CHANGE_STACK 8
3131

3232
int apparmor_bprm_set_creds(struct linux_binprm *bprm);
33-
int apparmor_bprm_secureexec(struct linux_binprm *bprm);
3433

3534
void aa_free_domain_entries(struct aa_domain *domain);
3635
int aa_change_hat(const char *hats[], int count, u64 token, int flags);

security/apparmor/include/file.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx)
101101
#define AA_X_INHERIT 0x4000
102102
#define AA_X_UNCONFINED 0x8000
103103

104-
/* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */
105-
#define AA_SECURE_X_NEEDED 0x8000
106-
107104
/* need to make conditional which ones are being set */
108105
struct path_cond {
109106
kuid_t uid;

security/apparmor/lsm.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
694694
LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
695695
LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
696696
LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
697-
LSM_HOOK_INIT(bprm_secureexec, apparmor_bprm_secureexec),
698697

699698
LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
700699
};

security/commoncap.c

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,6 @@ int cap_capset(struct cred *new,
285285
return 0;
286286
}
287287

288-
/*
289-
* Clear proposed capability sets for execve().
290-
*/
291-
static inline void bprm_clear_caps(struct linux_binprm *bprm)
292-
{
293-
cap_clear(bprm->cred->cap_permitted);
294-
bprm->cap_effective = false;
295-
}
296-
297288
/**
298289
* cap_inode_need_killpriv - Determine if inode change affects privileges
299290
* @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
@@ -443,7 +434,7 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c
443434
int rc = 0;
444435
struct cpu_vfs_cap_data vcaps;
445436

446-
bprm_clear_caps(bprm);
437+
cap_clear(bprm->cred->cap_permitted);
447438

448439
if (!file_caps_enabled)
449440
return 0;
@@ -476,7 +467,7 @@ static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_c
476467

477468
out:
478469
if (rc)
479-
bprm_clear_caps(bprm);
470+
cap_clear(bprm->cred->cap_permitted);
480471

481472
return rc;
482473
}
@@ -585,8 +576,6 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
585576
if (WARN_ON(!cap_ambient_invariant_ok(new)))
586577
return -EPERM;
587578

588-
bprm->cap_effective = effective;
589-
590579
/*
591580
* Audit candidate if current->cap_effective is set
592581
*
@@ -614,33 +603,17 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
614603
if (WARN_ON(!cap_ambient_invariant_ok(new)))
615604
return -EPERM;
616605

617-
return 0;
618-
}
619-
620-
/**
621-
* cap_bprm_secureexec - Determine whether a secure execution is required
622-
* @bprm: The execution parameters
623-
*
624-
* Determine whether a secure execution is required, return 1 if it is, and 0
625-
* if it is not.
626-
*
627-
* The credentials have been committed by this point, and so are no longer
628-
* available through @bprm->cred.
629-
*/
630-
int cap_bprm_secureexec(struct linux_binprm *bprm)
631-
{
632-
const struct cred *cred = current_cred();
633-
kuid_t root_uid = make_kuid(cred->user_ns, 0);
634-
635-
if (!uid_eq(cred->uid, root_uid)) {
636-
if (bprm->cap_effective)
637-
return 1;
638-
if (!cap_issubset(cred->cap_permitted, cred->cap_ambient))
639-
return 1;
606+
/* Check for privilege-elevated exec. */
607+
bprm->cap_elevated = 0;
608+
if (is_setid) {
609+
bprm->cap_elevated = 1;
610+
} else if (!uid_eq(new->uid, root_uid)) {
611+
if (effective ||
612+
!cap_issubset(new->cap_permitted, new->cap_ambient))
613+
bprm->cap_elevated = 1;
640614
}
641615

642-
return (!uid_eq(cred->euid, cred->uid) ||
643-
!gid_eq(cred->egid, cred->gid));
616+
return 0;
644617
}
645618

646619
/**
@@ -1079,7 +1052,6 @@ struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
10791052
LSM_HOOK_INIT(capget, cap_capget),
10801053
LSM_HOOK_INIT(capset, cap_capset),
10811054
LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
1082-
LSM_HOOK_INIT(bprm_secureexec, cap_bprm_secureexec),
10831055
LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
10841056
LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
10851057
LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),

security/security.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,6 @@ void security_bprm_committed_creds(struct linux_binprm *bprm)
351351
call_void_hook(bprm_committed_creds, bprm);
352352
}
353353

354-
int security_bprm_secureexec(struct linux_binprm *bprm)
355-
{
356-
return call_int_hook(bprm_secureexec, 0, bprm);
357-
}
358-
359354
int security_sb_alloc(struct super_block *sb)
360355
{
361356
return call_int_hook(sb_alloc_security, 0, sb);

0 commit comments

Comments
 (0)