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