Skip to content

Instantly share code, notes, and snippets.

@blogle
Last active January 8, 2018 03:50
Show Gist options
  • Save blogle/72de7e71a13e7e70db1d61a42c3ea59c to your computer and use it in GitHub Desktop.
Save blogle/72de7e71a13e7e70db1d61a42c3ea59c to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <linux/capability.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int child(void *args)
{
printf("pid as seen in the child: %lu\n", (unsigned long)getpid());
pid_t p = mount("overlay", "./hatch/mnt", "overlay", 0, "lowerdir=./hatch/lower,upperdir=./hatch/upper,workdir=./hatch/work");
if (p == -1){
perror("mount");
exit(1);
}
// Expose the mount to the parent namespace
p = mount("none", "./hatch/mnt", NULL, MS_SHARED, NULL);
if (p == -1){
perror("mount");
exit(1);
}
char *newargv[] = { "/bin/bash", NULL };
execv("/bin/bash", newargv);
perror("exec");
exit(EXIT_FAILURE);
return 0;
}
int main()
{
pid_t p = clone(child, malloc(4096) + 4096, CLONE_NEWNS | CLONE_NEWUSER | SIGCHLD, NULL);
if (p == -1) {
perror("clone");
exit(1);
}
printf("child pid: %lu\n", (unsigned long)p);
waitpid(p, NULL, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment