Skip to contentSkip to conversation

DEBUG

RFC PATCH In review Ondrej Mosnáček 1 participant 3 messages Nov 2 SELinux security module

lore
Share & tools
lore

Ondrej Mosnáček

author reviewer
#0 lore raw

RFC PATCH 1/2 DEBUG

---
 tests/Makefile | 2 ++
 1 file changed, 2 insertions(+)
tests/Makefile +2 -0
index 4c8e82b..8108b40 100644
@@ -202,6 +202,8 @@ ifeq ($(DISTRO),RHEL7)
     SUBDIRS:=$(filter-out netlink_socket, $(SUBDIRS))
 endif
 
+SUBDIRS:=unix_socket
+
 .PHONY: all test clean
 
 all:
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

RFC PATCH 2/2 tests/unix_socket: add test for MSG_CMSG_SEALED

Test the new MSG_CMSG_SEALED flag for UNIX sockets. This flag allows forwarding file descriptors via SCM_RIGHTS without having read/write access to the file handle being transported. The file descriptor received with this flag is an opaque anon inode that doesn't allow accessing the original file handle - it can only be passed further via SCM_RIGHTS again and then received normally by the destination process. The LSM checks for a received "sealed" file are skipped and only performed when the file is eventually received "unsealed".

The test verifies that a domain using this mechanism to forward a file handle doesn't need to have access to the file to do so and that the final recipient does.

The test is automatically skipped when the kernel doesn't support this feature, which is determined using the SO_RIGHTS_SEALED_SUPPORTED socket option on a dummy UNIX socket.

---
 policy/test_unix_socket.te          |  50 +++++++++
 tests/unix_socket/.gitignore        |   4 +
 tests/unix_socket/Makefile          |   2 +-
 tests/unix_socket/fd_forwarder.c    | 158 ++++++++++++++++++++++++++++
 tests/unix_socket/fd_receiver.c     |  98 +++++++++++++++++
 tests/unix_socket/fd_sealed_check.c |  26 +++++
 tests/unix_socket/fd_sender.c       |  88 ++++++++++++++++
 tests/unix_socket/test              |  83 +++++++++++++--
 8 files changed, 500 insertions(+), 9 deletions(-)
 create mode 100644 tests/unix_socket/fd_forwarder.c
 create mode 100644 tests/unix_socket/fd_receiver.c
 create mode 100644 tests/unix_socket/fd_sealed_check.c
 create mode 100644 tests/unix_socket/fd_sender.c
policy/test_unix_socket.te +50 -0
index f4e9e41..4d0f758 100644
@@ -58,3 +58,53 @@ allow test_unix_server_t test_unix_dgram_client_t:unix_dgram_socket sendto;
 type test_socketpair_t;
 testsuite_domain_type(test_socketpair_t)
 typeattribute test_socketpair_t unixsocketdomain;
