Skip to contentSkip to conversation

selinux: fix avdcache auditing

PATCH In review Stephen Smalley 2 participants 7 messages Apr 10 – Apr 28 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v4 1/1 selinux: fix avdcache auditing

The per-task avdcache was incorrectly saving and reusing the audited vector computed by avc_audit_required() rather than recomputing based on the currently requested permissions and distinguishing the denied versus allowed cases. As a result, some permission checks were not being audited, e.g. directory write checks after a previously cached directory search check.

Fixes dde3a5d0f4dce ("selinux: move avdcache to per-task security struct")

---
v4 changes struct avdc_entry to contain a complete struct av_decision rather than individual fields copied from it. This allows the use of avc_audit_required() for both the cache hit and miss cases.

 security/selinux/hooks.c          | 32 ++++++++++++++-----------------
 security/selinux/include/objsec.h |  4 +---
 2 files changed, 15 insertions(+), 21 deletions(-)
security/selinux/hooks.c +14 -18
index 6ef9c1aade65..dbb03132d4af 100644
@@ -3185,15 +3185,13 @@ static inline int task_avdcache_search(struct task_security_struct *tsec,
  * @tsec: the task's security state
  * @isec: the inode associated with the cache entry
  * @avd: the AVD to cache
- * @audited: the permission audit bitmask to cache
  *
- * Update the AVD cache in @tsec with the @avdc and @audited info associated
+ * Update the AVD cache in @tsec with the @avd info associated
  * with @isec.
  */
 static inline void task_avdcache_update(struct task_security_struct *tsec,
 					struct inode_security_struct *isec,
-					struct av_decision *avd,
-					u32 audited)
+					struct av_decision *avd)
 {
 	int spot;
 
@@ -3205,9 +3203,7 @@ static inline void task_avdcache_update(struct task_security_struct *tsec,
 	spot = (tsec->avdcache.dir_spot + 1) & (TSEC_AVDC_DIR_SIZE - 1);
 	tsec->avdcache.dir_spot = spot;
 	tsec->avdcache.dir[spot].isid = isec->sid;
-	tsec->avdcache.dir[spot].audited = audited;
-	tsec->avdcache.dir[spot].allowed = avd->allowed;
-	tsec->avdcache.dir[spot].permissive = avd->flags & AVD_FLAGS_PERMISSIVE;
+	tsec->avdcache.dir[spot].avd = *avd;
 	tsec->avdcache.permissive_neveraudit =
 		(avd->flags == (AVD_FLAGS_PERMISSIVE|AVD_FLAGS_NEVERAUDIT));
 }
@@ -3228,6 +3224,7 @@ static int selinux_inode_permission(struct inode *inode, int requested)
 	struct task_security_struct *tsec;
 	struct inode_security_struct *isec;
 	struct avdc_entry *avdc;
+	struct av_decision avd, *avdp = &avd;
 	int rc, rc2;
 	u32 audited, denied;
 
@@ -3249,23 +3246,22 @@ static int selinux_inode_permission(struct inode *inode, int requested)
 	rc = task_avdcache_search(tsec, isec, &avdc);
 	if (likely(!rc)) {
 		/* Cache hit. */
-		audited = perms & avdc->audited;
-		denied = perms & ~avdc->allowed;
-		if (unlikely(denied && enforcing_enabled() &&
-			     !avdc->permissive))
+		avdp = &avdc->avd;
+		denied = perms & ~avdp->allowed;
+		if (unlikely(denied) && enforcing_enabled() &&
+			!(avdp->flags & AVD_FLAGS_PERMISSIVE))
 			rc = -EACCES;
 	} else {
-		struct av_decision avd;
-
 		/* Cache miss. */
 		rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass,
-					  perms, 0, &avd);
-		audited = avc_audit_required(perms, &avd, rc,
-			(requested & MAY_ACCESS) ? FILE__AUDIT_ACCESS : 0,
-			&denied);
-		task_avdcache_update(tsec, isec, &avd, audited);
+					  perms, 0, avdp);
+		task_avdcache_update(tsec, isec, avdp);
 	}
 
+	audited = avc_audit_required(perms, avdp, rc,
+				(requested & MAY_ACCESS) ?
+				FILE__AUDIT_ACCESS : 0,
+				&denied);
 	if (likely(!audited))
 		return rc;
 
