Skip to contentSkip to conversation

selinux: do not leave dangling pointer behind

PATCH Applied Christian Göttsche 2 participants 13 messages Apr 20, 2023 – May 9, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 selinux: do not leave dangling pointer behind

In case mls_context_cpy() fails due to OOM set the free'd pointer in context_cpy() to NULL to avoid it potentially being dereferenced or free'd again in future. Freeing a NULL pointer is well-defined and a hard NULL dereference crash is at least not exploitable and should give a workable stack trace.

Fixes 12b29f34558b ("selinux: support deferred mapping of contexts")
---
 security/selinux/ss/context.h | 1 +
 1 file changed, 1 insertion(+)
security/selinux/ss/context.h +1 -0
index eda32c3d4c0a..44179977f434 100644
@@ -167,6 +167,7 @@ static inline int context_cpy(struct context *dst, const struct context *src)
 	rc = mls_context_cpy(dst, src);
 	if (rc) {
 		kfree(dst->str);
+		dst->str = NULL;
 		return rc;
 	}
 	return 0;
Signature · 1 line
2.40.0
#1 lore raw

PATCH 2/6 selinux: adjust typos in comments

Found by codespell(1)

---
 security/selinux/hooks.c         | 2 +-
 security/selinux/ima.c           | 2 +-
 security/selinux/include/audit.h | 2 +-
 security/selinux/include/ima.h   | 2 +-
 security/selinux/selinuxfs.c     | 2 +-
 security/selinux/ss/services.c   | 2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)
