Skip to contentSkip to conversation

Is there a command line tool that returns 1 if a file is in the wrong context?

DISCUSSION Joe Nall 3 participants 4 messages May 24, 2008 – May 27, 2008 SELinux security module

lore
Share & tools
lore

Joe Nall

author
#0 lore raw

Is there a command line tool that returns 1 if a file is in the wrong context?. I want to add a %verifyscript to our rpms to validate the installed file context when rpm -V is invoked.

joe

--
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 3 lines · Joe Nall

Is there a command line tool that returns 1 if a file is in the wrong context?. I want to add a %verifyscript to our rpms to validate the installed file context when rpm -V is invoked.

I was going to suggest matchpathcon -V, except when trying it, I found that there is a bug in the current libselinux that makes it seg fault and it also doesn't return the status but rather displays it. So how about the patch below to fix the bug and make the exit status usable?

After this patch, you can run '/usr/sbin/matchpathcon -Vq path1 [path2...]' and check the exit status; 0 will mean that all verified; non-zero will be the count of failures.

utils/matchpathcon.c +15 -6
Index: libselinux/utils/matchpathcon.c
===================================================================
@@ -12,7 +12,7 @@
 void usage(const char *progname)
 {
 	fprintf(stderr,
-		"usage:  %s [-N] [-n] [-f file_contexts] [-p prefix] [-V] path...\n",
+		"usage:  %s [-N] [-n] [-f file_contexts] [-p prefix] [-Vq] path...\n",
 		progname);
 	exit(1);
 }
@@ -42,11 +42,12 @@
 	int verify = 0;
 	int notrans = 0;
 	int error = 0;
+	int quiet = 0;
 
 	if (argc < 2)
 		usage(argv[0]);
 
