Skip to contentSkip to conversation

selinux: reduce the use of hard-coded hash sizes

PATCH Superseded Ondrej Mosnacek 2 participants 7 messages Feb 17, 2020 – Feb 19, 2020 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: reduce the use of hard-coded hash sizes

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---
 security/selinux/ss/hashtab.c  | 21 ++++++++++++--
 security/selinux/ss/hashtab.h  |  2 +-
 security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
 security/selinux/ss/policydb.h |  2 --
 4 files changed, 40 insertions(+), 38 deletions(-)
security/selinux/ss/hashtab.c +19 -2
index ebfdaa31ee32..554a91ef3f06 100644
@@ -12,12 +12,26 @@
 
 static struct kmem_cache *hashtab_node_cachep;
 
+static u32 hashtab_compute_size(u32 nel)
+{
+	u32 size;
+
+	if (nel <= 2)
+		return nel;
+
+	/* use the nearest power of two to balance size and access time */
+	size = roundup_pow_of_two(nel);
+	if (size - nel > size / 4)
+		size /= 2;
+	return size;
+}
+
 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
-			       u32 size)
+			       u32 nel_hint)
 {
 	struct hashtab *p;
-	u32 i;
+	u32 i, size = hashtab_compute_size(nel_hint);
 
 	p = kzalloc(sizeof(*p), GFP_KERNEL);
 	if (!p)
@@ -27,6 +41,9 @@ struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *
 	p->nel = 0;
 	p->hash_value = hash_value;
 	p->keycmp = keycmp;
+	if (!size)
+		return p;
+
 	p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
 	if (!p->htable) {
 		kfree(p);
security/selinux/ss/hashtab.h +1 -1
index 3e3e42bfd150..dde54d9ff01c 100644
@@ -42,7 +42,7 @@ struct hashtab_info {
  */
 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
-			       u32 size);
+			       u32 nel_hint);
 
 /*
  * Inserts the specified (key, datum) pair into the specified hash table.
security/selinux/ss/policydb.c +20 -33

113 lines not on this page · raw message

security/selinux/ss/policydb.h +0 -2
index 41ad78a1f17b..72e2932fb12d 100644
@@ -321,8 +321,6 @@ extern int policydb_role_isvalid(struct policydb *p, unsigned int role);
 extern int policydb_read(struct policydb *p, void *fp);
 extern int policydb_write(struct policydb *p, void *fp);
 
-#define PERM_SYMTAB_SIZE 32
-
 #define POLICYDB_CONFIG_MLS    1
 
 /* the config flags related to unknown classes/perms are bits 2 and 3 */
Signature · 1 line
2.24.1
#1 lore raw
Quoted 16 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

Quoted 7 lines
---
 security/selinux/ss/hashtab.c  | 21 ++++++++++++--
 security/selinux/ss/hashtab.h  |  2 +-
 security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
 security/selinux/ss/policydb.h |  2 --
 4 files changed, 40 insertions(+), 38 deletions(-)

Note: This patch applies on top of the filename transition series [1].

[1] https://lore.kernel.org/selinux/[email protected]/T/

Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
#2 lore raw
In reply to Ondrej Mosnacek
Quoted 29 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---
  security/selinux/ss/hashtab.c  | 21 ++++++++++++--
  security/selinux/ss/hashtab.h  |  2 +-
  security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
  security/selinux/ss/policydb.h |  2 --
  4 files changed, 40 insertions(+), 38 deletions(-)
security/selinux/ss/hashtab.c +0 -0
index ebfdaa31ee32..554a91ef3f06 100644

security/selinux/ss/hashtab.c struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void * · line 49

Quoted 9 lines
security/selinux/ss/hashtab.c +3 -0
@@ -27,6 +41,9 @@ struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *
  	p->nel = 0;
  	p->hash_value = hash_value;
  	p->keycmp = keycmp;
+	if (!size)
+		return p;
+
  	p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
  	if (!p->htable) {
  		kfree(p);

Thanks, this looks promising. However, I was wondering: if we end up with size == 0 (e.g. policy happens to have an empty table), does the rest of the hashtab_* code always correctly handle the fact that ->htable could be NULL? Doesn't look obviously safe to me on a quick look.

#3 lore raw
Quoted 36 lines · Stephen Smalley
Quoted 35 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---
  security/selinux/ss/hashtab.c  | 21 ++++++++++++--
  security/selinux/ss/hashtab.h  |  2 +-
  security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
  security/selinux/ss/policydb.h |  2 --
  4 files changed, 40 insertions(+), 38 deletions(-)
security/selinux/ss/hashtab.c +3 -0
index ebfdaa31ee32..554a91ef3f06 100644
@@ -27,6 +41,9 @@ struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *
      p->nel = 0;
      p->hash_value = hash_value;
      p->keycmp = keycmp;
+     if (!size)
+             return p;
+
Quoted 8 lines

security/selinux/ss/hashtab.c struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void * · line 49

Quoted 3 lines
security/selinux/ss/hashtab.c +0 -0
      p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
      if (!p->htable) {
              kfree(p);

Thanks, this looks promising. However, I was wondering: if we end up with size == 0 (e.g. policy happens to have an empty table), does the rest of the hashtab_* code always correctly handle the fact that ->htable could be NULL? Doesn't look obviously safe to me on a quick look.

Hm... it seems I didn't think this through when I was trying to handle this case. I was rebasing this patch all over the place as I was working on other changes in parallel, so maybe I just lost the safety somewhere along the way... I think I will just clamp the minimum size to 1, as that seems both safer and simpler. The extra 8-byte allocation shouldn't cost much (there are only O(number of classes + commons) hash tables and these make no sense to have 0 entries).

--
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.

#4 lore raw
Quoted 46 lines · Ondrej Mosnacek
Quoted 36 lines · Stephen Smalley
Quoted 35 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---
   security/selinux/ss/hashtab.c  | 21 ++++++++++++--
   security/selinux/ss/hashtab.h  |  2 +-
   security/selinux/ss/policydb.c | 53 +++++++++++++---------------------
   security/selinux/ss/policydb.h |  2 --
   4 files changed, 40 insertions(+), 38 deletions(-)
security/selinux/ss/hashtab.c +3 -0
index ebfdaa31ee32..554a91ef3f06 100644
@@ -27,6 +41,9 @@ struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *
       p->nel = 0;
       p->hash_value = hash_value;
       p->keycmp = keycmp;
+     if (!size)
+             return p;
+
Quoted 8 lines

security/selinux/ss/hashtab.c struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void * · line 49

Quoted 3 lines
security/selinux/ss/hashtab.c +0 -0
       p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
       if (!p->htable) {
               kfree(p);

Thanks, this looks promising. However, I was wondering: if we end up with size == 0 (e.g. policy happens to have an empty table), does the rest of the hashtab_* code always correctly handle the fact that ->htable could be NULL? Doesn't look obviously safe to me on a quick look.

Quoted 7 lines

Hm... it seems I didn't think this through when I was trying to handle this case. I was rebasing this patch all over the place as I was working on other changes in parallel, so maybe I just lost the safety somewhere along the way... I think I will just clamp the minimum size to 1, as that seems both safer and simpler. The extra 8-byte allocation shouldn't cost much (there are only O(number of classes + commons) hash tables and these make no sense to have 0 entries).

Hmm...on booting this kernel, I am seeing a number of calls to hashtab_create() with nel_hint==0 leading to size == 0 and nothing is obviously breaking, so maybe this is safe?

#5 lore raw
Quoted 58 lines · Stephen Smalley
Quoted 49 lines · Ondrej Mosnacek
Quoted 39 lines · Stephen Smalley
Quoted 38 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases from 870 ms to 170 ms. It is also likely that other operations are going to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---
   security/selinux/ss/hashtab.c  | 21 ++++++++++++-- security/selinux/ss/hashtab.h  |  2 +- security/selinux/ss/policydb.c | 53
+++++++++++++---------------------
   security/selinux/ss/policydb.h |  2 --

   4 files changed, 40 insertions(+), 38 deletions(-)
security/selinux/ss/hashtab.c +0 -0
index ebfdaa31ee32..554a91ef3f06 100644
@@ -27,6 +41,9 @@ struct hashtab *hashtab_create(u32 

(*hash_value)(struct hashtab *h, const void *
       p->nel = 0;
       p->hash_value = hash_value;
       p->keycmp = keycmp;
+     if (!size)
+             return p;
+
       p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);

Quoted 8 lines
Quoted 2 lines

       if (!p->htable) {
               kfree(p);

Thanks, this looks promising.  However, I was wondering: if we end up with size == 0 (e.g. policy happens to have an empty table), does the rest of the hashtab_* code always correctly handle the fact that ->htable could be NULL?  Doesn't look obviously safe to me on a quick look.

Quoted 7 lines

Hm... it seems I didn't think this through when I was trying to handle this case. I was rebasing this patch all over the place as I was working on other changes in parallel, so maybe I just lost the safety somewhere along the way... I think I will just clamp the minimum size to 1, as that seems both safer and simpler. The extra 8-byte allocation shouldn't cost much (there are only O(number of classes + commons) hash tables and these make no sense to have 0 entries).

Quoted 1 line

Hmm...on booting this kernel, I am seeing a number of calls to hashtab_create() with nel_hint==0 leading to size == 0 and nothing is obviously breaking, so maybe this is safe?

I guess this happens for any class that doesn't define any of its own permissions (beyond the inherited common ones). Probably could just add some simple checks to hashtab_* where absent to ensure we don't ever dereference ->htable if NULL.

#6 lore raw
Quoted 65 lines · Stephen Smalley
Quoted 64 lines · Stephen Smalley
Quoted 62 lines · Ondrej Mosnacek
Quoted 44 lines · Stephen Smalley
Quoted 43 lines · Ondrej Mosnacek

Instead allocate hash tables with just the right size based on the actual number of elements (which is almost always known beforehand, we just need to defer the hashtab allocation to the right time). The only case when we don't know the size (with the current policy format) is the
new filename transitions hashtable. Here I just left the existing value.

After this patch, the time to load Fedora policy on x86_64 decreases from 950 ms to 220 ms. If the unconfined module is removed, it decreases
from 870 ms to 170 ms. It is also likely that other operations are going
to be faster, mainly string_to_context_struct() or mls_compute_sid(), but I didn't try to quantify that.

The memory usage increases a bit after this patch, but only by ~1-2 MB (it is hard to measure precisely). I believe it is a small price to pay for the increased performance.

---
   security/selinux/ss/hashtab.c  | 21 ++++++++++++--
   security/selinux/ss/hashtab.h  |  2 +-
   security/selinux/ss/policydb.c | 53
+++++++++++++---------------------
   security/selinux/ss/policydb.h |  2 --
   4 files changed, 40 insertions(+), 38 deletions(-)
security/selinux/ss/hashtab.c +0 -0
index ebfdaa31ee32..554a91ef3f06 100644
@@ -27,6 +41,9 @@ struct hashtab *hashtab_create(u32

(*hash_value)(struct hashtab *h, const void *

       p->nel = 0;
       p->hash_value = hash_value;
       p->keycmp = keycmp;
security/selinux/ss/hashtab.c +3 -0
+     if (!size)
+             return p;
+
       p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
Quoted 8 lines
Quoted 2 lines
       if (!p->htable) {
               kfree(p);

Thanks, this looks promising. However, I was wondering: if we end up with size == 0 (e.g. policy happens to have an empty table), does the rest of the hashtab_* code always correctly handle the fact that ->htable could be NULL? Doesn't look obviously safe to me on a quick look.

Hm... it seems I didn't think this through when I was trying to handle this case. I was rebasing this patch all over the place as I was working on other changes in parallel, so maybe I just lost the safety somewhere along the way... I think I will just clamp the minimum size to 1, as that seems both safer and simpler. The extra 8-byte allocation shouldn't cost much (there are only O(number of classes + commons) hash tables and these make no sense to have 0 entries).

Quoted 8 lines
Quoted 3 lines

Hmm...on booting this kernel, I am seeing a number of calls to hashtab_create() with nel_hint==0 leading to size == 0 and nothing is obviously breaking, so maybe this is safe?

I guess this happens for any class that doesn't define any of its own permissions (beyond the inherited common ones). Probably could just add some simple checks to hashtab_* where absent to ensure we don't ever dereference ->htable if NULL.

Yeah, OK. I only needed to add a check to hashtab_insert() and hashtab_search(), the rest will already do the sane thing as-is.

Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.