]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
selinux: support infering SELinux label also from socket not connected to stdin
authorMichal Sekletar <msekleta@redhat.com>
Tue, 29 Jun 2021 15:10:27 +0000 (17:10 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 2 Jul 2021 07:26:22 +0000 (09:26 +0200)
Fixes #19918

man/systemd.socket.xml
src/core/execute.c

index b669e7d15d1b2bc74bd15e2a747fc99dbd753eaf..1600656fdbb9123af67d0e28dca7da5375926755 100644 (file)
          resulting SELinux context originate from either the target
          binary that is effectively triggered by socket unit or from
          the value of the <varname>SELinuxContext=</varname> option.
-         This configuration option only affects sockets with
-         <varname>Accept=</varname> mode set to
-         <literal>yes</literal>. Also note that this option is useful
-         only when MLS/MCS SELinux policy is deployed. Defaults to
+         This configuration option applies only when activated service
+         is passed in single socket file descriptor, i.e. service
+         instances that have standard input connected to a socket or
+         services triggered by exactly one socket unit. Also note
+         that this option is useful only when MLS/MCS SELinux policy
+         is deployed. Defaults to
          <literal>false</literal>. </para></listitem>
       </varlistentry>
 
index af24f9f71358db2afa52e4c8620fdd3d358c766e..2a337b55a2a34f12d711896869428d47cbcc73ab 100644 (file)
@@ -4345,11 +4345,22 @@ static int exec_child(
         }
 
 #if HAVE_SELINUX
-        if (needs_sandboxing && use_selinux && params->selinux_context_net && socket_fd >= 0) {
-                r = mac_selinux_get_child_mls_label(socket_fd, executable, context->selinux_context, &mac_selinux_context_net);
-                if (r < 0) {
-                        *exit_status = EXIT_SELINUX_CONTEXT;
-                        return log_unit_error_errno(unit, r, "Failed to determine SELinux context: %m");
+        if (needs_sandboxing && use_selinux && params->selinux_context_net) {
+                int fd = -1;
+
+                if (socket_fd >= 0)
+                        fd = socket_fd;
+                else if (params->n_socket_fds == 1)
+                        /* If stdin is not connected to a socket but we are triggered by exactly one socket unit then we
+                         * use context from that fd to compute the label. */
+                        fd = params->fds[0];
+
+                if (fd >= 0) {
+                        r = mac_selinux_get_child_mls_label(fd, executable, context->selinux_context, &mac_selinux_context_net);
+                        if (r < 0) {
+                                *exit_status = EXIT_SELINUX_CONTEXT;
+                                return log_unit_error_errno(unit, r, "Failed to determine SELinux context: %m");
+                        }
                 }
         }
 #endif