naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
923a60
From c211b650ee5cb9934067dbba40718a4a33063e06 Mon Sep 17 00:00:00 2001
923a60
From: David Tardon <dtardon@redhat.com>
923a60
Date: Thu, 3 Jan 2019 14:44:36 +0100
923a60
Subject: [PATCH] fs-util: add new chase_symlinks() flag CHASE_OPEN
923a60
923a60
The new flag returns the O_PATH fd of the final component, which may be
923a60
converted into a proper fd by open()ing it again through the
923a60
/proc/self/fd/xyz path.
923a60
923a60
Together with O_SAFE this provides us with a somewhat safe way to open()
923a60
files in directories potentially owned by unprivileged code, where we
923a60
want to refuse operation if any symlink tricks are played pointing to
923a60
privileged files.
923a60
923a60
(cherry picked from commit 1ed34d75d4f21d2335c5625261954c848d176ae6)
923a60
923a60
Related: #1663143
923a60
---
923a60
 Makefile.am          |  1 +
923a60
 src/shared/util.c    | 17 +++++++++++++
923a60
 src/shared/util.h    |  1 +
923a60
 src/test/test-util.c | 59 +++++++++++++++++++++++++++++++++++++++++++-
923a60
 4 files changed, 77 insertions(+), 1 deletion(-)
923a60
923a60
diff --git a/Makefile.am b/Makefile.am
923a60
index 995c421b8b..648f54b957 100644
923a60
--- a/Makefile.am
923a60
+++ b/Makefile.am
923a60
@@ -1675,6 +1675,7 @@ test_util_SOURCES = \
923a60
 	src/test/test-util.c
923a60
 
923a60
 test_util_LDADD = \
923a60
+	libsystemd-internal.la \
923a60
 	libsystemd-shared.la
923a60
 
923a60
 test_path_lookup_SOURCES = \
923a60
diff --git a/src/shared/util.c b/src/shared/util.c
923a60
index fc4887920f..354d15ff18 100644
923a60
--- a/src/shared/util.c
923a60
+++ b/src/shared/util.c
923a60
@@ -9225,6 +9225,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
923a60
 
923a60
         assert(path);
923a60
 
923a60
+        /* Either the file may be missing, or we return an fd to the final object, but both make no sense */
923a60
+        if ((flags & (CHASE_NONEXISTENT|CHASE_OPEN)) == (CHASE_NONEXISTENT|CHASE_OPEN))
923a60
+                return -EINVAL;
923a60
+
923a60
         /* This is a lot like canonicalize_file_name(), but takes an additional "root" parameter, that allows following
923a60
          * symlinks relative to a root directory, instead of the root of the host.
923a60
          *
923a60
@@ -9476,5 +9480,18 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
923a60
                 done = NULL;
923a60
         }
923a60
 
923a60
+        if (flags & CHASE_OPEN) {
923a60
+                int q;
923a60
+
923a60
+                /* Return the O_PATH fd we currently are looking to the caller. It can translate it to a proper fd by
923a60
+                 * opening /proc/self/fd/xyz. */
923a60
+
923a60
+                assert(fd >= 0);
923a60
+                q = fd;
923a60
+                fd = -1;
923a60
+
923a60
+                return q;
923a60
+        }
923a60
+
923a60
         return exists;
923a60
 }
923a60
diff --git a/src/shared/util.h b/src/shared/util.h
923a60
index fa3e2e3009..d89f0d34a1 100644
923a60
--- a/src/shared/util.h
923a60
+++ b/src/shared/util.h
923a60
@@ -1161,6 +1161,7 @@ enum {
923a60
         CHASE_NONEXISTENT = 1U << 1,   /* If set, it's OK if the path doesn't actually exist. */
923a60
         CHASE_NO_AUTOFS   = 1U << 2,   /* If set, return -EREMOTE if autofs mount point found */
923a60
         CHASE_SAFE        = 1U << 3,   /* If set, return EPERM if we ever traverse from unprivileged to privileged files or directories */
923a60
+        CHASE_OPEN        = 1U << 4,   /* If set, return an O_PATH object to the final component */
923a60
 };
923a60
 
923a60
 int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret);
923a60
diff --git a/src/test/test-util.c b/src/test/test-util.c
923a60
index e5a646ec20..8ef3850e10 100644
923a60
--- a/src/test/test-util.c
923a60
+++ b/src/test/test-util.c
923a60
@@ -1910,11 +1910,45 @@ static void test_acquire_data_fd(void) {
923a60
         test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE);
923a60
 }
923a60
 
923a60
+static int id128_read_fd(int fd, sd_id128_t *ret) {
923a60
+        char buf[33];
923a60
+        ssize_t k;
923a60
+        unsigned j;
923a60
+        sd_id128_t t;
923a60
+
923a60
+        assert_return(fd >= 0, -EINVAL);
923a60
+
923a60
+        k = loop_read(fd, buf, 33, false);
923a60
+        if (k < 0)
923a60
+                return (int) k;
923a60
+
923a60
+        if (k != 33)
923a60
+                return -EIO;
923a60
+
923a60
+        if (buf[32] !='\n')
923a60
+                return -EIO;
923a60
+
923a60
+        for (j = 0; j < 16; j++) {
923a60
+                int a, b;
923a60
+
923a60
+                a = unhexchar(buf[j*2]);
923a60
+                b = unhexchar(buf[j*2+1]);
923a60
+
923a60
+                if (a < 0 || b < 0)
923a60
+                        return -EIO;
923a60
+
923a60
+                t.bytes[j] = a << 4 | b;
923a60
+        }
923a60
+
923a60
+        *ret = t;
923a60
+        return 0;
923a60
+}
923a60
+
923a60
 static void test_chase_symlinks(void) {
923a60
         _cleanup_free_ char *result = NULL;
923a60
         char temp[] = "/tmp/test-chase.XXXXXX";
923a60
         const char *top, *p, *pslash, *q, *qslash;
923a60
-        int r;
923a60
+        int r, pfd;
923a60
 
923a60
         assert_se(mkdtemp(temp));
923a60
 
923a60
@@ -2139,6 +2173,29 @@ static void test_chase_symlinks(void) {
923a60
                 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
923a60
         }
923a60
 
923a60
+        p = strjoina(temp, "/machine-id-test");
923a60
+        assert_se(symlink("/usr/../etc/./machine-id", p) >= 0);
923a60
+
923a60
+        pfd = chase_symlinks(p, NULL, CHASE_OPEN, NULL);
923a60
+        if (pfd != -ENOENT) {
923a60
+                char procfs[sizeof("/proc/self/fd/") - 1 + DECIMAL_STR_MAX(pfd) + 1];
923a60
+                _cleanup_close_ int fd = -1;
923a60
+                sd_id128_t a, b;
923a60
+
923a60
+                assert_se(pfd >= 0);
923a60
+
923a60
+                xsprintf(procfs, "/proc/self/fd/%i", pfd);
923a60
+
923a60
+                fd = open(procfs, O_RDONLY|O_CLOEXEC);
923a60
+                assert_se(fd >= 0);
923a60
+
923a60
+                safe_close(pfd);
923a60
+
923a60
+                assert_se(id128_read_fd(fd, &a) >= 0);
923a60
+                assert_se(sd_id128_get_machine(&b) >= 0);
923a60
+                assert_se(sd_id128_equal(a, b));
923a60
+        }
923a60
+
923a60
         assert_se(rm_rf_dangerous(temp, false, true, false) >= 0);
923a60
 }
923a60