security/selinux/include/objsec.h +1 -3
index 5bddd28ea5cb..9b5151ad184c 100644
@@ -32,9 +32,7 @@
 
 struct avdc_entry {
 	u32 isid; /* inode SID */
-	u32 allowed; /* allowed permission bitmask */
-	u32 audited; /* audited permission bitmask */
-	bool permissive; /* AVC permissive flag */
+	struct av_decision avd; /* av decision */
 };
 
 struct cred_security_struct {
Signature · 1 line
2.53.0
author maintainer
#1 lore raw
Quoted 9 lines · Stephen Smalley

The per-task avdcache was incorrectly saving and reusing the audited vector computed by avc_audit_required() rather than recomputing based on the currently requested permissions and distinguishing the denied versus allowed cases. As a result, some permission checks were not being audited, e.g. directory write checks after a previously cached directory search check.

Quoted 6 lines
Fixes dde3a5d0f4dce ("selinux: move avdcache to per-task security struct")

---
v4 changes struct avdc_entry to contain a complete struct av_decision rather than individual fields copied from it. This allows the use of avc_audit_required() for both the cache hit and miss cases.

Entirely up to you whether you want to proceed with v3 or v4 (or something else altogether).
Just offering up options. Shouldn't be any behavioral difference, just a question of whether we want
to keep the change as minimal and streamlined in the cache hit case (v3) or prioritize ease of
maintenance of the logic (v4).

Quoted 98 lines
 security/selinux/hooks.c          | 32 ++++++++++++++-----------------
 security/selinux/include/objsec.h |  4 +---
 2 files changed, 15 insertions(+), 21 deletions(-)
security/selinux/hooks.c +14 -18
index 6ef9c1aade65..dbb03132d4af 100644
@@ -3185,15 +3185,13 @@ static inline int task_avdcache_search(struct task_security_struct *tsec,
  * @tsec: the task's security state
  * @isec: the inode associated with the cache entry
  * @avd: the AVD to cache
- * @audited: the permission audit bitmask to cache
  *
- * Update the AVD cache in @tsec with the @avdc and @audited info associated
+ * Update the AVD cache in @tsec with the @avd info associated
  * with @isec.
  */
 static inline void task_avdcache_update(struct task_security_struct *tsec,
                                        struct inode_security_struct *isec,
-                                       struct av_decision *avd,
-                                       u32 audited)
+                                       struct av_decision *avd)
 {
        int spot;
 
@@ -3205,9 +3203,7 @@ static inline void task_avdcache_update(struct task_security_struct *tsec,
        spot = (tsec->avdcache.dir_spot + 1) & (TSEC_AVDC_DIR_SIZE - 1);
        tsec->avdcache.dir_spot = spot;
        tsec->avdcache.dir[spot].isid = isec->sid;
-       tsec->avdcache.dir[spot].audited = audited;
-       tsec->avdcache.dir[spot].allowed = avd->allowed;
-       tsec->avdcache.dir[spot].permissive = avd->flags & AVD_FLAGS_PERMISSIVE;
+       tsec->avdcache.dir[spot].avd = *avd;
        tsec->avdcache.permissive_neveraudit =
                (avd->flags == (AVD_FLAGS_PERMISSIVE|AVD_FLAGS_NEVERAUDIT));
 }
@@ -3228,6 +3224,7 @@ static int selinux_inode_permission(struct inode *inode, int requested)
        struct task_security_struct *tsec;
        struct inode_security_struct *isec;
        struct avdc_entry *avdc;
+       struct av_decision avd, *avdp = &avd;
        int rc, rc2;
        u32 audited, denied;
 
@@ -3249,23 +3246,22 @@ static int selinux_inode_permission(struct inode *inode, int requested)
        rc = task_avdcache_search(tsec, isec, &avdc);
        if (likely(!rc)) {
                /* Cache hit. */
-               audited = perms & avdc->audited;
-               denied = perms & ~avdc->allowed;
-               if (unlikely(denied && enforcing_enabled() &&
-                            !avdc->permissive))
+               avdp = &avdc->avd;
+               denied = perms & ~avdp->allowed;
+               if (unlikely(denied) && enforcing_enabled() &&
+                       !(avdp->flags & AVD_FLAGS_PERMISSIVE))
                        rc = -EACCES;
        } else {
-               struct av_decision avd;
-
                /* Cache miss. */
                rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass,
-                                         perms, 0, &avd);
-               audited = avc_audit_required(perms, &avd, rc,
-                       (requested & MAY_ACCESS) ? FILE__AUDIT_ACCESS : 0,
-                       &denied);
-               task_avdcache_update(tsec, isec, &avd, audited);
+                                         perms, 0, avdp);
+               task_avdcache_update(tsec, isec, avdp);
        }
 
