6136c1
From 4f4e8bbd9ad46fc146a36f52790bc4920f42ef1f Mon Sep 17 00:00:00 2001
6136c1
From: Franck Bui <fbui@suse.com>
6136c1
Date: Mon, 2 Jul 2018 10:22:56 +0200
6136c1
Subject: [PATCH] selinux: introduce mac_selinux_create_file_prepare_at()
6136c1
6136c1
(cherry picked from commit 7e531a5265687aef5177b070c36ca4ceab42e768)
6136c1
6136c1
Related: #1888912
6136c1
---
6136c1
 src/basic/selinux-util.c | 83 ++++++++++++++++++++++++++++++----------
6136c1
 src/basic/selinux-util.h |  1 +
6136c1
 2 files changed, 63 insertions(+), 21 deletions(-)
6136c1
6136c1
diff --git a/src/basic/selinux-util.c b/src/basic/selinux-util.c
6136c1
index f69d88eb1e..a078ce23ef 100644
6136c1
--- a/src/basic/selinux-util.c
6136c1
+++ b/src/basic/selinux-util.c
6136c1
@@ -336,48 +336,89 @@ char* mac_selinux_free(char *label) {
6136c1
         return NULL;
6136c1
 }
6136c1
 
6136c1
-int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
6136c1
-
6136c1
 #if HAVE_SELINUX
6136c1
+static int selinux_create_file_prepare_abspath(const char *abspath, mode_t mode) {
6136c1
         _cleanup_freecon_ char *filecon = NULL;
6136c1
+        _cleanup_free_ char *path = NULL;
6136c1
         int r;
6136c1
 
6136c1
-        assert(path);
6136c1
-
6136c1
-        if (!label_hnd)
6136c1
-                return 0;
6136c1
-
6136c1
-        if (path_is_absolute(path))
6136c1
-                r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
6136c1
-        else {
6136c1
-                _cleanup_free_ char *newpath = NULL;
6136c1
-
6136c1
-                r = path_make_absolute_cwd(path, &newpath);
6136c1
-                if (r < 0)
6136c1
-                        return r;
6136c1
-
6136c1
-                r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode);
6136c1
-        }
6136c1
+        assert(abspath);
6136c1
+        assert(path_is_absolute(abspath));
6136c1
 
6136c1
+        r = selabel_lookup_raw(label_hnd, &filecon, abspath, mode);
6136c1
         if (r < 0) {
6136c1
                 /* No context specified by the policy? Proceed without setting it. */
6136c1
                 if (errno == ENOENT)
6136c1
                         return 0;
6136c1
 
6136c1
-                log_enforcing_errno(errno, "Failed to determine SELinux security context for %s: %m", path);
6136c1
+                log_enforcing_errno(errno, "Failed to determine SELinux security context for %s: %m", abspath);
6136c1
         } else {
6136c1
                 if (setfscreatecon_raw(filecon) >= 0)
6136c1
                         return 0; /* Success! */
6136c1
 
6136c1
-                log_enforcing_errno(errno, "Failed to set SELinux security context %s for %s: %m", filecon, path);
6136c1
+                log_enforcing_errno(errno, "Failed to set SELinux security context %s for %s: %m", filecon, abspath);
6136c1
         }
6136c1
 
6136c1
         if (security_getenforce() > 0)
6136c1
                 return -errno;
6136c1
 
6136c1
-#endif
6136c1
         return 0;
6136c1
 }
6136c1
+#endif
6136c1
+
6136c1
+int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode) {
6136c1
+        int r = 0;
6136c1
+
6136c1
+#if HAVE_SELINUX
6136c1
+        _cleanup_free_ char *abspath = NULL;
6136c1
+        _cleanup_close_ int fd = -1;
6136c1
+
6136c1
+        assert(path);
6136c1
+
6136c1
+        if (!label_hnd)
6136c1
+                return 0;
6136c1
+
6136c1
+        if (!path_is_absolute(path)) {
6136c1
+                _cleanup_free_ char *p = NULL;
6136c1
+
6136c1
+                if (dirfd == AT_FDCWD)
6136c1
+                        r = safe_getcwd(&p);
6136c1
+                else
6136c1
+                        r = fd_get_path(dirfd, &p);
6136c1
+                if (r < 0)
6136c1
+                        return r;
6136c1
+
6136c1
+                abspath = path_join(NULL, p, path);
6136c1
+                if (!abspath)
6136c1
+                        return -ENOMEM;
6136c1
+
6136c1
+                path = abspath;
6136c1
+        }
6136c1
+
6136c1
+        r = selinux_create_file_prepare_abspath(path, mode);
6136c1
+#endif
6136c1
+        return r;
6136c1
+}
6136c1
+
6136c1
+int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
6136c1
+        int r = 0;
6136c1
+
6136c1
+#if HAVE_SELINUX
6136c1
+        _cleanup_free_ char *abspath = NULL;
6136c1
+
6136c1
+        assert(path);
6136c1
+
6136c1
+        if (!label_hnd)
6136c1
+                return 0;
6136c1
+
6136c1
+        r = path_make_absolute_cwd(path, &abspath);
6136c1
+        if (r < 0)
6136c1
+                return r;
6136c1
+
6136c1
+        r = selinux_create_file_prepare_abspath(abspath, mode);
6136c1
+#endif
6136c1
+        return r;
6136c1
+}
6136c1
 
6136c1
 void mac_selinux_create_file_clear(void) {
6136c1
 
6136c1
diff --git a/src/basic/selinux-util.h b/src/basic/selinux-util.h
6136c1
index abcfabe777..639c35b687 100644
6136c1
--- a/src/basic/selinux-util.h
6136c1
+++ b/src/basic/selinux-util.h
6136c1
@@ -24,6 +24,7 @@ int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *
6136c1
 char* mac_selinux_free(char *label);
6136c1
 
6136c1
 int mac_selinux_create_file_prepare(const char *path, mode_t mode);
6136c1
+int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode);
6136c1
 void mac_selinux_create_file_clear(void);
6136c1
 
6136c1
 int mac_selinux_create_socket_prepare(const char *label);