b58745
From bf6405284aa3870a39b402309003633a1c230ed9 Mon Sep 17 00:00:00 2001
b58745
From: Aleksa Sarai <asarai@suse.de>
b58745
Date: Wed, 9 Jan 2019 13:40:01 +1100
b58745
Subject: [PATCH 1/1] nsenter: clone /proc/self/exe to avoid exposing host
b58745
 binary to container
b58745
b58745
There are quite a few circumstances where /proc/self/exe pointing to a
b58745
pretty important container binary is a _bad_ thing, so to avoid this we
b58745
have to make a copy (preferably doing self-clean-up and not being
b58745
writeable).
b58745
b58745
As a hotfix we require memfd_create(2), but we can always extend this to
b58745
use a scratch MNT_DETACH overlayfs or tmpfs. The main downside to this
b58745
approach is no page-cache sharing for the runc binary (which overlayfs
b58745
would give us) but this is far less complicated.
b58745
b58745
This is only done during nsenter so that it happens transparently to the
b58745
Go code, and any libcontainer users benefit from it. This also makes
b58745
ExtraFiles and --preserve-fds handling trivial (because we don't need to
b58745
worry about it).
b58745
b58745
Fixes: CVE-2019-5736
b58745
Co-developed-by: Christian Brauner <christian.brauner@ubuntu.com>
b58745
Signed-off-by: Aleksa Sarai <asarai@suse.de>
b58745
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
b58745
---
b58745
 libcontainer/nsenter/cloned_binary.c | 221 +++++++++++++++++++++++++++
b58745
 libcontainer/nsenter/nsexec.c        |  11 ++
b58745
 2 files changed, 232 insertions(+)
b58745
 create mode 100644 libcontainer/nsenter/cloned_binary.c
