selinux: policy load fixes
v3 cover selinux: policy load fixes
Changes in v3:
- move new struct declarations to the top of services.c
- add another patch to clean up error reporting during policy load
Changes in v2:
- switch to a more minimal fix which allocates the conversion params dynamically and passes them between the functions
- split out the MAC_POLICY_LOAD record fix into a separate patch
Ondrej Mosnacek (3):
selinux: don't log MAC_POLICY_LOAD record on failed policy load
selinux: fix variable scope issue in live sidtab conversion
selinuxfs: unify policy load error reporting
security/selinux/include/security.h | 15 +++++-- security/selinux/selinuxfs.c | 22 +++++----- security/selinux/ss/services.c | 63 ++++++++++++++++++----------- 3 files changed, 59 insertions(+), 41 deletions(-)
Signature · 1 line
2.30.2
PATCH v3 1/3 selinux: don't log MAC_POLICY_LOAD record on failed policy load
If sel_make_policy_nodes() fails, we should jump to 'out', not 'out1', as the latter would incorrectly log an MAC_POLICY_LOAD audit record, even though the policy hasn't actually been reloaded. The 'out1' jump label now becomes unused and can be removed.
--- security/selinux/selinuxfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
security/selinux/selinuxfs.c +1 -2
@@ -654,14 +654,13 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf, length =sel_make_policy_nodes (fsi, newpolicy);if (length) {selinux_policy_cancel (fsi->state, newpolicy); - goto out1; + goto out; }selinux_policy_commit (fsi->state, newpolicy); length = count; -out1: audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, "auid=%u ses=%u lsm=selinux res=1", from_kuid(&init_user_ns, audit_get_loginuid(current)),
Signature · 1 line
2.30.2
PATCH v3 2/3 selinux: fix variable scope issue in live sidtab conversion
In reply to Ondrej MosnacekCommit 02a52c5c8c3b ("selinux: move policy commit after updating selinuxfs") moved the selinux_policy_commit() call out of security_load_policy() into sel_write_load(), which caused a subtle yet rather serious bug.
The problem is that security_load_policy() passes a reference to the convert_params local variable to sidtab_convert(), which stores it in the sidtab, where it may be accessed until the policy is swapped over and RCU synchronized. Before 02a52c5c8c3b, selinux_policy_commit() was called directly from security_load_policy(), so the convert_params pointer remained valid all the way until the old sidtab was destroyed, but now that's no longer the case and calls to sidtab_context_to_sid() on the old sidtab after security_load_policy() returns may cause invalid memory accesses.
This can be easily triggered using the stress test from commit ee1a84fdfeed ("selinux: overhaul sidtab to fix bug and improve performance"):
```
function rand_cat() {
echo $(( $RANDOM % 1024 ))
}
function do_work() {
while true; do
echo -n "system_u:system_r:kernel_t:s0:c$(rand_cat),c$(rand_cat)" \
>/sys/fs/selinux/context 2>/dev/null || true
done
}
do_work >/dev/null &
do_work >/dev/null &
do_work >/dev/null &
while load_policy; do echo -n .; sleep 0.1; done
kill %1
kill %2
kill %3
```Fix this by allocating the temporary sidtab convert structures dynamically and passing them among the selinux_policy_{load,cancel,commit} functions.
--- security/selinux/include/security.h | 15 +++++-- security/selinux/selinuxfs.c | 10 ++--- security/selinux/ss/services.c | 63 ++++++++++++++++++----------- 3 files changed, 55 insertions(+), 33 deletions(-)
security/selinux/include/security.h +11 -4
@@ -219,14 +219,21 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)return READ_ONCE (state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS ]); } +struct selinux_policy_convert_data ; + +struct selinux_load_state { +struct selinux_policy *policy; +struct selinux_policy_convert_data *convert_data; +}; +int security_mls_enabled (struct selinux_state *state);int security_load_policy (struct selinux_state *state, -void *data,size_t len, -struct selinux_policy **newpolicyp); +void *data,size_t len, +struct selinux_load_state *load_state);void selinux_policy_commit (struct selinux_state *state, -struct selinux_policy *newpolicy); +struct selinux_load_state *load_state);void selinux_policy_cancel (struct selinux_state *state, -struct selinux_policy *policy); +struct selinux_load_state *load_state);int security_read_policy (struct selinux_state *state,void **data,size_t *len); int security_read_state_kernel(struct selinux_state *state,
security/selinux/selinuxfs.c +5 -5
@@ -619,7 +619,7 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf, {struct selinux_fs_info *fsi =file_inode (file)->i_sb->s_fs_info; -struct selinux_policy *newpolicy; +struct selinux_load_state load_state;ssize_t length;void *data =NULL ;
@@ -645,19 +645,19 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,if (copy_from_user (data, buf, count) !=0 ) goto out; - length =security_load_policy (fsi->state, data, count, &newpolicy); + length =security_load_policy (fsi->state, data, count, &load_state);if (length) {pr_warn_ratelimited ("SELinux: failed to load policy\n" ); goto out; } - length =sel_make_policy_nodes (fsi, newpolicy); + length =sel_make_policy_nodes (fsi, load_state.policy);if (length) { -selinux_policy_cancel (fsi->state, newpolicy); +selinux_policy_cancel (fsi->state, &load_state); goto out; } -selinux_policy_commit (fsi->state, newpolicy); +selinux_policy_commit (fsi->state, &load_state); length = count;
security/selinux/ss/services.c +39 -24
136 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v3 3/3 selinuxfs: unify policy load error reporting
In reply to Ondrej MosnacekLet's drop the pr_err()s from sel_make_policy_nodes() and just add one pr_warn_ratelimited() call to the sel_make_policy_nodes() error path in sel_write_load().
Changing from error to warning makes sense, since after 02a52c5c8c3b ("selinux: move policy commit after updating selinuxfs"), this error path no longer leads to a broken selinuxfs tree (it's just kept in the original state and policy load is aborted).
I also added _ratelimited to be consistent with the other prtin in the same function (it's probably not necessary, but can't really hurt... there are likely more important error messages to be printed when filesystem entry creation starts erroring out).
--- security/selinux/selinuxfs.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
security/selinux/selinuxfs.c +3 -6
@@ -566,17 +566,13 @@ static int sel_make_policy_nodes(struct selinux_fs_info *fsi, ret =sel_make_bools (newpolicy, tmp_bool_dir, &tmp_bool_num, &tmp_bool_names, &tmp_bool_values); -if (ret) { -pr_err ("SELinux: failed to load policy booleans\n" ); +if (ret) goto out; - } ret =sel_make_classes (newpolicy, tmp_class_dir, &fsi->last_class_ino); -if (ret) { -pr_err ("SELinux: failed to load policy classes\n" ); +if (ret) goto out; - }/* booleans */ old_dentry = fsi->bool_dir;
@@ -653,6 +649,7 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf, length =sel_make_policy_nodes (fsi, load_state.policy);if (length) { +pr_warn_ratelimited ("SELinux: failed to initialize selinuxfs\n" );selinux_policy_cancel (fsi->state, &load_state); goto out; }
Signature · 1 line
2.30.2
Paul Moore
maintainerQuoted 14 lines · Ondrej Mosnacek
Changes in v3:
- move new struct declarations to the top ofservices.c
- add another patch to clean up error reporting during policy loadChanges in v2:
- switch to a more minimal fix which allocates the conversion params dynamically and passes them between the functions
- split out theMAC_POLICY_LOADrecord fix into a separate patchOndrej Mosnacek (3):
selinux: don't logMAC_POLICY_LOADrecord on failed policy load
selinux: fix variable scope issue in live sidtab conversion
selinuxfs: unify policy load error reporting
Quoted 4 lines
security/selinux/include/security.h | 15 +++++-- security/selinux/selinuxfs.c | 22 +++++----- security/selinux/ss/services.c | 63 ++++++++++++++++++----------- 3 files changed, 59 insertions(+), 41 deletions(-)
Thanks Ondrej. I've merged all three of these into the selinux/stable-5.12 branch and if testing goes well I'll send it up to Linus next week. I was a little undecided on if patch 3/3 qualified as -stable material, but it does fix a user-visible issue and is small enough in scope that I don't think it should be a problem.
Signature · 2 lines
paul moore www.paul-moore.com