Skip to contentSkip to conversation

Fix MLS parsing of users

SEMANAGE Ivan Gyurdiev 2 participants 9 messages Nov 1, 2005 – Nov 2, 2005 SELinux security module

lore
Share & tools
lore
#0 lore raw

Okay, I hadn't really tested MLS users very well. This patch corrects that oversight, and makes commit() pass on an MLS/MCS enabled system.

Changes:

- parse_filter_space_until is hopelessly broken - there's at least 2 segfault bugs in it - delete the whole function, and handle MLS for users the same way I handle it for seusers - disallow multiline and spaces. This whole approach of looking for the "range" substring at the end is wrong in the first place, and genusers shouldn't be using it. Maybe I'll implement this properly in the future, but right now it doesn't seem too important - will keep MLS without spaces on a single line.

- the check whether "level" is present is backwards - fix it

libsemanage/src/parse_utils.c +2 -54
diff -Naurp --exclude CVS --exclude ChangeLog --exclude VERSION --exclude Makefile old/libsemanage/src/parse_utils.c new/libsemanage/src/parse_utils.c
@@ -151,7 +151,8 @@ int parse_assert_noeof(
 	parse_info_t* info) {
 
 	if (!info->ptr) {
-		ERR(handle, "unexpected end of file");
+		ERR(handle, "unexpected end of file (%s: %u)",
+			info->filename, info->lineno);
 		return STATUS_ERR;
 	}
 
@@ -233,59 +234,6 @@ int parse_optional_str(parse_info_t* inf
 	}
 }
 
