Skip to contentSkip to conversation

SELinux: Cleanup the secid/secctx conversion functions

RFC PATCH In review Paul Moore 5 participants 10 messages Feb 28, 2008 – Feb 29, 2008 SELinux security module

lore
Share & tools
lore

Paul Moore

author
#0 lore raw

RFC PATCH 1/1 SELinux: Cleanup the secid/secctx conversion functions

While looking at the SELinux secid/secctx conversion functions I realized they could probably do with a little cleanup to reduce the amount of code and make better use of existing string processing functions in the kernel. Making use of the kernel's existing string processing functions is a good idea as many architectures have specialized/optimized routines which should be an improvement over the generic code in the SELinux security server.
---

 security/selinux/ss/mls.c      |   61 +++++--------
 security/selinux/ss/mls.h      |    3 -
 security/selinux/ss/services.c |  194 ++++++++++++++++------------------------
 3 files changed, 103 insertions(+), 155 deletions(-)
security/selinux/ss/mls.c +23 -38
index feaf0a5..ba211b4 100644
@@ -239,8 +239,7 @@ int mls_context_isvalid(struct policydb *p, struct context *c)
  * Policy read-lock must be held for sidtab lookup.
  *
  */
-int mls_context_to_sid(char oldc,
-		       char **scontext,
+int mls_context_to_sid(char **scontext,
 		       struct context *context,
 		       struct sidtab *s,
 		       u32 def_sid)
@@ -250,10 +249,10 @@ int mls_context_to_sid(char oldc,
 	char *scontextp, *p, *rngptr;
 	struct level_datum *levdatum;
 	struct cat_datum *catdatum, *rngdatum;
-	int l, rc = -EINVAL;
+	int l, rc;
 
 	if (!selinux_mls_enabled) {
-		if (def_sid != SECSID_NULL && oldc)
+		if (def_sid != SECSID_NULL && *scontext && **scontext)
 			*scontext += strlen(*scontext)+1;
 		return 0;
 	}
@@ -262,18 +261,17 @@ int mls_context_to_sid(char oldc,
 	 * No MLS component to the security context, try and map to
 	 * default if provided.
 	 */
-	if (!oldc) {
+	if (!(*scontext) || !(**scontext)) {
 		struct context *defcon;
 
 		if (def_sid == SECSID_NULL)
-			goto out;
+			return -EINVAL;
 
 		defcon = sidtab_search(s, def_sid);
 		if (!defcon)
-			goto out;
+			return -EINVAL;
 
-		rc = mls_context_cpy(context, defcon);
-		goto out;
+		return mls_context_cpy(context, defcon);
 	}
 
 	/* Extract low sensitivity. */
@@ -287,10 +285,8 @@ int mls_context_to_sid(char oldc,
 
 	for (l = 0; l < 2; l++) {
 		levdatum = hashtab_search(policydb.p_levels.table, scontextp);
-		if (!levdatum) {
-			rc = -EINVAL;
-			goto out;
-		}
+		if (!levdatum)
+			return -EINVAL;
 
 		context->range.level[l].sens = levdatum->level->sens;
 
@@ -312,35 +308,29 @@ int mls_context_to_sid(char oldc,
 
 				catdatum = hashtab_search(policydb.p_cats.table,
 				                          scontextp);
-				if (!catdatum) {
-					rc = -EINVAL;
-					goto out;
-				}
+				if (!catdatum)
+					return -EINVAL;
 
 				rc = ebitmap_set_bit(&context->range.level[l].cat,
 				                     catdatum->value - 1, 1);
 				if (rc)
-					goto out;
+					return rc;
 
 				/* If range, set all categories in range */
 				if (rngptr) {
 					int i;
 
 					rngdatum = hashtab_search(policydb.p_cats.table, rngptr);
-					if (!rngdatum) {
-						rc = -EINVAL;
-						goto out;
-					}
+					if (!rngdatum)
+						return -EINVAL;
 
-					if (catdatum->value >= rngdatum->value) {
-						rc = -EINVAL;
-						goto out;
-					}
+					if (catdatum->value >= rngdatum->value)
+						return -EINVAL;
 
 					for (i = catdatum->value; i < rngdatum->value; i++) {
 						rc = ebitmap_set_bit(&context->range.level[l].cat, i, 1);
 						if (rc)
-							goto out;
+							return rc;
 					}
 				}
 
@@ -366,12 +356,10 @@ int mls_context_to_sid(char oldc,
 		rc = ebitmap_cpy(&context->range.level[1].cat,
 				 &context->range.level[0].cat);
 		if (rc)
-			goto out;
+			return rc;
 	}
 	*scontext = ++p;
-	rc = 0;
-out:
-	return rc;
+	return 0;
 }
 
 /*
@@ -391,13 +379,10 @@ int mls_from_string(char *str, struct context *context, gfp_t gfp_mask)
 	/* we need freestr because mls_context_to_sid will change
 	   the value of tmpstr */
 	tmpstr = freestr = kstrdup(str, gfp_mask);
-	if (!tmpstr) {
-		rc = -ENOMEM;
-	} else {
-		rc = mls_context_to_sid(':', &tmpstr, context,
-		                        NULL, SECSID_NULL);
-		kfree(freestr);
-	}
+	if (!tmpstr)
+		return -ENOMEM;
+	rc = mls_context_to_sid(&tmpstr, context, NULL, SECSID_NULL);
+	kfree(freestr);
 
 	return rc;
 }
security/selinux/ss/mls.h +1 -2
index ab53663..b364f61 100644
@@ -30,8 +30,7 @@ int mls_context_isvalid(struct policydb *p, struct context *c);
 int mls_range_isvalid(struct policydb *p, struct mls_range *r);
 int mls_level_isvalid(struct policydb *p, struct mls_level *l);
 
-int mls_context_to_sid(char oldc,
-	               char **scontext,
+int mls_context_to_sid(char **scontext,
 		       struct context *context,
 		       struct sidtab *s,
 		       u32 def_sid);
security/selinux/ss/services.c +79 -115

280 lines not on this page · raw message

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 6 lines · Paul Moore

While looking at the SELinux secid/secctx conversion functions I realized they could probably do with a little cleanup to reduce the amount of code and make better use of existing string processing functions in the kernel. Making use of the kernel's existing string processing functions is a good idea as many architectures have specialized/optimized routines which should be an improvement over the generic code in the SELinux security server.

Please always include a signed-off-by line, in case the patch gets applied.

security/selinux/ss/services.c out: · line 589

Quoted 3 lines
security/selinux/ss/services.c +2 -1
-static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
+static int context_struct_to_string(struct context *context,
+				    char **scontext, u32 *scontext_len)

I tend to not prefer the 80-column rule for function declarations, as it makes the code harder to grep (not a show-stopper, but generally, don't try and fix these).

- James

Signature · 7 lines
James Morris
<[email protected]>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#2 lore raw
Quoted 11 lines · James Morris
Quoted 7 lines · Paul Moore

While looking at the SELinux secid/secctx conversion functions I realized they could probably do with a little cleanup to reduce the amount of code and make better use of existing string processing functions in the kernel. Making use of the kernel's existing string processing functions is a good idea as many architectures have specialized/optimized routines which should be an improvement over the generic code in the SELinux security server.

Please always include a signed-off-by line, in case the patch gets applied.

Not including my sign-off is intentional as I've found it the only reliable to keep patches from being merged upstream before I'm happy with them. In this particular case I've only booted the patch once or twice and was posting it to see if others felt it worthwhile before I spent any more time on it.

Quoted 8 lines

security/selinux/ss/services.c out: · line 589

Quoted 4 lines

-static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len) +static int context_struct_to_string(struct context *context,

security/selinux/ss/services.c +1 -0
+				    char **scontext, u32 *scontext_len)

I tend to not prefer the 80-column rule for function declarations, as it makes the code harder to grep (not a show-stopper, but generally, don't try and fix these).

I find myself sticking pretty religiously to the 80-column rule, but you make a valid point that if I'm not changing the arguments I probably shouldn't mess with them. I'll fix that up.

Signature · 7 lines
paul moore
linux security @ hp

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#3 lore raw
In reply to Paul Moore
Quoted 8 lines · Paul Moore

While looking at the SELinux secid/secctx conversion functions I realized they could probably do with a little cleanup to reduce the amount of code and make better use of existing string processing functions in the kernel. Making use of the kernel's existing string processing functions is a good idea as many architectures have specialized/optimized routines which should be an improvement over the generic code in the SELinux security server.
---

Quoted 5 lines
 security/selinux/ss/mls.c      |   61 +++++--------
 security/selinux/ss/mls.h      |    3 -
 security/selinux/ss/services.c |  194 ++++++++++++++++------------------------
 3 files changed, 103 insertions(+), 155 deletions(-)

The snippet below looks like a step backward rather than an improvement
- single sprintf replaced by series of strcat calls. That can't be more efficient.

Quoted 4 lines
security/selinux/ss/services.c +0 -0
index f374186..4aab8a5 100644

<snip>

Quoted 20 lines
security/selinux/ss/services.c +6 -14
-	/*
-	 * Copy the user name, role name and type name into the context.
-	 */
-	sprintf(scontextp, "%s:%s:%s",
-		policydb.p_user_val_to_name[context->user - 1],
-		policydb.p_role_val_to_name[context->role - 1],
-		policydb.p_type_val_to_name[context->type - 1]);
-	scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
-	             1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
-	             1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
-
-	mls_sid_to_context(context, &scontextp);
-
-	*scontextp = 0;
+	ctx = *scontext;
+	strcpy(ctx, policydb.p_user_val_to_name[context->user - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_role_val_to_name[context->role - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_type_val_to_name[context->type - 1]);
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#4 lore raw
Quoted 15 lines · Stephen Smalley
Quoted 8 lines · Paul Moore

While looking at the SELinux secid/secctx conversion functions I realized they could probably do with a little cleanup to reduce the amount of code and make better use of existing string processing functions in the kernel. Making use of the kernel's existing string processing functions is a good idea as many architectures have specialized/optimized routines which should be an improvement over the generic code in the SELinux security server.
---

Quoted 5 lines
 security/selinux/ss/mls.c      |   61 +++++--------
 security/selinux/ss/mls.h      |    3 -
 security/selinux/ss/services.c |  194 ++++++++++++++++------------------------
 3 files changed, 103 insertions(+), 155 deletions(-)
Quoted 3 lines

The snippet below looks like a step backward rather than an improvement
- single sprintf replaced by series of strcat calls. That can't be more efficient.

Hmm...well, maybe I'm wrong (after looking at the implementations). Pity that Linux doesn't have stpcpy (as in glibc) - that is much nicer than a series of strcat's since it returns the end pointer and doesn't require finding the end of string each time.

Quoted 27 lines
Quoted 5 lines
security/selinux/ss/services.c +0 -0
index f374186..4aab8a5 100644

<snip>

Quoted 20 lines
security/selinux/ss/services.c +6 -14
-	/*
-	 * Copy the user name, role name and type name into the context.
-	 */
-	sprintf(scontextp, "%s:%s:%s",
-		policydb.p_user_val_to_name[context->user - 1],
-		policydb.p_role_val_to_name[context->role - 1],
-		policydb.p_type_val_to_name[context->type - 1]);
-	scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
-	             1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
-	             1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
-
-	mls_sid_to_context(context, &scontextp);
-
-	*scontextp = 0;
+	ctx = *scontext;
+	strcpy(ctx, policydb.p_user_val_to_name[context->user - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_role_val_to_name[context->role - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_type_val_to_name[context->type - 1]);
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#5 lore raw
Quoted 16 lines · Stephen Smalley
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · Paul Moore

While looking at the SELinux secid/secctx conversion functions I realized they could probably do with a little cleanup to reduce the amount of code and make better use of existing string processing functions in the kernel. Making use of the kernel's existing string processing functions is a good idea as many architectures have specialized/optimized routines which should be an improvement over the generic code in the SELinux security server. ---

 security/selinux/ss/mls.c      |   61 +++++--------
 security/selinux/ss/mls.h      |    3 -
 security/selinux/ss/services.c |  194

++++++++++++++++------------------------ 3 files changed, 103 insertions(+), 155 deletions(-)

Quoted 5 lines
Quoted 3 lines

The snippet below looks like a step backward rather than an improvement
- single sprintf replaced by series of strcat calls. That can't be more efficient.

Hmm...well, maybe I'm wrong (after looking at the implementations).

That is exactly why I decided the multiple strcpy()/strcat() calls would be faster. Our formatting needs here are pretty simple and the kernel's sprintf() implementation looks very involved versus strcpy()/strcat().

Quoted 3 lines

Pity that Linux doesn't have stpcpy (as in glibc) - that is much nicer than a series of strcat's since it returns the end pointer and doesn't require finding the end of string each time.

That would be nice here. However, I think at least moving away from sprintf() should yield an advantage.

Signature · 7 lines
paul moore
linux security @ hp

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#6 lore raw
In reply to Stephen Smalley
Quoted 9 lines · Stephen Smalley
Quoted 3 lines · Stephen Smalley

The snippet below looks like a step backward rather than an improvement
- single sprintf replaced by series of strcat calls. That can't be more efficient.

Hmm...well, maybe I'm wrong (after looking at the implementations). Pity that Linux doesn't have stpcpy (as in glibc) - that is much nicer than a series of strcat's since it returns the end pointer and doesn't require finding the end of string each time.

Note that you can do (only slightly abusing the interface):

	ctx = *scontext;

	ctx += strlcpy(ctx, policydb.p_user_val_to_name[context->user - 1], -1);
	ctx += strlcpy(ctx, ":", -1);
	ctx += strlcpy(ctx, policydb.p_role_val_to_name[context->role - 1], -1);
	ctx += strlcpy(ctx, ":", -1);
	ctx += strlcpy(ctx, policydb.p_type_val_to_name[context->type - 1], -1);

...which is basically a memcpy() with a simple if test.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
security/selinux/ss/services.c +6 -1
-	*scontextp = 0;
+	ctx = *scontext;
+	strcpy(ctx, policydb.p_user_val_to_name[context->user - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_role_val_to_name[context->role - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_type_val_to_name[context->type - 1]);
Signature · 2 lines
James Antill <[email protected]>
Red Hat
author
#7 lore raw
Quoted 20 lines · James Antill
Quoted 9 lines · Stephen Smalley
Quoted 3 lines · Stephen Smalley

The snippet below looks like a step backward rather than an improvement - single sprintf replaced by series of strcat calls. That can't be more efficient.

Hmm...well, maybe I'm wrong (after looking at the implementations). Pity that Linux doesn't have stpcpy (as in glibc) - that is much nicer than a series of strcat's since it returns the end pointer and doesn't require finding the end of string each time.

Note that you can do (only slightly abusing the interface):

	ctx = *scontext;

	ctx += strlcpy(ctx, policydb.p_user_val_to_name[context->user - 1],
-1); ctx += strlcpy(ctx, ":", -1);
	ctx += strlcpy(ctx, policydb.p_role_val_to_name[context->role - 1],
-1); ctx += strlcpy(ctx, ":", -1);
	ctx += strlcpy(ctx, policydb.p_type_val_to_name[context->type - 1],
Quoted 3 lines

-1);

...which is basically a memcpy() with a simple if test.

... except for that strlen() call right at the top, there are also two comparisons (you only accounted for one) and a add/subtract operation :)

[NOTE: I'm comparing the architecture independent versions of these functions]

The strlcpy() implementation does a strlen() and a memcpy() call along with two comparisons and some trivial math. The strcat() implementation does pretty much the same thing but with less comparisons and no additional math. The strcpy() implementation only does a byte-by-byte copy which should be faster then strlcpy() even when one factors in an optimized memcpy() implementation due to the strlen() call.

My conclusion is that the strcpy()/strcat() approach should be at least as fast as the strlcpy() approach if not a smidge faster.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
security/selinux/ss/services.c +6 -1
-	*scontextp = 0;
+	ctx = *scontext;
+	strcpy(ctx, policydb.p_user_val_to_name[context->user - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_role_val_to_name[context->role - 1]);
+	strcat(ctx, ":");
+	strcat(ctx, policydb.p_type_val_to_name[context->type - 1]);
Signature · 7 lines
paul moore
linux security @ hp

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#8 lore raw
Quoted 7 lines · Paul Moore

The strlcpy() implementation does a strlen() and a memcpy() call along with two comparisons and some trivial math. The strcat() implementation does pretty much the same thing but with less comparisons and no additional math. The strcpy() implementation only does a byte-by-byte copy which should be faster then strlcpy() even when one factors in an optimized memcpy() implementation due to the strlen() call.

The difference is that with strcat() you are also finding the length of dest whereas which strlcpy() you only need to find the length of src. I really doubt it makes a measurable difference and things may be muddied by gcc inlining some of the standard str* functions.

However, you definately do not need the final strlen(ctx) in either case since you know that *scontext_len - 1 == strlen(ctx).

I'm not sanctioning James' abuse of strlcpy(); I think it is pointless to try and guess which is faster without actually benchmarking it.

- todd

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#9 lore raw
Quoted 9 lines · Todd Miller
Quoted 7 lines · Paul Moore

The strlcpy() implementation does a strlen() and a memcpy() call along with two comparisons and some trivial math. The strcat() implementation does pretty much the same thing but with less comparisons and no additional math. The strcpy() implementation only does a byte-by-byte copy which should be faster then strlcpy() even when one factors in an optimized memcpy() implementation due to the strlen() call.

Quoted 3 lines

The difference is that with strcat() you are also finding the length of dest whereas which strlcpy() you only need to find the length of src.

True, strcat() does do a strlen() on dest while strlcpy() does a strlen() on src and in this case dest is almost always going to be bigger then src.

Quoted 2 lines

I really doubt it makes a measurable difference and things may be muddied by gcc inlining some of the standard str* functions.

Agree.

Also, (this starts getting into architecture specifics) on x86 (not x86_64) I'm pretty sure all function arguments are passed on the stack and strlcpy() requires three arguments versus strcat()/strcpy() which require only two. However, at this point I think the argument is starting to get a bit ridiculous.

Quoted 2 lines

However, you definately do not need the final strlen(ctx) in either case since you know that *scontext_len - 1 == strlen(ctx).

Disagree. At the point in time of the final strlen(ctx) call ctx looks like this (note the lack of MLS range):

user_u:role_r:type_t

... where as scontext_len is equal to the strlen() of the full context (MLS included). The results of strlen(*scontext) and *scontext_len are not the same until after the call to mls_sid_to_context().

Quoted 3 lines

I'm not sanctioning James' abuse of strlcpy(); I think it is pointless to try and guess which is faster without actually benchmarking it.

Yep ... slow news day and this discussion seems a lot safer then the current labeled NFS discussion ;)

Signature · 7 lines
paul moore
linux security @ hp

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.