Skip to contentSkip to conversation

selinux: introduce SECURITY_SELINUX_DEBUG configuration

PATCH Applied Christian Göttsche 2 participants 9 messages Jul 18, 2023 – Jul 28, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 selinux: introduce SECURITY_SELINUX_DEBUG configuration

The policy database code contains several debug output statements related to hashtable utilization. Those are guarded by the macro DEBUG_HASHES, which is neither documented nor set anywhere.

Introduce a new Kconfig configuration guarding this and potential other future debugging related code. Disable the setting by default.

---
This already uses the de-branded naming scheme, as proposed by Stephen.

---
 security/selinux/Kconfig       | 9 +++++++++
 security/selinux/ss/policydb.c | 8 ++++----
 2 files changed, 13 insertions(+), 4 deletions(-)
security/selinux/Kconfig +9 -0
index 95a186ec0fcb..9e5be5cc6215 100644
@@ -68,3 +68,12 @@ config SECURITY_SELINUX_SID2STR_CACHE_SIZE
 	  conversion.  Setting this option to 0 disables the cache completely.
 
 	  If unsure, keep the default value.
+
+config SECURITY_SELINUX_DEBUG
+	bool "SELinux kernel debugging support"
+	depends on SECURITY_SELINUX
+	default n
+	help
+	  This enables debugging code designed to help SELinux kernel developers,
+	  unless you know what this does in the kernel code you should leave this
+	  disabled.
security/selinux/ss/policydb.c +4 -4
index cfe77ef24ee2..cc478f3f4778 100644
@@ -41,7 +41,7 @@
 #include "mls.h"
 #include "services.h"
 
