Skip to contentSkip to conversation

selinux: cache access vector decisions in the inode security blob

RFC PATCH Superseded Stephen Smalley 1 participant 1 message Mar 10, 2023 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

RFC PATCH 1/1 selinux: cache access vector decisions in the inode security blob

I think Linus suggested this a long long time ago but I never got around to trying it. Better late than never. Compute the access vector decision when the inode security blob is initialized and cache it in the blob. In selinux_inode_permission and in inode_has_perm, use this cached decision unless invalidated.

NB This is relative to "selinux: stop passing selinux_state pointers and their offspring" which is not yet merged. It doesn't technically depend on it but would conflict significantly otherwise.

NB There is an obvious race here between the validation and a file relabel (inode_post_setxattr/inode_setsecurity) although it isn't entirely new to this change. Likely need to change reads/writes of isec->initialized to use smp_load_acquire()/store_release(). Not sure what the net effect on inode permission checking would be. Hence, this is RFC-only.

---
 security/selinux/avc.c            | 45 ++++++++++++++++++---
 security/selinux/hooks.c          | 67 ++++++++++++++++++++++++++-----
 security/selinux/include/avc.h    |  7 ++++
 security/selinux/include/objsec.h |  2 +
 security/selinux/ss/services.c    |  3 +-
 5 files changed, 106 insertions(+), 18 deletions(-)
security/selinux/avc.c +39 -6
index c162e51fb43c..c74bdd76b38a 100644
@@ -357,6 +357,8 @@ static int avc_xperms_populate(struct avc_node *node,
 	struct avc_xperms_decision_node *dest_xpd;
 	struct avc_xperms_decision_node *src_xpd;
 
+	if (!src)
+		return 0;
 	if (src->xp.len == 0)
 		return 0;
 	dest = avc_xperms_alloc();
@@ -988,15 +990,17 @@ static noinline struct avc_node *avc_compute_av(u32 ssid, u32 tsid, u16 tclass,
 						struct av_decision *avd,
 						struct avc_xperms_node *xp_node)
 {
-	INIT_LIST_HEAD(&xp_node->xpd_head);
-	security_compute_av(ssid, tsid, tclass, avd, &xp_node->xp);
+	if (xp_node)
+		INIT_LIST_HEAD(&xp_node->xpd_head);
+	security_compute_av(ssid, tsid, tclass, avd,
+			xp_node ? &xp_node->xp : NULL);
 	return avc_insert(ssid, tsid, tclass, avd, xp_node);
 }
 
-static noinline int avc_denied(u32 ssid, u32 tsid,
-			       u16 tclass, u32 requested,
-			       u8 driver, u8 xperm, unsigned int flags,
-			       struct av_decision *avd)
+noinline int avc_denied(u32 ssid, u32 tsid,
+			u16 tclass, u32 requested,
+			u8 driver, u8 xperm, unsigned int flags,
+			struct av_decision *avd)
 {
 	if (flags & AVC_STRICT)
 		return -EACCES;
@@ -1121,6 +1125,35 @@ static noinline int avc_perm_nonode(u32 ssid, u32 tsid, u16 tclass,
 	return 0;
 }
 
+/**
+ * avc_get_avd - Get access vector decisions
+ * @ssid: source security identifier
+ * @tsid: target security identifier
+ * @tclass: target security class
+ * @avd: access vector decisions
+ *
+ * Get access vector decisions for the specified (@ssid, @tsid, @tclass)
+ * triple, fetching them from the access vector cache if present or
+ * calling the security server to compute them on a miss. Unlike
+ * avc_has_perm_noaudit(), this function does not check any
+ * requested permission; it just returns the entire decision vector.
+ */
+void avc_get_avd(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd)
+{
+	struct avc_node *node;
+
+	rcu_read_lock();
+	node = avc_lookup(ssid, tsid, tclass);
+	if (unlikely(!node)) {
+		rcu_read_unlock();
+		avc_compute_av(ssid, tsid, tclass, avd, NULL);
+		return;
+	}
+	memcpy(avd, &node->ae.avd, sizeof(*avd));
+	rcu_read_unlock();
+}
+
+
 /**
  * avc_has_perm_noaudit - Check permissions but perform no auditing.
  * @ssid: source security identifier
security/selinux/hooks.c +56 -11

147 lines not on this page · raw message

security/selinux/include/avc.h +7 -0
index 9301222c8e55..14fc5ed1a156 100644
@@ -141,6 +141,13 @@ int avc_has_perm_noaudit(u32 ssid, u32 tsid,
 			 unsigned flags,
 			 struct av_decision *avd);
 
+void avc_get_avd(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd);
+
+int avc_denied(u32 ssid, u32 tsid,
+	u16 tclass, u32 requested,
+	u8 driver, u8 xperm, unsigned int flags,
+	struct av_decision *avd);
+
 int avc_has_perm(u32 ssid, u32 tsid,
 		 u16 tclass, u32 requested,
 		 struct common_audit_data *auditdata);
security/selinux/include/objsec.h +2 -0
index 2953132408bf..da95a75e6c77 100644
@@ -50,6 +50,8 @@ struct inode_security_struct {
 	u32 sid;		/* SID of this object */
 	u16 sclass;		/* security class of this object */
 	unsigned char initialized;	/* initialization flag */
+	u32 avdsid;		/* SID when avd was computed */
+	struct av_decision avd; /* access vector decisions */
 	spinlock_t lock;
 };
 
security/selinux/ss/services.c +2 -1
index f14d1ffe54c5..7353c027c389 100644
@@ -1107,7 +1107,8 @@ void security_compute_av(u32 ssid,
 	rcu_read_lock();
 	policy = rcu_dereference(selinux_state.policy);
 	avd_init(policy, avd);
-	xperms->len = 0;
+	if (xperms)
+		xperms->len = 0;
 	if (!selinux_initialized())
 		goto allow;
 
Signature · 1 line
2.39.2