Skip to contentSkip to conversation

selinux: drop event filtering from AVC callbacks

RFC PATCH In review Christian Göttsche 2 participants 64 messages Sep 28 – Sep 29 SELinux security module

lore
Share & tools
lore
#0 lore raw

RFC PATCH 2/32 selinux: drop event filtering from AVC callbacks

From: Christian Göttsche <[email protected]>

All AVC callbacks are registered for AVC_CALLBACK_RESET only, and it is the only event ever dispatched. Drop the events mask, the event argument and the redundant event checks in the callbacks.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/avc.c           | 23 +++++++++--------------
 security/selinux/hooks.c         | 28 +++++++++++-----------------
 security/selinux/include/audit.h |  7 +++----
 security/selinux/include/avc.h   |  4 +---
 security/selinux/ss/services.c   |  6 ++----
 5 files changed, 26 insertions(+), 42 deletions(-)
security/selinux/avc.c +9 -14
index 936e116b01fb..9edbc324121e 100644
@@ -78,8 +78,7 @@ struct avc_cache {
 };
 
 struct avc_callback_node {
-	int (*callback) (u32 event);
-	u32 events;
+	int (*callback)(void);
 	struct avc_callback_node *next;
 };
 
@@ -785,15 +784,14 @@ noinline int slow_avc_audit(u32 ssid, u32 tsid, u16 tclass,
 }
 
 /**
- * avc_add_callback - Register a callback for security events.
+ * avc_add_callback - Register a callback for AVC resets.
  * @callback: callback function
- * @events: security events
  *
- * Register a callback function for events in the set @events.
+ * Register a callback function to be called on every AVC reset.
  * Returns %0 on success or -%ENOMEM if insufficient memory
  * exists to add the callback.
  */
-int __init avc_add_callback(int (*callback)(u32 event), u32 events)
+int __init avc_add_callback(int (*callback)(void))
 {
 	struct avc_callback_node *c;
 	int rc = 0;
@@ -805,7 +803,6 @@ int __init avc_add_callback(int (*callback)(u32 event), u32 events)
 	}
 
 	c->callback = callback;
-	c->events = events;
 	c->next = avc_callbacks;
 	avc_callbacks = c;
 out:
@@ -953,13 +950,11 @@ int avc_ss_reset(u32 seqno)
 	avc_flush();
 
 	for (c = avc_callbacks; c; c = c->next) {
-		if (c->events & AVC_CALLBACK_RESET) {
-			tmprc = c->callback(AVC_CALLBACK_RESET);
-			/* save the first error encountered for the return
-			   value and continue processing the callbacks */
-			if (!rc)
-				rc = tmprc;
-		}
+		tmprc = c->callback();
+		/* save the first error encountered for the return
+		   value and continue processing the callbacks */
+		if (!rc)
+			rc = tmprc;
 	}
 
 	avc_latest_notif_update(seqno, 0);
security/selinux/hooks.c +11 -17
index 035aaf113d1d..9444c4d0b901 100644
@@ -188,24 +188,19 @@ static int selinux_peerlbl_enabled(void)
 		netlbl_enabled() || selinux_xfrm_enabled());
 }
 
-static int selinux_netcache_avc_callback(u32 event)
+static int selinux_netcache_avc_callback(void)
 {
-	if (event == AVC_CALLBACK_RESET) {
-		sel_netif_flush();
-		sel_netnode_flush();
-		sel_netport_flush();
-		synchronize_net();
-	}
+	sel_netif_flush();
+	sel_netnode_flush();
+	sel_netport_flush();
+	synchronize_net();
 	return 0;
 }
 
-static int selinux_lsm_notifier_avc_callback(u32 event)
+static int selinux_lsm_notifier_avc_callback(void)
 {
-	if (event == AVC_CALLBACK_RESET) {
-		sel_ib_pkey_flush();
-		call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
-	}
-
+	sel_ib_pkey_flush();
+	call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
 	return 0;
 }
 
@@ -7886,14 +7881,13 @@ static __init int selinux_init(void)
 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks),
 			   &selinux_lsmid);
 
-	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
+	if (avc_add_callback(selinux_netcache_avc_callback))
 		panic("SELinux: Unable to register AVC netcache callback\n");
 
-	if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
+	if (avc_add_callback(selinux_lsm_notifier_avc_callback))
 		panic("SELinux: Unable to register AVC LSM notifier callback\n");
 
-	if (avc_add_callback(selinux_audit_rule_avc_callback,
-			     AVC_CALLBACK_RESET))
+	if (avc_add_callback(selinux_audit_rule_avc_callback))
 		panic("SELinux: Unable to register AVC audit callback\n");
 
 	if (selinux_enforcing_boot)
security/selinux/include/audit.h +3 -4
index 85a531ac737b..3103a5784ee2 100644
@@ -16,13 +16,12 @@
 #include <linux/types.h>
 
 /**
- * selinux_audit_rule_avc_callback - update the audit LSM rules on AVC events.
- * @event: the AVC event
+ * selinux_audit_rule_avc_callback - update the audit LSM rules on AVC reset.
  *
- * Update any audit LSM rules based on the AVC event specified in @event.
+ * Update any audit LSM rules after the AVC has been reset.
  * Returns 0 on success, negative values otherwise.
  */
-int selinux_audit_rule_avc_callback(u32 event);
+int selinux_audit_rule_avc_callback(void);
 
 /**
  * selinux_audit_rule_init - alloc/init an selinux audit rule structure.
security/selinux/include/avc.h +1 -3
index 0f788dfc66e9..8cfc4255be36 100644
@@ -149,9 +149,7 @@ int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,
 
 u32 avc_policy_seqno(void);
 
-#define AVC_CALLBACK_RESET		8
-
-int avc_add_callback(int (*callback)(u32 event), u32 events);
+int avc_add_callback(int (*callback)(void));
 
 /* Exported to selinuxfs */
 int avc_get_hash_stats(char *page);
security/selinux/ss/services.c +2 -4
index da77ad769c83..3cfe9c7b28c6 100644
@@ -3462,11 +3462,9 @@ struct selinux_audit_rule {
 	struct context au_ctxt;
 };
 
-int selinux_audit_rule_avc_callback(u32 event)
+int selinux_audit_rule_avc_callback(void)
 {
-	if (event == AVC_CALLBACK_RESET)
-		return audit_update_lsm_rules();
-	return 0;
+	return audit_update_lsm_rules();
 }
 
 void selinux_audit_rule_free(void *vrule)
Signature · 1 line
2.55.0
#1 lore raw

RFC PATCH 1/32 selinux: remove unused AVC update events

From: Christian Göttsche <[email protected]>

avc_update_node() is only ever called with AVC_CALLBACK_GRANT or AVC_CALLBACK_ADD_XPERMS. Drop the unreachable revoke and audit toggle cases and their now unused event macros.

Convert the remaining update events, which are only used within avc.c, into a local enum.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/avc.c         | 26 ++++++++------------------
 security/selinux/include/avc.h |  8 --------
 2 files changed, 8 insertions(+), 26 deletions(-)
security/selinux/avc.c +8 -18
index dd8905648b6a..936e116b01fb 100644
@@ -812,6 +812,11 @@ int __init avc_add_callback(int (*callback)(u32 event), u32 events)
 	return rc;
 }
 
+enum avc_update_event {
+	AVC_CALLBACK_GRANT,
+	AVC_CALLBACK_ADD_XPERMS,
+};
+
 /**
  * avc_update_node - Update an AVC entry
  * @event : Updating event
@@ -831,8 +836,9 @@ int __init avc_add_callback(int (*callback)(u32 event), u32 events)
  * otherwise, this function updates the AVC entry. The original AVC-entry object
  * will release later by RCU.
  */
