Skip to contentSkip to conversation

Behavior of extended permissions

RFC PATCH Superseded bauen1 3 participants 8 messages Sep 10, 2020 – Oct 28, 2020 SELinux security module

lore
Share & tools
lore

bauen1

author
#0 lore raw

Hi,

In my policy I'm currently trying to make use ioctl whitelists. In some discussion with grift, that sparked https://lore.kernel.org/selinux/[email protected]/T/#t we came across various inconsistencies.

First some observations about the current behavior:

allowx rules requires a related allow rule to permit access:

; E.g. to allow access to ioctlcmd=0x5401 but no other ioctl
(allow test.type test1.type (file (ioctl))) ; if this rule is removed access is forbidden (allowx test.type test1.type (ioctl file (0x5401)))

dontauditx rules don't seem to work, they require an dontaudit rule but then every ioctl will be audited.

auditallowx rules might have the same problem.

neverallowx rules treat allow rules without related allowx rules as allowing access to all extended permissions:

; Will error
(allow test.type test1.type (file (ioctl))) (neverallowx test.type test1.type (ioctl file (not (0x5401))))

; Will compile fine
(allow test.type test2.type (file (ioctl)))
(allowx test.type test2.type (ioctl file (0x5401))) (neverallowx test.type test2.type (ioctl file (not (0x5401))))

Constraints are checked when the kernel checks for access to the normal permission (file (ioctl)).

After looking at the CIL docs, it appears that the intended behavior is that a dontauditx / auditallowx rule will only apply to the extended permission it covers.

; Only access with ioctlcmd=0x5401 will be audited, but nothing else (auditallowx test.type test1.type (ioctl file (0x5401)))

; Only access with ioctlcmd=0x5401 will be hidden, but nothing else (dontauditx test.type test1.type (ioctl file (0x5401)))

Of the behavior of allowx, dontauditx, auditallowx, neverallowx, I find that neverallowx has the most intuitive and useful behavior. Checking of constraints against (class (ioctl)) even when checking extended permissions is also very useful due to the nature of ioctls. The behavior of dontauditx and auditallowx appears to be broken making them useless.

Signature · 2 lines
bauen1
https://dn42.bauen1.xyz/
author
#1 lore raw
Quoted 3 lines

dontauditx rules don't seem to work, they require an dontaudit rule but then every ioctl will be audited.

auditallowx rules might have the same problem.

After a bit more digging, dontauditx and auditallowx rules only take effect if at least 1 allowx rule is defined. And auditallowx rules appear to require an additional auditallow rule.

It is more useful if dontauditx and auditallowx follow the behavior of neverallowx, i.e. take effect by themself without requiring any additional rules. A side effect of this is possibly that dontauditx or auditallowx rules will enable extended permission checks even if no allowx rule is present.

For example I want to dontaudit the very common TCGETS ioctl when giving a domain read access to a file. At the same time I want to know about every other ioctl issued against the file, they could be very important. Currently I would actually have to add an allowx rule to do that.

I might be able to provide kernel patches.

Signature · 2 lines
bauen1
https://dn42.bauen1.xyz/
author
#2 lore raw

RFC PATCH 1/1 selinux: allow dontauditx rules to take effect without allowx

This allows for dontauditing very specific ioctls e.g. TCGETS without dontauditing every ioctl or granting additional permissions.

Now either an allowx, dontauditx or auditallowx rules enables checking for extended permissions.

Dontaudit rules take precedence over dontauditx rules and auditallowx rules take precedence over auditallow rules.

---
 security/selinux/avc.c         | 12 ++++++++----
 security/selinux/ss/services.c |  4 +---
 2 files changed, 9 insertions(+), 7 deletions(-)
security/selinux/avc.c +8 -4
index 3c05827608b6..ad5b2e3b5abb 100644
@@ -402,10 +402,14 @@ static inline u32 avc_xperms_audit_required(u32 requested,
 	} else if (result) {
 		audited = denied = requested;
 	} else {
-		audited = requested & avd->auditallow;
-		if (audited && xpd) {
-			if (!avc_xperms_has_perm(xpd, perm, XPERMS_AUDITALLOW))
-				audited &= ~requested;
+		if (xpd) {
+			if (avc_xperms_has_perm(xpd, perm, XPERMS_AUDITALLOW)) {
+				audited = requested;
+			} else {
+				audited = 0;
+			}
+		} else {
+			audited = requested & avd->auditallow;
 		}
 	}
 
security/selinux/ss/services.c +1 -3
index 9704c8a32303..597b79703584 100644
@@ -596,9 +596,7 @@ void services_compute_xperms_drivers(
 					node->datum.u.xperms->driver);
 	}
 
-	/* If no ioctl commands are allowed, ignore auditallow and auditdeny */
-	if (node->key.specified & AVTAB_XPERMS_ALLOWED)
-		xperms->len = 1;
+	xperms->len = 1;
 }
 
 /*

--
2.28.0

maintainer
#3 lore raw

Re: [RFC PATCH] selinux: allow dontauditx rules to take effect without allowx

Quoted 9 lines · On Sat, Sep 12, 2020 at 3:54 PM bauen1 <[email protected]> wrote:

This allows for dontauditing very specific ioctls e.g. TCGETS without dontauditing every ioctl or granting additional permissions.

Now either an allowx, dontauditx or auditallowx rules enables checking for extended permissions.

Dontaudit rules take precedence over dontauditx rules and auditallowx rules take precedence over auditallow rules.

I'm not following why you are providing different precedence for dontauditx vs auditallowx.
Regardless, since this changes the semantics of such rules I'll need confirmation from Android that they want this change in behavior since they are the original developers of the ioctl whitelisting support and its primary users to date. We may also need to make the change conditional on a policy capability if backward compatibility is an issue. However, I suspect no one has been relying on the current behavior for dontauditx and auditallowx.

author
#4 lore raw

Re: [RFC PATCH] selinux: allow dontauditx rules to take effect without allowx

Quoted 5 lines · Stephen Smalley
Quoted 4 lines · On Sat, Sep 12, 2020 at 3:54 PM bauen1 <[email protected]> wrote:

This allows for dontauditing very specific ioctls e.g. TCGETS without dontauditing every ioctl or granting additional permissions.

Quoted 8 lines
Quoted 5 lines

Now either an allowx, dontauditx or auditallowx rules enables checking for extended permissions.

Dontaudit rules take precedence over dontauditx rules and auditallowx rules take precedence over auditallow rules.

I'm not following why you are providing different precedence for dontauditx vs auditallowx.

I selected this because I thought it is the most useful.
I think my original take was that with dontaudit you want to be broad if necessary, but with auditallowx you want to be specific. But now I'm not sure if the precedence of auditallow in the RFC is actually good. At least the precedence of dontaudit/dontauditx is good because it doesn't change the behavior of dontaudit in any (unexpected) way.
I will probably change it in a v2.

Quoted 4 lines

Regardless, since this changes the semantics of such rules I'll need confirmation from Android that they want this change in behavior since they are the original developers of the ioctl whitelisting support and its primary users to date.

I've copied Jeff Vander Stoep since he submitted the original patch, I don't know anyone else involved with this but I see you also added Nick Kralevich.

Quoted 5 lines

We may also need to make the change
conditional on a policy capability if backward compatibility is an issue. However, I suspect no one has been relying on the current behavior for dontauditx and auditallowx.

This would break any policy that relies on the old behavior that dontauditx/auditallowx don't enable extended permission checks. If a policy does require this behavior it will grant less access. But at the same time I have yet to find any policy other than seandroid that actually utilizes extended permissions and even it only has 2 dontauditxperm statements (at least that is what a grep of a recent checkout found).

Signature · 2 lines
bauen1
https://dn42.bauen1.xyz/
maintainer
#5 lore raw

Re: [RFC PATCH] selinux: allow dontauditx rules to take effect without allowx

Quoted 12 lines · On Mon, Sep 14, 2020 at 2:49 PM bauen1 <[email protected]> wrote:
Quoted 11 lines · Stephen Smalley
Quoted 9 lines · On Sat, Sep 12, 2020 at 3:54 PM bauen1 <[email protected]> wrote:

This allows for dontauditing very specific ioctls e.g. TCGETS without dontauditing every ioctl or granting additional permissions.

Now either an allowx, dontauditx or auditallowx rules enables checking for extended permissions.

Dontaudit rules take precedence over dontauditx rules and auditallowx rules take precedence over auditallow rules.

Quoted 7 lines
Quoted 2 lines

I'm not following why you are providing different precedence for dontauditx vs auditallowx.

I selected this because I thought it is the most useful.
I think my original take was that with dontaudit you want to be broad if necessary, but with auditallowx you want to be specific. But now I'm not sure if the precedence of auditallow in the RFC is actually good. At least the precedence of dontaudit/dontauditx is good because it doesn't change the behavior of dontaudit in any (unexpected) way.
I will probably change it in a v2.

I think that (dropping the precedence changes) is a good idea at this point. Let's focus on the change to services_compute_xperms_drivers() as I suspect this is the bigger issue.

Quoted 6 lines
Quoted 4 lines

Regardless, since this changes the semantics of such rules I'll need confirmation from Android that they want this change in behavior since they are the original developers of the ioctl whitelisting support and its primary users to date.

I've copied Jeff Vander Stoep since he submitted the original patch, I don't know anyone else involved with this but I see you also added Nick Kralevich.

We really should hear from the Android folks on this as they are probably the biggest user of the xperms code. I'm a little surprised and disappointed that we haven't heard from them yet, but they may be out of the office at the moment. I would suggest posting a v2 patch as you mentioned above and we'll see if we can get the attention of the Android folks.

Signature · 2 lines
paul moore
www.paul-moore.com
author
#6 lore raw

PATCH v2 1/1 selinux: allow dontauditx and auditallowx rules to take effect without allowx

This allows for dontauditing very specific ioctls e.g. TCGETS without dontauditing every ioctl or granting additional permissions.

Now either an allowx, dontauditx or auditallowx rules enables checking for extended permissions.

---

v2: dropped the precedence change, I will make my case for that seperately.

 security/selinux/ss/services.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
security/selinux/ss/services.c +1 -3
index 9704c8a32303..597b79703584 100644
@@ -596,9 +596,7 @@ void services_compute_xperms_drivers(
 					node->datum.u.xperms->driver);
 	}
 
-	/* If no ioctl commands are allowed, ignore auditallow and auditdeny */
-	if (node->key.specified & AVTAB_XPERMS_ALLOWED)
-		xperms->len = 1;
+	xperms->len = 1;
 }
 
 /*

--
2.28.0

maintainer
#7 lore raw

Re: [PATCH v2] selinux: allow dontauditx and auditallowx rules to take effect without allowx

Quoted 7 lines · On Fri, Oct 9, 2020 at 8:47 AM bauen1 <[email protected]> wrote:

This allows for dontauditing very specific ioctls e.g. TCGETS without dontauditing every ioctl or granting additional permissions.

Now either an allowx, dontauditx or auditallowx rules enables checking for extended permissions.

Quoted 7 lines

---

v2: dropped the precedence change, I will make my case for that seperately.

 security/selinux/ss/services.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

I feel that the Android folks have had plenty of time to comment on this, and they haven't, so I'm going to go ahead and merge this since it seems to make sense. Thanks for your patience Jonathan.

I think The SELinux Notebook probably needs an update with respect to this patch, mentioning that in Linux v5.10 the xperms auditing behavior changed. Can you submit a patch for that as well?

Quoted 17 lines
security/selinux/ss/services.c +1 -4
index 9704c8a32303..597b79703584 100644
@@ -596,9 +596,7 @@ void services_compute_xperms_drivers(
                                        node->datum.u.xperms->driver);
        }
 
-       /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
-       if (node->key.specified & AVTAB_XPERMS_ALLOWED)
-               xperms->len = 1;
+       xperms->len = 1;
 }
 
 /*
--

2.28.0

Signature · 2 lines
paul moore
www.paul-moore.com