+
+#
+# Domains for MSG_CMSG_SEALED tests.
+#
+type test_unix_payload_t;
+files_type(test_unix_payload_t)
+
+# Forwarder domain (can't rw payload; only pass it along)
+type test_unix_fd_forwarder_t;
+testsuite_domain_type(test_unix_fd_forwarder_t)
+typeattribute test_unix_fd_forwarder_t unixsocketdomain;
+
+type_transition test_unix_fd_forwarder_t test_file_t:sock_file test_unix_server_sockfile_t;
+allow test_unix_fd_forwarder_t test_file_t:dir rw_dir_perms;
+allow test_unix_fd_forwarder_t test_unix_server_sockfile_t:sock_file { create unlink };
+
+# For writing to flag file:
+allow test_unix_fd_forwarder_t test_file_t:fifo_file rw_file_perms;
+
+neverallow test_unix_fd_forwarder_t test_unix_payload_t:file { read write };
+
+# Sender domain
+type test_unix_fd_sender_t;
+testsuite_domain_type(test_unix_fd_sender_t)
+typeattribute test_unix_fd_sender_t unixsocketdomain;
+
+allow test_unix_fd_sender_t test_unix_fd_forwarder_t:unix_stream_socket connectto;
+allow test_unix_fd_sender_t test_unix_server_sockfile_t:sock_file write;
+
+allow test_unix_fd_sender_t test_unix_payload_t:file rw_file_perms;
+
+# Receiver domain (can rw payload)
+type test_unix_fd_receiver_t;
+testsuite_domain_type(test_unix_fd_receiver_t)
+typeattribute test_unix_fd_receiver_t unixsocketdomain;
+
+allow test_unix_fd_receiver_t test_unix_fd_forwarder_t:unix_stream_socket connectto;
+allow test_unix_fd_receiver_t test_unix_server_sockfile_t:sock_file write;
+
+allow test_unix_fd_receiver_t test_unix_payload_t:file rw_inherited_file_perms;
+
+# Bad receiver domain (can't rw payload - should fail receiving it)
+type test_unix_fd_bad_receiver_t;
+testsuite_domain_type(test_unix_fd_bad_receiver_t)
+typeattribute test_unix_fd_bad_receiver_t unixsocketdomain;
+
+allow test_unix_fd_bad_receiver_t test_unix_fd_forwarder_t:unix_stream_socket connectto;
+allow test_unix_fd_bad_receiver_t test_unix_server_sockfile_t:sock_file write;
+
+neverallow test_unix_fd_bad_receiver_t test_unix_payload_t:file { read write };
tests/unix_socket/.gitignore +4 -0
index 05ed268..f3eb70d 100644
@@ -1,3 +1,7 @@
 client
 server
 socketpair
+fd_sealed_check
+fd_forwarder
+fd_receiver
+fd_sender
tests/unix_socket/Makefile +1 -1
index f8db20b..f8b892d 100644
@@ -1,4 +1,4 @@
-TARGETS=client server socketpair
+TARGETS=client server socketpair fd_sealed_check fd_forwarder fd_receiver fd_sender
 
 LDLIBS+= -lselinux
 
tests/unix_socket/fd_forwarder.c new file +158 -0

158 lines not on this page · raw message

tests/unix_socket/fd_receiver.c new file +98 -0

98 lines not on this page · raw message

tests/unix_socket/fd_sealed_check.c new file +26 -0
new file mode 100644
index 0000000..cf7b47b
@@ -0,0 +1,26 @@
+#include <sys/socket.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#ifndef SO_RIGHTS_SEALED_SUPPORTED
+#define SO_RIGHTS_SEALED_SUPPORTED 88
+#endif
+
+int main(int argc, char **argv)
+{
+    int sock, ret;
+    socklen_t optlen = 0;
+
+    sock = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (sock < 0) {
+        perror("socket");
+        exit(2);
+    }
+
+    ret = getsockopt(sock, SOL_SOCKET, SO_RIGHTS_SEALED_SUPPORTED,
+                     NULL, &optlen);
+    close(sock);
+
+    return ret == 0 ? 0 : 1;
+}
tests/unix_socket/fd_sender.c new file +88 -0

88 lines not on this page · raw message

tests/unix_socket/test +75 -8

134 lines not on this page · raw message

Signature · 1 line
2.54.0
author reviewer
#2 lore raw
In reply to Ondrej Mosnáček
Quoted 19 lines · Ondrej Mosnáček
---
 tests/Makefile | 2 ++
 1 file changed, 2 insertions(+)
tests/Makefile +2 -1
index 4c8e82b..8108b40 100644
@@ -202,6 +202,8 @@ ifeq ($(DISTRO),RHEL7)
     SUBDIRS:=$(filter-out netlink_socket, $(SUBDIRS))
 endif
 
+SUBDIRS:=unix_socket
+
 .PHONY: all test clean
 
 all:
--
Quoted 2 lines

2.54.0

Eh, I didn't intend to send this patch, sorry... resent a v2 without it.