Skip to contentSkip to conversation

selinux: export validatetrans decisions

PATCH Superseded Andrew Perepechko 2 participants 6 messages Oct 27, 2015 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: export validatetrans decisions

Make validatetrans decisions available through selinuxfs. "/transition" is added to selinuxfs for this purpose. This functionality is needed by file system servers implemented in userspace or kernelspace without the VFS layer.

Writing "$oldcontext $newcontext $tclass $taskcontext" to /transition is expected to return 0 if the transition is allowed and -EPERM otherwise.

---
 security/selinux/hooks.c            |  2 +-
 security/selinux/include/security.h |  2 +-
 security/selinux/selinuxfs.c        | 81 +++++++++++++++++++++++++++++++++++++
 security/selinux/ss/services.c      | 20 +++++----
 4 files changed, 95 insertions(+), 10 deletions(-)
security/selinux/hooks.c +1 -1
index e4369d8..f40f4b4 100644
@@ -3043,7 +3043,7 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
 		return rc;
 
 	rc = security_validate_transition(isec->sid, newsid, sid,
-					  isec->sclass);
+					  isec->sclass, 0);
 	if (rc)
 		return rc;
 
security/selinux/include/security.h +1 -1
index 6a681d2..84dec31 100644
@@ -183,7 +183,7 @@ int security_node_sid(u16 domain, void *addr, u32 addrlen,
 	u32 *out_sid);
 
 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
-				 u16 tclass);
+				 u16 tclass, int user);
 
 int security_bounded_transition(u32 oldsid, u32 newsid);
 
security/selinux/selinuxfs.c +81 -0

99 lines not on this page · raw message

security/selinux/ss/services.c +12 -8
index b7df12b..d3058bf 100644
@@ -750,7 +750,7 @@ static void context_struct_compute_av(struct context *scontext,
 				 tclass, avd);
 }
 
-static int security_validtrans_handle_fail(struct context *ocontext,
+static void security_validtrans_audit_fail(struct context *ocontext,
 					   struct context *ncontext,
 					   struct context *tcontext,
 					   u16 tclass)
@@ -772,14 +772,10 @@ out:
 	kfree(o);
 	kfree(n);
 	kfree(t);
-
-	if (!selinux_enforcing)
-		return 0;
-	return -EPERM;
 }
 
 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
-				 u16 orig_tclass)
+				 u16 orig_tclass, int user)
 {
 	struct context *ocontext;
 	struct context *ncontext;
@@ -832,8 +828,16 @@ int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
 	while (constraint) {
 		if (!constraint_expr_eval(ocontext, ncontext, tcontext,
 					  constraint->expr)) {
-			rc = security_validtrans_handle_fail(ocontext, ncontext,
-							     tcontext, tclass);
+			if (!user)
+				security_validtrans_audit_fail(ocontext,
+								ncontext,
+								tcontext,
+								tclass);
+			if (!selinux_enforcing && !user)
+				rc = 0;
+			else
+				rc = -EPERM;
+
 			goto out;
 		}
 		constraint = constraint->next;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 24 lines · Andrew Perepechko

Make validatetrans decisions available through selinuxfs. "/transition" is added to selinuxfs for this purpose. This functionality is needed by file system servers implemented in userspace or kernelspace without the VFS layer.

Writing "$oldcontext $newcontext $tclass $taskcontext" to /transition is expected to return 0 if the transition is allowed and -EPERM otherwise.

---
  security/selinux/hooks.c            |  2 +-
  security/selinux/include/security.h |  2 +-
  security/selinux/selinuxfs.c        | 81 +++++++++++++++++++++++++++++++++++++
  security/selinux/ss/services.c      | 20 +++++----
  4 files changed, 95 insertions(+), 10 deletions(-)
security/selinux/hooks.c +0 -0
index e4369d8..f40f4b4 100644

security/selinux/hooks.c static int selinux_inode_setxattr(struct dentry *dentry, const char *name, · line 3046

Quoted 5 lines
security/selinux/hooks.c +1 -1
@@ -3043,7 +3043,7 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
  		return rc;
 
  	rc = security_validate_transition(isec->sid, newsid, sid,
-					  isec->sclass);
+					  isec->sclass, 0);

Follow the example of security_transition_sid(), i.e. introduce a _user interface() and re-factor the existing security_validate_transition() into a common helper that takes a bool argument.

Quoted 21 lines
security/selinux/hooks.c +0 -0
  	if (rc)
  		return rc;
 
security/selinux/include/security.h +1 -1
index 6a681d2..84dec31 100644
@@ -183,7 +183,7 @@ int security_node_sid(u16 domain, void *addr, u32 addrlen,
  	u32 *out_sid);
 
  int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
-				 u16 tclass);
+				 u16 tclass, int user);
 
  int security_bounded_transition(u32 oldsid, u32 newsid);
 
