Skip to content

Commit ace57c9

Browse files
eparisJiri Slaby
authored andcommitted
CAPABILITIES: remove undefined caps from all processes
commit 7d8b6c6 upstream. This is effectively a revert of 7b9a7ec plus fixing it a different way... We found, when trying to run an application from an application which had dropped privs that the kernel does security checks on undefined capability bits. This was ESPECIALLY difficult to debug as those undefined bits are hidden from /proc/$PID/status. Consider a root application which drops all capabilities from ALL 4 capability sets. We assume, since the application is going to set eff/perm/inh from an array that it will clear not only the defined caps less than CAP_LAST_CAP, but also the higher 28ish bits which are undefined future capabilities. The BSET gets cleared differently. Instead it is cleared one bit at a time. The problem here is that in security/commoncap.c::cap_task_prctl() we actually check the validity of a capability being read. So any task which attempts to 'read all things set in bset' followed by 'unset all things set in bset' will not even attempt to unset the undefined bits higher than CAP_LAST_CAP. So the 'parent' will look something like: CapInh: 0000000000000000 CapPrm: 0000000000000000 CapEff: 0000000000000000 CapBnd: ffffffc000000000 All of this 'should' be fine. Given that these are undefined bits that aren't supposed to have anything to do with permissions. But they do... So lets now consider a task which cleared the eff/perm/inh completely and cleared all of the valid caps in the bset (but not the invalid caps it couldn't read out of the kernel). We know that this is exactly what the libcap-ng library does and what the go capabilities library does. They both leave you in that above situation if you try to clear all of you capapabilities from all 4 sets. If that root task calls execve() the child task will pick up all caps not blocked by the bset. The bset however does not block bits higher than CAP_LAST_CAP. So now the child task has bits in eff which are not in the parent. These are 'meaningless' undefined bits, but still bits which the parent doesn't have. The problem is now in cred_cap_issubset() (or any operation which does a subset test) as the child, while a subset for valid cap bits, is not a subset for invalid cap bits! So now we set durring commit creds that the child is not dumpable. Given it is 'more priv' than its parent. It also means the parent cannot ptrace the child and other stupidity. The solution here: 1) stop hiding capability bits in status This makes debugging easier! 2) stop giving any task undefined capability bits. it's simple, it you don't put those invalid bits in CAP_FULL_SET you won't get them in init and you won't get them in any other task either. This fixes the cap_issubset() tests and resulting fallout (which made the init task in a docker container untraceable among other things) 3) mask out undefined bits when sys_capset() is called as it might use ~0, ~0 to denote 'all capabilities' for backward/forward compatibility. This lets 'capsh --caps="all=eip" -- -c /bin/bash' run. 4) mask out undefined bit when we read a file capability off of disk as again likely all bits are set in the xattr for forward/backward compatibility. This lets 'setcap all+pe /bin/bash; /bin/bash' run Signed-off-by: Eric Paris <eparis@redhat.com> Reviewed-by: Kees Cook <keescook@chromium.org> Cc: Andrew Vagin <avagin@openvz.org> Cc: Andrew G. Morgan <morgan@kernel.org> Cc: Serge E. Hallyn <serge.hallyn@canonical.com> Cc: Kees Cook <keescook@chromium.org> Cc: Steve Grubb <sgrubb@redhat.com> Cc: Dan Walsh <dwalsh@redhat.com> Signed-off-by: James Morris <james.l.morris@oracle.com> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 3f06fd4 commit ace57c9

5 files changed

Lines changed: 13 additions & 12 deletions

File tree

‎fs/proc/array.c‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,11 @@ static void render_cap_t(struct seq_file *m, const char *header,
304304
seq_puts(m, header);
305305
CAP_FOR_EACH_U32(__capi) {
306306
seq_printf(m, "%08x",
307-
a->cap[(_KERNEL_CAPABILITY_U32S-1) - __capi]);
307+
a->cap[CAP_LAST_U32 - __capi]);
308308
}
309309
seq_putc(m, '\n');
310310
}
311311

312-
/* Remove non-existent capabilities */
313-
#define NORM_CAPS(v) (v.cap[CAP_TO_INDEX(CAP_LAST_CAP)] &= \
314-
CAP_TO_MASK(CAP_LAST_CAP + 1) - 1)
315-
316312
static inline void task_cap(struct seq_file *m, struct task_struct *p)
317313
{
318314
const struct cred *cred;
@@ -326,11 +322,6 @@ static inline void task_cap(struct seq_file *m, struct task_struct *p)
326322
cap_bset = cred->cap_bset;
327323
rcu_read_unlock();
328324

329-
NORM_CAPS(cap_inheritable);
330-
NORM_CAPS(cap_permitted);
331-
NORM_CAPS(cap_effective);
332-
NORM_CAPS(cap_bset);
333-
334325
render_cap_t(m, "CapInh:\t", &cap_inheritable);
335326
render_cap_t(m, "CapPrm:\t", &cap_permitted);
336327
render_cap_t(m, "CapEff:\t", &cap_effective);

‎include/linux/capability.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ extern const kernel_cap_t __cap_init_eff_set;
7878
# error Fix up hand-coded capability macro initializers
7979
#else /* HAND-CODED capability initializers */
8080

81+
#define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
82+
#define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
83+
8184
# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
82-
# define CAP_FULL_SET ((kernel_cap_t){{ ~0, ~0 }})
85+
# define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
8386
# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
8487
| CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
8588
CAP_FS_MASK_B1 } })

‎kernel/audit.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
14121412
audit_log_format(ab, " %s=", prefix);
14131413
CAP_FOR_EACH_U32(i) {
14141414
audit_log_format(ab, "%08x",
1415-
cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1415+
cap->cap[CAP_LAST_U32 - i]);
14161416
}
14171417
}
14181418

‎kernel/capability.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
268268
i++;
269269
}
270270

271+
effective.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
272+
permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
273+
inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
274+
271275
new = prepare_creds();
272276
if (!new)
273277
return -ENOMEM;

‎security/commoncap.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
421421
cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
422422
}
423423

424+
cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
425+
cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
426+
424427
return 0;
425428
}
426429

0 commit comments

Comments
 (0)