ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
6f381c
From 1f408c8d9739b1038012eeec7bf0f918c8095bc4 Mon Sep 17 00:00:00 2001
6f381c
From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= <cgzones@googlemail.com>
6f381c
Date: Fri, 23 Sep 2022 19:00:22 +0200
6f381c
Subject: [PATCH] core: respect SELinuxContext= for socket creation
6f381c
6f381c
On socket creation respect the SELinuxContext= setting of the associated
6f381c
service, such that the initial created socket has the same label as the
6f381c
future process accepting the connection (since w.r.t SELinux sockets
6f381c
normally have the same label as the owning process).
6f381c
6f381c
Triggered by #24702
6f381c
6f381c
(cherry picked from commit 599b384924bbef9f8f7fa5700c6fa35a404d9a98)
6f381c
6f381c
Related: #2136738
6f381c
---
6f381c
 src/core/socket.c | 15 ++++++++++++++-
6f381c
 1 file changed, 14 insertions(+), 1 deletion(-)
6f381c
6f381c
diff --git a/src/core/socket.c b/src/core/socket.c
6f381c
index 9d47ca2616..d1ca0a07c5 100644
6f381c
--- a/src/core/socket.c
6f381c
+++ b/src/core/socket.c
6f381c
@@ -1427,6 +1427,7 @@ fail:
6f381c
 static int socket_determine_selinux_label(Socket *s, char **ret) {
6f381c
         Service *service;
6f381c
         ExecCommand *c;
6f381c
+        const char *exec_context;
6f381c
         _cleanup_free_ char *path = NULL;
6f381c
         int r;
6f381c
 
6f381c
@@ -1448,8 +1449,20 @@ static int socket_determine_selinux_label(Socket *s, char **ret) {
6f381c
 
6f381c
                 if (!UNIT_ISSET(s->service))
6f381c
                         goto no_label;
6f381c
-
6f381c
                 service = SERVICE(UNIT_DEREF(s->service));
6f381c
+
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
+
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;