security/selinux/selinuxfs.c +0 -0
index 5bed7716..eb487ff 100644

security/selinux/selinuxfs.c enum sel_inos { · line 119

Quoted 4 lines
security/selinux/selinuxfs.c +1 -0
@@ -116,6 +116,7 @@ enum sel_inos {
  	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
  	SEL_STATUS,	/* export current status using mmap() */
  	SEL_POLICY,	/* allow userspace to read the in kernel policy */
+	SEL_TRANSITION,	/* compute validatetrans decision */

Could be confusing since it is for validate_transition not transition_sid. Probably ought to put validate into the name in some form.

Quoted 7 lines
security/selinux/selinuxfs.c +0 -0
  	SEL_INO_NEXT,	/* The next inode number to use */
  };
 
@@ -653,6 +654,85 @@ static const struct file_operations sel_checkreqprot_ops = {
  	.llseek		= generic_file_llseek,
  };
 

security/selinux/selinuxfs.c static const struct file_operations sel_checkreqprot_ops = { · line 666

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+static ssize_t sel_write_transition(struct file *file, const char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
+	char *req = NULL;
+	u32 osid, nsid, tsid;
+	u16 tclass;
+	int rc;
+
+	rc = task_has_security(current, SECURITY__COMPUTE_AV);

Just define a new permission in include/classmap.h (and in your policy, of course) and use it.

security/selinux/selinuxfs.c static const struct file_operations sel_checkreqprot_ops = { · line 672

Quoted 6 lines
security/selinux/selinuxfs.c +6 -0
+	if (rc)
+		goto out;
+
+	rc = -ENOMEM;
+	if (count >= PAGE_SIZE - 1)
+		goto out;

Why PAGE_SIZE-1?

Quoted 24 lines
security/selinux/selinuxfs.c +24 -0
+
+	/* No partial writes. */
+	rc = -EINVAL;
+	if (*ppos != 0)
+		goto out;
+
+	rc = -ENOMEM;
+	req = kzalloc(count + 1, GFP_KERNEL);
+	if (!req)
+		goto out;
+
+	rc = -EFAULT;
+	if (copy_from_user(req, buf, count))
+		goto out;
+
+	rc = -ENOMEM;
+	oldcon = kzalloc(count + 1, GFP_KERNEL);
+	if (!oldcon)
+		goto out;
+
+	newcon = kzalloc(count + 1, GFP_KERNEL);
+	if (!newcon)
+		goto out;
+

security/selinux/selinuxfs.c static const struct file_operations sel_checkreqprot_ops = { · line 706

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+	taskcon = kzalloc(count + 1, GFP_KERNEL);
+	if (!taskcon)
+		goto out;
+
+	rc = -EINVAL;
+	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
+		goto out;
+
+	rc = security_context_to_sid(oldcon, strlen(oldcon) + 1, &osid,
+				     GFP_KERNEL);

#next has security_context_str_to_sid() as a convenient helper for this.

Quoted 74 lines
security/selinux/selinuxfs.c +30 -0
+	if (rc)
+		goto out;
+
+	rc = security_context_to_sid(newcon, strlen(newcon) + 1, &nsid,
+				     GFP_KERNEL);
+	if (rc)
+		goto out;
+
+	rc = security_context_to_sid(taskcon, strlen(taskcon) + 1, &tsid,
+				     GFP_KERNEL);
+	if (rc)
+		goto out;
+
+	rc = security_validate_transition(osid, nsid, tsid, tclass, 1);
+	if (!rc)
+		rc = count;
+out:
+	kfree(req);
+	kfree(oldcon);
+	kfree(newcon);
+	kfree(taskcon);
+	return rc;
+}
+
+static const struct file_operations sel_transition_ops = {
+	.write		= sel_write_transition,
+	.llseek		= generic_file_llseek,
+};
+
  /*
   * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
   */
@@ -1767,6 +1847,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
  		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
  		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
  		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
+		[SEL_TRANSITION] = {"transition", &sel_transition_ops, S_IWUSR},
  		/* last one */ {""}
  	};
  	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
security/selinux/ss/services.c +2 -7
index b7df12b..d3058bf 100644
@@ -750,7 +750,7 @@ static void context_struct_compute_av(struct context *scontext,
  				 tclass, avd);
  }
 
-static int security_validtrans_handle_fail(struct context *ocontext,
+static void security_validtrans_audit_fail(struct context *ocontext,
  					   struct context *ncontext,
  					   struct context *tcontext,
  					   u16 tclass)
@@ -772,14 +772,10 @@ out:
  	kfree(o);
  	kfree(n);
  	kfree(t);
-
-	if (!selinux_enforcing)
-		return 0;
-	return -EPERM;
  }
 
  int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
-				 u16 orig_tclass)
+				 u16 orig_tclass, int user)
  {
  	struct context *ocontext;
  	struct context *ncontext;
@@ -832,8 +828,16 @@ int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  	while (constraint) {
  		if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  					  constraint->expr)) {
-			rc = security_validtrans_handle_fail(ocontext, ncontext,

security/selinux/ss/services.c int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid, · line 839

Quoted 10 lines
security/selinux/ss/services.c +9 -1
-							     tcontext, tclass);
+			if (!user)
+				security_validtrans_audit_fail(ocontext,
+								ncontext,
+								tcontext,
+								tclass);
+			if (!selinux_enforcing && !user)
+				rc = 0;
+			else
+				rc = -EPERM;

Hmm...in what situation don't you want it to reflect the kernel enforcing mode (i.e. when won't you just have your userspace file server end up checking security_getenforce() and ignore the error in that situation)? Userspace AVC is different since it is caching decisions from the kernel but still ends up honoring the kernel's enforcing status (unless you explicitly set it to use its own private one).

