dryang / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
6f381c
From 3f90090e70a5fa81bced17792fe08d9c46324da9 Mon Sep 17 00:00:00 2001
6f381c
From: "Ted X. Toth" <txtoth@flycast.org>
6f381c
Date: Thu, 13 Oct 2022 12:58:26 -0700
6f381c
Subject: [PATCH] manager: use target process context to set socket context
6f381c
6f381c
Use target process context to set socket context when using SELinuxContextFromNet
6f381c
not systemd's context. Currently when using the SELinuxContextFromNet option for
6f381c
a socket activated services, systemd calls getcon_raw which returns init_t and
6f381c
uses the resulting context to compute the context to be passed to the
6f381c
setsockcreatecon call. A socket of type init_t is created and listened on and
6f381c
this means that SELinux policy cannot be written to control which processes
6f381c
(SELinux types) can connect to the socket since the ref policy allows all
6f381c
'types' to connect to sockets of the type init_t. When security accessors see
6f381c
that any process can connect to a socket this raises serious concerns. I have
6f381c
spoken with SELinux contributors in person and on the mailing list and the
6f381c
consensus is that the best solution is to use the target executables context
6f381c
when computing the sockets context in all cases.
6f381c
6f381c
[zjs review/comment:
6f381c
6f381c
This removes the branch that was added in 16115b0a7b7cdf08fb38084d857d572d8a9088dc.
6f381c
16115b0a7b7cdf08fb38084d857d572d8a9088dc did two things: it had the branch here
6f381c
in 'socket_determine_selinux_label()' and a code in 'exec_child()' to call
6f381c
'label_get_child_mls_label(socket_fd, command->path, &label)'.
6f381c
6f381c
Before this patch, the flow was:
6f381c
'''
6f381c
mac_selinux_get_child_mls_label:
6f381c
  peercon = getpeercon_raw(socket_fd);
6f381c
  if (!exec_label)
6f381c
     exec_label = getfilecon_raw(exe);
6f381c
6f381c
socket_open_fds:
6f381c
  if (params->selinux_context_net)                 #
6f381c
     label = mac_selinux_get_our_label();          #  this part is removed
6f381c
  else                                             #
6f381c
     label = mac_selinux_get_create_label_from_exe(path);
6f381c
  socket_address_listen_in_cgroup(s, &p->address, label);
6f381c
6f381c
exec_child():
6f381c
   exec_context = mac_selinux_get_child_mls_label(fd, executable, context->selinux_context);
6f381c
   setexeccon(exec_context);
6f381c
'''
6f381c
]
6f381c
6f381c
(cherry picked from commit 29dbc62d74f7b7881dc3136e68e03a03ea055b36)
6f381c
6f381c
Resolves: #2136738
6f381c
---
6f381c
 src/core/socket.c | 58 ++++++++++++++++++++---------------------------
6f381c
 1 file changed, 24 insertions(+), 34 deletions(-)
6f381c
6f381c
diff --git a/src/core/socket.c b/src/core/socket.c
6f381c
index d1ca0a07c5..8aa5463b25 100644
6f381c
--- a/src/core/socket.c
6f381c
+++ b/src/core/socket.c
6f381c
@@ -1434,47 +1434,37 @@ static int socket_determine_selinux_label(Socket *s, char **ret) {
6f381c
         assert(s);
6f381c
         assert(ret);
6f381c
 
6f381c
-        if (s->selinux_context_from_net) {
6f381c
-                /* If this is requested, get label from the network label */
6f381c
-
6f381c
-                r = mac_selinux_get_our_label(ret);
6f381c
-                if (r == -EOPNOTSUPP)
6f381c
-                        goto no_label;
6f381c
-
6f381c
-        } else {
6f381c
-                /* Otherwise, get it from the executable we are about to start */
6f381c
-                r = socket_instantiate_service(s);
6f381c
-                if (r < 0)
6f381c
-                        return r;
6f381c
+        r = socket_instantiate_service(s);
6f381c
+        if (r < 0)
6f381c
+                return r;
6f381c
 
6f381c
-                if (!UNIT_ISSET(s->service))
6f381c
-                        goto no_label;
6f381c
-                service = SERVICE(UNIT_DEREF(s->service));
6f381c
+        if (!UNIT_ISSET(s->service))
6f381c
+                goto no_label;
6f381c
+        service = SERVICE(UNIT_DEREF(s->service));
6f381c
 
6f381c
-                exec_context = service->exec_context.selinux_context;
6f381c
-                if (exec_context) {
6f381c
-                        char *con;
6f381c
+        exec_context = service->exec_context.selinux_context;
6f381c
+        if (exec_context) {
6f381c
+                char *con;
6f381c
 
6f381c
-                        con = strdup(exec_context);
6f381c
-                        if (!con)
6f381c
-                                return -ENOMEM;
6f381c
+                con = strdup(exec_context);
6f381c
+                if (!con)
6f381c
+                        return -ENOMEM;
6f381c
 
6f381c
-                        *ret = TAKE_PTR(con);
6f381c
-                        return 0;
6f381c
-                }
6f381c
+                *ret = TAKE_PTR(con);
6f381c
+                return 0;
6f381c
+        }
6f381c
 
6f381c
-                c = service->exec_command[SERVICE_EXEC_START];
6f381c
-                if (!c)
6f381c
-                        goto no_label;
6f381c
+        c = service->exec_command[SERVICE_EXEC_START];
6f381c
+        if (!c)
6f381c
+                goto no_label;
6f381c
 
6f381c
-                r = chase_symlinks(c->path, service->exec_context.root_directory, CHASE_PREFIX_ROOT, &path);
6f381c
-                if (r < 0)
6f381c
-                        goto no_label;
6f381c
+        r = chase_symlinks(c->path, service->exec_context.root_directory, CHASE_PREFIX_ROOT, &path);
6f381c
+        if (r < 0)
6f381c
+                goto no_label;
6f381c
 
6f381c
-                r = mac_selinux_get_create_label_from_exe(path, ret);
6f381c
-                if (IN_SET(r, -EPERM, -EOPNOTSUPP))
6f381c
-                        goto no_label;
6f381c
-        }
6f381c
+        r = mac_selinux_get_create_label_from_exe(path, ret);
6f381c
+        if (IN_SET(r, -EPERM, -EOPNOTSUPP))
6f381c
+                goto no_label;
6f381c
 
6f381c
         return r;
6f381c