Skip to content

Commit 8e4ff6f

Browse files
stephensmalleypcmoore
authored andcommitted
selinux: distinguish non-init user namespace capability checks
Distinguish capability checks against a target associated with the init user namespace versus capability checks against a target associated with a non-init user namespace by defining and using separate security classes for the latter. This is needed to support e.g. Chrome usage of user namespaces for the Chrome sandbox without needing to allow Chrome to also exercise capabilities on targets in the init user namespace. Suggested-by: Dan Walsh <dwalsh@redhat.com> Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 1ac4247 commit 8e4ff6f

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

security/selinux/hooks.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ static int current_has_perm(const struct task_struct *tsk,
16311631

16321632
/* Check whether a task is allowed to use a capability. */
16331633
static int cred_has_capability(const struct cred *cred,
1634-
int cap, int audit)
1634+
int cap, int audit, bool initns)
16351635
{
16361636
struct common_audit_data ad;
16371637
struct av_decision avd;
@@ -1645,10 +1645,10 @@ static int cred_has_capability(const struct cred *cred,
16451645

16461646
switch (CAP_TO_INDEX(cap)) {
16471647
case 0:
1648-
sclass = SECCLASS_CAPABILITY;
1648+
sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
16491649
break;
16501650
case 1:
1651-
sclass = SECCLASS_CAPABILITY2;
1651+
sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
16521652
break;
16531653
default:
16541654
printk(KERN_ERR
@@ -2152,7 +2152,7 @@ static int selinux_capset(struct cred *new, const struct cred *old,
21522152
static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
21532153
int cap, int audit)
21542154
{
2155-
return cred_has_capability(cred, cap, audit);
2155+
return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
21562156
}
21572157

21582158
static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
@@ -2230,7 +2230,7 @@ static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
22302230
int rc, cap_sys_admin = 0;
22312231

22322232
rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2233-
SECURITY_CAP_NOAUDIT);
2233+
SECURITY_CAP_NOAUDIT, true);
22342234
if (rc == 0)
22352235
cap_sys_admin = 1;
22362236

@@ -3213,7 +3213,7 @@ static int selinux_inode_getsecurity(struct inode *inode, const char *name, void
32133213
SECURITY_CAP_NOAUDIT);
32143214
if (!error)
32153215
error = cred_has_capability(current_cred(), CAP_MAC_ADMIN,
3216-
SECURITY_CAP_NOAUDIT);
3216+
SECURITY_CAP_NOAUDIT, true);
32173217
isec = inode_security(inode);
32183218
if (!error)
32193219
error = security_sid_to_context_force(isec->sid, &context,
@@ -3390,7 +3390,7 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd,
33903390
case KDSKBENT:
33913391
case KDSKBSENT:
33923392
error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3393-
SECURITY_CAP_AUDIT);
3393+
SECURITY_CAP_AUDIT, true);
33943394
break;
33953395

33963396
/* default case assumes that the command will go

security/selinux/include/classmap.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
#define COMMON_IPC_PERMS "create", "destroy", "getattr", "setattr", "read", \
1313
"write", "associate", "unix_read", "unix_write"
1414

15+
#define COMMON_CAP_PERMS "chown", "dac_override", "dac_read_search", \
16+
"fowner", "fsetid", "kill", "setgid", "setuid", "setpcap", \
17+
"linux_immutable", "net_bind_service", "net_broadcast", \
18+
"net_admin", "net_raw", "ipc_lock", "ipc_owner", "sys_module", \
19+
"sys_rawio", "sys_chroot", "sys_ptrace", "sys_pacct", "sys_admin", \
20+
"sys_boot", "sys_nice", "sys_resource", "sys_time", \
21+
"sys_tty_config", "mknod", "lease", "audit_write", \
22+
"audit_control", "setfcap"
23+
24+
#define COMMON_CAP2_PERMS "mac_override", "mac_admin", "syslog", \
25+
"wake_alarm", "block_suspend", "audit_read"
26+
1527
/*
1628
* Note: The name for any socket class should be suffixed by "socket",
1729
* and doesn't contain more than one substr of "socket".
@@ -34,14 +46,7 @@ struct security_class_mapping secclass_map[] = {
3446
{ "ipc_info", "syslog_read", "syslog_mod",
3547
"syslog_console", "module_request", "module_load", NULL } },
3648
{ "capability",
37-
{ "chown", "dac_override", "dac_read_search",
38-
"fowner", "fsetid", "kill", "setgid", "setuid", "setpcap",
39-
"linux_immutable", "net_bind_service", "net_broadcast",
40-
"net_admin", "net_raw", "ipc_lock", "ipc_owner", "sys_module",
41-
"sys_rawio", "sys_chroot", "sys_ptrace", "sys_pacct", "sys_admin",
42-
"sys_boot", "sys_nice", "sys_resource", "sys_time",
43-
"sys_tty_config", "mknod", "lease", "audit_write",
44-
"audit_control", "setfcap", NULL } },
49+
{ COMMON_CAP_PERMS, NULL } },
4550
{ "filesystem",
4651
{ "mount", "remount", "unmount", "getattr",
4752
"relabelfrom", "relabelto", "associate", "quotamod",
@@ -150,12 +155,15 @@ struct security_class_mapping secclass_map[] = {
150155
{ "memprotect", { "mmap_zero", NULL } },
151156
{ "peer", { "recv", NULL } },
152157
{ "capability2",
153-
{ "mac_override", "mac_admin", "syslog", "wake_alarm", "block_suspend",
154-
"audit_read", NULL } },
158+
{ COMMON_CAP2_PERMS, NULL } },
155159
{ "kernel_service", { "use_as_override", "create_files_as", NULL } },
156160
{ "tun_socket",
157161
{ COMMON_SOCK_PERMS, "attach_queue", NULL } },
158162
{ "binder", { "impersonate", "call", "set_context_mgr", "transfer",
159163
NULL } },
164+
{ "cap_userns",
165+
{ COMMON_CAP_PERMS, NULL } },
166+
{ "cap2_userns",
167+
{ COMMON_CAP2_PERMS, NULL } },
160168
{ NULL }
161169
};

0 commit comments

Comments
 (0)