-static int avc_update_node(u32 event, u32 perms, u8 driver, u8 base_perm,
-			   u8 xperm, u32 ssid, u32 tsid, u16 tclass, u32 seqno,
+static int avc_update_node(enum avc_update_event event, u32 perms, u8 driver,
+			   u8 base_perm, u8 xperm, u32 ssid, u32 tsid,
+			   u16 tclass, u32 seqno,
 			   const struct extended_perms_decision *xpd, u32 flags)
 {
 	u32 hvalue;
@@ -892,22 +898,6 @@ static int avc_update_node(u32 event, u32 perms, u8 driver, u8 base_perm,
 		if (node->ae.xp_node && (flags & AVC_EXTENDED_PERMS))
 			avc_xperms_allow_perm(node->ae.xp_node, driver, base_perm, xperm);
 		break;
-	case AVC_CALLBACK_TRY_REVOKE:
-	case AVC_CALLBACK_REVOKE:
-		node->ae.avd.allowed &= ~perms;
-		break;
-	case AVC_CALLBACK_AUDITALLOW_ENABLE:
-		node->ae.avd.auditallow |= perms;
-		break;
-	case AVC_CALLBACK_AUDITALLOW_DISABLE:
-		node->ae.avd.auditallow &= ~perms;
-		break;
-	case AVC_CALLBACK_AUDITDENY_ENABLE:
-		node->ae.avd.auditdeny |= perms;
-		break;
-	case AVC_CALLBACK_AUDITDENY_DISABLE:
-		node->ae.avd.auditdeny &= ~perms;
-		break;
 	case AVC_CALLBACK_ADD_XPERMS:
 		rc = avc_add_xperms_decision(node, xpd);
 		if (rc) {
security/selinux/include/avc.h +0 -8
index 87027a934a11..0f788dfc66e9 100644
@@ -149,15 +149,7 @@ int avc_has_extended_perms(u32 ssid, u32 tsid, u16 tclass, u32 requested,
 
 u32 avc_policy_seqno(void);
 
-#define AVC_CALLBACK_GRANT		1
-#define AVC_CALLBACK_TRY_REVOKE		2
-#define AVC_CALLBACK_REVOKE		4
 #define AVC_CALLBACK_RESET		8
-#define AVC_CALLBACK_AUDITALLOW_ENABLE	16
-#define AVC_CALLBACK_AUDITALLOW_DISABLE 32
-#define AVC_CALLBACK_AUDITDENY_ENABLE	64
-#define AVC_CALLBACK_AUDITDENY_DISABLE	128
-#define AVC_CALLBACK_ADD_XPERMS		256
 
 int avc_add_callback(int (*callback)(u32 event), u32 events);
 
Signature · 1 line
2.55.0
maintainer Acked-by on v1 1/32
#2 lore raw

Re: [RFC PATCH 01/32] selinux: remove unused AVC update events

Quoted 7 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

avc_update_node() is only ever called with AVC_CALLBACK_GRANT or AVC_CALLBACK_ADD_XPERMS. Drop the unreachable revoke and audit toggle cases and their now unused event macros.

Quoted 5 lines

Convert the remaining update events, which are only used within avc.c, into a local enum.

Assisted-by Claude:claude-opus-5-5

It makes me a little sad to see the lingering remnants of the original Flask architecture go, but I agree they are unused and we have no plans to use them presently.

#3 lore raw

RFC PATCH 3/32 selinux: use kfree_strarray()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Replace open-coded loops freeing an array of strings with the existing kfree_strarray() helper, which also handles a NULL array.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c   | 21 +++++----------------
 security/selinux/ss/services.c | 22 +++++-----------------
 2 files changed, 10 insertions(+), 33 deletions(-)
security/selinux/selinuxfs.c +5 -16
index b3eaf3823fb1..ca821a812691 100644
@@ -24,6 +24,7 @@
 #include <linux/namei.h>
 #include <linux/init.h>
 #include <linux/string.h>
+#include <linux/string_helpers.h>
 #include <linux/security.h>
 #include <linux/major.h>
 #include <linux/seq_file.h>
@@ -96,12 +97,8 @@ static int selinux_fs_info_create(struct super_block *sb)
 
 static void selinux_fs_info_free(struct selinux_fs_info *fsi)
 {
-	unsigned int i;
-
 	if (fsi) {
-		for (i = 0; i < fsi->bool_num; i++)
-			kfree(fsi->bool_pending_names[i]);
-		kfree(fsi->bool_pending_names);
+		kfree_strarray(fsi->bool_pending_names, fsi->bool_num);
 		kfree(fsi->bool_pending_values);
 	}
 	kfree(fsi);
@@ -443,12 +440,8 @@ static const struct file_operations sel_policy_ops = {
 static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names,
 				     int *bool_values)
 {
-	u32 i;
-
 	/* bool_dir cleanup */
-	for (i = 0; i < bool_num; i++)
-		kfree(bool_names[i]);
-	kfree(bool_names);
+	kfree_strarray(bool_names, bool_num);
 	kfree(bool_values);
 }
 
@@ -1658,9 +1651,7 @@ static int sel_make_perm_files(struct selinux_policy *newpolicy,
 
 		rc = sel_attach_file(dir, perms[i], inode);
 	}
-	for (i = 0; i < nperms; i++)
-		kfree(perms[i]);
-	kfree(perms);
+	kfree_strarray(perms, nperms);
 	return rc;
 }
 
@@ -1725,9 +1716,7 @@ static int sel_make_classes(struct selinux_policy *newpolicy,
 	}
 	rc = 0;
 out:
-	for (i = 0; i < nclasses; i++)
-		kfree(classes[i]);
-	kfree(classes);
+	kfree_strarray(classes, nclasses);
 	return rc;
 }
 
security/selinux/ss/services.c +5 -17
index 3cfe9c7b28c6..ce9f9c483f49 100644
@@ -40,6 +40,7 @@
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/string_helpers.h>
 #include <linux/spinlock.h>
 #include <linux/rcupdate.h>
 #include <linux/errno.h>
@@ -2960,11 +2961,7 @@ int security_get_bools(struct selinux_policy *policy,
 out:
 	return rc;
 err:
-	if (*names) {
-		for (i = 0; i < *len; i++)
-			kfree((*names)[i]);
-		kfree(*names);
-	}
+	kfree_strarray(*names, *len);
 	kfree(*values);
 	*len = 0;
 	*names = NULL;
@@ -3090,11 +3087,7 @@ static int security_preserve_bools(struct selinux_policy *oldpolicy,
 	evaluate_cond_nodes(&newpolicy->policydb);
 
 out:
-	if (bnames) {
-		for (i = 0; i < nbools; i++)
-			kfree(bnames[i]);
-	}
-	kfree(bnames);
+	kfree_strarray(bnames, nbools);
 	kfree(bvalues);
 	return rc;
 }
@@ -3336,9 +3329,7 @@ int security_get_classes(struct selinux_policy *policy,
 	return rc;
 
 err:
-	for (i = 0; i < *nclasses; i++)
-		kfree((*classes)[i]);
-	kfree(*classes);
+	kfree_strarray(*classes, *nclasses);
 	return rc;
 }
 
@@ -3359,7 +3350,6 @@ int security_get_permissions(struct selinux_policy *policy,
 			     const char *class, char ***perms, u32 *nperms)
 {
 	struct policydb *policydb;
-	u32 i;
 	int rc;
 	struct class_datum *match;
 
@@ -3395,9 +3385,7 @@ int security_get_permissions(struct selinux_policy *policy,
 	return rc;
 
 err:
-	for (i = 0; i < *nperms; i++)
-		kfree((*perms)[i]);
-	kfree(*perms);
+	kfree_strarray(*perms, *nperms);
 	return rc;
 }
 
Signature · 1 line
2.55.0
#5 lore raw

RFC PATCH 4/32 selinux: use vmemdup_user() for policy loading

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Replace the open-coded vmalloc() + copy_from_user() in sel_write_load() with vmemdup_user().

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
security/selinux/selinuxfs.c +4 -9
index ca821a812691..42cfef5fcca2 100644
@@ -543,13 +543,9 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
 	if (length)
 		return length;
 
-	data = vmalloc(count);
-	if (!data)
-		return -ENOMEM;
-	if (copy_from_user(data, buf, count) != 0) {
-		length = -EFAULT;
-		goto out;
-	}
+	data = vmemdup_user(buf, count);
+	if (IS_ERR(data))
+		return PTR_ERR(data);
 
 	mutex_lock(&selinux_state.policy_mutex);
 	length = security_load_policy(data, count, &load_state);
@@ -574,8 +570,7 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
 
 out_unlock:
 	mutex_unlock(&selinux_state.policy_mutex);
-out:
-	vfree(data);
+	kvfree(data);
 	return length;
 }
 
Signature · 1 line
2.55.0
#7 lore raw

RFC PATCH 5/32 selinux: use kmemdup_array() for conditional expressions

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Use kmemdup_array() instead of an open-coded, unchecked size multiplication when duplicating conditional expression nodes.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/conditional.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/ss/conditional.c +4 -4
index 2956dd7edea7..b517e31959f5 100644
@@ -655,10 +655,10 @@ static int duplicate_policydb_cond_list(struct policydb *newp,
 		newp->cond_list_len++;
 
 		newn->cur_state = orign->cur_state;
-		newn->expr.nodes =
-			kmemdup(orign->expr.nodes,
-				orign->expr.len * sizeof(*orign->expr.nodes),
-				GFP_KERNEL);
+		newn->expr.nodes = kmemdup_array(orign->expr.nodes,
+						 orign->expr.len,
+						 sizeof(*orign->expr.nodes),
+						 GFP_KERNEL);
 		if (!newn->expr.nodes)
 			goto error;
 
Signature · 1 line
2.55.0
#9 lore raw

RFC PATCH 6/32 selinux: use memcpy_{from,to}_le32() in avtab

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Replace the open-coded endianness conversion loops for extended permissions with the generic helpers.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/avtab.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
security/selinux/ss/avtab.c +4 -5
index 0f94edd01f69..64f9da168f1e 100644
@@ -479,8 +479,8 @@ int avtab_read_item(struct avtab *a, struct policy_file *fp, struct policydb *po
 			pr_err("SELinux: avtab: truncated entry\n");
 			return rc;
 		}
-		for (i = 0; i < ARRAY_SIZE(xperms.perms.p); i++)
-			xperms.perms.p[i] = le32_to_cpu(buf32[i]);
+		memcpy_from_le32(xperms.perms.p, buf32,
+				 ARRAY_SIZE(xperms.perms.p));
 		datum.u.xperms = &xperms;
 	} else {
 		rc = next_entry(buf32, fp, sizeof(u32));
@@ -557,7 +557,6 @@ int avtab_write_item(struct policydb *p, const struct avtab_node *cur, struct po
 	__le16 buf16[4];
 	__le32 buf32[ARRAY_SIZE(cur->datum.u.xperms->perms.p)];
 	int rc;
-	unsigned int i;
 
 	buf16[0] = cpu_to_le16(cur->key.source_type);
 	buf16[1] = cpu_to_le16(cur->key.target_type);
@@ -575,8 +574,8 @@ int avtab_write_item(struct policydb *p, const struct avtab_node *cur, struct po
 		rc = put_entry(&cur->datum.u.xperms->driver, sizeof(u8), 1, fp);
 		if (rc)
 			return rc;
-		for (i = 0; i < ARRAY_SIZE(cur->datum.u.xperms->perms.p); i++)
-			buf32[i] = cpu_to_le32(cur->datum.u.xperms->perms.p[i]);
+		memcpy_to_le32(buf32, cur->datum.u.xperms->perms.p,
+			       ARRAY_SIZE(cur->datum.u.xperms->perms.p));
 		rc = put_entry(buf32, sizeof(u32),
 			       ARRAY_SIZE(cur->datum.u.xperms->perms.p), fp);
 	} else {
Signature · 1 line
2.55.0
#11 lore raw

RFC PATCH 7/32 selinux: drop constant value parameter of ebitmap_set_bit()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

All callers of ebitmap_set_bit() pass 1 as value. Drop the parameter together with the unused bit clearing branch and ebitmap_node_clr_bit().

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/ebitmap.c  | 37 +++-------------------------------
 security/selinux/ss/ebitmap.h  | 11 +---------
 security/selinux/ss/mls.c      |  6 +++---
 security/selinux/ss/policydb.c |  8 ++++----
 4 files changed, 11 insertions(+), 51 deletions(-)
security/selinux/ss/ebitmap.c +3 -34
index ea894361fed6..fd489d8cfbed 100644
@@ -87,7 +87,7 @@ int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1,
 	ebitmap_for_each_positive_bit(e1, n, bit)
 	{
 		if (ebitmap_get_bit(e2, bit)) {
-			rc = ebitmap_set_bit(dst, bit, 1);
+			rc = ebitmap_set_bit(dst, bit);
 			if (rc < 0)
 				return rc;
 		}
@@ -301,7 +301,7 @@ int ebitmap_get_bit(const struct ebitmap *e, u32 bit)
 	return 0;
 }
 
-int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value)
+int ebitmap_set_bit(struct ebitmap *e, u32 bit)
 {
 	struct ebitmap_node *n, *prev, *new;
 
@@ -309,44 +309,13 @@ int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value)
 	n = e->node;
 	while (n && n->startbit <= bit) {
 		if ((n->startbit + EBITMAP_SIZE) > bit) {
-			if (value) {
-				ebitmap_node_set_bit(n, bit);
-			} else {
-				u32 s;
-
-				ebitmap_node_clr_bit(n, bit);
-
-				s = find_first_bit(n->maps, EBITMAP_SIZE);
-				if (s < EBITMAP_SIZE)
-					return 0;
-
-				/* drop this node from the bitmap */
-				if (!n->next) {
-					/*
-					 * this was the highest map
-					 * within the bitmap
-					 */
-					if (prev)
-						e->highbit = prev->startbit +
-							     EBITMAP_SIZE;
-					else
-						e->highbit = 0;
-				}
-				if (prev)
-					prev->next = n->next;
-				else
-					e->node = n->next;
-				kmem_cache_free(ebitmap_node_cachep, n);
-			}
+			ebitmap_node_set_bit(n, bit);
 			return 0;
 		}
 		prev = n;
 		n = n->next;
 	}
 
-	if (!value)
-		return 0;
-
 	new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
 	if (!new)
 		return -ENOMEM;
security/selinux/ss/ebitmap.h +1 -10
index ae05cc8e4672..9b14f2304620 100644
@@ -106,15 +106,6 @@ static inline void ebitmap_node_set_bit(struct ebitmap_node *n, u32 bit)
 	n->maps[index] |= (EBITMAP_BIT << ofs);
 }
 
-static inline void ebitmap_node_clr_bit(struct ebitmap_node *n, u32 bit)
-{
-	u32 index = EBITMAP_NODE_INDEX(n, bit);
-	u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
-
-	BUG_ON(index >= EBITMAP_UNIT_NUMS);
-	n->maps[index] &= ~(EBITMAP_BIT << ofs);
-}
-
 #define ebitmap_for_each_positive_bit(e, n, bit)      \
 	for ((bit) = ebitmap_start_positive(e, &(n)); \
 	     (bit) < ebitmap_length(e);               \
@@ -128,7 +119,7 @@ int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2,
 		     u32 last_e2bit);
 u32 ebitmap_get_highest_set_bit(const struct ebitmap *e);
 int ebitmap_get_bit(const struct ebitmap *e, u32 bit);
-int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value);
+int ebitmap_set_bit(struct ebitmap *e, u32 bit);
 void ebitmap_destroy(struct ebitmap *e);
 struct policy_file;
 int ebitmap_read(struct ebitmap *e, struct policy_file *fp);
security/selinux/ss/mls.c +3 -3
index b48f8256dd4c..3b2f888506af 100644
@@ -318,7 +318,7 @@ int mls_context_to_sid(struct policydb *pol, char oldc, char *scontext,
 				return -EINVAL;
 
 			rc = ebitmap_set_bit(&context->range.level[l].cat,
-					     catdatum->value - 1, 1);
+					     catdatum->value - 1);
 			if (rc)
 				return rc;
 
@@ -335,7 +335,7 @@ int mls_context_to_sid(struct policydb *pol, char oldc, char *scontext,
 
 			for (i = catdatum->value; i < rngdatum->value; i++) {
 				rc = ebitmap_set_bit(
-					&context->range.level[l].cat, i, 1);
+					&context->range.level[l].cat, i);
 				if (rc)
 					return rc;
 			}
@@ -438,7 +438,7 @@ int mls_convert_context(struct policydb *oldp, struct policydb *newp,
 			if (!catdatum)
 				return -EINVAL;
 			rc = ebitmap_set_bit(&newc->range.level[l].cat,
-					     catdatum->value - 1, 1);
+					     catdatum->value - 1);
 			if (rc)
 				return rc;
 		}
security/selinux/ss/policydb.c +4 -4
index 13d88e47db3c..cad404fc8819 100644
@@ -2292,13 +2292,13 @@ static int filename_trans_read_helper_compat(struct policydb *p, struct policy_f
 			name = NULL;
 
 			rc = ebitmap_set_bit(&p->filename_trans_ttypes,
-					     key.ttype, 1);
+					     key.ttype);
 			if (rc)
 				return rc;
 		}
 	}
 	kfree(name);
-	return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
+	return ebitmap_set_bit(&datum->stypes, stype - 1);
 
 out:
 	kfree(ft);
@@ -2400,7 +2400,7 @@ static int filename_trans_read_helper(struct policydb *p, struct policy_file *fp
 	if (rc)
 		goto out;
 
-	return ebitmap_set_bit(&p->filename_trans_ttypes, ttype, 1);
+	return ebitmap_set_bit(&p->filename_trans_ttypes, ttype);
 
 out:
 	kfree(ft);
@@ -3122,7 +3122,7 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
 			goto bad;
 
 		/* add the type itself as the degenerate case */
-		rc = ebitmap_set_bit(e, i, 1);
+		rc = ebitmap_set_bit(e, i);
 		if (rc)
 			goto bad;
 	}
Signature · 1 line
2.55.0
maintainer Acked-by on v1 7/32
#12 lore raw

Re: [RFC PATCH 07/32] selinux: drop constant value parameter of ebitmap_set_bit()

Quoted 9 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

All callers of ebitmap_set_bit() pass 1 as value. Drop the parameter together with the unused bit clearing branch and ebitmap_node_clr_bit().

Assisted-by Claude:claude-opus-5-5
#13 lore raw

RFC PATCH 8/32 selinux: use rounddown() and roundup() in ebitmap

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Use the generic rounding helpers instead of open-coding them, as ebitmap_write() already does. EBITMAP_SIZE is not necessarily a power of two, so the non power-of-two variants are required.

This also changes ebitmap_read() near the u32 highbit limit on 64-bit systems. EBITMAP_SIZE has type size_t, so the addition is no longer truncated to u32 before rounding down. For example, a bitmap with highbit 0xffffff00 and a nonzero map at startbit 0xfffffec0 is now read normally instead of being accepted as empty without consuming its node. Bitmaps whose rounded end exceeds u32 now fail validation instead of being treated as empty. The 32-bit arithmetic is unchanged.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/ebitmap.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
security/selinux/ss/ebitmap.c +4 -6
index fd489d8cfbed..f2e67aa5d9cf 100644
@@ -185,7 +185,7 @@ int ebitmap_netlbl_import(struct ebitmap *ebmap,
 						   GFP_ATOMIC);
 			if (e_iter == NULL)
 				goto netlbl_import_failure;
-			e_iter->startbit = offset - (offset % EBITMAP_SIZE);
+			e_iter->startbit = rounddown(offset, EBITMAP_SIZE);
 			if (e_prev == NULL)
 				ebmap->node = e_iter;
 			else
@@ -320,7 +320,7 @@ int ebitmap_set_bit(struct ebitmap *e, u32 bit)
 	if (!new)
 		return -ENOMEM;
 
-	new->startbit = bit - (bit % EBITMAP_SIZE);
+	new->startbit = rounddown(bit, EBITMAP_SIZE);
 	ebitmap_node_set_bit(new, bit);
 
 	if (!n)
@@ -383,9 +383,7 @@ int ebitmap_read(struct ebitmap *e, struct policy_file *fp)
 		goto bad;
 	}
 
-	/* round up e->highbit */
-	e->highbit += EBITMAP_SIZE - 1;
-	e->highbit -= (e->highbit % EBITMAP_SIZE);
+	e->highbit = roundup(e->highbit, EBITMAP_SIZE);
 
 	if (!e->highbit) {
 		e->node = NULL;
@@ -426,7 +424,7 @@ int ebitmap_read(struct ebitmap *e, struct policy_file *fp)
 				goto bad;
 			}
 			/* round down */
-			tmp->startbit = startbit - (startbit % EBITMAP_SIZE);
+			tmp->startbit = rounddown(startbit, EBITMAP_SIZE);
 			if (n)
 				n->next = tmp;
 			else
Signature · 1 line
2.55.0
maintainer Acked-by on v1 8/32
#14 lore raw

Re: [RFC PATCH 08/32] selinux: use rounddown() and roundup() in ebitmap

Quoted 15 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use the generic rounding helpers instead of open-coding them, as ebitmap_write() already does. EBITMAP_SIZE is not necessarily a power of two, so the non power-of-two variants are required.

This also changes ebitmap_read() near the u32 highbit limit on 64-bit systems. EBITMAP_SIZE has type size_t, so the addition is no longer truncated to u32 before rounding down. For example, a bitmap with highbit 0xffffff00 and a nonzero map at startbit 0xfffffec0 is now read normally instead of being accepted as empty without consuming its node. Bitmaps whose rounded end exceeds u32 now fail validation instead of being treated as empty. The 32-bit arithmetic is unchanged.

Quoted 2 lines
Assisted-by Claude:claude-opus-5-5
#15 lore raw

RFC PATCH 9/32 selinux: factor out sel_make_files()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_make_avc_files() and sel_make_ss_files() contain the same loop to create the files of a tree_descr table. Move it into a common helper.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 49 ++++++++++++++----------------------
 1 file changed, 19 insertions(+), 30 deletions(-)
security/selinux/selinuxfs.c +19 -30
index 42cfef5fcca2..0fce6c0b0426 100644
@@ -1447,22 +1447,14 @@ static const struct file_operations sel_avc_cache_stats_ops = {
 };
 #endif
 
-static int sel_make_avc_files(struct dentry *dir)
+static int sel_make_files(struct dentry *dir, const struct tree_descr *files,
+			  unsigned int nfiles)
 {
-	struct super_block *sb = dir->d_sb;
-	struct selinux_fs_info *fsi = sb->s_fs_info;
+	struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
 	unsigned int i;
 	int err = 0;
-	static const struct tree_descr files[] = {
-		{ "cache_threshold",
-		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
-		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
-#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
-		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
-#endif
-	};
 
-	for (i = 0; !err && i < ARRAY_SIZE(files); i++) {
+	for (i = 0; !err && i < nfiles; i++) {
 		struct inode *inode;
 
 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
@@ -1478,30 +1470,27 @@ static int sel_make_avc_files(struct dentry *dir)
 	return err;
 }
 
-static int sel_make_ss_files(struct dentry *dir)
+static int sel_make_avc_files(struct dentry *dir)
 {
-	struct super_block *sb = dir->d_sb;
-	struct selinux_fs_info *fsi = sb->s_fs_info;
-	unsigned int i;
-	int err = 0;
 	static const struct tree_descr files[] = {
-		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
+		{ "cache_threshold",
+		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
+		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
+#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
+		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
+#endif
 	};
 
-	for (i = 0; !err && i < ARRAY_SIZE(files); i++) {
-		struct inode *inode;
-
-		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
-		if (!inode)
-			return -ENOMEM;
-
-		inode->i_fop = files[i].ops;
-		inode->i_ino = ++fsi->last_ino;
+	return sel_make_files(dir, files, ARRAY_SIZE(files));
+}
 
-		err = sel_attach_file(dir, files[i].name, inode);
-	}
+static int sel_make_ss_files(struct dentry *dir)
+{
+	static const struct tree_descr files[] = {
+		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
+	};
 
-	return err;
+	return sel_make_files(dir, files, ARRAY_SIZE(files));
 }
 
 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
Signature · 1 line
2.55.0
#17 lore raw

RFC PATCH 10/32 selinux: use struct_size() and isdigit()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Use struct_size() for the size of the flexible xfrm security context struct, matching its allocation, and isdigit() instead of an open-coded range check.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/hooks.c | 3 ++-
 security/selinux/xfrm.c  | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
security/selinux/hooks.c +2 -1
index 9444c4d0b901..0c4511325ce3 100644
@@ -31,6 +31,7 @@
 #include <linux/lsm_hooks.h>
 #include <linux/xattr.h>
 #include <linux/capability.h>
+#include <linux/ctype.h>
 #include <linux/unistd.h>
 #include <linux/mm.h>
 #include <linux/mman.h>
@@ -1345,7 +1346,7 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 			/* each process gets a /proc/PID/ entry. Strip off the
 			 * PID part to get a valid selinux labeling.
 			 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
-			while (path[1] >= '0' && path[1] <= '9') {
+			while (isdigit(path[1])) {
 				path[1] = '/';
 				path++;
 			}
security/selinux/xfrm.c +2 -1
index 8e00b3306574..946846edb566 100644
@@ -295,7 +295,8 @@ int selinux_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
 	if (!old_ctx)
 		return 0;
 
-	new_ctx = kmemdup(old_ctx, sizeof(*old_ctx) + old_ctx->ctx_len,
+	new_ctx = kmemdup(old_ctx,
+			  struct_size(old_ctx, ctx_str, old_ctx->ctx_len),
 			  GFP_ATOMIC);
 	if (!new_ctx)
 		return -ENOMEM;
Signature · 1 line
2.55.0
maintainer Acked-by on v1 10/32
#18 lore raw

Re: [RFC PATCH 10/32] selinux: use struct_size() and isdigit()

Quoted 9 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Use struct_size() for the size of the flexible xfrm security context struct, matching its allocation, and isdigit() instead of an open-coded range check.

Assisted-by Claude:claude-opus-5-5
#19 lore raw

RFC PATCH 11/32 selinux: use file table names to mark selinuxfs inodes immutable

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Look up the status and policy files via their entries in the selinux_files table instead of duplicating the name literals.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
security/selinux/selinuxfs.c +2 -2
index 0fce6c0b0426..7236348a33dc 100644
@@ -1838,8 +1838,8 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
-	sel_mark_immutable(sb->s_root, "status");
-	sel_mark_immutable(sb->s_root, "policy");
+	sel_mark_immutable(sb->s_root, selinux_files[SEL_STATUS].name);
+	sel_mark_immutable(sb->s_root, selinux_files[SEL_POLICY].name);
 
 	fsi = sb->s_fs_info;
 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
Signature · 1 line
2.55.0
maintainer Acked-by on v1 11/32
#20 lore raw

Re: [RFC PATCH 11/32] selinux: use file table names to mark selinuxfs inodes immutable

Quoted 8 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Look up the status and policy files via their entries in the selinux_files table instead of duplicating the name literals.

Assisted-by Claude:claude-opus-5-5

Not sure this matters, but:

#21 lore raw

RFC PATCH 12/32 selinux: avoid context string allocation on getxattr size query

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

For a size-only query of the security.selinux xattr the context string is computed, copied and immediately freed. Pass a NULL buffer to security_sid_to_context*() instead, which only computes the length.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/hooks.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
security/selinux/hooks.c +6 -9
index 0c4511325ce3..67f96fc95492 100644
@@ -3630,21 +3630,18 @@ static int selinux_inode_getsecurity(struct mnt_idmap *idmap,
 	 */
 	isec = inode_security(inode);
 	if (has_cap_mac_admin(false))
-		error = security_sid_to_context_force(isec->sid, &context,
+		error = security_sid_to_context_force(isec->sid,
+						      alloc ? &context : NULL,
 						      &size);
 	else
 		error = security_sid_to_context(isec->sid,
-						&context, &size);
+						alloc ? &context : NULL,
+						&size);
 	if (error)
 		return error;
-	error = size;
-	if (alloc) {
+	if (alloc)
 		*buffer = context;
-		goto out_nofree;
-	}
-	kfree(context);
-out_nofree:
-	return error;
+	return size;
 }
 
 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Signature · 1 line
2.55.0
maintainer Acked-by on v1 12/32
#22 lore raw

Re: [RFC PATCH 12/32] selinux: avoid context string allocation on getxattr size query

Quoted 9 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

For a size-only query of the security.selinux xattr the context string is computed, copied and immediately freed. Pass a NULL buffer to security_sid_to_context*() instead, which only computes the length.

Assisted-by Claude:claude-opus-5-5
#23 lore raw

RFC PATCH 13/32 selinux: remove unused SECSID_WILD and sidtab_search_force()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Neither has any user.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/include/security.h | 1 -
 security/selinux/ss/sidtab.h        | 7 -------
 2 files changed, 8 deletions(-)
security/selinux/include/security.h +0 -1
index a74415e3afd3..ad470fbb966f 100644
@@ -22,7 +22,6 @@
 #include "policycap.h"
 
 #define SECSID_NULL   0x00000000 /* unspecified SID */
-#define SECSID_WILD   0xffffffff /* wildcard SID */
 #define SECCLASS_NULL 0x0000 /* no class */
 
 /* Identify specific policy version changes */
security/selinux/ss/sidtab.h +0 -7
index 832c85c70d83..a9d932a6352e 100644
@@ -116,13 +116,6 @@ static inline struct context *sidtab_search(struct sidtab *s, u32 sid)
 	return entry ? &entry->context : NULL;
 }
 
-static inline struct context *sidtab_search_force(struct sidtab *s, u32 sid)
-{
-	struct sidtab_entry *entry = sidtab_search_entry_force(s, sid);
-
-	return entry ? &entry->context : NULL;
-}
-
 int sidtab_convert(struct sidtab *s, struct sidtab_convert_params *params);
 
 void sidtab_cancel_convert(struct sidtab *s);
Signature · 1 line
2.55.0
#25 lore raw

RFC PATCH 14/32 selinux: make sidtab_init() return void

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

sidtab_init() cannot fail. Drop its return value and the unreachable error handling in its only caller.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/policydb.c | 6 +-----
 security/selinux/ss/sidtab.c   | 4 +---
 security/selinux/ss/sidtab.h   | 2 +-
 3 files changed, 3 insertions(+), 9 deletions(-)
security/selinux/ss/policydb.c +1 -5
index cad404fc8819..ba96baec74a9 100644
@@ -934,11 +934,7 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)
 	bool isid_init;
 	int rc;
 
-	rc = sidtab_init(s);
-	if (rc) {
-		pr_err("SELinux:  out of memory on SID table init\n");
-		return rc;
-	}
+	sidtab_init(s);
 
 	isid_init = ebitmap_get_bit(&p->policycaps,
 				    POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT);
security/selinux/ss/sidtab.c +1 -3
index 118af0aa2767..f55e1d781e52 100644
@@ -32,7 +32,7 @@ struct sidtab_str_cache {
 #define index_to_sid(index) ((index) + SECINITSID_NUM + 1)
 #define sid_to_index(sid)   ((sid) - (SECINITSID_NUM + 1))
 
-int sidtab_init(struct sidtab *s)
+void sidtab_init(struct sidtab *s)
 {
 	u32 i;
 
@@ -53,8 +53,6 @@ int sidtab_init(struct sidtab *s)
 	INIT_LIST_HEAD(&s->cache_lru_list);
 	spin_lock_init(&s->cache_lock);
 #endif
-
-	return 0;
 }
 
 static u32 context_to_sid(struct sidtab *s, struct context *context, u32 hash)
security/selinux/ss/sidtab.h +1 -1
index a9d932a6352e..0298070fb759 100644
@@ -104,7 +104,7 @@ struct sidtab {
 	DECLARE_HASHTABLE(context_to_sid, SIDTAB_HASH_BITS);
 };
 
-int sidtab_init(struct sidtab *s);
+void sidtab_init(struct sidtab *s);
 int sidtab_set_initial(struct sidtab *s, u32 sid, struct context *context);
 struct sidtab_entry *sidtab_search_entry(struct sidtab *s, u32 sid);
 struct sidtab_entry *sidtab_search_entry_force(struct sidtab *s, u32 sid);
Signature · 1 line
2.55.0
#27 lore raw

RFC PATCH 15/32 selinux: make mls_level_isvalid() static

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

It is only used within mls.c.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/mls.c | 3 ++-
 security/selinux/ss/mls.h | 1 -
 2 files changed, 2 insertions(+), 2 deletions(-)
security/selinux/ss/mls.c +2 -1
index 3b2f888506af..81e6cfd0272b 100644
@@ -156,7 +156,8 @@ void mls_sid_to_context(struct policydb *p, struct context *context,
 	*scontext = scontextp;
 }
 
-bool mls_level_isvalid(const struct policydb *p, const struct mls_level *l)
+static bool mls_level_isvalid(const struct policydb *p,
+			      const struct mls_level *l)
 {
 	const char *name;
 	const struct level_datum *levdatum;
security/selinux/ss/mls.h +0 -1
index c107b5182fb7..49efb1808ef0 100644
@@ -29,7 +29,6 @@ void mls_sid_to_context(struct policydb *p, struct context *context,
 			char **scontext);
 bool mls_context_isvalid(const struct policydb *p, const struct context *c);
 bool mls_range_isvalid(const struct policydb *p, const struct mls_range *r);
-bool mls_level_isvalid(const struct policydb *p, const struct mls_level *l);
 
 int mls_context_to_sid(struct policydb *p, char oldc, char *scontext,
 		       struct context *context, struct sidtab *s, u32 def_sid);
Signature · 1 line
2.55.0
maintainer
#28 lore raw

Re: [RFC PATCH 15/32] selinux: make mls_level_isvalid() static

Quoted 7 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

It is only used within mls.c.

Assisted-by Claude:claude-opus-5-5

Will collide with
https://lore.kernel.org/selinux/[email protected]/
Recommend dropping this one.

Quoted 34 lines
---
 security/selinux/ss/mls.c | 3 ++-
 security/selinux/ss/mls.h | 1 -
 2 files changed, 2 insertions(+), 2 deletions(-)
security/selinux/ss/mls.c +2 -1
index 3b2f888506af..81e6cfd0272b 100644
@@ -156,7 +156,8 @@ void mls_sid_to_context(struct policydb *p, struct context *context,
        *scontext = scontextp;
 }
 
-bool mls_level_isvalid(const struct policydb *p, const struct mls_level *l)
+static bool mls_level_isvalid(const struct policydb *p,
+                             const struct mls_level *l)
 {
        const char *name;
        const struct level_datum *levdatum;
security/selinux/ss/mls.h +0 -2
index c107b5182fb7..49efb1808ef0 100644
@@ -29,7 +29,6 @@ void mls_sid_to_context(struct policydb *p, struct context *context,
                        char **scontext);
 bool mls_context_isvalid(const struct policydb *p, const struct context *c);
 bool mls_range_isvalid(const struct policydb *p, const struct mls_range *r);
-bool mls_level_isvalid(const struct policydb *p, const struct mls_level *l);
 
 int mls_context_to_sid(struct policydb *p, char oldc, char *scontext,
                       struct context *context, struct sidtab *s, u32 def_sid);
--

2.55.0

#29 lore raw

RFC PATCH 16/32 selinux: drop unused policydb parameter of perm_read()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

perm_read() never uses its policydb argument.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/policydb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/ss/policydb.c +4 -4
index ba96baec74a9..94b0f1392ad3 100644
@@ -1224,8 +1224,8 @@ static u32 perm_claimed_mask(u32 nprim)
 	return nprim ? U32_MAX >> (SEL_VEC_MAX - nprim) : 0;
 }
 
-static int perm_read(struct policydb *p, struct symtab *s,
-		     struct policy_file *fp, u32 *claimed)
+static int perm_read(struct symtab *s, struct policy_file *fp,
+		     u32 *claimed)
 {
 	char *key = NULL;
 	struct perm_datum *perdatum;
@@ -1310,7 +1310,7 @@ static int common_read(struct policydb *p, struct symtab *s, struct policy_file
 		goto bad;
 
 	for (i = 0; i < nel; i++) {
-		rc = perm_read(p, &comdatum->permissions, fp, &claimed);
+		rc = perm_read(&comdatum->permissions, fp, &claimed);
 		if (rc)
 			goto bad;
 	}
@@ -1561,7 +1561,7 @@ static int class_read(struct policydb *p, struct symtab *s, struct policy_file *
 		}
 	}
 	for (i = 0; i < nel; i++) {
-		rc = perm_read(p, &cladatum->permissions, fp, &claimed);
+		rc = perm_read(&cladatum->permissions, fp, &claimed);
 		if (rc)
 			goto bad;
 	}
Signature · 1 line
2.55.0
#31 lore raw

RFC PATCH 17/32 selinux: drop redundant last_class_ino parameter

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_make_classes() is always passed a pointer to the last_class_ino member of the superblock's selinux_fs_info, which its callee sel_make_class_dir_entries() already accesses directly. Do the same.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
security/selinux/selinuxfs.c +6 -8
index 7236348a33dc..4982f9362e25 100644
@@ -295,8 +295,7 @@ static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_
 			  unsigned int *bool_num, char ***bool_pending_names,
 			  int **bool_pending_values);
 static int sel_make_classes(struct selinux_policy *newpolicy,
-			    struct dentry *class_dir,
-			    u64 *last_class_ino);
+			    struct dentry *class_dir);
 
 /* declaration for sel_make_class_dirs */
 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
@@ -479,8 +478,7 @@ static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
 	if (ret)
 		goto out;
 
-	ret = sel_make_classes(newpolicy, tmp_class_dir,
-			       &fsi->last_class_ino);
+	ret = sel_make_classes(newpolicy, tmp_class_dir);
 	if (ret)
 		goto out;
 
@@ -1668,9 +1666,9 @@ static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
 }
 
 static int sel_make_classes(struct selinux_policy *newpolicy,
-			    struct dentry *class_dir,
-			    u64 *last_class_ino)
+			    struct dentry *class_dir)
 {
+	struct selinux_fs_info *fsi = class_dir->d_sb->s_fs_info;
 	u32 i, nclasses;
 	int rc;
 	char **classes;
@@ -1680,13 +1678,13 @@ static int sel_make_classes(struct selinux_policy *newpolicy,
 		return rc;
 
 	/* +2 since classes are 1-indexed */
-	*last_class_ino = sel_class_to_ino(nclasses + 2);
+	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
 
 	for (i = 0; i < nclasses; i++) {
 		struct dentry *class_name_dir;
 
 		class_name_dir = sel_make_dir(class_dir, classes[i],
-					      last_class_ino);
+					      &fsi->last_class_ino);
 		if (IS_ERR(class_name_dir)) {
 			rc = PTR_ERR(class_name_dir);
 			goto out;
Signature · 1 line
2.55.0
maintainer Acked-by on v1 17/32
#32 lore raw

Re: [RFC PATCH 17/32] selinux: drop redundant last_class_ino parameter

Quoted 9 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_make_classes() is always passed a pointer to the last_class_ino member of the superblock's selinux_fs_info, which its callee sel_make_class_dir_entries() already accesses directly. Do the same.

Assisted-by Claude:claude-opus-5-5
#33 lore raw

RFC PATCH 18/32 selinux: drop dead NULL check in sel_open_policy()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

plm is always non-NULL on the error path.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
security/selinux/selinuxfs.c +2 -3
index 4982f9362e25..d86babad998c 100644
@@ -327,7 +327,7 @@ struct policy_load_memory {
 
 static int sel_open_policy(struct inode *inode, struct file *filp)
 {
-	struct policy_load_memory *plm = NULL;
+	struct policy_load_memory *plm;
 	int rc;
 
 	rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
@@ -356,8 +356,7 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 err:
 	mutex_unlock(&selinux_state.policy_mutex);
 
-	if (plm)
-		vfree(plm->data);
+	vfree(plm->data);
 	kfree(plm);
 	return rc;
 }
Signature · 1 line
2.55.0
#35 lore raw

RFC PATCH 19/32 selinux: drop no-op gotos in ocontext SID lookups

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Remove error checks that jump to the label directly following them.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/services.c | 15 ---------------
 1 file changed, 15 deletions(-)
security/selinux/ss/services.c +0 -15
index ce9f9c483f49..1b8f420e5e1b 100644
@@ -2482,13 +2482,10 @@ int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
 			rcu_read_unlock();
 			goto retry;
 		}
-		if (rc)
-			goto out;
 	} else {
 		*out_sid = SECINITSID_PORT;
 	}
 
-out:
 	rcu_read_unlock();
 	return rc;
 }
@@ -2535,12 +2532,9 @@ int security_ib_pkey_sid(u64 subnet_prefix, u16 pkey_num, u32 *out_sid)
 			rcu_read_unlock();
 			goto retry;
 		}
-		if (rc)
-			goto out;
 	} else
 		*out_sid = SECINITSID_UNLABELED;
 
-out:
 	rcu_read_unlock();
 	return rc;
 }
@@ -2588,12 +2582,9 @@ int security_ib_endport_sid(const char *dev_name, u8 port_num, u32 *out_sid)
 			rcu_read_unlock();
 			goto retry;
 		}
-		if (rc)
-			goto out;
 	} else
 		*out_sid = SECINITSID_UNLABELED;
 
-out:
 	rcu_read_unlock();
 	return rc;
 }
@@ -2644,12 +2635,9 @@ int security_netif_sid(const char *name, u32 *if_sid)
 			rcu_read_unlock();
 			goto retry;
 		}
-		if (rc)
-			goto out;
 	} else
 		*if_sid = SECINITSID_NETIF;
 
-out:
 	rcu_read_unlock();
 	return rc;
 }
@@ -2899,8 +2887,6 @@ int security_fs_use(struct super_block *sb)
 			rcu_read_unlock();
 			goto retry;
 		}
-		if (rc)
-			goto out;
 	} else {
 		rc = __security_genfs_sid(policy, fstype, "/",
 					SECCLASS_DIR, &sbsec->sid);
@@ -2916,7 +2902,6 @@ int security_fs_use(struct super_block *sb)
 		}
 	}
 
-out:
 	rcu_read_unlock();
 	return rc;
 }
