Skip to contentSkip to conversation

tests/inet_socket: cover the MPTCP protocol

PATCH In review Ondrej Mosnacek 1 participant 2 messages Jun 19, 2023 – Jun 29, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/inet_socket: cover the MPTCP protocol

As of kernel 6.5 MPTCP should work as a drop-in replacement for TCP w.r.t. SELinux, so test it in addition to TCP using the same tests.

This requires modifying the inet_socket test to test each protocol separately, using the same subdirectory symlink trick as in the [fs_]filesystem tests. After that, an mptcp subtest can be added easily by extending the programs to support it and adding a new subdirectory symlink for it.

---
 tests/Makefile              |  10 +-
 tests/inet_socket/bind.c    |  22 +-
 tests/inet_socket/client.c  |  33 ++-
 tests/inet_socket/connect.c |  27 ++-
 tests/inet_socket/mptcp     |   1 +
 tests/inet_socket/server.c  |  33 ++-
 tests/inet_socket/tcp       |   1 +
 tests/inet_socket/test      | 430 +++++++++++++++++-------------------
 tests/inet_socket/udp       |   1 +
 9 files changed, 292 insertions(+), 266 deletions(-)
 create mode 120000 tests/inet_socket/mptcp
 create mode 120000 tests/inet_socket/tcp
 create mode 120000 tests/inet_socket/udp
tests/Makefile +8 -2
index 74df247..db4cb38 100644
@@ -27,8 +27,8 @@ SUBDIRS:= domain_trans entrypoint execshare exectrace execute_no_trans \
 	task_setnice task_setscheduler task_getscheduler task_getsid \
 	task_getpgid task_setpgid file ioctl capable_file capable_net \
 	capable_sys dyntrans dyntrace bounds nnp_nosuid mmap unix_socket \
-	inet_socket overlay checkreqprot mqueue mac_admin atsecure \
-	infiniband_endport infiniband_pkey
+	inet_socket/tcp inet_socket/udp overlay checkreqprot mqueue \
+	mac_admin atsecure infiniband_endport infiniband_pkey
 
 ifeq ($(shell grep -q cap_userns $(POLDEV)/include/support/all_perms.spt && echo true),true)
 ifneq ($(shell ./kvercmp $$(uname -r) 4.7),-1)
@@ -147,6 +147,12 @@ SUBDIRS += secretmem
 endif
 endif
 
+# MPTCP is supported since kernel 5.6, but only works with SELinux
+# since 6.5
+ifneq ($(shell ./kvercmp $$(uname -r) 6.5),-1)
+SUBDIRS += inet_socket/mptcp
+endif
+
 ifeq ($(DISTRO),RHEL4)
     SUBDIRS:=$(filter-out bounds dyntrace dyntrans inet_socket mmap nnp_nosuid overlay unix_socket, $(SUBDIRS))
 endif
tests/inet_socket/bind.c +16 -6
index e16e117..389ca20 100644
@@ -8,9 +8,13 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#ifndef IPPROTO_MPTCP
+#define IPPROTO_MPTCP 262
+#endif
+
 void usage(char *progname)
 {
-	fprintf(stderr, "usage:  %s [stream|dgram] port\n", progname);
+	fprintf(stderr, "usage:  %s protocol port\n", progname);
 	exit(1);
 }
 
@@ -23,24 +27,30 @@ main(int argc, char **argv)
 	int result;
 	struct sockaddr_in sin;
 	socklen_t sinlen;
-	int type;
+	int type, protocol;
 	unsigned short port;
 
 	if (argc != 3)
 		usage(argv[0]);
 
-	if (!strcmp(argv[1], "stream"))
+	if (!strcmp(argv[1], "tcp")) {
+		type = SOCK_STREAM;
+		protocol = IPPROTO_TCP;
+	} else if (!strcmp(argv[1], "mptcp")) {
 		type = SOCK_STREAM;
-	else if (!strcmp(argv[1], "dgram"))
+		protocol = IPPROTO_MPTCP;
+	} else if (!strcmp(argv[1], "udp")) {
 		type = SOCK_DGRAM;
-	else
+		protocol = IPPROTO_UDP;
+	} else {
 		usage(argv[0]);
+	}
 
 	port = atoi(argv[2]);
 	if (!port)
 		usage(argv[0]);
 
