Skip to content

Commit 2c83164

Browse files
stephensmalleygregkh
authored andcommitted
selinux: fall back to ref-walk if audit is required
commit 0188d5c upstream. commit bda0be7 ("security: make inode_follow_link RCU-walk aware") passed down the rcu flag to the SELinux AVC, but failed to adjust the test in slow_avc_audit() to also return -ECHILD on LSM_AUDIT_DATA_DENTRY. Previously, we only returned -ECHILD if generating an audit record with LSM_AUDIT_DATA_INODE since this was only relevant from inode_permission. Move the handling of MAY_NOT_BLOCK to avc_audit() and its inlined equivalent in selinux_inode_permission() immediately after we determine that audit is required, and always fall back to ref-walk in this case. Fixes: bda0be7 ("security: make inode_follow_link RCU-walk aware") Reported-by: Will Deacon <will@kernel.org> Suggested-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Paul Moore <paul@paul-moore.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 36f4415 commit 2c83164

3 files changed

Lines changed: 17 additions & 26 deletions

File tree

‎security/selinux/avc.c‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static inline int avc_xperms_audit(struct selinux_state *state,
424424
if (likely(!audited))
425425
return 0;
426426
return slow_avc_audit(state, ssid, tsid, tclass, requested,
427-
audited, denied, result, ad, 0);
427+
audited, denied, result, ad);
428428
}
429429

430430
static void avc_node_free(struct rcu_head *rhead)
@@ -758,8 +758,7 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
758758
noinline int slow_avc_audit(struct selinux_state *state,
759759
u32 ssid, u32 tsid, u16 tclass,
760760
u32 requested, u32 audited, u32 denied, int result,
761-
struct common_audit_data *a,
762-
unsigned int flags)
761+
struct common_audit_data *a)
763762
{
764763
struct common_audit_data stack_data;
765764
struct selinux_audit_data sad;
@@ -772,17 +771,6 @@ noinline int slow_avc_audit(struct selinux_state *state,
772771
a->type = LSM_AUDIT_DATA_NONE;
773772
}
774773

775-
/*
776-
* When in a RCU walk do the audit on the RCU retry. This is because
777-
* the collection of the dname in an inode audit message is not RCU
778-
* safe. Note this may drop some audits when the situation changes
779-
* during retry. However this is logically just as if the operation
780-
* happened a little later.
781-
*/
782-
if ((a->type == LSM_AUDIT_DATA_INODE) &&
783-
(flags & MAY_NOT_BLOCK))
784-
return -ECHILD;
785-
786774
sad.tclass = tclass;
787775
sad.requested = requested;
788776
sad.ssid = ssid;
@@ -855,16 +843,14 @@ static int avc_update_node(struct selinux_avc *avc,
855843
/*
856844
* If we are in a non-blocking code path, e.g. VFS RCU walk,
857845
* then we must not add permissions to a cache entry
858-
* because we cannot safely audit the denial. Otherwise,
846+
* because we will not audit the denial. Otherwise,
859847
* during the subsequent blocking retry (e.g. VFS ref walk), we
860848
* will find the permissions already granted in the cache entry
861849
* and won't audit anything at all, leading to silent denials in
862850
* permissive mode that only appear when in enforcing mode.
863851
*
864-
* See the corresponding handling in slow_avc_audit(), and the
865-
* logic in selinux_inode_follow_link and selinux_inode_permission
866-
* for the VFS MAY_NOT_BLOCK flag, which is transliterated into
867-
* AVC_NONBLOCKING for avc_has_perm_noaudit().
852+
* See the corresponding handling of MAY_NOT_BLOCK in avc_audit()
853+
* and selinux_inode_permission().
868854
*/
869855
if (flags & AVC_NONBLOCKING)
870856
return 0;

‎security/selinux/hooks.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,8 +3019,7 @@ static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
30193019

30203020
static noinline int audit_inode_permission(struct inode *inode,
30213021
u32 perms, u32 audited, u32 denied,
3022-
int result,
3023-
unsigned flags)
3022+
int result)
30243023
{
30253024
struct common_audit_data ad;
30263025
struct inode_security_struct *isec = selinux_inode(inode);
@@ -3031,7 +3030,7 @@ static noinline int audit_inode_permission(struct inode *inode,
30313030

30323031
rc = slow_avc_audit(&selinux_state,
30333032
current_sid(), isec->sid, isec->sclass, perms,
3034-
audited, denied, result, &ad, flags);
3033+
audited, denied, result, &ad);
30353034
if (rc)
30363035
return rc;
30373036
return 0;
@@ -3078,7 +3077,11 @@ static int selinux_inode_permission(struct inode *inode, int mask)
30783077
if (likely(!audited))
30793078
return rc;
30803079

3081-
rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
3080+
/* fall back to ref-walk if we have to generate audit */
3081+
if (flags & MAY_NOT_BLOCK)
3082+
return -ECHILD;
3083+
3084+
rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
30823085
if (rc2)
30833086
return rc2;
30843087
return rc;

‎security/selinux/include/avc.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ static inline u32 avc_audit_required(u32 requested,
100100
int slow_avc_audit(struct selinux_state *state,
101101
u32 ssid, u32 tsid, u16 tclass,
102102
u32 requested, u32 audited, u32 denied, int result,
103-
struct common_audit_data *a,
104-
unsigned flags);
103+
struct common_audit_data *a);
105104

106105
/**
107106
* avc_audit - Audit the granting or denial of permissions.
@@ -135,9 +134,12 @@ static inline int avc_audit(struct selinux_state *state,
135134
audited = avc_audit_required(requested, avd, result, 0, &denied);
136135
if (likely(!audited))
137136
return 0;
137+
/* fall back to ref-walk if we have to generate audit */
138+
if (flags & MAY_NOT_BLOCK)
139+
return -ECHILD;
138140
return slow_avc_audit(state, ssid, tsid, tclass,
139141
requested, audited, denied, result,
140-
a, flags);
142+
a);
141143
}
142144

143145
#define AVC_STRICT 1 /* Ignore permissive mode. */

0 commit comments

Comments
 (0)