Skip to content

Commit 259e5e6

Browse files
amlutoJames Morris
authored andcommitted
Add PR_{GET,SET}_NO_NEW_PRIVS to prevent execve from granting privs
With this change, calling prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) disables privilege granting operations at execve-time. For example, a process will not be able to execute a setuid binary to change their uid or gid if this bit is set. The same is true for file capabilities. Additionally, LSM_UNSAFE_NO_NEW_PRIVS is defined to ensure that LSMs respect the requested behavior. To determine if the NO_NEW_PRIVS bit is set, a task may call prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0); It returns 1 if set and 0 if it is not set. If any of the arguments are non-zero, it will return -1 and set errno to -EINVAL. (PR_SET_NO_NEW_PRIVS behaves similarly.) This functionality is desired for the proposed seccomp filter patch series. By using PR_SET_NO_NEW_PRIVS, it allows a task to modify the system call behavior for itself and its child tasks without being able to impact the behavior of a more privileged task. Another potential use is making certain privileged operations unprivileged. For example, chroot may be considered "safe" if it cannot affect privileged tasks. Note, this patch causes execve to fail when PR_SET_NO_NEW_PRIVS is set and AppArmor is in use. It is fixed in a subsequent patch. Signed-off-by: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Will Drewry <wad@chromium.org> Acked-by: Eric Paris <eparis@redhat.com> Acked-by: Kees Cook <keescook@chromium.org> v18: updated change desc v17: using new define values as per 3.4 Signed-off-by: James Morris <james.l.morris@oracle.com>
1 parent 9ccf010 commit 259e5e6

8 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎fs/exec.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,13 @@ static int check_unsafe_exec(struct linux_binprm *bprm)
12451245
bprm->unsafe |= LSM_UNSAFE_PTRACE;
12461246
}
12471247

1248+
/*
1249+
* This isn't strictly necessary, but it makes it harder for LSMs to
1250+
* mess up.
1251+
*/
1252+
if (current->no_new_privs)
1253+
bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
1254+
12481255
n_fs = 1;
12491256
spin_lock(&p->fs->lock);
12501257
rcu_read_lock();
@@ -1288,7 +1295,8 @@ int prepare_binprm(struct linux_binprm *bprm)
12881295
bprm->cred->euid = current_euid();
12891296
bprm->cred->egid = current_egid();
12901297

1291-
if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
1298+
if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) &&
1299+
!current->no_new_privs) {
12921300
/* Set-uid? */
12931301
if (mode & S_ISUID) {
12941302
bprm->per_clear |= PER_CLEAR_ON_SETID;

‎include/linux/prctl.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,19 @@
124124
#define PR_SET_CHILD_SUBREAPER 36
125125
#define PR_GET_CHILD_SUBREAPER 37
126126

127+
/*
128+
* If no_new_privs is set, then operations that grant new privileges (i.e.
129+
* execve) will either fail or not grant them. This affects suid/sgid,
130+
* file capabilities, and LSMs.
131+
*
132+
* Operations that merely manipulate or drop existing privileges (setresuid,
133+
* capset, etc.) will still work. Drop those privileges if you want them gone.
134+
*
135+
* Changing LSM security domain is considered a new privilege. So, for example,
136+
* asking selinux for a specific new context (e.g. with runcon) will result
137+
* in execve returning -EPERM.
138+
*/
139+
#define PR_SET_NO_NEW_PRIVS 38
140+
#define PR_GET_NO_NEW_PRIVS 39
141+
127142
#endif /* _LINUX_PRCTL_H */

‎include/linux/sched.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,8 @@ struct task_struct {
13411341
* execve */
13421342
unsigned in_iowait:1;
13431343

1344+
/* task may not gain privileges */
1345+
unsigned no_new_privs:1;
13441346

13451347
/* Revert to default priority/policy when forking */
13461348
unsigned sched_reset_on_fork:1;

‎include/linux/security.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ struct request_sock;
144144
#define LSM_UNSAFE_SHARE 1
145145
#define LSM_UNSAFE_PTRACE 2
146146
#define LSM_UNSAFE_PTRACE_CAP 4
147+
#define LSM_UNSAFE_NO_NEW_PRIVS 8
147148

148149
#ifdef CONFIG_MMU
149150
extern int mmap_min_addr_handler(struct ctl_table *table, int write,

‎kernel/sys.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
19791979
error = put_user(me->signal->is_child_subreaper,
19801980
(int __user *) arg2);
19811981
break;
1982+
case PR_SET_NO_NEW_PRIVS:
1983+
if (arg2 != 1 || arg3 || arg4 || arg5)
1984+
return -EINVAL;
1985+
1986+
current->no_new_privs = 1;
1987+
break;
1988+
case PR_GET_NO_NEW_PRIVS:
1989+
if (arg2 || arg3 || arg4 || arg5)
1990+
return -EINVAL;
1991+
return current->no_new_privs ? 1 : 0;
19821992
default:
19831993
error = -EINVAL;
19841994
break;

‎security/apparmor/domain.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
360360
if (bprm->cred_prepared)
361361
return 0;
362362

363+
/* XXX: no_new_privs is not usable with AppArmor yet */
364+
if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
365+
return -EPERM;
366+
363367
cxt = bprm->cred->security;
364368
BUG_ON(!cxt);
365369

‎security/commoncap.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,17 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
506506
skip:
507507

508508
/* Don't let someone trace a set[ug]id/setpcap binary with the revised
509-
* credentials unless they have the appropriate permit
509+
* credentials unless they have the appropriate permit.
510+
*
511+
* In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
510512
*/
511513
if ((new->euid != old->uid ||
512514
new->egid != old->gid ||
513515
!cap_issubset(new->cap_permitted, old->cap_permitted)) &&
514516
bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
515517
/* downgrade; they get no more than they had, and maybe less */
516-
if (!capable(CAP_SETUID)) {
518+
if (!capable(CAP_SETUID) ||
519+
(bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
517520
new->euid = new->uid;
518521
new->egid = new->gid;
519522
}

‎security/selinux/hooks.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,13 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm)
20162016
new_tsec->sid = old_tsec->exec_sid;
20172017
/* Reset exec SID on execve. */
20182018
new_tsec->exec_sid = 0;
2019+
2020+
/*
2021+
* Minimize confusion: if no_new_privs and a transition is
2022+
* explicitly requested, then fail the exec.
2023+
*/
2024+
if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2025+
return -EPERM;
20192026
} else {
20202027
/* Check for a default transition on this program. */
20212028
rc = security_transition_sid(old_tsec->sid, isec->sid,
@@ -2029,7 +2036,8 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm)
20292036
ad.selinux_audit_data = &sad;
20302037
ad.u.path = bprm->file->f_path;
20312038

2032-
if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
2039+
if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2040+
(bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
20332041
new_tsec->sid = old_tsec->sid;
20342042

20352043
if (new_tsec->sid == old_tsec->sid) {

0 commit comments

Comments
 (0)