selinux: add unprivileged sandboxing capability
Stephen Smalley
maintainerRFC PATCH 1/1 selinux: add unprivileged sandboxing capability
From: Stephen Smalley <[email protected]>
This is an RFC on an early prototype of a new capability for SELinux to support unprivileged (or at least less privileged) sandboxing. This was developed by an intern whose internship has now completed and we are posting it as a RFC to gather feedback especially on the concept, design, userspace/policy and kernel interfaces, although implementation feedback is also welcome.
Currently it is not feasible for applications to opt into further restrictions via SELinux except by switching or transitioning into security contexts that are already predefined in the base policy, or by toggling booleans that likewise must be predefined in the base policy along with the associated conditional rules. The type hierarchy / typebounds mechanism has proven to be unwieldy due to the strict hierarchical restrictions, the need to explicitly define everything allowed to every descendant domain, and the lack of any interfaces for dynamically introducing bounded types by unprivileged programs. SELinux namespaces offer a potential alternative path toward unprivileged sandboxing via SELinux but substantial work remains to making them complete and practical, and they are more oriented toward the container use case (e.g. requiring loading of a complete SELinux policy within the namespace) than allowing an individual application to sandbox itself or one of its child processes.
While others are pursuing unprivileged sandboxing through separate LSMs (e.g. Landlock), the goal of this work is to reuse the existing mechanisms and policy language of SELinux as much as possible, permitting applications to take advantage of SELinux's existing facilities and easing analysis and validation of the composition of the mandatory system-wide policy and the discretionary application sandbox policy. This is not to meant to preclude or interfere with other LSMs like Landlock but merely to offer another option for those who wish to use it. Other LSMs (AppArmor, Smack) already have their own mechanisms for processes to apply further restrictions on top of their base policies.
This patch introduces the ability for a process to load sandbox policies to be enforced by the kernel on a specified context. To be permitted to take these actions, the base policy must allow the process to use the sandboxing interfaces exposed by selinuxfs (controlled by permission "sandbox" in the existing class "security", checked against the security initial SID context) and to load and/or unload a sandbox policy for the specified context (permissions "load_policy" and "unload_policy" in the new class "sandbox", checked against the specified context). The sandbox policy can only further restrict the context; it can never allow permissions beyond what the base policy allows.
While a sandbox policy may optionally be written as a whitelist ("allow mode") using allow statements as with regular SELinux policy, an alternative mode ("deny mode") exists for sandbox policies that supports blacklists written with deny statements. Given that the base policy already fully specifies the maximal whitelist for the context, the sandbox policy can then be limited to specifying the deltas via deny mode. These deny statements are limited to sandbox policies only, and only if the sandbox policy is built in deny mode.
Design
------
The goal was to keep the logic as simple as possible since the intent is to ultimately permit unprivileged (or less privileged) processes to load the sandbox policies into the kernel, and to reuse as much as possible of the existing SELinux userspace and kernel machinery for creating, loading, and enforcing policies. As a result, sandbox policies are designed as a minimal subset of the base policy, only containing what is necessary to support the allow or deny rules and their dependencies, and are compiled to a binary form that is a subset of the kernel binary policy, allowing reuse of large portions of the policy compiler and kernel policy loading logic. Further, sandbox policies are associated with a source/subject security context rather than with individual processes, allows us to keep all of our changes within the security server and reusing the AVC and the rest of the SELinux machinery for enforcement.
As a result, a binary sandbox policy consists of the following:
* security context (string)
* "allow" or "deny" mode
* commons symbol table
* classes symbol table
* types symbol table
* allow or deny rules
The security context designates the context to which the sandbox should be applied. The mode designates if the sandbox policy is specified as a whitelist ('allow' mode) or as a blacklist ('deny' mode). The string/value symbol table data associated with commons, classes, and types are used to correctly map the allow or deny rules to values used by the base policy.
Example
-------
Note, this example requires the separate selinux userspace patch to be applied, built, and installed to support the extensions to secilc and the new sebox command below.
$ sudo semodule -cE base $ sudo vi base.cil <add "sandbox" to (class security (....))> $ sudo semodule -i base.cil
$ cat ./definesandboxclass.cil (class sandbox (load_policy unload_policy)) (classorder (unordered sandbox)) $ sudo semodule -i definesandboxclass.cil
$ cat ./unconfinedcansandbox.cil ; allow unconfined_t to sandbox itself (allow unconfined_t unconfined_t (security (sandbox))) (allow unconfined_t unconfined_t (sandbox (load_policy unload_policy))) $ sudo semodule -i unconfinedcansandbox.cil
$ cat ./no-tmp.cil ; some boilerplate required by current limitations in secilc ; is omitted here to keep this text simple and readable, ; see the follow-up message for the full no-tmp.cil file. (class dir (read getattr open search)) (class file (getattr)) (classorder (dir file)) (type unconfined_t) (type tmp_t) (deny unconfined_t tmp_t (dir (read getattr open search))) (deny unconfined_t tmp_t (file (getattr)))
$ id -Z unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
$ ls /tmp/ systemd-private-123456789... tmp.12345678 ...
$ secilc --sandbox="unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023" --sandbox-deny -o ./no-tmp.sandbox ./no-tmp.cil
$ sebox --load ./no-tmp.sandbox
$ ls /tmp/ ls: cannot open directory '/tmp/': Permission denied
$ sebox --unload ./no-tmp.sandbox
$ ls /tmp/ systemd-private-123456789... tmp.12345678 ...
In real usage, one would load a sandbox for a different context than one's own context (e.g. a child) or one would load a sandbox that denies the ability to further load/unload the sandbox to prevent escaping the sandbox.
Advantages
----------
* The base policy must allow sandboxing before any unprivileged process is permitted to load or unload any sandbox policies. This means a system administrator can deny all sandboxing on the system, or allow select subjects to load/unload sandbox policies.
* Similarly, the base policy can specify which subjects another can sandbox. For example, a policy can designate that chrome_t can only sandbox chrome_renderer_t and nothing else.
* Sandbox policies are attached to contexts rather than individual processes, which is consistent with the SELinux model and allows reuse of the AVC and rest of the SELinux machinery in the kernel and facilitates polcy analysis/validation of the composition of policies.
* The addition of deny statements allows a sandbox policy to use the base policy as a starting point and remove only the permissions they no longer want within that sandbox. Additionally, a sandbox policy can be written in the traditional sense where all permissions are denied, unless specifically allowed.
Disadvantages/Limitations
-------------------------
* Currently no way to create a new type, all sandbox policies must use types that already exist in the base policy. This could be changed by extending the set of constructs supported by the sandbox policy, at the cost of greater complexity in the kernel.
* The sandbox is not tied to a process, it is tied to a context. As a result, the sandbox applies to any other processes in the same context and persists after a process terminates. Although the sandbox can be unloaded, the extra step to unload may be seen as a disadvantage especially when a process is attempting to sandbox themselves and they no longer have permissions to unload the sandbox.
* The use of a binary policy format with compiled allow/deny rules requires dynamically remapping the rules when the sandbox is loaded, and again if the base policy is reloaded to ensure consistency. The base policy reload case is not currently handled by this patch.
* This patch only allows one sandbox per context, loading a second sandbox replaces the previous sandbox. Similarly, this patch does NOT allow a single sandbox policy to be applied to multiple contexts. A separate policy would need to be compiled for each context.
* Minimal userspace support at this time; in particular, no support yet for programmatic creation of a sandbox policy by an application.
ToDo
----
* Locking and atomicity issues around sandbox load/unload and access.
* Handle xperms.
* Fix to use our own policydb struct populated from the sandbox symbol tables when loading the avtab to avoid problems with inconsistencies between the base policy and sandbox policy symbol tables.
* Handle base policy reload. When the base policy is reloaded, all sandbox policies need to be updated to match the values of the new policy, and the sandboxes need to be preserved.
* Make additional hashtab search functionality more generic.
* Not all access computations use entries from the sid table, e.g. the type bounds computations, so those access decisions would not have sandboxes applied to them. This needs to be fixed.
* Code review/audit especially of everything potentially exposed to unprivileged userspace.
--- security/selinux/Makefile | 3 +- security/selinux/include/classmap.h | 5 +- security/selinux/include/security.h | 6 + security/selinux/selinuxfs.c | 159 +++++++++++ security/selinux/ss/avtab.c | 27 +- security/selinux/ss/avtab.h | 10 +- security/selinux/ss/hashtab.c | 35 +++ security/selinux/ss/hashtab.h | 8 + security/selinux/ss/policydb.c | 42 +++ security/selinux/ss/policydb.h | 4 + security/selinux/ss/sandbox.c | 391 ++++++++++++++++++++++++++++ security/selinux/ss/sandbox.h | 24 ++ security/selinux/ss/services.c | 125 ++++++++- security/selinux/ss/sidtab.c | 27 ++ security/selinux/ss/sidtab.h | 6 + 15 files changed, 859 insertions(+), 13 deletions(-) create mode 100644 security/selinux/ss/sandbox.c create mode 100644 security/selinux/ss/sandbox.h
security/selinux/Makefile +2 -1
@@ -8,7 +8,8 @@ obj-$(CONFIG_SECURITY_SELINUX) := selinux.o selinux-y := avc.o hooks.o selinuxfs.o netlink.o nlmsgtab.o netif.o \ netnode.o netport.o status.o \ ss/ebitmap.o ss/hashtab.o ss/symtab.o ss/sidtab.o ss/avtab.o \ - ss/policydb.o ss/services.o ss/conditional.o ss/mls.o + ss/policydb.o ss/services.o ss/conditional.o ss/mls.o \ + ss/sandbox.o selinux-$(CONFIG_SECURITY_NETWORK_XFRM) += xfrm.o
security/selinux/include/classmap.h +4 -1
@@ -42,7 +42,8 @@ struct security_class_mapping secclass_map[] = { {"compute_av" ,"compute_create" ,"compute_member" ,"check_context" ,"load_policy" ,"compute_relabel" ,"compute_user" ,"setenforce" ,"setbool" ,"setsecparam" , - "setcheckreqprot", "read_policy", "validate_trans", NULL } }, +"setcheckreqprot" ,"read_policy" ,"validate_trans" , + "sandbox", NULL } }, { "process", { "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",
@@ -248,6 +249,8 @@ struct security_class_mapping secclass_map[] = { {"open", "cpu", "kernel", "tracepoint", "read", "write"} }, {"lockdown" , { "integrity", "confidentiality", NULL } }, + {"sandbox" , + { "load_policy", "unload_policy"} }, { NULL } };
security/selinux/include/security.h +6 -0
@@ -231,6 +231,12 @@ size_t security_policydb_len(struct selinux_state *state);int security_policycap_supported (struct selinux_state *state,unsigned int req_cap); +int security_sandbox_load (struct selinux_state *state,u32 ssid, +void *data,size_t len); +int security_sandbox_unload (struct selinux_state *state,u32 ssid, +void *data,size_t len); +int security_sandbox_list (struct selinux_state *state); +#define SEL_VEC_MAX 32struct av_decision {u32 allowed;
security/selinux/selinuxfs.c +159 -0
171 lines not on this page · raw message
security/selinux/ss/avtab.c +26 -1
@@ -103,7 +103,8 @@ avtab_insert_node(struct avtab *h, int hvalue,return newnode; } -static int avtab_insert (struct avtab *h,struct avtab_key *key,struct avtab_datum *datum) +int avtab_insert (struct avtab *h,struct avtab_key *key, +struct avtab_datum *datum) {int hvalue;struct avtab_node *prev, *cur, *newnode;
@@ -375,6 +376,7 @@ static uint16_t spec_order[] = {AVTAB_ALLOWED ,AVTAB_AUDITDENY ,AVTAB_AUDITALLOW , +AVTAB_SANDBOXDENY ,AVTAB_TRANSITION ,AVTAB_CHANGE , AVTAB_MEMBER,
@@ -654,6 +656,29 @@ int avtab_write(struct policydb *p, struct avtab *a, void *fp)return rc; } +int avtab_map (struct avtab *h, +int (*apply)(struct avtab_key *k,struct avtab_datum *d, +void *args), +void *args) +{ +int i; +int rc; +struct avtab_node *cur; + +if (!h) +return 0 ; + +for (i =0 ; i < h->nslot; i++) { +for (cur = h->htable[i]; cur; cur = cur->next) { + rc =apply (&cur->key, &cur->datum, args); +if (rc) +return rc; + } + } + +return 0 ; +} +void __init avtab_cache_init (void ) { avtab_node_cachep = kmem_cache_create("avtab_node",
security/selinux/ss/avtab.h +9 -1
@@ -30,7 +30,9 @@ struct avtab_key {#define AVTAB_ALLOWED 0x0001#define AVTAB_AUDITALLOW 0x0002#define AVTAB_AUDITDENY 0x0004 -#define AVTAB_AV (AVTAB_ALLOWED | AVTAB_AUDITALLOW | AVTAB_AUDITDENY) +#define AVTAB_SANDBOXDENY 0x1000 +#define AVTAB_AV (AVTAB_ALLOWED | AVTAB_AUDITALLOW | \ + AVTAB_AUDITDENY | AVTAB_SANDBOXDENY)#define AVTAB_TRANSITION 0x0010#define AVTAB_MEMBER 0x0020#define AVTAB_CHANGE 0x0040
@@ -105,10 +107,16 @@ int avtab_write(struct policydb *p, struct avtab *a, void *fp);struct avtab_node *avtab_insert_nonunique (struct avtab *h,struct avtab_key *key,struct avtab_datum *datum); +int avtab_insert (struct avtab *h,struct avtab_key *key, +struct avtab_datum *datum);struct avtab_node *avtab_search_node (struct avtab *h,struct avtab_key *key);struct avtab_node *avtab_search_node_next (struct avtab_node *node,int specified); +int avtab_map (struct avtab *h, +int (*apply)(struct avtab_key *k,struct avtab_datum *d, +void *args), +void *args);#define MAX_AVTAB_HASH_BITS 16#define MAX_AVTAB_HASH_BUCKETS (1 << MAX_AVTAB_HASH_BITS)
security/selinux/ss/hashtab.c +35 -0
@@ -116,6 +116,41 @@ void *hashtab_search(struct hashtab *h, const void *key)return cur->datum; } +struct hashtab_node *hashtab_rsearch (struct hashtab *h, +int (*cmp_f)(void *d,u32 value), +u32 value) +{ +struct hashtab_node *cur; +int i; + +if (!h) +return NULL ; + +for (i =0 ; i < h->size; i++) { + cur = h->htable[i]; +while (cur) { +if (cmp_f (cur->datum, value) ==0 ) +return cur; + cur = cur->next; + } + } + +return NULL ; + +} + +void *hashtab_compare_search (struct hashtab *base,struct hashtab *child, +int (cmp_f )(void *d,u32 value), +u32 value) +{ +struct hashtab_node *node; + + node =hashtab_rsearch (child, cmp_f, value); +if (!node) +return NULL ; +return hashtab_search (base, node->key); +} +void hashtab_destroy (struct hashtab *h) {u32 i;
security/selinux/ss/hashtab.h +8 -0
@@ -62,6 +62,14 @@ int hashtab_insert(struct hashtab *h, void *k, void *d);*/ void *hashtab_search (struct hashtab *h,const void *k); +struct hashtab_node *hashtab_rsearch (struct hashtab *h, +int (*cmp_f)(void *d,u32 value), +u32 value); + +void *hashtab_compare_search (struct hashtab *base,struct hashtab *child, +int (*cmp_f)(void *d,u32 value), +u32 value); +/* * Destroys the specified hash table. */
security/selinux/ss/policydb.c +42 -0
48 lines not on this page · raw message
security/selinux/ss/policydb.h +4 -0
@@ -29,6 +29,7 @@#include "mls_types.h" #include "context.h" #include "constraint.h" +#include "sandbox.h" /* * A datum type is defined for each kind of symbol
@@ -320,6 +321,9 @@ extern int policydb_type_isvalid(struct policydb *p, unsigned int type);extern int policydb_role_isvalid (struct policydb *p,unsigned int role);extern int policydb_read (struct policydb *p,void *fp);extern int policydb_write (struct policydb *p,void *fp); +extern int policydb_read_symbols (struct policydb *p,struct symtab *s, +int symbol_index,void *fp); +extern void policydb_destroy_symbols (struct symtab *s,int symbol_index);#define POLICYDB_CONFIG_MLS 1
security/selinux/ss/sandbox.c new file +391 -0
391 lines not on this page · raw message
security/selinux/ss/sandbox.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _SS_SANDBOX_H_ +#define _SS_SANDBOX_H_ + +#include "security.h" +#include "symtab.h" +#include "avtab.h" + +#define SANDBOX_MODE_ALLOW 0x00 +#define SANDBOX_MODE_DENY 0x01 + +struct sandbox { +char *context_str; +u32 context_strlen; +u32 mode; +struct avtab te; +}; + +int sandbox_load (struct selinux_state *state,u32 ssid,void *fp); +int sandbox_unload (struct selinux_state *state,u32 ssid,void *fp); +void sandbox_destroy (struct sandbox *s); + +#endif /* _SS_SANDBOX_H_ */
security/selinux/ss/services.c +116 -9
219 lines not on this page · raw message
security/selinux/ss/sidtab.c +27 -0
@@ -88,6 +88,7 @@ int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context)#if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE >0 isid->entry.cache =NULL ;#endif + isid->entry.sandbox =NULL ; isid->set =1 ;/*
@@ -292,6 +293,7 @@ int sidtab_context_to_sid(struct sidtab *s, struct context *context, goto out_unlock; dst->sid =index_to_sid (count); + dst->sandbox =NULL ; rc =context_cpy (&dst->context, context);if (rc)
@@ -465,6 +467,7 @@ static void sidtab_destroy_entry(struct sidtab_entry *entry)#if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE >0 kfree (rcu_dereference_raw (entry->cache));#endif +sandbox_destroy (entry->sandbox); }static void sidtab_destroy_tree (union sidtab_entry_inner entry,u32 level)
@@ -595,4 +598,28 @@ int sidtab_sid2str_get(struct sidtab *s, struct sidtab_entry *entry, return rc; } +int sidtab_sandbox_attach (struct sidtab_entry *entry, +struct sandbox *sandbox) +{ +if (!entry) +return -EINVAL ; + + entry->sandbox = sandbox; + +return 0 ; +} + +struct sandbox *sidtab_sandbox_detach (struct sidtab_entry *entry) +{ +struct sandbox *sandbox; + +if (!entry) +return NULL ; + + sandbox = entry->sandbox; + entry->sandbox =NULL ; + +return sandbox; +} +#endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */
security/selinux/ss/sidtab.h +6 -0
@@ -16,6 +16,7 @@#include <linux/hashtable.h> #include "context.h" +#include "sandbox.h" struct sidtab_entry {u32 sid;
@@ -24,6 +25,7 @@ struct sidtab_entry {struct sidtab_str_cache __rcu *cache;#endif struct hlist_node list; +struct sandbox *sandbox; };union sidtab_entry_inner {
@@ -147,6 +149,10 @@ static inline int sidtab_sid2str_get(struct sidtab *s, }#endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */ +int sidtab_sandbox_attach (struct sidtab_entry *entry, +struct sandbox *sandbox); +struct sandbox *sidtab_sandbox_detach (struct sidtab_entry *entry); +#endif /* _SS_SIDTAB_H_ */
Signature · 1 line
2.17.1
Stephen Smalley
maintainerQuoted 22 lines · Stephen Smalley
Example
-------
Note, this example requires the separate selinux userspace patch to be applied, built, and installed to support the extensions to secilc and the new sebox command below.$ sudo semodule -cE base $ sudo vi base.cil <add "sandbox" to (class security (....))> $ sudo semodule -i base.cil$ cat ./definesandboxclass.cil (class sandbox (load_policy unload_policy)) (classorder (unordered sandbox)) $ sudo semodule -i definesandboxclass.cil$ cat ./unconfinedcansandbox.cil ; allow unconfined_t to sandbox itself (allow unconfined_t unconfined_t (security (sandbox))) (allow unconfined_t unconfined_t (sandbox (load_policy unload_policy))) $ sudo semodule -i unconfinedcansandbox.cil
Quoted 11 lines
$ cat ./no-tmp.cil ; some boilerplate required by current limitations in secilc ; is omitted here to keep this text simple and readable, ; see the follow-up message for the full no-tmp.cil file. (class dir (read getattr open search)) (class file (getattr)) (classorder (dir file)) (type unconfined_t) (type tmp_t) (deny unconfined_t tmp_t (dir (read getattr open search))) (deny unconfined_t tmp_t (file (getattr)))
Attached is the complete no-tmp.cil file.
The boilerplate is just a reflection of the current prototype status of the implementation
and it should be possible to omit it in the future.