+       audited = avc_audit_required(perms, avdp, rc,
+                               (requested & MAY_ACCESS) ?
+                               FILE__AUDIT_ACCESS : 0,
+                               &denied);
        if (likely(!audited))
                return rc;
 
security/selinux/include/objsec.h +1 -4
index 5bddd28ea5cb..9b5151ad184c 100644
@@ -32,9 +32,7 @@
 
 struct avdc_entry {
        u32 isid; /* inode SID */
-       u32 allowed; /* allowed permission bitmask */
-       u32 audited; /* audited permission bitmask */
-       bool permissive; /* AVC permissive flag */
+       struct av_decision avd; /* av decision */
 };
 
 struct cred_security_struct {
--

2.53.0

author maintainer
#2 lore raw
Quoted 19 lines · Stephen Smalley
Quoted 9 lines · Stephen Smalley

The per-task avdcache was incorrectly saving and reusing the audited vector computed by avc_audit_required() rather than recomputing based on the currently requested permissions and distinguishing the denied versus allowed cases. As a result, some permission checks were not being audited, e.g. directory write checks after a previously cached directory search check.

Quoted 6 lines
Fixes dde3a5d0f4dce ("selinux: move avdcache to per-task security struct")

---
v4 changes struct avdc_entry to contain a complete struct av_decision rather than individual fields copied from it. This allows the use of avc_audit_required() for both the cache hit and miss cases.

Quoted 7 lines

Entirely up to you whether you want to proceed with v3 or v4 (or something else altogether).
Just offering up options. Shouldn't be any behavioral difference, just a question of whether we want
to keep the change as minimal and streamlined in the cache hit case (v3) or prioritize ease of
maintenance of the logic (v4).

Here is some perf data, using default Fedora kconfig:
v7.0, unmodified:
2.03% [k] __d_lookup_rcu
0.80% [k] selinux_inode_permission
Ratio: 39%

v7.0 with v3 patch:
1.95% [k] __d_lookup_rcu
0.81% [k] selinux_inode_permission
Ratio: 41%

v7.0 with v4 patch:
1.89% [k] __d_lookup_rcu
0.92% [k] selinux_inode_permission
Ratio: 49%

I only compared the percentage spent in selinux_inode_permission to __d_lookup_rcu since the changes were localized to selinux_inode_permission. Aside from the fact that v4 is measurably worse than v3, I also noticed that even the baseline seems measurably worse than the selinux_inode_permission() percentages reported in earlier commit messages, although I guess we didn't re-measure when we switched from the per-cred cache to the per-task cache (but that was necessary regardless).

author maintainer
#3 lore raw
Quoted 16 lines · Stephen Smalley

Here is some perf data, using default Fedora kconfig:
v7.0, unmodified:

   2.03%  [k] __d_lookup_rcu
   0.80%  [k] selinux_inode_permission

Ratio: 39%

v7.0 with v3 patch:

   1.95%  [k] __d_lookup_rcu
   0.81%  [k] selinux_inode_permission

Ratio: 41%

v7.0 with v4 patch:

   1.89%  [k] __d_lookup_rcu
   0.92%  [k] selinux_inode_permission

Ratio: 49%

Quoted 8 lines

I only compared the percentage spent in selinux_inode_permission to __d_lookup_rcu since the changes were localized to selinux_inode_permission. Aside from the fact that v4 is measurably worse than v3, I also noticed that even the baseline seems measurably worse than the selinux_inode_permission() percentages reported in earlier commit messages, although I guess we didn't re-measure when we switched from the per-cred cache to the per-task cache (but that was necessary regardless).

Actually, re-running the perf commands on the older commits show similar results for selinux_inode_permission() as I am seeing now, so something else must have changed in my VM or the kconfig.

maintainer
#4 lore raw
Quoted 27 lines · Stephen Smalley
Quoted 16 lines · Stephen Smalley

Here is some perf data, using default Fedora kconfig:
v7.0, unmodified:

   2.03%  [k] __d_lookup_rcu
   0.80%  [k] selinux_inode_permission

Ratio: 39%

v7.0 with v3 patch:

   1.95%  [k] __d_lookup_rcu
   0.81%  [k] selinux_inode_permission

Ratio: 41%

v7.0 with v4 patch:

   1.89%  [k] __d_lookup_rcu
   0.92%  [k] selinux_inode_permission

Ratio: 49%

Quoted 8 lines

I only compared the percentage spent in selinux_inode_permission to __d_lookup_rcu since the changes were localized to selinux_inode_permission. Aside from the fact that v4 is measurably worse than v3, I also noticed that even the baseline seems measurably worse than the selinux_inode_permission() percentages reported in earlier commit messages, although I guess we didn't re-measure when we switched from the per-cred cache to the per-task cache (but that was necessary regardless).

Quoted 3 lines

Actually, re-running the perf commands on the older commits show similar results for selinux_inode_permission() as I am seeing now, so something else must have changed in my VM or the kconfig.

To clarify, are you now seeing similar results with both v7.0+v3 and v7.0+v4?

(v7.0+v3 looks pretty much like noise when compared to v7.0)

Signature · 1 line
paul-moore.com
author maintainer
#5 lore raw
Quoted 30 lines · Paul Moore
Quoted 27 lines · Stephen Smalley
Quoted 24 lines · Stephen Smalley

Here is some perf data, using default Fedora kconfig:
v7.0, unmodified:

   2.03%  [k] __d_lookup_rcu
   0.80%  [k] selinux_inode_permission

Ratio: 39%

v7.0 with v3 patch:

   1.95%  [k] __d_lookup_rcu
   0.81%  [k] selinux_inode_permission

Ratio: 41%

v7.0 with v4 patch:

   1.89%  [k] __d_lookup_rcu
   0.92%  [k] selinux_inode_permission

Ratio: 49%

I only compared the percentage spent in selinux_inode_permission to __d_lookup_rcu since the changes were localized to selinux_inode_permission. Aside from the fact that v4 is measurably worse than v3, I also noticed that even the baseline seems measurably worse than the selinux_inode_permission() percentages reported in earlier commit messages, although I guess we didn't re-measure when we switched from the per-cred cache to the per-task cache (but that was necessary regardless).

Quoted 7 lines
Quoted 3 lines

Actually, re-running the perf commands on the older commits show similar results for selinux_inode_permission() as I am seeing now, so something else must have changed in my VM or the kconfig.

To clarify, are you now seeing similar results with both v7.0+v3 and v7.0+v4?

(v7.0+v3 looks pretty much like noise when compared to v7.0)

No, v7.0+v4 appears to be worse than v7.0+v3. My comment above was with regard to re-testing older (pre-v7.0) commits where we had reported lower overheads for selinux_inode_permission relative to __d_lookup_rcu. On my current test VM, those commits are now showing similar overheads to v7.0+v3, so I think the difference is just some change in my test VM, kernel config, or noise.

maintainer
#6 lore raw
Quoted 36 lines · Stephen Smalley
Quoted 35 lines · Paul Moore
Quoted 27 lines · Stephen Smalley
Quoted 16 lines · Stephen Smalley

Here is some perf data, using default Fedora kconfig:
v7.0, unmodified:

   2.03%  [k] __d_lookup_rcu
   0.80%  [k] selinux_inode_permission

Ratio: 39%

v7.0 with v3 patch:

   1.95%  [k] __d_lookup_rcu
   0.81%  [k] selinux_inode_permission

Ratio: 41%

v7.0 with v4 patch:

   1.89%  [k] __d_lookup_rcu
   0.92%  [k] selinux_inode_permission

Ratio: 49%

Quoted 8 lines

I only compared the percentage spent in selinux_inode_permission to __d_lookup_rcu since the changes were localized to selinux_inode_permission. Aside from the fact that v4 is measurably worse than v3, I also noticed that even the baseline seems measurably worse than the selinux_inode_permission() percentages reported in earlier commit messages, although I guess we didn't re-measure when we switched from the per-cred cache to the per-task cache (but that was necessary regardless).

Quoted 3 lines

Actually, re-running the perf commands on the older commits show similar results for selinux_inode_permission() as I am seeing now, so something else must have changed in my VM or the kconfig.

To clarify, are you now seeing similar results with both v7.0+v3 and v7.0+v4?

Quoted 8 lines
Quoted 1 line

(v7.0+v3 looks pretty much like noise when compared to v7.0)

No, v7.0+v4 appears to be worse than v7.0+v3. My comment above was with regard to re-testing older (pre-v7.0) commits where we had reported lower overheads for selinux_inode_permission relative to __d_lookup_rcu. On my current test VM, those commits are now showing similar overheads to v7.0+v3, so I think the difference is just some change in my test VM, kernel config, or noise.

Okay, thanks for clarifying. Let's go with v4, it may be a bit slower, but as you already said, it should be easier to maintain and slightly less fragile. I'm going to merge this into selinux/stable-7.1 now and send it up to Linus later this week assuming all goes well.

Thanks!

Signature · 1 line
paul-moore.com