-	while ((opt = getopt(argc, argv, "Nnf:p:V")) > 0) {
+	while ((opt = getopt(argc, argv, "Nnf:p:Vq")) > 0) {
 		switch (opt) {
 		case 'n':
 			header = 0;
@@ -90,6 +91,9 @@
 				exit(1);
 			}
 			break;
+		case 'q':
+			quiet = 1;
+			break;
 		default:
 			usage(argv[0]);
 		}
@@ -101,11 +105,18 @@
 			mode = buf.st_mode;
 
 		if (verify) {
+			if (quiet) {
+				if (selinux_file_context_verify(argv[i], 0))
+					continue;
+				else
+					exit(1);
+			}
 			if (selinux_file_context_verify(argv[i], 0)) {
 				printf("%s verified.\n", argv[i]);
 			} else {
 				security_context_t con;
 				int rc;
+				error++;
 				if (notrans)
 					rc = lgetfilecon_raw(argv[i], &con);
 				else
@@ -114,15 +125,13 @@
 				if (rc >= 0) {
 					printf("%s has context %s, should be ",
 					       argv[i], con);
-					error +=
-					    printmatchpathcon(argv[i], 0, mode);
+					printmatchpathcon(argv[i], 0, mode);
 					freecon(con);
 				} else {
 					printf
 					    ("actual context unknown: %s, should be ",
 					     strerror(errno));
-					error +=
-					    printmatchpathcon(argv[i], 0, mode);
+					printmatchpathcon(argv[i], 0, mode);
 				}
 			}
 		} else {
src/matchpathcon.c +6 -0
Index: libselinux/src/matchpathcon.c
===================================================================
@@ -372,6 +372,9 @@
 		else
 			return 0;
 	}
+	
+	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
+			return -1;
 
 	if (selabel_lookup_raw(hnd, &fcontext, path, mode) != 0) {
 		if (errno != ENOENT)
@@ -394,6 +397,9 @@
 	if (lstat(path, &st) != 0)
 		return rc;
 
+	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
+			return -1;
+
 	/* If there's an error determining the context, or it has none, 
 	   return to allow default context */
 	if (selabel_lookup_raw(hnd, &scontext, path, st.st_mode)) {
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.
#2 lore raw
Quoted 5 lines · Stephen Smalley
Quoted 2 lines · Joe Nall

Is there a command line tool that returns 1 if a file is in the wrong context?. I want to add a %verifyscript to our rpms to validate the installed file context when rpm -V is invoked.

Quoted 9 lines

I was going to suggest matchpathcon -V, except when trying it, I found that there is a bug in the current libselinux that makes it seg fault and it also doesn't return the status but rather displays it. So how about the patch below to fix the bug and make the exit status usable?

After this patch, you can run '/usr/sbin/matchpathcon -Vq path1 [path2...]' and check the exit status; 0 will mean that all verified; non-zero will be the count of failures.

Minor nit, but return codes are limited to 1 byte; multiples of 256 errors will "succeed":

$ for i in {250..260}; do sh -c "exit $i"; echo $? ; done
250
251
252
253
254
255
0
1
2
3
4
Quoted 100 lines

Index: libselinux/utils/matchpathcon.c
===================================================================

utils/matchpathcon.c +15 -6
@@ -12,7 +12,7 @@
 void usage(const char *progname)
 {
 	fprintf(stderr,
-		"usage:  %s [-N] [-n] [-f file_contexts] [-p prefix] [-V] path...\n",
+		"usage:  %s [-N] [-n] [-f file_contexts] [-p prefix] [-Vq] path...\n",
 		progname);
 	exit(1);
 }
@@ -42,11 +42,12 @@
 	int verify = 0;
 	int notrans = 0;
 	int error = 0;
+	int quiet = 0;
 
 	if (argc < 2)
 		usage(argv[0]);
 
-	while ((opt = getopt(argc, argv, "Nnf:p:V")) > 0) {
+	while ((opt = getopt(argc, argv, "Nnf:p:Vq")) > 0) {
 		switch (opt) {
 		case 'n':
 			header = 0;
@@ -90,6 +91,9 @@
 				exit(1);
 			}
 			break;
+		case 'q':
+			quiet = 1;
+			break;
 		default:
 			usage(argv[0]);
 		}
@@ -101,11 +105,18 @@
 			mode = buf.st_mode;
 
 		if (verify) {
+			if (quiet) {
+				if (selinux_file_context_verify(argv[i], 0))
+					continue;
+				else
+					exit(1);
+			}
 			if (selinux_file_context_verify(argv[i], 0)) {
 				printf("%s verified.\n", argv[i]);
 			} else {
 				security_context_t con;
 				int rc;
+				error++;
 				if (notrans)
 					rc = lgetfilecon_raw(argv[i], &con);
 				else
@@ -114,15 +125,13 @@
 				if (rc >= 0) {
 					printf("%s has context %s, should be ",
 					       argv[i], con);
-					error +=
-					    printmatchpathcon(argv[i], 0, mode);
+					printmatchpathcon(argv[i], 0, mode);
 					freecon(con);
 				} else {
 					printf
 					    ("actual context unknown: %s, should be ",
 					     strerror(errno));
-					error +=
-					    printmatchpathcon(argv[i], 0, mode);
+					printmatchpathcon(argv[i], 0, mode);
 				}
 			}
 		} else {

Index: libselinux/src/matchpathcon.c
===================================================================

src/matchpathcon.c +6 -0
@@ -372,6 +372,9 @@
 		else
 			return 0;
 	}
+	
+	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
+			return -1;
 
 	if (selabel_lookup_raw(hnd, &fcontext, path, mode) != 0) {
 		if (errno != ENOENT)
@@ -394,6 +397,9 @@
 	if (lstat(path, &st) != 0)
 		return rc;
 
+	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
+			return -1;
+
 	/* If there's an error determining the context, or it has none, 
 	   return to allow default context */
 	if (selabel_lookup_raw(hnd, &scontext, path, st.st_mode)) {
Signature · 8 lines
Eamon Walsh <[email protected]>
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.
#3 lore raw
Quoted 13 lines · Eamon Walsh
Quoted 12 lines · Stephen Smalley
Quoted 4 lines · Joe Nall

Is there a command line tool that returns 1 if a file is in the wrong context?. I want to add a %verifyscript to our rpms to validate the installed file context when rpm -V is invoked.

I was going to suggest matchpathcon -V, except when trying it, I found that there is a bug in the current libselinux that makes it seg fault and it also doesn't return the status but rather displays it. So how about the patch below to fix the bug and make the exit status usable?

Quoted 7 lines
Quoted 4 lines

After this patch, you can run '/usr/sbin/matchpathcon -Vq path1 [path2...]' and check the exit status; 0 will mean that all verified; non-zero will be the count of failures.

Minor nit, but return codes are limited to 1 byte; multiples of 256 errors will "succeed":

Ok, that's already a bug in the current matchpathcon utility in that case. So I'll just make it always exit 0 or 1.

Quoted 120 lines
$ for i in {250..260}; do sh -c "exit $i"; echo $? ; done
250
251
252
253
254
255
0
1
2
3
4
Quoted 100 lines

Index: libselinux/utils/matchpathcon.c
===================================================================

utils/matchpathcon.c +15 -6
@@ -12,7 +12,7 @@
 void usage(const char *progname)
 {
 	fprintf(stderr,
-		"usage:  %s [-N] [-n] [-f file_contexts] [-p prefix] [-V] path...\n",
+		"usage:  %s [-N] [-n] [-f file_contexts] [-p prefix] [-Vq] path...\n",
 		progname);
 	exit(1);
 }
@@ -42,11 +42,12 @@
 	int verify = 0;
 	int notrans = 0;
 	int error = 0;
+	int quiet = 0;
 
 	if (argc < 2)
 		usage(argv[0]);
 
-	while ((opt = getopt(argc, argv, "Nnf:p:V")) > 0) {
+	while ((opt = getopt(argc, argv, "Nnf:p:Vq")) > 0) {
 		switch (opt) {
 		case 'n':
 			header = 0;
@@ -90,6 +91,9 @@
 				exit(1);
 			}
 			break;
+		case 'q':
+			quiet = 1;
+			break;
 		default:
 			usage(argv[0]);
 		}
@@ -101,11 +105,18 @@
 			mode = buf.st_mode;
 
 		if (verify) {
+			if (quiet) {
+				if (selinux_file_context_verify(argv[i], 0))
+					continue;
+				else
+					exit(1);
+			}
 			if (selinux_file_context_verify(argv[i], 0)) {
 				printf("%s verified.\n", argv[i]);
 			} else {
 				security_context_t con;
 				int rc;
+				error++;
 				if (notrans)
 					rc = lgetfilecon_raw(argv[i], &con);
 				else
@@ -114,15 +125,13 @@
 				if (rc >= 0) {
 					printf("%s has context %s, should be ",
 					       argv[i], con);
-					error +=
-					    printmatchpathcon(argv[i], 0, mode);
+					printmatchpathcon(argv[i], 0, mode);
 					freecon(con);
 				} else {
 					printf
 					    ("actual context unknown: %s, should be ",
 					     strerror(errno));
-					error +=
-					    printmatchpathcon(argv[i], 0, mode);
+					printmatchpathcon(argv[i], 0, mode);
 				}
 			}
 		} else {

Index: libselinux/src/matchpathcon.c
===================================================================

src/matchpathcon.c +6 -0
@@ -372,6 +372,9 @@
 		else
 			return 0;
 	}
+	
+	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
+			return -1;
 
 	if (selabel_lookup_raw(hnd, &fcontext, path, mode) != 0) {
 		if (errno != ENOENT)
@@ -394,6 +397,9 @@
 	if (lstat(path, &st) != 0)
 		return rc;
 
+	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
+			return -1;
+
 	/* If there's an error determining the context, or it has none, 
 	   return to allow default context */
 	if (selabel_lookup_raw(hnd, &scontext, path, st.st_mode)) {
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.