diff options
author | Stefano Brivio <sbrivio@redhat.com> | 2021-10-14 04:54:06 +0200 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2021-10-14 13:19:25 +0200 |
commit | 3bb859c50522e92f01241403501049692f98d25d (patch) | |
tree | b4e98ab6dab02516cc78af2ca224010efe2e525d | |
parent | fc93f977741f4f32adde7f051a0bd21673d0e94e (diff) | |
download | passt-3bb859c50522e92f01241403501049692f98d25d.tar passt-3bb859c50522e92f01241403501049692f98d25d.tar.gz passt-3bb859c50522e92f01241403501049692f98d25d.tar.bz2 passt-3bb859c50522e92f01241403501049692f98d25d.tar.lz passt-3bb859c50522e92f01241403501049692f98d25d.tar.xz passt-3bb859c50522e92f01241403501049692f98d25d.tar.zst passt-3bb859c50522e92f01241403501049692f98d25d.zip |
passt: Warn if we're running as root, abort if we can't change to nobody:nobody
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r-- | passt.c | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -56,6 +56,8 @@ #include <linux/filter.h> #include <stddef.h> #include <linux/capability.h> +#include <pwd.h> +#include <grp.h> #include "seccomp.h" #include "util.h" @@ -187,6 +189,30 @@ static void seccomp(struct ctx *c) } /** + * check_root() - Warn if we're running as root, exit if we can't drop to nobody + */ +static void check_root(void) +{ + struct passwd *pw; + + if (getuid() && geteuid()) + return; + + fprintf(stderr, "Don't run this as root. Changing to nobody...\n"); + pw = getpwnam("nobody"); + if (!pw) { + perror("getpwnam"); + exit(EXIT_FAILURE); + } + + if (initgroups(pw->pw_name, pw->pw_gid) || + setgid(pw->pw_gid) || setuid(pw->pw_uid)) { + fprintf(stderr, "Can't change to user/group nobody, exiting"); + exit(EXIT_FAILURE); + } +} + +/** * drop_caps() - Drop capabilities we might have except for CAP_NET_BIND_SERVICE */ static void drop_caps(void) @@ -223,6 +249,9 @@ int main(int argc, char **argv) char *log_name; int nfds, i; +#ifndef PASST_LEGACY_NO_OPTIONS + check_root(); +#endif drop_caps(); if (strstr(argv[0], "pasta") || strstr(argv[0], "passt4netns")) { |