diff options
Diffstat (limited to 'isolation.c')
| -rw-r--r-- | isolation.c | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/isolation.c b/isolation.c index c868e66..ea85fdb 100644 --- a/isolation.c +++ b/isolation.c @@ -24,13 +24,18 @@ * done anything we need to do with those resources, so we have * multiple stages of self-isolation. In order these are: * - * 1. isolate_initial() + * 1a. isolate_initial() * ==================== * * Executed immediately after startup, drops capabilities we don't * need at any point during execution (or which we gain back when we - * need by joining other namespaces), and closes any leaked file we - * might have inherited from the parent process. + * need by joining other namespaces). + * + * 1b. isolate_fds() + * ================ + * + * Executed immediately after isolate_initial(). Closes any leaked + * files we might have inherited from the parent process. * * 2. isolate_user() * ================= @@ -58,6 +63,7 @@ #include <errno.h> #include <fcntl.h> +#include <getopt.h> #include <grp.h> #include <inttypes.h> #include <limits.h> @@ -88,6 +94,7 @@ #include "passt.h" #include "log.h" #include "isolation.h" +#include "conf.h" #define CAP_VERSION _LINUX_CAPABILITY_VERSION_3 #define CAP_WORDS _LINUX_CAPABILITY_U32S_3 @@ -193,16 +200,13 @@ static int move_root(void) /** * isolate_initial() - Early, mostly config independent self isolation - * @argc: Argument count - * @argv: Command line options: only --fd (if present) is relevant here * * Should: * - drop unneeded capabilities - * - close all open files except for standard streams and the one from --fd * Mustn't: * - remove filesystem access (we need to access files during setup) */ -void isolate_initial(int argc, char **argv) +void isolate_initial(void) { uint64_t keep; @@ -243,8 +247,55 @@ void isolate_initial(int argc, char **argv) keep |= BIT(CAP_SETFCAP) | BIT(CAP_SYS_PTRACE); drop_caps_ep_except(keep); +} + + +/* + * isolate_fds() - Close leaked files, but not --fd, stdin, stdout, stderr + * @argc: Argument count + * @argv: Command line options, as we need to skip any file given via --fd + * + * Should: + * - close all open files except for standard streams and the one from --fd + */ +void isolate_fds(int argc, char **argv) +{ + const struct option optfd[] = { { "fd", required_argument, NULL, 'F' }, + { 0 }, }; + long fd = -1; + int name, rc; + + do { + name = getopt_long(argc, argv, "-:F:", optfd, NULL); + + if (name == 'F') + fd = conf_tap_fd(optarg); + } while (name != -1); + + if (fd == -1) { + rc = close_range(STDERR_FILENO + 1, ~0U, CLOSE_RANGE_UNSHARE); + } else if (fd == STDERR_FILENO + 1) { /* Still a single range */ + rc = close_range(STDERR_FILENO + 2, ~0U, CLOSE_RANGE_UNSHARE); + } else { + rc = close_range(STDERR_FILENO + 1, fd - 1, + CLOSE_RANGE_UNSHARE); + if (!rc) + rc = close_range(fd + 1, ~0U, CLOSE_RANGE_UNSHARE); + } - close_open_files(argc, argv); + if (rc) { + if (errno == ENOSYS || errno == EINVAL) { + /* This probably means close_range() or the + * CLOSE_RANGE_UNSHARE flag is not supported by the + * kernel. Not much we can do here except carry on and + * hope for the best. + */ + warn( +"Can't use close_range() to ensure no files leaked by parent"); + } else { + die_perror("Failed to close files leaked by parent"); + } + } } /** |
