Hash context structures directly
cover Hash context structures directly
Converting a valid context to string and hashing that is inefficient because the conversion is slow. Instead we can hash the stucture contents directly, which is about 10 times faster.
After that, it is possible to move the hashing under sidtab.c, which avoids the complexity of calling context_add_hash() all over the place.
Ondrej Mosnacek (2):
selinux: hash context structure directly
selinux: move context hashing under sidtab
security/selinux/Makefile | 2 +- security/selinux/ss/context.c | 28 +++++++++++ security/selinux/ss/context.h | 11 +---- security/selinux/ss/ebitmap.c | 14 ++++++ security/selinux/ss/ebitmap.h | 1 + security/selinux/ss/policydb.c | 5 -- security/selinux/ss/services.c | 86 ++++++++++------------------------ security/selinux/ss/services.h | 3 -- security/selinux/ss/sidtab.c | 32 ++++++++----- security/selinux/ss/sidtab.h | 1 + 10 files changed, 92 insertions(+), 91 deletions(-) create mode 100644 security/selinux/ss/context.c
Signature · 1 line
2.25.2
PATCH 1/2 selinux: hash context structure directly
Always hashing the string representation is inefficient. Just hash the contents of the structure directly (using jhash). If the context is invalid (str & len are set), then hash the string as before, otherwise hash the structured data. Any context that is valid under the given policy should always be structured, and also any context that is invalid should be never structured, so the hashes should always match for the same context. The fact that context_cmp() also follows this logic further reinforces this assumption.
Since the context hashing function is now faster (about 10 times), this patch decreases the overhead of security_transition_sid(), which is called from many hooks.
The jhash function seemed as a good choice, since it is used as the default hashing algorithm in rhashtable.
--- security/selinux/Makefile | 2 +- security/selinux/ss/context.c | 28 ++++++++++++ security/selinux/ss/context.h | 6 ++- security/selinux/ss/ebitmap.c | 14 ++++++ security/selinux/ss/ebitmap.h | 1 + security/selinux/ss/policydb.c | 7 +-- security/selinux/ss/services.c | 80 ++++++++++++++-------------------- security/selinux/ss/services.h | 3 -- 8 files changed, 82 insertions(+), 59 deletions(-) create mode 100644 security/selinux/ss/context.c
security/selinux/Makefile +1 -1
@@ -8,7 +8,7 @@ obj-$(CONFIG_SECURITY_SELINUX) := selinux.o selinux-y := avc.o hooks.o selinuxfs.o netlink.o nlmsgtab.o netif.o \ netnode.o netport.o status.o \ ss/ebitmap.o ss/hashtab.o ss/symtab.o ss/sidtab.o ss/avtab.o \ - ss/policydb.o ss/services.o ss/conditional.o ss/mls.o + ss/policydb.o ss/services.o ss/conditional.o ss/mls.o ss/context.o selinux-$(CONFIG_SECURITY_NETWORK_XFRM) += xfrm.o
security/selinux/ss/context.c new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Implementations of the security context functions. +* +* Author: Ondrej Mosnacek <[email protected]> +* Copyright (C) 2018 Red Hat, Inc. +*/ + +#include <linux/jhash.h> + +#include "context.h" +#include "mls.h" + +u32 context_compute_hash (const struct context *c) +{ +u32 hash =0 ; + +if (c->len) +return full_name_hash (NULL , c->str, c->len); + + hash =jhash_3words (c->user, c->role, c->type, hash); + hash =jhash_2words (c->range.level[0 ].sens, + c->range.level[1 ].sens, hash); + hash =ebitmap_hash (&c->range.level[0 ].cat, hash); + hash =ebitmap_hash (&c->range.level[1 ].cat, hash); + +return hash; +}
security/selinux/ss/context.h +4 -2
@@ -196,9 +196,11 @@ static inline int context_cmp(struct context *c1, struct context *c2) mls_context_cmp(c1, c2)); } -static inline unsigned int context_compute_hash (const char *s) +u32 context_compute_hash (const struct context *c); + +static inline void context_add_hash (struct context *context) { -return full_name_hash (NULL , s,strlen (s)); + context->hash =context_compute_hash (context); }#endif /* _SS_CONTEXT_H_ */
security/selinux/ss/ebitmap.c +14 -0
@@ -19,6 +19,7 @@#include <linux/kernel.h> #include <linux/slab.h> #include <linux/errno.h> +#include <linux/jhash.h> #include <net/netlabel.h> #include "ebitmap.h" #include "policydb.h"
@@ -542,6 +543,19 @@ int ebitmap_write(struct ebitmap *e, void *fp)return 0 ; } +u32 ebitmap_hash (const struct ebitmap *e,u32 hash) +{ +struct ebitmap_node *node; + +/* need to change hash even if ebitmap is empty */ + hash =jhash_1word (e->highbit, hash); +for (node = e->node; node; node = node->next) { + hash =jhash_1word (node->startbit, hash); + hash =jhash (node->maps,sizeof (node->maps), hash); + } +return hash; +} +void __init ebitmap_cache_init (void ) { ebitmap_node_cachep = kmem_cache_create("ebitmap_node",
security/selinux/ss/ebitmap.h +1 -0
@@ -131,6 +131,7 @@ int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value);void ebitmap_destroy (struct ebitmap *e);int ebitmap_read (struct ebitmap *e,void *fp);int ebitmap_write (struct ebitmap *e,void *fp); +u32 ebitmap_hash (const struct ebitmap *e,u32 hash);#ifdef CONFIG_NETLABEL int ebitmap_netlbl_export(struct ebitmap *ebmap,
security/selinux/ss/policydb.c +2 -5
@@ -836,11 +836,8 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!name)continue ; - rc =context_add_hash (p, &c->context[0 ]); -if (rc) { -sidtab_destroy (s); - goto out; - } +context_add_hash (&c->context[0 ]); + rc =sidtab_set_initial (s, sid, &c->context[0 ]);if (rc) { pr_err("SELinux: unable to load initial SID %s.\n",
security/selinux/ss/services.c +32 -48
252 lines not on this page · raw message
security/selinux/ss/services.h +0 -3
@@ -8,7 +8,6 @@#define _SS_SERVICES_H_#include "policydb.h" -#include "context.h" /* Mapping for a single class */ struct selinux_mapping {
@@ -37,6 +36,4 @@ void services_compute_xperms_drivers(struct extended_perms *xperms,void services_compute_xperms_decision (struct extended_perms_decision *xpermd,struct avtab_node *node); -int context_add_hash (struct policydb *policydb,struct context *context); -#endif /* _SS_SERVICES_H_ */
Signature · 1 line
2.25.2
Paul Moore
maintainerRe: [PATCH 1/2] selinux: hash context structure directly
Quoted 29 lines · Ondrej Mosnacek
Always hashing the string representation is inefficient. Just hash the contents of the structure directly (using jhash). If the context is invalid (str & len are set), then hash the string as before, otherwise hash the structured data. Any context that is valid under the given policy should always be structured, and also any context that is invalid should be never structured, so the hashes should always match for the same context. The fact that
context_cmp()also follows this logic further reinforces this assumption.Since the context hashing function is now faster (about 10 times), this patch decreases the overhead of
security_transition_sid(), which is called from many hooks.The jhash function seemed as a good choice, since it is used as the default hashing algorithm in rhashtable.
Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/Makefile | 2 +- security/selinux/ss/context.c | 28 ++++++++++++ security/selinux/ss/context.h | 6 ++- security/selinux/ss/ebitmap.c | 14 ++++++ security/selinux/ss/ebitmap.h | 1 + security/selinux/ss/policydb.c | 7 +-- security/selinux/ss/services.c | 80 ++++++++++++++-------------------- security/selinux/ss/services.h | 3 -- 8 files changed, 82 insertions(+), 59 deletions(-) create mode 100644 security/selinux/ss/context.c
...
security/selinux/ss/context.c line 6
Quoted 12 lines
security/selinux/ss/context.c new file +6
-0@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Implementations of the security context functions. +* +* Author: Ondrej Mosnacek <[email protected]> +* Copyright (C) 2018 Red Hat, Inc.
I think your clock is a bit off ... ;)
Joking aside, copyright dates are important so please fix this (also because it is a copyright related issue, it isn't something I want to fix during the merge).
Quoted 9 lines
security/selinux/ss/context.c new file +9
-0+*/ + +#include <linux/jhash.h> + +#include "context.h" +#include "mls.h" + +u32 context_compute_hash (const struct context *c) +{
security/selinux/ss/context.c line 25
Quoted 10 lines
security/selinux/ss/context.c new file +10
-0+u32 hash =0 ; + +if (c->len) +return full_name_hash (NULL , c->str, c->len); + + hash =jhash_3words (c->user, c->role, c->type, hash); + hash =jhash_2words (c->range.level[0 ].sens, + c->range.level[1 ].sens, hash); + hash =ebitmap_hash (&c->range.level[0 ].cat, hash); + hash =ebitmap_hash (&c->range.level[1 ].cat, hash);
Most other places we try to abstract away the mls_range details by having an associated mls_XXX(...) function, it seems like that would be a good idea here too. How about adding a mls_context_hash(), or similarly named function and calling it here.
Quoted 87 lines
security/selinux/ss/context.c new file +2
-0+return hash; +}security/selinux/ss/context.h +4
-2@@ -196,9 +196,11 @@ static inline int context_cmp(struct context *c1, struct context *c2) mls_context_cmp(c1, c2)); } -static inline unsigned int context_compute_hash (const char *s) +u32 context_compute_hash (const struct context *c); + +static inline void context_add_hash (struct context *context) { -return full_name_hash (NULL , s,strlen (s)); + context->hash =context_compute_hash (context); }#endif /* _SS_CONTEXT_H_ */ security/selinux/ss/ebitmap.c +14
-0@@ -19,6 +19,7 @@#include <linux/kernel.h> #include <linux/slab.h> #include <linux/errno.h> +#include <linux/jhash.h> #include <net/netlabel.h> #include "ebitmap.h" #include "policydb.h" @@ -542,6 +543,19 @@ int ebitmap_write(struct ebitmap *e, void *fp)return 0 ; } +u32 ebitmap_hash (const struct ebitmap *e,u32 hash) +{ +struct ebitmap_node *node; + +/* need to change hash even if ebitmap is empty */ + hash =jhash_1word (e->highbit, hash); +for (node = e->node; node; node = node->next) { + hash =jhash_1word (node->startbit, hash); + hash =jhash (node->maps,sizeof (node->maps), hash); + } +return hash; +} +void __init ebitmap_cache_init (void ) { ebitmap_node_cachep = kmem_cache_create("ebitmap_node",security/selinux/ss/ebitmap.h +1
-0@@ -131,6 +131,7 @@ int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value);void ebitmap_destroy (struct ebitmap *e);int ebitmap_read (struct ebitmap *e,void *fp);int ebitmap_write (struct ebitmap *e,void *fp); +u32 ebitmap_hash (const struct ebitmap *e,u32 hash);#ifdef CONFIG_NETLABEL int ebitmap_netlbl_export(struct ebitmap *ebmap,security/selinux/ss/policydb.c +2
-5@@ -836,11 +836,8 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!name)continue ; - rc =context_add_hash (p, &c->context[0 ]); -if (rc) { -sidtab_destroy (s); - goto out; - } +context_add_hash (&c->context[0 ]); + rc =sidtab_set_initial (s, sid, &c->context[0 ]);if (rc) { pr_err("SELinux: unable to load initial SID %s.\n",security/selinux/ss/services.c +0
-0
security/selinux/ss/services.c out: · line 1494
Quoted 7 lines
security/selinux/ss/services.c +2
-2@@ -1490,38 +1490,11 @@ out:return rc; } -int context_add_hash (struct policydb *policydb, -struct context *context) +static int context_struct_to_sid (struct sidtab *sidtab,struct context *context, +u32 *sid)
Since you need to respin this patchset anyway, and patch 2/2 deals with the move to sidtab, I think it might be better to keep the context_struct_to_sid() arguments the same and not swap the first argument for the sidtab. If you keep the first argument as the state it makes this patch much more focused on the change at hand and leaves patch 2/2 dedicated to just the sidtab move.
Quoted 35 lines
security/selinux/ss/services.c +2
-29{ -int rc; -char *str; -int len; - -if (context->str) { - context->hash =context_compute_hash (context->str); - }else { - rc =context_struct_to_string (policydb, context, - &str, &len); -if (rc) -return rc; - context->hash =context_compute_hash (str); -kfree (str); - } -return 0 ; -} - -static int context_struct_to_sid (struct selinux_state *state, -struct context *context,u32 *sid) -{ -int rc; -struct sidtab *sidtab = state->ss->sidtab; -struct policydb *policydb = &state->ss->policydb; - -if (!context->hash) { - rc =context_add_hash (policydb, context); -if (rc) -return rc; - } +if (!context->hash) +context_add_hash (context);return sidtab_context_to_sid (sidtab, context, sid); }
Signature · 2 lines
paul moore www.paul-moore.com
Re: [PATCH 1/2] selinux: hash context structure directly
Quoted 48 lines · Paul Moore
Quoted 29 lines · Ondrej Mosnacek
Always hashing the string representation is inefficient. Just hash the contents of the structure directly (using jhash). If the context is invalid (str & len are set), then hash the string as before, otherwise hash the structured data. Any context that is valid under the given policy should always be structured, and also any context that is invalid should be never structured, so the hashes should always match for the same context. The fact that
context_cmp()also follows this logic further reinforces this assumption.Since the context hashing function is now faster (about 10 times), this patch decreases the overhead of
security_transition_sid(), which is called from many hooks.The jhash function seemed as a good choice, since it is used as the default hashing algorithm in rhashtable.
Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/Makefile | 2 +- security/selinux/ss/context.c | 28 ++++++++++++ security/selinux/ss/context.h | 6 ++- security/selinux/ss/ebitmap.c | 14 ++++++ security/selinux/ss/ebitmap.h | 1 + security/selinux/ss/policydb.c | 7 +-- security/selinux/ss/services.c | 80 ++++++++++++++-------------------- security/selinux/ss/services.h | 3 -- 8 files changed, 82 insertions(+), 59 deletions(-) create mode 100644 security/selinux/ss/context.c...
security/selinux/ss/context.c line 6
Quoted 12 lines
security/selinux/ss/context.c new file +6
-0@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Implementations of the security context functions. +* +* Author: Ondrej Mosnacek <[email protected]> +* Copyright (C) 2018 Red Hat, Inc. I think your clock is a bit off ... ;)
Quoted 3 lines
Joking aside, copyright dates are important so please fix this (also because it is a copyright related issue, it isn't something I want to fix during the merge).
Ah, I copy-pasted the boilerplate from elsewhere and forgot to fix the year... No problem, I'll send a fixed patch.
Quoted 17 lines
Quoted 17 lines
security/selinux/ss/context.c new file +16
-0+*/ + +#include <linux/jhash.h> + +#include "context.h" +#include "mls.h" + +u32 context_compute_hash (const struct context *c) +{ +u32 hash =0 ; + +if (c->len) +return full_name_hash (NULL , c->str, c->len); + + hash =jhash_3words (c->user, c->role, c->type, hash); + hash = jhash_2words(c->range.level[0].sens,
Quoted 8 lines
security/selinux/ss/context.c line 25
Quoted 3 lines
security/selinux/ss/context.c new file +3
-0+ c->range.level[1].sens, hash); + hash =ebitmap_hash (&c->range.level[0 ].cat, hash); + hash =ebitmap_hash (&c->range.level[1 ].cat, hash);Most other places we try to abstract away the
mls_rangedetails by having an associatedmls_XXX(...)function, it seems like that would be a good idea here too. How about adding amls_context_hash(), or similarly named function and calling it here.
Good point... I'm a little wary of adding yet another nested function call to something that should be fast (and that will do only a few operations on its own), but I suppose I can just make it an inline function.
Quoted 3 lines
Quoted 3 lines
security/selinux/ss/context.c new file +2
-0+return hash; +}
[...]
Quoted 8 lines
Quoted 8 lines
security/selinux/ss/services.c +0
-0@@ -1490,38 +1490,11 @@ out:return rc; }
Quoted 11 lines
security/selinux/ss/services.c out: · line 1494
Quoted 4 lines
security/selinux/ss/services.c +2
-2-int context_add_hash (struct policydb *policydb, -struct context *context) +static int context_struct_to_sid (struct sidtab *sidtab,struct context *context, +u32 *sid)Since you need to respin this patchset anyway, and patch 2/2 deals with the move to sidtab, I think it might be better to keep the
context_struct_to_sid()arguments the same and not swap the first argument for the sidtab. If you keep the first argument as the state it makes this patch much more focused on the change at hand and leaves patch 2/2 dedicated to just the sidtab move.
Also a good point. I wanted to emphasize that the computation no longer depends on policydb, but I agree that it adds an unnecessary churn to this patch. I'll amend it as you suggest.
Thanks,
--
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
PATCH 2/2 selinux: move context hashing under sidtab
In reply to Ondrej MosnacekNow that context hash computation no longer depends on policydb, we can simplify things by moving the context hashing completely under sidtab. The hash is still cached in sidtab entries, but not for the in-flight context structures.
--- security/selinux/ss/context.h | 11 +-------- security/selinux/ss/policydb.c | 2 -- security/selinux/ss/services.c | 44 ++++++++++------------------------ security/selinux/ss/sidtab.c | 32 +++++++++++++++---------- security/selinux/ss/sidtab.h | 1 + 5 files changed, 34 insertions(+), 56 deletions(-)
security/selinux/ss/context.h +1 -10
@@ -31,7 +31,6 @@ struct context {u32 len;/* length of string in bytes */ struct mls_range range;char *str;/* string representation if context cannot be mapped. */ -u32 hash;/* a hash of the string representation */ };static inline void mls_context_init (struct context *c)
@@ -169,13 +168,12 @@ static inline int context_cpy(struct context *dst, struct context *src)kfree (dst->str);return rc; } - dst->hash = src->hash;return 0 ; }static inline void context_destroy (struct context *c) { - c->user = c->role = c->type = c->hash =0 ; + c->user = c->role = c->type =0 ;kfree (c->str); c->str =NULL ; c->len =0 ;
@@ -184,8 +182,6 @@ static inline void context_destroy(struct context *c)static inline int context_cmp (struct context *c1,struct context *c2) { -if (c1->hash && c2->hash && (c1->hash != c2->hash)) -return 0 ;if (c1->len && c2->len)return (c1->len == c2->len && !strcmp (c1->str, c2->str));if (c1->len || c2->len)
@@ -198,10 +194,5 @@ static inline int context_cmp(struct context *c1, struct context *c2)u32 context_compute_hash (const struct context *c); -static inline void context_add_hash (struct context *context) -{ - context->hash =context_compute_hash (context); -} -#endif /* _SS_CONTEXT_H_ */
security/selinux/ss/policydb.c +0 -2
@@ -836,8 +836,6 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!name)continue ; -context_add_hash (&c->context[0 ]); - rc =sidtab_set_initial (s, sid, &c->context[0 ]);if (rc) { pr_err("SELinux: unable to load initial SID %s.\n",
security/selinux/ss/services.c +13 -31
148 lines not on this page · raw message
security/selinux/ss/sidtab.c +19 -13
@@ -54,14 +54,15 @@ int sidtab_init(struct sidtab *s)return 0 ; } -static u32 context_to_sid (struct sidtab *s,struct context *context) +static u32 context_to_sid (struct sidtab *s,struct context *context,u32 hash) {struct sidtab_entry *entry;u32 sid =0 ;rcu_read_lock (); -hash_for_each_possible_rcu (s->context_to_sid, entry, list, - context->hash) { +hash_for_each_possible_rcu (s->context_to_sid, entry, list, hash) { +if (entry->hash != hash) +continue ;if (context_cmp (&entry->context, context)) { sid = entry->sid;break ;
@@ -74,6 +75,7 @@ static u32 context_to_sid(struct sidtab *s, struct context *context)int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context) {struct sidtab_isid_entry *isid; +u32 hash;int rc;if (sid ==0 || sid >SECINITSID_NUM )
@@ -90,15 +92,18 @@ int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context)#endif isid->set =1 ; + hash =context_compute_hash (context); +/* * Multiple initial sids may map to the same context. Check that this * context is not already represented in the context_to_sid hashtable * to avoid duplicate entries and long linked lists upon hash * collision. */ -if (!context_to_sid (s, context)) { +if (!context_to_sid (s, context, hash)) { isid->entry.sid = sid; -hash_add (s->context_to_sid, &isid->entry.list, context->hash); + isid->entry.hash = hash; +hash_add (s->context_to_sid, &isid->entry.list, hash); }return 0 ;
@@ -259,12 +264,12 @@ int sidtab_context_to_sid(struct sidtab *s, struct context *context, u32 *sid) {unsigned long flags; -u32 count; +u32 count, hash =context_compute_hash (context);struct sidtab_convert_params *convert;struct sidtab_entry *dst, *dst_convert;int rc; - *sid =context_to_sid (s, context); + *sid =context_to_sid (s, context, hash);if (*sid)return 0 ;
@@ -272,7 +277,7 @@ int sidtab_context_to_sid(struct sidtab *s, struct context *context,spin_lock_irqsave (&s->lock, flags); rc =0 ; - *sid =context_to_sid (s, context); + *sid =context_to_sid (s, context, hash);if (*sid) goto out_unlock;
@@ -292,6 +297,7 @@ int sidtab_context_to_sid(struct sidtab *s, struct context *context, goto out_unlock; dst->sid =index_to_sid (count); + dst->hash = hash; rc =context_cpy (&dst->context, context);if (rc)
@@ -316,10 +322,11 @@ int sidtab_context_to_sid(struct sidtab *s, struct context *context, goto out_unlock; } dst_convert->sid =index_to_sid (count); + dst_convert->hash =context_compute_hash (&dst_convert->context); convert->target->count = count +1 ;hash_add_rcu (convert->target->context_to_sid, - &dst_convert->list, dst_convert->context.hash); + &dst_convert->list, dst_convert->hash); }if (context->len)
@@ -330,7 +337,7 @@ int sidtab_context_to_sid(struct sidtab *s, struct context *context,/* write entries before updating count */ smp_store_release (&s->count, count +1 ); -hash_add_rcu (s->context_to_sid, &dst->list, dst->context.hash); +hash_add_rcu (s->context_to_sid, &dst->list, dst->hash); rc =0 ; out_unlock:
@@ -346,10 +353,9 @@ static void sidtab_convert_hashtable(struct sidtab *s, u32 count)for (i =0 ; i < count; i++) { entry =sidtab_do_lookup (s, i,0 ); entry->sid =index_to_sid (i); + entry->hash =context_compute_hash (&entry->context); -hash_add_rcu (s->context_to_sid, &entry->list, - entry->context.hash); - +hash_add_rcu (s->context_to_sid, &entry->list, entry->hash); } }
security/selinux/ss/sidtab.h +1 -0
@@ -19,6 +19,7 @@struct sidtab_entry {u32 sid; +u32 hash;struct context context;#if CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE >0 struct sidtab_str_cache __rcu *cache;
Signature · 1 line
2.25.2