Beyond that, the things that you don't want to happen when called from userspace include not unmapping the class value and not printk'ing on an unrecognized class. See security_transition_sid_user() -> security_compute_sid() for example.

Quoted 5 lines
security/selinux/ss/services.c +1 -0
+
  			goto out;
  		}
  		constraint = constraint->next;
#2 lore raw

Hi Stephen!

Thank you for your the response.

Quoted 3 lines · On Tuesday 27 Oct 2015 14:15:44 Stephen Smalley wrote:

Follow the example of security_transition_sid(), i.e. introduce a _user interface() and re-factor the existing security_validate_transition() into a common helper that takes a bool argument.

OK.

Quoted 4 lines

Could be confusing since it is for validate_transition not transition_sid. Probably ought to put validate into the name in some form.

OK.

Quoted 4 lines

Just define a new permission in include/classmap.h (and in your policy, of course) and use it.

OK.

Quoted 9 lines

security/selinux/selinuxfs.c static const struct file_operations sel_checkreqprot_ops = { · line 672

Quoted 6 lines
security/selinux/selinuxfs.c +6 -0
+	if (rc)
+		goto out;
+
+	rc = -ENOMEM;
+	if (count >= PAGE_SIZE - 1)
+		goto out;

Why PAGE_SIZE-1?

This is to avoid allocation of more than a single page. kzalloc(count+1) will guarantee the string is zero-terminated.

The code below can be slightly optimized by modifying the copied string in-place, but I tried to follow the style used in neighbouring functions.

Quoted 3 lines

#next has security_context_str_to_sid() as a convenient helper for this.

OK.

Quoted 7 lines

Hmm...in what situation don't you want it to reflect the kernel enforcing mode (i.e. when won't you just have your userspace file server end up checking security_getenforce() and ignore the error in that situation)? Userspace AVC is different since it is caching decisions from the kernel but still ends up honoring the kernel's enforcing status (unless you explicitly set it to use its own private one).

We expected to have an option to be able to enforce the policy even if the server itself is running in permissive, but this is not a critical requirement, so I'll update this bit.

Quoted 6 lines

Beyond that, the things that you don't want to happen when called from userspace include not unmapping the class value and not printk'ing on an unrecognized class. See security_transition_sid_user() -> security_compute_sid() for example.