security/selinux/hooks.c +1 -1
index 79b4890e9936..c49ce2044a17 100644
@@ -937,7 +937,7 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
 }
 
 /*
- * NOTE: the caller is resposible for freeing the memory even if on error.
+ * NOTE: the caller is responsible for freeing the memory even if on error.
  */
 static int selinux_add_opt(int token, const char *s, void **mnt_opts)
 {
security/selinux/ima.c +1 -1
index 7daf59667f59..aa34da9b0aeb 100644
@@ -4,7 +4,7 @@
  *
  * Author: Lakshmi Ramasubramanian ([email protected])
  *
- * Measure critical data structures maintainted by SELinux
+ * Measure critical data structures maintained by SELinux
  * using IMA subsystem.
  */
 #include <linux/vmalloc.h>
security/selinux/include/audit.h +1 -1
index 406bceb90c6c..d5495134a5b9 100644
@@ -41,7 +41,7 @@ void selinux_audit_rule_free(void *rule);
  *	selinux_audit_rule_match - determine if a context ID matches a rule.
  *	@sid: the context ID to check
  *	@field: the field this rule refers to
- *	@op: the operater the rule uses
+ *	@op: the operator the rule uses
  *	@rule: pointer to the audit rule to check against
  *
  *	Returns 1 if the context id matches the rule, 0 if it does not, and
security/selinux/include/ima.h +1 -1
index 05e04172c86d..93c05e97eb7f 100644
@@ -4,7 +4,7 @@
  *
  * Author: Lakshmi Ramasubramanian ([email protected])
  *
- * Measure critical data structures maintainted by SELinux
+ * Measure critical data structures maintained by SELinux
  * using IMA subsystem.
  */
 
security/selinux/selinuxfs.c +1 -1
index 69a583b91fc5..7cc7e2f0272b 100644
@@ -951,7 +951,7 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
 		 * either whitespace or multibyte characters, they shall be
 		 * encoded based on the percentage-encoding rule.
 		 * If not encoded, the sscanf logic picks up only left-half
-		 * of the supplied name; splitted by a whitespace unexpectedly.
+		 * of the supplied name; split by a whitespace unexpectedly.
 		 */
 		char   *r, *w;
 		int     c1, c2;
security/selinux/ss/services.c +1 -1
index f14d1ffe54c5..9571a447e427 100644
@@ -583,7 +583,7 @@ static void type_attribute_bounds_av(struct policydb *policydb,
 
 /*
  * flag which drivers have permissions
- * only looking for ioctl based extended permssions
+ * only looking for ioctl based extended permissions
  */
 void services_compute_xperms_drivers(
 		struct extended_perms *xperms,
Signature · 1 line
2.40.0
maintainer Applied
#2 lore raw

Re: [PATCH 2/6] selinux: adjust typos in comments

Quoted 4 lines · Christian Göttsche

Found by codespell(1)

Quoted 8 lines
---
 security/selinux/hooks.c         | 2 +-
 security/selinux/ima.c           | 2 +-
 security/selinux/include/audit.h | 2 +-
 security/selinux/include/ima.h   | 2 +-
 security/selinux/selinuxfs.c     | 2 +-
 security/selinux/ss/services.c   | 2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)

Merged into selinux/next.

Signature · 1 line
paul-moore.com
#3 lore raw

PATCH 3/6 selinux: avc: drop unused function avc_disable()

In reply to Christian Göttsche

Since commit f22f9aaf6c3d ("selinux: remove the runtime disable functionality") the function avc_disable() is no longer used.

Improves: f22f9aaf6c3d ("selinux: remove the runtime disable functionality")

---
 security/selinux/avc.c         | 19 -------------------
 security/selinux/include/avc.h |  3 ---
 2 files changed, 22 deletions(-)
security/selinux/avc.c +0 -19
index eaed5c2da02b..6bc65830e1a9 100644
@@ -1203,22 +1203,3 @@ u32 avc_policy_seqno(void)
 {
 	return selinux_avc.avc_cache.latest_notif;
 }
-
-void avc_disable(void)
-{
-	/*
-	 * If you are looking at this because you have realized that we are
-	 * not destroying the avc_node_cachep it might be easy to fix, but
-	 * I don't know the memory barrier semantics well enough to know.  It's
-	 * possible that some other task dereferenced security_ops when
-	 * it still pointed to selinux operations.  If that is the case it's
-	 * possible that it is about to use the avc and is about to need the
-	 * avc_node_cachep.  I know I could wrap the security.c security_ops call
-	 * in an rcu_lock, but seriously, it's not worth it.  Instead I just flush
-	 * the cache and get that memory back.
-	 */
-	if (avc_node_cachep) {
-		avc_flush();
-		/* kmem_cache_destroy(avc_node_cachep); */
-	}
-}
security/selinux/include/avc.h +0 -3
index 9301222c8e55..9e055f74daf6 100644
@@ -168,9 +168,6 @@ int avc_get_hash_stats(char *page);
 unsigned int avc_get_cache_threshold(void);
 void avc_set_cache_threshold(unsigned int cache_threshold);
 
-/* Attempt to free avc node cache */
-void avc_disable(void);
-
 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
 DECLARE_PER_CPU(struct avc_cache_stats, avc_cache_stats);
 #endif
Signature · 1 line
2.40.0
maintainer Applied
#4 lore raw

Re: [PATCH 3/6] selinux: avc: drop unused function avc_disable()

Quoted 10 lines · Christian Göttsche

Since commit f22f9aaf6c3d ("selinux: remove the runtime disable functionality") the function avc_disable() is no longer used.

Improves: f22f9aaf6c3d ("selinux: remove the runtime disable functionality")

---
 security/selinux/avc.c         | 19 -------------------
 security/selinux/include/avc.h |  3 ---
 2 files changed, 22 deletions(-)

Merged into selinux/next (without the Improves tag), thanks!

Signature · 1 line
paul-moore.com
#5 lore raw

PATCH 4/6 selinux: drop return at end of void function avc_insert()

In reply to Christian Göttsche

Commit 539813e4184a ("selinux: stop returning node from avc_insert()") converted the return value of avc_insert() to void but left the now unnecessary trailing return statement.

Improves: 539813e4184a ("selinux: stop returning node from avc_insert()")

---
 security/selinux/avc.c | 1 -
 1 file changed, 1 deletion(-)
security/selinux/avc.c +0 -1
index 6bc65830e1a9..1074db66e5ff 100644
@@ -642,7 +642,6 @@ static void avc_insert(u32 ssid, u32 tsid, u16 tclass,
 	hlist_add_head_rcu(&node->list, head);
 found:
 	spin_unlock_irqrestore(lock, flag);
-	return;
 }
 
 /**
Signature · 1 line
2.40.0
maintainer Applied
#6 lore raw

Re: [PATCH 4/6] selinux: drop return at end of void function avc_insert()

Quoted 10 lines · Christian Göttsche

Commit 539813e4184a ("selinux: stop returning node from avc_insert()") converted the return value of avc_insert() to void but left the now unnecessary trailing return statement.

Improves: 539813e4184a ("selinux: stop returning node from avc_insert()")

---
 security/selinux/avc.c | 1 -
 1 file changed, 1 deletion(-)

Merged into selinux/next, without the improves tag, thanks.

Signature · 1 line
paul-moore.com
#7 lore raw

PATCH 5/6 selinux: retain const qualifier on string literal in avtab_hash_eval()

In reply to Christian Göttsche

The second parameter tag of avtab_hash_eval() is only used for printing.
In policydb_index() it is called with a string literal:

    avtab_hash_eval(&p->te_avtab, "rules");
---
 security/selinux/ss/avtab.c | 2 +-
 security/selinux/ss/avtab.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
security/selinux/ss/avtab.c +1 -1
index 8480ec6c6e75..6766edc0fe68 100644
@@ -354,7 +354,7 @@ int avtab_alloc_dup(struct avtab *new, const struct avtab *orig)
 	return avtab_alloc_common(new, orig->nslot);
 }
 
-void avtab_hash_eval(struct avtab *h, char *tag)
+void avtab_hash_eval(struct avtab *h, const char *tag)
 {
 	int i, chain_len, slots_used, max_chain_len;
 	unsigned long long chain2_len_sum;
security/selinux/ss/avtab.h +1 -1
index d3ebea8d146f..d6742fd9c560 100644
@@ -92,7 +92,7 @@ int avtab_alloc(struct avtab *, u32);
 int avtab_alloc_dup(struct avtab *new, const struct avtab *orig);
 struct avtab_datum *avtab_search(struct avtab *h, const struct avtab_key *k);
 void avtab_destroy(struct avtab *h);
-void avtab_hash_eval(struct avtab *h, char *tag);
+void avtab_hash_eval(struct avtab *h, const char *tag);
 
 struct policydb;
 int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
Signature · 1 line
2.40.0
maintainer Applied
#8 lore raw

Re: [PATCH 5/6] selinux: retain const qualifier on string literal in avtab_hash_eval()

Quoted 5 lines · Christian Göttsche

The second parameter tag of avtab_hash_eval() is only used for printing.
In policydb_index() it is called with a string literal:

Quoted 7 lines
    avtab_hash_eval(&p->te_avtab, "rules");
---
 security/selinux/ss/avtab.c | 2 +-
 security/selinux/ss/avtab.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Merged into selinux/next, thanks.

Signature · 1 line
paul-moore.com
#9 lore raw

PATCH 6/6 selinux: declare read-only data arrays const

In reply to Christian Göttsche

The array of mount tokens in only used in match_opt_prefix() and never modified.

The array of symtab names is never modified and only used in the DEBUG_HASHES configuration as output.

The array of files for the SElinux filesystem sub-directory ss is similar to the other struct tree_descr usages only read from to construct the containing entries.

---
 security/selinux/hooks.c       | 2 +-
 security/selinux/selinuxfs.c   | 2 +-
 security/selinux/ss/policydb.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
security/selinux/hooks.c +1 -1
index c49ce2044a17..99ded60a6b91 100644
@@ -357,7 +357,7 @@ enum {
 };
 
 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
-static struct {
+static const struct {
 	const char *name;
 	int len;
 	int opt;
security/selinux/selinuxfs.c +1 -1
index 7cc7e2f0272b..bad1f6b685fd 100644
@@ -1649,7 +1649,7 @@ static int sel_make_ss_files(struct dentry *dir)
 	struct super_block *sb = dir->d_sb;
 	struct selinux_fs_info *fsi = sb->s_fs_info;
 	int i;
-	static struct tree_descr files[] = {
+	static const struct tree_descr files[] = {
 		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
 	};
 
security/selinux/ss/policydb.c +1 -1
index adcfb63b3550..97c0074f9312 100644
@@ -42,7 +42,7 @@
 #include "services.h"
 
 #ifdef DEBUG_HASHES
-static const char *symtab_name[SYM_NUM] = {
+static const char *const symtab_name[SYM_NUM] = {
 	"common prefixes",
 	"classes",
 	"roles",
Signature · 1 line
2.40.0
maintainer Applied
#10 lore raw

Re: [PATCH 6/6] selinux: declare read-only data arrays const

Quoted 11 lines · Christian Göttsche

The array of mount tokens in only used in match_opt_prefix() and never modified.

The array of symtab names is never modified and only used in the DEBUG_HASHES configuration as output.

The array of files for the SElinux filesystem sub-directory ss is similar to the other struct tree_descr usages only read from to construct the containing entries.

Quoted 6 lines
---
 security/selinux/hooks.c       | 2 +-
 security/selinux/selinuxfs.c   | 2 +-
 security/selinux/ss/policydb.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

Merged into selinux/next, thanks.

Signature · 1 line
paul-moore.com
maintainer Applied
#11 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

In case mls_context_cpy() fails due to OOM set the free'd pointer in context_cpy() to NULL to avoid it potentially being dereferenced or free'd again in future. Freeing a NULL pointer is well-defined and a hard NULL dereference crash is at least not exploitable and should give a workable stack trace.

Quoted 5 lines
Fixes 12b29f34558b ("selinux: support deferred mapping of contexts")
---
 security/selinux/ss/context.h | 1 +
 1 file changed, 1 insertion(+)

Merged into selinux/next.

Did you actually run into a problem where the system crashed/panic'd/etc. due to this? I'll leave the fixes tag on this since it is pretty minor, but generally I think it is best to reserve the fixes tag for problems that can be triggered as a fixes tag generally results in a stable backport, regardless of it is marked for stable or not.

Signature · 1 line
paul-moore.com
#12 lore raw
Quoted 16 lines · Paul Moore
Quoted 7 lines · Christian Göttsche

In case mls_context_cpy() fails due to OOM set the free'd pointer in context_cpy() to NULL to avoid it potentially being dereferenced or free'd again in future. Freeing a NULL pointer is well-defined and a hard NULL dereference crash is at least not exploitable and should give a workable stack trace.

Quoted 5 lines
Fixes 12b29f34558b ("selinux: support deferred mapping of contexts")
---
 security/selinux/ss/context.h | 1 +
 1 file changed, 1 insertion(+)
Quoted 4 lines

Merged into selinux/next.

Did you actually run into a problem where the system crashed/panic'd/etc. due to this?

No, no actual crash, just came to mind while looking over the code.

My understanding of the sidtab code is only superficial, so maybe Stephen can correct my analysis:

There are two callers of context_cpy(): sidtab_set_initial() and sidtab_context_to_sid():

sidtab_set_initial() is only called in policydb_load_isids(). If context_cpy() fails on &isid->entry.context isid->set is not set to 1 and thus the cleanup via sidtab_destroy() called in policydb_load_isids() causes no use-after-free, since sidtab_destroy() calls sidtab_destroy_entry() only on if (s->isids[i].set).

In sidtab_context_to_sid(), if context_cpy() failed, the sidtab count remains unchanged, since smp_store_release(&s->count, count + 1); is skipped. So simple lookups via sidtab_lookup() should not access the zombie context since the count is checked. But I think sidtab_destroy(), e.g via a policy load on the old policy, unconditionally calls, via sidtab_destroy_tree(), sidtab_destroy_entry() on all fixed sized array elements, which leads to a call of context_destroy() leading to a double-free.

Even if this analysis is wrong the change seems to me a good defence-in-depth measure, e.g. for future refactorings.
Also the cost of a pointer assigmnment in an OOM error branch seems negligible.

I'll probably send a patch to set dst->len to 0 as well, since both members are coupled.

Quoted 8 lines

I'll leave the fixes tag on this
since it is pretty minor, but generally I think it is best to reserve the fixes tag for problems that can be triggered as a fixes tag generally results in a stable backport, regardless of it is marked for stable or not.

--
paul-moore.com