|
|
17b0f1 |
From 0e64363bb21a07fa318017ea8c90597db63a9545 Mon Sep 17 00:00:00 2001
|
|
|
17b0f1 |
From: David Tardon <dtardon@redhat.com>
|
|
|
17b0f1 |
Date: Thu, 3 Jan 2019 14:37:34 +0100
|
|
|
17b0f1 |
Subject: [PATCH] fs-util: add new CHASE_SAFE flag to chase_symlinks()
|
|
|
17b0f1 |
|
|
|
17b0f1 |
When the flag is specified we won't transition to a privilege-owned
|
|
|
17b0f1 |
file or directory from an unprivileged-owned one. This is useful when
|
|
|
17b0f1 |
privileged code wants to load data from a file unprivileged users have
|
|
|
17b0f1 |
write access to, and validates the ownership, but want's to make sure
|
|
|
17b0f1 |
that no symlink games are played to read a root-owned system file
|
|
|
17b0f1 |
belonging to a different context.
|
|
|
17b0f1 |
|
|
|
17b0f1 |
(cherry picked from commit f14f1806e329fe92d01f15c22a384702f0cb4ae0)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
Related: #1663143
|
|
|
17b0f1 |
---
|
|
|
17b0f1 |
src/shared/util.c | 43 +++++++++++++++++++++++++++++++++++++++++++
|
|
|
17b0f1 |
src/shared/util.h | 7 ++++---
|
|
|
17b0f1 |
src/test/test-util.c | 26 ++++++++++++++++++++++++++
|
|
|
17b0f1 |
3 files changed, 73 insertions(+), 3 deletions(-)
|
|
|
17b0f1 |
|
|
|
17b0f1 |
diff --git a/src/shared/util.c b/src/shared/util.c
|
|
|
17b0f1 |
index 385551f2b3..fc4887920f 100644
|
|
|
17b0f1 |
--- a/src/shared/util.c
|
|
|
17b0f1 |
+++ b/src/shared/util.c
|
|
|
17b0f1 |
@@ -9203,10 +9203,22 @@ int fd_is_fs_type(int fd, statfs_f_type_t magic_value) {
|
|
|
17b0f1 |
return is_fs_type(&s, magic_value);
|
|
|
17b0f1 |
}
|
|
|
17b0f1 |
|
|
|
17b0f1 |
+static bool safe_transition(const struct stat *a, const struct stat *b) {
|
|
|
17b0f1 |
+ /* Returns true if the transition from a to b is safe, i.e. that we never transition from unprivileged to
|
|
|
17b0f1 |
+ * privileged files or directories. Why bother? So that unprivileged code can't symlink to privileged files
|
|
|
17b0f1 |
+ * making us believe we read something safe even though it isn't safe in the specific context we open it in. */
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (a->st_uid == 0) /* Transitioning from privileged to unprivileged is always fine */
|
|
|
17b0f1 |
+ return true;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ return a->st_uid == b->st_uid; /* Otherwise we need to stay within the same UID */
|
|
|
17b0f1 |
+}
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
int chase_symlinks(const char *path, const char *original_root, unsigned flags, char **ret) {
|
|
|
17b0f1 |
_cleanup_free_ char *buffer = NULL, *done = NULL, *root = NULL;
|
|
|
17b0f1 |
_cleanup_close_ int fd = -1;
|
|
|
17b0f1 |
unsigned max_follow = 32; /* how many symlinks to follow before giving up and returning ELOOP */
|
|
|
17b0f1 |
+ struct stat previous_stat;
|
|
|
17b0f1 |
bool exists = true;
|
|
|
17b0f1 |
char *todo;
|
|
|
17b0f1 |
int r;
|
|
|
17b0f1 |
@@ -9250,6 +9262,11 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
|
|
17b0f1 |
if (fd < 0)
|
|
|
17b0f1 |
return -errno;
|
|
|
17b0f1 |
|
|
|
17b0f1 |
+ if (flags & CHASE_SAFE) {
|
|
|
17b0f1 |
+ if (fstat(fd, &previous_stat) < 0)
|
|
|
17b0f1 |
+ return -errno;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
todo = buffer;
|
|
|
17b0f1 |
for (;;) {
|
|
|
17b0f1 |
_cleanup_free_ char *first = NULL;
|
|
|
17b0f1 |
@@ -9313,6 +9330,16 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
|
|
17b0f1 |
if (fd_parent < 0)
|
|
|
17b0f1 |
return -errno;
|
|
|
17b0f1 |
|
|
|
17b0f1 |
+ if (flags & CHASE_SAFE) {
|
|
|
17b0f1 |
+ if (fstat(fd_parent, &st) < 0)
|
|
|
17b0f1 |
+ return -errno;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (!safe_transition(&previous_stat, &st))
|
|
|
17b0f1 |
+ return -EPERM;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ previous_stat = st;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
safe_close(fd);
|
|
|
17b0f1 |
fd = fd_parent;
|
|
|
17b0f1 |
|
|
|
17b0f1 |
@@ -9347,6 +9374,12 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
|
|
17b0f1 |
|
|
|
17b0f1 |
if (fstat(child, &st) < 0)
|
|
|
17b0f1 |
return -errno;
|
|
|
17b0f1 |
+ if ((flags & CHASE_SAFE) &&
|
|
|
17b0f1 |
+ !safe_transition(&previous_stat, &st))
|
|
|
17b0f1 |
+ return -EPERM;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ previous_stat = st;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
if ((flags & CHASE_NO_AUTOFS) &&
|
|
|
17b0f1 |
fd_is_fs_type(child, AUTOFS_SUPER_MAGIC) > 0)
|
|
|
17b0f1 |
return -EREMOTE;
|
|
|
17b0f1 |
@@ -9379,6 +9412,16 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
|
|
17b0f1 |
|
|
|
17b0f1 |
free(done);
|
|
|
17b0f1 |
|
|
|
17b0f1 |
+ if (flags & CHASE_SAFE) {
|
|
|
17b0f1 |
+ if (fstat(fd, &st) < 0)
|
|
|
17b0f1 |
+ return -errno;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ if (!safe_transition(&previous_stat, &st))
|
|
|
17b0f1 |
+ return -EPERM;
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ previous_stat = st;
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
/* Note that we do not revalidate the root, we take it as is. */
|
|
|
17b0f1 |
if (isempty(root))
|
|
|
17b0f1 |
done = NULL;
|
|
|
17b0f1 |
diff --git a/src/shared/util.h b/src/shared/util.h
|
|
|
17b0f1 |
index 915c7439e8..fa3e2e3009 100644
|
|
|
17b0f1 |
--- a/src/shared/util.h
|
|
|
17b0f1 |
+++ b/src/shared/util.h
|
|
|
17b0f1 |
@@ -1157,9 +1157,10 @@ bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
|
|
|
17b0f1 |
int fd_is_fs_type(int fd, statfs_f_type_t magic_value);
|
|
|
17b0f1 |
|
|
|
17b0f1 |
enum {
|
|
|
17b0f1 |
- CHASE_PREFIX_ROOT = 1, /* If set, the specified path will be prefixed by the specified root before beginning the iteration */
|
|
|
17b0f1 |
- CHASE_NONEXISTENT = 2, /* If set, it's OK if the path doesn't actually exist. */
|
|
|
17b0f1 |
- CHASE_NO_AUTOFS = 4, /* If set, return -EREMOTE if autofs mount point found */
|
|
|
17b0f1 |
+ CHASE_PREFIX_ROOT = 1U << 0, /* If set, the specified path will be prefixed by the specified root before beginning the iteration */
|
|
|
17b0f1 |
+ CHASE_NONEXISTENT = 1U << 1, /* If set, it's OK if the path doesn't actually exist. */
|
|
|
17b0f1 |
+ CHASE_NO_AUTOFS = 1U << 2, /* If set, return -EREMOTE if autofs mount point found */
|
|
|
17b0f1 |
+ CHASE_SAFE = 1U << 3, /* If set, return EPERM if we ever traverse from unprivileged to privileged files or directories */
|
|
|
17b0f1 |
};
|
|
|
17b0f1 |
|
|
|
17b0f1 |
int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret);
|
|
|
17b0f1 |
diff --git a/src/test/test-util.c b/src/test/test-util.c
|
|
|
17b0f1 |
index 397c45a9f4..e5a646ec20 100644
|
|
|
17b0f1 |
--- a/src/test/test-util.c
|
|
|
17b0f1 |
+++ b/src/test/test-util.c
|
|
|
17b0f1 |
@@ -2113,6 +2113,32 @@ static void test_chase_symlinks(void) {
|
|
|
17b0f1 |
r = chase_symlinks(p, NULL, 0, &result);
|
|
|
17b0f1 |
assert_se(r == -ENOENT);
|
|
|
17b0f1 |
|
|
|
17b0f1 |
+ if (geteuid() == 0) {
|
|
|
17b0f1 |
+ p = strjoina(temp, "/priv1");
|
|
|
17b0f1 |
+ assert_se(mkdir(p, 0755) >= 0);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ q = strjoina(p, "/priv2");
|
|
|
17b0f1 |
+ assert_se(mkdir(q, 0755) >= 0);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(chown(q, 65534, 65534) >= 0);
|
|
|
17b0f1 |
+ assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(chown(p, 65534, 65534) >= 0);
|
|
|
17b0f1 |
+ assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(chown(q, 0, 0) >= 0);
|
|
|
17b0f1 |
+ assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) == -EPERM);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(rmdir(q) >= 0);
|
|
|
17b0f1 |
+ assert_se(symlink("/etc/passwd", q) >= 0);
|
|
|
17b0f1 |
+ assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) == -EPERM);
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
+ assert_se(chown(p, 0, 0) >= 0);
|
|
|
17b0f1 |
+ assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
|
|
|
17b0f1 |
+ }
|
|
|
17b0f1 |
+
|
|
|
17b0f1 |
assert_se(rm_rf_dangerous(temp, false, true, false) >= 0);
|
|
|
17b0f1 |
}
|
|
|
17b0f1 |
|