aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Smalley <sds@tycho.nsa.gov>2017-07-31 10:12:46 -0400
committerPaul Moore <paul@paul-moore.com>2017-08-02 16:36:04 -0400
commitaf63f4193f9fbbbac50fc766417d74735afd87ef (patch)
tree60aa058f4d6afd03bc09cfa1c0433691e2c90ce8
parent20a8d62eeff844a8624d6b58a0227c057b1aa43f (diff)
selinux: Generalize support for NNP/nosuid SELinux domain transitions
As systemd ramps up enabling NNP (NoNewPrivileges) for system services, it is increasingly breaking SELinux domain transitions for those services and their descendants. systemd enables NNP not only for services whose unit files explicitly specify NoNewPrivileges=yes but also for services whose unit files specify any of the following options in combination with running without CAP_SYS_ADMIN (e.g. specifying User= or a CapabilityBoundingSet= without CAP_SYS_ADMIN): SystemCallFilter=, SystemCallArchitectures=, RestrictAddressFamilies=, RestrictNamespaces=, PrivateDevices=, ProtectKernelTunables=, ProtectKernelModules=, MemoryDenyWriteExecute=, or RestrictRealtime= as per the systemd.exec(5) man page. The end result is bad for the security of both SELinux-disabled and SELinux-enabled systems. Packagers have to turn off these options in the unit files to preserve SELinux domain transitions. For users who choose to disable SELinux, this means that they miss out on at least having the systemd-supported protections. For users who keep SELinux enabled, they may still be missing out on some protections because it isn't necessarily guaranteed that the SELinux policy for that service provides the same protections in all cases. commit 7b0d0b40cd78 ("selinux: Permit bounded transitions under NO_NEW_PRIVS or NOSUID.") allowed bounded transitions under NNP in order to support limited usage for sandboxing programs. However, defining typebounds for all of the affected service domains is impractical to implement in policy, since typebounds requires us to ensure that each domain is allowed everything all of its descendant domains are allowed, and this has to be repeated for the entire chain of domain transitions. There is no way to clone all allow rules from descendants to their ancestors in policy currently, and doing so would be undesirable even if it were practical, as it requires leaking permissions to objects and operations into ancestor domains that could weaken their own security in order to allow them to the descendants (e.g. if a descendant requires execmem permission, then so do all of its ancestors; if a descendant requires execute permission to a file, then so do all of its ancestors; if a descendant requires read to a symbolic link or temporary file, then so do all of its ancestors...). SELinux domains are intentionally not hierarchical / bounded in this manner normally, and making them so would undermine their protections and least privilege. We have long had a similar tension with SELinux transitions and nosuid mounts, albeit not as severe. Users often have had to choose between retaining nosuid on a mount and allowing SELinux domain transitions on files within those mounts. This likewise leads to unfortunate tradeoffs in security. Decouple NNP/nosuid from SELinux transitions, so that we don't have to make a choice between them. Introduce a nnp_nosuid_transition policy capability that enables transitions under NNP/nosuid to be based on a permission (nnp_transition for NNP; nosuid_transition for nosuid) between the old and new contexts in addition to the current support for bounded transitions. Domain transitions can then be allowed in policy without requiring the parent to be a strict superset of all of its children. With this change, systemd unit files can be left unmodified from upstream. SELinux-disabled and SELinux-enabled users will benefit from retaining any of the systemd-provided protections. SELinux policy will only need to be adapted to enable the new policy capability and to allow the new permissions between domain pairs as appropriate. NB: Allowing nnp_transition between two contexts opens up the potential for the old context to subvert the new context by installing seccomp filters before the execve. Allowing nosuid_transition between two contexts opens up the potential for a context transition to occur on a file from an untrusted filesystem (e.g. removable media or remote filesystem). Use with care. Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Paul Moore <paul@paul-moore.com>
-rw-r--r--security/selinux/hooks.c47
-rw-r--r--security/selinux/include/classmap.h2
-rw-r--r--security/selinux/include/security.h2
-rw-r--r--security/selinux/ss/services.c7
4 files changed, 42 insertions, 16 deletions
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 00ad46e166f6..04b8e1082c9a 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2318,6 +2318,7 @@ static int check_nnp_nosuid(const struct linux_binprm *bprm,
2318 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2318 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2319 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 2319 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
2320 int rc; 2320 int rc;
2321 u32 av;
2321 2322
2322 if (!nnp && !nosuid) 2323 if (!nnp && !nosuid)
2323 return 0; /* neither NNP nor nosuid */ 2324 return 0; /* neither NNP nor nosuid */
@@ -2326,24 +2327,40 @@ static int check_nnp_nosuid(const struct linux_binprm *bprm,
2326 return 0; /* No change in credentials */ 2327 return 0; /* No change in credentials */
2327 2328
2328 /* 2329 /*
2329 * The only transitions we permit under NNP or nosuid 2330 * If the policy enables the nnp_nosuid_transition policy capability,
2330 * are transitions to bounded SIDs, i.e. SIDs that are 2331 * then we permit transitions under NNP or nosuid if the
2331 * guaranteed to only be allowed a subset of the permissions 2332 * policy allows the corresponding permission between
2332 * of the current SID. 2333 * the old and new contexts.
2333 */ 2334 */
2334 rc = security_bounded_transition(old_tsec->sid, new_tsec->sid); 2335 if (selinux_policycap_nnp_nosuid_transition) {
2335 if (rc) { 2336 av = 0;
2336 /*
2337 * On failure, preserve the errno values for NNP vs nosuid.
2338 * NNP: Operation not permitted for caller.
2339 * nosuid: Permission denied to file.
2340 */
2341 if (nnp) 2337 if (nnp)
2342 return -EPERM; 2338 av |= PROCESS2__NNP_TRANSITION;
2343 else 2339 if (nosuid)
2344 return -EACCES; 2340 av |= PROCESS2__NOSUID_TRANSITION;
2341 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2342 SECCLASS_PROCESS2, av, NULL);
2343 if (!rc)
2344 return 0;
2345 } 2345 }
2346 return 0; 2346
2347 /*
2348 * We also permit NNP or nosuid transitions to bounded SIDs,
2349 * i.e. SIDs that are guaranteed to only be allowed a subset
2350 * of the permissions of the current SID.
2351 */
2352 rc = security_bounded_transition(old_tsec->sid, new_tsec->sid);
2353 if (!rc)
2354 return 0;
2355
2356 /*
2357 * On failure, preserve the errno values for NNP vs nosuid.
2358 * NNP: Operation not permitted for caller.
2359 * nosuid: Permission denied to file.
2360 */
2361 if (nnp)
2362 return -EPERM;
2363 return -EACCES;
2347} 2364}
2348 2365
2349static int selinux_bprm_set_creds(struct linux_binprm *bprm) 2366static int selinux_bprm_set_creds(struct linux_binprm *bprm)
diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
index b9fe3434b036..35ffb29a69cb 100644
--- a/security/selinux/include/classmap.h
+++ b/security/selinux/include/classmap.h
@@ -48,6 +48,8 @@ struct security_class_mapping secclass_map[] = {
48 "setrlimit", "rlimitinh", "dyntransition", "setcurrent", 48 "setrlimit", "rlimitinh", "dyntransition", "setcurrent",
49 "execmem", "execstack", "execheap", "setkeycreate", 49 "execmem", "execstack", "execheap", "setkeycreate",
50 "setsockcreate", "getrlimit", NULL } }, 50 "setsockcreate", "getrlimit", NULL } },
51 { "process2",
52 { "nnp_transition", "nosuid_transition", NULL } },
51 { "system", 53 { "system",
52 { "ipc_info", "syslog_read", "syslog_mod", 54 { "ipc_info", "syslog_read", "syslog_mod",
53 "syslog_console", "module_request", "module_load", NULL } }, 55 "syslog_console", "module_request", "module_load", NULL } },
diff --git a/security/selinux/include/security.h b/security/selinux/include/security.h
index e91f08c16c0b..3e323179159a 100644
--- a/security/selinux/include/security.h
+++ b/security/selinux/include/security.h
@@ -73,6 +73,7 @@ enum {
73 POLICYDB_CAPABILITY_EXTSOCKCLASS, 73 POLICYDB_CAPABILITY_EXTSOCKCLASS,
74 POLICYDB_CAPABILITY_ALWAYSNETWORK, 74 POLICYDB_CAPABILITY_ALWAYSNETWORK,
75 POLICYDB_CAPABILITY_CGROUPSECLABEL, 75 POLICYDB_CAPABILITY_CGROUPSECLABEL,
76 POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
76 __POLICYDB_CAPABILITY_MAX 77 __POLICYDB_CAPABILITY_MAX
77}; 78};
78#define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1) 79#define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
@@ -84,6 +85,7 @@ extern int selinux_policycap_openperm;
84extern int selinux_policycap_extsockclass; 85extern int selinux_policycap_extsockclass;
85extern int selinux_policycap_alwaysnetwork; 86extern int selinux_policycap_alwaysnetwork;
86extern int selinux_policycap_cgroupseclabel; 87extern int selinux_policycap_cgroupseclabel;
88extern int selinux_policycap_nnp_nosuid_transition;
87 89
88/* 90/*
89 * type_datum properties 91 * type_datum properties
diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c
index 2f02fa67ec2e..16c55de21b9f 100644
--- a/security/selinux/ss/services.c
+++ b/security/selinux/ss/services.c
@@ -76,7 +76,8 @@ char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
76 "open_perms", 76 "open_perms",
77 "extended_socket_class", 77 "extended_socket_class",
78 "always_check_network", 78 "always_check_network",
79 "cgroup_seclabel" 79 "cgroup_seclabel",
80 "nnp_nosuid_transition"
80}; 81};
81 82
82int selinux_policycap_netpeer; 83int selinux_policycap_netpeer;
@@ -84,6 +85,7 @@ int selinux_policycap_openperm;
84int selinux_policycap_extsockclass; 85int selinux_policycap_extsockclass;
85int selinux_policycap_alwaysnetwork; 86int selinux_policycap_alwaysnetwork;
86int selinux_policycap_cgroupseclabel; 87int selinux_policycap_cgroupseclabel;
88int selinux_policycap_nnp_nosuid_transition;
87 89
88static DEFINE_RWLOCK(policy_rwlock); 90static DEFINE_RWLOCK(policy_rwlock);
89 91
@@ -2009,6 +2011,9 @@ static void security_load_policycaps(void)
2009 selinux_policycap_cgroupseclabel = 2011 selinux_policycap_cgroupseclabel =
2010 ebitmap_get_bit(&policydb.policycaps, 2012 ebitmap_get_bit(&policydb.policycaps,
2011 POLICYDB_CAPABILITY_CGROUPSECLABEL); 2013 POLICYDB_CAPABILITY_CGROUPSECLABEL);
2014 selinux_policycap_nnp_nosuid_transition =
2015 ebitmap_get_bit(&policydb.policycaps,
2016 POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION);
2012 2017
2013 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++) 2018 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
2014 pr_info("SELinux: policy capability %s=%d\n", 2019 pr_info("SELinux: policy capability %s=%d\n",