Signature · 1 line
2.55.0
#37 lore raw

RFC PATCH 20/32 selinux: consolidate MLS context copy helpers

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

mls_context_cpy(), mls_context_cpy_low() and mls_context_cpy_high() only differ in the source levels. Implement them via a common helper.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/context.h | 56 ++++++++++++++---------------------
 1 file changed, 22 insertions(+), 34 deletions(-)
security/selinux/ss/context.h +22 -34
index dd3b9b5b588e..4fe42da81cc3 100644
@@ -39,43 +39,42 @@ static inline void mls_context_init(struct context *c)
 	memset(&c->range, 0, sizeof(c->range));
 }
 
-static inline int mls_context_cpy(struct context *dst,
-				  const struct context *src)
+/*
+ * Sets the MLS range of 'dst' to the levels 'low' and 'high'.
+ */
+static inline int mls_context_cpy_levels(struct context *dst,
+					 const struct mls_level *low,
+					 const struct mls_level *high)
 {
 	int rc;
 
-	dst->range.level[0].sens = src->range.level[0].sens;
-	rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
+	dst->range.level[0].sens = low->sens;
+	rc = ebitmap_cpy(&dst->range.level[0].cat, &low->cat);
 	if (rc)
-		goto out;
+		return rc;
 
-	dst->range.level[1].sens = src->range.level[1].sens;
-	rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
+	dst->range.level[1].sens = high->sens;
+	rc = ebitmap_cpy(&dst->range.level[1].cat, &high->cat);
 	if (rc)
 		ebitmap_destroy(&dst->range.level[0].cat);
-out:
 	return rc;
 }
 
+static inline int mls_context_cpy(struct context *dst,
+				  const struct context *src)
+{
+	return mls_context_cpy_levels(dst, &src->range.level[0],
+				      &src->range.level[1]);
+}
+
 /*
  * Sets both levels in the MLS range of 'dst' to the low level of 'src'.
  */
 static inline int mls_context_cpy_low(struct context *dst,
 				      const struct context *src)
 {
-	int rc;
-
-	dst->range.level[0].sens = src->range.level[0].sens;
-	rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
-	if (rc)
-		goto out;
-
-	dst->range.level[1].sens = src->range.level[0].sens;
-	rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[0].cat);
-	if (rc)
-		ebitmap_destroy(&dst->range.level[0].cat);
-out:
-	return rc;
+	return mls_context_cpy_levels(dst, &src->range.level[0],
+				      &src->range.level[0]);
 }
 
 /*
@@ -84,19 +83,8 @@ static inline int mls_context_cpy_low(struct context *dst,
 static inline int mls_context_cpy_high(struct context *dst,
 				       const struct context *src)
 {
-	int rc;
-
-	dst->range.level[0].sens = src->range.level[1].sens;
-	rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[1].cat);
-	if (rc)
-		goto out;
-
-	dst->range.level[1].sens = src->range.level[1].sens;
-	rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
-	if (rc)
-		ebitmap_destroy(&dst->range.level[0].cat);
-out:
-	return rc;
+	return mls_context_cpy_levels(dst, &src->range.level[1],
+				      &src->range.level[1]);
 }
 
 static inline int mls_context_glblub(struct context *dst,
Signature · 1 line
2.55.0
maintainer Acked-by on v1 20/32
#38 lore raw

Re: [RFC PATCH 20/32] selinux: consolidate MLS context copy helpers

Quoted 8 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

mls_context_cpy(), mls_context_cpy_low() and mls_context_cpy_high() only differ in the source levels. Implement them via a common helper.

Assisted-by Claude:claude-opus-5-5
#39 lore raw

RFC PATCH 21/32 selinux: simplify mls_context_glblub() and mls_context_equal()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Drop a no-op goto and a dead initialization in mls_context_glblub() and use mls_level_eq() in mls_context_equal().

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/context.h | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
security/selinux/ss/context.h +6 -13
index 4fe42da81cc3..ca8bd9fc100e 100644
@@ -93,7 +93,7 @@ static inline int mls_context_glblub(struct context *dst,
 {
 	struct mls_range *dr = &dst->range;
 	const struct mls_range *r1 = &c1->range, *r2 = &c2->range;
-	int rc = 0;
+	int rc;
 
 	if (r1->level[1].sens < r2->level[0].sens ||
 	    r2->level[1].sens < r1->level[0].sens)
@@ -109,24 +109,17 @@ static inline int mls_context_glblub(struct context *dst,
 	rc = ebitmap_and(&dr->level[0].cat, &r1->level[0].cat,
 			 &r2->level[0].cat);
 	if (rc)
-		goto out;
-
-	rc = ebitmap_and(&dr->level[1].cat, &r1->level[1].cat,
-			 &r2->level[1].cat);
-	if (rc)
-		goto out;
+		return rc;
 
-out:
-	return rc;
+	return ebitmap_and(&dr->level[1].cat, &r1->level[1].cat,
+			   &r2->level[1].cat);
 }
 
 static inline bool mls_context_equal(const struct context *c1,
 				     const struct context *c2)
 {
-	return ((c1->range.level[0].sens == c2->range.level[0].sens) &&
-		ebitmap_equal(&c1->range.level[0].cat, &c2->range.level[0].cat) &&
-		(c1->range.level[1].sens == c2->range.level[1].sens) &&
-		ebitmap_equal(&c1->range.level[1].cat, &c2->range.level[1].cat));
+	return mls_level_eq(&c1->range.level[0], &c2->range.level[0]) &&
+	       mls_level_eq(&c1->range.level[1], &c2->range.level[1]);
 }
 
 static inline void mls_context_destroy(struct context *c)
Signature · 1 line
2.55.0
#41 lore raw

RFC PATCH 22/32 selinux: factor out constraint_list_destroy()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

cls_destroy() tears down the constraints and validatetrans lists with the same code. Move it into a helper.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/policydb.c | 47 +++++++++++++++-------------------
 1 file changed, 20 insertions(+), 27 deletions(-)
security/selinux/ss/policydb.c +20 -27
index 94b0f1392ad3..a80aebf0bbfa 100644
@@ -221,42 +221,35 @@ static void constraint_expr_destroy(struct constraint_expr *expr)
 	}
 }
 
+static void constraint_list_destroy(struct constraint_node *constraint)
+{
+	struct constraint_node *ctemp;
+	struct constraint_expr *e, *etmp;
+
+	while (constraint) {
+		e = constraint->expr;
+		while (e) {
+			etmp = e;
+			e = e->next;
+			constraint_expr_destroy(etmp);
+		}
+		ctemp = constraint;
+		constraint = constraint->next;
+		kfree(ctemp);
+	}
+}
+
 static int cls_destroy(void *key, void *datum, void *p)
 {
 	struct class_datum *cladatum;
-	struct constraint_node *constraint, *ctemp;
-	struct constraint_expr *e, *etmp;
 
 	kfree(key);
 	if (datum) {
 		cladatum = datum;
 		hashtab_map(&cladatum->permissions.table, perm_destroy, NULL);
 		hashtab_destroy(&cladatum->permissions.table);
-		constraint = cladatum->constraints;
-		while (constraint) {
-			e = constraint->expr;
-			while (e) {
-				etmp = e;
-				e = e->next;
-				constraint_expr_destroy(etmp);
-			}
-			ctemp = constraint;
-			constraint = constraint->next;
-			kfree(ctemp);
-		}
-
-		constraint = cladatum->validatetrans;
-		while (constraint) {
-			e = constraint->expr;
-			while (e) {
-				etmp = e;
-				e = e->next;
-				constraint_expr_destroy(etmp);
-			}
-			ctemp = constraint;
-			constraint = constraint->next;
-			kfree(ctemp);
-		}
+		constraint_list_destroy(cladatum->constraints);
+		constraint_list_destroy(cladatum->validatetrans);
 		kfree(cladatum->comkey);
 	}
 	kfree(datum);
Signature · 1 line
2.55.0
#43 lore raw

RFC PATCH 23/32 selinux: simplify nlmsg_perm()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Pass the number of table entries instead of the table size in bytes, return directly on a match and drop unreachable breaks after returns.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/nlmsgtab.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
security/selinux/nlmsgtab.c +9 -14
index 655d2616c9d2..41618c3f81c1 100644
@@ -153,17 +153,16 @@ static const struct nlmsg_perm nlmsg_audit_perms[] = {
 static int nlmsg_perm(u16 nlmsg_type, u32 *perm, const struct nlmsg_perm *tab,
 		      size_t tabsize)
 {
-	unsigned int i;
-	int err = -EINVAL;
+	size_t i;
 
-	for (i = 0; i < tabsize / sizeof(struct nlmsg_perm); i++)
+	for (i = 0; i < tabsize; i++) {
 		if (nlmsg_type == tab[i].nlmsg_type) {
 			*perm = tab[i].perm;
-			err = 0;
-			break;
+			return 0;
 		}
+	}
 
-	return err;
+	return -EINVAL;
 }
 
 int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
@@ -189,16 +188,14 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
 			return 0;
 		}
 		return nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms,
-				  sizeof(nlmsg_route_perms));
-		break;
+				  ARRAY_SIZE(nlmsg_route_perms));
 	case SECCLASS_NETLINK_TCPDIAG_SOCKET:
 		if (selinux_policycap_netlink_xperm()) {
 			*perm = NETLINK_TCPDIAG_SOCKET__NLMSG;
 			return 0;
 		}
 		return nlmsg_perm(nlmsg_type, perm, nlmsg_tcpdiag_perms,
-				  sizeof(nlmsg_tcpdiag_perms));
-		break;
+				  ARRAY_SIZE(nlmsg_tcpdiag_perms));
 	case SECCLASS_NETLINK_XFRM_SOCKET:
 		/* If the BUILD_BUG_ON() below fails you must update the
 		 * structures at the top of this file with the new mappings
@@ -211,8 +208,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
 			return 0;
 		}
 		return nlmsg_perm(nlmsg_type, perm, nlmsg_xfrm_perms,
-				  sizeof(nlmsg_xfrm_perms));
-		break;
+				  ARRAY_SIZE(nlmsg_xfrm_perms));
 	case SECCLASS_NETLINK_AUDIT_SOCKET:
 		if (selinux_policycap_netlink_xperm()) {
 			*perm = NETLINK_AUDIT_SOCKET__NLMSG;
@@ -225,8 +221,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
 			return 0;
 		}
 		return nlmsg_perm(nlmsg_type, perm, nlmsg_audit_perms,
-				  sizeof(nlmsg_audit_perms));
-		break;
+				  ARRAY_SIZE(nlmsg_audit_perms));
 	}
 
 	/* No messaging from userspace, or class unknown/unhandled */
