ff6046
From 2115fcc1e673079fe76e949ac0904267075c25a4 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Wed, 31 Oct 2018 13:04:20 +0100
ff6046
Subject: [PATCH] nspawn: beef up netns checking a bit, for compat with old
ff6046
 kernels
ff6046
ff6046
Fixes: #10544
ff6046
ff6046
Cherry-picked from: 6619ad889da260cf83079cc74a85d571acd1df5a
ff6046
---
ff6046
 src/basic/stat-util.c     | 40 +++++++++++++++++++++++++++++++++++----
ff6046
 src/nspawn/nspawn.c       |  8 +++++---
ff6046
 src/test/test-stat-util.c | 15 +++++++++++++++
ff6046
 3 files changed, 56 insertions(+), 7 deletions(-)
ff6046
ff6046
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
ff6046
index 07154e25bb..26aee9bad6 100644
ff6046
--- a/src/basic/stat-util.c
ff6046
+++ b/src/basic/stat-util.c
ff6046
@@ -204,15 +204,47 @@ int fd_is_network_fs(int fd) {
ff6046
 }
ff6046
 
ff6046
 int fd_is_network_ns(int fd) {
ff6046
+        struct statfs s;
ff6046
         int r;
ff6046
 
ff6046
-        r = fd_is_fs_type(fd, NSFS_MAGIC);
ff6046
-        if (r <= 0)
ff6046
-                return r;
ff6046
+        /* Checks whether the specified file descriptor refers to a network namespace. On old kernels there's no nice
ff6046
+         * way to detect that, hence on those we'll return a recognizable error (EUCLEAN), so that callers can handle
ff6046
+         * this somewhat nicely.
ff6046
+         *
ff6046
+         * This function returns > 0 if the fd definitely refers to a network namespace, 0 if it definitely does not
ff6046
+         * refer to a network namespace, -EUCLEAN if we can't determine, and other negative error codes on error. */
ff6046
+
ff6046
+        if (fstatfs(fd, &s) < 0)
ff6046
+                return -errno;
ff6046
+
ff6046
+        if (!is_fs_type(&s, NSFS_MAGIC)) {
ff6046
+                /* On really old kernels, there was no "nsfs", and network namespace sockets belonged to procfs
ff6046
+                 * instead. Handle that in a somewhat smart way. */
ff6046
+
ff6046
+                if (is_fs_type(&s, PROC_SUPER_MAGIC)) {
ff6046
+                        struct statfs t;
ff6046
+
ff6046
+                        /* OK, so it is procfs. Let's see if our own network namespace is procfs, too. If so, then the
ff6046
+                         * passed fd might refer to a network namespace, but we can't know for sure. In that case,
ff6046
+                         * return a recognizable error. */
ff6046
+
ff6046
+                        if (statfs("/proc/self/ns/net", &t) < 0)
ff6046
+                                return -errno;
ff6046
+
ff6046
+                        if (s.f_type == t.f_type)
ff6046
+                                return -EUCLEAN; /* It's possible, we simply don't know */
ff6046
+                }
ff6046
+
ff6046
+                return 0; /* No! */
ff6046
+        }
ff6046
 
ff6046
         r = ioctl(fd, NS_GET_NSTYPE);
ff6046
-        if (r < 0)
ff6046
+        if (r < 0) {
ff6046
+                if (errno == ENOTTY) /* Old kernels didn't know this ioctl, let's also return a recognizable error in that case */
ff6046
+                        return -EUCLEAN;
ff6046
+
ff6046
                 return -errno;
ff6046
+        }
ff6046
 
ff6046
         return r == CLONE_NEWNET;
ff6046
 }
ff6046
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
ff6046
index 56877bd932..8aec893a69 100644
ff6046
--- a/src/nspawn/nspawn.c
ff6046
+++ b/src/nspawn/nspawn.c
ff6046
@@ -3701,10 +3701,12 @@ static int run(int master,
ff6046
                         return log_error_errno(errno, "Cannot open file %s: %m", arg_network_namespace_path);
ff6046
 
ff6046
                 r = fd_is_network_ns(netns_fd);
ff6046
-                if (r < 0 && r != -ENOTTY)
ff6046
+                if (r == -EUCLEAN)
ff6046
+                        log_debug_errno(r, "Cannot determine if passed network namespace path '%s' really refers to a network namespace, assuming it does.", arg_network_namespace_path);
ff6046
+                else if (r < 0)
ff6046
                         return log_error_errno(r, "Failed to check %s fs type: %m", arg_network_namespace_path);
ff6046
-                if (r == 0) {
ff6046
-                        log_error("Path %s doesn't refer to a network namespace", arg_network_namespace_path);
ff6046
+                else if (r == 0) {
ff6046
+                        log_error("Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path);
ff6046
                         return -EINVAL;
ff6046
                 }
ff6046
         }
ff6046
diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c
ff6046
index 43f56a6c20..2b0564d8a0 100644
ff6046
--- a/src/test/test-stat-util.c
ff6046
+++ b/src/test/test-stat-util.c
ff6046
@@ -67,11 +67,26 @@ static void test_path_is_temporary_fs(void) {
ff6046
         assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT);
ff6046
 }
ff6046
 
ff6046
+static void test_fd_is_network_ns(void) {
ff6046
+        _cleanup_close_ int fd = -1;
ff6046
+        assert_se(fd_is_network_ns(STDIN_FILENO) == 0);
ff6046
+        assert_se(fd_is_network_ns(STDERR_FILENO) == 0);
ff6046
+        assert_se(fd_is_network_ns(STDOUT_FILENO) == 0);
ff6046
+
ff6046
+        assert_se((fd = open("/proc/self/ns/mnt", O_CLOEXEC|O_RDONLY)) >= 0);
ff6046
+        assert_se(IN_SET(fd_is_network_ns(fd), 0, -EUCLEAN));
ff6046
+        fd = safe_close(fd);
ff6046
+
ff6046
+        assert_se((fd = open("/proc/self/ns/net", O_CLOEXEC|O_RDONLY)) >= 0);
ff6046
+        assert_se(IN_SET(fd_is_network_ns(fd), 1, -EUCLEAN));
ff6046
+}
ff6046
+
ff6046
 int main(int argc, char *argv[]) {
ff6046
         test_files_same();
ff6046
         test_is_symlink();
ff6046
         test_path_is_fs_type();
ff6046
         test_path_is_temporary_fs();
ff6046
+        test_fd_is_network_ns();
ff6046
 
ff6046
         return 0;
ff6046
 }