Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit dc54fb8

Browse files
amlutoGerrit - the friendly Code Review server
authored and
Gerrit - the friendly Code Review server
committed
UPSTREAM: capabilities: ambient capabilities
Credit where credit is due: this idea comes from Christoph Lameter with a lot of valuable input from Serge Hallyn. This patch is heavily based on Christoph's patch. ===== The status quo ===== On Linux, there are a number of capabilities defined by the kernel. To perform various privileged tasks, processes can wield capabilities that they hold. Each task has four capability masks: effective (pE), permitted (pP), inheritable (pI), and a bounding set (X). When the kernel checks for a capability, it checks pE. The other capability masks serve to modify what capabilities can be in pE. Any task can remove capabilities from pE, pP, or pI at any time. If a task has a capability in pP, it can add that capability to pE and/or pI. If a task has CAP_SETPCAP, then it can add any capability to pI, and it can remove capabilities from X. Tasks are not the only things that can have capabilities; files can also have capabilities. A file can have no capabilty information at all [1]. If a file has capability information, then it has a permitted mask (fP) and an inheritable mask (fI) as well as a single effective bit (fE) [2]. File capabilities modify the capabilities of tasks that execve(2) them. A task that successfully calls execve has its capabilities modified for the file ultimately being excecuted (i.e. the binary itself if that binary is ELF or for the interpreter if the binary is a script.) [3] In the capability evolution rules, for each mask Z, pZ represents the old value and pZ' represents the new value. The rules are: pP' = (X & fP) | (pI & fI) pI' = pI pE' = (fE ? pP' : 0) X is unchanged For setuid binaries, fP, fI, and fE are modified by a moderately complicated set of rules that emulate POSIX behavior. Similarly, if euid == 0 or ruid == 0, then fP, fI, and fE are modified differently (primary, fP and fI usually end up being the full set). For nonroot users executing binaries with neither setuid nor file caps, fI and fP are empty and fE is false. As an extra complication, if you execute a process as nonroot and fE is set, then the "secure exec" rules are in effect: AT_SECURE gets set, LD_PRELOAD doesn't work, etc. This is rather messy. We've learned that making any changes is dangerous, though: if a new kernel version allows an unprivileged program to change its security state in a way that persists cross execution of a setuid program or a program with file caps, this persistent state is surprisingly likely to allow setuid or file-capped programs to be exploited for privilege escalation. ===== The problem ===== Capability inheritance is basically useless. If you aren't root and you execute an ordinary binary, fI is zero, so your capabilities have no effect whatsoever on pP'. This means that you can't usefully execute a helper process or a shell command with elevated capabilities if you aren't root. On current kernels, you can sort of work around this by setting fI to the full set for most or all non-setuid executable files. This causes pP' = pI for nonroot, and inheritance works. No one does this because it's a PITA and it isn't even supported on most filesystems. If you try this, you'll discover that every nonroot program ends up with secure exec rules, breaking many things. This is a problem that has bitten many people who have tried to use capabilities for anything useful. ===== The proposed change ===== This patch adds a fifth capability mask called the ambient mask (pA). pA does what most people expect pI to do. pA obeys the invariant that no bit can ever be set in pA if it is not set in both pP and pI. Dropping a bit from pP or pI drops that bit from pA. This ensures that existing programs that try to drop capabilities still do so, with a complication. Because capability inheritance is so broken, setting KEEPCAPS, using setresuid to switch to nonroot uids, and then calling execve effectively drops capabilities. Therefore, setresuid from root to nonroot conditionally clears pA unless SECBIT_NO_SETUID_FIXUP is set. Processes that don't like this can re-add bits to pA afterwards. The capability evolution rules are changed: pA' = (file caps or setuid or setgid ? 0 : pA) pP' = (X & fP) | (pI & fI) | pA' pI' = pI pE' = (fE ? pP' : pA') X is unchanged If you are nonroot but you have a capability, you can add it to pA. If you do so, your children get that capability in pA, pP, and pE. For example, you can set pA = CAP_NET_BIND_SERVICE, and your children can automatically bind low-numbered ports. Hallelujah! Unprivileged users can create user namespaces, map themselves to a nonzero uid, and create both privileged (relative to their namespace) and unprivileged process trees. This is currently more or less impossible. Hallelujah! You cannot use pA to try to subvert a setuid, setgid, or file-capped program: if you execute any such program, pA gets cleared and the resulting evolution rules are unchanged by this patch. Users with nonzero pA are unlikely to unintentionally leak that capability. If they run programs that try to drop privileges, dropping privileges will still work. It's worth noting that the degree of paranoia in this patch could possibly be reduced without causing serious problems. Specifically, if we allowed pA to persist across executing non-pA-aware setuid binaries and across setresuid, then, naively, the only capabilities that could leak as a result would be the capabilities in pA, and any attacker *already* has those capabilities. This would make me nervous, though -- setuid binaries that tried to privilege-separate might fail to do so, and putting CAP_DAC_READ_SEARCH or CAP_DAC_OVERRIDE into pA could have unexpected side effects. (Whether these unexpected side effects would be exploitable is an open question.) I've therefore taken the more paranoid route. We can revisit this later. An alternative would be to require PR_SET_NO_NEW_PRIVS before setting ambient capabilities. I think that this would be annoying and would make granting otherwise unprivileged users minor ambient capabilities (CAP_NET_BIND_SERVICE or CAP_NET_RAW for example) much less useful than it is with this patch. ===== Footnotes ===== [1] Files that are missing the "security.capability" xattr or that have unrecognized values for that xattr end up with has_cap set to false. The code that does that appears to be complicated for no good reason. [2] The libcap capability mask parsers and formatters are dangerously misleading and the documentation is flat-out wrong. fE is *not* a mask; it's a single bit. This has probably confused every single person who has tried to use file capabilities. [3] Linux very confusingly processes both the script and the interpreter if applicable, for reasons that elude me. The results from thinking about a script's file capabilities and/or setuid bits are mostly discarded. Preliminary userspace code is here, but it needs updating: https://git.kernel.org/cgit/linux/kernel/git/luto/util-linux-playground.git/commit/?h=cap_ambient&id=7f5afbd175d2 Here is a test program that can be used to verify the functionality (from Christoph): /* * Test program for the ambient capabilities. This program spawns a shell * that allows running processes with a defined set of capabilities. * * (C) 2015 Christoph Lameter <[email protected]> * Released under: GPL v3 or later. * * * Compile using: * * gcc -o ambient_test ambient_test.o -lcap-ng * * This program must have the following capabilities to run properly: * Permissions for CAP_NET_RAW, CAP_NET_ADMIN, CAP_SYS_NICE * * A command to equip the binary with the right caps is: * * setcap cap_net_raw,cap_net_admin,cap_sys_nice+p ambient_test * * * To get a shell with additional caps that can be inherited by other processes: * * ./ambient_test /bin/bash * * * Verifying that it works: * * From the bash spawed by ambient_test run * * cat /proc/$$/status * * and have a look at the capabilities. */ /* * Definitions from the kernel header files. These are going to be removed * when the /usr/include files have these defined. */ static void set_ambient_cap(int cap) { int rc; capng_get_caps_process(); rc = capng_update(CAPNG_ADD, CAPNG_INHERITABLE, cap); if (rc) { printf("Cannot add inheritable cap\n"); exit(2); } capng_apply(CAPNG_SELECT_CAPS); /* Note the two 0s at the end. Kernel checks for these */ if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0)) { perror("Cannot set cap"); exit(1); } } int main(int argc, char **argv) { int rc; set_ambient_cap(CAP_NET_RAW); set_ambient_cap(CAP_NET_ADMIN); set_ambient_cap(CAP_SYS_NICE); printf("Ambient_test forking shell\n"); if (execv(argv[1], argv + 1)) perror("Cannot exec"); return 0; } Signed-off-by: Christoph Lameter <[email protected]> # Original author Signed-off-by: Andy Lutomirski <[email protected]> Acked-by: Serge E. Hallyn <[email protected]> Acked-by: Kees Cook <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Aaron Jones <[email protected]> Cc: Ted Ts'o <[email protected]> Cc: Andrew G. Morgan <[email protected]> Cc: Mimi Zohar <[email protected]> Cc: Austin S Hemmelgarn <[email protected]> Cc: Markku Savela <[email protected]> Cc: Jarkko Sakkinen <[email protected]> Cc: Michael Kerrisk <[email protected]> Cc: James Morris <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> (cherry picked from commit 5831905) Bug: 31038224 Change-Id: Idab768286dffe9873a8ab0934f3824fa5905a06d Signed-off-by: Jorge Lucangeli Obes <[email protected]> Signed-off-by: Chetan C R <[email protected]>
1 parent 7c53df2 commit dc54fb8

