Introduce a SELinux namespace
Subsystems (2)
cover Introduce a SELinux namespace
I normally wouldn't post these patches at this stage of development, but several people have requested them, so here they are. Note that they are very incomplete and unsafe and should not be used on any production systems. The first four patches should actually be safe, since they merely lay the groundwork of enabling selinux state to be namespaced, but the rest are not; specific known issues with each of them are noted in the patch descriptions. It isn't until the next to last patch that the facility is even exposed to userspace, and that patch description explains sample usage (as well as summarizing known issues). I am intentionally only sending this to the selinux list at the moment because I don't think it is ready for wider consumption and expect much of it to change or be completely replaced. I had some other patches in the works as well, but they were lost in a recent hardware failure so it will take some time to recover those.
Motivating use cases for a SELinux namespace include:
1. Enabling one to apply SELinux confinement within a container on a host that is itself using SELinux to enforce container isolation and confinement to host resources (svirt). For example, one might wish to isolate multiple services running within a container, or to enforce a W^X policy for a service running within a container. Today one is forced to treat the entire container as a single context and from within the container it appears that SELinux is disabled.
2. Supporting the ChromeOS use case of running an Android SELinux container when the host itself is not using SELinux. My impression is that the ChromeOS developers first tried hacking support for a per-pid-namespace SELinux enforcing mode into the kernel, and then later resorted to essentially running the ChromeOS processes in an unconfined or permissive domain while running the Android processes in their usual contexts; I don't know how this could have passed Android CTS however since the full policy would have been exposed to the Android instance via the single selinuxfs instance.
3. Running multiple Android instances on a single host, each with their own SELinux policy and enforcing mode, as in the Cells/Cellrox virtual smartphone platform.
4. Running Fedora or other SELinux-enabled systems with SELinux confinement enabled in containers on non-SELinux hosts.
It should be noted that in their current form, these patches do not yet support any of these use cases.
You can also find these patches in the following tree:
https://github.com/stephensmalley/selinux-kernel/tree/selinuxns
Use at your own risk. Enjoy!
Stephen Smalley (10):
selinux: introduce a selinux namespace
selinux: support multiple selinuxfs instances
selinux: move the AVC into the selinux namespace netns,selinux: create the selinux netlink socket per network namespace
selinux: support per-task/cred selinux namespace
selinux: introduce cred_selinux_ns() and use it
selinux: support per-namespace inode security structures
selinux: support per-namespace superblock security structures
selinux: add a selinuxfs interface to unshare selinux namespace
selinuxfs: restrict write operations to the same selinux namespace
include/net/net_namespace.h | 3 + security/selinux/avc.c | 290 ++++---- security/selinux/hooks.c | 884 ++++++++++++++++++------- security/selinux/ibpkey.c | 3 +- security/selinux/include/avc.h | 38 +- security/selinux/include/avc_ss.h | 9 +- security/selinux/include/classmap.h | 3 +- security/selinux/include/conditional.h | 11 +- security/selinux/include/objsec.h | 18 +- security/selinux/include/security.h | 231 +++++-- security/selinux/netif.c | 2 +- security/selinux/netlabel.c | 14 +- security/selinux/netlink.c | 31 +- security/selinux/netnode.c | 4 +- security/selinux/netport.c | 2 +- security/selinux/selinuxfs.c | 627 ++++++++++++------ security/selinux/ss/avtab.c | 9 +- security/selinux/ss/avtab.h | 3 - security/selinux/ss/ebitmap.c | 7 +- security/selinux/ss/ebitmap.h | 3 - security/selinux/ss/hashtab.c | 8 +- security/selinux/ss/hashtab.h | 4 - security/selinux/ss/mls.c | 72 +- security/selinux/ss/mls.h | 38 +- security/selinux/ss/services.c | 1126 ++++++++++++++++++-------------- security/selinux/ss/services.h | 23 +- security/selinux/ss/status.c | 47 +- security/selinux/xfrm.c | 23 +- 28 files changed, 2289 insertions(+), 1244 deletions(-)
Signature · 1 line
2.9.5
RFC 1/10 selinux: introduce a selinux namespace
Define a selinux namespace structure (struct selinux_ns) for SELinux state and pass it explicitly to all security server functions. The public portion of the structure contains state that is used throughout the SELinux code, such as the enforcing mode. The structure also contains a pointer to a selinux_ss structure whose definition is private to the security server and contains security server specific state such as the policy database and SID table.
This change allocates a single selinux namespace, the init_selinux_ns. It defines and passes a symbol for the current selinux namespace (current_selinux_ns) as a placeholder for future changes where multiple selinux namespaces will be supported, but in this change the current selinux namespace is always the init selinux namespace. Note that passing the current selinux namespace is not correct for all hooks; some hooks will need to be adjusted to pass the selinux namespace associated with an open file, a network namespace or socket, etc, since not all hooks are invoked in process context and some hooks operate in the context of a cred that may differ from current's cred. Fixing all of these cases is left to future changes, once we introduce the support for multiple selinux namespaces.
This change by itself should have no effect on SELinux behavior or APIs (userspace or LSM). It merely wraps SELinux state and passes it explicitly as needed.
--- security/selinux/avc.c | 13 +- security/selinux/hooks.c | 226 +++++-- security/selinux/ibpkey.c | 3 +- security/selinux/include/avc.h | 6 - security/selinux/include/avc_ss.h | 6 - security/selinux/include/conditional.h | 11 +- security/selinux/include/objsec.h | 2 - security/selinux/include/security.h | 213 +++++-- security/selinux/netif.c | 2 +- security/selinux/netlabel.c | 11 +- security/selinux/netnode.c | 4 +- security/selinux/netport.c | 2 +- security/selinux/selinuxfs.c | 122 ++-- security/selinux/ss/avtab.c | 9 +- security/selinux/ss/avtab.h | 3 - security/selinux/ss/ebitmap.c | 7 +- security/selinux/ss/ebitmap.h | 3 - security/selinux/ss/hashtab.c | 8 +- security/selinux/ss/hashtab.h | 4 - security/selinux/ss/mls.c | 72 ++- security/selinux/ss/mls.h | 38 +- security/selinux/ss/services.c | 1087 ++++++++++++++++++-------------- security/selinux/ss/services.h | 23 +- security/selinux/ss/status.c | 45 +- security/selinux/xfrm.c | 6 +- 25 files changed, 1163 insertions(+), 763 deletions(-)
security/selinux/avc.c +8 -5
@@ -149,7 +149,8 @@ static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclachar *scontext;u32 scontext_len; - rc =security_sid_to_context (ssid, &scontext, &scontext_len); + rc =security_sid_to_context (current_selinux_ns, ssid, + &scontext, &scontext_len);if (rc)audit_log_format (ab,"ssid=%d" , ssid);else {
@@ -157,7 +158,8 @@ static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclakfree (scontext); } - rc =security_sid_to_context (tsid, &scontext, &scontext_len); + rc =security_sid_to_context (current_selinux_ns, tsid, + &scontext, &scontext_len);if (rc)audit_log_format (ab," tsid=%d" , tsid);else {
@@ -969,7 +971,8 @@ static noinline struct avc_node *avc_compute_av(u32 ssid, u32 tsid, {rcu_read_unlock ();INIT_LIST_HEAD (&xp_node->xpd_head); -security_compute_av (ssid, tsid, tclass, avd, &xp_node->xp); +security_compute_av (current_selinux_ns, ssid, tsid, tclass, + avd, &xp_node->xp);rcu_read_lock ();return avc_insert (ssid, tsid, tclass, avd, xp_node); }
@@ -1043,8 +1046,8 @@ int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested, goto decision; }rcu_read_unlock (); -security_compute_xperms_decision (ssid, tsid, tclass, driver, - &local_xpd); +security_compute_xperms_decision (current_selinux_ns, ssid, tsid, + tclass, driver, &local_xpd);rcu_read_lock ();avc_update_node (AVC_CALLBACK_ADD_XPERMS , requested, driver, xperm, ssid, tsid, tclass, avd.seqno, &local_xpd,0 );
security/selinux/hooks.c +172 -54
519 lines not on this page · raw message
security/selinux/ibpkey.c +2 -1
@@ -152,7 +152,8 @@ static int sel_ib_pkey_sid_slow(u64 subnet_prefix, u16 pkey_num, u32 *sid)return 0 ; } - ret =security_ib_pkey_sid (subnet_prefix, pkey_num, sid); + ret =security_ib_pkey_sid (current_selinux_ns, subnet_prefix, pkey_num, + sid);if (ret) goto out;
security/selinux/include/avc.h +0 -6
@@ -19,12 +19,6 @@#include "av_permissions.h" #include "security.h" -#ifdef CONFIG_SECURITY_SELINUX_DEVELOP -extern int selinux_enforcing; -#else -#define selinux_enforcing 1 -#endif -/* * An entry in the AVC. */
security/selinux/include/avc_ss.h +0 -6
@@ -18,11 +18,5 @@ struct security_class_mapping {extern struct security_class_mapping secclass_map[]; -/* -* The security server must be initialized before -* any labeling or access decisions can be provided. -*/ -extern int ss_initialized; -#endif /* _SELINUX_AVC_SS_H_ */
security/selinux/include/conditional.h +8 -3
@@ -13,10 +13,15 @@#ifndef _SELINUX_CONDITIONAL_H_#define _SELINUX_CONDITIONAL_H_ -int security_get_bools (int *len,char ***names,int **values); +#include "security.h" -int security_set_bools (int len,int *values); +int security_get_bools (struct selinux_ns *ns, +int *len,char ***names,int **values); -int security_get_bool_value (int index); +int security_set_bools (struct selinux_ns *ns, +int len,int *values); + +int security_get_bool_value (struct selinux_ns *ns, +int index);#endif
security/selinux/include/objsec.h +0 -2
@@ -150,6 +150,4 @@ struct pkey_security_struct { u32 sid; /* SID of pkey */ }; -extern unsigned int selinux_checkreqprot; -#endif /* _SELINUX_OBJSEC_H_ */
security/selinux/include/security.h +154 -59
311 lines not on this page · raw message
security/selinux/netif.c +1 -1
@@ -163,7 +163,7 @@ static int sel_netif_sid_slow(struct net *ns, int ifindex, u32 *sid) ret = -ENOMEM ; goto out; } - ret =security_netif_sid (dev->name, &new->nsec.sid); + ret =security_netif_sid (current_selinux_ns, dev->name, &new->nsec.sid);if (ret !=0 ) goto out; new->nsec.ns = ns;
security/selinux/netlabel.c +7 -4
@@ -60,7 +60,7 @@ static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb, {int rc; - rc =security_netlbl_secattr_to_sid (secattr, sid); + rc =security_netlbl_secattr_to_sid (current_selinux_ns, secattr, sid);if (rc ==0 && (secattr->flags &NETLBL_SECATTR_CACHEABLE ) && (secattr->flags &NETLBL_SECATTR_CACHE ))
@@ -91,7 +91,8 @@ static struct netlbl_lsm_secattr *selinux_netlbl_sock_genattr(struct sock *sk) secattr =netlbl_secattr_alloc (GFP_ATOMIC );if (secattr ==NULL )return NULL ; - rc =security_netlbl_sid_to_secattr (sksec->sid, secattr); + rc =security_netlbl_sid_to_secattr (current_selinux_ns, sksec->sid, + secattr);if (rc !=0 ) {netlbl_secattr_free (secattr);return NULL ;
@@ -257,7 +258,8 @@ int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,if (secattr ==NULL ) { secattr = &secattr_storage;netlbl_secattr_init (secattr); - rc =security_netlbl_sid_to_secattr (sid, secattr); + rc =security_netlbl_sid_to_secattr (current_selinux_ns, sid, + secattr);if (rc !=0 ) goto skbuff_setsid_return; }
@@ -290,7 +292,8 @@ int selinux_netlbl_inet_conn_request(struct request_sock *req, u16 family)return 0 ;netlbl_secattr_init (&secattr); - rc =security_netlbl_sid_to_secattr (req->secid, &secattr); + rc =security_netlbl_sid_to_secattr (current_selinux_ns, req->secid, + &secattr);if (rc !=0 ) goto inet_conn_request_return; rc =netlbl_req_setattr (req, &secattr);
security/selinux/netnode.c +2 -2
@@ -215,12 +215,12 @@ static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid) goto out;switch (family) {case PF_INET : - ret =security_node_sid (PF_INET , + ret =security_node_sid (current_selinux_ns,PF_INET , addr,sizeof (struct in_addr ), sid); new->nsec.addr.ipv4 = *(__be32 *)addr;break ;case PF_INET6 : - ret =security_node_sid (PF_INET6 , + ret =security_node_sid (current_selinux_ns,PF_INET6 , addr,sizeof (struct in6_addr ), sid); new->nsec.addr.ipv6 = *(struct in6_addr *)addr;break ;
security/selinux/netport.c +1 -1
@@ -161,7 +161,7 @@ static int sel_netport_sid_slow(u8 protocol, u16 pnum, u32 *sid) new =kzalloc (sizeof (*new),GFP_ATOMIC );if (new ==NULL ) goto out; - ret =security_port_sid (protocol, pnum, sid); + ret =security_port_sid (current_selinux_ns, protocol, pnum, sid);if (ret !=0 ) goto out;
security/selinux/selinuxfs.c +69 -53
318 lines not on this page · raw message
security/selinux/ss/avtab.c +2 -7
@@ -655,7 +655,8 @@ int avtab_write(struct policydb *p, struct avtab *a, void *fp)return rc; } -void avtab_cache_init (void ) + +void __init avtab_cache_init (void ) { avtab_node_cachep = kmem_cache_create("avtab_node", sizeof(struct avtab_node),
@@ -664,9 +665,3 @@ void avtab_cache_init(void)sizeof (struct avtab_extended_perms ), 0, SLAB_PANIC, NULL); } - -void avtab_cache_destroy (void ) -{ -kmem_cache_destroy (avtab_node_cachep); -kmem_cache_destroy (avtab_xperms_cachep); -}
security/selinux/ss/avtab.h +0 -3
@@ -114,9 +114,6 @@ 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); -void avtab_cache_init (void ); -void avtab_cache_destroy (void ); -#define MAX_AVTAB_HASH_BITS 16#define MAX_AVTAB_HASH_BUCKETS (1 << MAX_AVTAB_HASH_BITS)
security/selinux/ss/ebitmap.c +1 -6
@@ -522,14 +522,9 @@ int ebitmap_write(struct ebitmap *e, void *fp)return 0 ; } -void ebitmap_cache_init (void ) +void __init ebitmap_cache_init (void ) { ebitmap_node_cachep =kmem_cache_create ("ebitmap_node" ,sizeof (struct ebitmap_node ),0 ,SLAB_PANIC ,NULL ); } - -void ebitmap_cache_destroy (void ) -{ -kmem_cache_destroy (ebitmap_node_cachep); -}
security/selinux/ss/ebitmap.h +0 -3
@@ -130,9 +130,6 @@ void ebitmap_destroy(struct ebitmap *e);int ebitmap_read (struct ebitmap *e,void *fp);int ebitmap_write (struct ebitmap *e,void *fp); -void ebitmap_cache_init (void ); -void ebitmap_cache_destroy (void ); -#ifdef CONFIG_NETLABEL int ebitmap_netlbl_export (struct ebitmap *ebmap,struct netlbl_lsm_catmap **catmap);
security/selinux/ss/hashtab.c +2 -6
@@ -168,14 +168,10 @@ void hashtab_stat(struct hashtab *h, struct hashtab_info *info) info->slots_used = slots_used; info->max_chain_len = max_chain_len; } -void hashtab_cache_init (void ) + +void __init hashtab_cache_init (void ) { hashtab_node_cachep =kmem_cache_create ("hashtab_node" ,sizeof (struct hashtab_node ),0 ,SLAB_PANIC ,NULL ); } - -void hashtab_cache_destroy (void ) -{ -kmem_cache_destroy (hashtab_node_cachep); -}
security/selinux/ss/hashtab.h +0 -4
@@ -84,8 +84,4 @@ int hashtab_map(struct hashtab *h,/* Fill info with some hash table statistics */ void hashtab_stat (struct hashtab *h,struct hashtab_info *info); -/* Use kmem_cache for hashtab_node */ -void hashtab_cache_init (void ); -void hashtab_cache_destroy (void ); -#endif /* _SS_HASHTAB_H */
security/selinux/ss/mls.c +40 -32
232 lines not on this page · raw message
security/selinux/ss/mls.h +25 -13
@@ -24,8 +24,9 @@#include "context.h" #include "policydb.h" -int mls_compute_context_len (struct context *context); -void mls_sid_to_context (struct context *context,char **scontext); +int mls_compute_context_len (struct policydb *p,struct context *context); +void mls_sid_to_context (struct policydb *p,struct context *context, +char **scontext);int mls_context_isvalid (struct policydb *p,struct context *c);int mls_range_isvalid (struct policydb *p,struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);
@@ -37,7 +38,8 @@ int mls_context_to_sid(struct policydb *p,struct sidtab *s, u32 def_sid); -int mls_from_string (char *str,struct context *context,gfp_t gfp_mask); +int mls_from_string (struct policydb *p,char *str,struct context *context, +gfp_t gfp_mask);int mls_range_set (struct context *context,struct mls_range *range);
@@ -45,42 +47,52 @@ int mls_convert_context(struct policydb *oldp,struct policydb *newp, struct context *context); -int mls_compute_sid (struct context *scontext, +int mls_compute_sid (struct policydb *p, +struct context *scontext,struct context *tcontext,u16 tclass,u32 specified,struct context *newcontext,bool sock); -int mls_setup_user_range (struct context *fromcon,struct user_datum *user, +int mls_setup_user_range (struct policydb *p, +struct context *fromcon,struct user_datum *user,struct context *usercon);#ifdef CONFIG_NETLABEL -void mls_export_netlbl_lvl (struct context *context, +void mls_export_netlbl_lvl (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr); -void mls_import_netlbl_lvl (struct context *context, +void mls_import_netlbl_lvl (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr); -int mls_export_netlbl_cat (struct context *context, +int mls_export_netlbl_cat (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr); -int mls_import_netlbl_cat (struct context *context, +int mls_import_netlbl_cat (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr);#else -static inline void mls_export_netlbl_lvl (struct context *context, +static inline void mls_export_netlbl_lvl (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr) {return ; } -static inline void mls_import_netlbl_lvl (struct context *context, +static inline void mls_import_netlbl_lvl (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr) {return ; } -static inline int mls_export_netlbl_cat (struct context *context, +static inline int mls_export_netlbl_cat (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr) {return -ENOMEM ; } -static inline int mls_import_netlbl_cat (struct context *context, +static inline int mls_import_netlbl_cat (struct policydb *p, +struct context *context,struct netlbl_lsm_secattr *secattr) {return -ENOMEM ;
security/selinux/ss/services.c +622 -465
1,305 lines not on this page · raw message
security/selinux/ss/status.c +22 -23
45 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.9.5
Part of the diff is not shown here. View the raw message.
Paul Moore
maintainerRe: [RFC 01/10] selinux: introduce a selinux namespace
Quoted 21 lines · Stephen Smalley
Define a selinux namespace structure (struct
selinux_ns) for SELinux state and pass it explicitly to all security server functions. The public portion of the structure contains state that is used throughout the SELinux code, such as the enforcing mode. The structure also contains a pointer to aselinux_ssstructure whose definition is private to the security server and contains security server specific state such as the policy database and SID table.This change allocates a single selinux namespace, the
init_selinux_ns. It defines and passes a symbol for the current selinux namespace (current_selinux_ns) as a placeholder for future changes where multiple selinux namespaces will be supported, but in this change the current selinux namespace is always the init selinux namespace. Note that passing the current selinux namespace is not correct for all hooks; some hooks will need to be adjusted to pass the selinux namespace associated with an open file, a network namespace or socket, etc, since not all hooks are invoked in process context and some hooks operate in the context of a cred that may differ from current's cred. Fixing all of these cases is left to future changes, once we introduce the support for multiple selinux namespaces.
Quoted 3 lines
This change by itself should have no effect on SELinux behavior or APIs (userspace or LSM). It merely wraps SELinux state and passes it explicitly as needed.
To put things in context, I'm looking at this patch not really with namespacing in mind, but rather with the idea of encapsulating the SELinux global state. Regardless of what we do with namespacing, I believe the encapsulation work still has value.
My comments are inline below. I'm going to try and trim out a lot of this patch in my reply as most of are trivial changes needed to pass around the new state pointers.
security/selinux/hooks.c line 100
Quoted 9 lines
security/selinux/hooks.c +1
-0@@ -97,20 +97,24 @@#include "audit.h" #include "avc_ss.h" +struct selinux_ns *init_selinux_ns;
This comment applies to more than just the particular line above, it really applies to the entire patch.
If we are going to merge this patch, and presumably a few others, independent of the namespacing work, I would strongly prefer if we used more general names instead of ones tied so closely to namespacing. For example, something along the lines of "struct selinux_state" would be more desirable than "struct selinux_ns"; it doesn't have to be "selinux_state", that was just the first thing I could come up with.
Quoted 35 lines
security/selinux/hooks.c +35
-0+static void selinux_ns_free (struct work_struct *work); + +int selinux_ns_create (struct selinux_ns *parent,struct selinux_ns **ns) +{ +struct selinux_ns *newns; +int rc; + + newns =kzalloc (sizeof (*newns),GFP_KERNEL ); +if (!newns) +return -ENOMEM ; + +refcount_set (&newns->count,1 ); +INIT_WORK (&newns->work, selinux_ns_free); + + rc =selinux_ss_create (&newns->ss); +if (rc) + goto err; + +if (parent) + newns->parent =get_selinux_ns (parent); + + *ns = newns; +return 0 ; +err: +kfree (newns); +return rc; +} + +static void selinux_ns_free (struct work_struct *work) +{ +struct selinux_ns *parent, *ns = + container_of(work, struct selinux_ns, work); + + do { + parent = ns->parent;
security/selinux/hooks.c static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · line 6572
Quoted 10 lines
security/selinux/hooks.c +10
-0+selinux_ss_free (ns->ss); +kfree (ns); + ns = parent; + }while (ns &&refcount_dec_and_test (&ns->count)); +} + +void __put_selinux_ns (struct selinux_ns *ns) +{ +schedule_work (&ns->work); +}
While we would obviously need something like this for proper namespacing, with only a single state struct we don't need all the state/namespace management.
However, I would still be willing to accept all the changes to pass around a reference to the global state struct, even if in this particular case it was always going to be single/init struct.
security/selinux/hooks.c static __init int selinux_init(void) · line 6589
Quoted 6 lines
security/selinux/hooks.c +2
-0@@ -6487,6 +6585,11 @@ static __init int selinux_init(void)printk (KERN_INFO "SELinux: Initializing.\n" ); +if (selinux_ns_create (NULL , &init_selinux_ns)) +panic ("SELinux: Could not create initial namespace\n" );
Not yet :) If we stick with selinux_ns_create(), see my comments above, at the very least we need to pick an error message that doesn't hint at namespacing.
Quoted 22 lines
security/selinux/hooks.c +4
-5@@ -6629,23 +6738,32 @@ static void selinux_nf_ip_exit(void)#endif /* CONFIG_NETFILTER */ #ifdef CONFIG_SECURITY_SELINUX_DISABLE -static int selinux_disabled; - -int selinux_disable (void ) +int selinux_disable (struct selinux_ns *ns) { -if (ss_initialized) { +if (ns->initialized) {/* Not permitted after initial policy load. */ return -EINVAL ; } -if (selinux_disabled) { +if (ns->disabled) {/* Only do this once. */ return -EINVAL ; } + ns->disabled =1 ;
security/selinux/hooks.c static void selinux_nf_ip_exit(void) · line 6763
Quoted 10 lines
security/selinux/hooks.c +10
-0+ +/* +* Disable of a non-init ns does not disable SELinux in the host. +* We simply let the disable succeed, and init will then +* unmount its selinuxfs instance and subsequent userspace +* within the ns will interpret the absence of a selinuxfs mount +* as SELinux being disabled. +*/ +if (ns != init_selinux_ns) +return 0 ;
Something else we don't really need at this point.
Quoted 12 lines
security/selinux/include/security.h +3
-1@@ -97,13 +92,80 @@ extern int selinux_policycap_nnp_nosuid_transition;/* limitation of boundary depth */ #define POLICYDB_BOUNDS_MAXDEPTH 4 -int security_mls_enabled (void ); +struct selinux_ss ; + +struct selinux_ns {
security/selinux/include/security.h extern int selinux_policycap_nnp_nosuid_transition; · line 107
Quoted 10 lines
security/selinux/include/security.h +10
-0+refcount_t count; +struct work_struct work; +bool disabled; +#ifdef CONFIG_SECURITY_SELINUX_DEVELOP +bool enforcing; +#endif +bool checkreqprot; +bool initialized; +bool policycap[__POLICYDB_CAPABILITY_MAX]; +struct selinux_ss *ss;
While I don't think we need to tackle this as part of the encapsulation work, this is another reminder that we should look into breaking the separation between the security server and the Linux/hooks code. I understand there were historical reasons for this split, but I think all of those reasons are now gone, and further I think enough Linux'isms have crept into the security server that the separation is no longer as meaningful as it may have been in the past.
Quoted 18 lines
security/selinux/include/security.h +17
-0+#define current_selinux_ns (init_selinux_ns) +#define ss_initialized (current_selinux_ns->initialized) + +#ifdef CONFIG_SECURITY_SELINUX_DEVELOP +#define selinux_enforcing (current_selinux_ns->enforcing) +#define ns_enforcing (ns) ((ns)->enforcing) +#define set_ns_enforcing (ns, value) ((ns)->enforcing = value) +#else +#define selinux_enforcing 1 +#define ns_enforcing (ns) 1 +#define set_ns_enforcing (ns, value) +#endif + +#define selinux_checkreqprot (current_selinux_ns->checkreqprot) + +#define selinux_policycap_netpeer \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_NETPEER])
security/selinux/include/security.h extern int selinux_policycap_nnp_nosuid_transition; · line 158
Quoted 10 lines
security/selinux/include/security.h +10
-0+#define selinux_policycap_openperm \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_OPENPERM]) +#define selinux_policycap_extsockclass \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_EXTSOCKCLASS]) +#define selinux_policycap_alwaysnetwork \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_ALWAYSNETWORK]) +#define selinux_policycap_cgroupseclabel \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_CGROUPSECLABEL]) +#define selinux_policycap_nnp_nosuid_transition \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION])
I realize this was likely the quickest solution, but I think I would prefer to see stuff like the above written as small static inline functions.
Signature · 2 lines
paul moore www.paul-moore.com
Paul Moore
maintainerRe: [RFC 01/10] selinux: introduce a selinux namespace
Quoted 22 lines · Paul Moore
Quoted 21 lines · Stephen Smalley
Define a selinux namespace structure (struct
selinux_ns) for SELinux state and pass it explicitly to all security server functions. The public portion of the structure contains state that is used throughout the SELinux code, such as the enforcing mode. The structure also contains a pointer to aselinux_ssstructure whose definition is private to the security server and contains security server specific state such as the policy database and SID table.This change allocates a single selinux namespace, the
init_selinux_ns. It defines and passes a symbol for the current selinux namespace (current_selinux_ns) as a placeholder for future changes where multiple selinux namespaces will be supported, but in this change the current selinux namespace is always the init selinux namespace. Note that passing the current selinux namespace is not correct for all hooks; some hooks will need to be adjusted to pass the selinux namespace associated with an open file, a network namespace or socket, etc, since not all hooks are invoked in process context and some hooks operate in the context of a cred that may differ from current's cred. Fixing all of these cases is left to future changes, once we introduce the support for multiple selinux namespaces.
Quoted 8 lines
Quoted 3 lines
This change by itself should have no effect on SELinux behavior or APIs (userspace or LSM). It merely wraps SELinux state and passes it explicitly as needed.
To put things in context, I'm looking at this patch not really with namespacing in mind, but rather with the idea of encapsulating the SELinux global state. Regardless of what we do with namespacing, I believe the encapsulation work still has value.
With the above comment in mind, I've started looking at the remaining patches in the patchset to see what might be worth merging, independent of the greater namespacing effort.
* 02/10
My comments are very similar to 01/10; I think the encapsulation is a good thing, although perhaps not as important as the changes in 01/100, but there are some namespace specific things that should probably be dropped.
* 03/10
Once again, similar comments to the previous two patches, although this patch is perhaps the least "namespace-y" of the three. While I agree with James comments regarding namespaced stats, that isn't a major concern at this point.
* {04..10}/10
Changes specific to namespacing, not generally useful in other contexts.
Quoted 192 lines
My comments are inline below. I'm going to try and trim out a lot of this patch in my reply as most of are trivial changes needed to pass around the new state pointers.
security/selinux/hooks.c line 100
Quoted 9 lines
security/selinux/hooks.c +1
-0@@ -97,20 +97,24 @@#include "audit.h" #include "avc_ss.h" +struct selinux_ns *init_selinux_ns;This comment applies to more than just the particular line above, it really applies to the entire patch.
If we are going to merge this patch, and presumably a few others, independent of the namespacing work, I would strongly prefer if we used more general names instead of ones tied so closely to namespacing. For example, something along the lines of "struct
selinux_state" would be more desirable than "structselinux_ns"; it doesn't have to be "selinux_state", that was just the first thing I could come up with.Quoted 35 lines
security/selinux/hooks.c +35
-0+static void selinux_ns_free (struct work_struct *work); + +int selinux_ns_create (struct selinux_ns *parent,struct selinux_ns **ns) +{ +struct selinux_ns *newns; +int rc; + + newns =kzalloc (sizeof (*newns),GFP_KERNEL ); +if (!newns) +return -ENOMEM ; + +refcount_set (&newns->count,1 ); +INIT_WORK (&newns->work, selinux_ns_free); + + rc =selinux_ss_create (&newns->ss); +if (rc) + goto err; + +if (parent) + newns->parent =get_selinux_ns (parent); + + *ns = newns; +return 0 ; +err: +kfree (newns); +return rc; +} + +static void selinux_ns_free (struct work_struct *work) +{ +struct selinux_ns *parent, *ns = + container_of(work, struct selinux_ns, work); + + do { + parent = ns->parent;security/selinux/hooks.c static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · line 6572
Quoted 10 lines
security/selinux/hooks.c +10
-0+selinux_ss_free (ns->ss); +kfree (ns); + ns = parent; + }while (ns &&refcount_dec_and_test (&ns->count)); +} + +void __put_selinux_ns (struct selinux_ns *ns) +{ +schedule_work (&ns->work); +}While we would obviously need something like this for proper namespacing, with only a single state struct we don't need all the state/namespace management.
However, I would still be willing to accept all the changes to pass around a reference to the global state struct, even if in this particular case it was always going to be single/init struct.
security/selinux/hooks.c static __init int selinux_init(void) · line 6589
Quoted 6 lines
security/selinux/hooks.c +2
-0@@ -6487,6 +6585,11 @@ static __init int selinux_init(void)printk (KERN_INFO "SELinux: Initializing.\n" ); +if (selinux_ns_create (NULL , &init_selinux_ns)) +panic ("SELinux: Could not create initial namespace\n" );Not yet :) If we stick with
selinux_ns_create(), see my comments above, at the very least we need to pick an error message that doesn't hint at namespacing.Quoted 22 lines
security/selinux/hooks.c +4
-5@@ -6629,23 +6738,32 @@ static void selinux_nf_ip_exit(void)#endif /* CONFIG_NETFILTER */ #ifdef CONFIG_SECURITY_SELINUX_DISABLE -static int selinux_disabled; - -int selinux_disable (void ) +int selinux_disable (struct selinux_ns *ns) { -if (ss_initialized) { +if (ns->initialized) {/* Not permitted after initial policy load. */ return -EINVAL ; } -if (selinux_disabled) { +if (ns->disabled) {/* Only do this once. */ return -EINVAL ; } + ns->disabled =1 ;security/selinux/hooks.c static void selinux_nf_ip_exit(void) · line 6763
Quoted 10 lines
security/selinux/hooks.c +10
-0+ +/* +* Disable of a non-init ns does not disable SELinux in the host. +* We simply let the disable succeed, and init will then +* unmount its selinuxfs instance and subsequent userspace +* within the ns will interpret the absence of a selinuxfs mount +* as SELinux being disabled. +*/ +if (ns != init_selinux_ns) +return 0 ;Something else we don't really need at this point.
Quoted 12 lines
security/selinux/include/security.h +3
-1@@ -97,13 +92,80 @@ extern int selinux_policycap_nnp_nosuid_transition;/* limitation of boundary depth */ #define POLICYDB_BOUNDS_MAXDEPTH 4 -int security_mls_enabled (void ); +struct selinux_ss ; + +struct selinux_ns {security/selinux/include/security.h extern int selinux_policycap_nnp_nosuid_transition; · line 107
Quoted 10 lines
security/selinux/include/security.h +10
-0+refcount_t count; +struct work_struct work; +bool disabled; +#ifdef CONFIG_SECURITY_SELINUX_DEVELOP +bool enforcing; +#endif +bool checkreqprot; +bool initialized; +bool policycap[__POLICYDB_CAPABILITY_MAX]; +struct selinux_ss *ss;While I don't think we need to tackle this as part of the encapsulation work, this is another reminder that we should look into breaking the separation between the security server and the Linux/hooks code. I understand there were historical reasons for this split, but I think all of those reasons are now gone, and further I think enough Linux'isms have crept into the security server that the separation is no longer as meaningful as it may have been in the past.
Quoted 18 lines
security/selinux/include/security.h +17
-0+#define current_selinux_ns (init_selinux_ns) +#define ss_initialized (current_selinux_ns->initialized) + +#ifdef CONFIG_SECURITY_SELINUX_DEVELOP +#define selinux_enforcing (current_selinux_ns->enforcing) +#define ns_enforcing (ns) ((ns)->enforcing) +#define set_ns_enforcing (ns, value) ((ns)->enforcing = value) +#else +#define selinux_enforcing 1 +#define ns_enforcing (ns) 1 +#define set_ns_enforcing (ns, value) +#endif + +#define selinux_checkreqprot (current_selinux_ns->checkreqprot) + +#define selinux_policycap_netpeer \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_NETPEER])security/selinux/include/security.h extern int selinux_policycap_nnp_nosuid_transition; · line 158
Quoted 10 lines
security/selinux/include/security.h +10
-0+#define selinux_policycap_openperm \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_OPENPERM]) +#define selinux_policycap_extsockclass \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_EXTSOCKCLASS]) +#define selinux_policycap_alwaysnetwork \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_ALWAYSNETWORK]) +#define selinux_policycap_cgroupseclabel \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_CGROUPSECLABEL]) +#define selinux_policycap_nnp_nosuid_transition \ + (current_selinux_ns->policycap[POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION])I realize this was likely the quickest solution, but I think I would prefer to see stuff like the above written as small static inline functions.
--
paul moore
www.paul-moore.com
Signature · 2 lines
paul moore www.paul-moore.com
Re: [RFC 01/10] selinux: introduce a selinux namespace
In reply to Paul MooreQuoted 194 lines · Paul Moore
Quoted 26 lines · Stephen Smalley
Define a selinux namespace structure (struct
selinux_ns) for SELinux state and pass it explicitly to all security server functions. The public portion of the structure contains state that is used throughout the SELinux code, such as the enforcing mode.
The structure also contains a pointer to aselinux_ssstructure whose
definition is private to the security server and contains security server specific state such as the policy database and SID table.This change allocates a single selinux namespace, the
init_selinux_ns.
It defines and passes a symbol for the current selinux namespace (current_selinux_ns) as a placeholder for future changes where multiple selinux namespaces will be supported, but in this change the current selinux namespace is always the init selinux namespace. Note that passing the current selinux namespace is not correct for all hooks; some hooks will need to be adjusted to pass the selinux namespace associated with an open file, a network namespace or socket,
etc, since not all hooks are invoked in process context and some hooks operate in the context of a cred that may differ from current's
cred. Fixing all of these cases is left to future changes, once we introduce the support for multiple selinux namespaces.Quoted 4 lines
This change by itself should have no effect on SELinux behavior or APIs (userspace or LSM). It merely wraps SELinux state and passes it
explicitly as needed.To put things in context, I'm looking at this patch not really with namespacing in mind, but rather with the idea of encapsulating the SELinux global state. Regardless of what we do with namespacing, I believe the encapsulation work still has value.
My comments are inline below. I'm going to try and trim out a lot of this patch in my reply as most of are trivial changes needed to pass around the new state pointers.
security/selinux/hooks.c line 100
Quoted 9 lines
security/selinux/hooks.c +1
-0@@ -97,20 +97,24 @@#include "audit.h" #include "avc_ss.h" +struct selinux_ns *init_selinux_ns;This comment applies to more than just the particular line above, it really applies to the entire patch.
If we are going to merge this patch, and presumably a few others, independent of the namespacing work, I would strongly prefer if we used more general names instead of ones tied so closely to namespacing. For example, something along the lines of "struct
selinux_state" would be more desirable than "structselinux_ns"; it doesn't have to be "selinux_state", that was just the first thing I could come up with.Quoted 36 lines
security/selinux/hooks.c +35
-0+static void selinux_ns_free (struct work_struct *work); + +int selinux_ns_create (struct selinux_ns *parent,struct selinux_ns **ns) +{ +struct selinux_ns *newns; +int rc; + + newns =kzalloc (sizeof (*newns),GFP_KERNEL ); +if (!newns) +return -ENOMEM ; + +refcount_set (&newns->count,1 ); +INIT_WORK (&newns->work, selinux_ns_free); + + rc =selinux_ss_create (&newns->ss); +if (rc) + goto err; + +if (parent) + newns->parent =get_selinux_ns (parent); + + *ns = newns; +return 0 ; +err: +kfree (newns); +return rc; +} + +static void selinux_ns_free (struct work_struct *work) +{ +struct selinux_ns *parent, *ns = + container_of(work, struct selinux_ns, work); + + do { + parent = ns->parent;security/selinux/hooks.c static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · line 6572
Quoted 10 lines
security/selinux/hooks.c +10
-0+selinux_ss_free (ns->ss); +kfree (ns); + ns = parent; + }while (ns &&refcount_dec_and_test (&ns->count)); +} + +void __put_selinux_ns (struct selinux_ns *ns) +{ +schedule_work (&ns->work); +}While we would obviously need something like this for proper namespacing, with only a single state struct we don't need all the state/namespace management.
However, I would still be willing to accept all the changes to pass around a reference to the global state struct, even if in this particular case it was always going to be single/init struct.
security/selinux/hooks.c static __init int selinux_init(void) · line 6590
Quoted 7 lines
security/selinux/hooks.c +2
-0@@ -6487,6 +6585,11 @@ static __init int selinux_init(void)printk (KERN_INFO "SELinux: Initializing.\n" ); +if (selinux_ns_create (NULL , &init_selinux_ns)) +panic ("SELinux: Could not create initial namespace\n");Not yet :) If we stick with
selinux_ns_create(), see my comments above, at the very least we need to pick an error message that doesn't
hint at namespacing.Quoted 30 lines
security/selinux/hooks.c +7
-5@@ -6629,23 +6738,32 @@ static void selinux_nf_ip_exit(void)#endif /* CONFIG_NETFILTER */ #ifdef CONFIG_SECURITY_SELINUX_DISABLE -static int selinux_disabled; - -int selinux_disable (void ) +int selinux_disable (struct selinux_ns *ns) { -if (ss_initialized) { +if (ns->initialized) {/* Not permitted after initial policy load. */ return -EINVAL ; } -if (selinux_disabled) { +if (ns->disabled) {/* Only do this once. */ return -EINVAL ; } + ns->disabled =1 ; + +/* +* Disable of a non-init ns does not disable SELinux in the host.
security/selinux/hooks.c +3
-0+* We simply let the disable succeed, and init will then +* unmount its selinuxfs instance and subsequent userspace +* within the ns will interpret the absence of a selinuxfs mount
security/selinux/hooks.c static void selinux_nf_ip_exit(void) · line 6763
Quoted 4 lines
security/selinux/hooks.c +4
-0+* as SELinux being disabled. +*/ +if (ns != init_selinux_ns) +return 0 ;Something else we don't really need at this point.
Quoted 14 lines
security/selinux/include/security.h +3
-1@@ -97,13 +92,80 @@ extern int selinux_policycap_nnp_nosuid_transition;/* limitation of boundary depth */ #define POLICYDB_BOUNDS_MAXDEPTH 4 -int security_mls_enabled (void ); +struct selinux_ss ; + +struct selinux_ns {security/selinux/include/security.h extern int · line 108
Quoted 10 lines
security/selinux/include/security.h +10
-0+refcount_t count; +struct work_struct work; +bool disabled; +#ifdef CONFIG_SECURITY_SELINUX_DEVELOP +bool enforcing; +#endif +bool checkreqprot; +bool initialized; +bool policycap[__POLICYDB_CAPABILITY_MAX]; +struct selinux_ss *ss;While I don't think we need to tackle this as part of the
Quoted 8 lines
encapsulation work, this is another reminder that we should look into breaking the separation between the security server and the Linux/hooks code. I understand there were historical reasons for this
split, but I think all of those reasons are now gone, and further I think enough Linux'isms have crept into the security server that the separation is no longer as meaningful as it may have been in the past.
I think we want to retain some degree of encapsulation of the policy logic and data structures, which is what the security server is supposed to provide. That allows us to evolve that logic and structures without impacting the object label management and permission enforcement code. Separation of policy from mechanism. I don't mind nativizing the security server for Linux, and where appropriate, allowing some optimization of the interfaces between it and the rest of the SELinux code, but I wouldn't want to e.g. directly expose the policydb to the rest of the code. There has been some leakage of policy awareness outside the security server in the past but I view that as a mistake that ought to be corrected over time.
Quoted 40 lines
Quoted 25 lines
security/selinux/include/security.h +21
-0+#define current_selinux_ns (init_selinux_ns) +#define ss_initialized (current_selinux_ns->initialized) + +#ifdef CONFIG_SECURITY_SELINUX_DEVELOP +#define selinux_enforcing (current_selinux_ns->enforcing) +#define ns_enforcing (ns) ((ns)->enforcing) +#define set_ns_enforcing (ns, value) ((ns)->enforcing = value) +#else +#define selinux_enforcing 1 +#define ns_enforcing (ns) 1 +#define set_ns_enforcing (ns, value) +#endif + +#define selinux_checkreqprot (current_selinux_ns->checkreqprot) + +#define selinux_policycap_netpeer \ + (current_selinux_ns- >policycap[POLICYDB_CAPABILITY_NETPEER]) +#define selinux_policycap_openperm \ + (current_selinux_ns- >policycap[POLICYDB_CAPABILITY_OPENPERM]) +#define selinux_policycap_extsockclass \ + (current_selinux_ns-security/selinux/include/security.h extern int selinux_policycap_nnp_nosuid_transition;
Quoted 10 lines
security/selinux/include/security.h +6
-0>policycap[POLICYDB_CAPABILITY_EXTSOCKCLASS]) +#define selinux_policycap_alwaysnetwork \ + (current_selinux_ns- >policycap[POLICYDB_CAPABILITY_ALWAYSNETWORK]) +#define selinux_policycap_cgroupseclabel \ + (current_selinux_ns- >policycap[POLICYDB_CAPABILITY_CGROUPSECLABEL]) +#define selinux_policycap_nnp_nosuid_transition \ + (current_selinux_ns- >policycap[POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION])I realize this was likely the quickest solution, but I think I would prefer to see stuff like the above written as small static inline functions.
Paul Moore
maintainerRe: [RFC 01/10] selinux: introduce a selinux namespace
Quoted 1 line · Stephen Smalley
On Tue, 2018-02-06 at 17:18 -0500, Paul Moore wrote:
...
Quoted 10 lines
Quoted 9 lines
While I don't think we need to tackle this as part of the encapsulation work, this is another reminder that we should look into breaking the separation between the security server and the Linux/hooks code. I understand there were historical reasons for this
split, but I think all of those reasons are now gone, and further I think enough Linux'isms have crept into the security server that the separation is no longer as meaningful as it may have been in the past.
Quoted 11 lines
I think we want to retain some degree of encapsulation of the policy logic and data structures, which is what the security server is supposed to provide. That allows us to evolve that logic and structures without impacting the object label management and permission enforcement code. Separation of policy from mechanism. I don't mind nativizing the security server for Linux, and where appropriate, allowing some optimization of the interfaces between it and the rest of the SELinux code, but I wouldn't want to e.g. directly expose the policydb to the rest of the code. There has been some leakage of policy awareness outside the security server in the past but I view that as a mistake that ought to be corrected over time.
I agree that a level of abstraction between the policydb code and the enforcement code is a good thing, but I think there are some boundaries between the hook code and the security server that we could do without. Once again, not really part of this work, just popped up in my head again while looking at these patches.
Signature · 2 lines
paul moore www.paul-moore.com
Re: [RFC 01/10] selinux: introduce a selinux namespace
Quoted 35 lines · Paul Moore
Quoted 1 line · Stephen Smalley
On Tue, 2018-02-06 at 17:18 -0500, Paul Moore wrote:
...
Quoted 13 lines
Quoted 4 lines
While I don't think we need to tackle this as part of the encapsulation work, this is another reminder that we should look into
breaking the separation between the security server and theQuoted 8 lines
Linux/hooks code. I understand there were historical reasons for this
split, but I think all of those reasons are now gone, and further I
think enough Linux'isms have crept into the security server that the
separation is no longer as meaningful as it may have been in the past.Quoted 15 lines
I think we want to retain some degree of encapsulation of the policy
logic and data structures, which is what the security server is supposed to provide. That allows us to evolve that logic and structures without impacting the object label management and permission
enforcement code. Separation of policy from mechanism. I don't mind
nativizing the security server for Linux, and where appropriate, allowing some optimization of the interfaces between it and the rest of
the SELinux code, but I wouldn't want to e.g. directly expose the policydb to the rest of the code. There has been some leakage of policy awareness outside the security server in the past but I view that as a mistake that ought to be corrected over time.
Quoted 6 lines
I agree that a level of abstraction between the policydb code and the enforcement code is a good thing, but I think there are some boundaries between the hook code and the security server that we could
do without. Once again, not really part of this work, just popped up in my head again while looking at these patches.
I wanted to clarify that point because it motivates keeping selinux_ss as a separate struct from selinux_state (previously selinux_ns), and not exposing the selinux_ss struct definition outside of the security server. So the selinux_state struct would still be something like:
struct selinux_state {
bool disabled;
#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
bool enforcing;
#endif
bool checkreqprot;
bool initialized;
bool policycap[__POLICYDB_CAPABILITY_MAX];
struct selinux_avc *avc;
struct selinux_ss *ss;
}(dropping the reference count, work struct, and parent pointers that were part of selinux_ns as being specific to the namespace work).
hooks.c would declare struct selinux_state selinux_state;. ss/services.c would declare static struct selinux_ss selinux_ss; and provide a selinux_ss_init() function (instead of selinux_ss_create) that would set the ->ss field to &selinux_ss. Likewise for avc.c and the selinux_avc. hooks.c:selinux_init() would call selinux_ss_init(&selinux_state.ss) and selinux_avc_init(&selinux_state.avc) to set those pointers to the appropriate structures private to the ss and avc code. The _create and _free functions would go away and none of the structures would be dynamically allocated/freed.
This is in contrast to exposing the selinux_ss struct definition outside the security server and directly embedding it in the selinux_state, since that would require exposing the policydb and sidtab structures as well (unless we were to make those opaque pointers within the selinux_ss; currently the policydb and sidtab are directly embedded within it). Likewise for the selinux_avc and its embedded avc_cache structure.
Trying to make sure we're in agreement on the data structures before I rewrite since I don't want to have to rewrite it twice ;)
Paul Moore
maintainerRe: [RFC 01/10] selinux: introduce a selinux namespace
Quoted 59 lines · Stephen Smalley
Quoted 35 lines · Paul Moore
Quoted 1 line · Stephen Smalley
On Tue, 2018-02-06 at 17:18 -0500, Paul Moore wrote:
...
Quoted 13 lines
Quoted 4 lines
While I don't think we need to tackle this as part of the encapsulation work, this is another reminder that we should look into
breaking the separation between the security server and theQuoted 8 lines
Linux/hooks code. I understand there were historical reasons for this
split, but I think all of those reasons are now gone, and further I
think enough Linux'isms have crept into the security server that the
separation is no longer as meaningful as it may have been in the past.Quoted 15 lines
I think we want to retain some degree of encapsulation of the policy
logic and data structures, which is what the security server is supposed to provide. That allows us to evolve that logic and structures without impacting the object label management and permission
enforcement code. Separation of policy from mechanism. I don't mind
nativizing the security server for Linux, and where appropriate, allowing some optimization of the interfaces between it and the rest of
the SELinux code, but I wouldn't want to e.g. directly expose the policydb to the rest of the code. There has been some leakage of policy awareness outside the security server in the past but I view that as a mistake that ought to be corrected over time.Quoted 6 lines
I agree that a level of abstraction between the policydb code and the enforcement code is a good thing, but I think there are some boundaries between the hook code and the security server that we could
do without. Once again, not really part of this work, just popped up in my head again while looking at these patches.I wanted to clarify that point because it motivates keeping
selinux_ssas a separate struct fromselinux_state(previouslyselinux_ns), and not exposing theselinux_ssstruct definition outside of the security server. So theselinux_statestruct would still be something like:struct selinux_state { bool disabled; #ifdef CONFIG_SECURITY_SELINUX_DEVELOP bool enforcing; #endif bool checkreqprot; bool initialized; bool policycap[__POLICYDB_CAPABILITY_MAX]; struct selinux_avc *avc; struct selinux_ss *ss; }
Quoted 2 lines
(dropping the reference count, work struct, and parent pointers that were part of
selinux_nsas being specific to the namespace work).
I think keeping the selinux_avc and selinux_ss structs separate is fine right now, as I mentioned earlier, I really don't want to entangle the encapsulation work with any potential hooks/ss consolidation work.
Directly embedding the selinux_avc and selinux_ss structures inside selinux_state would be nice as we could just do one allocation, but that isn't worth breaking the abstraction at this point. Maybe we will get there at some point, but I think it's important to stay focused on the task at hand.
Quoted 21 lines
hooks.cwould declare structselinux_stateselinux_state;.ss/services.cwould declare static structselinux_ssselinux_ss; and provide aselinux_ss_init()function (instead ofselinux_ss_create) that would set the ->ss field to&selinux_ss. Likewise foravc.cand theselinux_avc. hooks.c:selinux_init() would callselinux_ss_init(&selinux_state.ss)andselinux_avc_init(&selinux_state.avc)to set those pointers to the appropriate structures private to the ss and avc code. The_createand_freefunctions would go away and none of the structures would be dynamically allocated/freed.This is in contrast to exposing the
selinux_ssstruct definition outside the security server and directly embedding it in theselinux_state, since that would require exposing the policydb and sidtab structures as well (unless we were to make those opaque pointers within theselinux_ss; currently the policydb and sidtab are directly embedded within it). Likewise for theselinux_avcand its embeddedavc_cachestructure.Trying to make sure we're in agreement on the data structures before I rewrite since I don't want to have to rewrite it twice ;)
Signature · 2 lines
paul moore www.paul-moore.com
RFC 2/10 selinux: support multiple selinuxfs instances
In reply to Stephen SmalleySupport multiple selinuxfs instances, per namespace. Move global selinuxfs state to a per-instance structure (selinux_fs_info), and include a reference to the corresponding selinux namespace in this structure. Pass this selinux namespace to all security server operations, thereby ensuring that each selinuxfs instance presents a view of and acts as an interface to a particular namespace.
The expected usage would be to unshare the SELinux namespace and the mount namespace, and then mount a new selinuxfs instance. The new instance would then provide an interface for viewing and manipulating the state of the new SELinux namespace and would not affect the parent namespace in any manner.
This change by itself should have no effect on SELinux behavior or APIs (userspace or LSM). It merely wraps the selinuxfs global state, links it to a particular selinux namespace (currently always the initial namespace) and uses that namespace for all selinuxfs operations.
--- security/selinux/include/security.h | 3 +- security/selinux/selinuxfs.c | 480 ++++++++++++++++++++++-------------- security/selinux/ss/services.c | 13 + security/selinux/ss/status.c | 4 +- 4 files changed, 315 insertions(+), 185 deletions(-)
security/selinux/include/security.h +1 -2
@@ -355,8 +355,7 @@ struct selinux_kernel_status {*/ } __packed; -extern void selinux_status_update_setenforce (struct selinux_ns *ns, -int enforcing); +extern void selinux_status_update_setenforce (struct selinux_ns *ns);extern void selinux_status_update_policyload (struct selinux_ns *ns,int seqno);extern void selinux_complete_init (void );
security/selinux/selinuxfs.c +299 -181
1,108 lines not on this page · raw message
security/selinux/ss/services.c +13 -0
@@ -2823,6 +2823,13 @@ int security_get_bools(struct selinux_ns *ns,struct policydb *policydb;int i, rc; +if (!ns->initialized) { + *len =0 ; + *names =NULL ; + *values =NULL ; +return 0 ; + } +read_lock (&ns->ss->policy_rwlock); policydb = &ns->ss->policydb;
@@ -3153,6 +3160,12 @@ int security_get_classes(struct selinux_ns *ns,struct policydb *policydb = &ns->ss->policydb;int rc; +if (!ns->initialized) { + *nclasses =0 ; + *classes =NULL ; +return 0 ; + } +read_lock (&ns->ss->policy_rwlock); rc = -ENOMEM ;
security/selinux/ss/status.c +2 -2
@@ -78,7 +78,7 @@ struct page *selinux_kernel_status_page(struct selinux_ns *ns)* * It updates status of the current enforcing/permissive mode. */ -void selinux_status_update_setenforce (struct selinux_ns *ns,int enforcing) +void selinux_status_update_setenforce (struct selinux_ns *ns) {struct selinux_kernel_status *status;
@@ -89,7 +89,7 @@ void selinux_status_update_setenforce(struct selinux_ns *ns, int enforcing) status->sequence++;smp_wmb (); - status->enforcing = enforcing; + status->enforcing =ns_enforcing (ns);smp_wmb (); status->sequence++;
Signature · 1 line
2.9.5
RFC 3/10 selinux: move the AVC into the selinux namespace
In reply to Stephen SmalleyMove the access vector cache (AVC) into the selinux namespace structure and pass it explicitly to all AVC functions. The AVC private state is encapsulated in a selinux_avc structure that is allocated and freed by the AVC during selinux namespace creation and destruction.
This is necessary to support multiple selinux namespaces since the AVC caches state (e.g. SIDs, policy sequence number) that is maintained and provided by the security server on a per-namespace basis.
This change by itself should have no effect on SELinux behavior or APIs (userspace or LSM).
--- security/selinux/avc.c | 293 +++++++++++++++------------ security/selinux/hooks.c | 382 ++++++++++++++++++++++++------------ security/selinux/include/avc.h | 32 +-- security/selinux/include/avc_ss.h | 3 +- security/selinux/include/security.h | 5 + security/selinux/netlabel.c | 3 +- security/selinux/selinuxfs.c | 60 ++++-- security/selinux/ss/services.c | 9 +- security/selinux/xfrm.c | 17 +- 9 files changed, 515 insertions(+), 289 deletions(-)
security/selinux/avc.c +172 -121
@@ -82,14 +82,56 @@ struct avc_callback_node {struct avc_callback_node *next; }; -/* Exported via selinufs */ -unsigned int avc_cache_threshold =AVC_DEF_CACHE_THRESHOLD ; -#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };#endif -static struct avc_cache avc_cache; +struct selinux_avc { +unsigned int avc_cache_threshold; +struct avc_cache avc_cache; +}; + +int selinux_avc_create (struct selinux_avc **avc) +{ +struct selinux_avc *newavc; +int i; + + newavc =kzalloc (sizeof (*newavc),GFP_KERNEL ); +if (!newavc) +return -ENOMEM ; + + newavc->avc_cache_threshold =AVC_DEF_CACHE_THRESHOLD ; + +for (i =0 ; i <AVC_CACHE_SLOTS ; i++) { +INIT_HLIST_HEAD (&newavc->avc_cache.slots[i]); +spin_lock_init (&newavc->avc_cache.slots_lock[i]); + } +atomic_set (&newavc->avc_cache.active_nodes,0 ); +atomic_set (&newavc->avc_cache.lru_hint,0 ); + + *avc = newavc; +return 0 ; +} + +static void avc_flush (struct selinux_avc *avc); + +void selinux_avc_free (struct selinux_avc *avc) +{ +avc_flush (avc); +kfree (avc); +} + +unsigned int avc_get_cache_threshold (struct selinux_avc *avc) +{ +return avc->avc_cache_threshold; +} + +void avc_set_cache_threshold (struct selinux_avc *avc, +unsigned int cache_threshold) +{ + avc->avc_cache_threshold = cache_threshold; +} +static struct avc_callback_node *avc_callbacks;static struct kmem_cache *avc_node_cachep;static struct kmem_cache *avc_xperms_data_cachep;
@@ -143,14 +185,14 @@ static void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)* @tsid: target security identifier * @tclass: target security class */ -static void avc_dump_query (struct audit_buffer *ab,u32 ssid,u32 tsid,u16 tclass) +static void avc_dump_query (struct audit_buffer *ab,struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass) {int rc;char *scontext;u32 scontext_len; - rc =security_sid_to_context (current_selinux_ns, ssid, - &scontext, &scontext_len); + rc =security_sid_to_context (ns, ssid, &scontext, &scontext_len);if (rc)audit_log_format (ab,"ssid=%d" , ssid);else {
@@ -158,8 +200,7 @@ static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclakfree (scontext); } - rc =security_sid_to_context (current_selinux_ns, tsid, - &scontext, &scontext_len); + rc =security_sid_to_context (ns, tsid, &scontext, &scontext_len);if (rc)audit_log_format (ab," tsid=%d" , tsid);else {
@@ -178,15 +219,6 @@ static void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tcla*/ void __init avc_init (void ) { -int i; - -for (i =0 ; i <AVC_CACHE_SLOTS ; i++) { -INIT_HLIST_HEAD (&avc_cache.slots[i]); -spin_lock_init (&avc_cache.slots_lock[i]); - } -atomic_set (&avc_cache.active_nodes,0 ); -atomic_set (&avc_cache.lru_hint,0 ); - avc_node_cachep =kmem_cache_create ("avc_node" ,sizeof (struct avc_node ),0 ,SLAB_PANIC ,NULL ); avc_xperms_cachep = kmem_cache_create("avc_xperms_node",
@@ -201,7 +233,7 @@ void __init avc_init(void) 0, SLAB_PANIC, NULL); } -int avc_get_hash_stats (char *page) +int avc_get_hash_stats (struct selinux_avc *avc,char *page) {int i, chain_len, max_chain_len, slots_used;struct avc_node *node;
@@ -212,7 +244,7 @@ int avc_get_hash_stats(char *page) slots_used =0 ; max_chain_len =0 ;for (i =0 ; i <AVC_CACHE_SLOTS ; i++) { - head = &avc_cache.slots[i]; + head = &avc->avc_cache.slots[i];if (!hlist_empty (head)) { slots_used++; chain_len =0 ;
@@ -227,7 +259,7 @@ int avc_get_hash_stats(char *page)return scnprintf (page,PAGE_SIZE ,"entries: %d\nbuckets used: %d/%d\n" "longest chain: %d\n" , -atomic_read (&avc_cache.active_nodes), +atomic_read (&avc->avc_cache.active_nodes), slots_used,AVC_CACHE_SLOTS , max_chain_len); }
@@ -464,11 +496,12 @@ static inline u32 avc_xperms_audit_required(u32 requested,return audited; } -static inline int avc_xperms_audit (u32 ssid,u32 tsid,u16 tclass, -u32 requested,struct av_decision *avd, -struct extended_perms_decision *xpd, -u8 perm,int result, -struct common_audit_data *ad) +static inline int avc_xperms_audit (struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass, +u32 requested,struct av_decision *avd, +struct extended_perms_decision *xpd, +u8 perm,int result, +struct common_audit_data *ad) {u32 audited, denied;
@@ -476,7 +509,7 @@ static inline int avc_xperms_audit(u32 ssid, u32 tsid, u16 tclass, requested, avd, xpd, perm, result, &denied);if (likely (!audited))return 0 ; -return slow_avc_audit (ssid, tsid, tclass, requested, +return slow_avc_audit (ns, ssid, tsid, tclass, requested, audited, denied, result, ad,0 ); }
@@ -488,29 +521,30 @@ static void avc_node_free(struct rcu_head *rhead)avc_cache_stats_incr (frees); } -static void avc_node_delete (struct avc_node *node) +static void avc_node_delete (struct selinux_avc *avc,struct avc_node *node) {hlist_del_rcu (&node->list);call_rcu (&node->rhead, avc_node_free); -atomic_dec (&avc_cache.active_nodes); +atomic_dec (&avc->avc_cache.active_nodes); } -static void avc_node_kill (struct avc_node *node) +static void avc_node_kill (struct selinux_avc *avc,struct avc_node *node) {avc_xperms_free (node->ae.xp_node);kmem_cache_free (avc_node_cachep, node);avc_cache_stats_incr (frees); -atomic_dec (&avc_cache.active_nodes); +atomic_dec (&avc->avc_cache.active_nodes); } -static void avc_node_replace (struct avc_node *new,struct avc_node *old) +static void avc_node_replace (struct selinux_avc *avc, +struct avc_node *new,struct avc_node *old) {hlist_replace_rcu (&old->list, &new->list);call_rcu (&old->rhead, avc_node_free); -atomic_dec (&avc_cache.active_nodes); +atomic_dec (&avc->avc_cache.active_nodes); } -static inline int avc_reclaim_node (void ) +static inline int avc_reclaim_node (struct selinux_avc *avc) {struct avc_node *node;int hvalue, try, ecx;
@@ -519,16 +553,17 @@ static inline int avc_reclaim_node(void)spinlock_t *lock;for (try =0 , ecx =0 ; try <AVC_CACHE_SLOTS ; try++) { - hvalue =atomic_inc_return (&avc_cache.lru_hint) & (AVC_CACHE_SLOTS -1 ); - head = &avc_cache.slots[hvalue]; - lock = &avc_cache.slots_lock[hvalue]; + hvalue =atomic_inc_return (&avc->avc_cache.lru_hint) & + (AVC_CACHE_SLOTS -1 ); + head = &avc->avc_cache.slots[hvalue]; + lock = &avc->avc_cache.slots_lock[hvalue];if (!spin_trylock_irqsave (lock, flags))continue ;rcu_read_lock ();hlist_for_each_entry (node, head, list) { -avc_node_delete (node); +avc_node_delete (avc, node);avc_cache_stats_incr (reclaims); ecx++;if (ecx >=AVC_CACHE_RECLAIM ) {
@@ -544,7 +579,7 @@ static inline int avc_reclaim_node(void)return ecx; } -static struct avc_node *avc_alloc_node (void ) +static struct avc_node *avc_alloc_node (struct selinux_avc *avc) {struct avc_node *node;
@@ -555,8 +590,9 @@ static struct avc_node *avc_alloc_node(void)INIT_HLIST_NODE (&node->list);avc_cache_stats_incr (allocations); -if (atomic_inc_return (&avc_cache.active_nodes) > avc_cache_threshold) -avc_reclaim_node (); +if (atomic_inc_return (&avc->avc_cache.active_nodes) > + avc->avc_cache_threshold) +avc_reclaim_node (avc); out:return node;
@@ -570,14 +606,15 @@ static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid, u16 tclmemcpy (&node->ae.avd, avd,sizeof (node->ae.avd)); } -static inline struct avc_node *avc_search_node (u32 ssid,u32 tsid,u16 tclass) +static inline struct avc_node *avc_search_node (struct selinux_avc *avc, +u32 ssid,u32 tsid,u16 tclass) {struct avc_node *node, *ret =NULL ;int hvalue;struct hlist_head *head; hvalue =avc_hash (ssid, tsid, tclass); - head = &avc_cache.slots[hvalue]; + head = &avc->avc_cache.slots[hvalue];hlist_for_each_entry_rcu (node, head, list) { if (ssid == node->ae.ssid && tclass == node->ae.tclass &&
@@ -602,12 +639,13 @@ static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)* then this function returns the avc_node. * Otherwise, this function returns NULL. */ -static struct avc_node *avc_lookup (u32 ssid,u32 tsid,u16 tclass) +static struct avc_node *avc_lookup (struct selinux_avc *avc, +u32 ssid,u32 tsid,u16 tclass) {struct avc_node *node;avc_cache_stats_incr (lookups); - node =avc_search_node (ssid, tsid, tclass); + node =avc_search_node (avc, ssid, tsid, tclass);if (node)return node;
@@ -616,7 +654,8 @@ static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass)return NULL ; } -static int avc_latest_notif_update (int seqno,int is_insert) +static int avc_latest_notif_update (struct selinux_avc *avc, +int seqno,int is_insert) {int ret =0 ;static DEFINE_SPINLOCK (notif_lock);
@@ -624,14 +663,14 @@ static int avc_latest_notif_update(int seqno, int is_insert)spin_lock_irqsave (¬if_lock, flag);if (is_insert) { -if (seqno < avc_cache.latest_notif) { +if (seqno < avc->avc_cache.latest_notif) {printk (KERN_WARNING "SELinux: avc: seqno %d < latest_notif %d\n" , - seqno, avc_cache.latest_notif); + seqno, avc->avc_cache.latest_notif); ret = -EAGAIN ; } }else { -if (seqno > avc_cache.latest_notif) - avc_cache.latest_notif = seqno; +if (seqno > avc->avc_cache.latest_notif) + avc->avc_cache.latest_notif = seqno; }spin_unlock_irqrestore (¬if_lock, flag);
@@ -656,18 +695,19 @@ static int avc_latest_notif_update(int seqno, int is_insert)* the access vectors into a cache entry, returns * avc_node inserted. Otherwise, this function returns NULL. */ -static struct avc_node *avc_insert (u32 ssid,u32 tsid,u16 tclass, -struct av_decision *avd, -struct avc_xperms_node *xp_node) +static struct avc_node *avc_insert (struct selinux_avc *avc, +u32 ssid,u32 tsid,u16 tclass, +struct av_decision *avd, +struct avc_xperms_node *xp_node) {struct avc_node *pos, *node =NULL ;int hvalue;unsigned long flag; -if (avc_latest_notif_update (avd->seqno,1 )) +if (avc_latest_notif_update (avc, avd->seqno,1 )) goto out; - node =avc_alloc_node (); + node =avc_alloc_node (avc);if (node) {struct hlist_head *head;spinlock_t *lock;
@@ -680,15 +720,15 @@ static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass,kmem_cache_free (avc_node_cachep, node);return NULL ; } - head = &avc_cache.slots[hvalue]; - lock = &avc_cache.slots_lock[hvalue]; + head = &avc->avc_cache.slots[hvalue]; + lock = &avc->avc_cache.slots_lock[hvalue];spin_lock_irqsave (lock, flag);hlist_for_each_entry (pos, head, list) {if (pos->ae.ssid == ssid && pos->ae.tsid == tsid && pos->ae.tclass == tclass) { -avc_node_replace (node, pos); +avc_node_replace (avc, node, pos); goto found; } }
@@ -726,9 +766,10 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a) {struct common_audit_data *ad = a;audit_log_format (ab," " ); -avc_dump_query (ab, ad->selinux_audit_data->ssid, - ad->selinux_audit_data->tsid, - ad->selinux_audit_data->tclass); +avc_dump_query (ab, ad->selinux_audit_data->ns, + ad->selinux_audit_data->ssid, + ad->selinux_audit_data->tsid, + ad->selinux_audit_data->tclass);if (ad->selinux_audit_data->denied) {audit_log_format (ab," permissive=%u" , ad->selinux_audit_data->result ?0 :1 );
@@ -736,10 +777,11 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a) }/* This is the slow part of avc audit with big stack footprint */ -noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass, -u32 requested,u32 audited,u32 denied,int result, -struct common_audit_data *a, -unsigned flags) +noinline int slow_avc_audit(struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass, +u32 requested,u32 audited,u32 denied,int result, +struct common_audit_data *a, +unsigned int flags) {struct common_audit_data stack_data;struct selinux_audit_data sad;
@@ -767,6 +809,7 @@ noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass, sad.audited = audited; sad.denied = denied; sad.result = result; + sad.ns = ns; a->selinux_audit_data = &sad;
@@ -815,10 +858,11 @@ int __init avc_add_callback(int (*callback)(u32 event), u32 events)* otherwise, this function updates the AVC entry. The original AVC-entry object * will release later by RCU. */ -static int avc_update_node (u32 event,u32 perms,u8 driver,u8 xperm,u32 ssid, -u32 tsid,u16 tclass,u32 seqno, -struct extended_perms_decision *xpd, -u32 flags) +static int avc_update_node (struct selinux_avc *avc, +u32 event,u32 perms,u8 driver,u8 xperm,u32 ssid, +u32 tsid,u16 tclass,u32 seqno, +struct extended_perms_decision *xpd, +u32 flags) {int hvalue, rc =0 ;unsigned long flag;
@@ -826,7 +870,7 @@ static int avc_update_node(u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,struct hlist_head *head;spinlock_t *lock; - node =avc_alloc_node (); + node =avc_alloc_node (avc);if (!node) { rc = -ENOMEM ; goto out;
@@ -835,8 +879,8 @@ static int avc_update_node(u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,/* Lock the target slot */ hvalue =avc_hash (ssid, tsid, tclass); - head = &avc_cache.slots[hvalue]; - lock = &avc_cache.slots_lock[hvalue]; + head = &avc->avc_cache.slots[hvalue]; + lock = &avc->avc_cache.slots_lock[hvalue];spin_lock_irqsave (lock, flag);
@@ -852,7 +896,7 @@ static int avc_update_node(u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,if (!orig) { rc = -ENOENT ; -avc_node_kill (node); +avc_node_kill (avc, node); goto out_unlock; }
@@ -896,7 +940,7 @@ static int avc_update_node(u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,avc_add_xperms_decision (node, xpd);break ; } -avc_node_replace (node, orig); +avc_node_replace (avc, node, orig); out_unlock:spin_unlock_irqrestore (lock, flag); out:
@@ -906,7 +950,7 @@ static int avc_update_node(u32 event, u32 perms, u8 driver, u8 xperm, u32 ssid,/** * avc_flush - Flush the cache */ -static void avc_flush (void ) +static void avc_flush (struct selinux_avc *avc) {struct hlist_head *head;struct avc_node *node;
@@ -915,8 +959,8 @@ static void avc_flush(void)int i;for (i =0 ; i <AVC_CACHE_SLOTS ; i++) { - head = &avc_cache.slots[i]; - lock = &avc_cache.slots_lock[i]; + head = &avc->avc_cache.slots[i]; + lock = &avc->avc_cache.slots_lock[i];spin_lock_irqsave (lock, flag);/*
@@ -925,7 +969,7 @@ static void avc_flush(void)*/ rcu_read_lock ();hlist_for_each_entry (node, head, list) -avc_node_delete (node); +avc_node_delete (avc, node);rcu_read_unlock ();spin_unlock_irqrestore (lock, flag); }
@@ -935,12 +979,12 @@ static void avc_flush(void)* avc_ss_reset - Flush the cache and revalidate migrated permissions. * @seqno: policy sequence number */ -int avc_ss_reset (u32 seqno) +int avc_ss_reset (struct selinux_avc *avc,u32 seqno) {struct avc_callback_node *c;int rc =0 , tmprc; -avc_flush (); +avc_flush (avc);for (c = avc_callbacks; c; c = c->next) {if (c->events &AVC_CALLBACK_RESET ) {
@@ -952,7 +996,7 @@ int avc_ss_reset(u32 seqno) } } -avc_latest_notif_update (seqno,0 ); +avc_latest_notif_update (avc, seqno,0 );return rc; }
@@ -965,31 +1009,33 @@ int avc_ss_reset(u32 seqno)* Don't inline this, since it's the slow-path and just * results in a bigger stack frame. */ -static noinline struct avc_node *avc_compute_av(u32 ssid, u32 tsid, -u16 tclass,struct av_decision *avd, -struct avc_xperms_node *xp_node) +static noinline struct avc_node *avc_compute_av(struct selinux_ns *ns, +u32 ssid,u32 tsid, +u16 tclass, +struct av_decision *avd, +struct avc_xperms_node *xp_node) {rcu_read_unlock ();INIT_LIST_HEAD (&xp_node->xpd_head); -security_compute_av (current_selinux_ns, ssid, tsid, tclass, - avd, &xp_node->xp); +security_compute_av (ns, ssid, tsid, tclass, avd, &xp_node->xp);rcu_read_lock (); -return avc_insert (ssid, tsid, tclass, avd, xp_node); +return avc_insert (ns->avc, ssid, tsid, tclass, avd, xp_node); } -static noinline int avc_denied(u32 ssid, u32 tsid, -u16 tclass,u32 requested, -u8 driver,u8 xperm,unsigned flags, -struct av_decision *avd) +static noinline int avc_denied(struct selinux_ns *ns, +u32 ssid,u32 tsid, +u16 tclass,u32 requested, +u8 driver,u8 xperm,unsigned int flags, +struct av_decision *avd) {if (flags &AVC_STRICT )return -EACCES ; -if (selinux_enforcing && !(avd->flags &AVD_FLAGS_PERMISSIVE )) +if (ns_enforcing (ns) && !(avd->flags &AVD_FLAGS_PERMISSIVE ))return -EACCES ; -avc_update_node (AVC_CALLBACK_GRANT , requested, driver, xperm, ssid, - tsid, tclass, avd->seqno,NULL , flags); +avc_update_node (ns->avc,AVC_CALLBACK_GRANT , requested, driver, xperm, + ssid, tsid, tclass, avd->seqno,NULL , flags);return 0 ; }
@@ -1000,8 +1046,9 @@ static noinline int avc_denied(u32 ssid, u32 tsid,* as-is the case with ioctls, then multiple may be chained together and the * driver field is used to specify which set contains the permission. */ -int avc_has_extended_perms (u32 ssid,u32 tsid,u16 tclass,u32 requested, -u8 driver,u8 xperm,struct common_audit_data *ad) +int avc_has_extended_perms (struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass,u32 requested, +u8 driver,u8 xperm,struct common_audit_data *ad) {struct avc_node *node;struct av_decision avd;
@@ -1020,9 +1067,9 @@ int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,rcu_read_lock (); - node =avc_lookup (ssid, tsid, tclass); + node =avc_lookup (ns->avc, ssid, tsid, tclass);if (unlikely (!node)) { - node =avc_compute_av (ssid, tsid, tclass, &avd, xp_node); + node =avc_compute_av (ns, ssid, tsid, tclass, &avd, xp_node); }else {memcpy (&avd, &node->ae.avd,sizeof (avd)); xp_node = node->ae.xp_node;
@@ -1046,11 +1093,12 @@ int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested, goto decision; }rcu_read_unlock (); -security_compute_xperms_decision (current_selinux_ns, ssid, tsid, - tclass, driver, &local_xpd); +security_compute_xperms_decision (ns, ssid, tsid, tclass, driver, + &local_xpd);rcu_read_lock (); -avc_update_node (AVC_CALLBACK_ADD_XPERMS , requested, driver, xperm, - ssid, tsid, tclass, avd.seqno, &local_xpd,0 ); +avc_update_node (ns->avc,AVC_CALLBACK_ADD_XPERMS , requested, + driver, xperm, ssid, tsid, tclass, avd.seqno, + &local_xpd,0 ); }else {avc_quick_copy_xperms_decision (xperm, &local_xpd, xpd); }
@@ -1062,12 +1110,12 @@ int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested, decision: denied = requested & ~(avd.allowed);if (unlikely (denied)) - rc =avc_denied (ssid, tsid, tclass, requested, driver, xperm, -AVC_EXTENDED_PERMS , &avd); + rc =avc_denied (ns, ssid, tsid, tclass, requested, + driver, xperm,AVC_EXTENDED_PERMS , &avd);rcu_read_unlock (); - rc2 =avc_xperms_audit (ssid, tsid, tclass, requested, + rc2 =avc_xperms_audit (ns, ssid, tsid, tclass, requested, &avd, xpd, xperm, rc, ad);if (rc2)return rc2;
@@ -1094,10 +1142,11 @@ int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,* auditing, e.g. in cases where a lock must be held for the check but * should be released for the auditing. */ -inline int avc_has_perm_noaudit (u32 ssid,u32 tsid, -u16 tclass,u32 requested, -unsigned flags, -struct av_decision *avd) +inline int avc_has_perm_noaudit (struct selinux_ns *ns, +u32 ssid,u32 tsid, +u16 tclass,u32 requested, +unsigned int flags, +struct av_decision *avd) {struct avc_node *node;struct avc_xperms_node xp_node;
@@ -1108,15 +1157,16 @@ inline int avc_has_perm_noaudit(u32 ssid, u32 tsid,rcu_read_lock (); - node =avc_lookup (ssid, tsid, tclass); + node =avc_lookup (ns->avc, ssid, tsid, tclass);if (unlikely (!node)) - node =avc_compute_av (ssid, tsid, tclass, avd, &xp_node); + node =avc_compute_av (ns, ssid, tsid, tclass, avd, &xp_node);else memcpy (avd, &node->ae.avd,sizeof (*avd)); denied = requested & ~(avd->allowed);if (unlikely (denied)) - rc =avc_denied (ssid, tsid, tclass, requested,0 ,0 , flags, avd); + rc =avc_denied (ns, ssid, tsid, tclass, requested,0 ,0 , + flags, avd);rcu_read_unlock ();return rc;
@@ -1138,39 +1188,40 @@ inline int avc_has_perm_noaudit(u32 ssid, u32 tsid,* permissions are granted, -%EACCES if any permissions are denied, or * another -errno upon other errors. */ -int avc_has_perm (u32 ssid,u32 tsid,u16 tclass, +int avc_has_perm (struct selinux_ns *ns,u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data *auditdata) {struct av_decision avd;int rc, rc2; - rc =avc_has_perm_noaudit (ssid, tsid, tclass, requested,0 , &avd); + rc =avc_has_perm_noaudit (ns, ssid, tsid, tclass, requested,0 , &avd); - rc2 =avc_audit (ssid, tsid, tclass, requested, &avd, rc, auditdata,0 ); + rc2 =avc_audit (ns, ssid, tsid, tclass, requested, &avd, rc, + auditdata,0 );if (rc2)return rc2;return rc; } -int avc_has_perm_flags (u32 ssid,u32 tsid,u16 tclass, +int avc_has_perm_flags (struct selinux_ns *ns,u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data *auditdata,int flags) {struct av_decision avd;int rc, rc2; - rc =avc_has_perm_noaudit (ssid, tsid, tclass, requested,0 , &avd); + rc =avc_has_perm_noaudit (ns, ssid, tsid, tclass, requested,0 , &avd); - rc2 =avc_audit (ssid, tsid, tclass, requested, &avd, rc, + rc2 =avc_audit (ns, ssid, tsid, tclass, requested, &avd, rc, auditdata, flags);if (rc2)return rc2;return rc; } -u32 avc_policy_seqno (void ) +u32 avc_policy_seqno (struct selinux_ns *ns) { -return avc_cache.latest_notif; +return ns->avc->avc_cache.latest_notif; }void avc_disable (void )
@@ -1187,7 +1238,7 @@ void avc_disable(void)* the cache and get that memory back. */ if (avc_node_cachep) { -avc_flush (); +avc_flush (init_selinux_ns->avc);/* kmem_cache_destroy(avc_node_cachep); */ } }
security/selinux/hooks.c +256 -126
1,080 lines not on this page · raw message
security/selinux/include/avc.h +21 -11
@@ -51,6 +51,7 @@ struct selinux_audit_data {u32 audited;u32 denied;int result; +struct selinux_ns *ns; };/*
@@ -95,7 +96,8 @@ static inline u32 avc_audit_required(u32 requested,return audited; } -int slow_avc_audit (u32 ssid,u32 tsid,u16 tclass, +int slow_avc_audit (struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass,u32 requested,u32 audited,u32 denied,int result,struct common_audit_data *a,unsigned flags);
@@ -120,7 +122,8 @@ int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,* be performed under a lock, to allow the lock to be released * before calling the auditing code. */ -static inline int avc_audit(u32 ssid, u32 tsid, +static inline int avc_audit(struct selinux_ns *ns, + u32 ssid, u32 tsid, u16 tclass, u32 requested, struct av_decision *avd, int result,
@@ -131,31 +134,35 @@ static inline int avc_audit(u32 ssid, u32 tsid, audited =avc_audit_required (requested, avd, result,0 , &denied);if (likely (!audited))return 0 ; -return slow_avc_audit (ssid, tsid, tclass, +return slow_avc_audit (ns, ssid, tsid, tclass, requested, audited, denied, result, a, flags); }#define AVC_STRICT 1/* Ignore permissive mode. */ #define AVC_EXTENDED_PERMS 2/* update extended permissions */ -int avc_has_perm_noaudit (u32 ssid,u32 tsid, +int avc_has_perm_noaudit (struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass,u32 requested,unsigned flags,struct av_decision *avd); -int avc_has_perm (u32 ssid,u32 tsid, +int avc_has_perm (struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data *auditdata); -int avc_has_perm_flags (u32 ssid,u32 tsid, +int avc_has_perm_flags (struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass,u32 requested,struct common_audit_data *auditdata,int flags); -int avc_has_extended_perms (u32 ssid,u32 tsid,u16 tclass,u32 requested, -u8 driver,u8 perm,struct common_audit_data *ad); +int avc_has_extended_perms (struct selinux_ns *ns, +u32 ssid,u32 tsid,u16 tclass,u32 requested, +u8 driver,u8 perm,struct common_audit_data *ad); -u32 avc_policy_seqno (void ); +u32 avc_policy_seqno (struct selinux_ns *ns);#define AVC_CALLBACK_GRANT 1#define AVC_CALLBACK_TRY_REVOKE 2
@@ -170,8 +177,11 @@ u32 avc_policy_seqno(void);int avc_add_callback (int (*callback)(u32 event),u32 events);/* Exported to selinuxfs */ -int avc_get_hash_stats (char *page); -extern unsigned int avc_cache_threshold; +struct selinux_avc ; +int avc_get_hash_stats (struct selinux_avc *avc,char *page); +unsigned int avc_get_cache_threshold (struct selinux_avc *avc); +void avc_set_cache_threshold (struct selinux_avc *avc, +unsigned int cache_threshold);/* Attempt to free avc node cache */ void avc_disable (void );
security/selinux/include/avc_ss.h +2 -1
@@ -8,7 +8,8 @@#include "flask.h" -int avc_ss_reset (u32 seqno); +struct selinux_avc ; +int avc_ss_reset (struct selinux_avc *avc,u32 seqno);/* Class/perm mapping support */ struct security_class_mapping {
security/selinux/include/security.h +5 -0
@@ -92,6 +92,7 @@ extern char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX];/* limitation of boundary depth */ #define POLICYDB_BOUNDS_MAXDEPTH 4 +struct selinux_avc ;struct selinux_ss ;struct selinux_ns {
@@ -104,6 +105,7 @@ struct selinux_ns {bool checkreqprot;bool initialized;bool policycap[__POLICYDB_CAPABILITY_MAX]; +struct selinux_avc *avc;struct selinux_ss *ss;struct selinux_ns *parent; };
@@ -114,6 +116,9 @@ void __put_selinux_ns(struct selinux_ns *ns);int selinux_ss_create (struct selinux_ss **ss);void selinux_ss_free (struct selinux_ss *ss); +int selinux_avc_create (struct selinux_avc **avc); +void selinux_avc_free (struct selinux_avc *avc); +static inline void put_selinux_ns (struct selinux_ns *ns) {if (ns &&refcount_dec_and_test (&ns->count))
security/selinux/netlabel.c +2 -1
@@ -406,7 +406,8 @@ int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec, perm =RAWIP_SOCKET__RECVFROM ; } - rc =avc_has_perm (sksec->sid, nlbl_sid, sksec->sclass, perm, ad); + rc =avc_has_perm (current_selinux_ns, + sksec->sid, nlbl_sid, sksec->sclass, perm, ad);if (rc ==0 )return 0 ;
security/selinux/selinuxfs.c +41 -19
187 lines not on this page · raw message
security/selinux/ss/services.c +5 -4
@@ -2163,7 +2163,7 @@ int security_load_policy(struct selinux_ns *ns, void *data, size_t len) ns->initialized =1 ; seqno = ++ns->ss->latest_granting;selinux_complete_init (); -avc_ss_reset (seqno); +avc_ss_reset (ns->avc, seqno);selnl_notify_policyload (seqno);selinux_status_update_policyload (ns, seqno);selinux_netlbl_cache_invalidate ();
@@ -2245,7 +2245,7 @@ int security_load_policy(struct selinux_ns *ns, void *data, size_t len)sidtab_destroy (&oldsidtab);kfree (oldmapping); -avc_ss_reset (seqno); +avc_ss_reset (ns->avc, seqno);selnl_notify_policyload (seqno);selinux_status_update_policyload (ns, seqno);selinux_netlbl_cache_invalidate ();
@@ -2661,7 +2661,8 @@ int security_get_user_sids(struct selinux_ns *ns, }for (i =0 , j =0 ; i < mynel; i++) {struct av_decision dummy_avd; - rc =avc_has_perm_noaudit (fromsid, mysids[i], + rc =avc_has_perm_noaudit (ns, + fromsid, mysids[i],SECCLASS_PROCESS ,/* kernel value */ PROCESS__TRANSITION ,AVC_STRICT , &dummy_avd);
@@ -2919,7 +2920,7 @@ int security_set_bools(struct selinux_ns *ns, int len, int *values) out:write_unlock_irq (&ns->ss->policy_rwlock);if (!rc) { -avc_ss_reset (seqno); +avc_ss_reset (ns->avc, seqno);selnl_notify_policyload (seqno);selinux_status_update_policyload (ns, seqno);selinux_xfrm_notify_policyload ();
security/selinux/xfrm.c +11 -6
@@ -106,7 +106,8 @@ static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp,if (rc) goto err; - rc =avc_has_perm (tsec->sid, ctx->ctx_sid, + rc =avc_has_perm (current_selinux_ns, + tsec->sid, ctx->ctx_sid,SECCLASS_ASSOCIATION ,ASSOCIATION__SETCONTEXT ,NULL );if (rc) goto err;
@@ -142,7 +143,8 @@ static int selinux_xfrm_delete(struct xfrm_sec_ctx *ctx)if (!ctx)return 0 ; -return avc_has_perm (tsec->sid, ctx->ctx_sid, +return avc_has_perm (current_selinux_ns, + tsec->sid, ctx->ctx_sid,SECCLASS_ASSOCIATION ,ASSOCIATION__SETCONTEXT ,NULL ); }
@@ -164,7 +166,8 @@ int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)if (!selinux_authorizable_ctx (ctx))return -EINVAL ; - rc =avc_has_perm (fl_secid, ctx->ctx_sid, + rc =avc_has_perm (current_selinux_ns, + fl_secid, ctx->ctx_sid,SECCLASS_ASSOCIATION ,ASSOCIATION__POLMATCH ,NULL );return (rc == -EACCES ? -ESRCH : rc); }
@@ -203,7 +206,8 @@ int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,/* We don't need a separate SA Vs. policy polmatch check since the SA * is now of the same label as the flow and a flow Vs. policy polmatch * check had already happened in selinux_xfrm_policy_lookup() above. */ -return (avc_has_perm (fl->flowi_secid, state_sid, +return (avc_has_perm (current_selinux_ns, + fl->flowi_secid, state_sid,SECCLASS_ASSOCIATION ,ASSOCIATION__SENDTO ,NULL ) ?0 :1 ); }
@@ -422,7 +426,8 @@ int selinux_xfrm_sock_rcv_skb(u32 sk_sid, struct sk_buff *skb,/* This check even when there's no association involved is intended, * according to Trent Jaeger, to make sure a process can't engage in * non-IPsec communication unless explicitly allowed by policy. */ -return avc_has_perm (sk_sid, peer_sid, +return avc_has_perm (current_selinux_ns, + sk_sid, peer_sid,SECCLASS_ASSOCIATION ,ASSOCIATION__RECVFROM , ad); }
@@ -465,6 +470,6 @@ int selinux_xfrm_postroute_last(u32 sk_sid, struct sk_buff *skb,/* This check even when there's no association involved is intended, * according to Trent Jaeger, to make sure a process can't engage in * non-IPsec communication unless explicitly allowed by policy. */ -return avc_has_perm (sk_sid,SECINITSID_UNLABELED , +return avc_has_perm (current_selinux_ns, sk_sid,SECINITSID_UNLABELED ,SECCLASS_ASSOCIATION ,ASSOCIATION__SENDTO , ad); }
Signature · 1 line
2.9.5
Re: [RFC 03/10] selinux: move the AVC into the selinux namespace
Quoted 10 lines · Stephen Smalley
Move the access vector cache (AVC) into the selinux namespace structure and pass it explicitly to all AVC functions. The AVC private state is encapsulated in a
selinux_avcstructure that is allocated and freed by the AVC during selinux namespace creation and destruction.This is necessary to support multiple selinux namespaces since the AVC caches state (e.g. SIDs, policy sequence number) that is maintained and provided by the security server on a per-namespace basis.
What about per-namespace AVC stats?
At the moment, it seems that the stats for all AVCs are combined in the existing percpu stats, which could be confusing for someone trying to tune the host or a guest, as the hash stats & config are per-namespace. Also, a user likely wants to see only their own AVC stats generally.
Signature · 2 lines
James Morris <[email protected]>
Re: [RFC 03/10] selinux: move the AVC into the selinux namespace
Quoted 16 lines · James Morris
Quoted 11 lines · Stephen Smalley
Move the access vector cache (AVC) into the selinux namespace structure and pass it explicitly to all AVC functions. The AVC private state is encapsulated in a
selinux_avcstructure that is allocated and freed by the AVC during selinux namespace creation and destruction.This is necessary to support multiple selinux namespaces since the AVC caches state (e.g. SIDs, policy sequence number) that is maintained and provided by the security server on a per- namespace
basis.What about per-namespace AVC stats?
Quoted 7 lines
At the moment, it seems that the stats for all AVCs are combined in the
existing percpu stats, which could be confusing for someone trying to tune
the host or a guest, as the hash stats & config are per- namespace. Also,
a user likely wants to see only their own AVC stats generally.
Yes, we should likely split those too; something else to add to the TODO list for this patch series.
RFC 4/10 netns, selinux: create the selinux netlink socket per network namespace
In reply to Stephen SmalleyThe selinux netlink socket is used to notify userspace of changes to the enforcing mode and policy reloads. At present, these notifications are always sent to the initial network namespace. In order to support multiple selinux namespaces, each with its own enforcing mode and policy, we need to create and use a separate selinux netlink socket for each network namespace.
Without this change, a policy reload in a child selinux namespace causes a notification to be sent to processes in the init namespace with a sequence number that may be higher than the policy sequence number for that namespace. As a result, userspace AVC instances in the init namespace will then end up rejecting any further access vector results from its own security server instance due to the policy sequence number appearing to regress, which in turn causes all subsequent uncached access checks to fail. Similarly, without this change, changing enforcing mode in the child selinux namespace triggers a notification to all userspace AVC instances in the init namespace that will switch their enforcing modes.
This change does alter SELinux behavior, since previously reloading policy or changing enforcing mode in a non-init network namespace would trigger a notification to processes in the init network namespace. However, this behavior is not being relied upon by existing userspace AFAICT and is arguably wrong regardless.
This change presumes that one will always unshare the network namespace when unsharing a new selinux namespace (the reverse is not required). Otherwise, the same inconsistencies could arise between the notifications and the relevant policy. At present, nothing enforces this guarantee at the kernel level; it is left up to userspace (e.g. container runtimes). It is an open question as to whether this is a good idea or whether unsharing of the selinux namespace should automatically unshare the network namespace. However, keeping them separate is consistent with the handling of the mount namespace currently, which also should be unshared so that a private selinuxfs mount can be created.
--- include/net/net_namespace.h | 3 +++ security/selinux/netlink.c | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-)
include/net/net_namespace.h +3 -0
@@ -149,6 +149,9 @@ struct net {#endif struct sock *diag_nlsk;atomic_t fnhe_genid; +#if IS_ENABLED (CONFIG_SECURITY_SELINUX ) +struct sock *selnl; +#endif } __randomize_layout;#include <linux/seq_file_net.h>
security/selinux/netlink.c +25 -6
@@ -22,8 +22,6 @@#include "security.h" -static struct sock *selnl; -static int selnl_msglen (int msgtype) {int ret =0 ;
@@ -69,6 +67,7 @@ static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *static void selnl_notify (int msgtype,void *data) { +struct sock *selnl = current->nsproxy->net_ns->selnl;int len;sk_buff_data_t tmp;struct sk_buff *skb;
@@ -108,16 +107,36 @@ void selnl_notify_policyload(u32 seqno)selnl_notify (SELNL_MSG_POLICYLOAD , &seqno); } -static int __init selnl_init (void ) +static int __net_init selnl_net_init (struct net *net) { +struct sock *sk;struct netlink_kernel_cfg cfg = { .groups =SELNLGRP_MAX , .flags =NL_CFG_F_NONROOT_RECV , }; - selnl =netlink_kernel_create (&init_net,NETLINK_SELINUX , &cfg); -if (selnl ==NULL ) -panic ("SELinux: Cannot create netlink socket." ); + sk =netlink_kernel_create (net,NETLINK_SELINUX , &cfg); +if (!sk) +return -ENOMEM ; + net->selnl = sk; +return 0 ; +} + +static void __net_exit selnl_net_exit(struct net *net) +{ +netlink_kernel_release (net->selnl); + net->selnl =NULL ; +} + +static struct pernet_operations selnl_net_ops = { + .init = selnl_net_init, + .exit = selnl_net_exit, +}; + +static int __init selnl_init (void ) +{ +if (register_pernet_subsys (&selnl_net_ops)) +panic ("Could not register selinux netlink operations\n" );return 0 ; }
Signature · 1 line
2.9.5
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
Quoted 6 lines · Stephen Smalley
The selinux netlink socket is used to notify userspace of changes to the enforcing mode and policy reloads. At present, these notifications are always sent to the initial network namespace. In order to support multiple selinux namespaces, each with its own enforcing mode and policy, we need to create and use a separate selinux netlink socket for each network namespace.
...
security/selinux/netlink.c void selnl_notify_policyload(u32 seqno) · line 141
Quoted 6 lines
security/selinux/netlink.c +4
-0+static int __init selnl_init (void ) +{ +if (register_pernet_subsys (&selnl_net_ops)) +panic ("Could not register selinux netlink operations\n" );return 0 ; }
This doesn't seem right to me. If the socket is only used to send notifications to userspace, then every net_ns doesn't need a socket, only the first netns that the selinux ns was associated, right?
So long as there is a way to find the netns to which an selinux ns is tied, a userspace logger could even setns into that netns to listen for updates, if it wasn't certain to be in the right ns when it ran.
Otherwise (I haven't peeked ahead) you'll have to keep the *list* of net_ns which live in a given selinuxfs and copy all messages to all of those namesapces?
-serge
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
Quoted 13 lines · Serge E. Hallyn
Quoted 9 lines · Stephen Smalley
The selinux netlink socket is used to notify userspace of changes to
the enforcing mode and policy reloads. At present, these notifications
are always sent to the initial network namespace. In order to support
multiple selinux namespaces, each with its own enforcing mode and policy, we need to create and use a separate selinux netlink socket for each network namespace....
Quoted 11 lines
Quoted 7 lines
security/selinux/netlink.c +4
-0+static int __init selnl_init (void ) +{ +if (register_pernet_subsys (&selnl_net_ops)) + panic("Could not register selinux netlinkoperations\n");
return 0;
}This doesn't seem right to me. If the socket is only used to send notifications to userspace, then every
net_nsdoesn't need a socket, only the first netns that the selinux ns was associated, right?
What does "the first netns that the selinux ns was associated" mean? We could unshare them in any order; in the sample command sequence I gave in the patch description for "selinux: add a selinuxfs interface to unshare selinux namespace", I unshared the SELinux namespace first, then the network namespace, but we could just as easily do it in the reverse order (or at the same time if unshare(2) supported that). So you can't assume that the network namespace in which you are running at the time you unshare selinux namespace is the right one, nor that the first unshare of the network namespace after unsharing the selinux namespace is the right one (not that we even have a way to catch that currently).
Quoted 9 lines
So long as there is a way to find the netns to which an selinux ns is tied, a userspace logger could even setns into that netns to listen
for updates, if it wasn't certain to be in the right ns when it ran.Otherwise (I haven't peeked ahead) you'll have to keep the *list* of
net_nswhich live in a given selinuxfs and copy all messages to all of
those namesapces?
No, we only deliver to the network namespace of the process that performed the setenforce or policy load (most commonly init, could also be an admin running a management command or installing a policy rpm). We assume the container runtime properly handles unsharing of the mount, network, and selinux namespaces before launching the container init. A container process that subsequently unshares its network namespace won't see notifications for any subsequent policy reloads or setenforce calls. I don't know if that will prove to be a problem in practice.
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
Quoted 54 lines · Stephen Smalley
Quoted 14 lines · Serge E. Hallyn
Quoted 10 lines · Stephen Smalley
The selinux netlink socket is used to notify userspace of changes to
the enforcing mode and policy reloads. At present, these notifications
are always sent to the initial network namespace. In order to support
multiple selinux namespaces, each with its own enforcing mode and policy, we need to create and use a separate selinux netlink socket
for each network namespace....
Quoted 12 lines
Quoted 7 lines
security/selinux/netlink.c +4
-0+static int __init selnl_init (void ) +{ +if (register_pernet_subsys (&selnl_net_ops)) + panic("Could not register selinux netlinkoperations\n");
return 0;
}This doesn't seem right to me. If the socket is only used to send notifications to userspace, then every
net_nsdoesn't need a socket,
only the first netns that the selinux ns was associated, right?What does "the first netns that the selinux ns was associated" mean? We could unshare them in any order; in the sample command sequence I gave in the patch description for "selinux: add a selinuxfs interface to unshare selinux namespace", I unshared the SELinux namespace first,
then the network namespace, but we could just as easily do it in the reverse order (or at the same time if unshare(2) supported that). So you can't assume that the network namespace in which you are running at
the time you unshare selinux namespace is the right one, nor that the first unshare of the network namespace after unsharing the selinux namespace is the right one (not that we even have a way to catch that currently).Quoted 11 lines
So long as there is a way to find the netns to which an selinux ns is tied, a userspace logger could even setns into that netns to listen
for updates, if it wasn't certain to be in the right ns when it ran.Otherwise (I haven't peeked ahead) you'll have to keep the *list* of
net_nswhich live in a given selinuxfs and copy all messages to all of
those namesapces?
Quoted 12 lines
No, we only deliver to the network namespace of the process that performed the setenforce or policy load (most commonly init, could also
be an admin running a management command or installing a policy rpm).
We assume the container runtime properly handles unsharing of the mount, network, and selinux namespaces before launching the container init. A container process that subsequently unshares its network namespace won't see notifications for any subsequent policy reloads or
setenforce calls. I don't know if that will prove to be a problem in practice.
It should be noted however that this wouldn't be a regression, since today the netlink notifications are only delivered to the init network namespace.
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
In reply to Stephen SmalleyQuoted 43 lines · Stephen Smalley
Quoted 13 lines · Serge E. Hallyn
Quoted 9 lines · Stephen Smalley
The selinux netlink socket is used to notify userspace of changes to
the enforcing mode and policy reloads. At present, these notifications
are always sent to the initial network namespace. In order to support
multiple selinux namespaces, each with its own enforcing mode and policy, we need to create and use a separate selinux netlink socket for each network namespace....
Quoted 11 lines
Quoted 7 lines
security/selinux/netlink.c +4
-0+static int __init selnl_init (void ) +{ +if (register_pernet_subsys (&selnl_net_ops)) + panic("Could not register selinux netlinkoperations\n");
return 0;
}This doesn't seem right to me. If the socket is only used to send notifications to userspace, then every
net_nsdoesn't need a socket, only the first netns that the selinux ns was associated, right?What does "the first netns that the selinux ns was associated" mean? We could unshare them in any order; in the sample command sequence I gave in the patch description for "selinux: add a selinuxfs interface to unshare selinux namespace", I unshared the SELinux namespace first, then the network namespace, but we could just as easily do it in the reverse order (or at the same time if unshare(2) supported that). So you can't assume that the network namespace in which you are running at the time you unshare selinux namespace is the right one, nor that the first unshare of the network namespace after unsharing the selinux namespace is the right one (not that we even have a way to catch that currently).
Quoted 5 lines
So long as there is a way to find the netns to which an selinux ns is tied, a userspace logger could even setns into that netns to listen
for updates, if it wasn't certain to be in the right ns when it ran.
Quoted 7 lines
Quoted 4 lines
Otherwise (I haven't peeked ahead) you'll have to keep the *list* of
net_nswhich live in a given selinuxfs and copy all messages to all of
those namesapces?No, we only deliver to the network namespace of the process that performed the setenforce or policy load (most commonly init, could also
(Oops, I see I never replied to this,)
I see - sounds good then, thanks.
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
In reply to Stephen SmalleyQuoted 8 lines · Stephen Smalley
This change presumes that one will always unshare the network namespace when unsharing a new selinux namespace (the reverse is not required). Otherwise, the same inconsistencies could arise between the notifications and the relevant policy. At present, nothing enforces this guarantee at the kernel level; it is left up to userspace (e.g. container runtimes). It is an open question as to whether this is a good idea or whether unsharing of the selinux namespace should automatically unshare the network namespace.
What about logging a kernel warning if just SELinux is unshared?
I think we want to avoid surprising the user by unsharing things for them, and yes, it will be possible to mess your system up if you configure it badly.
Quoted 3 lines
However, keeping them separate is consistent with the handling of the mount namespace currently, which also should be unshared so that a private selinuxfs mount can be created.
Right, and this will in practice always be automated and abstracted from an end user pov.
Signature · 2 lines
James Morris <[email protected]>
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
Quoted 17 lines · James Morris
Quoted 6 lines · Stephen Smalley
This change presumes that one will always unshare the network namespace
when unsharing a new selinux namespace (the reverse is not required).
Otherwise, the same inconsistencies could arise between the notificationsQuoted 8 lines
and the relevant policy. At present, nothing enforces this guarantee
at the kernel level; it is left up to userspace (e.g. container runtimes).
It is an open question as to whether this is a good idea or whether unsharing of the selinux namespace should automatically unshare the network
namespace.
Quoted 1 line
What about logging a kernel warning if just SELinux is unshared?
As with Serge's suggestion, the problem is that one can unshare them in any order, and potentially with intervening steps to set up the namespace or prepare for doing so, so there is no obvious point where you could detect and issue such a warning. Without an interface that allows unsharing them both simultaneously (either unshare(2)-based or selinuxfs-based), I don't think we can provide such a warning.
I don't think it will prove to be a problem in practice however; container runtimes just need to do the right thing (and we can help this by providing helpers in libselinux or the like). The larger concern is not that we'll forget to unshare the network namespace when we unshare the selinux namespace, but that subsequent further unsharing of the network namespace by itself could cause lossage of notifications. The two cases of concern are that a process unshares its network namespace again (after the original unsharing of both selinux namespace and network namespace for the container creation) and subsequently:
1) Does not get any netlink notifications of setenforce or policy load events for its selinux namespace. This is only an issue if a program that uses the userspace AVC also unshares its network namespace or otherwise is launched into its own network namespace separate from that of its container. And it isn't a regression, since before this change notifications would only be sent to the init network namespace ever, so this change actually represents an improvement in the ability to at least get notifications when running in the container's network namespace.
2) Sets enforcing mode or loads policy itself, in which case the notification for its setenforce or load_policy will only go to its network namespace and will not be received by other processes in the same selinux namespace. This is only an issue if a process running in a separate network namespace from that of its container sets enforcing mode or loads policy. This seems unlikely to me, since such setting of enforcing mode or loading of policy will conventionally be restricted to a small set of privileged processes, such as the container init process, administrator shells, and package installation/updates, and I wouldn't expect them to run in a separate network namespace than their container.
Quoted 15 lines
I think we want to avoid surprising the user by unsharing things for them,
and yes, it will be possible to mess your system up if you configure it
badly.Quoted 4 lines
However, keeping them separate is consistent with the handling of the mount namespace currently, which also should be unshared so that
a private selinuxfs mount can be created.Right, and this will in practice always be automated and abstracted from
an end user pov.
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
Quoted 18 lines · Stephen Smalley
Quoted 17 lines · James Morris
Quoted 14 lines · Stephen Smalley
This change presumes that one will always unshare the network namespace
when unsharing a new selinux namespace (the reverse is not required).
Otherwise, the same inconsistencies could arise between the notifications
and the relevant policy. At present, nothing enforces this guarantee
at the kernel level; it is left up to userspace (e.g. container runtimes).
It is an open question as to whether this is a good idea or whether unsharing of the selinux namespace should automatically unshare the network
namespace.
Quoted 4 lines
Quoted 1 line
What about logging a kernel warning if just SELinux is unshared?
As with Serge's suggestion, the problem is that one can unshare them in any order, and potentially with intervening steps to set up the
But is it going to stay that way? I thought that was a temporary thing while you test it out.
Because as it is it seems unacceptably loose. A few questions:
(thinking as I type here - ok only the last one is the real question)
Assume I'm in my given netns. I unshare just my selinuxns. Does the selinuxns have the authority to decide things about that netns?
If I unshare selinuxns+netns, then the selinuxns clearly "owns" the netns, so the selinuxns has clear authority. Likewise, when I unshare the netns after the selinuxns, from that point on the selinuxns can be said to have authority over the netns.
I think you want to keep it completely orthogonal, and I guess so long as the parent selinuxns policy still applies it's ok.
So I unshare my selinuxns but not userns. I don't have CAP_MAC_ADMIN against the user_ns, so any selinux related changes will be denied. Once I unshare userns, they will be allowed.
Ah, right. Here's the real question. Policy dictates file transition rules. How will those be namespaced? Will only the init_selinux_ns be allowed to specify a file context? You can't let ns_capable(current_selinux_ns, CAP_MAC_ADMIN) guide that safely, but capable(CAP_MAC_ADMIN) will be very restrictive. What's the plan there? Sorry if it's spelled out elsewhere.
-serge
Re: [RFC 04/10] netns, selinux: create the selinux netlink socket per network namespace
Quoted 53 lines · Serge E. Hallyn
Quoted 20 lines · Stephen Smalley
Quoted 19 lines · James Morris
Quoted 16 lines · Stephen Smalley
This change presumes that one will always unshare the network namespace
when unsharing a new selinux namespace (the reverse is not required).
Otherwise, the same inconsistencies could arise between the notifications
and the relevant policy. At present, nothing enforces this guarantee
at the kernel level; it is left up to userspace (e.g. container runtimes).
It is an open question as to whether this is a good idea or whether
unsharing of the selinux namespace should automatically unshare the
network
namespace.Quoted 5 lines
Quoted 1 line
What about logging a kernel warning if just SELinux is unshared?
As with Serge's suggestion, the problem is that one can unshare them in
any order, and potentially with intervening steps to set up theBut is it going to stay that way? I thought that was a temporary thing
while you test it out.Because as it is it seems unacceptably loose. A few questions:
(thinking as I type here - ok only the last one is the real question)
Assume I'm in my given netns. I unshare just my selinuxns. Does the selinuxns have the authority to decide things about that netns?
If I unshare selinuxns+netns, then the selinuxns clearly "owns" the netns,
so the selinuxns has clear authority. Likewise, when I unshare the netns after the selinuxns, from that point on the selinuxns can be said
to have authority over the netns.I think you want to keep it completely orthogonal, and I guess so long
as the parent selinuxns policy still applies it's ok.So I unshare my selinuxns but not userns. I don't have
CAP_MAC_ADMINagainst theuser_ns, so any selinux related changes will be denied. Once I unshare userns, they will be allowed.
Quoted 9 lines
Ah, right. Here's the real question. Policy dictates file transition
rules. How will those be namespaced? Will only theinit_selinux_nsbe
allowed to specify a file context? You can't let ns_capable(current_selinux_ns,CAP_MAC_ADMIN) guide that safely, butcapable(CAP_MAC_ADMIN)will be very restrictive. What's the plan there? Sorry if it's spelled out elsewhere.
I don't think we know yet. The current patches support a separate and distinct in-core inode security context for each namespace, which supports scenarios such as using context mounts on the host OS to label each container with a single context with MCS separation while using per-file xattrs within a container to support a conventional targeted policy, but we haven't yet resolved how to deal with the actual persistent file security contexts, e.g. whether there will be only one (possibly with a label mapping defined) or multiple.
RFC 5/10 selinux: support per-task/cred selinux namespace
In reply to Stephen SmalleyExtend the task security structure to include a reference to the associated selinux namespace, and to also contain a pointer to the cred in the parent namespace. The current selinux namespace is changed to the per-task/cred selinux namespace for the current task/cred.
This change makes it possible to support per-cred selinux namespaces, but does not yet introduce a mechanism for unsharing of the selinux namespace. Thus, by itself, this change does not alter the existing situation with respect to all processes still using a single init selinux namespace.
An alternative would be to hang the selinux namespace off of the user namespace, which itself is associated with the cred. This seems undesirable however since DAC and MAC are orthogonal, and there appear to be real use cases where one will want to use selinux namespaces without user namespaces and vice versa. However, one advantage of hanging off the user namespace would be that it is already associated with other namespaces, such as the network namespace, thus potentially facilitating looking up the relevant selinux namespace from the network input/forward hooks. In most cases however, it appears that we could instead copy a reference to the creating task selinux namespace to sock security structures and use that in those hooks.
Introduce a task_security() helper to obtain the correct task/cred security structure from the hooks, and update the hooks to use it. This returns a pointer to the security structure for the task in the same selinux namespace as the caller, or if there is none, a fake security structure with the well-defined unlabeled SIDs. This ensures that we return a valid result that can be used for permission checks and for returning contexts from e.g. reading /proc/pid/attr files.
I am not signing off on this or most subsequent patches as I am not yet convinced that this is the right approach.
--- security/selinux/hooks.c | 34 ++++++++++++++++++++++++++++++++-- security/selinux/include/objsec.h | 9 --------- security/selinux/include/security.h | 13 ++++++++++++- 3 files changed, 44 insertions(+), 12 deletions(-)
security/selinux/hooks.c +32 -2
@@ -213,6 +213,7 @@ static void cred_init_security(void)panic ("SELinux: Failed to initialize initial task.\n" ); tsec->osid = tsec->sid =SECINITSID_KERNEL ; + tsec->ns =get_selinux_ns (init_selinux_ns); cred->security = tsec; }
@@ -227,15 +228,35 @@ static inline u32 cred_sid(const struct cred *cred)return tsec->sid; } +static struct task_security_struct unlabeled_task_security = { + .osid =SECINITSID_UNLABELED , + .sid =SECINITSID_UNLABELED , +}; + +static const struct task_security_struct *task_security ( +const struct task_struct *p) +{ +const struct task_security_struct *tsec; + + tsec =__task_cred (p)->security; +while (tsec->ns != current_selinux_ns && tsec->parent_cred) + tsec = tsec->parent_cred->security; +if (tsec->ns != current_selinux_ns) +return &unlabeled_task_security; +return tsec; +} +/* * get the objective security ID of a task */ static inline u32 task_sid (const struct task_struct *task) { +const struct task_security_struct *tsec;u32 sid;rcu_read_lock (); - sid =cred_sid (__task_cred (task)); + tsec =task_security (task); + sid = tsec->sid;rcu_read_unlock ();return sid; }
@@ -3900,6 +3921,9 @@ static void selinux_cred_free(struct cred *cred)*/ BUG_ON (cred->security && (unsigned long ) cred->security <PAGE_SIZE ); cred->security = (void *)0x7UL ; +put_selinux_ns (tsec->ns); +if (tsec->parent_cred) +put_cred (tsec->parent_cred);kfree (tsec); }
@@ -3918,6 +3942,9 @@ static int selinux_cred_prepare(struct cred *new, const struct cred *old,if (!tsec)return -ENOMEM ; + tsec->ns =get_selinux_ns (old_tsec->ns); +if (old_tsec->parent_cred) + tsec->parent_cred =get_cred (old_tsec->parent_cred); new->security = tsec;return 0 ; }
@@ -3931,6 +3958,9 @@ static void selinux_cred_transfer(struct cred *new, const struct cred *old)struct task_security_struct *tsec = new->security; *tsec = *old_tsec; + tsec->ns =get_selinux_ns (old_tsec->ns); +if (old_tsec->parent_cred) + tsec->parent_cred =get_cred (old_tsec->parent_cred); }/*
@@ -6054,7 +6084,7 @@ static int selinux_getprocattr(struct task_struct *p,unsigned len;rcu_read_lock (); - __tsec =__task_cred (p)->security; + __tsec =task_security (p);if (current != p) { error = avc_has_perm(current_selinux_ns,
security/selinux/include/objsec.h +0 -9
@@ -29,15 +29,6 @@#include "flask.h" #include "avc.h" -struct task_security_struct { -u32 osid;/* SID prior to last execve */ -u32 sid;/* current SID */ -u32 exec_sid;/* exec SID */ -u32 create_sid;/* fscreate SID */ -u32 keycreate_sid;/* keycreate SID */ -u32 sockcreate_sid;/* fscreate SID */ -}; -/* * get the subjective security ID of the current task */
security/selinux/include/security.h +12 -1
@@ -133,7 +133,18 @@ static inline struct selinux_ns *get_selinux_ns(struct selinux_ns *ns)extern struct selinux_ns *init_selinux_ns; -#define current_selinux_ns (init_selinux_ns) +struct task_security_struct { +u32 osid;/* SID prior to last execve */ +u32 sid;/* current SID */ +u32 exec_sid;/* exec SID */ +u32 create_sid;/* fscreate SID */ +u32 keycreate_sid;/* keycreate SID */ +u32 sockcreate_sid;/* fscreate SID */ +struct selinux_ns *ns;/* selinux namespace */ +const struct cred *parent_cred;/* cred in parent ns */ +}; + +#define current_selinux_ns (((struct task_security_struct *)current_security())->ns)#define ss_initialized (current_selinux_ns->initialized)
Signature · 1 line
2.9.5
Re: [RFC 05/10] selinux: support per-task/cred selinux namespace
Quoted 5 lines · Stephen Smalley
An alternative would be to hang the selinux namespace off of the user namespace, which itself is associated with the cred. This seems undesirable however since DAC and MAC are orthogonal, and there appear to be real use cases where one will want to use selinux namespaces without user namespaces and vice versa.
Indeed, an Oracle use-case is for privileged containers and for this MAC must remain separate.
Signature · 2 lines
James Morris <[email protected]>
Re: [RFC 05/10] selinux: support per-task/cred selinux namespace
Quoted 10 lines · James Morris
Quoted 5 lines · Stephen Smalley
An alternative would be to hang the selinux namespace off of the user namespace, which itself is associated with the cred. This seems undesirable however since DAC and MAC are orthogonal, and there appear to be real use cases where one will want to use selinux namespaces without user namespaces and vice versa.
Indeed, an Oracle use-case is for privileged containers and for this MAC must remain separate.
Will that always be the case? Is that to allow (selinux-confined) device administration from containers?
Re: [RFC 05/10] selinux: support per-task/cred selinux namespace
Quoted 9 lines · Serge E. Hallyn
Quoted 8 lines · James Morris
Quoted 5 lines · Stephen Smalley
An alternative would be to hang the selinux namespace off of the user namespace, which itself is associated with the cred. This seems undesirable however since DAC and MAC are orthogonal, and there appear to be real use cases where one will want to use selinux namespaces without user namespaces and vice versa.
Quoted 5 lines
Quoted 2 lines
Indeed, an Oracle use-case is for privileged containers and for this MAC must remain separate.
Will that always be the case? Is that to allow (selinux-confined) device administration from containers?
It's to provide the user with a full OS experience generally. It's not necessarily the only use-case, though.
Signature · 2 lines
James Morris <[email protected]>
RFC 6/10 selinux: introduce cred_selinux_ns() and use it
In reply to Stephen SmalleyWhen using the SID from a cred, we should pass the selinux namespace associated with the cred on security server calls rather than the current selinux namespace, since they could differ. In some of these cases, the cred is always obtained from the current task so there is no real change, but this is cleaner and hopefully less fragile. In other cases, the cred could in fact differ.
--- security/selinux/hooks.c | 42 ++++++++++++++++++------------------- security/selinux/include/security.h | 2 ++ 2 files changed, 23 insertions(+), 21 deletions(-)
security/selinux/hooks.c +21 -21
@@ -483,13 +483,13 @@ static int may_context_mount_sb_relabel(u32 sid,const struct task_security_struct *tsec = cred->security;int rc; - rc =avc_has_perm (current_selinux_ns, + rc =avc_has_perm (cred_selinux_ns (cred), tsec->sid, sbsec->sid,SECCLASS_FILESYSTEM ,FILESYSTEM__RELABELFROM ,NULL );if (rc)return rc; - rc =avc_has_perm (current_selinux_ns, + rc =avc_has_perm (cred_selinux_ns (cred), tsec->sid, sid,SECCLASS_FILESYSTEM ,FILESYSTEM__RELABELTO ,NULL );return rc;
@@ -501,13 +501,13 @@ static int may_context_mount_inode_relabel(u32 sid, {const struct task_security_struct *tsec = cred->security;int rc; - rc =avc_has_perm (current_selinux_ns, + rc =avc_has_perm (cred_selinux_ns (cred), tsec->sid, sbsec->sid,SECCLASS_FILESYSTEM ,FILESYSTEM__RELABELFROM ,NULL );if (rc)return rc; - rc =avc_has_perm (current_selinux_ns, + rc =avc_has_perm (cred_selinux_ns (cred), sid, sbsec->sid,SECCLASS_FILESYSTEM ,FILESYSTEM__ASSOCIATE ,NULL );return rc;
@@ -1793,10 +1793,10 @@ static int cred_has_capability(const struct cred *cred,return -EINVAL ; } - rc =avc_has_perm_noaudit (current_selinux_ns, + rc =avc_has_perm_noaudit (cred_selinux_ns (cred), sid, sid, sclass, av,0 , &avd);if (audit ==SECURITY_CAP_AUDIT ) { -int rc2 =avc_audit (current_selinux_ns, +int rc2 =avc_audit (cred_selinux_ns (cred), sid, sid, sclass, av, &avd, rc, &ad,0 );if (rc2)return rc2;
@@ -1823,7 +1823,7 @@ static int inode_has_perm(const struct cred *cred, sid =cred_sid (cred); isec = inode->i_security; -return avc_has_perm (current_selinux_ns, +return avc_has_perm (cred_selinux_ns (cred), sid, isec->sid, isec->sclass, perms, adp); }
@@ -1893,7 +1893,7 @@ static int file_has_perm(const struct cred *cred, ad.u.file = file;if (sid != fsec->sid) { - rc = avc_has_perm(current_selinux_ns, + rc = avc_has_perm(cred_selinux_ns(cred), sid, fsec->sid, SECCLASS_FD, FD__USE,
@@ -2101,7 +2101,7 @@ static int superblock_has_perm(const struct cred *cred,u32 sid =cred_sid (cred); sbsec = sb->s_security; -return avc_has_perm (current_selinux_ns, +return avc_has_perm (cred_selinux_ns (cred), sid, sbsec->sid,SECCLASS_FILESYSTEM , perms, ad); }
@@ -2282,7 +2282,7 @@ static int selinux_capset(struct cred *new, const struct cred *old,const kernel_cap_t *inheritable, const kernel_cap_t *permitted) { - return avc_has_perm(current_selinux_ns, + return avc_has_perm(cred_selinux_ns(old),cred_sid (old ),cred_sid (new ),SECCLASS_PROCESS ,PROCESS__SETCAP ,NULL ); }
@@ -3122,7 +3122,7 @@ static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,if (IS_ERR (isec))return PTR_ERR (isec); -return avc_has_perm_flags (current_selinux_ns, +return avc_has_perm_flags (cred_selinux_ns (cred), sid, isec->sid, isec->sclass,FILE__READ , &ad, rcu ?MAY_NOT_BLOCK :0 ); }
@@ -3178,7 +3178,7 @@ static int selinux_inode_permission(struct inode *inode, int mask)if (IS_ERR (isec))return PTR_ERR (isec); - rc =avc_has_perm_noaudit (current_selinux_ns, + rc =avc_has_perm_noaudit (cred_selinux_ns (cred), sid, isec->sid, isec->sclass, perms,0 , &avd); audited = avc_audit_required(perms, &avd, rc, from_access ? FILE__AUDIT_ACCESS : 0,
@@ -3584,7 +3584,7 @@ static int ioctl_has_perm(const struct cred *cred, struct file *file, ad.u.op->path = file->f_path;if (ssid != fsec->sid) { - rc = avc_has_perm(current_selinux_ns, + rc = avc_has_perm(cred_selinux_ns(cred), ssid, fsec->sid, SECCLASS_FD, FD__USE,
@@ -3667,7 +3667,7 @@ static int file_map_prot_check(struct file *file, unsigned long prot, int shared* private file mapping that will also be writable. * This has an additional check. */ - rc =avc_has_perm (current_selinux_ns, + rc =avc_has_perm (cred_selinux_ns (cred), sid, sid,SECCLASS_PROCESS ,PROCESS__EXECMEM ,NULL );if (rc)
@@ -3743,14 +3743,14 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,int rc =0 ;if (vma->vm_start >= vma->vm_mm->start_brk && vma->vm_end <= vma->vm_mm->brk) { - rc =avc_has_perm (current_selinux_ns, + rc =avc_has_perm (cred_selinux_ns (cred), sid, sid,SECCLASS_PROCESS ,PROCESS__EXECHEAP ,NULL ); }else if (!vma->vm_file && ((vma->vm_start <= vma->vm_mm->start_stack && vma->vm_end >= vma->vm_mm->start_stack) ||vma_is_stack_for_current (vma))) { - rc =avc_has_perm (current_selinux_ns, + rc =avc_has_perm (cred_selinux_ns (cred), sid, sid,SECCLASS_PROCESS ,PROCESS__EXECSTACK ,NULL ); }else if (vma->vm_file && vma->anon_vma) {
@@ -3870,7 +3870,7 @@ static int selinux_file_open(struct file *file, const struct cred *cred)* struct as its SID. */ fsec->isid = isec->sid; - fsec->pseqno =avc_policy_seqno (current_selinux_ns); + fsec->pseqno =avc_policy_seqno (cred_selinux_ns (cred));/* * Since the inode label or policy seqno may have changed * between the selinux_inode_permission check and the saving
@@ -3973,7 +3973,7 @@ static int selinux_kernel_act_as(struct cred *new, u32 secid)u32 sid =current_sid ();int ret; - ret = avc_has_perm(current_selinux_ns, + ret = avc_has_perm(tsec->ns, sid, secid, SECCLASS_KERNEL_SERVICE, KERNEL_SERVICE__USE_AS_OVERRIDE,
@@ -3998,7 +3998,7 @@ static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)u32 sid =current_sid ();int ret; - ret = avc_has_perm(current_selinux_ns, + ret = avc_has_perm(tsec->ns, sid, isec->sid, SECCLASS_KERNEL_SERVICE, KERNEL_SERVICE__CREATE_FILES_AS,
@@ -4128,7 +4128,7 @@ int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, av |=PROCESS__SETRLIMIT ;if (flags &LSM_PRLIMIT_READ ) av |=PROCESS__GETRLIMIT ; -return avc_has_perm (current_selinux_ns, +return avc_has_perm (cred_selinux_ns (cred),cred_sid (cred),cred_sid (tcred),SECCLASS_PROCESS , av,NULL ); }
@@ -6371,7 +6371,7 @@ static int selinux_key_permission(key_ref_t key_ref, key =key_ref_to_ptr (key_ref); ksec = key->security; -return avc_has_perm (current_selinux_ns, +return avc_has_perm (cred_selinux_ns (cred), sid, ksec->sid,SECCLASS_KEY , perm,NULL ); }
security/selinux/include/security.h +2 -0
@@ -146,6 +146,8 @@ struct task_security_struct {#define current_selinux_ns (((struct task_security_struct *)current_security())->ns) +#define cred_selinux_ns (cred) (((struct task_security_struct *)(cred)->security)->ns) +#define ss_initialized (current_selinux_ns->initialized)#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Signature · 1 line
2.9.5
RFC 7/10 selinux: support per-namespace inode security structures
In reply to Stephen SmalleyExtend the inode security structure to include a reference to the associated selinux namespace, and turn it into a list so that we can maintain per-inode security state for each namespace. This is necessary since the inode SIDs are per-namespace and multiple namespaces may access the same inodes.
Introduce a find_isec() helper to find the correct inode security structure for the current selinux namespace, creating one if one does not already exist. Update the existing inode_security*() helpers to use this helper, to pass the per-namespace inode security structure for initialization, and to return the resulting inode security structure to the caller. Replace direct references to inode->i_security with the appropriate helper or to use the returned result.
This change is problematic in several respects, so it is unclear it will survive in the final implementation. Some of the issues are:
1) The inode security structures pin the selinux namespace in memory for all namespaces that ever access the inode, preventing timely (or possibly ever) freeing of the namespace.
2) Not everything in the inode security structure needs to be replicated per namespace, so this is wasteful and potentially confusing (e.g. inode back pointer, rcu head - but that overlaps with the list which does need to be per-namespace, sclass unless we anticipate policy-driven file class assignments in the future as we already have for sockets).
3) It is not safe to sleep from all callers of inode_security*(), and thus we cannot always allocate a security blob for the namespace or fetch the xattr on demand. Thus, we could encounter a memory allocation failure or fail to fetch the xattr and map it to a SID in the caller's
namespace. This is not handled safely currently!
4) We only support a single security.selinux xattr, which must be mappable to a SID in every namespace that accesses the inodes (or it will be mapped to the unlabeled SID, which must then be accessible if we wish to permit access from that namespace).
5) We do not yet properly handle setxattr of security.selinux; at present, it will modify the on-disk xattr but will only update the in-core SID for the current namespace and could leave other namespaces out of sync until the inode is evicted and refetched.
--- security/selinux/hooks.c | 137 +++++++++++++++++++++++++++----------- security/selinux/include/objsec.h | 4 ++ security/selinux/selinuxfs.c | 2 +- 3 files changed, 104 insertions(+), 39 deletions(-)
security/selinux/hooks.c +99 -38
@@ -263,28 +263,83 @@ static inline u32 task_sid(const struct task_struct *task)/* Allocate and free functions for each kind of security blob. */ -static int inode_alloc_security (struct inode *inode) +static struct inode_security_struct *isec_alloc (bool may_sleep) {struct inode_security_struct *isec;u32 sid =current_sid (); +gfp_t flags = may_sleep ?GFP_NOFS :GFP_NOWAIT ; - isec =kmem_cache_zalloc (sel_inode_cache,GFP_NOFS ); + isec =kmem_cache_zalloc (sel_inode_cache, flags);if (!isec) -return -ENOMEM ; +return NULL ;spin_lock_init (&isec->lock);INIT_LIST_HEAD (&isec->list); - isec->inode = inode; isec->sid =SECINITSID_UNLABELED ; isec->sclass =SECCLASS_FILE ; isec->task_sid = sid; isec->initialized =LABEL_INVALID ; - inode->i_security = isec; + isec->ns =get_selinux_ns (current_selinux_ns); +INIT_LIST_HEAD (&isec->isec_list); +return isec; +} + +static int inode_alloc_security (struct inode *inode) +{ +struct inode_security_struct *isec =isec_alloc (true); + +if (!isec) +return -ENOMEM ; + isec->inode = inode; + inode->i_security = isec;return 0 ; } -static int inode_doinit_with_dentry (struct inode *inode,struct dentry *opt_dentry); +static int inode_doinit_with_dentry (struct inode *inode, +struct dentry *opt_dentry, +struct inode_security_struct *isec); + +static struct inode_security_struct *find_isec (struct inode *inode, +bool may_sleep) +{ +struct inode_security_struct *isec = inode->i_security; +struct inode_security_struct *cur, *new; + +if (isec->ns == current_selinux_ns) +return isec; + +spin_lock (&isec->lock); + +list_for_each_entry (cur, &isec->isec_list, isec_list) { +if (cur->ns == current_selinux_ns) + goto out; + } + +spin_unlock (&isec->lock); + + new =isec_alloc (may_sleep); +if (!new) { + cur =NULL ; + goto out; + } + new->inode = inode; + +spin_lock (&isec->lock); + +list_for_each_entry (cur, &isec->isec_list, isec_list) { +if (cur->ns == current_selinux_ns) { +kmem_cache_free (sel_inode_cache, new); + goto out; + } + } + +list_add (&new->isec_list, &isec->isec_list); + cur = new; +out: +spin_unlock (&isec->lock); +return cur; +}/* * Try reloading inode security labels that have been marked as invalid. The
@@ -293,57 +348,52 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent* invalid. The @opt_dentry parameter should be set to a dentry of the inode; * when no dentry is available, set it to NULL instead. */ -static int __inode_security_revalidate (struct inode *inode, -struct dentry *opt_dentry, -bool may_sleep) +static struct inode_security_struct * +__inode_security_revalidate (struct inode *inode, +struct dentry *opt_dentry, +bool may_sleep) { -struct inode_security_struct *isec = inode->i_security; +struct inode_security_struct *isec =find_isec (inode, may_sleep);might_sleep_if (may_sleep);if (ss_initialized && isec->initialized !=LABEL_INITIALIZED ) {if (!may_sleep) -return -ECHILD ; +return ERR_PTR (-ECHILD );/* * Try reloading the inode security label. This will fail if * @opt_dentry is NULL and no dentry for this inode can be * found; in that case, continue using the old label. */ -inode_doinit_with_dentry (inode, opt_dentry); +inode_doinit_with_dentry (inode, opt_dentry, isec); } -return 0 ; +return isec; }static struct inode_security_struct *inode_security_novalidate (struct inode *inode) { -return inode->i_security; +return find_isec (inode, false); }static struct inode_security_struct *inode_security_rcu (struct inode *inode,bool rcu) { -int error; - - error =__inode_security_revalidate (inode,NULL , !rcu); -if (error) -return ERR_PTR (error); -return inode->i_security; +return __inode_security_revalidate (inode,NULL , !rcu); }/* * Get the security label of an inode. */ -static struct inode_security_struct *inode_security (struct inode *inode) +struct inode_security_struct *inode_security (struct inode *inode) { -__inode_security_revalidate (inode,NULL , true); -return inode->i_security; +return __inode_security_revalidate (inode,NULL , true); }static struct inode_security_struct *backing_inode_security_novalidate (struct dentry *dentry) {struct inode *inode =d_backing_inode (dentry); -return inode->i_security; +return find_isec (inode, false); }/*
@@ -353,15 +403,22 @@ static struct inode_security_struct *backing_inode_security(struct dentry *dentr {struct inode *inode =d_backing_inode (dentry); -__inode_security_revalidate (inode, dentry, true); -return inode->i_security; +return __inode_security_revalidate (inode, dentry, true); }static void inode_free_rcu (struct rcu_head *head) { -struct inode_security_struct *isec; +struct inode_security_struct *isec, *entry, *tmp; isec = container_of(head, struct inode_security_struct, rcu); + +list_for_each_entry_safe (entry, tmp, &isec->isec_list, isec_list) { +put_selinux_ns (entry->ns); +kmem_cache_free (sel_inode_cache, entry); + } + +put_selinux_ns (isec->ns); +kmem_cache_free (sel_inode_cache, isec); }
@@ -450,7 +507,7 @@ static void superblock_free_security(struct super_block *sb)static inline int inode_doinit (struct inode *inode) { -return inode_doinit_with_dentry (inode,NULL ); +return inode_doinit_with_dentry (inode,NULL ,find_isec (inode, true)); }enum {
@@ -579,7 +636,8 @@ static int sb_finish_set_opts(struct super_block *sb) sbsec->flags &= ~SBLABEL_MNT ;/* Initialize the root inode. */ - rc =inode_doinit_with_dentry (root_inode, root); + rc =inode_doinit_with_dentry (root_inode, root,find_isec (root_inode, + true));/* Initialize any other inodes associated with the superblock, e.g. inodes created prior to initial policy load or inodes created
@@ -1529,10 +1587,11 @@ static int selinux_genfs_get_sid(struct dentry *dentry, }/* The inode's security attributes must be initialized before first use. */ -static int inode_doinit_with_dentry (struct inode *inode,struct dentry *opt_dentry) +static int inode_doinit_with_dentry (struct inode *inode, +struct dentry *opt_dentry, +struct inode_security_struct *isec) {struct superblock_security_struct *sbsec =NULL ; -struct inode_security_struct *isec = inode->i_security;u32 task_sid, sid =0 ;u16 sclass;struct dentry *dentry;
@@ -1552,7 +1611,8 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent isec->sclass =inode_mode_to_security_class (inode->i_mode); sbsec = inode->i_sb->s_security; -if (!(sbsec->flags &SE_SBINITIALIZED )) { +if (!current_selinux_ns->initialized || + !(sbsec->flags &SE_SBINITIALIZED )) {/* Defer initialization until selinux_complete_init, after the initial policy is loaded and the security server is ready to handle calls. */
@@ -1821,7 +1881,7 @@ static int inode_has_perm(const struct cred *cred,return 0 ; sid =cred_sid (cred); - isec = inode->i_security; + isec =inode_security_novalidate (inode);return avc_has_perm (cred_selinux_ns (cred), sid, isec->sid, isec->sclass, perms, adp);
@@ -3033,7 +3093,8 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,/* Possibly defer initialization to selinux_complete_init. */ if (sbsec->flags &SE_SBINITIALIZED ) { -struct inode_security_struct *isec = inode->i_security; +struct inode_security_struct *isec = +inode_security_novalidate (inode); isec->sclass =inode_mode_to_security_class (inode->i_mode); isec->sid = newsid; isec->initialized =LABEL_INITIALIZED ;
@@ -3133,7 +3194,7 @@ static noinline int audit_inode_permission(struct inode *inode, unsigned flags) {struct common_audit_data ad; -struct inode_security_struct *isec = inode->i_security; +struct inode_security_struct *isec =inode_security_novalidate (inode);int rc; ad.type =LSM_AUDIT_DATA_INODE ;
@@ -4189,7 +4250,7 @@ static int selinux_task_kill(struct task_struct *p, struct siginfo *info,static void selinux_task_to_inode (struct task_struct *p,struct inode *inode) { -struct inode_security_struct *isec = inode->i_security; +struct inode_security_struct *isec =inode_security (inode);u32 sid =task_sid (p);spin_lock (&isec->lock);
@@ -6072,7 +6133,7 @@ static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)static void selinux_d_instantiate (struct dentry *dentry,struct inode *inode) {if (inode) -inode_doinit_with_dentry (inode, dentry); +inode_doinit_with_dentry (inode, dentry,find_isec (inode, true)); } static int selinux_getprocattr(struct task_struct *p,
@@ -6289,7 +6350,7 @@ static void selinux_release_secctx(char *secdata, u32 seclen)static void selinux_inode_invalidate_secctx (struct inode *inode) { -struct inode_security_struct *isec = inode->i_security; +struct inode_security_struct *isec =inode_security_novalidate (inode);spin_lock (&isec->lock); isec->initialized =LABEL_INVALID ;
security/selinux/include/objsec.h +4 -0
@@ -56,6 +56,8 @@ struct inode_security_struct {u16 sclass;/* security class of this object */ unsigned char initialized;/* initialization flag */ spinlock_t lock; +struct selinux_ns *ns; +struct list_head isec_list; };struct file_security_struct {
@@ -141,4 +143,6 @@ struct pkey_security_struct { u32 sid; /* SID of pkey */ }; +struct inode_security_struct *inode_security (struct inode *inode); +#endif /* _SELINUX_OBJSEC_H_ */
security/selinux/selinuxfs.c +1 -1
@@ -1387,7 +1387,7 @@ static int sel_make_bools(struct selinux_fs_info *fsi)if (len >=PAGE_SIZE ) goto out; - isec = (struct inode_security_struct *)inode->i_security; + isec =inode_security (inode); ret =security_genfs_sid (fsi->ns,"selinuxfs" , page,SECCLASS_FILE , &sid);if (ret) {
Signature · 1 line
2.9.5
RFC 8/10 selinux: support per-namespace superblock security structures
In reply to Stephen SmalleyExtend the superblock security structure to include a reference to the associated selinux namespace, and turn it into a list so that we can maintain per-superblock security state for each namespace. This is necessary because the superblock SIDs and labeling behavior are per selinux namespace. It further enables one to context-mount a filesystem with a particular context in one namespace while using xattrs in another, e.g. one might context mount a container filesystem in the init selinux namespace to provide MCS-style isolation of the containers while using per-file xattrs within the container to support conventional SELinux targeted policy.
Introduce a superblock_security() helper to return the superblock security blob for the current selinux namespace and replace direct uses of sb->s_security with calls to it.
Also revert the changes made by
commit a64c54cf0811b8032fdab8c9d52576f0370837fa ("SELinux: pass a superblock to security_fs_use") so that access to the superblock security structure is properly encapsulated and we can support per-namespace structures.
This change has similar problems as with the inode security structure change, see the list of issues in that commit.
--- security/selinux/hooks.c | 109 ++++++++++++++++++++++++++++-------- security/selinux/include/objsec.h | 5 +- security/selinux/include/security.h | 3 +- security/selinux/ss/services.c | 19 ++++--- 4 files changed, 102 insertions(+), 34 deletions(-)
security/selinux/hooks.c +86 -23
@@ -478,33 +478,90 @@ static void file_free_security(struct file *file)kmem_cache_free (file_security_cache, fsec); } -static int superblock_alloc_security (struct super_block *sb) +static struct superblock_security_struct *sbsec_alloc ( +const struct super_block *sb) {struct superblock_security_struct *sbsec; - sbsec =kzalloc (sizeof (struct superblock_security_struct ),GFP_KERNEL ); + sbsec =kzalloc (sizeof (struct superblock_security_struct ),GFP_NOFS );if (!sbsec) -return -ENOMEM ; +return NULL ;mutex_init (&sbsec->lock);INIT_LIST_HEAD (&sbsec->isec_head);spin_lock_init (&sbsec->isec_lock); - sbsec->sb = sb; sbsec->sid =SECINITSID_UNLABELED ; sbsec->def_sid =SECINITSID_FILE ; sbsec->mntpoint_sid =SECINITSID_UNLABELED ; - sb->s_security = sbsec; + sbsec->sb = sb; + sbsec->ns =get_selinux_ns (current_selinux_ns); +INIT_LIST_HEAD (&sbsec->sbsec_list); +return sbsec; +} + +static int superblock_alloc_security (struct super_block *sb) +{ +struct superblock_security_struct *sbsec =sbsec_alloc (sb); + +if (!sbsec) +return -ENOMEM ; + sb->s_security = sbsec;return 0 ; }static void superblock_free_security (struct super_block *sb) { -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec = sb->s_security, *entry, *tmp; sb->s_security =NULL ; +put_selinux_ns (sbsec->ns); +list_for_each_entry_safe (entry, tmp, &sbsec->sbsec_list, sbsec_list) { +put_selinux_ns (entry->ns); +kfree (entry); + }kfree (sbsec); } +static struct superblock_security_struct *superblock_security ( +const struct super_block *sb) +{ +struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *cur, *new; + +if (sbsec->ns == current_selinux_ns) +return sbsec; + +spin_lock (&sbsec->sbsec_list_lock); + +list_for_each_entry (cur, &sbsec->sbsec_list, sbsec_list) { +if (cur->ns == current_selinux_ns) + goto out; + } + +spin_unlock (&sbsec->sbsec_list_lock); + + new =sbsec_alloc (sb); +if (!new) { + cur =NULL ; + goto out; + } + +spin_lock (&sbsec->sbsec_list_lock); + +list_for_each_entry (cur, &sbsec->sbsec_list, sbsec_list) { +if (cur->ns == current_selinux_ns) { +kfree (new); + goto out; + } + } + +list_add (&new->sbsec_list, &sbsec->sbsec_list); + cur = new; +out: +spin_unlock (&sbsec->sbsec_list_lock); +return cur; +} +static inline int inode_doinit (struct inode *inode) {return inode_doinit_with_dentry (inode,NULL ,find_isec (inode, true));
@@ -572,7 +629,7 @@ static int may_context_mount_inode_relabel(u32 sid,static int selinux_is_sblabel_mnt (struct super_block *sb) { -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =superblock_security (sb);return sbsec->behavior ==SECURITY_FS_USE_XATTR || sbsec->behavior == SECURITY_FS_USE_TRANS ||
@@ -591,7 +648,7 @@ static int selinux_is_sblabel_mnt(struct super_block *sb)static int sb_finish_set_opts (struct super_block *sb) { -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =superblock_security (sb);struct dentry *root = sb->s_root;struct inode *root_inode =d_backing_inode (root);int rc =0 ;
@@ -675,7 +732,7 @@ static int selinux_get_mnt_opts(const struct super_block *sb, struct security_mnt_opts *opts) {int rc =0 , i; -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =superblock_security (sb);char *context =NULL ;u32 len;char tmp;
@@ -796,7 +853,7 @@ static int selinux_set_mnt_opts(struct super_block *sb, {const struct cred *cred =current_cred ();int rc =0 , i; -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =superblock_security (sb);const char *name = sb->s_type->name;struct dentry *root = sbsec->sb->s_root;struct inode_security_struct *root_isec;
@@ -932,7 +989,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,* Determine the labeling behavior to use for this * filesystem type. */ - rc =security_fs_use (current_selinux_ns, sb); + rc =security_fs_use (current_selinux_ns, sb->s_type->name, + &sbsec->behavior, &sbsec->sid);if (rc) { printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
@@ -1051,8 +1109,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,static int selinux_cmp_sb_context (const struct super_block *oldsb,const struct super_block *newsb) { -struct superblock_security_struct *old = oldsb->s_security; -struct superblock_security_struct *new = newsb->s_security; +struct superblock_security_struct *old =superblock_security (oldsb); +struct superblock_security_struct *new =superblock_security (newsb);char oldflags = old->flags &SE_MNTMASK ;char newflags = new->flags &SE_MNTMASK ;
@@ -1084,8 +1142,10 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, unsigned long *set_kern_flags) {int rc =0 ; -const struct superblock_security_struct *oldsbsec = oldsb->s_security; -struct superblock_security_struct *newsbsec = newsb->s_security; +const struct superblock_security_struct *oldsbsec = +superblock_security (oldsb); +struct superblock_security_struct *newsbsec = +superblock_security (newsb);int set_fscontext = (oldsbsec->flags &FSCONTEXT_MNT );int set_context = (oldsbsec->flags &CONTEXT_MNT );
@@ -1122,7 +1182,8 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,if (newsbsec->behavior ==SECURITY_FS_USE_NATIVE && !(kern_flags &SECURITY_LSM_NATIVE_LABELS ) && !set_context) { - rc =security_fs_use (current_selinux_ns, newsb); + rc =security_fs_use (current_selinux_ns, newsb->s_type->name, + &newsbsec->behavior, &newsbsec->sid);if (rc) goto out; }
@@ -1603,6 +1664,8 @@ static int inode_doinit_with_dentry(struct inode *inode,if (isec->initialized ==LABEL_INITIALIZED )return 0 ; + sbsec =superblock_security (inode->i_sb); +spin_lock (&isec->lock);if (isec->initialized ==LABEL_INITIALIZED ) goto out_unlock;
@@ -1610,7 +1673,6 @@ static int inode_doinit_with_dentry(struct inode *inode,if (isec->sclass ==SECCLASS_FILE ) isec->sclass =inode_mode_to_security_class (inode->i_mode); - sbsec = inode->i_sb->s_security;if (!current_selinux_ns->initialized || !(sbsec->flags &SE_SBINITIALIZED )) {/* Defer initialization until selinux_complete_init,
@@ -1980,7 +2042,8 @@ selinux_determine_inode_label(const struct task_security_struct *tsec, const struct qstr *name, u16 tclass, u32 *_new_isid) { - const struct superblock_security_struct *sbsec = dir->i_sb->s_security; + const struct superblock_security_struct *sbsec = +superblock_security (dir->i_sb);if ((sbsec->flags &SE_SBINITIALIZED ) && (sbsec->behavior ==SECURITY_FS_USE_MNTPOINT )) {
@@ -2011,7 +2074,7 @@ static int may_create(struct inode *dir,int rc; dsec =inode_security (dir); - sbsec = dir->i_sb->s_security; + sbsec =superblock_security (dir->i_sb); sid = tsec->sid;
@@ -2160,7 +2223,7 @@ static int superblock_has_perm(const struct cred *cred,struct superblock_security_struct *sbsec;u32 sid =cred_sid (cred); - sbsec = sb->s_security; + sbsec =superblock_security (sb);return avc_has_perm (cred_selinux_ns (cred), sid, sbsec->sid,SECCLASS_FILESYSTEM , perms, ad); }
@@ -2885,7 +2948,7 @@ static int selinux_sb_remount(struct super_block *sb, void *data)int rc, i, *flags;struct security_mnt_opts opts;char *secdata, **mount_options; -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =superblock_security (sb);if (!(sbsec->flags &SE_SBINITIALIZED ))return 0 ;
@@ -3079,7 +3142,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,int rc;char *context; - sbsec = dir->i_sb->s_security; + sbsec =superblock_security (dir->i_sb); sid = tsec->sid; newsid = tsec->create_sid;
@@ -3332,7 +3395,7 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,if (strcmp (name,XATTR_NAME_SELINUX ))return selinux_inode_setotherxattr (dentry, name); - sbsec = inode->i_sb->s_security; + sbsec =superblock_security (inode->i_sb);if (!(sbsec->flags &SBLABEL_MNT ))return -EOPNOTSUPP ;
security/selinux/include/objsec.h +4 -1
@@ -68,7 +68,7 @@ struct file_security_struct { };struct superblock_security_struct { -struct super_block *sb;/* back pointer to sb object */ +const struct super_block *sb;/* back pointer to sb object */ u32 sid;/* SID of file system superblock */ u32 def_sid;/* default SID for labeling */ u32 mntpoint_sid;/* SECURITY_FS_USE_MNTPOINT context for files */
@@ -77,6 +77,9 @@ struct superblock_security_struct {struct mutex lock;struct list_head isec_head;spinlock_t isec_lock; +struct selinux_ns *ns; +struct list_head sbsec_list; +spinlock_t sbsec_list_lock; };struct msg_security_struct {
security/selinux/include/security.h +2 -1
@@ -324,7 +324,8 @@ int security_get_allow_unknown(struct selinux_ns *ns);#define SECURITY_FS_USE_NATIVE 7/* use native label support */ #define SECURITY_FS_USE_MAX 7/* Highest SECURITY_FS_USE_XXX */ -int security_fs_use (struct selinux_ns *ns,struct super_block *sb); +int security_fs_use (struct selinux_ns *ns, +const char *fstype,unsigned short *behavior,u32 *sid); int security_genfs_sid(struct selinux_ns *ns, const char *fstype, char *name, u16 sclass,
security/selinux/ss/services.c +10 -9
@@ -2770,16 +2770,17 @@ int security_genfs_sid(struct selinux_ns *ns,/** * security_fs_use - Determine how to handle labeling for a filesystem. -* @sb: superblock in question +* @fstype: filesystem type +* @behavior: labeling behavior +* @sid: SID for filesystem (superblock) */ -int security_fs_use (struct selinux_ns *ns,struct super_block *sb) +int security_fs_use (struct selinux_ns *ns,const char *fstype, +unsigned short *behavior,u32 *sid) {struct policydb *policydb;struct sidtab *sidtab;int rc =0 ;struct ocontext *c; -struct superblock_security_struct *sbsec = sb->s_security; -const char *fstype = sb->s_type->name;read_lock (&ns->ss->policy_rwlock);
@@ -2794,22 +2795,22 @@ int security_fs_use(struct selinux_ns *ns, struct super_block *sb) }if (c) { - sbsec->behavior = c->v.behavior; + *behavior = c->v.behavior;if (!c->sid[0 ]) { rc =sidtab_context_to_sid (sidtab, &c->context[0 ], &c->sid[0 ]);if (rc) goto out; } - sbsec->sid = c->sid[0 ]; + *sid = c->sid[0 ]; }else { rc =__security_genfs_sid (ns, fstype,"/" ,SECCLASS_DIR , - &sbsec->sid); + sid);if (rc) { - sbsec->behavior =SECURITY_FS_USE_NONE ; + *behavior =SECURITY_FS_USE_NONE ; rc =0 ; }else { - sbsec->behavior =SECURITY_FS_USE_GENFS ; + *behavior =SECURITY_FS_USE_GENFS ; } }
Signature · 1 line
2.9.5
RFC 9/10 selinux: add a selinuxfs interface to unshare selinux namespace
In reply to Stephen SmalleyProvide a userspace API to unshare the selinux namespace. Currently implemented via a selinuxfs node. This could be coupled with unsharing of other namespaces (e.g. mount namespace, network namespace) that will always be needed or left independent. Don't get hung up on the interface itself, it is just to allow experimentation and testing.
Sample usage:
echo 1 > /sys/fs/selinux/unshare
unshare -m -n
umount /sys/fs/selinux
mount -t selinuxfs none /sys/fs/selinuxload_policy
getenforce
id
echo $$
The above will show that the process now views itself as running in the kernel domain in permissive mode, as would be the case at boot.
Quoted 1 line
From a different shell on the host system, running ps -eZ or
cat /proc/<pid>/attr/current will show that the process that unshared its selinux namespace is still running in its original context in the initial namespace, and getenforce will show the the initial namespace remains enforcing. Enforcing mode or policy changes in the child will not affect the parent.
This is not yet safe; do not use on production systems. Known issues include at least the following items:
* The policy loading code has not been thoroughly audited and hardened for use by unprivileged code, both with respect to ensuring that the policy is internally consistent and restricting the range of values used from the policy as loop bounds and memory allocation sizes to sane limits.
* The SELinux hook functions have not been modified to be namespace-aware, so the hooks only perform checking against the current namespace. Thus, unsharing allows the process to escape confinement by the parent. Fixing this requires updating each hook to perform its processing on the current namespace and all of its ancestors up to the init namespace.
* Some of the hook functions can be called outside of process context (e.g. task_kill, send_sigiotask, network input/forward) and should not use the current task's selinux namespace. These hooks need to be updated to obtain the proper selinux namespace to use instead from the caller or cached in a suitable data structure (e.g. the file or sock security structures).
* There are number of issues with the inode and superblock security blob handling for multiple namespaces, see those commits for more details.
* Only a subset of object security blobs have been updated to be namespace-aware and support multiple namespaces. The ones that have not yet been updated could end up performing permission checks or other operations on SIDs created in a different selinux namespace.
* The network SID caches (netif, netnode, netport) have not yet been instantiated per selinux namespace, unlike the AVC and SS.
* There is no way currently to restrict or bound nesting of namespaces; if you allow it to a domain in the init namespace, then that domain can in turn unshare to arbitrary depths and can grant the same to any domain in its own policy. Related to this is the fact that there is no way to control resource usage due to selinux namespaces and they can be substantial (per-namespace policydb, sidtab, AVC, etc).
* SIDs may be cached by audit and networking code and in external kernel data structures and used later, potentially in a different selinux namespace than the one in which the SID was originally created.
* No doubt other things I'm forgetting or haven't thought of. Use at your own risk.
--- security/selinux/include/classmap.h | 3 +- security/selinux/selinuxfs.c | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-)
security/selinux/include/classmap.h +2 -1
@@ -39,7 +39,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" ,"unshare" , + NULL } }, { "process", { "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",
security/selinux/selinuxfs.c +66 -0
84 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
Quoted 68 lines · Stephen Smalley
Provide a userspace API to unshare the selinux namespace. Currently implemented via a selinuxfs node. This could be coupled with unsharing of other namespaces (e.g. mount namespace, network namespace) that will always be needed or left independent. Don't get hung up on the interface itself, it is just to allow experimentation and testing.
Sample usage:
echo 1 >/sys/fs/selinux/unshare
unshare -m -n
umount/sys/fs/selinux
mount -t selinuxfs none/sys/fs/selinuxload_policy
getenforce
id
echo $$The above will show that the process now views itself as running in the kernel domain in permissive mode, as would be the case at boot.
Quoted 1 line
From a different shell on the host system, running ps -eZ or
cat /proc/<pid>/attr/current will show that the process that unshared its selinux namespace is still running in its original context in the initial namespace, and getenforce will show the the initial namespace remains enforcing. Enforcing mode or policy changes in the child will not affect the parent.
This is not yet safe; do not use on production systems. Known issues include at least the following items:
* The policy loading code has not been thoroughly audited and hardened for use by unprivileged code, both with respect to ensuring that the policy is internally consistent and restricting the range of values used from the policy as loop bounds and memory allocation sizes to sane limits.
* The SELinux hook functions have not been modified to be namespace-aware, so the hooks only perform checking against the current namespace. Thus, unsharing allows the process to escape confinement by the parent. Fixing this requires updating each hook to perform its processing on the current namespace and all of its ancestors up to the init namespace.
* Some of the hook functions can be called outside of process context (e.g.
task_kill,send_sigiotask, network input/forward) and should not use the current task's selinux namespace. These hooks need to be updated to obtain the proper selinux namespace to use instead from the caller or cached in a suitable data structure (e.g. the file or sock security structures).* There are number of issues with the inode and superblock security blob handling for multiple namespaces, see those commits for more details.
* Only a subset of object security blobs have been updated to be namespace-aware and support multiple namespaces. The ones that have not yet been updated could end up performing permission checks or other operations on SIDs created in a different selinux namespace.
* The network SID caches (netif, netnode, netport) have not yet been instantiated per selinux namespace, unlike the AVC and SS.
* There is no way currently to restrict or bound nesting of namespaces; if you allow it to a domain in the init namespace, then that domain can in turn unshare to arbitrary depths and can grant the same to any domain in its own policy. Related to this is the fact that there is no way to control resource usage due to selinux namespaces and they can be substantial (per-namespace policydb, sidtab, AVC, etc).
Quoted 3 lines
* SIDs may be cached by audit and networking code and in external kernel data structures and used later, potentially in a different selinux namespace than the one in which the SID was originally created.
Is there a good reason that SIDs (and security contexts) need to be maintained separately in the namespaces? Using the same secid to map to a different context depending on the namespace seems like you're asking for trouble you don't need. A namespace that hasn't policy for a context/SID won't use it if it is defined for a different namespace, and should detect the fact if it somehow gets referenced, because it isn't in the policy.
Do the context/SID mapping globally. Or, if you must duplicate contexts, allocate the SIDs from a single source so that they aren't ambiguous.
Quoted 108 lines
* No doubt other things I'm forgetting or haven't thought of. Use at your own risk.
Not-signed-off-by Stephen Smalley <[email protected]>--- security/selinux/include/classmap.h | 3 +- security/selinux/selinuxfs.c | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-)security/selinux/include/classmap.h +2
-1@@ -39,7 +39,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" ,"unshare" , + NULL } }, { "process", { "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",security/selinux/selinuxfs.c +65
-0@@ -63,6 +63,7 @@ enum sel_inos {SEL_STATUS ,/* export current status using mmap() */ SEL_POLICY ,/* allow userspace to read the in kernel policy */ SEL_VALIDATE_TRANS ,/* compute validatetrans decision */ +SEL_UNSHARE ,/* unshare selinux namespace */ SEL_INO_NEXT, /* The next inode number to use */ };@@ -321,6 +322,70 @@ static const struct file_operations sel_disable_ops = { .llseek = generic_file_llseek, }; +static ssize_t sel_write_unshare (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) + +{ +struct selinux_fs_info *fsi =file_inode (file)->i_sb->s_fs_info; +struct selinux_ns *ns = fsi->ns; +char *page; +ssize_t length; +bool set; +int rc; + +if (count >=PAGE_SIZE ) +return -ENOMEM ; + +/* No partial writes. */ +if (*ppos !=0 ) +return -EINVAL ; + + rc =avc_has_perm (current_selinux_ns,current_sid (), +SECINITSID_SECURITY ,SECCLASS_SECURITY , +SECURITY__UNSHARE ,NULL ); +if (rc) +return rc; + + page =memdup_user_nul (buf, count); +if (IS_ERR (page)) +return PTR_ERR (page); + + length = -EINVAL ; +if (kstrtobool (page, &set)) + goto out; + +if (set) { +struct cred *cred =prepare_creds (); +struct task_security_struct *tsec; + +if (!cred) { + length = -ENOMEM ; + goto out; + } + tsec = cred->security; +if (selinux_ns_create (ns, &tsec->ns)) { +abort_creds (cred); + length = -ENOMEM ; + goto out; + } + tsec->osid = tsec->sid =SECINITSID_KERNEL ; + tsec->exec_sid = tsec->create_sid = tsec->keycreate_sid = + tsec->sockcreate_sid =SECSID_NULL ; + tsec->parent_cred =get_current_cred (); +commit_creds (cred); + } + + length = count; +out: +kfree (page); +return length; +} + +static const struct file_operations sel_unshare_ops = { + .write = sel_write_unshare, + .llseek = generic_file_llseek, +}; +static ssize_t sel_read_policyvers (struct file *filp,char __user *buf,size_t count,loff_t *ppos) {
security/selinux/selinuxfs.c static int sel_fill_super(struct super_block *sb, void *data, int silent) · line 1994
Quoted 7 lines
security/selinux/selinuxfs.c +1
-0@@ -1923,6 +1988,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, [SEL_VALIDATE_TRANS ] = {"validatetrans" , &sel_transition_ops, S_IWUGO}, + [SEL_UNSHARE] = {"unshare", &sel_unshare_ops, 0222}, /* last one */ {""} };
.
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
Quoted 93 lines · Casey Schaufler
Quoted 79 lines · Stephen Smalley
Provide a userspace API to unshare the selinux namespace. Currently implemented via a selinuxfs node. This could be coupled with unsharing of other namespaces (e.g. mount namespace, network namespace) that will always be needed or left independent. Don't get hung up on the interface itself, it is just to allow experimentation and testing.
Sample usage:
echo 1 >/sys/fs/selinux/unshare
unshare -m -n
umount/sys/fs/selinux
mount -t selinuxfs none/sys/fs/selinuxload_policy
getenforce
id
echo $$The above will show that the process now views itself as running in the
kernel domain in permissive mode, as would be the case at boot.Quoted 1 line
From a different shell on the host system, running ps -eZ or
cat /proc/<pid>/attr/current will show that the process that unshared its selinux namespace is still running in its original context in the initial namespace, and getenforce will show the the initial namespace remains enforcing. Enforcing mode or policy changes in the child will not affect the parent.
This is not yet safe; do not use on production systems. Known issues include at least the following items:
* The policy loading code has not been thoroughly audited and hardened for use by unprivileged code, both with respect to ensuring that the policy is internally consistent and restricting the range of values used from the policy as loop bounds and memory allocation sizes to sane limits.
* The SELinux hook functions have not been modified to be namespace-aware, so the hooks only perform checking against the current namespace. Thus, unsharing allows the process to escape confinement by the parent. Fixing this requires updating each hook to
perform its processing on the current namespace and all of its ancestors
up to the init namespace.* Some of the hook functions can be called outside of process context
(e.g.task_kill,send_sigiotask, network input/forward) and should not use
the current task's selinux namespace. These hooks need to be updated to
obtain the proper selinux namespace to use instead from the caller or
cached in a suitable data structure (e.g. the file or sock security structures).* There are number of issues with the inode and superblock security blob
handling for multiple namespaces, see those commits for more details.* Only a subset of object security blobs have been updated to be namespace-aware and support multiple namespaces. The ones that have not yet been updated could end up performing permission checks or
other operations on SIDs created in a different selinux namespace.* The network SID caches (netif, netnode, netport) have not yet been instantiated per selinux namespace, unlike the AVC and SS.
* There is no way currently to restrict or bound nesting of namespaces; if you allow it to a domain in the init namespace, then that domain can in turn unshare to arbitrary depths and can grant the same to any domain in its own policy. Related to this is the fact that there is no way to control resource usage due to selinux namespaces and they can be substantial (per-namespace policydb, sidtab, AVC, etc).
Quoted 4 lines
* SIDs may be cached by audit and networking code and in external kernel data structures and used later, potentially in a different selinux namespace than the one in which the SID was originally created.
Is there a good reason that SIDs (and security contexts) need to be maintained separately in the namespaces? Using the same secid to map to a different context depending on the namespace seems like you're asking for trouble you don't need. A namespace that hasn't policy for a context/SID won't use it if it is defined for a different namespace, and should detect the fact if it somehow gets referenced, because it isn't in the policy.
Quoted 3 lines
Do the context/SID mapping globally. Or, if you must duplicate contexts,
allocate the SIDs from a single source so that they aren't ambiguous.
Yes, that may be the right answer, but it requires introducing a new SID/context layer above the current security server layer (or changing the latter), because the security server SID/context mappings are from SIDs to internal struct representations of the context, where the struct representation is policy-specific. Also, certain SIDs (e.g. the kernel SID, the unlabeled SID, etc) are predefined for system initialization before policy load and to support usage from policy- independent code, but may be mapped to different context values by different policies. So even the SID->string mappings may differ for different policies, and hence for different namespaces.
Quoted 137 lines
Quoted 130 lines
* No doubt other things I'm forgetting or haven't thought of. Use at your own risk.
Not-signed-off-by Stephen Smalley <[email protected]>---
security/selinux/include/classmap.h | 3 +-
security/selinux/selinuxfs.c | 66
+++++++++++++++++++++++++++++++++++++2 files changed, 68 insertions(+), 1 deletion(-)security/selinux/include/classmap.h +2
-1@@ -39,7 +39,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","unshare" , + NULL } },{ "process",
{ "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",security/selinux/selinuxfs.c +0
-0@@ -63,6 +63,7 @@ enum sel_inos { SEL_STATUS, /* export current status using mmap()*/
SEL_POLICY, /* allow userspace to read the in
kernel policy */
SEL_VALIDATE_TRANS, /* compute validatetrans decision */security/selinux/selinuxfs.c +1
-0+ SEL_UNSHARE, /* unshare selinux namespace */
SEL_INO_NEXT, /* The next inode number to use */
};security/selinux/selinuxfs.c +0
-0@@ -321,6 +322,70 @@ static const struct file_operations sel_disable_ops = { .llseek = generic_file_llseek, };
+staticssize_tsel_write_unshare(struct file *file, const char__user*buf,security/selinux/selinuxfs.c +4
-0+ size_t count, loff_t *ppos) + +{ + struct selinux_fs_info *fsi = file_inode(file)->i_sb-Quoted 1 line
s_fs_info;security/selinux/selinuxfs.c +43
-0+struct selinux_ns *ns = fsi->ns; +char *page; +ssize_t length; +bool set; +int rc; + +if (count >=PAGE_SIZE ) +return -ENOMEM ; + +/* No partial writes. */ +if (*ppos !=0 ) +return -EINVAL ; + + rc =avc_has_perm (current_selinux_ns,current_sid (), + SECINITSID_SECURITY, SECCLASS_SECURITY, + SECURITY__UNSHARE, NULL); +if (rc) +return rc; + + page =memdup_user_nul (buf, count); +if (IS_ERR (page)) +return PTR_ERR (page); + + length = -EINVAL ; +if (kstrtobool (page, &set)) + goto out; + +if (set) { +struct cred *cred =prepare_creds (); +struct task_security_struct *tsec; + +if (!cred) { + length = -ENOMEM ; + goto out; + } + tsec = cred->security; +if (selinux_ns_create (ns, &tsec->ns)) { +abort_creds (cred); + length = -ENOMEM ; + goto out; + } + tsec->osid = tsec->sid =SECINITSID_KERNEL ; + tsec->exec_sid = tsec->create_sid = tsec-Quoted 1 line
keycreate_sid=security/selinux/selinuxfs.c +16
-0+ tsec->sockcreate_sid =SECSID_NULL ; + tsec->parent_cred =get_current_cred (); +commit_creds (cred); + } + + length = count; +out: +kfree (page); +return length; +} + +static const struct file_operations sel_unshare_ops = { + .write = sel_write_unshare, + .llseek = generic_file_llseek, +}; +static
ssize_tsel_read_policyvers(struct file *filp, char__user*buf,
size_t count,loff_t*ppos) {security/selinux/selinuxfs.c +1
-0@@ -1923,6 +1988,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) [SEL_POLICY] = {"policy", &sel_policy_ops,S_IRUGO }, [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, S_IWUGO}, + [SEL_UNSHARE ] = {"unshare" , &sel_unshare_ops,Quoted 4 lines
0222},
/* last one */ {""}
};.
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
Quoted 92 lines · Stephen Smalley
Quoted 91 lines · Casey Schaufler
Quoted 78 lines · Stephen Smalley
Provide a userspace API to unshare the selinux namespace. Currently implemented via a selinuxfs node. This could be coupled with unsharing of other namespaces (e.g. mount namespace, network namespace) that will always be needed or left independent. Don't get hung up on the interface itself, it is just to allow experimentation and testing.
Sample usage:
echo 1 >/sys/fs/selinux/unshare
unshare -m -n
umount/sys/fs/selinux
mount -t selinuxfs none/sys/fs/selinuxload_policy
getenforce
id
echo $$The above will show that the process now views itself as running in the
kernel domain in permissive mode, as would be the case at boot.Quoted 1 line
From a different shell on the host system, running ps -eZ or
cat /proc/<pid>/attr/current will show that the process that unshared its selinux namespace is still running in its original context in the initial namespace, and getenforce will show the the initial namespace remains enforcing. Enforcing mode or policy changes in the child will not affect the parent.
This is not yet safe; do not use on production systems. Known issues include at least the following items:
* The policy loading code has not been thoroughly audited and hardened for use by unprivileged code, both with respect to ensuring that the policy is internally consistent and restricting the range of values used from the policy as loop bounds and memory allocation sizes to sane limits.
* The SELinux hook functions have not been modified to be namespace-aware, so the hooks only perform checking against the current namespace. Thus, unsharing allows the process to escape confinement by the parent. Fixing this requires updating each hook to
perform its processing on the current namespace and all of its ancestors
up to the init namespace.* Some of the hook functions can be called outside of process context
(e.g.task_kill,send_sigiotask, network input/forward) and should not use
the current task's selinux namespace. These hooks need to be updated to
obtain the proper selinux namespace to use instead from the caller or
cached in a suitable data structure (e.g. the file or sock security structures).* There are number of issues with the inode and superblock security blob
handling for multiple namespaces, see those commits for more details.* Only a subset of object security blobs have been updated to be namespace-aware and support multiple namespaces. The ones that have not yet been updated could end up performing permission checks or
other operations on SIDs created in a different selinux namespace.* The network SID caches (netif, netnode, netport) have not yet been instantiated per selinux namespace, unlike the AVC and SS.
* There is no way currently to restrict or bound nesting of namespaces; if you allow it to a domain in the init namespace, then that domain can in turn unshare to arbitrary depths and can grant the same to any domain in its own policy. Related to this is the fact that there is no way to control resource usage due to selinux namespaces and they can be substantial (per-namespace policydb, sidtab, AVC, etc).
Quoted 4 lines
* SIDs may be cached by audit and networking code and in external kernel data structures and used later, potentially in a different selinux namespace than the one in which the SID was originally created.
Is there a good reason that SIDs (and security contexts) need to be maintained separately in the namespaces? Using the same secid to map to a different context depending on the namespace seems like you're asking for trouble you don't need. A namespace that hasn't policy for a context/SID won't use it if it is defined for a different namespace, and should detect the fact if it somehow gets referenced, because it isn't in the policy.
Quoted 6 lines
Quoted 3 lines
Do the context/SID mapping globally. Or, if you must duplicate contexts,
allocate the SIDs from a single source so that they aren't ambiguous.Yes, that may be the right answer, but it requires introducing a new SID/context layer above the current security server layer (or changing the latter),
It could be as simple as using a global next_sid instead of attaching that to the sidtab (s->next_sid).
What I'm really afraid of is an ambiguous secid being set in a secmark. In the stacking case it's going to be bad enough mapping a set of secids into a "token" without having to deal with the possibility that SELinux could give you a secid that has multiple possible mappings depending on which namespace it came from and/or which it is going into.
Quoted 143 lines
because the security server SID/context mappings are from
SIDs to internal struct representations of the context, where the struct representation is policy-specific. Also, certain SIDs (e.g. the kernel SID, the unlabeled SID, etc) are predefined for system initialization before policy load and to support usage from policy- independent code, but may be mapped to different context values by different policies. So even theSID->stringmappings may differ for different policies, and hence for different namespaces.Quoted 134 lines
Quoted 128 lines
* No doubt other things I'm forgetting or haven't thought of. Use at your own risk.
Not-signed-off-by Stephen Smalley <[email protected]>---
security/selinux/include/classmap.h | 3 +-
security/selinux/selinuxfs.c | 66
+++++++++++++++++++++++++++++++++++++2 files changed, 68 insertions(+), 1 deletion(-)security/selinux/include/classmap.h +2
-1@@ -39,7 +39,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","unshare" , + NULL } },{ "process",
{ "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",security/selinux/selinuxfs.c +0
-0@@ -63,6 +63,7 @@ enum sel_inos { SEL_STATUS, /* export current status using mmap()*/
SEL_POLICY, /* allow userspace to read the in
kernel policy */
SEL_VALIDATE_TRANS, /* compute validatetrans decision */security/selinux/selinuxfs.c +1
-0+ SEL_UNSHARE, /* unshare selinux namespace */
SEL_INO_NEXT, /* The next inode number to use */
};security/selinux/selinuxfs.c +0
-0@@ -321,6 +322,70 @@ static const struct file_operations sel_disable_ops = { .llseek = generic_file_llseek, };
+staticssize_tsel_write_unshare(struct file *file, const char__user*buf,security/selinux/selinuxfs.c +4
-0+ size_t count, loff_t *ppos) + +{ + struct selinux_fs_info *fsi = file_inode(file)->i_sb-Quoted 1 line
s_fs_info;security/selinux/selinuxfs.c +43
-0+struct selinux_ns *ns = fsi->ns; +char *page; +ssize_t length; +bool set; +int rc; + +if (count >=PAGE_SIZE ) +return -ENOMEM ; + +/* No partial writes. */ +if (*ppos !=0 ) +return -EINVAL ; + + rc =avc_has_perm (current_selinux_ns,current_sid (), + SECINITSID_SECURITY, SECCLASS_SECURITY, + SECURITY__UNSHARE, NULL); +if (rc) +return rc; + + page =memdup_user_nul (buf, count); +if (IS_ERR (page)) +return PTR_ERR (page); + + length = -EINVAL ; +if (kstrtobool (page, &set)) + goto out; + +if (set) { +struct cred *cred =prepare_creds (); +struct task_security_struct *tsec; + +if (!cred) { + length = -ENOMEM ; + goto out; + } + tsec = cred->security; +if (selinux_ns_create (ns, &tsec->ns)) { +abort_creds (cred); + length = -ENOMEM ; + goto out; + } + tsec->osid = tsec->sid =SECINITSID_KERNEL ; + tsec->exec_sid = tsec->create_sid = tsec-Quoted 1 line
keycreate_sid=security/selinux/selinuxfs.c +16
-0+ tsec->sockcreate_sid =SECSID_NULL ; + tsec->parent_cred =get_current_cred (); +commit_creds (cred); + } + + length = count; +out: +kfree (page); +return length; +} + +static const struct file_operations sel_unshare_ops = { + .write = sel_write_unshare, + .llseek = generic_file_llseek, +}; +static
ssize_tsel_read_policyvers(struct file *filp, char__user*buf,
size_t count,loff_t*ppos) {security/selinux/selinuxfs.c +1
-0@@ -1923,6 +1988,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) [SEL_POLICY] = {"policy", &sel_policy_ops,S_IRUGO }, [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, S_IWUGO}, + [SEL_UNSHARE ] = {"unshare" , &sel_unshare_ops,Quoted 4 lines
0222},
/* last one */ {""}
};.
.
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
In reply to Stephen SmalleyQuoted 7 lines · Stephen Smalley
Provide a userspace API to unshare the selinux namespace. Currently implemented via a selinuxfs node. This could be coupled with unsharing of other namespaces (e.g. mount namespace, network namespace) that will always be needed or left independent. Don't get hung up on the interface itself, it is just to allow experimentation and testing.
Quoted 9 lines
Sample usage:
echo 1 >/sys/fs/selinux/unshare
unshare -m -n
umount/sys/fs/selinux
mount -t selinuxfs none/sys/fs/selinuxload_policy
getenforce
id
echo $$
For added fun, you can do the following after unsharing and loading a policy into your namespace above:
# Transition from kernel context to an unconfined context.
runcon unconfined_u:unconfined_u:unconfined_t:s0:c0.c1023 /bin/bash # Allow use of file descriptors inherited from the parent namespace, e.g the pty. cat <<EOF > allowunlabeledfd.cil
(allow domain unlabeled_t (fd (use)))
EOF
semodule -i allowunlabeledfd.cil
# Switch namespace to enforcing mode
setenforce 1
# Run the selinux testsuite
cd /path/to/selinux-testsuite
make test
inet_socket test failures are expected due to running in a non-init network namespace; they don't work even without unsharing the selinux namespace.
Quoted 193 lines
The above will show that the process now views itself as running in the
kernel domain in permissive mode, as would be the case at boot.Quoted 1 line
From a different shell on the host system, running ps -eZ or
cat /proc/<pid>/attr/current will show that the process that unshared its selinux namespace is still running in its original context in the initial namespace, and getenforce will show the the initial namespace remains enforcing. Enforcing mode or policy changes in the child will not affect the parent.
This is not yet safe; do not use on production systems. Known issues include at least the following items:
* The policy loading code has not been thoroughly audited and hardened for use by unprivileged code, both with respect to ensuring that the policy is internally consistent and restricting the range of values used from the policy as loop bounds and memory allocation sizes to sane limits.
* The SELinux hook functions have not been modified to be namespace-aware, so the hooks only perform checking against the current namespace. Thus, unsharing allows the process to escape confinement by the parent. Fixing this requires updating each hook to
perform its processing on the current namespace and all of its ancestors
up to the init namespace.* Some of the hook functions can be called outside of process context (e.g.
task_kill,send_sigiotask, network input/forward) and should not use
the current task's selinux namespace. These hooks need to be updated to
obtain the proper selinux namespace to use instead from the caller or cached in a suitable data structure (e.g. the file or sock security structures).* There are number of issues with the inode and superblock security blob
handling for multiple namespaces, see those commits for more details.* Only a subset of object security blobs have been updated to be namespace-aware and support multiple namespaces. The ones that have not yet been updated could end up performing permission checks or
other operations on SIDs created in a different selinux namespace.* The network SID caches (netif, netnode, netport) have not yet been instantiated per selinux namespace, unlike the AVC and SS.
* There is no way currently to restrict or bound nesting of namespaces; if you allow it to a domain in the init namespace, then that domain can in turn unshare to arbitrary depths and can grant the same to any domain in its own policy. Related to this is the fact that there is no way to control resource usage due to selinux namespaces and they can be substantial (per-namespace policydb, sidtab, AVC, etc).
* SIDs may be cached by audit and networking code and in external kernel data structures and used later, potentially in a different selinux namespace than the one in which the SID was originally created.
* No doubt other things I'm forgetting or haven't thought of. Use at your own risk.
Not-signed-off-by Stephen Smalley <[email protected]>---
security/selinux/include/classmap.h | 3 +-
security/selinux/selinuxfs.c | 66
+++++++++++++++++++++++++++++++++++++2 files changed, 68 insertions(+), 1 deletion(-)security/selinux/include/classmap.h +2
-1@@ -39,7 +39,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","unshare" , + NULL } }, { "process",{ "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",
security/selinux/selinuxfs.c +0
-0@@ -63,6 +63,7 @@ enum sel_inos {
SEL_STATUS, /* export current status usingmmap()*/
SEL_POLICY, /* allow userspace to read the in kernel
policy */
SEL_VALIDATE_TRANS, /* compute validatetrans decision */security/selinux/selinuxfs.c +1
-0+ SEL_UNSHARE, /* unshare selinux namespace */
SEL_INO_NEXT, /* The next inode number to use */
};security/selinux/selinuxfs.c +0
-0@@ -321,6 +322,70 @@ static const struct file_operations sel_disable_ops = { .llseek = generic_file_llseek, };
+staticssize_tsel_write_unshare(struct file *file, const char__user*buf,security/selinux/selinuxfs.c +4
-0+ size_t count, loff_t *ppos) + +{ + struct selinux_fs_info *fsi = file_inode(file)->i_sb-Quoted 1 line
s_fs_info;security/selinux/selinuxfs.c +43
-0+struct selinux_ns *ns = fsi->ns; +char *page; +ssize_t length; +bool set; +int rc; + +if (count >=PAGE_SIZE ) +return -ENOMEM ; + +/* No partial writes. */ +if (*ppos !=0 ) +return -EINVAL ; + + rc =avc_has_perm (current_selinux_ns,current_sid (), + SECINITSID_SECURITY, SECCLASS_SECURITY, + SECURITY__UNSHARE, NULL); +if (rc) +return rc; + + page =memdup_user_nul (buf, count); +if (IS_ERR (page)) +return PTR_ERR (page); + + length = -EINVAL ; +if (kstrtobool (page, &set)) + goto out; + +if (set) { +struct cred *cred =prepare_creds (); +struct task_security_struct *tsec; + +if (!cred) { + length = -ENOMEM ; + goto out; + } + tsec = cred->security; +if (selinux_ns_create (ns, &tsec->ns)) { +abort_creds (cred); + length = -ENOMEM ; + goto out; + } + tsec->osid = tsec->sid =SECINITSID_KERNEL ; + tsec->exec_sid = tsec->create_sid = tsec-Quoted 1 line
keycreate_sid=security/selinux/selinuxfs.c +16
-0+ tsec->sockcreate_sid =SECSID_NULL ; + tsec->parent_cred =get_current_cred (); +commit_creds (cred); + } + + length = count; +out: +kfree (page); +return length; +} + +static const struct file_operations sel_unshare_ops = { + .write = sel_write_unshare, + .llseek = generic_file_llseek, +}; +static
ssize_tsel_read_policyvers(struct file *filp, char__user*buf,
size_t count,loff_t*ppos) {security/selinux/selinuxfs.c +1
-0@@ -1923,6 +1988,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, S_IWUGO}, + [SEL_UNSHARE] = {"unshare", &sel_unshare_ops, 0222}, /* last one */ {""}};
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
Quoted 18 lines · Stephen Smalley
Quoted 7 lines · Stephen Smalley
Provide a userspace API to unshare the selinux namespace. Currently implemented via a selinuxfs node. This could be coupled with unsharing of other namespaces (e.g. mount namespace, network namespace) that will always be needed or left independent. Don't get hung up on the interface itself, it is just to allow experimentation and testing.
Quoted 9 lines
Sample usage:
echo 1 >/sys/fs/selinux/unshare
unshare -m -n
umount/sys/fs/selinux
mount -t selinuxfs none/sys/fs/selinuxload_policy
getenforce
id
echo $$
Quoted 10 lines
For added fun, you can do the following after unsharing and loading a policy into your namespace above:
# Transition from kernel context to an unconfined context. runcon unconfined_u:unconfined_u:unconfined_t:s0:c0.c1023/bin/bash# Allow use of file descriptors inherited from the parent namespace, e.g the pty.
cat <<EOF > allowunlabeledfd.cil
(allow domainunlabeled_t(fd (use)))
EOF
semodule -i allowunlabeledfd.cil
Also:
restorecon -R /dev
to fix up the /dev node contexts in your namespace.
Quoted 215 lines
# Switch namespace to enforcing mode
setenforce 1
# Run the selinux testsuite
cd /path/to/selinux-testsuite
make test
inet_sockettest failures are expected due to running in a non-init network namespace; they don't work even without unsharing the selinux namespace.Quoted 205 lines
The above will show that the process now views itself as running in the
kernel domain in permissive mode, as would be the case at boot.Quoted 1 line
From a different shell on the host system, running ps -eZ or
cat /proc/<pid>/attr/current will show that the process that unshared its selinux namespace is still running in its original context in the initial namespace, and getenforce will show the the initial namespace remains enforcing. Enforcing mode or policy changes in the child will not affect the parent.
This is not yet safe; do not use on production systems. Known issues include at least the following items:
* The policy loading code has not been thoroughly audited and hardened for use by unprivileged code, both with respect to ensuring that the policy is internally consistent and restricting the range of values used from the policy as loop bounds and memory allocation sizes to sane limits.
* The SELinux hook functions have not been modified to be namespace-aware, so the hooks only perform checking against the current namespace. Thus, unsharing allows the process to escape confinement by the parent. Fixing this requires updating each hook to
perform its processing on the current namespace and all of its ancestors
up to the init namespace.* Some of the hook functions can be called outside of process context
(e.g.task_kill,send_sigiotask, network input/forward) and should not use
the current task's selinux namespace. These hooks need to be updated
to
obtain the proper selinux namespace to use instead from the caller or
cached in a suitable data structure (e.g. the file or sock security structures).* There are number of issues with the inode and superblock security blob
handling for multiple namespaces, see those commits for more details.* Only a subset of object security blobs have been updated to be namespace-aware and support multiple namespaces. The ones that have not yet been updated could end up performing permission checks or
other operations on SIDs created in a different selinux namespace.* The network SID caches (netif, netnode, netport) have not yet been instantiated per selinux namespace, unlike the AVC and SS.
* There is no way currently to restrict or bound nesting of namespaces; if you allow it to a domain in the init namespace, then that domain can in turn unshare to arbitrary depths and can grant the same to any domain in its own policy. Related to this is the fact that there is no way to control resource usage due to selinux namespaces and they can be substantial (per-namespace policydb, sidtab, AVC, etc).
* SIDs may be cached by audit and networking code and in external kernel data structures and used later, potentially in a different selinux namespace than the one in which the SID was originally created.
* No doubt other things I'm forgetting or haven't thought of. Use at your own risk.
Not-signed-off-by Stephen Smalley <[email protected]>---
security/selinux/include/classmap.h | 3 +-
security/selinux/selinuxfs.c | 66
+++++++++++++++++++++++++++++++++++++2 files changed, 68 insertions(+), 1 deletion(-)security/selinux/include/classmap.h +0
-1@@ -39,7 +39,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
} },security/selinux/include/classmap.h +1
-0+ "setcheckreqprot", "read_policy", "validate_trans","unshare",
security/selinux/include/classmap.h +1
-0+ NULL } },{ "process",
{ "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",security/selinux/selinuxfs.c +0
-0@@ -63,6 +63,7 @@ enum sel_inos { SEL_STATUS, /* export current status using mmap()*/
SEL_POLICY, /* allow userspace to read the in
kernel
policy */
SEL_VALIDATE_TRANS, /* compute validatetrans decision */security/selinux/selinuxfs.c +1
-0+ SEL_UNSHARE, /* unshare selinux namespace */
SEL_INO_NEXT, /* The next inode number to use */
};security/selinux/selinuxfs.c +0
-0@@ -321,6 +322,70 @@ static const struct file_operations sel_disable_ops = { .llseek = generic_file_llseek, };
+staticssize_tsel_write_unshare(struct file *file, const char__user*buf,security/selinux/selinuxfs.c +4
-0+ size_t count, loff_t *ppos) + +{ + struct selinux_fs_info *fsi = file_inode(file)->i_sb-Quoted 1 line
s_fs_info;security/selinux/selinuxfs.c +43
-0+struct selinux_ns *ns = fsi->ns; +char *page; +ssize_t length; +bool set; +int rc; + +if (count >=PAGE_SIZE ) +return -ENOMEM ; + +/* No partial writes. */ +if (*ppos !=0 ) +return -EINVAL ; + + rc =avc_has_perm (current_selinux_ns,current_sid (), + SECINITSID_SECURITY, SECCLASS_SECURITY, + SECURITY__UNSHARE, NULL); +if (rc) +return rc; + + page =memdup_user_nul (buf, count); +if (IS_ERR (page)) +return PTR_ERR (page); + + length = -EINVAL ; +if (kstrtobool (page, &set)) + goto out; + +if (set) { +struct cred *cred =prepare_creds (); +struct task_security_struct *tsec; + +if (!cred) { + length = -ENOMEM ; + goto out; + } + tsec = cred->security; +if (selinux_ns_create (ns, &tsec->ns)) { +abort_creds (cred); + length = -ENOMEM ; + goto out; + } + tsec->osid = tsec->sid =SECINITSID_KERNEL ; + tsec->exec_sid = tsec->create_sid = tsec-Quoted 1 line
keycreate_sid=security/selinux/selinuxfs.c +16
-0+ tsec->sockcreate_sid =SECSID_NULL ; + tsec->parent_cred =get_current_cred (); +commit_creds (cred); + } + + length = count; +out: +kfree (page); +return length; +} + +static const struct file_operations sel_unshare_ops = { + .write = sel_write_unshare, + .llseek = generic_file_llseek, +}; +static
ssize_tsel_read_policyvers(struct file *filp, char__user*buf,
size_t count,loff_t*ppos) {security/selinux/selinuxfs.c +1
-0@@ -1923,6 +1988,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) [SEL_POLICY] = {"policy", &sel_policy_ops,S_IRUGO }, [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, S_IWUGO}, + [SEL_UNSHARE ] = {"unshare" , &sel_unshare_ops,0222},
/* last one */ {""}
};
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
Quoted 21 lines · Stephen Smalley
Quoted 20 lines · Stephen Smalley
Quoted 18 lines · Stephen Smalley
Provide a userspace API to unshare the selinux namespace. Currently implemented via a selinuxfs node. This could be coupled with unsharing of other namespaces (e.g. mount namespace,
network namespace) that will always be needed or left independent.
Don't get hung up on the interface itself, it is just to allow experimentation and testing.Sample usage:
echo 1 >/sys/fs/selinux/unshare
unshare -m -n
umount/sys/fs/selinux
mount -t selinuxfs none/sys/fs/selinuxload_policy
getenforce
id
echo $$
Quoted 5 lines
Quoted 5 lines
For added fun, you can do the following after unsharing and loading a
policy into your namespace above:
# Transition from kernel context to an unconfined context. runcon unconfined_u:unconfined_u:unconfined_t:s0:c0.c1023/bin/bash
That should be:
runcon unconfined_u:unconfined_r:unconfined_t:s0:c0.c1023 /bin/bash
Quoted 244 lines
Quoted 7 lines
# Allow use of file descriptors inherited from the parent namespace,
e.g the pty.
cat <<EOF > allowunlabeledfd.cil
(allow domainunlabeled_t(fd (use)))
EOF
semodule -i allowunlabeledfd.cilAlso:
restorecon -R /dev
to fix up the /dev node contexts in your namespace.Quoted 232 lines
# Switch namespace to enforcing mode
setenforce 1
# Run the selinux testsuite
cd /path/to/selinux-testsuite
make test
inet_sockettest failures are expected due to running in a non-init network namespace; they don't work even without unsharing the selinux
namespace.Quoted 221 lines
The above will show that the process now views itself as running in
the
kernel domain in permissive mode, as would be the case at boot.Quoted 1 line
From a different shell on the host system, running ps -eZ or
cat /proc/<pid>/attr/current will show that the process that unshared its selinux namespace is still running in its original context in the initial namespace, and getenforce will show the the initial namespace remains enforcing. Enforcing mode or policy
changes in the child will not affect the parent.This is not yet safe; do not use on production systems. Known issues include at least the following items:
* The policy loading code has not been thoroughly audited and hardened for use by unprivileged code, both with respect to ensuring that the policy is internally consistent and restricting the range of values used from the policy as loop bounds and memory
allocation sizes to sane limits.* The SELinux hook functions have not been modified to be namespace-aware, so the hooks only perform checking against the current namespace. Thus, unsharing allows the process to escape confinement by the parent. Fixing this requires updating each hook
to
perform its processing on the current namespace and all of its ancestors
up to the init namespace.* Some of the hook functions can be called outside of process context
(e.g.task_kill,send_sigiotask, network input/forward) and should
not use
the current task's selinux namespace. These hooks need to be updated
to
obtain the proper selinux namespace to use instead from the caller
or
cached in a suitable data structure (e.g. the file or sock security
structures).* There are number of issues with the inode and superblock security
blob
handling for multiple namespaces, see those commits for more details.* Only a subset of object security blobs have been updated to be namespace-aware and support multiple namespaces. The ones that
have not yet been updated could end up performing permission checks
or
other operations on SIDs created in a different selinux namespace.* The network SID caches (netif, netnode, netport) have not yet been instantiated per selinux namespace, unlike the AVC and SS.
* There is no way currently to restrict or bound nesting of namespaces; if you allow it to a domain in the init namespace, then that domain can in turn unshare to arbitrary depths and can grant the same to any domain in its own policy. Related to this is the fact that there is no way to control resource usage due to selinux namespaces and they can be substantial (per-namespace policydb, sidtab, AVC, etc).
* SIDs may be cached by audit and networking code and in external kernel data structures and used later, potentially in a different selinux namespace than the one in which the SID was originally created.
* No doubt other things I'm forgetting or haven't thought of. Use at your own risk.
Not-signed-off-by Stephen Smalley <[email protected]>---
security/selinux/include/classmap.h | 3 +-
security/selinux/selinuxfs.c | 66
+++++++++++++++++++++++++++++++++++++2 files changed, 68 insertions(+), 1 deletion(-)security/selinux/include/classmap.h +0
-1@@ -39,7 +39,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
} },security/selinux/include/classmap.h +1
-0+ "setcheckreqprot", "read_policy", "validate_trans","unshare",
security/selinux/include/classmap.h +1
-0+ NULL } },{ "process",
{ "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched",
"setsched",security/selinux/selinuxfs.c +0
-0@@ -63,6 +63,7 @@ enum sel_inos { SEL_STATUS, /* export current status using mmap()*/
SEL_POLICY, /* allow userspace to read the in
kernel
policy */
SEL_VALIDATE_TRANS, /* compute validatetrans decision */security/selinux/selinuxfs.c +1
-0+ SEL_UNSHARE, /* unshare selinux namespace */
SEL_INO_NEXT, /* The next inode number to use */
};security/selinux/selinuxfs.c +0
-0@@ -321,6 +322,70 @@ static const struct file_operations sel_disable_ops = { .llseek = generic_file_llseek, };
+staticssize_tsel_write_unshare(struct file *file, const char__user*buf,security/selinux/selinuxfs.c +4
-0+ size_t count, loff_t *ppos) + +{ + struct selinux_fs_info *fsi = file_inode(file)->i_sb-Quoted 1 line
s_fs_info;security/selinux/selinuxfs.c +15
-0+struct selinux_ns *ns = fsi->ns; +char *page; +ssize_t length; +bool set; +int rc; + +if (count >=PAGE_SIZE ) +return -ENOMEM ; + +/* No partial writes. */ +if (*ppos !=0 ) +return -EINVAL ; + + rc = avc_has_perm(current_selinux_ns, current_sid(), + SECINITSID_SECURITY,
SECCLASS_SECURITY,security/selinux/selinuxfs.c +28
-0+ SECURITY__UNSHARE, NULL); +if (rc) +return rc; + + page =memdup_user_nul (buf, count); +if (IS_ERR (page)) +return PTR_ERR (page); + + length = -EINVAL ; +if (kstrtobool (page, &set)) + goto out; + +if (set) { +struct cred *cred =prepare_creds (); +struct task_security_struct *tsec; + +if (!cred) { + length = -ENOMEM ; + goto out; + } + tsec = cred->security; +if (selinux_ns_create (ns, &tsec->ns)) { +abort_creds (cred); + length = -ENOMEM ; + goto out; + } + tsec->osid = tsec->sid =SECINITSID_KERNEL ; + tsec->exec_sid = tsec->create_sid = tsec-Quoted 1 line
keycreate_sid=security/selinux/selinuxfs.c +16
-0+ tsec->sockcreate_sid =SECSID_NULL ; + tsec->parent_cred =get_current_cred (); +commit_creds (cred); + } + + length = count; +out: +kfree (page); +return length; +} + +static const struct file_operations sel_unshare_ops = { + .write = sel_write_unshare, + .llseek = generic_file_llseek, +}; +static
ssize_tsel_read_policyvers(struct file *filp, char__user
*buf,
size_t count,loff_t*ppos) {security/selinux/selinuxfs.c +0
-0@@ -1923,6 +1988,7 @@ static int sel_fill_super(struct
super_block
*sb, void *data, int silent)
[SEL_POLICY] = {"policy",&sel_policy_ops,S_IRUGO},
[SEL_VALIDATE_TRANS] = {"validatetrans",&sel_transition_ops,
S_IWUGO},security/selinux/selinuxfs.c +1
-0+ [SEL_UNSHARE] = {"unshare", &sel_unshare_ops,0222},
/* last one */ {""}
};
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
In reply to Stephen SmalleyQuoted 3 lines · Stephen Smalley
inet_sockettest failures are expected due to running in a non-init network namespace; they don't work even without unsharing the selinux namespace.
Do these results all look as expected?
Test Summary Report
-------------------
fdreceive/test (Wstat: 0 Tests: 3 Failed: 1)
Failed test: 3
inherit/test (Wstat: 0 Tests: 3 Failed: 1)
Failed test: 1
file/test (Wstat: 0 Tests: 16 Failed: 1)
Failed test: 8
bounds/test (Wstat: 0 Tests: 24 Failed: 5)
Failed tests: 3, 6, 12, 21, 23
mmap/test (Wstat: 0 Tests: 46 Failed: 2)
Failed tests: 9, 13
inet_socket/test (Wstat: 3584 Tests: 33 Failed: 14)
Failed tests: 1, 3, 5-6, 8, 16, 18, 20, 22, 25-26, 28
30, 32
Non-zero exit status: 14
overlay/test (Wstat: 3072 Tests: 121 Failed: 12)
Failed tests: 1, 25, 27, 39-40, 57, 63, 87, 89, 98-99
116 Non-zero exit status: 12
Files=51, Tests=485, 28 wallclock secs ( 0.60 usr 0.13 sys + 2.67 cusr 3.76 csys = 7.16 CPU)
Result: FAIL
Failed 7/51 test programs. 36/485 subtests failed.
Signature · 2 lines
James Morris <[email protected]>
A message that is not in the archive (sent off-list or not yet received): CAB9W1A2-PT8QU-md1s9fxhNg+Cv0C4Xu-i1w_q0XzQ+K9rsyAg@mail.gmail.com
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
On Oct 8, 2017 9:54 PM, "James Morris" <[email protected]> wrote:
Quoted 3 lines · Stephen Smalley
inet_sockettest failures are expected due to running in a non-init network namespace; they don't work even without unsharing the selinux namespace.
Do these results all look as expected?
No, that suggests that you either didn't insert the policy module allowing access to unlabeled fds or you didn't run restorecon -R /dev before running the tests. The only expected failures are the inet socket ones.
Test Summary Report
-------------------
fdreceive/test (Wstat: 0 Tests: 3 Failed: 1)
Failed test: 3
inherit/test (Wstat: 0 Tests: 3 Failed: 1)
Failed test: 1
file/test (Wstat: 0 Tests: 16 Failed: 1)
Failed test: 8
bounds/test (Wstat: 0 Tests: 24 Failed: 5)
Failed tests: 3, 6, 12, 21, 23
mmap/test (Wstat: 0 Tests: 46 Failed: 2)
Failed tests: 9, 13
inet_socket/test (Wstat: 3584 Tests: 33 Failed: 14)
Failed tests: 1, 3, 5-6, 8, 16, 18, 20, 22, 25-26, 28
30, 32
Non-zero exit status: 14
overlay/test (Wstat: 3072 Tests: 121 Failed: 12)
Failed tests: 1, 25, 27, 39-40, 57, 63, 87, 89, 98-99
116 Non-zero exit status: 12
Files=51, Tests=485, 28 wallclock secs ( 0.60 usr 0.13 sys + 2.67 cusr 3.76 csys = 7.16 CPU)
Result: FAIL
Failed 7/51 test programs. 36/485 subtests failed.
--
James Morris
<[email protected]>
Re: [RFC 09/10] selinux: add a selinuxfs interface to unshare selinux namespace
Quoted 8 lines · Stephen Smalley
On Oct 8, 2017 9:54 PM, "James Morris" <[email protected]> wrote:
Quoted 3 lines · Stephen Smalley
inet_sockettest failures are expected due to running in a non-init network namespace; they don't work even without unsharing the selinux namespace.
Quoted 7 lines
Do these results all look as expected?
No, that suggests that you either didn't insert the policy module allowing access to unlabeled fds or you didn't run restorecon -R /dev before running the tests. The only expected failures are the inet socket ones.
Looking better now -- I think it was the restorecon.
Signature · 2 lines
James Morris <[email protected]>
RFC 10/10 selinuxfs: restrict write operations to the same selinux namespace
In reply to Stephen SmalleyThis ensures that once a process unshares its selinux namespace, it can no longer act on the parent namespace's selinuxfs instance, irrespective of policy. This is a safety measure so that even if an otherwise unconfined process unshares its selinux namespace, it won't be able to subsequently affect the enforcing mode or policy of the parent. This also helps avoid common mistakes like failing to create a mount namespace and mount a new selinuxfs instance in order to act on one's own selinux namespace after unsharing.
--- security/selinux/selinuxfs.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
security/selinux/selinuxfs.c +33 -0
@@ -144,6 +144,9 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,ssize_t length;int new_value; +if (ns != current_selinux_ns) +return -EPERM ; +if (count >=PAGE_SIZE )return -ENOMEM ;
@@ -283,6 +286,9 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf,ssize_t length;int new_value; +if (ns != current_selinux_ns) +return -EPERM ; +if (count >=PAGE_SIZE )return -ENOMEM ;
@@ -333,6 +339,9 @@ static ssize_t sel_write_unshare(struct file *file, const char __user *buf,bool set;int rc; +if (ns != current_selinux_ns) +return -EPERM ; +if (count >=PAGE_SIZE )return -ENOMEM ;
@@ -605,6 +614,9 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,ssize_t length;void *data =NULL ; +if (ns != current_selinux_ns) +return -EPERM ; +mutex_lock (&fsi->mutex); length = avc_has_perm(current_selinux_ns,
@@ -716,6 +728,9 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,ssize_t length;unsigned int new_value; +if (ns != current_selinux_ns) +return -EPERM ; + length = avc_has_perm(current_selinux_ns, current_sid(), SECINITSID_SECURITY, SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
@@ -762,6 +777,9 @@ static ssize_t sel_write_validatetrans(struct file *file,u16 tclass;int rc; +if (ns != current_selinux_ns) +return -EPERM ; + rc =avc_has_perm (current_selinux_ns,current_sid (),SECINITSID_SECURITY ,SECCLASS_SECURITY ,SECURITY__VALIDATE_TRANS ,NULL );
@@ -849,6 +867,8 @@ static ssize_t (*write_op[])(struct file *, char *, size_t) = {static ssize_t selinux_transaction_write (struct file *file,const char __user *buf,size_t size,loff_t *pos) { +struct selinux_fs_info *fsi =file_inode (file)->i_sb->s_fs_info; +struct selinux_ns *ns = fsi->ns;ino_t ino =file_inode (file)->i_ino;char *data;ssize_t rv;
@@ -856,6 +876,9 @@ static ssize_t selinux_transaction_write(struct file *file, const char __user *bif (ino >=ARRAY_SIZE (write_op) || !write_op[ino])return -EINVAL ; +if (ns != current_selinux_ns) +return -EPERM ; + data =simple_transaction_get (file, buf, size);if (IS_ERR (data))return PTR_ERR (data);
@@ -1279,12 +1302,16 @@ static ssize_t sel_write_bool(struct file *filep, const char __user *buf, size_t count, loff_t *ppos) { struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; +struct selinux_ns *ns = fsi->ns;char *page =NULL ;ssize_t length;int new_value;unsigned index =file_inode (filep)->i_ino &SEL_INO_MASK ;const char *name = filep->f_path.dentry->d_name.name; +if (ns != current_selinux_ns) +return -EPERM ; +mutex_lock (&fsi->mutex); length = avc_has_perm(current_selinux_ns,
@@ -1347,6 +1374,9 @@ static ssize_t sel_commit_bools_write(struct file *filep,ssize_t length;int new_value; +if (ns != current_selinux_ns) +return -EPERM ; +mutex_lock (&fsi->mutex); length = avc_has_perm(current_selinux_ns,
@@ -1511,6 +1541,9 @@ static ssize_t sel_write_avc_cache_threshold(struct file *file,ssize_t ret;unsigned int new_value; +if (ns != current_selinux_ns) +return -EPERM ; + ret = avc_has_perm(current_selinux_ns, current_sid(), SECINITSID_SECURITY, SECCLASS_SECURITY, SECURITY__SETSECPARAM,
Signature · 1 line
2.9.5