selinux: Add helper functions to get and set checkreqprot
Subsystems (2)
PATCH 1/1 selinux: Add helper functions to get and set checkreqprot
checkreqprot data member in selinux_state struct is accessed directly by SELinux functions to get and set. This could cause unexpected read or write access to this data member due to compiler optimizations and\or compiler's reordering of access to this field.
Add helper functions to get and set checkreqprot data member in selinux_state struct. These helper functions use READ_ONCE and WRITE_ONCE macros to ensure explicit read or write of memory for this data member.
This patch is based on commit 66ccd2560aff
("selinux: simplify away security_policydb_len()") in "next" branch in https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git
--- security/selinux/hooks.c | 6 +++--- security/selinux/include/security.h | 10 ++++++++++ security/selinux/selinuxfs.c | 5 +++-- 3 files changed, 16 insertions(+), 5 deletions(-)
security/selinux/hooks.c +3 -3
@@ -3718,7 +3718,7 @@ static int selinux_mmap_file(struct file *file, unsigned long reqprot,return rc; } -if (selinux_state.checkreqprot) +if (selinux_checkreqprot (&selinux_state)) prot = reqprot; return file_map_prot_check(file, prot,
@@ -3732,7 +3732,7 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,const struct cred *cred =current_cred ();u32 sid =cred_sid (cred); -if (selinux_state.checkreqprot) +if (selinux_checkreqprot (&selinux_state)) prot = reqprot; if (default_noexec &&
@@ -7234,7 +7234,7 @@ 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_checkreqprot_set (&selinux_state, selinux_checkreqprot_boot);selinux_avc_init (&selinux_state.avc);mutex_init (&selinux_state.status_lock);mutex_init (&selinux_state.policy_mutex);
security/selinux/include/security.h +10 -0
@@ -209,6 +209,16 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)return state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS ]; } +static inline bool selinux_checkreqprot (const struct selinux_state *state) +{ +return READ_ONCE (state->checkreqprot); +} +static inline void selinux_checkreqprot_set (struct selinux_state *state, +bool value) +{ +WRITE_ONCE (state->checkreqprot, value); +} +int security_mls_enabled (struct selinux_state *state); int security_load_policy(struct selinux_state *state, void *data, size_t len,
security/selinux/selinuxfs.c +3 -2
@@ -717,7 +717,8 @@ static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,char tmpbuf[TMPBUFLEN ];ssize_t length; - length =scnprintf (tmpbuf,TMPBUFLEN ,"%u" , fsi->state->checkreqprot); + length =scnprintf (tmpbuf,TMPBUFLEN ,"%u" , +selinux_checkreqprot (fsi->state));return simple_read_from_buffer (buf, count, ppos, tmpbuf, length); }
@@ -759,7 +760,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, comm, current->pid); } - fsi->state->checkreqprot = new_value ?1 :0 ; +selinux_checkreqprot_set (fsi->state, (new_value ?1 :0 )); length = count; out:kfree (page);
Signature · 1 line
2.28.0
Stephen Smalley
maintainerQuoted 4 lines · Lakshmi Ramasubramanian
checkreqprot data member in
selinux_statestruct is accessed directly by SELinux functions to get and set. This could cause unexpected read or write access to this data member due to compiler optimizations and\or
and/or
Quoted 6 lines
compiler's reordering of access to this field.
Add helper functions to get and set checkreqprot data member in
selinux_statestruct. These helper functions useREAD_ONCEandWRITE_ONCEmacros to ensure explicit read or write of memory for this data member.
s/explicit/atomic/
Quoted 3 lines
This patch is based on commit
66ccd2560aff
("selinux: simplify awaysecurity_policydb_len()") in "next" branch in https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git
Don't include this kind of information in a commit message, if needed it can go after the --- or in brackets in the subject line ala [-next]
but it isn't necessary when sending against the next branch because
that's the default expectation for submitted patches for selinux. No need to cc lsm list on selinux-only patches.
Quoted 11 lines
Signed-off-by Lakshmi Ramasubramanian <[email protected]>Suggested-by Stephen Smalley <[email protected]>---
security/selinux/include/security.h +0
-0@@ -209,6 +209,16 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)return state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS ]; }
security/selinux/include/security.h static inline bool selinux_policycap_genfs_seclabel_symlinks(void) · line 220
Quoted 10 lines
security/selinux/include/security.h +9
-0+static inline bool selinux_checkreqprot (const struct selinux_state *state) +{ +return READ_ONCE (state->checkreqprot); +} +static inline void selinux_checkreqprot_set (struct selinux_state *state, +bool value) +{ +WRITE_ONCE (state->checkreqprot, value); +}
Move these up with the enforcing accessor functions in this header and use a consistent naming, e.g. checkreqprot_enabled(), checkreqprot_set().