-#ifdef DEBUG_HASHES
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 static const char *const symtab_name[SYM_NUM] = {
 	"common prefixes",
 	"classes",
@@ -678,7 +678,7 @@ static int (*const index_f[SYM_NUM]) (void *key, void *datum, void *datap) = {
 	cat_index,
 };
 
-#ifdef DEBUG_HASHES
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 static void hash_eval(struct hashtab *h, const char *hash_name)
 {
 	struct hashtab_info info;
@@ -701,7 +701,7 @@ static void symtab_hash_eval(struct symtab *s)
 static inline void hash_eval(struct hashtab *h, const char *hash_name)
 {
 }
-#endif
+#endif /* CONFIG_SECURITY_SELINUX_DEBUG */
 
 /*
  * Define the other val_to_name and val_to_struct arrays
@@ -725,7 +725,7 @@ static int policydb_index(struct policydb *p)
 	pr_debug("SELinux:  %d classes, %d rules\n",
 		 p->p_classes.nprim, p->te_avtab.nel);
 
-#ifdef DEBUG_HASHES
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 	avtab_hash_eval(&p->te_avtab, "rules");
 	symtab_hash_eval(p->symtab);
 #endif
Signature · 1 line
2.40.1
#1 lore raw

PATCH 2/3 selinux: move debug functions into debug configuration

avtab_hash_eval() and hashtab_stat() are only used in policydb.c when the configuration SECURITY_SELINUX_DEBUG is enabled.

Move the function declarations and definitions under that configuration as well.

---
 security/selinux/ss/avtab.c   | 2 ++
 security/selinux/ss/avtab.h   | 3 +++
 security/selinux/ss/hashtab.c | 3 ++-
 security/selinux/ss/hashtab.h | 2 ++
 4 files changed, 9 insertions(+), 1 deletion(-)
security/selinux/ss/avtab.c +2 -0
index 6766edc0fe68..1f44d277ff61 100644
@@ -354,6 +354,7 @@ int avtab_alloc_dup(struct avtab *new, const struct avtab *orig)
 	return avtab_alloc_common(new, orig->nslot);
 }
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 void avtab_hash_eval(struct avtab *h, const char *tag)
 {
 	int i, chain_len, slots_used, max_chain_len;
@@ -384,6 +385,7 @@ void avtab_hash_eval(struct avtab *h, const char *tag)
 	       tag, h->nel, slots_used, h->nslot, max_chain_len,
 	       chain2_len_sum);
 }
+#endif /* CONFIG_SECURITY_SELINUX_DEBUG */
 
 static const uint16_t spec_order[] = {
 	AVTAB_ALLOWED,
security/selinux/ss/avtab.h +3 -0
index d6742fd9c560..0ea1ee9d4dae 100644
@@ -92,7 +92,10 @@ 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);
+
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 void avtab_hash_eval(struct avtab *h, const char *tag);
+#endif
 
 struct policydb;
 int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
security/selinux/ss/hashtab.c +2 -1
index 3fb8f9026e9b..b44e62f2b1e4 100644
@@ -103,7 +103,7 @@ int hashtab_map(struct hashtab *h,
 	return 0;
 }
 
-
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
 {
 	u32 i, chain_len, slots_used, max_chain_len;
@@ -129,6 +129,7 @@ void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
 	info->slots_used = slots_used;
 	info->max_chain_len = max_chain_len;
 }
+#endif /* CONFIG_SECURITY_SELINUX_DEBUG */
 
 int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
 		int (*copy)(struct hashtab_node *new,
security/selinux/ss/hashtab.h +2 -0
index 043a773bf0b7..7836c0995871 100644
@@ -142,7 +142,9 @@ int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
 		int (*destroy)(void *k, void *d, void *args),
 		void *args);
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 /* Fill info with some hash table statistics */
 void hashtab_stat(struct hashtab *h, struct hashtab_info *info);
+#endif
 
 #endif	/* _SS_HASHTAB_H */
Signature · 1 line
2.40.1
maintainer
#2 lore raw

Re: [PATCH 2/3] selinux: move debug functions into debug configuration

Quoted 14 lines · On Jul 18, 2023 =?UTF-8?q?Christian=20G=C3=B6ttsche?= <[email protected]>…

avtab_hash_eval() and hashtab_stat() are only used in policydb.c when the configuration SECURITY_SELINUX_DEBUG is enabled.

Move the function declarations and definitions under that configuration as well.

---
 security/selinux/ss/avtab.c   | 2 ++
 security/selinux/ss/avtab.h   | 3 +++
 security/selinux/ss/hashtab.c | 3 ++-
 security/selinux/ss/hashtab.h | 2 ++
 4 files changed, 9 insertions(+), 1 deletion(-)

...

security/selinux/ss/avtab.h int avtab_alloc(struct avtab *, u32); · line 98

Quoted 12 lines
security/selinux/ss/avtab.h +3 -0
index d6742fd9c560..0ea1ee9d4dae 100644
@@ -92,7 +92,10 @@ 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);
+
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 void avtab_hash_eval(struct avtab *h, const char *tag);
+#endif

Let's do the usual dummy function here so we don't have to use the CONFIG_SECURITY_SELINUX_DEBUG more than we have to ...

#ifdef CONFIG_SECURITY_SELINUX_DEBUG
void avtab_hash_eval(struct avtab *h, const char *tag); #else
void avtab_hash_eval(struct avtab *h, const char *tag)

{
	return;
}

#endif

Please do the same for symtab_hash_eval() too so we can get rid of the #ifdef in the callers.

security/selinux/ss/hashtab.h int hashtab_duplicate(struct hashtab *new, struct hashtab *orig, · line 148

Quoted 12 lines
security/selinux/ss/hashtab.h +2 -0
index 043a773bf0b7..7836c0995871 100644
@@ -142,7 +142,9 @@ int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
 		int (*destroy)(void *k, void *d, void *args),
 		void *args);
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 /* Fill info with some hash table statistics */
 void hashtab_stat(struct hashtab *h, struct hashtab_info *info);
+#endif

Same thing here.

--
paul-moore.com

#3 lore raw

PATCH v2 1/1 selinux: move debug functions into debug configuration

In reply to Christian Göttsche

avtab_hash_eval() and hashtab_stat() are only used in policydb.c when the configuration SECURITY_SELINUX_DEBUG is enabled.

Move the function definitions under that configuration as well and provide empty definitions in case SECURITY_SELINUX_DEBUG is disabled, to avoid using #ifdef in the callers.

---
v2:
- provide empty definitions for !SECURITY_SELINUX_DEBUG
- drop #ifdef in caller

---
 security/selinux/ss/avtab.c    | 2 ++
 security/selinux/ss/avtab.h    | 7 +++++++
 security/selinux/ss/hashtab.c  | 3 ++-
 security/selinux/ss/hashtab.h  | 6 ++++++
 security/selinux/ss/policydb.c | 5 +++--
 5 files changed, 20 insertions(+), 3 deletions(-)
security/selinux/ss/avtab.c +2 -0
index 32f92da00b0e..243e5dabfa86 100644
@@ -322,6 +322,7 @@ int avtab_alloc_dup(struct avtab *new, const struct avtab *orig)
 	return avtab_alloc_common(new, orig->nslot);
 }
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 void avtab_hash_eval(struct avtab *h, const char *tag)
 {
 	int i, chain_len, slots_used, max_chain_len;
@@ -352,6 +353,7 @@ void avtab_hash_eval(struct avtab *h, const char *tag)
 	       tag, h->nel, slots_used, h->nslot, max_chain_len,
 	       chain2_len_sum);
 }
+#endif /* CONFIG_SECURITY_SELINUX_DEBUG */
 
 static const uint16_t spec_order[] = {
 	AVTAB_ALLOWED,
security/selinux/ss/avtab.h +7 -0
index 2ef5d1ae2844..3c3904bf02b0 100644
@@ -91,7 +91,14 @@ void avtab_init(struct avtab *h);
 int avtab_alloc(struct avtab *, u32);
 int avtab_alloc_dup(struct avtab *new, const struct avtab *orig);
 void avtab_destroy(struct avtab *h);
+
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 void avtab_hash_eval(struct avtab *h, const char *tag);
+#else
+static inline void avtab_hash_eval(struct avtab *h, const char *tag)
+{
+}
+#endif
 
 struct policydb;
 int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
security/selinux/ss/hashtab.c +2 -1
index 30532ec319ce..e3747b5dd3e7 100644
@@ -103,7 +103,7 @@ int hashtab_map(struct hashtab *h,
 	return 0;
 }
 
-
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
 {
 	u32 i, chain_len, slots_used, max_chain_len;
@@ -129,6 +129,7 @@ void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
 	info->slots_used = slots_used;
 	info->max_chain_len = max_chain_len;
 }
+#endif /* CONFIG_SECURITY_SELINUX_DEBUG */
 
 int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
 		int (*copy)(struct hashtab_node *new,
security/selinux/ss/hashtab.h +6 -0
index 9dac6da45b98..f9713b56d3d0 100644
@@ -142,7 +142,13 @@ int hashtab_duplicate(struct hashtab *new, struct hashtab *orig,
 		int (*destroy)(void *k, void *d, void *args),
 		void *args);
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 /* Fill info with some hash table statistics */
 void hashtab_stat(struct hashtab *h, struct hashtab_info *info);
+#else
+static inline void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
+{
+}
+#endif
 
 #endif	/* _SS_HASHTAB_H */
security/selinux/ss/policydb.c +3 -2
index dc66868ff62c..a424997c79eb 100644
@@ -701,6 +701,9 @@ static void symtab_hash_eval(struct symtab *s)
 static inline void hash_eval(struct hashtab *h, const char *hash_name)
 {
 }
+static inline void symtab_hash_eval(struct symtab *s)
+{
+}
 #endif /* CONFIG_SECURITY_SELINUX_DEBUG */
 
 /*
@@ -725,10 +728,8 @@ static int policydb_index(struct policydb *p)
 	pr_debug("SELinux:  %d classes, %d rules\n",
 		 p->p_classes.nprim, p->te_avtab.nel);
 
-#ifdef CONFIG_SECURITY_SELINUX_DEBUG
 	avtab_hash_eval(&p->te_avtab, "rules");
 	symtab_hash_eval(p->symtab);
-#endif
 
 	p->class_val_to_struct = kcalloc(p->p_classes.nprim,
 					 sizeof(*p->class_val_to_struct),
Signature · 1 line
2.40.1
maintainer Applied
#4 lore raw

Re: [PATCH v2] selinux: move debug functions into debug configuration

Quoted 13 lines · On Jul 28, 2023 =?UTF-8?q?Christian=20G=C3=B6ttsche?= <[email protected]>…

avtab_hash_eval() and hashtab_stat() are only used in policydb.c when the configuration SECURITY_SELINUX_DEBUG is enabled.

Move the function definitions under that configuration as well and provide empty definitions in case SECURITY_SELINUX_DEBUG is disabled, to avoid using #ifdef in the callers.

---
v2:
- provide empty definitions for !SECURITY_SELINUX_DEBUG
- drop #ifdef in caller

Quoted 7 lines
---
 security/selinux/ss/avtab.c    | 2 ++
 security/selinux/ss/avtab.h    | 7 +++++++
 security/selinux/ss/hashtab.c  | 3 ++-
 security/selinux/ss/hashtab.h  | 6 ++++++
 security/selinux/ss/policydb.c | 5 +++--
 5 files changed, 20 insertions(+), 3 deletions(-)

Merged into selinux/next, thanks!

--
paul-moore.com

#5 lore raw

PATCH 3/3 selinux: log missing anonclass in debug configuration

In reply to Christian Göttsche

Log under the SELinux debug configuration when a caller to the LSM hook inode_init_security_anon does not pass a anonymous inode class name. The class name allows policy writers to transition the anonymous inode into a private type via a name based type transition.

---
 security/selinux/hooks.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
security/selinux/hooks.c +11 -0
index b8a8a4f0f2ad..f6ffab9958b6 100644
@@ -2918,6 +2918,17 @@ static int selinux_inode_init_security_anon(struct inode *inode,
 	if (unlikely(!selinux_initialized()))
 		return 0;
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
+	/*
+	 * Allow policy writers to transition the anonymous inode into
+	 * a private type via a name based type transition.
+	 */
+	if (!name) {
+		pr_debug("SELinux:  no class given for anonymous inode\n");
+		dump_stack();
+	}
+#endif
+
 	isec = selinux_inode(inode);
 
 	/*
Signature · 1 line
2.40.1
maintainer
#6 lore raw

Re: [PATCH 3/3] selinux: log missing anonclass in debug configuration

Quoted 10 lines · On Jul 18, 2023 =?UTF-8?q?Christian=20G=C3=B6ttsche?= <[email protected]>…

Log under the SELinux debug configuration when a caller to the LSM hook inode_init_security_anon does not pass a anonymous inode class name. The class name allows policy writers to transition the anonymous inode into a private type via a name based type transition.

---
 security/selinux/hooks.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Is this really a problem? There are two callers in v6.5-rc2 and both properly populate the @name parameter.

Considering how easy it is to look up the callers in the kernel source and ensure they are passing a valid @name parameter I'm inclined to leave this patch unmerged.

Thoughts?

Quoted 24 lines
security/selinux/hooks.c +11 -0
index b8a8a4f0f2ad..f6ffab9958b6 100644
@@ -2918,6 +2918,17 @@ static int selinux_inode_init_security_anon(struct inode *inode,
 	if (unlikely(!selinux_initialized()))
 		return 0;
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
+	/*
+	 * Allow policy writers to transition the anonymous inode into
+	 * a private type via a name based type transition.
+	 */
+	if (!name) {
+		pr_debug("SELinux:  no class given for anonymous inode\n");
+		dump_stack();
+	}
+#endif
+
 	isec = selinux_inode(inode);
 
 	/*
Signature · 1 line
2.40.1

--
paul-moore.com

#7 lore raw

Re: [PATCH 3/3] selinux: log missing anonclass in debug configuration

Quoted 13 lines · Paul Moore
Quoted 10 lines · On Jul 18, 2023 =?UTF-8?q?Christian=20G=C3=B6ttsche?= <[email protected]>…

Log under the SELinux debug configuration when a caller to the LSM hook inode_init_security_anon does not pass a anonymous inode class name. The class name allows policy writers to transition the anonymous inode into a private type via a name based type transition.

---
 security/selinux/hooks.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
Quoted 8 lines

Is this really a problem? There are two callers in v6.5-rc2 and both properly populate the @name parameter.

Considering how easy it is to look up the callers in the kernel source and ensure they are passing a valid @name parameter I'm inclined to leave this patch unmerged.

Thoughts?

True, there are currently only two callers. The intention was to be notified if in the future new callers are added and author being unaware of the effects for SELinux and the patch not noticed by the SELinux maintainers. Also normal users/distributions will never see this message.

If you aren't convinced then feel free to disregard this patch.

Quoted 28 lines
Quoted 24 lines
security/selinux/hooks.c +11 -1
index b8a8a4f0f2ad..f6ffab9958b6 100644
@@ -2918,6 +2918,17 @@ static int selinux_inode_init_security_anon(struct inode *inode,
      if (unlikely(!selinux_initialized()))
              return 0;
 
+#ifdef CONFIG_SECURITY_SELINUX_DEBUG
+     /*
+      * Allow policy writers to transition the anonymous inode into
+      * a private type via a name based type transition.
+      */
+     if (!name) {
+             pr_debug("SELinux:  no class given for anonymous inode\n");
+             dump_stack();
+     }
+#endif
+
      isec = selinux_inode(inode);
 
      /*
--
Quoted 1 line

2.40.1

--
paul-moore.com

maintainer Applied
#8 lore raw
In reply to Christian Göttsche
Quoted 10 lines · On Jul 18, 2023 =?UTF-8?q?Christian=20G=C3=B6ttsche?= <[email protected]>…

The policy database code contains several debug output statements related to hashtable utilization. Those are guarded by the macro DEBUG_HASHES, which is neither documented nor set anywhere.

Introduce a new Kconfig configuration guarding this and potential other future debugging related code. Disable the setting by default.

Quoted 7 lines

---
This already uses the de-branded naming scheme, as proposed by Stephen.

---
 security/selinux/Kconfig       | 9 +++++++++
 security/selinux/ss/policydb.c | 8 ++++----
 2 files changed, 13 insertions(+), 4 deletions(-)

Merged into selinux/next with some minor adjustments to the help text line lengths, thanks for doing this Christian.

--
paul-moore.com