selinux: convert policy read-write lock to RCU
Stephen Smalley
maintainerRFC PATCH v4 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]/
---
v4 fixes the __rcu annotation so that sparse doesn't complain about services.c and removes a few extraneous empty lines.
security/selinux/hooks.c | 1 - security/selinux/include/security.h | 5 +- security/selinux/ss/services.c | 487 ++++++++++++++++------------ security/selinux/ss/services.h | 5 - 4 files changed, 280 insertions(+), 218 deletions(-)
security/selinux/hooks.c +0 -1
@@ -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
@@ -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 4struct 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 __rcu *policy; } __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 +278 -209
1,212 lines not on this page · raw message
security/selinux/ss/services.h +0 -5
@@ -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
Quoted 15 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_grantingsequence number into theselinux_policystructure so that it can be updated atomically with the policy. Since removing the policy rwlock and movinglatest_grantingreduces theselinux_ssstructure to nothing more than a wrapper around theselinux_policypointer, 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 takercu_read_lock(), with the preceding comment explaining why. Alternatively we could passfsi->mutexdown from selinuxfs and apply a lockdep check on it instead.
Quoted 8 lines
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]/Signed-off-by Stephen Smalley <[email protected]>---
(from v3 comment section:)
Quoted 4 lines
Remaining open questions include whether I should change selinuxfs to pass down
fsi->mutexso that we can use it in a lockdep check forrcu_dereference_check()and whether the sidtab live convert is safe after this change.
FTR, I spent some time pondering on whether there is any bad interaction with the sidtab live convert and I couldn't find anything. The tricky part was splitting the policy load into load/commit/cancel and that seems to have been done correctly.
As for rcu_dereference_check(), I'd prefer to pass the fsi->mutex in there for better clarity. For example, if someone were to call one of the functions containing it, they may not realize that some mutual exclusion is expected around them. If the functions take the mutex as an argument, they at least have to stop and think what to pass in, which should eventually lead them down the right path.
Quoted 9 lines
v4 fixes the
__rcuannotation so that sparse doesn't complain aboutservices.cand removes a few extraneous empty lines.security/selinux/hooks.c | 1 - security/selinux/include/security.h | 5 +- security/selinux/ss/services.c | 487 ++++++++++++++++------------ security/selinux/ss/services.h | 5 - 4 files changed, 280 insertions(+), 218 deletions(-)
[...]
Quoted 4 lines
security/selinux/ss/services.c +0
-0
[...]
security/selinux/ss/services.c void selinux_policy_commit(struct selinux_state *state, · line 2216
Quoted 10 lines
security/selinux/ss/services.c +6
-0@@ -2174,14 +2208,18 @@ void selinux_policy_commit(struct selinux_state *state,pr_info ("SELinux: Enabling MLS support...\n" ); } +/* Set latest granting seqno for new policy. */ +if (oldpolicy) + newpolicy->latest_granting = oldpolicy->latest_granting +1 ; +else + newpolicy->latest_granting =1 ; + seqno = newpolicy->latest_granting;
This could be written as:
seqno = newpolicy->latest_granting = oldpolicy ?
oldpolicy->latest_granting + 1 : 1;
...which is a bit easier to read to me, but others may differ.
Quoted 14 lines
security/selinux/ss/services.c +3
-5+/* Install the new policy. */ -write_lock_irq (&state->ss->policy_rwlock); - state->ss->policy = newpolicy; - seqno = ++state->ss->latest_granting; -write_unlock_irq (&state->ss->policy_rwlock); +rcu_assign_pointer (state->policy, newpolicy);/* Load the policycaps from the new policy */ -security_load_policycaps (state); +security_load_policycaps (state, newpolicy);if (!selinux_initialized (state)) {/*
[...]
security/selinux/ss/services.c int security_set_bools(struct selinux_state *state, u32 len, int *values) · line 3054
Quoted 7 lines
security/selinux/ss/services.c +3
-0@@ -2997,20 +3049,20 @@ int security_set_bools(struct selinux_state *state, u32 len, int *values)/* Re-evaluate the conditional rules in the copy */ evaluate_cond_nodes (&newpolicy->policydb); +/* Set latest granting seqno for new policy */ + newpolicy->latest_granting = oldpolicy->latest_granting +1 ; + seqno = newpolicy->latest_granting;
These could also be merged into one line.
security/selinux/ss/services.c int security_set_bools(struct selinux_state *state, u32 len, int *values) · line 3058
Quoted 8 lines
security/selinux/ss/services.c +2
-4+/* Install the new policy */ -write_lock_irq (&state->ss->policy_rwlock); - state->ss->policy = newpolicy; - seqno = ++state->ss->latest_granting; -write_unlock_irq (&state->ss->policy_rwlock); +rcu_assign_pointer (state->policy, newpolicy);
[...]
Other than the minor things above, I didn't find any logical issue in this patch.
Since the mutex passing is going to be discussed in a separate patch and the only other comments I had are minor nits (and which may not align with your and/or Paul's preference), here you go:
--
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel Red Hat, Inc.
Quoted 32 lines · Ondrej Mosnacek
Quoted 15 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_grantingsequence number into theselinux_policystructure so that it can be updated atomically with the policy. Since removing the policy rwlock and movinglatest_grantingreduces theselinux_ssstructure to nothing more than a wrapper around theselinux_policypointer, 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 takercu_read_lock(), with the preceding comment explaining why. Alternatively we could passfsi->mutexdown from selinuxfs and apply a lockdep check on it instead.Quoted 8 lines
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]/Signed-off-by Stephen Smalley <[email protected]>---
(from v3 comment section:)
Quoted 4 lines
Remaining open questions include whether I should change selinuxfs to pass down
fsi->mutexso that we can use it in a lockdep check forrcu_dereference_check()and whether the sidtab live convert is safe after this change.
Quoted 4 lines
FTR, I spent some time pondering on whether there is any bad interaction with the sidtab live convert and I couldn't find anything. The tricky part was splitting the policy load into load/commit/cancel and that seems to have been done correctly.
You and Stephen have obviously spent more time looking at this than I have, but looking it over this morning it seems reasonable.
Quoted 2 lines
As for
rcu_dereference_check(), I'd prefer to pass thefsi->mutexin there for better clarity.
I still can't say I'm really loving the write lock side of this, but short of breaking the boundary between the security server and the Linux/LSM hook side of the code I'm not sure there is a really good solution. I think simply commenting it as Stephen has done is about the best we can do.
It is my opinion that passing an arbitrary mutex into the security server code doesn't really accomplish much. Sure, it gives the caller some indication that exclusivity is required, but a bare mutex doesn't really provide any guarantee that the *right* mutex is being used. If we wanted to fix that we would need to look at passing down the selinux_fs_info pointer itself, but then we are back to breaking the abstraction between the security server and the Linux hook layer. The only thing saving us here is that this code is *deep* inside SELinux and it is *extremely* unlikely that we are going to have any cross-subsystem abuse which touches this code. The comment based approach isn't perfect, but I think it is our best option at this point in time.
Quoted 18 lines
Quoted 4 lines
security/selinux/ss/services.c +0
-0[...]
security/selinux/ss/services.c void selinux_policy_commit(struct selinux_state *state, · line 2216
Quoted 10 lines
security/selinux/ss/services.c +6
-0@@ -2174,14 +2208,18 @@ void selinux_policy_commit(struct selinux_state *state,pr_info ("SELinux: Enabling MLS support...\n" ); } +/* Set latest granting seqno for new policy. */ +if (oldpolicy) + newpolicy->latest_granting = oldpolicy->latest_granting +1 ; +else + newpolicy->latest_granting =1 ; + seqno = newpolicy->latest_granting;This could be written as:
Quoted 4 lines
seqno = newpolicy->latest_granting = oldpolicy ?
oldpolicy->latest_granting+ 1 : 1;...which is a bit easier to read to me, but others may differ.
I'll differ :)
I realize multiple assignments look pretty cool in that hey-I-can-combine-three-lines-of-code-into-one sorta way, but I hate having to read code like that, especially when you get to that point where you end up reading more code than you write.
Simple code is easier to read, easier to review, easier to debug, and easier to maintain.
... and just to be clear, combining multiple assignments on a single line in combination with a ternary statement is not my idea of "simple" ;)
Quoted 8 lines
Other than the minor things above, I didn't find any logical issue in this patch.
Since the mutex passing is going to be discussed in a separate patch and the only other comments I had are minor nits (and which may not align with your and/or Paul's preference), here you go:
Reviewed-by Ondrej Mosnacek <[email protected]>
Merged into selinux/next, thanks everyone.
Signature · 2 lines
paul moore www.paul-moore.com