Signature · 1 line
2.55.0
maintainer Acked-by on v1 23/32
#44 lore raw

Re: [RFC PATCH 23/32] selinux: simplify nlmsg_perm()

Quoted 8 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Pass the number of table entries instead of the table size in bytes, return directly on a match and drop unreachable breaks after returns.

Assisted-by Claude:claude-opus-5-5

Nit: You didn't rename the tabsize parameter to reflect the change in meaning. Otherwise,

#45 lore raw

RFC PATCH 24/32 selinux: factor out sel_sid_to_payload()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

The context, create, relabel and member transaction handlers all end by converting the resulting SID to a context string, rejecting it with -ERANGE if it does not fit into the transaction buffer, and copying it into the buffer as the response.

Move that tail into a common helper. The values returned to userspace are unchanged. As a side effect an oversized context is now also logged for relabel, which previously was the only handler to fail silently, and the logged function name is that of the helper.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 90 ++++++++++++------------------------
 1 file changed, 30 insertions(+), 60 deletions(-)
security/selinux/selinuxfs.c +30 -60
index d86babad998c..d1815cb88840 100644
@@ -576,22 +576,17 @@ static const struct file_operations sel_load_ops = {
 	.llseek		= generic_file_llseek,
 };
 
-static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
+/*
+ * Store the context of @sid as the response of a transaction based
+ * write method in @buf and return its length.
+ */
+static ssize_t sel_sid_to_payload(u32 sid, char *buf)
 {
-	char *canon = NULL;
-	u32 sid, len;
+	char *con = NULL;
+	u32 len;
 	ssize_t length;
 
-	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
-			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
-	if (length)
-		goto out;
-
-	length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
-	if (length)
-		goto out;
-
-	length = security_sid_to_context(sid, &canon, &len);
+	length = security_sid_to_context(sid, &con, &len);
 	if (length)
 		goto out;
 
@@ -602,13 +597,30 @@ static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
 		goto out;
 	}
 
-	memcpy(buf, canon, len);
+	memcpy(buf, con, len);
 	length = len;
 out:
-	kfree(canon);
+	kfree(con);
 	return length;
 }
 
+static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
+{
+	u32 sid;
+	ssize_t length;
+
+	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
+			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
+	if (length)
+		return length;
+
+	length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
+	if (length)
+		return length;
+
+	return sel_sid_to_payload(sid, buf);
+}
+
 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
 				     size_t count, loff_t *ppos)
 {
@@ -820,8 +832,6 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
 	u32 ssid, tsid, newsid;
 	u16 tclass;
 	ssize_t length;
-	char *newcon = NULL;
-	u32 len;
 	int nargs;
 
 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
@@ -893,21 +903,8 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
 	if (length)
 		goto out;
 
-	length = security_sid_to_context(newsid, &newcon, &len);
-	if (length)
-		goto out;
-
-	length = -ERANGE;
-	if (len > SIMPLE_TRANSACTION_LIMIT) {
-		pr_err("SELinux: %s:  context size (%u) exceeds "
-			"payload max\n", __func__, len);
-		goto out;
-	}
-
-	memcpy(buf, newcon, len);
-	length = len;
+	length = sel_sid_to_payload(newsid, buf);
 out:
-	kfree(newcon);
 	kfree(namebuf);
 	kfree(tcon);
 	kfree(scon);
@@ -920,8 +917,6 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
 	u32 ssid, tsid, newsid;
 	u16 tclass;
 	ssize_t length;
-	char *newcon = NULL;
-	u32 len;
 
 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
@@ -955,18 +950,8 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
 	if (length)
 		goto out;
 
-	length = security_sid_to_context(newsid, &newcon, &len);
-	if (length)
-		goto out;
-
-	length = -ERANGE;
-	if (len > SIMPLE_TRANSACTION_LIMIT)
-		goto out;
-
-	memcpy(buf, newcon, len);
-	length = len;
+	length = sel_sid_to_payload(newsid, buf);
 out:
-	kfree(newcon);
 	kfree(tcon);
 	kfree(scon);
 	return length;
@@ -987,8 +972,6 @@ static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
 	u32 ssid, tsid, newsid;
 	u16 tclass;
 	ssize_t length;
-	char *newcon = NULL;
-	u32 len;
 
 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
@@ -1022,21 +1005,8 @@ static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
 	if (length)
 		goto out;
 
-	length = security_sid_to_context(newsid, &newcon, &len);
-	if (length)
-		goto out;
-
-	length = -ERANGE;
-	if (len > SIMPLE_TRANSACTION_LIMIT) {
-		pr_err("SELinux: %s:  context size (%u) exceeds "
-			"payload max\n", __func__, len);
-		goto out;
-	}
-
-	memcpy(buf, newcon, len);
-	length = len;
+	length = sel_sid_to_payload(newsid, buf);
 out:
-	kfree(newcon);
 	kfree(tcon);
 	kfree(scon);
 	return length;
Signature · 1 line
2.55.0
maintainer Acked-by on v1 24/32
#46 lore raw

Re: [RFC PATCH 24/32] selinux: factor out sel_sid_to_payload()

Quoted 8 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

The context, create, relabel and member transaction handlers all end by converting the resulting SID to a context string, rejecting it with -ERANGE if it does not fit into the transaction buffer, and copying it into the buffer as the response.

Quoted 7 lines

Move that tail into a common helper. The values returned to userspace are unchanged. As a side effect an oversized context is now also logged for relabel, which previously was the only handler to fail silently, and the logged function name is that of the helper.

Assisted-by Claude:claude-opus-5-5
#47 lore raw

RFC PATCH 25/32 selinux: share implementation of relabel and member transactions

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_write_relabel() and sel_write_member() are identical except for the permission checked and the security server function computing the new SID. Implement both via a common helper taking these two as arguments.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 60 +++++++++---------------------------
 1 file changed, 14 insertions(+), 46 deletions(-)
security/selinux/selinuxfs.c +14 -46
index d1815cb88840..595f9fabbc0b 100644
@@ -911,7 +911,10 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
 	return length;
 }
 
-static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
+static ssize_t sel_write_compute_sid(char *buf, size_t size, u32 perm,
+				     int (*compute_sid)(u32 ssid, u32 tsid,
+							u16 tclass,
+							u32 *out_sid))
 {
 	char *scon = NULL, *tcon = NULL;
 	u32 ssid, tsid, newsid;
@@ -919,8 +922,7 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
 	ssize_t length;
 
 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
-			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
-			      NULL);
+			      SECCLASS_SECURITY, perm, NULL);
 	if (length)
 		goto out;
 
@@ -946,7 +948,7 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
 	if (length)
 		goto out;
 
-	length = security_change_sid(ssid, tsid, tclass, &newsid);
+	length = compute_sid(ssid, tsid, tclass, &newsid);
 	if (length)
 		goto out;
 
@@ -957,6 +959,12 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
 	return length;
 }
 
+static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
+{
+	return sel_write_compute_sid(buf, size, SECURITY__COMPUTE_RELABEL,
+				     security_change_sid);
+}
+
 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
 {
 	pr_err_once("SELinux: %s (%d) wrote to user. This is no longer supported.\n",
@@ -968,48 +976,8 @@ static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
 
 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
 {
-	char *scon = NULL, *tcon = NULL;
-	u32 ssid, tsid, newsid;
-	u16 tclass;
-	ssize_t length;
-
-	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
-			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
-			      NULL);
-	if (length)
-		goto out;
-
-	length = -ENOMEM;
-	scon = kzalloc(size + 1, GFP_KERNEL);
-	if (!scon)
-		goto out;
-
-	length = -ENOMEM;
-	tcon = kzalloc(size + 1, GFP_KERNEL);
-	if (!tcon)
-		goto out;
-
-	length = -EINVAL;
-	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
-		goto out;
-
-	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
-	if (length)
-		goto out;
-
-	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
-	if (length)
-		goto out;
-
-	length = security_member_sid(ssid, tsid, tclass, &newsid);
-	if (length)
-		goto out;
-
-	length = sel_sid_to_payload(newsid, buf);
-out:
-	kfree(tcon);
-	kfree(scon);
-	return length;
+	return sel_write_compute_sid(buf, size, SECURITY__COMPUTE_MEMBER,
+				     security_member_sid);
 }
 
 static struct inode *sel_make_inode(struct super_block *sb, umode_t mode)
Signature · 1 line
2.55.0
maintainer Acked-by on v1 25/32
#48 lore raw

Re: [RFC PATCH 25/32] selinux: share implementation of relabel and member transactions

Quoted 10 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_write_relabel() and sel_write_member() are identical except for the permission checked and the security server function computing the new SID. Implement both via a common helper taking these two as arguments.

Assisted-by Claude:claude-opus-5-5
#49 lore raw

RFC PATCH 26/32 selinux: drop page_mkwrite handler for the policy mapping

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

->page_mkwrite() is only invoked for write faults on VM_SHARED mappings. A shared mapping of the policy file can never become
writable: the inode is S_IMMUTABLE, so the file cannot be opened for writing and do_mmap() clears VM_SHARED and VM_MAYWRITE for MAP_SHARED mappings of it, and in addition sel_mmap_policy() clears VM_MAYWRITE and rejects VM_WRITE for any VM_SHARED mapping.

Drop the unreachable handler and with it the FAULT_FLAG_MKWRITE test, which is only set on the page_mkwrite path. Write faults on private mappings still reach ->fault() with FAULT_FLAG_WRITE and keep failing with SIGBUS.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
security/selinux/selinuxfs.c +1 -2
index 595f9fabbc0b..9c626ef26a78 100644
@@ -391,7 +391,7 @@ static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
 	unsigned long offset;
 	struct page *page;
 
-	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
+	if (vmf->flags & FAULT_FLAG_WRITE)
 		return VM_FAULT_SIGBUS;
 
 	offset = vmf->pgoff << PAGE_SHIFT;
@@ -408,7 +408,6 @@ static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
 
 static const struct vm_operations_struct sel_mmap_policy_ops = {
 	.fault = sel_mmap_policy_fault,
-	.page_mkwrite = sel_mmap_policy_fault,
 };
 
 static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Signature · 1 line
2.55.0
maintainer Acked-by on v1 26/32
#50 lore raw

Re: [RFC PATCH 26/32] selinux: drop page_mkwrite handler for the policy mapping

Quoted 10 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

->page_mkwrite() is only invoked for write faults on VM_SHARED mappings. A shared mapping of the policy file can never become
writable: the inode is S_IMMUTABLE, so the file cannot be opened for writing and do_mmap() clears VM_SHARED and VM_MAYWRITE for MAP_SHARED mappings of it, and in addition sel_mmap_policy() clears VM_MAYWRITE and rejects VM_WRITE for any VM_SHARED mapping.

Quoted 7 lines

Drop the unreachable handler and with it the FAULT_FLAG_MKWRITE test, which is only set on the page_mkwrite path. Write faults on private mappings still reach ->fault() with FAULT_FLAG_WRITE and keep failing with SIGBUS.

Assisted-by Claude:claude-opus-5-5
#51 lore raw

RFC PATCH 27/32 selinux: fail mount if status or policy cannot be marked immutable

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_mark_immutable() silently ignores a failed lookup of the file to mark. Since the status and policy files rely on S_IMMUTABLE to prevent writable opens and thereby writable shared mappings, fail the mount instead of continuing without the protection.

The lookup cannot fail in practice, as the dentries have just been created by simple_fill_super() and are pinned in the dcache, so this does not change behavior.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
security/selinux/selinuxfs.c +16 -7
index 9c626ef26a78..8d72fba5bcdc 100644
@@ -1722,15 +1722,19 @@ static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
 
 #define NULL_FILE_NAME "null"
 
-static void sel_mark_immutable(struct dentry *root, const char *name)
+static int sel_mark_immutable(struct dentry *root, const char *name)
 {
 	struct qstr q = QSTR(name);
 	struct dentry *dentry = try_lookup_noperm(&q, root);
 
-	if (!IS_ERR_OR_NULL(dentry)) {
-		d_inode(dentry)->i_flags |= S_IMMUTABLE;
-		dput(dentry);
-	}
+	if (IS_ERR(dentry))
+		return PTR_ERR(dentry);
+	if (!dentry)
+		return -ENOENT;
+
+	d_inode(dentry)->i_flags |= S_IMMUTABLE;
+	dput(dentry);
+	return 0;
 }
 
 static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
@@ -1772,8 +1776,13 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
-	sel_mark_immutable(sb->s_root, selinux_files[SEL_STATUS].name);
-	sel_mark_immutable(sb->s_root, selinux_files[SEL_POLICY].name);
+	ret = sel_mark_immutable(sb->s_root, selinux_files[SEL_STATUS].name);
+	if (ret)
+		goto err;
+
+	ret = sel_mark_immutable(sb->s_root, selinux_files[SEL_POLICY].name);
+	if (ret)
+		goto err;
 
 	fsi = sb->s_fs_info;
 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
Signature · 1 line
2.55.0
maintainer
#52 lore raw

Re: [RFC PATCH 27/32] selinux: fail mount if status or policy cannot be marked immutable

Quoted 8 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_mark_immutable() silently ignores a failed lookup of the file to mark. Since the status and policy files rely on S_IMMUTABLE to prevent writable opens and thereby writable shared mappings, fail the mount instead of continuing without the protection.

Quoted 6 lines

The lookup cannot fail in practice, as the dentries have just been created by simple_fill_super() and are pinned in the dcache, so this does not change behavior.

Assisted-by Claude:claude-opus-5-5

As you note above this isn't necessary because the lookup cannot fail in practice.
Hence, I don't see the point of making this change. If we truly want to fail hard/closed,
I would just BUG_ON() a failed lookup and be done with it.

#53 lore raw

RFC PATCH 28/32 selinux: pass inode number by value to sel_make_dir()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_make_dir() and sel_make_swapover_dir() take a pointer to an inode number counter which they increment and assign. This forces sel_make_policy_nodes(), which wants to reuse the inode numbers of the existing booleans and class directories, to set a temporary counter to the desired value minus one.

Pass the inode number to assign instead and let callers increment their counters themselves. All directories keep their inode numbers.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 40 ++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
security/selinux/selinuxfs.c +20 -20
index 8d72fba5bcdc..a131b27c70a7 100644
@@ -299,10 +299,10 @@ static int sel_make_classes(struct selinux_policy *newpolicy,
 
 /* declaration for sel_make_class_dirs */
 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
-				   u64 *ino);
+				   u64 ino);
 
 /* declaration for sel_make_policy_nodes */
