Skip to contentSkip to conversation

selinux-testsuite: unix_socket: Add tests for file namespace.

PATCH New Stephen Smalley 1 participant 1 message Jul 8, 2015 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux-testsuite: unix_socket: Add tests for file namespace.

Add tests for Unix sockets in the file namespace. The original tests only exercised the abstract namespace.

---
 ToDo                       |  1 -
 policy/test_unix_socket.te | 31 ++++++++++++++++++++---
 tests/unix_socket/client.c | 53 +++++++++++++++++++++++++++++++---------
 tests/unix_socket/server.c | 39 +++++++++++++++++++++++------
 tests/unix_socket/test     | 61 ++++++++++++++++++++++++++++++++++++++++------
 5 files changed, 154 insertions(+), 31 deletions(-)
ToDo +0 -1
index 06f8aaf..3f4cb29 100644
@@ -1,6 +1,5 @@
 unix_socket, inet_socket: test individual failure (denial) of common socket permissions (create, bind, ...).
 unix_socket, inet_socket: test setsockcreatecon.
-unix_socket: generalize tests to cover file namespace as well as abstract.
 inet_socket: generalize tests to cover INET6 as well as INET.
 inet_socket: test individual failure of netif ingress/egress.
 inet_socket: test individual failure of node recvfrom/sendto.
policy/test_unix_socket.te +28 -3
index cf3df35..c6e06cc 100644
@@ -12,13 +12,29 @@ unconfined_runs_test(test_unix_server_t)
 typeattribute test_unix_server_t testdomain;
 typeattribute test_unix_server_t unixsocketdomain;
 
+# Socket file for the server process.
+type test_unix_server_sockfile_t;
+files_type(test_unix_server_sockfile_t)
+type_transition test_unix_server_t test_file_t:sock_file test_unix_server_sockfile_t;
+allow test_unix_server_t test_file_t:dir rw_dir_perms;
+allow test_unix_server_t test_unix_server_sockfile_t:sock_file { create unlink };
+
 # Domain for stream client process.
 type test_unix_stream_client_t;
 domain_type(test_unix_stream_client_t)
 unconfined_runs_test(test_unix_stream_client_t)
 typeattribute test_unix_stream_client_t testdomain;
 typeattribute test_unix_stream_client_t unixsocketdomain;
-# It can connectto the server.
+
+# Socket file for the client process.
+type test_unix_client_sockfile_t;
+files_type(test_unix_client_sockfile_t)
+type_transition test_unix_stream_client_t test_file_t:sock_file test_unix_client_sockfile_t;
+allow test_unix_stream_client_t test_file_t:dir rw_dir_perms;
+allow test_unix_stream_client_t test_unix_client_sockfile_t:sock_file { create unlink };
+
+# client can connect to the server via the socket file or via abstract sockets.
+allow test_unix_stream_client_t test_unix_server_sockfile_t:sock_file write;
 allow test_unix_stream_client_t test_unix_server_t:unix_stream_socket connectto;
 
 # Domain for dgram client process.
@@ -27,9 +43,18 @@ domain_type(test_unix_dgram_client_t)
 unconfined_runs_test(test_unix_dgram_client_t)
 typeattribute test_unix_dgram_client_t testdomain;
 typeattribute test_unix_dgram_client_t unixsocketdomain;
-# It can sendto the server.
+
+# Reuse the same type for its socket file.
+type_transition test_unix_dgram_client_t test_file_t:sock_file test_unix_client_sockfile_t;
+allow test_unix_dgram_client_t test_file_t:dir rw_dir_perms;
+allow test_unix_dgram_client_t test_unix_client_sockfile_t:sock_file { create unlink };
+
+# client can send to the server via the socket file or via abstract sockets.
+allow test_unix_dgram_client_t test_unix_server_sockfile_t:sock_file write;
 allow test_unix_dgram_client_t test_unix_server_t:unix_dgram_socket sendto;
-# And the server can sendto it in order to reply.
+
+# And the server can send back to it in order to reply.
+allow test_unix_server_t test_unix_client_sockfile_t:sock_file write;
 allow test_unix_server_t test_unix_dgram_client_t:unix_dgram_socket sendto;
 
 # Allow all of these domains to be entered from the sysadm domain.
tests/unix_socket/client.c +41 -12

92 lines not on this page · raw message

tests/unix_socket/server.c +31 -8
index e8958dd..7cbebe3 100644
@@ -2,6 +2,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <string.h>
 #include <unistd.h>
@@ -17,7 +18,7 @@
 
 void usage(char *progname)
 {
-	fprintf(stderr, "usage:  %s [stream|dgram] socket-name\n", progname);
+	fprintf(stderr, "usage:  %s [-a] [stream|dgram] socket-name\n", progname);
 	exit(1);
 }
 
@@ -32,13 +33,26 @@ main(int argc, char **argv)
 	socklen_t sunlen, remotesunlen;
 	int type;
 	char byte;
+	int opt;
+	bool abstract = false;
+
+	while ((opt = getopt(argc, argv, "a")) != -1) {
+		switch (opt) {
+		case 'a':
+			abstract = true;
+			break;
+		default:
+			usage(argv[0]);
+			break;
+		}
+	}
 
-	if (argc != 3)
+	if ((argc - optind) != 2)
 		usage(argv[0]);
 
-	if (!strcmp(argv[1], "stream"))
+	if (!strcmp(argv[optind], "stream"))
 		type = SOCK_STREAM;
-	else if (!strcmp(argv[1], "dgram"))
+	else if (!strcmp(argv[optind], "dgram"))
 		type = SOCK_DGRAM;
 	else
 		usage(argv[0]);
@@ -58,10 +72,17 @@ main(int argc, char **argv)
 
 	bzero(&sun, sizeof(struct sockaddr_un));
 	sun.sun_family = AF_UNIX;
-	sun.sun_path[0] = 0;
-	strcpy(&sun.sun_path[1], argv[2]);
-	sunlen = offsetof(struct sockaddr_un, sun_path) +
-		 strlen(&sun.sun_path[1]) + 1;
+	if (abstract) {
+		sun.sun_path[0] = 0;
+		strcpy(&sun.sun_path[1], argv[optind+1]);
+		sunlen = offsetof(struct sockaddr_un, sun_path) +
+			strlen(&sun.sun_path[1]) + 1;
+	} else {
+		strcpy(sun.sun_path, argv[optind+1]);
+		unlink(sun.sun_path);
+		sunlen = offsetof(struct sockaddr_un, sun_path) +
+			strlen(sun.sun_path) + 1;
+	}
 
 	if (bind(sock, (struct sockaddr *) &sun, sunlen) < 0) {
 		perror("bind");
@@ -163,5 +184,7 @@ main(int argc, char **argv)
 	}
 
 	close(sock);
+	if (!abstract)
+		unlink(sun.sun_path);
 	exit(0);
 }
tests/unix_socket/test +54 -7

97 lines not on this page · raw message

Signature · 1 line
2.1.0