Skip to contentSkip to conversation

Fix ENOMEM errors during policy reload

RFC PATCH Superseded Ondrej Mosnacek 2 participants 12 messages Nov 29, 2018 – Nov 30, 2018 SELinux security module

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix ENOMEM errors during policy reload

Previous versions of this patchset:
https://lore.kernel.org/selinux/[email protected]/T/
https://lore.kernel.org/selinux/[email protected]/T/

The patches that have "[squash]" in the subject are posted separately for ease of review but will be folded into the previous patch in the final posting. Likewise, patches with "[fixup]" at the beginning odf the subject are logically separarted fixes/tweaks for the last non-fixup commit preceding them.

Changes in v3:
(new changes are only in the "[fixup]" patches)
- fixed handling of SECSID_NULL (it is now remapped to unlabeled as before)
* verified using selinux-testsuite
- cosmetic tweak: add sidtab_lookup_initial() helper to make sidtab_search_core() a bit more readable
- fixed converting of unmapped contexts
* verified using the reproducer from Stephen Smalley
- cosmetic tweak: drop the 'out' goto label in convert_context()
- fixed SIDTAB_LEAF_ENTRIES to use SIDTAB_NODE_ALLOC_SIZE instead of using PAGE_SIZE directly

Changes in v2:
- dropped the first patch since it is already merged in -next
- fixed initial SID table to not reserve an entry for SECSID_NULL
- added back an equivalent of the reverse lookup cache
- fixed checkpatch.pl errors and some warnings

Testing:
An x86_64 Fedora 29 kernel with this patchset applied passes selinux-testsuite and the reproducer from GH issue #38 [1]. I also made sure that the two bugs discovered in previous version are now fixed.

[1] https://github.com/SELinuxProject/selinux-kernel/issues/38

Ondrej Mosnacek (9):
selinux: use separate table for initial SID lookup [squash] do not store entry for SECSID_NULL
[fixup] fix handling of SECSID_NULL
[fixup] cosmetic tweak for sidtab_search_core()
selinux: overhaul sidtab to fix bug and improve performance [fixup] fix handling of invalid context in convert_context [fixup] remove the now unneeded 'out' label
[fixup] fix SIDTAB_LEAF_ENTRIES to use the correct size macro [squash] add back reverse lookup cache to sidtab

 security/selinux/ss/mls.c      |  23 +-
 security/selinux/ss/mls.h      |   3 +-
 security/selinux/ss/policydb.c |  10 +-
 security/selinux/ss/services.c | 200 +++++------
 security/selinux/ss/services.h |   2 +-
 security/selinux/ss/sidtab.c   | 616 +++++++++++++++++++++------------
 security/selinux/ss/sidtab.h   |  95 +++--
 7 files changed, 574 insertions(+), 375 deletions(-)
Signature · 1 line
2.19.2
#1 lore raw

RFC PATCH v3 1/9 selinux: use separate table for initial SID lookup

This moves handling of initial SIDs into a separate table. Note that the SIDs stored in the main table are now shifted by SECINITSID_NUM and converted to/from the actual SIDs transparently by helper functions.

This change doesn't make much sense on its own, but it simplifies further sidtab overhaul in a succeeding patch.

---
 security/selinux/ss/policydb.c |  10 ++-
 security/selinux/ss/services.c |  88 ++++++++++--------
 security/selinux/ss/services.h |   2 +-
 security/selinux/ss/sidtab.c   | 158 +++++++++++++++++++--------------
 security/selinux/ss/sidtab.h   |  14 +--
 5 files changed, 162 insertions(+), 110 deletions(-)
security/selinux/ss/policydb.c +9 -1
index b63ef865ce1e..59359fa0bd74 100644
@@ -909,13 +909,21 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)
 		if (!c->context[0].user) {
 			pr_err("SELinux:  SID %s was never defined.\n",
 				c->u.name);
+			sidtab_destroy(s);
+			goto out;
+		}
+		if (c->sid[0] > SECINITSID_NUM) {
+			pr_err("SELinux:  Initial SID %s out of range.\n",
+				c->u.name);
+			sidtab_destroy(s);
 			goto out;
 		}
 