b58745
b58745
diff --git a/libcontainer/nsenter/cloned_binary.c b/libcontainer/nsenter/cloned_binary.c
b58745
new file mode 100644
b58745
index 00000000..d9f6093a
b58745
--- /dev/null
b58745
+++ b/libcontainer/nsenter/cloned_binary.c
b58745
@@ -0,0 +1,221 @@
b58745
+#define _GNU_SOURCE
b58745
+#include <unistd.h>
b58745
+#include <stdio.h>
b58745
+#include <stdlib.h>
b58745
+#include <stdbool.h>
b58745
+#include <string.h>
b58745
+#include <limits.h>
b58745
+#include <fcntl.h>
b58745
+#include <errno.h>
b58745
+
b58745
+#include <sys/types.h>
b58745
+#include <sys/stat.h>
b58745
+#include <sys/vfs.h>
b58745
+#include <sys/mman.h>
b58745
+#include <sys/sendfile.h>
b58745
+#include <sys/syscall.h>
b58745
+
b58745
+#include <linux/magic.h>
b58745
+#include <linux/memfd.h>
b58745
+
b58745
+/* Use our own wrapper for memfd_create. */
b58745
+#if !defined(SYS_memfd_create) && defined(__NR_memfd_create)
b58745
+#  define SYS_memfd_create __NR_memfd_create
b58745
+#endif
b58745
+#ifndef SYS_memfd_create
b58745
+#  error "memfd_create(2) syscall not supported by this glibc version"
b58745
+#endif
b58745
+int memfd_create(const char *name, unsigned int flags)
b58745
+{
b58745
+	return syscall(SYS_memfd_create, name, flags);
b58745
+}
b58745
+
b58745
+/* This comes directly from <linux/fcntl.h>. */
b58745
+#ifndef F_LINUX_SPECIFIC_BASE
b58745
+#  define F_LINUX_SPECIFIC_BASE 1024
b58745
+#endif
b58745
+#ifndef F_ADD_SEALS
b58745
+#  define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
b58745
+#  define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
b58745
+#endif
b58745
+#ifndef F_SEAL_SEAL
b58745
+#  define F_SEAL_SEAL   0x0001	/* prevent further seals from being set */
b58745
+#  define F_SEAL_SHRINK 0x0002	/* prevent file from shrinking */
b58745
+#  define F_SEAL_GROW   0x0004	/* prevent file from growing */
b58745
+#  define F_SEAL_WRITE  0x0008	/* prevent writes */
b58745
+#endif
b58745
+
b58745
+
b58745
+#define OUR_MEMFD_COMMENT "runc_cloned:/proc/self/exe"
b58745
+#define OUR_MEMFD_SEALS \
b58745
+	(F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)
b58745
+
b58745
+static void *must_realloc(void *ptr, size_t size)
b58745
+{
b58745
+	void *old = ptr;
b58745
+	do {
b58745
+		ptr = realloc(old, size);
b58745
+	} while(!ptr);
b58745
+	return ptr;
b58745
+}
b58745
+
b58745
+/*
b58745
+ * Verify whether we are currently in a self-cloned program (namely, is
b58745
+ * /proc/self/exe a memfd). F_GET_SEALS will only succeed for memfds (or rather
b58745
+ * for shmem files), and we want to be sure it's actually sealed.
b58745
+ */
b58745
+static int is_self_cloned(void)
b58745
+{
b58745
+	int fd, seals;
b58745
+
b58745
+	fd = open("/proc/self/exe", O_RDONLY|O_CLOEXEC);
b58745
+	if (fd < 0)
b58745
+		return -ENOTRECOVERABLE;
b58745
+
b58745
+	seals = fcntl(fd, F_GET_SEALS);
b58745
+	close(fd);
b58745
+	return seals == OUR_MEMFD_SEALS;
b58745
+}
b58745
+
b58745
+/*
b58745
+ * Basic wrapper around mmap(2) that gives you the file length so you can
b58745
+ * safely treat it as an ordinary buffer. Only gives you read access.
b58745
+ */
b58745
+static char *read_file(char *path, size_t *length)
b58745
+{
b58745
+	int fd;
b58745
+	char buf[4096], *copy = NULL;
b58745
+
b58745
+	if (!length)
b58745
+		return NULL;
b58745
+
b58745
+	fd = open(path, O_RDONLY | O_CLOEXEC);
b58745
+	if (fd < 0)
b58745
+		return NULL;
b58745
+
b58745
+	*length = 0;
b58745
+	for (;;) {
b58745
+		int n;
b58745
+
b58745
+		n = read(fd, buf, sizeof(buf));
b58745
+		if (n < 0)
b58745
+			goto error;
b58745
+		if (!n)
b58745
+			break;
b58745
+
b58745
+		copy = must_realloc(copy, (*length + n) * sizeof(*copy));
b58745
+		memcpy(copy + *length, buf, n);
b58745
+		*length += n;
b58745
+	}
b58745
+	close(fd);
b58745
+	return copy;
b58745
+
b58745
+error:
b58745
+	close(fd);
b58745
+	free(copy);
b58745
+	return NULL;
b58745
+}
b58745
+
b58745
+/*
b58745
+ * A poor-man's version of "xargs -0". Basically parses a given block of
b58745
+ * NUL-delimited data, within the given length and adds a pointer to each entry
b58745
+ * to the array of pointers.
b58745
+ */
b58745
+static int parse_xargs(char *data, int data_length, char ***output)
b58745
+{
b58745
+	int num = 0;
b58745
+	char *cur = data;
b58745
+
b58745
+	if (!data || *output != NULL)
b58745
+		return -1;
b58745
+
b58745
+	while (cur < data + data_length) {
b58745
+		num++;
b58745
+		*output = must_realloc(*output, (num + 1) * sizeof(**output));
b58745
+		(*output)[num - 1] = cur;
b58745
+		cur += strlen(cur) + 1;
b58745
+	}
b58745
+	(*output)[num] = NULL;
b58745
+	return num;
b58745
+}
b58745
+
b58745
+/*
b58745
+ * "Parse" out argv and envp from /proc/self/cmdline and /proc/self/environ.
b58745
+ * This is necessary because we are running in a context where we don't have a
b58745
+ * main() that we can just get the arguments from.
b58745
+ */
b58745
+static int fetchve(char ***argv, char ***envp)
b58745
+{
b58745
+	char *cmdline = NULL, *environ = NULL;
b58745
+	size_t cmdline_size, environ_size;
b58745
+
b58745
+	cmdline = read_file("/proc/self/cmdline", &cmdline_size);
b58745
+	if (!cmdline)
b58745
+		goto error;
b58745
+	environ = read_file("/proc/self/environ", &environ_size);
b58745
+	if (!environ)
b58745
+		goto error;
b58745
+
b58745
+	if (parse_xargs(cmdline, cmdline_size, argv) <= 0)
b58745
+		goto error;
b58745
+	if (parse_xargs(environ, environ_size, envp) <= 0)
b58745
+		goto error;
b58745
+
b58745
+	return 0;
b58745
+
b58745
+error:
b58745
+	free(environ);
b58745
+	free(cmdline);
b58745
+	return -EINVAL;
b58745
+}
b58745
+
b58745
+#define SENDFILE_MAX 0x7FFFF000 /* sendfile(2) is limited to 2GB. */
b58745
+static int clone_binary(void)
b58745
+{
b58745
+	int binfd, memfd, err;
b58745
+	ssize_t sent = 0;
b58745
+
b58745
+	memfd = memfd_create(OUR_MEMFD_COMMENT, MFD_CLOEXEC | MFD_ALLOW_SEALING);
b58745
+	if (memfd < 0)
b58745
+		return -ENOTRECOVERABLE;
b58745
+
b58745
+	binfd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC);
b58745
+	if (binfd < 0)
b58745
+		goto error;
b58745
+
b58745
+	sent = sendfile(memfd, binfd, NULL, SENDFILE_MAX);
b58745
+	close(binfd);
b58745
+	if (sent < 0)
b58745
+		goto error;
b58745
+
b58745
+	err = fcntl(memfd, F_ADD_SEALS, OUR_MEMFD_SEALS);
b58745
+	if (err < 0)
b58745
+		goto error;
b58745
+
b58745
+	return memfd;
b58745
+
b58745
+error:
b58745
+	close(memfd);
b58745
+	return -EIO;
b58745
+}
b58745
+
b58745
+int ensure_cloned_binary(void)
b58745
+{
b58745
+	int execfd;
b58745
+	char **argv = NULL, **envp = NULL;
b58745
+
b58745
+	/* Check that we're not self-cloned, and if we are then bail. */
b58745
+	int cloned = is_self_cloned();
b58745
+	if (cloned > 0 || cloned == -ENOTRECOVERABLE)
b58745
+		return cloned;
b58745
+
b58745
+	if (fetchve(&argv, &envp) < 0)
b58745
+		return -EINVAL;
b58745
+
b58745
+	execfd = clone_binary();
b58745
+	if (execfd < 0)
b58745
+		return -EIO;
b58745
+
b58745
+	fexecve(execfd, argv, envp);
b58745
+	return -ENOEXEC;
b58745
+}
b58745
diff --git a/libcontainer/nsenter/nsexec.c b/libcontainer/nsenter/nsexec.c
b58745
index cb224314..784fd9b0 100644
b58745
--- a/libcontainer/nsenter/nsexec.c
b58745
+++ b/libcontainer/nsenter/nsexec.c
b58745
@@ -528,6 +528,9 @@ void join_namespaces(char *nslist)
b58745
 	free(namespaces);
b58745
 }
b58745
 
b58745
+/* Defined in cloned_binary.c. */
b58745
+int ensure_cloned_binary(void);
b58745
+
b58745
 void nsexec(void)
b58745
 {
b58745
 	int pipenum;
b58745
@@ -543,6 +546,14 @@ void nsexec(void)
b58745
 	if (pipenum == -1)
b58745
 		return;
b58745
 
b58745
+	/*
b58745
+	 * We need to re-exec if we are not in a cloned binary. This is necessary
b58745
+	 * to ensure that containers won't be able to access the host binary
b58745
+	 * through /proc/self/exe. See CVE-2019-5736.
b58745
+	 */
b58745
+	if (ensure_cloned_binary() < 0)
b58745
+		bail("could not ensure we are a cloned binary");
b58745
+
b58745
 	/* Parse all of the netlink configuration. */
b58745
 	nl_parse(pipenum, &config);
b58745
 
b58745
-- 
b58745
2.20.1
b58745