Skip to contentSkip to conversation

selinux: fix avdcache auditing

PATCH Superseded Stephen Smalley 2 participants 3 messages Apr 8 SELinux security module

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v2 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. This manifests as unaudited permission denials even after removing all dontaudit rules.

Fixes dde3a5d0f4dce ("selinux: move avdcache to per-task security struct")
---
 security/selinux/hooks.c          | 25 +++++++++++++++----------
 security/selinux/include/objsec.h |  3 ++-
 2 files changed, 17 insertions(+), 11 deletions(-)
security/selinux/hooks.c +15 -10
index 6ef9c1aade65..c279ebdb3a33 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,7 +3203,8 @@ 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].auditallow = avd->auditallow;
+	tsec->avdcache.dir[spot].auditdeny = avd->auditdeny;
 	tsec->avdcache.dir[spot].allowed = avd->allowed;
 	tsec->avdcache.dir[spot].permissive = avd->flags & AVD_FLAGS_PERMISSIVE;
 	tsec->avdcache.permissive_neveraudit =
@@ -3249,11 +3248,17 @@ 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))
-			rc = -EACCES;
+		if (unlikely(denied)) {
+			if (enforcing_enabled() && !avdc->permissive)
+				rc = -EACCES;
+			audited = denied & avdc->auditdeny;
+			if ((requested & MAY_ACCESS) &&
+				!(FILE__AUDIT_ACCESS & avdc->auditdeny))
+				audited = 0;
+		} else {
+			audited = requested & avdc->auditallow;
+		}
 	} else {
 		struct av_decision avd;
 
@@ -3263,7 +3268,7 @@ static int selinux_inode_permission(struct inode *inode, int requested)
 		audited = avc_audit_required(perms, &avd, rc,
 			(requested & MAY_ACCESS) ? FILE__AUDIT_ACCESS : 0,
 			&denied);
-		task_avdcache_update(tsec, isec, &avd, audited);
+		task_avdcache_update(tsec, isec, &avd);
 	}
 
 	if (likely(!audited))
security/selinux/include/objsec.h +2 -1
index 5bddd28ea5cb..baf7564c6a2f 100644
@@ -33,7 +33,8 @@
 struct avdc_entry {
 	u32 isid; /* inode SID */
 	u32 allowed; /* allowed permission bitmask */
-	u32 audited; /* audited permission bitmask */
+	u32 auditallow; /* auditallow permission bitmask */
+	u32 auditdeny; /* auditdeny aka ~dontaudit permission bitmask */
 	bool permissive; /* AVC permissive flag */
 };
 
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 10 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. This manifests as unaudited permission denials even after removing all dontaudit rules.

Quoted 6 lines
Fixes dde3a5d0f4dce ("selinux: move avdcache to per-task security struct")
---
 security/selinux/hooks.c          | 25 +++++++++++++++----------
 security/selinux/include/objsec.h |  3 ++-
 2 files changed, 17 insertions(+), 11 deletions(-)

Of course I hit send on my reply to your v1 patch only to see a v2 in my inbox :)

Do you have a quick changelog of the changes between v1 and v2? It looks like the inode permission code changed ...

Quoted 79 lines
security/selinux/hooks.c +15 -10
index 6ef9c1aade65..c279ebdb3a33 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,7 +3203,8 @@ 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].auditallow = avd->auditallow;
+       tsec->avdcache.dir[spot].auditdeny = avd->auditdeny;
        tsec->avdcache.dir[spot].allowed = avd->allowed;
        tsec->avdcache.dir[spot].permissive = avd->flags & AVD_FLAGS_PERMISSIVE;
        tsec->avdcache.permissive_neveraudit =
@@ -3249,11 +3248,17 @@ 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))
-                       rc = -EACCES;
+               if (unlikely(denied)) {
+                       if (enforcing_enabled() && !avdc->permissive)
+                               rc = -EACCES;
+                       audited = denied & avdc->auditdeny;
+                       if ((requested & MAY_ACCESS) &&
+                               !(FILE__AUDIT_ACCESS & avdc->auditdeny))
+                               audited = 0;
+               } else {
+                       audited = requested & avdc->auditallow;
+               }
        } else {
                struct av_decision avd;
 
@@ -3263,7 +3268,7 @@ static int selinux_inode_permission(struct inode *inode, int requested)
                audited = avc_audit_required(perms, &avd, rc,
                        (requested & MAY_ACCESS) ? FILE__AUDIT_ACCESS : 0,
                        &denied);
-               task_avdcache_update(tsec, isec, &avd, audited);
+               task_avdcache_update(tsec, isec, &avd);
        }
 
        if (likely(!audited))
security/selinux/include/objsec.h +2 -2
index 5bddd28ea5cb..baf7564c6a2f 100644
@@ -33,7 +33,8 @@
 struct avdc_entry {
        u32 isid; /* inode SID */
        u32 allowed; /* allowed permission bitmask */
-       u32 audited; /* audited permission bitmask */
+       u32 auditallow; /* auditallow permission bitmask */
+       u32 auditdeny; /* auditdeny aka ~dontaudit permission bitmask */
        bool permissive; /* AVC permissive flag */
 };
 
--

2.53.0

Signature · 1 line
paul-moore.com
author maintainer
#2 lore raw
Quoted 20 lines · Paul Moore
Quoted 10 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. This manifests as unaudited permission denials even after removing all dontaudit rules.

Quoted 6 lines
Fixes dde3a5d0f4dce ("selinux: move avdcache to per-task security struct")
---
 security/selinux/hooks.c          | 25 +++++++++++++++----------
 security/selinux/include/objsec.h |  3 ++-
 2 files changed, 17 insertions(+), 11 deletions(-)
Quoted 5 lines

Of course I hit send on my reply to your v1 patch only to see a v2 in my inbox :)

Do you have a quick changelog of the changes between v1 and v2? It looks like the inode permission code changed ...

v2 splits out the enforcing_enabled && !avdc->permissive test from the unlikely(denied) test because we only set rc = -EACCES if enforcing but we want to use the auditdeny vector for computing audited regardless if this is a permission denial.

Quoted 83 lines
Quoted 79 lines
security/selinux/hooks.c +15 -10
index 6ef9c1aade65..c279ebdb3a33 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,7 +3203,8 @@ 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].auditallow = avd->auditallow;
+       tsec->avdcache.dir[spot].auditdeny = avd->auditdeny;
        tsec->avdcache.dir[spot].allowed = avd->allowed;
        tsec->avdcache.dir[spot].permissive = avd->flags & AVD_FLAGS_PERMISSIVE;
        tsec->avdcache.permissive_neveraudit =
@@ -3249,11 +3248,17 @@ 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))
-                       rc = -EACCES;
+               if (unlikely(denied)) {
+                       if (enforcing_enabled() && !avdc->permissive)
+                               rc = -EACCES;
+                       audited = denied & avdc->auditdeny;
+                       if ((requested & MAY_ACCESS) &&
+                               !(FILE__AUDIT_ACCESS & avdc->auditdeny))
+                               audited = 0;
+               } else {
+                       audited = requested & avdc->auditallow;
+               }
        } else {
                struct av_decision avd;
 
@@ -3263,7 +3268,7 @@ static int selinux_inode_permission(struct inode *inode, int requested)
                audited = avc_audit_required(perms, &avd, rc,
                        (requested & MAY_ACCESS) ? FILE__AUDIT_ACCESS : 0,
                        &denied);
-               task_avdcache_update(tsec, isec, &avd, audited);
+               task_avdcache_update(tsec, isec, &avd);
        }
 
        if (likely(!audited))
security/selinux/include/objsec.h +2 -2
index 5bddd28ea5cb..baf7564c6a2f 100644
@@ -33,7 +33,8 @@
 struct avdc_entry {
        u32 isid; /* inode SID */
        u32 allowed; /* allowed permission bitmask */
-       u32 audited; /* audited permission bitmask */
+       u32 auditallow; /* auditallow permission bitmask */
+       u32 auditdeny; /* auditdeny aka ~dontaudit permission bitmask */
        bool permissive; /* AVC permissive flag */
 };
 
--
Quoted 1 line

2.53.0

--
paul-moore.com