-		rc = sidtab_insert(s, c->sid[0], &c->context[0]);
+		rc = sidtab_set_initial(s, c->sid[0], &c->context[0]);
 		if (rc) {
 			pr_err("SELinux:  unable to load initial SID %s.\n",
 				c->u.name);
+			sidtab_destroy(s);
 			goto out;
 		}
 	}
security/selinux/ss/services.c +50 -38

299 lines not on this page · raw message

security/selinux/ss/services.h +1 -1
index 24c7bdcc8075..9a36de860368 100644
@@ -24,7 +24,7 @@ struct selinux_map {
 };
 
 struct selinux_ss {
-	struct sidtab sidtab;
+	struct sidtab *sidtab;
 	struct policydb policydb;
 	rwlock_t policy_rwlock;
 	u32 latest_granting;
security/selinux/ss/sidtab.c +93 -65

255 lines not on this page · raw message

security/selinux/ss/sidtab.h +9 -5
index e1d1f0beb17c..dc0a80bc8894 100644
@@ -22,6 +22,11 @@ struct sidtab_node {
 
 #define SIDTAB_SIZE SIDTAB_HASH_BUCKETS
 
+struct sidtab_isid_entry {
+	int set;
+	struct context context;
+};
+
 struct sidtab {
 	struct sidtab_node **htable;
 	unsigned int nel;	/* number of elements */
@@ -30,10 +35,12 @@ struct sidtab {
 #define SIDTAB_CACHE_LEN	3
 	struct sidtab_node *cache[SIDTAB_CACHE_LEN];
 	spinlock_t lock;
+
+	struct sidtab_isid_entry isids[SECINITSID_NUM + 1];
 };
 
 int sidtab_init(struct sidtab *s);
-int sidtab_insert(struct sidtab *s, u32 sid, struct context *context);
+int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context);
 struct context *sidtab_search(struct sidtab *s, u32 sid);
 struct context *sidtab_search_force(struct sidtab *s, u32 sid);
 
@@ -43,13 +50,10 @@ int sidtab_convert(struct sidtab *s, struct sidtab *news,
 				void *args),
 		   void *args);
 
-int sidtab_context_to_sid(struct sidtab *s,
-			  struct context *context,
-			  u32 *sid);
+int sidtab_context_to_sid(struct sidtab *s, struct context *context, u32 *sid);
 
 void sidtab_hash_eval(struct sidtab *h, char *tag);
 void sidtab_destroy(struct sidtab *s);
-void sidtab_set(struct sidtab *dst, struct sidtab *src);
 
 #endif	/* _SS_SIDTAB_H_ */
 
Signature · 1 line
2.19.2
#2 lore raw

RFC PATCH v3 2/9 do not store entry for SECSID_NULL

In reply to Ondrej Mosnacek

This patch is kept separate only for review. Eventually it will be folded into the previous patch.

---
 security/selinux/ss/policydb.c |  2 +-
 security/selinux/ss/sidtab.c   | 25 ++++++++++++++++---------
 security/selinux/ss/sidtab.h   |  3 ++-
 3 files changed, 19 insertions(+), 11 deletions(-)
security/selinux/ss/policydb.c +1 -1
index 59359fa0bd74..a50d625e7946 100644
@@ -912,7 +912,7 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)
 			sidtab_destroy(s);
 			goto out;
 		}
-		if (c->sid[0] > SECINITSID_NUM) {
+		if (c->sid[0] == SECSID_NULL || c->sid[0] > SECINITSID_NUM) {
 			pr_err("SELinux:  Initial SID %s out of range.\n",
 				c->u.name);
 			sidtab_destroy(s);
security/selinux/ss/sidtab.c +16 -9
index fd8115b211a6..e157d8240cf1 100644
@@ -23,7 +23,7 @@ int sidtab_init(struct sidtab *s)
 	if (!s->htable)
 		return -ENOMEM;
 
-	for (i = 0; i <= SECINITSID_NUM; i++)
+	for (i = 0; i < SECINITSID_NUM; i++)
 		s->isids[i].set = 0;
 
 	for (i = 0; i < SIDTAB_SIZE; i++)
@@ -86,8 +86,15 @@ static int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)
 
 int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context)
 {
-	struct sidtab_isid_entry *entry = &s->isids[sid];
-	int rc = context_cpy(&entry->context, context);
+	struct sidtab_isid_entry *entry;
+	int rc;
+
+	if (sid == 0 || sid > SECINITSID_NUM)
+		return -EINVAL;
+
+	entry = &s->isids[sid - 1];
+
+	rc = context_cpy(&entry->context, context);
 	if (rc)
 		return rc;
 
@@ -116,19 +123,19 @@ static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
 	struct context *context;
 	struct sidtab_isid_entry *entry;
 
-	if (!s)
+	if (!s || sid == 0)
 		return NULL;
 
 	if (sid > SECINITSID_NUM) {
 		context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
 	} else {
-		entry = &s->isids[sid];
+		entry = &s->isids[sid - 1];
 		context = entry->set ? &entry->context : NULL;
 	}
 	if (context && (!context->len || force))
 		return context;
 
-	entry = &s->isids[SECINITSID_UNLABELED];
+	entry = &s->isids[SECINITSID_UNLABELED - 1];
 	return entry->set ? &entry->context : NULL;
 }
 
@@ -283,11 +290,11 @@ int sidtab_context_to_sid(struct sidtab *s, struct context *context, u32 *sid)
 	int rc;
 	u32 i;
 
