Skip to content

Instantly share code, notes, and snippets.

@hvdijk
Created February 13, 2015 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hvdijk/e3a6443ac40574331790 to your computer and use it in GitHub Desktop.
Save hvdijk/e3a6443ac40574331790 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sched.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
void error (const char *func) {
perror (func);
exit (1);
}
int child (void *arg) {
if (mount (0, "/", 0, MS_REC | MS_PRIVATE, 0))
error ("mount[1]");
if (mount (".", ".", 0, MS_BIND, 0))
error ("mount[2]");
if (chdir ("/bin"))
error ("chdir[2]");
if (mount (".", "/", 0, MS_MOVE, 0))
error ("mount[3]");
system ("ls /");
return 0;
}
#define CHILD_STACK_SIZE (1 << 13)
int main () {
if (chdir ("/bin"))
error ("chdir[1]");
char *child_stack = malloc(CHILD_STACK_SIZE);
if (child_stack == 0)
error ("malloc");
int child_pid = clone (child, child_stack + CHILD_STACK_SIZE, CLONE_NEWNS | CLONE_NEWUSER | SIGCHLD, 0);
if (child_pid == -1)
error ("clone");
if (waitpid (child_pid, 0, 0) == -1)
error ("waitpid");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment