Skip to content

Commit

Permalink
sandbox: Unshare pids and mount a minimal /proc
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Larsson <[email protected]>
  • Loading branch information
alexlarsson committed Oct 12, 2023
1 parent 8a88bfa commit d79fd60
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tools/sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <sys/syscall.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef HAVE_SYS_CAPABILITY_H
#include <sys/capability.h>
#endif
Expand Down Expand Up @@ -91,10 +93,25 @@ static void do_namespace_sandbox(void)
#endif

ret = unshare(CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWUTS |
CLONE_NEWIPC | CLONE_NEWNET);
CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWPID);
if (ret < 0)
return;

pid_t pid = fork();
if (pid < 0)
err(EXIT_FAILURE, "fork");
if (pid != 0) {
int wstatus;
int res = waitpid(pid, &wstatus, 0);
if (res == -1)
err(EXIT_FAILURE, "waitpid");

if (!WIFEXITED(wstatus))
err(EXIT_FAILURE, "sandbox process died");

exit(WEXITSTATUS(wstatus));
}

fd = open("/proc/self/setgroups", O_WRONLY | O_CLOEXEC);
if (fd < 0)
err(EXIT_FAILURE, "open /proc/self/setgroups");
Expand Down Expand Up @@ -143,6 +160,14 @@ static void do_namespace_sandbox(void)
free(cwd);
cwd = NULL;

ret = mkdir("proc", 0755);
if (ret < 0)
err(EXIT_FAILURE, "mkdir /proc");

if (mount("proc", "proc", "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV,
"subset=pid,hidepid=noaccess") != 0)
err(EXIT_FAILURE, "mount /proc");

ret = pivot_root(".", ".");
if (ret < 0)
err(EXIT_FAILURE, "pivot_root");
Expand Down

0 comments on commit d79fd60

Please sign in to comment.