Skip to contentSkip to conversation

selinux: convert policy read-write lock to RCU

RFC PATCH Superseded Stephen Smalley 1 participant 2 messages Aug 19, 2020 SELinux security module

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

RFC PATCH v3 1/1 selinux: convert policy read-write lock to RCU

Convert the policy read-write lock to RCU. This is significantly simplified by the earlier work to encapsulate the policy data structures and refactor the policy load and boolean setting logic. Move the latest_granting sequence number into the selinux_policy structure so that it can be updated atomically with the policy. Since removing the policy rwlock and moving latest_granting reduces the selinux_ss structure to nothing more than a wrapper around the selinux_policy pointer, get rid of the extra layer of indirection.

At present this change merely passes a hardcoded 1 to rcu_dereference_check() in the cases where we know we do not need to take rcu_read_lock(), with the preceding comment explaining why. Alternatively we could pass fsi->mutex down from selinuxfs and apply a lockdep check on it instead.

Based in part on earlier attempts to convert the policy rwlock to RCU by Kaigai Kohei [1] and by Peter Enderborg [2].

[1] https://lore.kernel.org/selinux/[email protected]/
[2] https://lore.kernel.org/selinux/[email protected]/

---
v3 passes the selinux_policy pointer down to lower level functions after the initial rcu_dereference() so that we never dereference it twice on the same code path. It also corrects a missing rcu_deference() and gets rid of some unnecessary cases of rcu_read_lock()/unlock(). Remaining open questions include whether I should change selinuxfs to pass down fsi->mutex so that we can use it in a lockdep check for rcu_dereference_check() and whether the sidtab live convert is safe after this change.

 security/selinux/hooks.c            |   1 -
 security/selinux/include/security.h |   5 +-
 security/selinux/ss/services.c      | 490 ++++++++++++++++------------
 security/selinux/ss/services.h      |   5 -
 4 files changed, 283 insertions(+), 218 deletions(-)
security/selinux/hooks.c +0 -1
index ca901025802a..102292f96b0f 100644
@@ -7232,7 +7232,6 @@ static __init int selinux_init(void)
 	memset(&selinux_state, 0, sizeof(selinux_state));
 	enforcing_set(&selinux_state, selinux_enforcing_boot);
 	selinux_state.checkreqprot = selinux_checkreqprot_boot;
-	selinux_ss_init(&selinux_state.ss);
 	selinux_avc_init(&selinux_state.avc);
 	mutex_init(&selinux_state.status_lock);
 
security/selinux/include/security.h +2 -3
index c68ed2beadff..1b6a67a66a7e 100644
@@ -13,6 +13,7 @@
 #include <linux/dcache.h>
 #include <linux/magic.h>
 #include <linux/types.h>
+#include <linux/rcupdate.h>
 #include <linux/refcount.h>
 #include <linux/workqueue.h>
 #include "flask.h"
@@ -84,7 +85,6 @@ extern int selinux_enabled_boot;
 #define POLICYDB_BOUNDS_MAXDEPTH	4
 
 struct selinux_avc;
-struct selinux_ss;
 struct selinux_policy;
 
 struct selinux_state {
@@ -102,10 +102,9 @@ struct selinux_state {
 	struct mutex status_lock;
 
 	struct selinux_avc *avc;
-	struct selinux_ss *ss;
+	struct selinux_policy *policy __rcu;
 } __randomize_layout;
 
-void selinux_ss_init(struct selinux_ss **ss);
 void selinux_avc_init(struct selinux_avc **avc);
 
 extern struct selinux_state selinux_state;
security/selinux/ss/services.c +281 -209

1,215 lines not on this page · raw message

security/selinux/ss/services.h +0 -5
index 06931e34cb24..9555ad074303 100644
@@ -26,12 +26,7 @@ struct selinux_policy {
 	struct sidtab *sidtab;
 	struct policydb policydb;
 	struct selinux_map map;
-};
-
-struct selinux_ss {
-	rwlock_t policy_rwlock;
 	u32 latest_granting;
-	struct selinux_policy *policy;
 } __randomize_layout;
 
 void services_compute_xperms_drivers(struct extended_perms *xperms,
Signature · 1 line
2.25.1
author maintainer
#1 lore raw
Quoted 23 lines · Stephen Smalley

Convert the policy read-write lock to RCU. This is significantly simplified by the earlier work to encapsulate the policy data structures and refactor the policy load and boolean setting logic. Move the latest_granting sequence number into the selinux_policy structure so that it can be updated atomically with the policy. Since removing the policy rwlock and moving latest_granting reduces the selinux_ss structure to nothing more than a wrapper around the selinux_policy pointer, get rid of the extra layer of indirection.

At present this change merely passes a hardcoded 1 to rcu_dereference_check() in the cases where we know we do not need to take rcu_read_lock(), with the preceding comment explaining why. Alternatively we could pass fsi->mutex down from selinuxfs and apply a lockdep check on it instead.

Based in part on earlier attempts to convert the policy rwlock to RCU by Kaigai Kohei [1] and by Peter Enderborg [2].

[1] https://lore.kernel.org/selinux/[email protected]/
[2] https://lore.kernel.org/selinux/[email protected]/

Quoted 9 lines

---
v3 passes the selinux_policy pointer down to lower level functions after the initial rcu_dereference() so that we never dereference it twice on the same code path. It also corrects a missing rcu_deference() and gets rid of some unnecessary cases of rcu_read_lock()/unlock(). Remaining open questions include whether I should change selinuxfs to pass down fsi->mutex so that we can use it in a lockdep check for rcu_dereference_check() and whether the sidtab live convert is safe after this change.

I forgot to mention: v3 is relative to
https://patchwork.kernel.org/patch/11724203/ on top of the selinux next branch.