dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
9a102d
From 9fa7f77c5a3d92fe03d6eec8384043555ea16dc6 Mon Sep 17 00:00:00 2001
9a102d
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
9a102d
Date: Mon, 28 Nov 2022 12:12:55 +0100
9a102d
Subject: [PATCH] coredump: do not allow user to access coredumps with changed
9a102d
 uid/gid/capabilities
9a102d
9a102d
When the user starts a program which elevates its permissions via setuid,
9a102d
setgid, or capabilities set on the file, it may access additional information
9a102d
which would then be visible in the coredump. We shouldn't make the the coredump
9a102d
visible to the user in such cases.
9a102d
9a102d
Reported-by: Matthias Gerstner <mgerstner@suse.de>
9a102d
9a102d
This reads the /proc/<pid>/auxv file and attaches it to the process metadata as
9a102d
PROC_AUXV. Before the coredump is submitted, it is parsed and if either
9a102d
at_secure was set (which the kernel will do for processes that are setuid,
9a102d
setgid, or setcap), or if the effective uid/gid don't match uid/gid, the file
9a102d
is not made accessible to the user. If we can't access this data, we assume the
9a102d
file should not be made accessible either. In principle we could also access
9a102d
the auxv data from a note in the core file, but that is much more complex and
9a102d
it seems better to use the stand-alone file that is provided by the kernel.
9a102d
9a102d
Attaching auxv is both convient for this patch (because this way it's passed
9a102d
between the stages along with other fields), but I think it makes sense to save
9a102d
it in general.
9a102d
9a102d
We use the information early in the core file to figure out if the program was
9a102d
32-bit or 64-bit and its endianness. This way we don't need heuristics to guess
9a102d
whether the format of the auxv structure. This test might reject some cases on
9a102d
fringe architecutes. But the impact would be limited: we just won't grant the
9a102d
user permissions to view the coredump file. If people report that we're missing
9a102d
some cases, we can always enhance this to support more architectures.
9a102d
9a102d
I tested auxv parsing on amd64, 32-bit program on amd64, arm64, arm32, and
9a102d
ppc64el, but not the whole coredump handling.
9a102d
9a102d
(cherry picked from commit 3e4d0f6cf99f8677edd6a237382a65bfe758de03)
9a102d
9a102d
Resolves: #2155519
9a102d
---
9a102d
 src/coredump/coredump.c | 190 ++++++++++++++++++++++++++++++++++++++--
9a102d
 1 file changed, 182 insertions(+), 8 deletions(-)
9a102d
9a102d
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
9a102d
index ebc56d8342..d8acd2d3a7 100644
9a102d
--- a/src/coredump/coredump.c
9a102d
+++ b/src/coredump/coredump.c
9a102d
@@ -4,6 +4,7 @@
9a102d
 #include <stdio.h>
9a102d
 #include <stdio_ext.h>
9a102d
 #include <sys/prctl.h>
9a102d
+#include <sys/auxv.h>
9a102d
 #include <sys/xattr.h>
9a102d
 #include <unistd.h>
9a102d
 
9a102d
@@ -88,11 +89,13 @@ enum {
9a102d
         CONTEXT_COMM,
9a102d
         CONTEXT_EXE,
9a102d
         CONTEXT_UNIT,
9a102d
+        CONTEXT_PROC_AUXV,
9a102d
         _CONTEXT_MAX
9a102d
 };
9a102d
 
9a102d
 typedef struct Context {
9a102d
         const char *meta[_CONTEXT_MAX];
9a102d
+        size_t meta_size[_CONTEXT_MAX];
9a102d
 } Context;
9a102d
 
