aboutgitcodebugslistschat
path: root/tap.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 /tap.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 'tap.c')
-rw-r--r--tap.c58
1 files changed, 27 insertions, 31 deletions
diff --git a/tap.c b/tap.c
index 22db9c5..38004a5 100644
--- a/tap.c
+++ b/tap.c
@@ -11,7 +11,6 @@
* Copyright (c) 2020-2021 Red Hat GmbH
* Author: Stefano Brivio <sbrivio@redhat.com>
*
- * #syscalls recvfrom sendto
*/
#include <sched.h>
@@ -769,12 +768,10 @@ restart:
}
/**
- * tap_sock_init_unix() - Create and bind AF_UNIX socket, listen for connection
+ * tap_sock_unix_init() - Create and bind AF_UNIX socket, listen for connection
* @c: Execution context
- *
- * #syscalls:passt unlink|unlinkat
*/
-static void tap_sock_init_unix(struct ctx *c)
+static void tap_sock_unix_init(struct ctx *c)
{
int fd = socket(AF_UNIX, SOCK_STREAM, 0), ex;
struct epoll_event ev = { 0 };
@@ -783,11 +780,6 @@ static void tap_sock_init_unix(struct ctx *c)
};
int i, ret;
- if (c->fd_tap_listen != -1) {
- epoll_ctl(c->epollfd, EPOLL_CTL_DEL, c->fd_tap_listen, &ev);
- close(c->fd_tap_listen);
- }
-
if (fd < 0) {
perror("UNIX socket");
exit(EXIT_FAILURE);
@@ -834,8 +826,6 @@ static void tap_sock_init_unix(struct ctx *c)
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
#endif
- pcap_init(c, i);
-
listen(fd, 0);
ev.data.fd = c->fd_tap_listen = fd;
@@ -852,19 +842,26 @@ static void tap_sock_init_unix(struct ctx *c)
}
/**
- * tap_sock_accept_unix() - Accept connection on listening socket
+ * tap_sock_unix_new() - Handle new connection on listening socket
* @c: Execution context
*/
-static void tap_sock_accept_unix(struct ctx *c)
+static void tap_sock_unix_new(struct ctx *c)
{
struct epoll_event ev = { 0 };
int v = INT_MAX / 2;
- c->fd_tap = accept(c->fd_tap_listen, NULL, NULL);
+ /* Another client is already connected: accept and close right away. */
+ if (c->fd_tap != -1) {
+ int discard = accept4(c->fd_tap_listen, NULL, NULL,
+ SOCK_NONBLOCK);
+
+ if (discard != -1)
+ close(discard);
- epoll_ctl(c->epollfd, EPOLL_CTL_DEL, c->fd_tap_listen, &ev);
- close(c->fd_tap_listen);
- c->fd_tap_listen = -1;
+ return;
+ }
+
+ c->fd_tap = accept4(c->fd_tap_listen, NULL, NULL, 0);
if (!c->low_rmem)
setsockopt(c->fd_tap, SOL_SOCKET, SO_RCVBUF, &v, sizeof(v));
@@ -884,8 +881,6 @@ static int tun_ns_fd = -1;
* @c: Execution context
*
* Return: 0
- *
- * #syscalls:pasta ioctl
*/
static int tap_ns_tun(void *arg)
{
@@ -907,7 +902,7 @@ static int tap_ns_tun(void *arg)
* tap_sock_init_tun() - Set up tuntap file descriptor
* @c: Execution context
*/
-static void tap_sock_init_tun(struct ctx *c)
+static void tap_sock_tun_init(struct ctx *c)
{
struct epoll_event ev = { 0 };
@@ -919,8 +914,6 @@ static void tap_sock_init_tun(struct ctx *c)
pasta_ns_conf(c);
- pcap_init(c, c->pasta_netns_fd);
-
c->fd_tap = tun_ns_fd;
ev.data.fd = c->fd_tap;
@@ -937,12 +930,15 @@ void tap_sock_init(struct ctx *c)
if (c->fd_tap != -1) {
epoll_ctl(c->epollfd, EPOLL_CTL_DEL, c->fd_tap, NULL);
close(c->fd_tap);
+ c->fd_tap = -1;
}
- if (c->mode == MODE_PASST)
- tap_sock_init_unix(c);
- else
- tap_sock_init_tun(c);
+ if (c->mode == MODE_PASST) {
+ if (c->fd_tap_listen == -1)
+ tap_sock_unix_init(c);
+ } else {
+ tap_sock_tun_init(c);
+ }
}
/**
@@ -955,18 +951,18 @@ void tap_sock_init(struct ctx *c)
void tap_handler(struct ctx *c, int fd, uint32_t events, struct timespec *now)
{
if (fd == c->fd_tap_listen && events == EPOLLIN) {
- tap_sock_accept_unix(c);
+ tap_sock_unix_new(c);
return;
}
if (events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))
- goto fail;
+ goto reinit;
if ((c->mode == MODE_PASST && tap_handler_passt(c, now)) ||
(c->mode == MODE_PASTA && tap_handler_pasta(c, now)))
- goto fail;
+ goto reinit;
return;
-fail:
+reinit:
tap_sock_init(c);
}