-	for (i = 0; i <= SECINITSID_NUM; i++) {
+	for (i = 0; i < SECINITSID_NUM; i++) {
 		struct sidtab_isid_entry *entry = &s->isids[i];
 
 		if (entry->set && context_cmp(context, &entry->context)) {
-			*sid = i;
+			*sid = i + 1;
 			return 0;
 		}
 	}
@@ -334,7 +341,7 @@ void sidtab_destroy(struct sidtab *s)
 	if (!s)
 		return;
 
-	for (i = 0; i <= SECINITSID_NUM; i++)
+	for (i = 0; i < SECINITSID_NUM; i++)
 		if (s->isids[i].set)
 			context_destroy(&s->isids[i].context);
 
security/selinux/ss/sidtab.h +2 -1
index dc0a80bc8894..e657ae6bf996 100644
@@ -36,7 +36,8 @@ struct sidtab {
 	struct sidtab_node *cache[SIDTAB_CACHE_LEN];
 	spinlock_t lock;
 
-	struct sidtab_isid_entry isids[SECINITSID_NUM + 1];
+	/* index == SID - 1 (no entry for SECSID_NULL) */
+	struct sidtab_isid_entry isids[SECINITSID_NUM];
 };
 
 int sidtab_init(struct sidtab *s);
Signature · 1 line
2.19.2
#3 lore raw

RFC PATCH v3 3/9 fix handling of SECSID_NULL

In reply to Ondrej Mosnacek
---
 security/selinux/ss/sidtab.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
security/selinux/ss/sidtab.c +10 -8
index e157d8240cf1..31588d704b98 100644
@@ -123,17 +123,19 @@ static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
 	struct context *context;
 	struct sidtab_isid_entry *entry;
 
-	if (!s || sid == 0)
+	if (!s)
 		return NULL;
 
-	if (sid > SECINITSID_NUM) {
-		context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
-	} else {
-		entry = &s->isids[sid - 1];
-		context = entry->set ? &entry->context : NULL;
+	if (sid != 0) {
+		if (sid > SECINITSID_NUM) {
+			context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
+		} else {
+			entry = &s->isids[sid - 1];
+			context = entry->set ? &entry->context : NULL;
+		}
+		if (context && (!context->len || force))
+			return context;
 	}
-	if (context && (!context->len || force))
-		return context;
 
 	entry = &s->isids[SECINITSID_UNLABELED - 1];
 	return entry->set ? &entry->context : NULL;
Signature · 1 line
2.19.2
maintainer
#4 lore raw

Re: [RFC PATCH v3 3/9] [fixup] fix handling of SECSID_NULL

Quoted 4 lines · Ondrej Mosnacek
---
 security/selinux/ss/sidtab.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

A quick administrative comment: please don't send "[fixup]" patches except under the most extreme circumstances, especially in the case where you are respinning the patch{set}. Using fixup patches makes it much harder for me to review, test, apply your patches which makes me grumpy.

I don't like being grumpy.

Quoted 35 lines
security/selinux/ss/sidtab.c +10 -9
index e157d8240cf1..31588d704b98 100644
@@ -123,17 +123,19 @@ static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
        struct context *context;
        struct sidtab_isid_entry *entry;
 
-       if (!s || sid == 0)
+       if (!s)
                return NULL;
 
-       if (sid > SECINITSID_NUM) {
-               context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
-       } else {
-               entry = &s->isids[sid - 1];
-               context = entry->set ? &entry->context : NULL;
+       if (sid != 0) {
+               if (sid > SECINITSID_NUM) {
+                       context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
+               } else {
+                       entry = &s->isids[sid - 1];
+                       context = entry->set ? &entry->context : NULL;
+               }
+               if (context && (!context->len || force))
+                       return context;
        }
-       if (context && (!context->len || force))
-               return context;
 
        entry = &s->isids[SECINITSID_UNLABELED - 1];
        return entry->set ? &entry->context : NULL;
--

2.19.2

Signature · 2 lines
paul moore
www.paul-moore.com
#5 lore raw

Re: [RFC PATCH v3 3/9] [fixup] fix handling of SECSID_NULL

Quoted 6 lines · Paul Moore
Quoted 4 lines · Ondrej Mosnacek
---
 security/selinux/ss/sidtab.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
Quoted 7 lines

A quick administrative comment: please don't send "[fixup]" patches except under the most extreme circumstances, especially in the case where you are respinning the patch{set}. Using fixup patches makes it much harder for me to review, test, apply your patches which makes me grumpy.

I don't like being grumpy.

Oh, sorry... I hoped it would actually make review easier to have each new change clearly separated out. Now I will know that's not case and will change my practices accordingly.

I will do a new squashed respin identical to this patchset (except for one tiny change that came into my mind in the meantime).

Quoted 31 lines
Quoted 31 lines
security/selinux/ss/sidtab.c +10 -8
index e157d8240cf1..31588d704b98 100644
@@ -123,17 +123,19 @@ static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
        struct context *context;
        struct sidtab_isid_entry *entry;
 
-       if (!s || sid == 0)
+       if (!s)
                return NULL;
 
-       if (sid > SECINITSID_NUM) {
-               context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
-       } else {
-               entry = &s->isids[sid - 1];
-               context = entry->set ? &entry->context : NULL;
+       if (sid != 0) {
+               if (sid > SECINITSID_NUM) {
+                       context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
+               } else {
+                       entry = &s->isids[sid - 1];
+                       context = entry->set ? &entry->context : NULL;
+               }
+               if (context && (!context->len || force))
+                       return context;
        }
-       if (context && (!context->len || force))
-               return context;
 
Quoted 10 lines
Quoted 5 lines
security/selinux/ss/sidtab.c +0 -1
        entry = &s->isids[SECINITSID_UNLABELED - 1];
        return entry->set ? &entry->context : NULL;
--

2.19.2

--
paul moore
www.paul-moore.com

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

#6 lore raw

RFC PATCH v3 4/9 cosmetic tweak for sidtab_search_core()

In reply to Ondrej Mosnacek
---
 security/selinux/ss/sidtab.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
security/selinux/ss/sidtab.c +9 -8
index 31588d704b98..45a50172f139 100644
@@ -118,27 +118,28 @@ static struct context *sidtab_lookup(struct sidtab *s, u32 sid)
 	return &cur->context;
 }
 
+static struct context *sidtab_lookup_initial(struct sidtab *s, u32 sid)
+{
+	return s->isids[sid - 1].set ? &s->isids[sid - 1].context : NULL;
+}
+
 static struct context *sidtab_search_core(struct sidtab *s, u32 sid, int force)
 {
 	struct context *context;
-	struct sidtab_isid_entry *entry;
 
 	if (!s)
 		return NULL;
 
 	if (sid != 0) {
-		if (sid > SECINITSID_NUM) {
+		if (sid > SECINITSID_NUM)
 			context = sidtab_lookup(s, sid - (SECINITSID_NUM + 1));
-		} else {
-			entry = &s->isids[sid - 1];
-			context = entry->set ? &entry->context : NULL;
-		}
+		else
+			context = sidtab_lookup_initial(s, sid);
 		if (context && (!context->len || force))
 			return context;
 	}
 
-	entry = &s->isids[SECINITSID_UNLABELED - 1];
-	return entry->set ? &entry->context : NULL;
+	return sidtab_lookup_initial(s, SECINITSID_UNLABELED);
 }
 
 struct context *sidtab_search(struct sidtab *s, u32 sid)
Signature · 1 line
2.19.2
#7 lore raw

RFC PATCH v3 5/9 selinux: overhaul sidtab to fix bug and improve performance

In reply to Ondrej Mosnacek

Before this patch, during a policy reload the sidtab would become frozen and trying to map a new context to SID would be unable to add a new entry to sidtab and fail with -ENOMEM.

Such failures are usually propagated into userspace, which has no way of distignuishing them from actual allocation failures and thus doesn't handle them gracefully. Such situation can be triggered e.g. by the following reproducer:

    while true; do load_policy; echo -n .; sleep 0.1; done &
    for (( i = 0; i < 1024; i++ )); do
        runcon -l s0:c$i echo -n x || break
        # or:
        # chcon -l s0:c$i <some_file> || break
    done

This patch overhauls the sidtab so it doesn't need to be frozen during policy reload, thus solving the above problem.

The new SID table leverages the fact that SIDs are allocated sequentially and are never invalidated and stores them in linear buckets indexed by a tree structure. This brings several advantages:
1. Fast SID -> context lookup - this lookup can now be done in logarithmic time complexity (usually in less than 4 array lookups) and can still be done safely without locking.
2. No need to re-search the whole table on reverse lookup miss - after acquiring the spinlock only the newly added entries need to be searched, which means that reverse lookups that end up inserting a new entry are now about twice as fast.
3. No need to freeze sidtab during policy reload - it is now possible to handle insertion of new entries even during sidtab conversion.

The tree structure of the new sidtab is able to grow automatically to up to about 2^31 entries (at which point it should not have more than about 4 tree levels). The old sidtab had a theoretical capacity of almost 2^32 entries, but half of that is still more than enough since by that point the reverse table lookups would become unusably slow anyway...

The number of entries per tree node is selected automatically so that each node fits into a single page, which should be the easiest size for kmalloc() to handle.

Note that the new implementation does not have a cache for recent reverse lookups as the old one had. Such cache is, however, added in a subsequent patch.

Tested by selinux-testsuite and thoroughly tortured by this simple stress test:

```
function rand_cat() {
	echo $(( $RANDOM % 1024 ))
}

function do_work() {
	while true; do
		echo -n "system_u:system_r:kernel_t:s0:c$(rand_cat),c$(rand_cat)" \
			>/sys/fs/selinux/context 2>/dev/null || true
	done
}

do_work >/dev/null &
do_work >/dev/null &
do_work >/dev/null &

while load_policy; do echo -n .; sleep 0.1; done

kill %1
kill %2
kill %3
```
---
 security/selinux/ss/mls.c      |  23 +-
 security/selinux/ss/mls.h      |   3 +-
 security/selinux/ss/services.c |  90 +++---
 security/selinux/ss/sidtab.c   | 523 +++++++++++++++++++--------------
 security/selinux/ss/sidtab.h   |  75 +++--
 5 files changed, 401 insertions(+), 313 deletions(-)
security/selinux/ss/mls.c +10 -13
index 2fe459df3c85..267ae4f9be79 100644
@@ -436,16 +436,17 @@ int mls_setup_user_range(struct policydb *p,
 
 /*
  * Convert the MLS fields in the security context
- * structure `c' from the values specified in the
- * policy `oldp' to the values specified in the policy `newp'.
+ * structure `oldc' from the values specified in the
+ * policy `oldp' to the values specified in the policy `newp',
+ * storing the resulting context in `newc'.
  */
 int mls_convert_context(struct policydb *oldp,
 			struct policydb *newp,
-			struct context *c)
+			struct context *oldc,
+			struct context *newc)
 {
 	struct level_datum *levdatum;
 	struct cat_datum *catdatum;
-	struct ebitmap bitmap;
 	struct ebitmap_node *node;
 	int l, i;
 
@@ -455,28 +456,24 @@ int mls_convert_context(struct policydb *oldp,
 	for (l = 0; l < 2; l++) {
 		levdatum = hashtab_search(newp->p_levels.table,
 					  sym_name(oldp, SYM_LEVELS,
-						   c->range.level[l].sens - 1));
+						   oldc->range.level[l].sens - 1));
 
 		if (!levdatum)
 			return -EINVAL;
-		c->range.level[l].sens = levdatum->level->sens;
+		newc->range.level[l].sens = levdatum->level->sens;
 
-		ebitmap_init(&bitmap);
-		ebitmap_for_each_positive_bit(&c->range.level[l].cat, node, i) {
+		ebitmap_for_each_positive_bit(&oldc->range.level[l].cat, node, i) {
 			int rc;
 
 			catdatum = hashtab_search(newp->p_cats.table,
 						  sym_name(oldp, SYM_CATS, i));
 			if (!catdatum)
 				return -EINVAL;
-			rc = ebitmap_set_bit(&bitmap, catdatum->value - 1, 1);
+			rc = ebitmap_set_bit(&newc->range.level[l].cat,
+					     catdatum->value - 1, 1);
 			if (rc)
 				return rc;
-
-			cond_resched();
 		}
-		ebitmap_destroy(&c->range.level[l].cat);
-		c->range.level[l].cat = bitmap;
 	}
 
 	return 0;
security/selinux/ss/mls.h +2 -1
index 67093647576d..7954b1e60b64 100644
@@ -46,7 +46,8 @@ int mls_range_set(struct context *context, struct mls_range *range);
 
 int mls_convert_context(struct policydb *oldp,
 			struct policydb *newp,
-			struct context *context);
+			struct context *oldc,
+			struct context *newc);
 
 int mls_compute_sid(struct policydb *p,
 		    struct context *scontext,
security/selinux/ss/services.c +35 -55

195 lines not on this page · raw message

security/selinux/ss/sidtab.c +301 -222

616 lines not on this page · raw message

security/selinux/ss/sidtab.h +53 -22

107 lines not on this page · raw message

Signature · 1 line
2.19.2
#8 lore raw

RFC PATCH v3 6/9 fix handling of invalid context in convert_context

In reply to Ondrej Mosnacek

We can reuse the temporary string 's' and save one kstrdup() call.

---
 security/selinux/ss/services.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
security/selinux/ss/services.c +8 -4
index 3c5887838e12..e70c7c5d7f26 100644
@@ -1934,15 +1934,19 @@ static int convert_context(struct context *oldc, struct context *newc, void *p)
 
 		rc = string_to_context_struct(args->newp, NULL, s,
 					      newc, SECSID_NULL);
+		if (rc == -EINVAL) {
+			/* Retain string representation for later mapping. */
+			context_init(newc);
+			newc->str = s;
+			newc->len = oldc->len;
+			rc = 0;
+			goto out;
+		}
 		kfree(s);
 		if (!rc) {
 			pr_info("SELinux:  Context %s became valid (mapped).\n",
 				oldc->str);
 			goto out;
-		} else if (rc == -EINVAL) {
-			/* Retain string representation for later mapping. */
-			rc = 0;
-			goto out;
 		} else {
 			/* Other error condition, e.g. ENOMEM. */
 			pr_err("SELinux:   Unable to map context %s, rc = %d.\n",
Signature · 1 line
2.19.2
#9 lore raw

RFC PATCH v3 7/9 remove the now unneeded 'out' label

In reply to Ondrej Mosnacek
---
 security/selinux/ss/services.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
security/selinux/ss/services.c +9 -15
index e70c7c5d7f26..2dc3189d684c 100644
@@ -1927,10 +1927,9 @@ static int convert_context(struct context *oldc, struct context *newc, void *p)
 	args = p;
 
 	if (oldc->str) {
-		rc = -ENOMEM;
 		s = kstrdup(oldc->str, GFP_KERNEL);
 		if (!s)
-			goto out;
+			return -ENOMEM;
 
 		rc = string_to_context_struct(args->newp, NULL, s,
 					      newc, SECSID_NULL);
@@ -1939,20 +1938,18 @@ static int convert_context(struct context *oldc, struct context *newc, void *p)
 			context_init(newc);
 			newc->str = s;
 			newc->len = oldc->len;
-			rc = 0;
-			goto out;
+			return 0;
 		}
 		kfree(s);
-		if (!rc) {
-			pr_info("SELinux:  Context %s became valid (mapped).\n",
-				oldc->str);
-			goto out;
-		} else {
+		if (rc) {
 			/* Other error condition, e.g. ENOMEM. */
 			pr_err("SELinux:   Unable to map context %s, rc = %d.\n",
 			       oldc->str, -rc);
-			goto out;
+			return rc;
 		}
+		pr_info("SELinux:  Context %s became valid (mapped).\n",
+			oldc->str);
+		return 0;
 	}
 
 	context_init(newc);
@@ -2015,9 +2012,7 @@ static int convert_context(struct context *oldc, struct context *newc, void *p)
 			goto bad;
 	}
 
-	rc = 0;
-out:
-	return rc;
+	return 0;
 bad:
 	/* Map old representation to string and save it. */
 	rc = context_struct_to_string(args->oldp, oldc, &s, &len);
@@ -2028,8 +2023,7 @@ bad:
 	newc->len = len;
 	pr_info("SELinux:  Context %s became invalid (unmapped).\n",
 		newc->str);
-	rc = 0;
-	goto out;
+	return 0;
 }
 
 static void security_load_policycaps(struct selinux_state *state)
Signature · 1 line
2.19.2
#10 lore raw

RFC PATCH v3 8/9 fix SIDTAB_LEAF_ENTRIES to use the correct size macro

In reply to Ondrej Mosnacek
---
 security/selinux/ss/sidtab.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
security/selinux/ss/sidtab.h +2 -2
index 292512792a70..4c46a48f8d7e 100644
@@ -36,9 +36,9 @@ union sidtab_entry_inner {
 
 #define SIDTAB_INNER_SHIFT \
 	(SIDTAB_NODE_ALLOC_SHIFT - size_to_shift(sizeof(union sidtab_entry_inner)))
-
-#define SIDTAB_LEAF_ENTRIES  (PAGE_SIZE / sizeof(struct sidtab_entry_leaf))
 #define SIDTAB_INNER_ENTRIES ((size_t)1 << SIDTAB_INNER_SHIFT)
+#define SIDTAB_LEAF_ENTRIES \
+	(SIDTAB_NODE_ALLOC_SIZE / sizeof(struct sidtab_entry_leaf))
 
 #define SIDTAB_MAX_BITS 31 /* limited to INT_MAX due to atomic_t range */
 #define SIDTAB_MAX (((u32)1 << SIDTAB_MAX_BITS) - 1)
Signature · 1 line
2.19.2
#11 lore raw

RFC PATCH v3 9/9 add back reverse lookup cache to sidtab

In reply to Ondrej Mosnacek

This patch adds a simple cache for reverse lookup to the new sidtab implementation. The cache works in a very similar way as the old implementation's cache. The only difference is that instead of storing pointers to the hash table nodes it stores just the indices of the 'cached' entries.

The new cache ensures that the indices are loaded/stored atomically, but it still has the drawback that concurrent cache updates may mess up the contents of the cache. Such situation however only reduces its effectivity, not the correctness of lookups.

---
 security/selinux/ss/sidtab.c | 49 ++++++++++++++++++++++++++++++++++--
 security/selinux/ss/sidtab.h |  5 ++++
 2 files changed, 52 insertions(+), 2 deletions(-)
security/selinux/ss/sidtab.c +47 -2
index ddea154774bb..48ed97ea2c44 100644
@@ -23,6 +23,9 @@ int sidtab_init(struct sidtab *s)
 
 	memset(s->roots, 0, sizeof(s->roots));
 
+	for (i = 0; i < SIDTAB_RCACHE_SIZE; i++)
+		atomic_set(&s->rcache[i], -1);
+
 	for (i = 0; i < SECINITSID_NUM; i++)
 		s->isids[i].set = 0;
 
@@ -204,6 +207,40 @@ static int sidtab_find_context(union sidtab_entry_inner entry,
 	return -ENOENT;
 }
 
+static void sidtab_rcache_update(struct sidtab *s, u32 index, u32 pos)
+{
+	while (pos > 0) {
+		atomic_set(&s->rcache[pos], atomic_read(&s->rcache[pos - 1]));
+		--pos;
+	}
+	atomic_set(&s->rcache[0], (int)index);
+}
+
+static void sidtab_rcache_push(struct sidtab *s, u32 index)
+{
+	sidtab_rcache_update(s, index, SIDTAB_RCACHE_SIZE - 1);
+}
+
+static int sidtab_rcache_search(struct sidtab *s, struct context *context,
+				u32 *index)
+{
+	u32 i;
+
+	for (i = 0; i < SIDTAB_RCACHE_SIZE; i++) {
+		int v = atomic_read(&s->rcache[i]);
+
+		if (v < 0)
+			continue;
+
+		if (context_cmp(sidtab_do_lookup(s, (u32)v, 0), context)) {
+			sidtab_rcache_update(s, (u32)v, i);
+			*index = (u32)v;
+			return 0;
+		}
+	}
+	return -ENOENT;
+}
+
 static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
 				 u32 *index)
 {
@@ -214,6 +251,10 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
 	struct context *dst, *dst_convert;
 	int rc;
 
+	rc = sidtab_rcache_search(s, context, index);
+	if (rc == 0)
+		return 0;
+
 	level = sidtab_level_from_count(count);
 
 	/* read entries after reading count */
@@ -222,8 +263,10 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
 	pos = 0;
 	rc = sidtab_find_context(s->roots[level], &pos, count, level,
 				 context, index);
-	if (rc == 0)
+	if (rc == 0) {
+		sidtab_rcache_push(s, *index);
 		return 0;
+	}
 
 	/* lock-free search failed: lock, re-search, and insert if not found */
 	spin_lock_irqsave(&s->lock, flags);
@@ -235,8 +278,9 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
 	/* if count has changed before we acquired the lock, then catch up */
 	while (count < count_locked) {
 		if (context_cmp(sidtab_do_lookup(s, count, 0), context)) {
-			rc = 0;
+			sidtab_rcache_push(s, count);
 			*index = count;
+			rc = 0;
 			goto out_unlock;
 		}
 		++count;
@@ -278,6 +322,7 @@ static int sidtab_reverse_lookup(struct sidtab *s, struct context *context,
 		pr_info("SELinux:  Context %s is not valid (left unmapped).\n",
 			context->str);
 
+	sidtab_rcache_push(s, count);
 	*index = count;
 
 	/* write entries before writing new count */
security/selinux/ss/sidtab.h +5 -0
index 4c46a48f8d7e..f214eafabdd2 100644
@@ -66,12 +66,17 @@ struct sidtab_convert_params {
 	struct sidtab *target;
 };
 
+#define SIDTAB_RCACHE_SIZE 4
+
 struct sidtab {
 	union sidtab_entry_inner roots[SIDTAB_MAX_LEVEL + 1];
 	atomic_t count;
 	struct sidtab_convert_params *convert;
 	spinlock_t lock;
 
+	/* reverse lookup cache */
+	atomic_t rcache[SIDTAB_RCACHE_SIZE];
+
 	/* index == SID - 1 (no entry for SECSID_NULL) */
 	struct sidtab_isid_entry isids[SECINITSID_NUM];
 };
Signature · 1 line
2.19.2