ecbff1
From 6d9aff83ef5d50a65fad4f4218073bd4aa3e6902 Mon Sep 17 00:00:00 2001
ecbff1
From: Lennart Poettering <lennart@poettering.net>
ecbff1
Date: Tue, 10 Nov 2015 20:08:04 +0100
ecbff1
Subject: [PATCH] journald: never accept fds from file systems with mandatory
ecbff1
 locking enabled
ecbff1
ecbff1
This is pretty much a work-around for a security vulnerability in
ecbff1
kernels that allow unprivileged user namespaces.
ecbff1
ecbff1
Fixes #1822.
ecbff1
ecbff1
Cherry-picked from: 1e603a482f57edb1fb863dbf23b868cf5854e004
ecbff1
Resolves: #1501017
ecbff1
---
ecbff1
 src/journal/journald-native.c | 30 ++++++++++++++++++++++++++++++
ecbff1
 1 file changed, 30 insertions(+)
ecbff1
ecbff1
diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c
ecbff1
index 2c9cf6e7a..fdb1a38dd 100644
ecbff1
--- a/src/journal/journald-native.c
ecbff1
+++ b/src/journal/journald-native.c
ecbff1
@@ -23,6 +23,7 @@
ecbff1
 #include <stddef.h>
ecbff1
 #include <sys/epoll.h>
ecbff1
 #include <sys/mman.h>
ecbff1
+#include <sys/statvfs.h>
ecbff1
 
ecbff1
 #include "socket-util.h"
ecbff1
 #include "path-util.h"
ecbff1
@@ -391,8 +392,37 @@ void server_process_native_file(
ecbff1
                 assert_se(munmap(p, ps) >= 0);
ecbff1
         } else {
ecbff1
                 _cleanup_free_ void *p = NULL;
ecbff1
+                struct statvfs vfs;
ecbff1
                 ssize_t n;
ecbff1
 
ecbff1
+                if (fstatvfs(fd, &vfs) < 0) {
ecbff1
+                        log_error_errno(errno, "Failed to stat file system of passed file, ignoring: %m");
ecbff1
+                        return;
ecbff1
+                }
ecbff1
+
ecbff1
+                /* Refuse operating on file systems that have
ecbff1
+                 * mandatory locking enabled, see:
ecbff1
+                 *
ecbff1
+                 * https://github.com/systemd/systemd/issues/1822
ecbff1
+                 */
ecbff1
+                if (vfs.f_flag & ST_MANDLOCK) {
ecbff1
+                        log_error("Received file descriptor from file system with mandatory locking enable, refusing.");
ecbff1
+                        return;
ecbff1
+                }
ecbff1
+
ecbff1
+                /* Make the fd non-blocking. On regular files this has
ecbff1
+                 * the effect of bypassing mandatory locking. Of
ecbff1
+                 * course, this should normally not be necessary given
ecbff1
+                 * the check above, but let's better be safe than
ecbff1
+                 * sorry, after all NFS is pretty confusing regarding
ecbff1
+                 * file system flags, and we better don't trust it,
ecbff1
+                 * and so is SMB. */
ecbff1
+                r = fd_nonblock(fd, true);
ecbff1
+                if (r < 0) {
ecbff1
+                        log_error_errno(r, "Failed to make fd non-blocking, ignoring: %m");
ecbff1
+                        return;
ecbff1
+                }
ecbff1
+
ecbff1
                 /* The file is not sealed, we can't map the file here, since
ecbff1
                  * clients might then truncate it and trigger a SIGBUS for
ecbff1
                  * us. So let's stupidly read it */