-static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino);
+static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 ino);
 
 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
 				size_t count, loff_t *ppos)
@@ -451,21 +451,21 @@ static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
 	unsigned int bool_num = 0;
 	char **bool_names = NULL;
 	int *bool_values = NULL;
-	u64 tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */
 
-	tmp_parent = sel_make_swapover_dir(fsi->sb, &tmp_ino);
+	/* Don't increment last_ino in this function */
+	tmp_parent = sel_make_swapover_dir(fsi->sb, fsi->last_ino + 1);
 	if (IS_ERR(tmp_parent))
 		return PTR_ERR(tmp_parent);
 
-	tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
-	tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino);
+	tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME,
+				    d_inode(fsi->bool_dir)->i_ino);
 	if (IS_ERR(tmp_bool_dir)) {
 		ret = PTR_ERR(tmp_bool_dir);
 		goto out;
 	}
 
-	tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
-	tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino);
+	tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME,
+				     d_inode(fsi->class_dir)->i_ino);
 	if (IS_ERR(tmp_class_dir)) {
 		ret = PTR_ERR(tmp_class_dir);
 		goto out;
@@ -1594,7 +1594,7 @@ static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
 	if (err)
 		return err;
 
-	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
+	dentry = sel_make_dir(dir, "perms", ++fsi->last_class_ino);
 	if (IS_ERR(dentry))
 		return PTR_ERR(dentry);
 
@@ -1620,7 +1620,7 @@ static int sel_make_classes(struct selinux_policy *newpolicy,
 		struct dentry *class_name_dir;
 
 		class_name_dir = sel_make_dir(class_dir, classes[i],
-					      &fsi->last_class_ino);
+					      ++fsi->last_class_ino);
 		if (IS_ERR(class_name_dir)) {
 			rc = PTR_ERR(class_name_dir);
 			goto out;
@@ -1666,7 +1666,7 @@ static int sel_make_policycap(struct dentry *dir)
 }
 
 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
-				   u64 *ino)
+				   u64 ino)
 {
 	struct inode *inode;
 
@@ -1676,7 +1676,7 @@ static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
 
 	inode->i_op = &simple_dir_inode_operations;
 	inode->i_fop = &simple_dir_operations;
-	inode->i_ino = ++(*ino);
+	inode->i_ino = ino;
 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
 	inc_nlink(inode);
 	/* bump link count on parent directory, too */
@@ -1695,7 +1695,7 @@ static const struct inode_operations swapover_dir_inode_operations = {
 	.permission	= reject_all,
 };
 
-static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
+static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 ino)
 {
 	struct dentry *dentry;
 	struct inode *inode;
@@ -1711,7 +1711,7 @@ static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
 	}
 
 	inode->i_op = &swapover_dir_inode_operations;
-	inode->i_ino = ++(*ino);
+	inode->i_ino = ino;
 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
 	inc_nlink(inode);
 	d_make_persistent(dentry, inode);
@@ -1785,7 +1785,7 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 		goto err;
 
 	fsi = sb->s_fs_info;
-	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
+	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, ++fsi->last_ino);
 	if (IS_ERR(fsi->bool_dir)) {
 		ret = PTR_ERR(fsi->bool_dir);
 		fsi->bool_dir = NULL;
@@ -1808,7 +1808,7 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
-	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
+	dentry = sel_make_dir(sb->s_root, "avc", ++fsi->last_ino);
 	if (IS_ERR(dentry)) {
 		ret = PTR_ERR(dentry);
 		goto err;
@@ -1818,7 +1818,7 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
-	dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
+	dentry = sel_make_dir(sb->s_root, "ss", ++fsi->last_ino);
 	if (IS_ERR(dentry)) {
 		ret = PTR_ERR(dentry);
 		goto err;
@@ -1828,7 +1828,7 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
-	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
+	dentry = sel_make_dir(sb->s_root, "initial_contexts", ++fsi->last_ino);
 	if (IS_ERR(dentry)) {
 		ret = PTR_ERR(dentry);
 		goto err;
@@ -1838,14 +1838,14 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
-	fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino);
+	fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, ++fsi->last_ino);
 	if (IS_ERR(fsi->class_dir)) {
 		ret = PTR_ERR(fsi->class_dir);
 		fsi->class_dir = NULL;
 		goto err;
 	}
 
-	dentry = sel_make_dir(sb->s_root, "policy_capabilities", &fsi->last_ino);
+	dentry = sel_make_dir(sb->s_root, "policy_capabilities", ++fsi->last_ino);
 	if (IS_ERR(dentry)) {
 		ret = PTR_ERR(dentry);
 		goto err;
Signature · 1 line
2.55.0
maintainer Acked-by on v1 28/32
#54 lore raw

Re: [RFC PATCH 28/32] selinux: pass inode number by value to sel_make_dir()

Quoted 9 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

sel_make_dir() and sel_make_swapover_dir() take a pointer to an inode number counter which they increment and assign. This forces sel_make_policy_nodes(), which wants to reuse the inode numbers of the existing booleans and class directories, to set a temporary counter to the desired value minus one.

Quoted 5 lines

Pass the inode number to assign instead and let callers increment their counters themselves. All directories keep their inode numbers.

Assisted-by Claude:claude-opus-5-5
#55 lore raw

RFC PATCH 29/32 selinux: drop sb member of struct selinux_fs_info

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

The back pointer to the super block is only used by sel_make_policy_nodes(), whose sole caller has the super block at hand via the written file's inode. Pass the super block instead and look up the selinuxfs info from it.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/selinuxfs.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
security/selinux/selinuxfs.c +7 -9
index a131b27c70a7..648bee713ea8 100644
@@ -78,7 +78,6 @@ struct selinux_fs_info {
 	struct dentry *class_dir;
 	u64 last_class_ino;
 	u64 last_ino;
-	struct super_block *sb;
 };
 
 static int selinux_fs_info_create(struct super_block *sb)
@@ -90,7 +89,6 @@ static int selinux_fs_info_create(struct super_block *sb)
 		return -ENOMEM;
 
 	fsi->last_ino = SEL_INO_NEXT - 1;
-	fsi->sb = sb;
 	sb->s_fs_info = fsi;
 	return 0;
 }
@@ -442,9 +440,10 @@ static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names,
 	kfree(bool_values);
 }
 
-static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
-				struct selinux_policy *newpolicy)
+static int sel_make_policy_nodes(struct super_block *sb,
+				 struct selinux_policy *newpolicy)
 {
+	struct selinux_fs_info *fsi = sb->s_fs_info;
 	int ret = 0;
 	struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir;
 	struct renamedata rd = {};
@@ -453,7 +452,7 @@ static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
 	int *bool_values = NULL;
 
 	/* Don't increment last_ino in this function */
-	tmp_parent = sel_make_swapover_dir(fsi->sb, fsi->last_ino + 1);
+	tmp_parent = sel_make_swapover_dir(sb, fsi->last_ino + 1);
 	if (IS_ERR(tmp_parent))
 		return PTR_ERR(tmp_parent);
 
@@ -481,7 +480,7 @@ static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
 		goto out;
 
 	rd.old_parent = tmp_parent;
-	rd.new_parent = fsi->sb->s_root;
+	rd.new_parent = sb->s_root;
 
 	/* booleans */
 	ret = start_renaming_two_dentries(&rd, tmp_bool_dir, fsi->bool_dir);
@@ -522,7 +521,6 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
 			      size_t count, loff_t *ppos)
 
 {
-	struct selinux_fs_info *fsi;
 	struct selinux_load_state load_state;
 	ssize_t length;
 	void *data = NULL;
@@ -549,8 +547,8 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
 		pr_warn_ratelimited("SELinux: failed to load policy\n");
 		goto out_unlock;
 	}
-	fsi = file_inode(file)->i_sb->s_fs_info;
-	length = sel_make_policy_nodes(fsi, load_state.policy);
+	length = sel_make_policy_nodes(file_inode(file)->i_sb,
+				       load_state.policy);
 	if (length) {
 		pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
 		selinux_policy_cancel(&load_state);
Signature · 1 line
2.55.0
maintainer Acked-by on v1 29/32
#56 lore raw

Re: [RFC PATCH 29/32] selinux: drop sb member of struct selinux_fs_info

Quoted 10 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

The back pointer to the super block is only used by sel_make_policy_nodes(), whose sole caller has the super block at hand via the written file's inode. Pass the super block instead and look up the selinuxfs info from it.

Assisted-by Claude:claude-opus-5-5
#57 lore raw

RFC PATCH 30/32 selinux: add level_val_to_struct[] for sensitivity lookups

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

mls_level_isvalid() is reached from policydb_context_isvalid() for every newly computed context, e.g. on each security_compute_sid() call. To get at the level datum it translates the sensitivity value to its name via sym_val_to_name[] and then looks that name up again in the p_levels hash table, which requires hashing and comparing the string.

Record the non-alias level datums in a new level_val_to_struct[] array, filled in sens_index() next to sym_val_to_name[SYM_LEVELS] and freed in policydb_destroy() like the other *_val_to_struct arrays, and use it in mls_level_isvalid().

Allocate the array with kvzalloc_objs() and release it with kvfree(), so large sensitivity indexes can fall back to vmalloc just like the existing sensitivity name array.

The result is the same: sym_val_to_name[SYM_LEVELS] only holds the keys of non-alias datums, and looking such a key up in the (unique-key) symbol table yields exactly the datum that registered it. A value with no non-alias datum still has a NULL entry and is rejected.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/mls.c      | 7 +------
 security/selinux/ss/policydb.c | 7 +++++++
 security/selinux/ss/policydb.h | 2 ++
 3 files changed, 10 insertions(+), 6 deletions(-)
security/selinux/ss/mls.c +1 -6
index 81e6cfd0272b..68e4625a2a38 100644
@@ -159,17 +159,12 @@ void mls_sid_to_context(struct policydb *p, struct context *context,
 static bool mls_level_isvalid(const struct policydb *p,
 			      const struct mls_level *l)
 {
-	const char *name;
 	const struct level_datum *levdatum;
 
 	if (!l->sens || l->sens > p->p_levels.nprim)
 		return false;
 
-	name = sym_name(p, SYM_LEVELS, l->sens - 1);
-	if (!name)
-		return false;
-
-	levdatum = symtab_search(&p->p_levels, name);
+	levdatum = p->level_val_to_struct[l->sens - 1];
 	if (!levdatum)
 		return false;
 
security/selinux/ss/policydb.c +7 -0
index a80aebf0bbfa..8bdfad99aafa 100644
@@ -658,6 +658,7 @@ static int sens_index(void *key, void *datum, void *datap)
 		if (p->sym_val_to_name[SYM_LEVELS][levdatum->level.sens - 1])
 			return -EINVAL;
 		p->sym_val_to_name[SYM_LEVELS][levdatum->level.sens - 1] = key;
+		p->level_val_to_struct[levdatum->level.sens - 1] = levdatum;
 	}
 
 	return 0;
@@ -791,6 +792,11 @@ static int policydb_index(struct policydb *p)
 	if (!p->type_val_to_struct)
 		return -ENOMEM;
 
+	p->level_val_to_struct = kvzalloc_objs(*p->level_val_to_struct,
+					       p->p_levels.nprim);
+	if (!p->level_val_to_struct)
+		return -ENOMEM;
+
 	rc = cond_init_bool_indexes(p);
 	if (rc)
 		goto out;
@@ -857,6 +863,7 @@ void policydb_destroy(struct policydb *p)
 	kfree(p->role_val_to_struct);
 	kfree(p->user_val_to_struct);
 	kvfree(p->type_val_to_struct);
+	kvfree(p->level_val_to_struct);
 
 	avtab_destroy(&p->te_avtab);
 
security/selinux/ss/policydb.h +2 -0
index 974180b2e3a3..a5ab8ffc4eae 100644
@@ -256,6 +256,8 @@ struct policydb {
 	struct role_datum **role_val_to_struct;
 	struct user_datum **user_val_to_struct;
 	struct type_datum **type_val_to_struct;
+	/* non-alias sensitivities indexed by (sens - 1) */
+	struct level_datum **level_val_to_struct;
 
 	/* type enforcement access vectors and transitions */
 	struct avtab te_avtab;
Signature · 1 line
2.55.0
maintainer Acked-by on v1 30/32
#58 lore raw

Re: [RFC PATCH 30/32] selinux: add level_val_to_struct[] for sensitivity lookups

Quoted 18 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

mls_level_isvalid() is reached from policydb_context_isvalid() for every newly computed context, e.g. on each security_compute_sid() call. To get at the level datum it translates the sensitivity value to its name via sym_val_to_name[] and then looks that name up again in the p_levels hash table, which requires hashing and comparing the string.

Record the non-alias level datums in a new level_val_to_struct[] array, filled in sens_index() next to sym_val_to_name[SYM_LEVELS] and freed in policydb_destroy() like the other *_val_to_struct arrays, and use it in mls_level_isvalid().

Allocate the array with kvzalloc_objs() and release it with kvfree(), so large sensitivity indexes can fall back to vmalloc just like the existing sensitivity name array.

Quoted 7 lines

The result is the same: sym_val_to_name[SYM_LEVELS] only holds the keys of non-alias datums, and looking such a key up in the (unique-key) symbol table yields exactly the datum that registered it. A value with no non-alias datum still has a NULL entry and is rejected.

Assisted-by Claude:claude-opus-5-5
#59 lore raw

RFC PATCH 31/32 selinux: add policydb_class_datum() helper

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Five sites open-code the range check of a class value followed by the lookup in class_val_to_struct[]. Add a helper returning the class datum or NULL for an out of range value, and use it in context_struct_compute_av(), security_compute_validatetrans(), security_compute_sid(), mls_compute_sid() and string_to_av_perm().

Each caller keeps its own handling of an invalid class. In security_compute_sid() the unconditional assignment makes the reset of cladatum on retry redundant.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/mls.c      |  8 +++-----
 security/selinux/ss/policydb.c |  4 ++--
 security/selinux/ss/policydb.h |  9 +++++++++
 security/selinux/ss/services.c | 13 +++++--------
 4 files changed, 19 insertions(+), 15 deletions(-)
security/selinux/ss/mls.c +3 -5
index 68e4625a2a38..fa19ef6caf7f 100644
@@ -465,11 +465,9 @@ int mls_compute_sid(struct policydb *p, struct context *scontext,
 		if (r)
 			return mls_range_set(newcontext, r);
 
-		if (tclass && tclass <= p->p_classes.nprim) {
-			cladatum = p->class_val_to_struct[tclass - 1];
-			if (cladatum)
-				default_range = cladatum->default_range;
-		}
+		cladatum = policydb_class_datum(p, tclass);
+		if (cladatum)
+			default_range = cladatum->default_range;
 
 		switch (default_range) {
 		case DEFAULT_SOURCE_LOW:
security/selinux/ss/policydb.c +2 -2
index 8bdfad99aafa..5230c24d46c3 100644
@@ -2103,10 +2103,10 @@ u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
 	struct perm_datum *perdatum = NULL;
 	struct common_datum *comdatum;
 
-	if (!tclass || tclass > p->p_classes.nprim)
+	cladatum = policydb_class_datum(p, tclass);
+	if (!cladatum)
 		return 0;
 
-	cladatum = p->class_val_to_struct[tclass - 1];
 	comdatum = cladatum->comdatum;
 	if (comdatum)
 		perdatum = symtab_search(&comdatum->permissions, name);
security/selinux/ss/policydb.h +9 -0
index a5ab8ffc4eae..8ccb4c3392fc 100644
@@ -408,6 +408,15 @@ static inline const char *sym_name(const struct policydb *p, unsigned int sym_nu
 	return p->sym_val_to_name[sym_num][element_nr];
 }
 
+static inline struct class_datum *policydb_class_datum(const struct policydb *p,
+						       u16 tclass)
+{
+	if (!tclass || tclass > p->p_classes.nprim)
+		return NULL;
+
+	return p->class_val_to_struct[tclass - 1];
+}
+
 static inline bool val_is_boolean(u32 value)
 {
 	return value == 0 || value == 1;
security/selinux/ss/services.c +5 -8
index 1b8f420e5e1b..aa7579d21140 100644
@@ -641,13 +641,12 @@ static void context_struct_compute_av(struct policydb *policydb,
 		memset(xperms, 0, sizeof(*xperms));
 	}
 
-	if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
+	tclass_datum = policydb_class_datum(policydb, tclass);
+	if (unlikely(!tclass_datum)) {
 		pr_warn_ratelimited("SELinux:  Invalid class %u\n", tclass);
 		return;
 	}
 
-	tclass_datum = policydb->class_val_to_struct[tclass - 1];
-
 	/*
 	 * If a specific type enforcement rule was defined for
 	 * this permission check, then use it.
@@ -784,11 +783,11 @@ static int security_compute_validatetrans(u32 oldsid, u32 newsid, u32 tasksid,
 	else
 		tclass = orig_tclass;
 
-	if (!tclass || tclass > policydb->p_classes.nprim) {
+	tclass_datum = policydb_class_datum(policydb, tclass);
+	if (!tclass_datum) {
 		rc = -EINVAL;
 		goto out;
 	}
-	tclass_datum = policydb->class_val_to_struct[tclass - 1];
 
 	oentry = sidtab_search_entry(sidtab, oldsid);
 	if (!oentry) {
@@ -1785,7 +1784,6 @@ static int security_compute_sid(u32 ssid,
 	}
 
 retry:
-	cladatum = NULL;
 	context_init(&newcontext);
 
 	rcu_read_lock();
@@ -1822,8 +1820,7 @@ static int security_compute_sid(u32 ssid,
 	scontext = &sentry->context;
 	tcontext = &tentry->context;
 
-	if (tclass && tclass <= policydb->p_classes.nprim)
-		cladatum = policydb->class_val_to_struct[tclass - 1];
+	cladatum = policydb_class_datum(policydb, tclass);
 
 	/* Set the user identity. */
 	switch (specified) {
Signature · 1 line
2.55.0
maintainer Acked-by on v1 31/32
#60 lore raw

Re: [RFC PATCH 31/32] selinux: add policydb_class_datum() helper

Quoted 9 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

Five sites open-code the range check of a class value followed by the lookup in class_val_to_struct[]. Add a helper returning the class datum or NULL for an out of range value, and use it in context_struct_compute_av(), security_compute_validatetrans(), security_compute_sid(), mls_compute_sid() and string_to_av_perm().

Quoted 6 lines

Each caller keeps its own handling of an invalid class. In security_compute_sid() the unconditional assignment makes the reset of cladatum on retry redundant.

Assisted-by Claude:claude-opus-5-5

This also fixes context_struct_compute_av(), security_compute_validatetrans(), and string_to_av_perm() for the case where class_val_to_struct[value-1] == NULL so it is more than just a refactor.

#61 lore raw

RFC PATCH 32/32 selinux: drop unused policydb parameter of context_write()

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

context_write() never uses its policydb argument.

Assisted-by Claude:claude-opus-5-5
---
 security/selinux/ss/policydb.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
security/selinux/ss/policydb.c +11 -11
index 5230c24d46c3..5c3bf5bcbe47 100644
@@ -3317,7 +3317,7 @@ static int role_allow_write(struct role_allow *r, struct policy_file *fp)
  * Write a security context structure
  * to a policydb binary representation file.
  */
-static int context_write(struct policydb *p, struct context *c, struct policy_file *fp)
+static int context_write(struct context *c, struct policy_file *fp)
 {
 	int rc;
 	__le32 buf[3];
@@ -3705,7 +3705,7 @@ static int ocontext_write(struct policydb *p,
 				rc = put_entry(buf, sizeof(u32), 1, fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3719,10 +3719,10 @@ static int ocontext_write(struct policydb *p,
 				rc = put_entry(c->u.name, 1, len, fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[1], fp);
+				rc = context_write(&c->context[1], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3733,7 +3733,7 @@ static int ocontext_write(struct policydb *p,
 				rc = put_entry(buf, sizeof(u32), 3, fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3743,7 +3743,7 @@ static int ocontext_write(struct policydb *p,
 				rc = put_entry(nodebuf, sizeof(u32), 2, fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3757,7 +3757,7 @@ static int ocontext_write(struct policydb *p,
 				rc = put_entry(c->u.name, 1, len, fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3773,7 +3773,7 @@ static int ocontext_write(struct policydb *p,
 				rc = put_entry(nodebuf, sizeof(u32), 8, fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3792,7 +3792,7 @@ static int ocontext_write(struct policydb *p,
 				rc = put_entry(buf, sizeof(u32), 2, fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3807,7 +3807,7 @@ static int ocontext_write(struct policydb *p,
 					       fp);
 				if (rc)
 					return rc;
-				rc = context_write(p, &c->context[0], fp);
+				rc = context_write(&c->context[0], fp);
 				if (rc)
 					return rc;
 				break;
@@ -3861,7 +3861,7 @@ static int genfs_write(struct policydb *p, struct policy_file *fp)
 			rc = put_entry(buf, sizeof(u32), 1, fp);
 			if (rc)
 				return rc;
-			rc = context_write(p, &c->context[0], fp);
+			rc = context_write(&c->context[0], fp);
 			if (rc)
 				return rc;
 		}
Signature · 1 line
2.55.0
maintainer Acked-by on v1 2/32
#63 lore raw
In reply to Christian Göttsche
Quoted 9 lines · Christian Göttsche

From: Christian Göttsche <[email protected]>

All AVC callbacks are registered for AVC_CALLBACK_RESET only, and it is the only event ever dispatched. Drop the events mask, the event argument and the redundant event checks in the callbacks.

Assisted-by Claude:claude-opus-5-5