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