selinux: Implement userns_create hook
authorFrederick Lawler <fred@cloudflare.com>
Mon, 15 Aug 2022 16:20:28 +0000 (11:20 -0500)
committerPaul Moore <paul@paul-moore.com>
Tue, 16 Aug 2022 21:44:44 +0000 (17:44 -0400)
Unprivileged user namespace creation is an intended feature to enable
sandboxing, however this feature is often used to as an initial step to
perform a privilege escalation attack.

This patch implements a new user_namespace { create } access control
permission to restrict which domains allow or deny user namespace
creation. This is necessary for system administrators to quickly protect
their systems while waiting for vulnerability patches to be applied.

This permission can be used in the following way:

        allow domA_t domA_t : user_namespace { create };

Signed-off-by: Frederick Lawler <fred@cloudflare.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
security/selinux/hooks.c
security/selinux/include/classmap.h

index 79573504783bb7f34f698ee9ef1b76c0f063c06a..b9f1078450b3a303dfa8c69b24fecfbe07483e49 100644 (file)
@@ -4221,6 +4221,14 @@ static void selinux_task_to_inode(struct task_struct *p,
        spin_unlock(&isec->lock);
 }
 
+static int selinux_userns_create(const struct cred *cred)
+{
+       u32 sid = current_sid();
+
+       return avc_has_perm(&selinux_state, sid, sid, SECCLASS_USER_NAMESPACE,
+                                               USER_NAMESPACE__CREATE, NULL);
+}
+
 /* Returns error only if unable to parse addresses */
 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
                        struct common_audit_data *ad, u8 *proto)
@@ -7111,6 +7119,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
        LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
        LSM_HOOK_INIT(task_kill, selinux_task_kill),
        LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
+       LSM_HOOK_INIT(userns_create, selinux_userns_create),
 
        LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
        LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
index ff757ae5f25379ee7fb49887abc0524715a7661b..0bff55bb9cde6df4dfe051cf0cba1814adabf7f0 100644 (file)
@@ -254,6 +254,8 @@ const struct security_class_mapping secclass_map[] = {
          { COMMON_FILE_PERMS, NULL } },
        { "io_uring",
          { "override_creds", "sqpoll", NULL } },
+       { "user_namespace",
+         { "create", NULL } },
        { NULL }
   };