-	sock = socket(AF_INET, type, 0);
+	sock = socket(AF_INET, type, protocol);
 	if (sock < 0) {
 		perror("socket");
 		exit(1);
tests/inet_socket/client.c +21 -12
index f8780d9..d3fedf4 100644
@@ -16,25 +16,28 @@
 #include <stdbool.h>
 #include <selinux/selinux.h>
 
+#ifndef IPPROTO_MPTCP
+#define IPPROTO_MPTCP 262
+#endif
+
 void usage(char *progname)
 {
 	fprintf(stderr,
-		"usage:  %s [-e expected_msg] [stream|dgram] addr port\n"
+		"usage:  %s [-e expected_msg] protocol addr port\n"
 		"\nWhere:\n\t"
-		"-e      Optional expected message from server e.g. \"nopeer\".\n\t"
-		"        If not present the client context will be used as a\n\t"
-		"        comparison with the servers reply.\n\t"
-		"stream  Use TCP protocol or:\n\t"
-		"dgram   use UDP protocol.\n\t"
-		"addr    IPv4 or IPv6 address (e.g. 127.0.0.1 or ::1)\n\t"
-		"port    Port for accessing server.\n", progname);
+		"-e        Optional expected message from server e.g. \"nopeer\".\n\t"
+		"          If not present the client context will be used as a\n\t"
+		"          comparison with the servers reply.\n\t"
+		"protocol  Protocol to use (tcp, udp, or mptcp)\n\t"
+		"addr      IPv4 or IPv6 address (e.g. 127.0.0.1 or ::1)\n\t"
+		"port      Port for accessing server.\n", progname);
 	exit(1);
 }
 
 int main(int argc, char **argv)
 {
 	char byte, label[256], *expected = NULL;
-	int sock, result, opt;
+	int sock, result, sockprotocol, opt;
 	struct addrinfo hints, *serverinfo;
 	struct timeval tm;
 
@@ -53,12 +56,18 @@ int main(int argc, char **argv)
 
 	memset(&hints, 0, sizeof(struct addrinfo));
 
-	if (!strcmp(argv[optind], "stream")) {
+	if (!strcmp(argv[optind], "tcp")) {
+		hints.ai_socktype = SOCK_STREAM;
+		hints.ai_protocol = IPPROTO_TCP;
+		sockprotocol      = IPPROTO_TCP;
+	} else if (!strcmp(argv[optind], "mptcp")) {
 		hints.ai_socktype = SOCK_STREAM;
 		hints.ai_protocol = IPPROTO_TCP;
-	} else if (!strcmp(argv[optind], "dgram")) {
+		sockprotocol      = IPPROTO_MPTCP;
+	} else if (!strcmp(argv[optind], "udp")) {
 		hints.ai_socktype = SOCK_DGRAM;
 		hints.ai_protocol = IPPROTO_UDP;
+		sockprotocol      = IPPROTO_UDP;
 	} else {
 		usage(argv[0]);
 	}
@@ -71,7 +80,7 @@ int main(int argc, char **argv)
 	}
 
 	sock = socket(serverinfo->ai_family, serverinfo->ai_socktype,
-		      serverinfo->ai_protocol);
+		      sockprotocol);
 	if (sock < 0) {
 		perror("socket");
 		exit(3);
tests/inet_socket/connect.c +22 -5
index 0abadc9..e2d02da 100644
@@ -11,10 +11,13 @@
 #include <errno.h>
 #include <poll.h>
 
+#ifndef IPPROTO_MPTCP
+#define IPPROTO_MPTCP 262
+#endif
+
 void usage(char *progname)
 {
-	fprintf(stderr,
-		"usage:  %s port\n", progname);
+	fprintf(stderr, "usage:  %s protocol port\n", progname);
 	exit(1);
 }
 
@@ -27,16 +30,30 @@ main(int argc, char **argv)
 	int result;
 	struct sockaddr_in sin;
 	socklen_t sinlen;
+	int type, protocol;
 	unsigned short port;
 
-	if (argc != 2)
+	if (argc != 3)
+		usage(argv[0]);
+
+	if (!strcmp(argv[1], "tcp")) {
+		type = SOCK_STREAM;
+		protocol = IPPROTO_TCP;
+	} else if (!strcmp(argv[1], "mptcp")) {
+		type = SOCK_STREAM;
+		protocol = IPPROTO_MPTCP;
+	} else if (!strcmp(argv[1], "udp")) {
+		type = SOCK_DGRAM;
+		protocol = IPPROTO_UDP;
+	} else {
 		usage(argv[0]);
+	}
 
-	port = atoi(argv[1]);
+	port = atoi(argv[2]);
 	if (!port)
 		usage(argv[0]);
 
-	ssock = socket(AF_INET, SOCK_STREAM, 0);
+	ssock = socket(AF_INET, type, protocol);
 	if (ssock < 0) {
 		perror("socket");
 		exit(1);
tests/inet_socket/mptcp new file +1 -0
new file mode 120000
index 0000000..945c9b4
@@ -0,0 +1 @@
+.
\ No newline at end of file
tests/inet_socket/server.c +21 -12
index 234c2d6..63b6849 100644
@@ -18,24 +18,27 @@
 #define SCM_SECURITY 0x03
 #endif
 
+#ifndef IPPROTO_MPTCP
+#define IPPROTO_MPTCP 262
+#endif
+
 void usage(char *progname)
 {
 	fprintf(stderr,
-		"usage:  %s [-f file] [-n] [stream|dgram] port\n"
+		"usage:  %s [-f file] [-n] protocol port\n"
 		"\nWhere:\n\t"
-		"-f      Write a line to the file when listening starts.\n\t"
-		"-n      No peer context will be available therefore send\n\t"
-		"        \"nopeer\" message to client, otherwise the peer context\n\t"
-		"        will be retrieved and sent to client.\n\t"
-		"stream  Use TCP protocol or:\n\t"
-		"dgram   use UDP protocol.\n\t"
-		"port    Listening port\n", progname);
+		"-f        Write a line to the file when listening starts.\n\t"
+		"-n        No peer context will be available therefore send\n\t"
+		"          \"nopeer\" message to client, otherwise the peer context\n\t"
+		"          will be retrieved and sent to client.\n\t"
+		"protocol  Protocol to use (tcp, udp, or mptcp)\n\t"
+		"port      Listening port\n", progname);
 	exit(1);
 }
 
 int main(int argc, char **argv)
 {
-	int sock, result, opt, on = 1;
+	int sock, result, opt, sockprotocol, on = 1;
 	socklen_t sinlen;
 	struct sockaddr_storage sin;
 	struct addrinfo hints, *res;
@@ -63,12 +66,18 @@ int main(int argc, char **argv)
 	hints.ai_flags = AI_PASSIVE;
 	hints.ai_family = AF_INET6;
 
-	if (!strcmp(argv[optind], "stream")) {
+	if (!strcmp(argv[optind], "tcp")) {
+		hints.ai_socktype = SOCK_STREAM;
+		hints.ai_protocol = IPPROTO_TCP;
+		sockprotocol      = IPPROTO_TCP;
+	} else if (!strcmp(argv[optind], "mptcp")) {
 		hints.ai_socktype = SOCK_STREAM;
 		hints.ai_protocol = IPPROTO_TCP;
-	} else if (!strcmp(argv[optind], "dgram")) {
+		sockprotocol      = IPPROTO_MPTCP;
+	} else if (!strcmp(argv[optind], "udp")) {
 		hints.ai_socktype = SOCK_DGRAM;
 		hints.ai_protocol = IPPROTO_UDP;
+		sockprotocol      = IPPROTO_UDP;
 	} else {
 		usage(argv[0]);
 	}
@@ -79,7 +88,7 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+	sock = socket(res->ai_family, res->ai_socktype, sockprotocol);
 	if (sock < 0) {
 		perror("socket");
 		exit(1);
tests/inet_socket/tcp new file +1 -0
new file mode 120000
index 0000000..945c9b4
@@ -0,0 +1 @@
+.
\ No newline at end of file
tests/inet_socket/test +201 -229

601 lines not on this page · raw message

tests/inet_socket/udp new file +1 -0
new file mode 120000
index 0000000..945c9b4
@@ -0,0 +1 @@
+.
\ No newline at end of file
Signature · 1 line
2.41.0
#1 lore raw
Quoted 11 lines · Ondrej Mosnacek

As of kernel 6.5 MPTCP should work as a drop-in replacement for TCP w.r.t. SELinux, so test it in addition to TCP using the same tests.

This requires modifying the inet_socket test to test each protocol separately, using the same subdirectory symlink trick as in the [fs_]filesystem tests. After that, an mptcp subtest can be added easily by extending the programs to support it and adding a new subdirectory symlink for it.

Quoted 14 lines
---
 tests/Makefile              |  10 +-
 tests/inet_socket/bind.c    |  22 +-
 tests/inet_socket/client.c  |  33 ++-
 tests/inet_socket/connect.c |  27 ++-
 tests/inet_socket/mptcp     |   1 +
 tests/inet_socket/server.c  |  33 ++-
 tests/inet_socket/tcp       |   1 +
 tests/inet_socket/test      | 430 +++++++++++++++++-------------------
 tests/inet_socket/udp       |   1 +
 9 files changed, 292 insertions(+), 266 deletions(-)
 create mode 120000 tests/inet_socket/mptcp
 create mode 120000 tests/inet_socket/tcp
 create mode 120000 tests/inet_socket/udp

The new test now runs and passes on the latest secnext kernel, so I've applied the patch:
https://github.com/SELinuxProject/selinux-testsuite/commit/4dcea27ada77f51c2868095e951aab790374fba9

Signature · 3 lines
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.