Skip to contentSkip to conversation

PATCH: libselinux matchpathcon() eliminate %as scanf format

PATCH Todd C. Miller 2 participants 2 messages Jan 26, 2007 – Feb 1, 2007 SELinux security module

lore
Share & tools
lore
#0 lore raw

This is a patch I sent in last year but forgot to split up as requested; it still applies to the recently-released selinux-1.34.0.

THe patch replaces usage of the non-standard %as scanf() format (which conflicts with C99) with strtok_r(). This does mean that line_buf is modified but this variable is only used as an argument to process_line() and is freed thereafter.

I made this change as part of the port of libselinux to SEBSD and SEDarwin.

- todd

matchpathcon.c +15 -2
@@ -444,7 +444,7 @@
 			int pass, unsigned lineno)
 {
 	int items, len, regerr, ret;
-	char *buf_p;
+	char *buf_p, *ptr;
 	char *regex, *type, *context;
 	const char *reg_buf;
 	char *anchored_regex;
@@ -459,7 +459,11 @@
 	/* Skip comment lines and empty lines. */
 	if (*buf_p == '#' || *buf_p == 0)
 		return 0;
-	items = sscanf(line_buf, "%as %as %as", &regex, &type, &context);
+
+	regex = strtok_r(buf_p, " \t", &ptr);
+	type = strtok_r(NULL, " \t", &ptr);
+	context = strtok_r(NULL, " \t", &ptr);
+	items = !!regex + !!type + !!context;
 	if (items < 2) {
 		myprintf("%s:  line %d is missing fields, skipping\n", path,
 			 lineno);
@@ -470,6 +474,15 @@
 		type = NULL;
 	}
 
+	regex = strdup(regex);
+	if (type != NULL)
+		type = strdup(type);
+	context = strdup(context);
+	if (!!regex + !!type + !!context != items) {
+		ret = -1;
+		goto finish;
+	}
+
 	reg_buf = regex;
 	len = get_stem_from_spec(reg_buf);
 	if (len && prefix && strncmp(prefix, regex, len)) {

--
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 8 lines · Todd C. Miller

This is a patch I sent in last year but forgot to split up as requested; it still applies to the recently-released selinux-1.34.0.

THe patch replaces usage of the non-standard %as scanf() format (which conflicts with C99) with strtok_r(). This does mean that line_buf is modified but this variable is only used as an argument to process_line() and is freed thereafter.

Quoted 4 lines

I made this change as part of the port of libselinux to SEBSD and SEDarwin.

- todd

I made a few updates for style (the !! thing was too clever for me) and merged into trunk and stable. Actual merged version is attached.

Quoted 42 lines
matchpathcon.c +15 -3
@@ -444,7 +444,7 @@
 			int pass, unsigned lineno)
 {
 	int items, len, regerr, ret;
-	char *buf_p;
+	char *buf_p, *ptr;
 	char *regex, *type, *context;
 	const char *reg_buf;
 	char *anchored_regex;
@@ -459,7 +459,11 @@
 	/* Skip comment lines and empty lines. */
 	if (*buf_p == '#' || *buf_p == 0)
 		return 0;
-	items = sscanf(line_buf, "%as %as %as", &regex, &type, &context);
+
+	regex = strtok_r(buf_p, " \t", &ptr);
+	type = strtok_r(NULL, " \t", &ptr);
+	context = strtok_r(NULL, " \t", &ptr);
+	items = !!regex + !!type + !!context;
 	if (items < 2) {
 		myprintf("%s:  line %d is missing fields, skipping\n", path,
 			 lineno);
@@ -470,6 +474,15 @@
 		type = NULL;
 	}
 
+	regex = strdup(regex);
+	if (type != NULL)
+		type = strdup(type);
+	context = strdup(context);
+	if (!!regex + !!type + !!context != items) {
+		ret = -1;
+		goto finish;
+	}
+
 	reg_buf = regex;
 	len = get_stem_from_spec(reg_buf);
 	if (len && prefix && strncmp(prefix, regex, len)) {
 
--
Quoted 3 lines

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.

src/matchpathcon.c +30 -2
Index: libselinux/src/matchpathcon.c
===================================================================
@@ -444,7 +444,7 @@
 			int pass, unsigned lineno)
 {
 	int items, len, regerr, ret;
-	char *buf_p;
+	char *buf_p, *ptr;
 	char *regex, *type, *context;
 	const char *reg_buf;
 	char *anchored_regex;
@@ -459,7 +459,18 @@
 	/* Skip comment lines and empty lines. */
 	if (*buf_p == '#' || *buf_p == 0)
 		return 0;
-	items = sscanf(line_buf, "%as %as %as", &regex, &type, &context);
+
+	items = 0;
+	regex = strtok_r(buf_p, " \t", &ptr);
+	if (regex)
+		items += 1;
+	type = strtok_r(NULL, " \t", &ptr);
+	if (type)
+		items += 1;
+	context = strtok_r(NULL, " \t", &ptr);
+	if (context)
+		items += 1;
+	
 	if (items < 2) {
 		myprintf("%s:  line %d is missing fields, skipping\n", path,
 			 lineno);
@@ -470,6 +481,23 @@
 		type = NULL;
 	}
 
+	regex = strdup(regex);
+	if (!regex) {
+		return -1;
+	}
+	if (type) {
+		type = strdup(type);
+		if (!type) {
+			ret = -1;
+			goto finish;
+		}
+	}
+	context = strdup(context);
+	if (!context) {
+		ret = -1;
+		goto finish;
+	}
+
 	reg_buf = regex;
 	len = get_stem_from_spec(reg_buf);
 	if (len && prefix && strncmp(prefix, regex, len)) {