9a102d
 typedef enum CoredumpStorage {
9a102d
@@ -148,8 +151,7 @@ static inline uint64_t storage_size_max(void) {
9a102d
         return 0;
9a102d
 }
9a102d
 
9a102d
-static int fix_acl(int fd, uid_t uid) {
9a102d
-
9a102d
+static int fix_acl(int fd, uid_t uid, bool allow_user) {
9a102d
 #if HAVE_ACL
9a102d
         _cleanup_(acl_freep) acl_t acl = NULL;
9a102d
         acl_entry_t entry;
9a102d
@@ -157,6 +159,11 @@ static int fix_acl(int fd, uid_t uid) {
9a102d
         int r;
9a102d
 
9a102d
         assert(fd >= 0);
9a102d
+        assert(uid_is_valid(uid));
9a102d
+
9a102d
+        /* We don't allow users to read coredumps if the uid or capabilities were changed. */
9a102d
+        if (!allow_user)
9a102d
+                return 0;
9a102d
 
9a102d
         if (uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY)
9a102d
                 return 0;
9a102d
@@ -235,7 +242,8 @@ static int fix_permissions(
9a102d
                 const char *filename,
9a102d
                 const char *target,
9a102d
                 const Context *context,
9a102d
-                uid_t uid) {
9a102d
+                uid_t uid,
9a102d
+                bool allow_user) {
9a102d
 
9a102d
         int r;
9a102d
 
9a102d
@@ -245,7 +253,7 @@ static int fix_permissions(
9a102d
 
9a102d
         /* Ignore errors on these */
9a102d
         (void) fchmod(fd, 0640);
9a102d
-        (void) fix_acl(fd, uid);
9a102d
+        (void) fix_acl(fd, uid, allow_user);
9a102d
         (void) fix_xattr(fd, context);
9a102d
 
9a102d
         if (fsync(fd) < 0)
9a102d
@@ -316,6 +324,154 @@ static int make_filename(const Context *context, char **ret) {
9a102d
         return 0;
9a102d
 }
9a102d
 
9a102d
+static int parse_auxv64(
9a102d
+                const uint64_t *auxv,
9a102d
+                size_t size_bytes,
9a102d
+                int *at_secure,
9a102d
+                uid_t *uid,
9a102d
+                uid_t *euid,
9a102d
+                gid_t *gid,
9a102d
+                gid_t *egid) {
9a102d
+
9a102d
+        assert(auxv || size_bytes == 0);
9a102d
+
9a102d
+        if (size_bytes % (2 * sizeof(uint64_t)) != 0)
9a102d
+                return log_warning_errno(-EIO, "Incomplete auxv structure (%zu bytes).", size_bytes);
9a102d
+
9a102d
+        size_t words = size_bytes / sizeof(uint64_t);
9a102d
+
9a102d
+        /* Note that we set output variables even on error. */
9a102d
+
9a102d
+        for (size_t i = 0; i + 1 < words; i += 2)
9a102d
+                switch (auxv[i]) {
9a102d
+                case AT_SECURE:
9a102d
+                        *at_secure = auxv[i + 1] != 0;
9a102d
+                        break;
9a102d
+                case AT_UID:
9a102d
+                        *uid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_EUID:
9a102d
+                        *euid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_GID:
9a102d
+                        *gid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_EGID:
9a102d
+                        *egid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_NULL:
9a102d
+                        if (auxv[i + 1] != 0)
9a102d
+                                goto error;
9a102d
+                        return 0;
9a102d
+                }
9a102d
+ error:
9a102d
+        return log_warning_errno(-ENODATA,
9a102d
+                                 "AT_NULL terminator not found, cannot parse auxv structure.");
9a102d
+}
9a102d
+
9a102d
+static int parse_auxv32(
9a102d
+                const uint32_t *auxv,
9a102d
+                size_t size_bytes,
9a102d
+                int *at_secure,
9a102d
+                uid_t *uid,
9a102d
+                uid_t *euid,
9a102d
+                gid_t *gid,
9a102d
+                gid_t *egid) {
9a102d
+
9a102d
+        assert(auxv || size_bytes == 0);
9a102d
+
9a102d
+        size_t words = size_bytes / sizeof(uint32_t);
9a102d
+
9a102d
+        if (size_bytes % (2 * sizeof(uint32_t)) != 0)
9a102d
+                return log_warning_errno(-EIO, "Incomplete auxv structure (%zu bytes).", size_bytes);
9a102d
+
9a102d
+        /* Note that we set output variables even on error. */
9a102d
+
9a102d
+        for (size_t i = 0; i + 1 < words; i += 2)
9a102d
+                switch (auxv[i]) {
9a102d
+                case AT_SECURE:
9a102d
+                        *at_secure = auxv[i + 1] != 0;
9a102d
+                        break;
9a102d
+                case AT_UID:
9a102d
+                        *uid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_EUID:
9a102d
+                        *euid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_GID:
9a102d
+                        *gid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_EGID:
9a102d
+                        *egid = auxv[i + 1];
9a102d
+                        break;
9a102d
+                case AT_NULL:
9a102d
+                        if (auxv[i + 1] != 0)
9a102d
+                                goto error;
9a102d
+                        return 0;
9a102d
+                }
9a102d
+ error:
9a102d
+        return log_warning_errno(-ENODATA,
9a102d
+                                 "AT_NULL terminator not found, cannot parse auxv structure.");
9a102d
+}
9a102d
+
9a102d
+static int grant_user_access(int core_fd, const Context *context) {
9a102d
+        int at_secure = -1;
9a102d
+        uid_t uid = UID_INVALID, euid = UID_INVALID;
9a102d
+        uid_t gid = GID_INVALID, egid = GID_INVALID;
9a102d
+        int r;
9a102d
+
9a102d
+        assert(core_fd >= 0);
9a102d
+        assert(context);
9a102d
+
9a102d
+        if (!context->meta[CONTEXT_PROC_AUXV])
9a102d
+                return log_warning_errno(-ENODATA, "No auxv data, not adjusting permissions.");
9a102d
+
9a102d
+        uint8_t elf[EI_NIDENT];
9a102d
+        errno = 0;
9a102d
+        if (pread(core_fd, &elf, sizeof(elf), 0) != sizeof(elf))
9a102d
+                return log_warning_errno(errno > 0 ? -errno : -EIO,
9a102d
+                                         "Failed to pread from coredump fd: %s",
9a102d
+                                         errno > 0 ? STRERROR(errno) : "Unexpected EOF");
9a102d
+
9a102d
+        if (elf[EI_MAG0] != ELFMAG0 ||
9a102d
+            elf[EI_MAG1] != ELFMAG1 ||
9a102d
+            elf[EI_MAG2] != ELFMAG2 ||
9a102d
+            elf[EI_MAG3] != ELFMAG3 ||
9a102d
+            elf[EI_VERSION] != EV_CURRENT)
9a102d
+                return log_info_errno(-EUCLEAN,
9a102d
+                                      "Core file does not have ELF header, not adjusting permissions.");
9a102d
+        if (!IN_SET(elf[EI_CLASS], ELFCLASS32, ELFCLASS64) ||
9a102d
+            !IN_SET(elf[EI_DATA], ELFDATA2LSB, ELFDATA2MSB))
9a102d
+                return log_info_errno(-EUCLEAN,
9a102d
+                                      "Core file has strange ELF class, not adjusting permissions.");
9a102d
+
9a102d
+        if ((elf[EI_DATA] == ELFDATA2LSB) != (__BYTE_ORDER == __LITTLE_ENDIAN))
9a102d
+                return log_info_errno(-EUCLEAN,
9a102d
+                                      "Core file has non-native endianness, not adjusting permissions.");
9a102d
+
9a102d
+        if (elf[EI_CLASS] == ELFCLASS64)
9a102d
+                r = parse_auxv64((const uint64_t*) context->meta[CONTEXT_PROC_AUXV],
9a102d
+                                 context->meta_size[CONTEXT_PROC_AUXV],
9a102d
+                                 &at_secure, &uid, &euid, &gid, &egid);
9a102d
+        else
9a102d
+                r = parse_auxv32((const uint32_t*) context->meta[CONTEXT_PROC_AUXV],
9a102d
+                                 context->meta_size[CONTEXT_PROC_AUXV],
9a102d
+                                 &at_secure, &uid, &euid, &gid, &egid);
9a102d
+        if (r < 0)
9a102d
+                return r;
9a102d
+
9a102d
+        /* We allow access if we got all the data and at_secure is not set and
9a102d
+         * the uid/gid matches euid/egid. */
9a102d
+        bool ret =
9a102d
+                at_secure == 0 &&
9a102d
+                uid != UID_INVALID && euid != UID_INVALID && uid == euid &&
9a102d
+                gid != GID_INVALID && egid != GID_INVALID && gid == egid;
9a102d
+        log_debug("Will %s access (uid="UID_FMT " euid="UID_FMT " gid="GID_FMT " egid="GID_FMT " at_secure=%s)",
9a102d
+                  ret ? "permit" : "restrict",
9a102d
+                  uid, euid, gid, egid, yes_no(at_secure));
9a102d
+        return ret;
9a102d
+}
9a102d
+
9a102d
 static int save_external_coredump(
9a102d
                 const Context *context,
9a102d
                 int input_fd,
9a102d
@@ -395,6 +551,8 @@ static int save_external_coredump(
9a102d
                 goto fail;
9a102d
         }
9a102d
 
9a102d
+        bool allow_user = grant_user_access(fd, context) > 0;
9a102d
+
9a102d
 #if HAVE_XZ || HAVE_LZ4
9a102d
         /* If we will remove the coredump anyway, do not compress. */
9a102d
         if (arg_compress && !maybe_remove_external_coredump(NULL, st.st_size)) {
9a102d
@@ -420,7 +578,7 @@ static int save_external_coredump(
9a102d
                         goto fail_compressed;
9a102d
                 }
9a102d
 
9a102d
-                r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid);
9a102d
+                r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, context, uid, allow_user);
9a102d
                 if (r < 0)
9a102d
                         goto fail_compressed;
9a102d
 
9a102d
@@ -443,7 +601,7 @@ static int save_external_coredump(
9a102d
 uncompressed:
9a102d
 #endif
9a102d
 
9a102d
-        r = fix_permissions(fd, tmp, fn, context, uid);
9a102d
+        r = fix_permissions(fd, tmp, fn, context, uid, allow_user);
9a102d
         if (r < 0)
9a102d
                 goto fail;
9a102d
 
9a102d
@@ -842,6 +1000,7 @@ static void map_context_fields(const struct iovec *iovec, Context *context) {
9a102d
                 [CONTEXT_HOSTNAME] = "COREDUMP_HOSTNAME=",
9a102d
                 [CONTEXT_COMM] = "COREDUMP_COMM=",
9a102d
                 [CONTEXT_EXE] = "COREDUMP_EXE=",
9a102d
+                [CONTEXT_PROC_AUXV] = "COREDUMP_PROC_AUXV=",
9a102d
         };
9a102d
 
9a102d
         unsigned i;
9a102d
@@ -862,6 +1021,7 @@ static void map_context_fields(const struct iovec *iovec, Context *context) {
9a102d
                 /* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the
9a102d
                  * buffer, though not included in the iov_len count. (see below) */
9a102d
                 context->meta[i] = p;
9a102d
+                context->meta_size[i] = iovec->iov_len - strlen(context_field_names[i]);
9a102d
                 break;
9a102d
         }
9a102d
 }
9a102d
@@ -1070,7 +1230,7 @@ static int gather_pid_metadata(
9a102d
                 char **comm_fallback,
9a102d
                 struct iovec *iovec, size_t *n_iovec) {
9a102d
 
9a102d
-        /* We need 27 empty slots in iovec!
9a102d
+        /* We need 28 empty slots in iovec!
9a102d
          *
9a102d
          * Note that if we fail on oom later on, we do not roll-back changes to the iovec structure. (It remains valid,
9a102d
          * with the first n_iovec fields initialized.) */
9a102d
@@ -1078,6 +1238,7 @@ static int gather_pid_metadata(
9a102d
         uid_t owner_uid;
9a102d
         pid_t pid;
9a102d
         char *t;
9a102d
+        size_t size;
9a102d
         const char *p;
9a102d
         int r, signo;
9a102d
 
9a102d
@@ -1187,6 +1348,19 @@ static int gather_pid_metadata(
9a102d
         if (read_full_file(p, &t, NULL) >=0)
9a102d
                 set_iovec_field_free(iovec, n_iovec, "COREDUMP_PROC_MOUNTINFO=", t);
9a102d
 
9a102d
+        /* We attach /proc/auxv here. ELF coredumps also contain a note for this (NT_AUXV), see elf(5). */
9a102d
+        p = procfs_file_alloca(pid, "auxv");
9a102d
+        if (read_full_file(p, &t, &size) >= 0) {
9a102d
+                char *buf = malloc(strlen("COREDUMP_PROC_AUXV=") + size + 1);
9a102d
+                if (buf) {
9a102d
+                        /* Add a dummy terminator to make save_context() happy. */
9a102d
+                        *((uint8_t*) mempcpy(stpcpy(buf, "COREDUMP_PROC_AUXV="), t, size)) = '\0';
9a102d
+                        iovec[(*n_iovec)++] = IOVEC_MAKE(buf, size + strlen("COREDUMP_PROC_AUXV="));
9a102d
+                }
9a102d
+
9a102d
+                free(t);
9a102d
+        }
9a102d
+
9a102d
         if (get_process_cwd(pid, &t) >= 0)
9a102d
                 set_iovec_field_free(iovec, n_iovec, "COREDUMP_CWD=", t);
9a102d
 
9a102d
@@ -1219,7 +1393,7 @@ static int gather_pid_metadata(
9a102d
 static int process_kernel(int argc, char* argv[]) {
9a102d
 
9a102d
         Context context = {};
9a102d
-        struct iovec iovec[29 + SUBMIT_COREDUMP_FIELDS];
9a102d
+        struct iovec iovec[30 + SUBMIT_COREDUMP_FIELDS];
9a102d
         size_t i, n_iovec, n_to_free = 0;
9a102d
         int r;
9a102d