File tree

6 files changed

+113
-11
lines changed

6 files changed

+113
-11
lines changed

fs/proc/array.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,16 @@ static void render_cap_t(struct seq_file *m, const char *header,
316316
static inline void task_cap(struct seq_file *m, struct task_struct *p)
317317
{
318318
const struct cred *cred;
319-
kernel_cap_t cap_inheritable, cap_permitted, cap_effective, cap_bset;
319+
kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
320+
cap_bset, cap_ambient;
320321

321322
rcu_read_lock();
322323
cred = __task_cred(p);
323324
cap_inheritable = cred->cap_inheritable;
324325
cap_permitted = cred->cap_permitted;
325326
cap_effective = cred->cap_effective;
326327
cap_bset = cred->cap_bset;
328+
cap_ambient = cred->cap_ambient;
327329
rcu_read_unlock();
328330

329331
NORM_CAPS(cap_inheritable);
@@ -335,6 +337,7 @@ static inline void task_cap(struct seq_file *m, struct task_struct *p)
335337
render_cap_t(m, "CapPrm:\t", &cap_permitted);
336338
render_cap_t(m, "CapEff:\t", &cap_effective);
337339
render_cap_t(m, "CapBnd:\t", &cap_bset);
340+
render_cap_t(m, "CapAmb:\t", &cap_ambient);
338341
}
339342

340343
static inline void task_seccomp(struct seq_file *m, struct task_struct *p)

include/linux/cred.h

+8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ struct cred {
121121
kernel_cap_t cap_permitted; /* caps we're permitted */
122122
kernel_cap_t cap_effective; /* caps we can actually use */
123123
kernel_cap_t cap_bset; /* capability bounding set */
124+
kernel_cap_t cap_ambient; /* Ambient capability set */
124125
#ifdef CONFIG_KEYS
125126
unsigned char jit_keyring; /* default keyring to attach requested
126127
* keys to */
@@ -196,6 +197,13 @@ static inline void validate_process_creds(void)
196197
}
197198
#endif
198199

200+
static inline bool cap_ambient_invariant_ok(const struct cred *cred)
201+
{
202+
return cap_issubset(cred->cap_ambient,
203+
cap_intersect(cred->cap_permitted,
204+
cred->cap_inheritable));
205+
}
206+
199207
/**
200208
* get_new_cred - Get a reference on a new set of credentials
201209
* @cred: The new credentials to reference

include/uapi/linux/prctl.h

+7
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,11 @@
158158
#define PR_SET_VMA 0x53564d41
159159
# define PR_SET_VMA_ANON_NAME 0
160160

161+
/* Control the ambient capability set */
162+
#define PR_CAP_AMBIENT 47
163+
# define PR_CAP_AMBIENT_IS_SET 1
164+
# define PR_CAP_AMBIENT_RAISE 2
165+
# define PR_CAP_AMBIENT_LOWER 3
166+
# define PR_CAP_AMBIENT_CLEAR_ALL 4
167+
161168
#endif /* _LINUX_PRCTL_H */

kernel/user_namespace.c

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
3838
cred->cap_inheritable = CAP_EMPTY_SET;
3939
cred->cap_permitted = CAP_FULL_SET;
4040
cred->cap_effective = CAP_FULL_SET;
41+
cred->cap_ambient = CAP_EMPTY_SET;
4142
cred->cap_bset = CAP_FULL_SET;
4243
#ifdef CONFIG_KEYS
4344
key_put(cred->request_key_auth);

security/commoncap.c

+92-10
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ int cap_capset(struct cred *new,
283283
new->cap_effective = *effective;
284284
new->cap_inheritable = *inheritable;
285285
new->cap_permitted = *permitted;
286+
287+
/*
288+
* Mask off ambient bits that are no longer both permitted and
289+
* inheritable.
290+
*/
291+
new->cap_ambient = cap_intersect(new->cap_ambient,
292+
cap_intersect(*permitted,
293+
*inheritable));
294+
if (WARN_ON(!cap_ambient_invariant_ok(new)))
295+
return -EINVAL;
286296
return 0;
287297
}
288298

@@ -363,6 +373,7 @@ static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
363373

364374
/*
365375
* pP' = (X & fP) | (pI & fI)
376+
* The addition of pA' is handled later.
366377
*/
367378
new->cap_permitted.cap[i] =
368379
(new->cap_bset.cap[i] & permitted) |
@@ -491,10 +502,13 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
491502
{
492503
const struct cred *old = current_cred();
493504
struct cred *new = bprm->cred;
494-
bool effective, has_cap = false;
505+
bool effective, has_cap = false, is_setid;
495506
int ret;
496507
kuid_t root_uid;
497508

509+
if (WARN_ON(!cap_ambient_invariant_ok(old)))
510+
return -EPERM;
511+
498512
effective = false;
499513
ret = get_file_caps(bprm, &effective, &has_cap);
500514
if (ret < 0)
@@ -539,8 +553,9 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
539553
*
540554
* In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
541555
*/
542-
if ((!uid_eq(new->euid, old->uid) ||
543-
!gid_eq(new->egid, old->gid) ||
556+
is_setid = !uid_eq(new->euid, old->uid) || !gid_eq(new->egid, old->gid);
557+
558+
if ((is_setid ||
544559
!cap_issubset(new->cap_permitted, old->cap_permitted)) &&
545560
bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
546561
/* downgrade; they get no more than they had, and maybe less */
@@ -556,10 +571,28 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
556571
new->suid = new->fsuid = new->euid;
557572
new->sgid = new->fsgid = new->egid;
558573

574+
/* File caps or setid cancels ambient. */
575+
if (has_cap || is_setid)
576+
cap_clear(new->cap_ambient);
577+
578+
/*
579+
* Now that we've computed pA', update pP' to give:
580+
* pP' = (X & fP) | (pI & fI) | pA'
581+
*/
582+
new->cap_permitted = cap_combine(new->cap_permitted, new->cap_ambient);
583+
584+
/*
585+
* Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
586+
* this is the same as pE' = (fE ? pP' : 0) | pA'.
587+
*/
559588
if (effective)
560589
new->cap_effective = new->cap_permitted;
561590
else
562-
cap_clear(new->cap_effective);
591+
new->cap_effective = new->cap_ambient;
592+
593+
if (WARN_ON(!cap_ambient_invariant_ok(new)))
594+
return -EPERM;
595+
563596
bprm->cap_effective = effective;
564597

565598
/*
@@ -574,7 +607,7 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
574607
* Number 1 above might fail if you don't have a full bset, but I think
575608
* that is interesting information to audit.
576609
*/
577-
if (!cap_isclear(new->cap_effective)) {
610+
if (!cap_issubset(new->cap_effective, new->cap_ambient)) {
578611
if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
579612
!uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
580613
issecure(SECURE_NOROOT)) {
@@ -585,6 +618,10 @@ int cap_bprm_set_creds(struct linux_binprm *bprm)
585618
}
586619

587620
new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
621+
622+
if (WARN_ON(!cap_ambient_invariant_ok(new)))
623+
return -EPERM;
624+
588625
return 0;
589626
}
590627

@@ -606,7 +643,7 @@ int cap_bprm_secureexec(struct linux_binprm *bprm)
606643
if (!uid_eq(cred->uid, root_uid)) {
607644
if (bprm->cap_effective)
608645
return 1;
609-
if (!cap_isclear(cred->cap_permitted))
646+
if (!cap_issubset(cred->cap_permitted, cred->cap_ambient))
610647
return 1;
611648
}
612649

@@ -708,10 +745,18 @@ static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
708745
uid_eq(old->suid, root_uid)) &&
709746
(!uid_eq(new->uid, root_uid) &&
710747
!uid_eq(new->euid, root_uid) &&
711-
!uid_eq(new->suid, root_uid)) &&
712-
!issecure(SECURE_KEEP_CAPS)) {
713-
cap_clear(new->cap_permitted);
714-
cap_clear(new->cap_effective);
748+
!uid_eq(new->suid, root_uid))) {
749+
if (!issecure(SECURE_KEEP_CAPS)) {
750+
cap_clear(new->cap_permitted);
751+
cap_clear(new->cap_effective);
752+
}
753+
754+
/*
755+
* Pre-ambient programs expect setresuid to nonroot followed
756+
* by exec to drop capabilities. We should make sure that
757+
* this remains the case.
758+
*/
759+
cap_clear(new->cap_ambient);
715760
}
716761
if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
717762
cap_clear(new->cap_effective);
@@ -943,6 +988,43 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
943988
new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
944989
goto changed;
945990

991+
case PR_CAP_AMBIENT:
992+
if (arg2 == PR_CAP_AMBIENT_CLEAR_ALL) {
993+
if (arg3 | arg4 | arg5)
994+
return -EINVAL;
995+
996+
new = prepare_creds();
997+
if (!new)
998+
return -ENOMEM;
999+
cap_clear(new->cap_ambient);
1000+
return commit_creds(new);
1001+
}
1002+
1003+
if (((!cap_valid(arg3)) | arg4 | arg5))
1004+
return -EINVAL;
1005+
1006+
if (arg2 == PR_CAP_AMBIENT_IS_SET) {
1007+
return !!cap_raised(current_cred()->cap_ambient, arg3);
1008+
} else if (arg2 != PR_CAP_AMBIENT_RAISE &&
1009+
arg2 != PR_CAP_AMBIENT_LOWER) {
1010+
return -EINVAL;
1011+
} else {
1012+
if (arg2 == PR_CAP_AMBIENT_RAISE &&
1013+
(!cap_raised(current_cred()->cap_permitted, arg3) ||
1014+
!cap_raised(current_cred()->cap_inheritable,
1015+
arg3)))
1016+
return -EPERM;
1017+
1018+
new = prepare_creds();
1019+
if (!new)
1020+
return -ENOMEM;
1021+
if (arg2 == PR_CAP_AMBIENT_RAISE)
1022+
cap_raise(new->cap_ambient, arg3);
1023+
else
1024+
cap_lower(new->cap_ambient, arg3);
1025+
return commit_creds(new);
1026+
}
1027+
9461028
default:
9471029
/* No functionality available - continue with default */
9481030
error = -ENOSYS;

security/keys/process_keys.c

+1
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ void key_change_session_keyring(struct callback_head *twork)
847847
new->cap_inheritable = old->cap_inheritable;
848848
new->cap_permitted = old->cap_permitted;
849849
new->cap_effective = old->cap_effective;
850+
new->cap_ambient = old->cap_ambient;
850851
new->cap_bset = old->cap_bset;
851852

852853
new->jit_keyring = old->jit_keyring;

0 commit comments

Comments
 (0)