-char* parse_filter_space_until(
-	semanage_handle_t* handle, 
-	parse_info_t* info, 
-	const char* substr) {
-
-	char* buffer = NULL, *wr, *tmp;
-	int len = strlen(substr);
-	int used = 0;
-	int csize = 0;
-
-	wr = buffer;
-	do {
-		/* If content is not a space, copy to buffer */
-		if (!isspace(info->ptr)) {
-
-			/* If we're out of space, increase by 15 */
-			if (used + 1 >= csize) {
-				csize += 15;
-				tmp = realloc(buffer, csize);
-				if (!tmp)
-					goto omem;
-				buffer = tmp;
-			}
-			*wr++ = *info->ptr;
-			used++;
-		}
-		info->ptr++;
-
-		if (parse_skip_space(handle, info) < 0)
-			goto err;
-		if (parse_assert_noeof(handle, info) < 0)
-			goto err;
-
-	} while(!strncasecmp(info->ptr, substr, len));
-
-	if (!buffer) {
-		buffer = malloc(1);
-		if (!buffer)
-			goto omem;
-	}
-
-	*wr = '\0';
-
-	return buffer;
-
-	omem:
-	ERR(handle, "out of memory, could not allocate buffer");
-
-	err:
-	free(buffer);
-	return NULL;
-}
-
 int parse_fetch_string(
 	semanage_handle_t* handle,
 	parse_info_t* info, 
libsemanage/src/parse_utils.h +0 -9
diff -Naurp --exclude CVS --exclude ChangeLog --exclude VERSION --exclude Makefile old/libsemanage/src/parse_utils.h new/libsemanage/src/parse_utils.h
@@ -83,15 +83,6 @@ extern int parse_optional_str(
 	parse_info_t* info, 
 	const char* str);
 
-/* Buffer a string, filtering all 
- * whitespace, until substring is encountered,
- * at which point return the buffered string.
- * This function will work on multiple lines */
-extern char* parse_filter_space_until(
-	semanage_handle_t* handle,
-	parse_info_t* info,
-	const char* substr);
-
 /* Extract the next string (delimited by 
  * whitespace), and move the read pointer past it */
 extern int parse_fetch_string(
libsemanage/src/users_file.c +17 -13
diff -Naurp --exclude CVS --exclude ChangeLog --exclude VERSION --exclude Makefile old/libsemanage/src/users_file.c new/libsemanage/src/users_file.c
@@ -70,7 +70,7 @@ static int user_parse(
 	semanage_user_t* user) {
 
 	int islist = 0;
-	char* mls = NULL;
+	char* str = NULL;
 	char* start;
 	char* name_str = NULL;
 
@@ -164,35 +164,39 @@ static int user_parse(
 			goto err;
 		if (parse_assert_noeof(handle, info) < 0)
 			goto err;
-		if (parse_optional_str(info, "level") != STATUS_NODATA)
+		if (parse_optional_str(info, "level") == STATUS_NODATA)
 			goto semicolon;
 		if (parse_assert_space(handle, info) < 0)
 			goto err;
 		if (parse_assert_noeof(handle, info) < 0)
 			goto err;
 
-		mls = parse_filter_space_until(handle, info, "range");
-		if (!mls)
+		/* NOTE: does not allow spaces/multiline */
+		if (parse_fetch_string(handle, info, &str) < 0)
 			goto err;
-		if (semanage_user_set_mlslevel(handle, user, mls) < 0)
+		if (semanage_user_set_mlslevel(handle, user, str) < 0)
 			goto err;
-		free(mls);
+		free(str);
+		str = NULL;
 
 		/* Parse range header */
+		if (parse_assert_space(handle, info) < 0)
+			goto err;
 		if (parse_assert_str(handle, info, "range") < 0)
-                       goto err;
-
+			goto err;
 		if (parse_assert_space(handle, info) < 0)
 			goto err;
 		if (parse_assert_noeof(handle, info) < 0)
 			goto err;
 
-		mls = parse_filter_space_until(handle, info, ";");
-		if (!mls)
+		/* NOTE: does not allow spaces/multiline */
+		if (parse_fetch_string_until(handle, info, &str, ';') < 0)
 			goto err;
-		if (semanage_user_set_mlsrange(handle, user, mls) < 0)
+		if (semanage_user_set_mlsrange(handle, user, str) < 0)
 			goto err;
-		free(mls);
+
+		free(str);
+		str = NULL;
 	}
 
 	/* Check for semicolon */
@@ -213,7 +217,7 @@ static int user_parse(
 
 	err:
 	ERR(handle, "could not parse user record");
-	free(mls);
+	free(str);
 	parse_dispose_line(info);
 	return STATUS_ERR;
 }
#1 lore raw

By the way, expanding the module (sepol_expand_module) takes an unusually long time to run... commit is very slow (and this is on an Athlon3k computer)....

1. Time to modify seuser things: 0.5 sec
2. Time to call sepol_expand_module: 9 sec
3. Time to merge and commit other policy components, and load the policy: 1 sec

You can see why I want (2), and most of (3) skipped in the case of seusers...need to add tracking of when the policy is modified.

--
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.

#2 lore raw

[ SEMANAGE ] Some seusers mapping validation

Quoted 3 lines

You can see why I want (2), and most of (3) skipped in the case of seusers...need to add tracking of when the policy is modified.

Well... with the attached patch I need a policydb, regardless of whether modifications occured.. otoh if no modifications, then I don't have to call expand, which takes so long... can go through the policydb_cache function instead (not sure if that's any faster, however..)

Changes:
- add some basic validation for seusers - abort the commit if the sename is invalid (Selinux user does not exist). This will also prevent deletion of users without deleting/changing the corresponding mappings in the seusers file - it works out rather nicely.

We might also want to validate the MLS range, and the Unix name. I am not clear on how to validate the MLS range - what's happening with the old (local.users) MLS range? Is it deprecated? Are they supposed to match? How to handle this?

libsemanage/src/policy_components.c +5 -0
diff -Naurp --exclude CVS --exclude ChangeLog --exclude VERSION --exclude Makefile old/libsemanage/src/policy_components.c new/libsemanage/src/policy_components.c
@@ -2,6 +2,7 @@
 #include "handle.h"
 #include "database.h"
 #include "modules.h"
+#include "seusers.h"
 #include "debug.h"
 
 #define MODE_SET    1
@@ -125,6 +126,10 @@ int semanage_commit_components(
 		semanage_seuser_dbase(handle)
 	};
 
+	/* Validate seusers */
+	if (semanage_seuser_validate(handle) < 0)
+		goto err;
+
 	for (i = 0; i < CCOUNT; i++) {
 		/* Flush to disk */
 		if (components[i]->dtable->flush(
libsemanage/src/seusers.c +60 -1

72 lines not on this page · raw message

libsemanage/src/seusers.h +9 -0
diff -Naurp --exclude CVS --exclude ChangeLog --exclude VERSION --exclude Makefile old/libsemanage/src/seusers.h new/libsemanage/src/seusers.h
@@ -0,0 +1,9 @@
+#ifndef _SEUSERS_INTERNAL_H_
+#define _SEUSERS_INTERNAL_H_
+
+#include <semanage/seusers.h>
+
+extern int semanage_seuser_validate(
+	semanage_handle_t* handle);
+
+#endif
#3 lore raw

Re: [ SEMANAGE ] Some seusers mapping validation

libsemanage/src/seusers.h line 7

Quoted 4 lines
libsemanage/src/seusers.h +2 -0
+extern int semanage_seuser_validate(
+	semanage_handle_t* handle);

Doh... will be exported by the map wildcard...
Should I get rid of wildcards?

--
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
In reply to Ivan Gyurdiev
Quoted 11 lines · Ivan Gyurdiev

By the way, expanding the module (sepol_expand_module) takes an unusually long time to run... commit is very slow (and this is on an Athlon3k computer)....

1. Time to modify seuser things: 0.5 sec
2. Time to call sepol_expand_module: 9 sec
3. Time to merge and commit other policy components, and load the
policy: 1 sec

You can see why I want (2), and most of (3) skipped in the case of seusers...need to add tracking of when the policy is modified.

If you pass a '0' check flag to sepol_expand_module to suppress
assertion and hierarchy checking, it is much faster. For development,
you can just echo "expand-check = 0" >> /etc/selinux/semanage.conf to turn it off. For production, you could make it a flag parameter to semanage_expand_sandbox rather than just a config setting.

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

Re: [ SEMANAGE ] Some seusers mapping validation

In reply to Ivan Gyurdiev
Quoted 5 lines · Ivan Gyurdiev

libsemanage/src/seusers.h line 7

Quoted 3 lines
libsemanage/src/seusers.h +2 -0
+extern int semanage_seuser_validate(
+	semanage_handle_t* handle);

Doh... will be exported by the map wildcard...
Should I get rid of wildcards?

Or we can mark it hidden, once I add dso.h.

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.
#5 lore raw

Re: [ SEMANAGE ] Some seusers mapping validation

In reply to Ivan Gyurdiev
Quoted 4 lines · Ivan Gyurdiev

We might also want to validate the MLS range, and the Unix name. I am not clear on how to validate the MLS range - what's happening with the old (local.users) MLS range? Is it deprecated? Are they supposed to match? How to handle this?

users.local allows you to define additional SELinux users and authorize them for role sets and ranges. seusers allows you to map Linux users to SELinux users defined in either the policy modules or users.local, and to assign the Linux user a subset of the range authorized for the SELinux user. Hence, validation of the MLS range in seusers would consist of:
- validating the range by itself as usual,
- validating that the range is a subset of the range authorized for the SELinux user.

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.
#7 lore raw

Re: [ SEMANAGE ] Some seusers mapping validation

In reply to Ivan Gyurdiev
Quoted 7 lines · Ivan Gyurdiev
Quoted 3 lines

You can see why I want (2), and most of (3) skipped in the case of seusers...need to add tracking of when the policy is modified.

Well... with the attached patch I need a policydb, regardless of whether modifications occured.. otoh if no modifications, then I don't have to call expand, which takes so long... can go through the policydb_cache function instead (not sure if that's any faster, however..)

All five patches merged as of libsemanage 1.3.40, with the minor change to make errors upon copying of seusers non-fatal.

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.
#6 lore raw

Re: [ SEMANAGE ] Some seusers mapping validation

In reply to Stephen Smalley
Quoted 11 lines

users.local allows you to define additional SELinux users and authorize them for role sets and ranges. seusers allows you to map Linux users to SELinux users defined in either the policy modules or users.local, and to assign the Linux user a subset of the range authorized for the SELinux user. Hence, validation of the MLS range in seusers would consist of:
- validating the range by itself as usual,
- validating that the range is a subset of the range authorized for the SELinux user.

This requires additional interfaces in context.c that do not currently exist...

--
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.