Sorry, you are right, indeed. :( This part somehow did not get in the final patch.

I'll update the patch according to your comments.

Andrew

#3 lore raw
Quoted 11 lines · Andrew Perepechko
Quoted 9 lines

security/selinux/selinuxfs.c static const struct file_operations sel_checkreqprot_ops = { · line 672

Quoted 6 lines
security/selinux/selinuxfs.c +6 -0
+	if (rc)
+		goto out;
+
+	rc = -ENOMEM;
+	if (count >= PAGE_SIZE - 1)
+		goto out;

Why PAGE_SIZE-1?

This is to avoid allocation of more than a single page.

Yes, but you don't need PAGE_SIZE - 1 for that. The check can just be >= PAGE_SIZE, as used elsewhere in selinuxfs.c.

Quoted 35 lines

kzalloc(count+1) will guarantee the string is zero-terminated.

The code below can be slightly optimized by modifying the copied string in-place, but I tried to follow the style used in neighbouring functions.

Quoted 3 lines

#next has security_context_str_to_sid() as a convenient helper for this.

OK.

Quoted 7 lines

Hmm...in what situation don't you want it to reflect the kernel enforcing mode (i.e. when won't you just have your userspace file server end up checking security_getenforce() and ignore the error in that situation)? Userspace AVC is different since it is caching decisions from the kernel but still ends up honoring the kernel's enforcing status (unless you explicitly set it to use its own private one).

We expected to have an option to be able to enforce the policy even if the server itself is running in permissive, but this is not a critical requirement, so I'll update this bit.

Quoted 6 lines

Beyond that, the things that you don't want to happen when called from userspace include not unmapping the class value and not printk'ing on an unrecognized class. See security_transition_sid_user() -> security_compute_sid() for example.

Sorry, you are right, indeed. :( This part somehow did not get in the final patch.

I'll update the patch according to your comments.

#4 lore raw
Quoted 7 lines · On Tuesday 27 Oct 2015 14:46:29 Stephen Smalley wrote:
Quoted 3 lines
Quoted 1 line

Why PAGE_SIZE-1?

This is to avoid allocation of more than a single page.

Yes, but you don't need PAGE_SIZE - 1 for that. The check can just be

Quoted 1 line

= PAGE_SIZE, as used elsewhere in selinuxfs.c.

A sequence of bytes passed to a write handler may not be 0-terminated, so it cannot be used directly as a scanf argument.

As far as I can see, compute_av and similar handlers use transaction ops, which have more or less similar condition:

char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)

{
        struct simple_transaction_argresp *ar;
        static DEFINE_SPINLOCK(simple_transaction_lock);

        if (size > SIMPLE_TRANSACTION_LIMIT - 1)
                return ERR_PTR(-EFBIG);

SIMPLE_TRANSACTION_LIMIT is PAGE_SIZE subtracted by sizeof(ssize_t).

Since the buffer is the result of get_zeroed_page(), it is guaranteed to be 0-terminated: [ssize_t][SIMPLE_TRANSACTION_LIMIT-1][\0].

On the other hand, e.g. sel_write_checkreqprot() does not look correct, since the only reason that it won't access beyond page boundary on incorrect 4096-byte input is if sscanf stops parsing a number after integer overflow happens.

#5 lore raw

Ah, you meant that it should be either > PAGE_SIZE - 1 or >= PAGE_SIZE, which are the same. I'll fix that.

Quoted 40 lines · On Tuesday 27 Oct 2015 22:25:50 Andrew Perepechko wrote:
Quoted 7 lines · On Tuesday 27 Oct 2015 14:46:29 Stephen Smalley wrote:
Quoted 3 lines
Quoted 1 line

Why PAGE_SIZE-1?

This is to avoid allocation of more than a single page.

Yes, but you don't need PAGE_SIZE - 1 for that. The check can just be

Quoted 1 line

= PAGE_SIZE, as used elsewhere in selinuxfs.c.

A sequence of bytes passed to a write handler may not be 0-terminated, so it cannot be used directly as a scanf argument.

As far as I can see, compute_av and similar handlers use transaction ops, which have more or less similar condition:

char *simple_transaction_get(struct file *file, const char __user *buf,

size_t size) {
        struct simple_transaction_argresp *ar;
        static DEFINE_SPINLOCK(simple_transaction_lock);

        if (size > SIMPLE_TRANSACTION_LIMIT - 1)
                return ERR_PTR(-EFBIG);

SIMPLE_TRANSACTION_LIMIT is PAGE_SIZE subtracted by sizeof(ssize_t).

Since the buffer is the result of get_zeroed_page(), it is guaranteed to be 0-terminated: [ssize_t][SIMPLE_TRANSACTION_LIMIT-1][\0].

On the other hand, e.g. sel_write_checkreqprot() does not look correct, since the only reason that it won't access beyond page boundary on incorrect 4096-byte input is if sscanf stops parsing a number after integer overflow happens.

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].