aboutgitcodebugslistschat
path: root/pasta.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2022-02-07 21:11:37 +0100
committerStefano Brivio <sbrivio@redhat.com>2022-02-21 13:41:13 +0100
commit0515adceaa8f69a1d85ae3c8c550c37dd49b0c47 (patch)
tree6ca9be64f2fa252da4ffdbe4c591778eb06b7b4a /pasta.c
parentfcc3db78cd5fdf1b02e2339b722512b97998f28f (diff)
downloadpasst-0515adceaa8f69a1d85ae3c8c550c37dd49b0c47.tar
passt-0515adceaa8f69a1d85ae3c8c550c37dd49b0c47.tar.gz
passt-0515adceaa8f69a1d85ae3c8c550c37dd49b0c47.tar.bz2
passt-0515adceaa8f69a1d85ae3c8c550c37dd49b0c47.tar.lz
passt-0515adceaa8f69a1d85ae3c8c550c37dd49b0c47.tar.xz
passt-0515adceaa8f69a1d85ae3c8c550c37dd49b0c47.tar.zst
passt-0515adceaa8f69a1d85ae3c8c550c37dd49b0c47.zip
passt, pasta: Namespace-based sandboxing, defer seccomp policy application
To reach (at least) a conceptually equivalent security level as implemented by --enable-sandbox in slirp4netns, we need to create a new mount namespace and pivot_root() into a new (empty) mountpoint, so that passt and pasta can't access any filesystem resource after initialisation. While at it, also detach IPC, PID (only for passt, to prevent vulnerabilities based on the knowledge of a target PID), and UTS namespaces. With this approach, if we apply the seccomp filters right after the configuration step, the number of allowed syscalls grows further. To prevent this, defer the application of seccomp policies after the initialisation phase, before the main loop, that's where we expect bad things to happen, potentially. This way, we get back to 22 allowed syscalls for passt and 34 for pasta, on x86_64. While at it, move #syscalls notes to specific code paths wherever it conceptually makes sense. We have to open all the file handles we'll ever need before sandboxing: - the packet capture file can only be opened once, drop instance numbers from the default path and use the (pre-sandbox) PID instead - /proc/net/tcp{,v6} and /proc/net/udp{,v6}, for automatic detection of bound ports in pasta mode, are now opened only once, before sandboxing, and their handles are stored in the execution context - the UNIX domain socket for passt is also bound only once, before sandboxing: to reject clients after the first one, instead of closing the listening socket, keep it open, accept and immediately discard new connection if we already have a valid one Clarify the (unchanged) behaviour for --netns-only in the man page. To actually make passt and pasta processes run in a separate PID namespace, we need to unshare(CLONE_NEWPID) before forking to background (if configured to do so). Introduce a small daemon() implementation, __daemon(), that additionally saves the PID file before forking. While running in foreground, the process itself can't move to a new PID namespace (a process can't change the notion of its own PID): mention that in the man page. For some reason, fork() in a detached PID namespace causes SIGTERM and SIGQUIT to be ignored, even if the handler is still reported as SIG_DFL: add a signal handler that just exits. We can now drop most of the pasta_child_handler() implementation, that took care of terminating all processes running in the same namespace, if pasta started a shell: the shell itself is now the init process in that namespace, and all children will terminate once the init process exits. Issuing 'echo $$' in a detached PID namespace won't return the actual namespace PID as seen from the init namespace: adapt demo and test setup scripts to reflect that. Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'pasta.c')
-rw-r--r--pasta.c165
1 files changed, 59 insertions, 106 deletions
diff --git a/pasta.c b/pasta.c
index bce30d4..972cbcf 100644
--- a/pasta.c
+++ b/pasta.c
@@ -11,9 +11,8 @@
* Copyright (c) 2020-2021 Red Hat GmbH
* Author: Stefano Brivio <sbrivio@redhat.com>
*
- * #syscalls:pasta clone unshare waitid kill execve exit_group rt_sigprocmask
- * #syscalls:pasta geteuid getdents64|getdents readlink|readlinkat setsid
- * #syscalls:pasta nanosleep clock_nanosleep
+ * #syscalls:pasta clone waitid exit exit_group rt_sigprocmask
+ * #syscalls:pasta rt_sigreturn|sigreturn ppc64:sigreturn s390x:sigreturn
*/
#include <sched.h>
@@ -40,75 +39,8 @@
#include "passt.h"
#include "netlink.h"
-/* PID of child, in case we created a namespace, and its procfs link */
+/* PID of child, in case we created a namespace */
static int pasta_child_pid;
-static char pasta_child_ns[PATH_MAX];
-
-/**
- * pasta_ns_cleanup() - Look for processes in namespace, terminate them
- */
-static void pasta_ns_cleanup(void)
-{
- char proc_path[PATH_MAX], ns_link[PATH_MAX], buf[BUFSIZ];
- int recheck = 0, found = 0, waited = 0;
- int dir_fd, n;
-
- if (!*pasta_child_ns)
- return;
-
-loop:
- if ((dir_fd = open("/proc", O_RDONLY | O_DIRECTORY)) < 0)
- return;
-
- while ((n = syscall(SYS_getdents64, dir_fd, buf, BUFSIZ)) > 0) {
- struct dirent *dp = (struct dirent *)buf;
- int pos = 0;
-
- while (dp->d_reclen && pos < n) {
- pid_t pid;
-
- errno = 0;
- pid = strtol(dp->d_name, NULL, 0);
- if (!pid || errno)
- goto next;
-
- snprintf(proc_path, PATH_MAX, "/proc/%i/ns/net", pid);
- if (readlink(proc_path, ns_link, PATH_MAX) < 0)
- goto next;
-
- if (!strncmp(ns_link, pasta_child_ns, PATH_MAX)) {
- found = 1;
- if (waited)
- kill(pid, SIGKILL);
- else
- kill(pid, SIGQUIT);
- }
-next:
- dp = (struct dirent *)(buf + (pos += dp->d_reclen));
- }
- }
-
- close(dir_fd);
-
- if (!found)
- return;
-
- if (waited) {
- if (recheck) {
- info("Some processes in namespace didn't quit");
- } else {
- found = 0;
- recheck = 1;
- goto loop;
- }
- return;
- }
-
- info("Waiting for all processes in namespace to terminate");
- sleep(1);
- waited = 1;
- goto loop;
-}
/**
* pasta_child_handler() - Exit once shell exits (if we started it), reap clones
@@ -120,12 +52,14 @@ void pasta_child_handler(int signal)
(void)signal;
+ if (signal != SIGCHLD)
+ return;
+
if (pasta_child_pid &&
!waitid(P_PID, pasta_child_pid, &infop, WEXITED | WNOHANG)) {
- if (infop.si_pid == pasta_child_pid) {
- pasta_ns_cleanup();
+ if (infop.si_pid == pasta_child_pid)
exit(EXIT_SUCCESS);
- }
+ /* Nothing to do, detached PID namespace going away */
}
waitid(P_ALL, 0, NULL, WEXITED | WNOHANG);
@@ -163,45 +97,31 @@ netns:
}
/**
- * pasta_start_ns() - Fork shell in new namespace if target ns is not given
+ * struct pasta_setup_ns_arg - Argument for pasta_setup_ns()
* @c: Execution context
+ * @euid: Effective UID of caller
*/
-void pasta_start_ns(struct ctx *c)
+struct pasta_setup_ns_arg {
+ struct ctx *c;
+ int euid;
+};
+
+/**
+ * pasta_setup_ns() - Map credentials, enable access to ping sockets, run shell
+ * @arg: See @pasta_setup_ns_arg
+ *
+ * Return: this function never returns
+ */
+static int pasta_setup_ns(void *arg)
{
- int euid = geteuid(), fd;
+ struct pasta_setup_ns_arg *a = (struct pasta_setup_ns_arg *)arg;
char *shell;
+ int fd;
- c->foreground = 1;
- if (!c->debug)
- c->quiet = 1;
-
- if ((pasta_child_pid = fork()) == -1) {
- perror("fork");
- exit(EXIT_FAILURE);
- }
-
- if (pasta_child_pid) {
- char proc_path[PATH_MAX];
-
- NS_CALL(pasta_wait_for_ns, c);
-
- snprintf(proc_path, PATH_MAX, "/proc/%i/ns/net",
- pasta_child_pid);
- if (readlink(proc_path, pasta_child_ns, PATH_MAX) < 0)
- warn("Cannot read link to ns, won't clean up on exit");
-
- return;
- }
-
- if (unshare(CLONE_NEWNET | (c->netns_only ? 0 : CLONE_NEWUSER))) {
- perror("unshare");
- exit(EXIT_FAILURE);
- }
-
- if (!c->netns_only) {
+ if (!a->c->netns_only) {
char buf[BUFSIZ];
- snprintf(buf, BUFSIZ, "%i %i %i", 0, euid, 1);
+ snprintf(buf, BUFSIZ, "%i %i %i", 0, a->euid, 1);
fd = open("/proc/self/uid_map", O_WRONLY);
if (write(fd, buf, strlen(buf)) < 0)
@@ -235,6 +155,39 @@ void pasta_start_ns(struct ctx *c)
}
/**
+ * pasta_start_ns() - Fork shell in new namespace if target ns is not given
+ * @c: Execution context
+ */
+void pasta_start_ns(struct ctx *c)
+{
+ struct pasta_setup_ns_arg arg = { .c = c, .euid = geteuid() };
+ char ns_fn_stack[NS_FN_STACK_SIZE];
+
+ c->foreground = 1;
+ if (!c->debug)
+ c->quiet = 1;
+
+ pasta_child_pid = clone(pasta_setup_ns,
+ ns_fn_stack + sizeof(ns_fn_stack) / 2,
+ (c->netns_only ? 0 : CLONE_NEWNET) |
+ CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWUSER |
+ CLONE_NEWUTS,
+ (void *)&arg);
+
+ if (pasta_child_pid == -1) {
+ perror("clone");
+ exit(EXIT_FAILURE);
+ }
+
+ drop_caps();
+
+ if (pasta_child_pid) {
+ NS_CALL(pasta_wait_for_ns, c);
+ return;
+ }
+}
+
+/**
* pasta_ns_conf() - Set up loopback and tap interfaces in namespace as needed
* @c: Execution context
*/