aboutgitcodebugslistschat
path: root/pasta.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2023-02-12 12:22:59 +0100
committerStefano Brivio <sbrivio@redhat.com>2023-02-12 12:22:59 +0100
commitd8921dafe506599fd7ff723b211f9113faff9ada (patch)
tree12477d15170f135b4004b635d4bf8e7ae4de938e /pasta.c
parent54502cca7f7a89eadce92bb7a2b6ab61b878a5c6 (diff)
downloadpasst-d8921dafe506599fd7ff723b211f9113faff9ada.tar
passt-d8921dafe506599fd7ff723b211f9113faff9ada.tar.gz
passt-d8921dafe506599fd7ff723b211f9113faff9ada.tar.bz2
passt-d8921dafe506599fd7ff723b211f9113faff9ada.tar.lz
passt-d8921dafe506599fd7ff723b211f9113faff9ada.tar.xz
passt-d8921dafe506599fd7ff723b211f9113faff9ada.tar.zst
passt-d8921dafe506599fd7ff723b211f9113faff9ada.zip
pasta: Wait for tap to be set up before spawning command
Adapted from a patch by Paul Holzinger: when pasta spawns a command, operating without a pre-existing user and network namespace, it needs to wait for the tap device to be configured and its handler ready, before the command is actually executed. Otherwise, something like: pasta --config-net nslookup passt.top usually fails as the nslookup command is issued before the network interface is ready. We can't adopt a simpler approach based on SIGSTOP and SIGCONT here: the child runs in a separate PID namespace, so it can't send SIGSTOP to itself as the kernel sees the child as init process and blocks the delivery of the signal. We could send SIGSTOP from the parent, but this wouldn't avoid the possible condition where the child isn't ready to wait for it when the parent sends it, also raised by Paul -- and SIGSTOP can't be blocked, so it can never be pending. Use SIGUSR1 instead: mask it before clone(), so that the child starts with it blocked, and can safely wait for it. Once the parent is ready, it sends SIGUSR1 to the child. If SIGUSR1 is sent before the child is waiting for it, the kernel will queue it for us, because it's blocked. Reported-by: Paul Holzinger <pholzing@redhat.com> Fixes: 1392bc5ca002 ("Allow pasta to take a command to execute") Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'pasta.c')
-rw-r--r--pasta.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/pasta.c b/pasta.c
index 528f02a..9169913 100644
--- a/pasta.c
+++ b/pasta.c
@@ -47,7 +47,7 @@
#include "log.h"
/* PID of child, in case we created a namespace */
-static int pasta_child_pid;
+int pasta_child_pid;
/**
* pasta_child_handler() - Exit once shell exits (if we started it), reap clones
@@ -166,10 +166,16 @@ struct pasta_spawn_cmd_arg {
static int pasta_spawn_cmd(void *arg)
{
const struct pasta_spawn_cmd_arg *a;
+ sigset_t set;
if (write_file("/proc/sys/net/ipv4/ping_group_range", "0 0"))
warn("Cannot set ping_group_range, ICMP requests might fail");
+ /* Wait for the parent to be ready: see main() */
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ sigwaitinfo(&set, NULL);
+
a = (const struct pasta_spawn_cmd_arg *)arg;
execvp(a->exe, a->argv);
@@ -196,6 +202,7 @@ void pasta_start_ns(struct ctx *c, uid_t uid, gid_t gid,
char ns_fn_stack[NS_FN_STACK_SIZE];
char *sh_argv[] = { NULL, NULL };
char sh_arg0[PATH_MAX + 1];
+ sigset_t set;
c->foreground = 1;
if (!c->debug)
@@ -226,6 +233,11 @@ void pasta_start_ns(struct ctx *c, uid_t uid, gid_t gid,
arg.argv = sh_argv;
}
+ /* Block SIGUSR1 in child, we queue it in main() when we're ready */
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ sigprocmask(SIG_BLOCK, &set, NULL);
+
pasta_child_pid = do_clone(pasta_spawn_cmd, ns_fn_stack,
sizeof(ns_fn_stack),
CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET |