Skip to content

Instantly share code, notes, and snippets.

@maxux
Last active May 22, 2019 02:38
Show Gist options
  • Save maxux/098144c5ca9fa4ede92a5bfaeddb012d to your computer and use it in GitHub Desktop.
Save maxux/098144c5ca9fa4ede92a5bfaeddb012d to your computer and use it in GitHub Desktop.
laptix /home/maxux/git/coreX # ls /tmp/new-root/
helloworld init
laptix /home/maxux/git/coreX # ./poc
PID: 17504
Parent: waiting...
Executing init
CHILD PID: 1
----
init
.
..
helloworld
----
Hello 0 ! (pid: 1)
Hello 1 ! (pid: 1)
Hello 2 ! (pid: 1)
Hello 3 ! (pid: 1)
...
#include <stdio.h>
#include <unistd.h>
int main(void) {
for(int i = 0; i < 64; i++) {
printf("Hello %d ! (pid: %d)\n", i, getpid());
sleep(1);
}
return 0;
}
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
void diep(char *str) {
perror(str);
exit(EXIT_FAILURE);
}
void fileslist(char *path) {
struct dirent *de;
DIR *dr;
if(!(dr = opendir(path))) {
printf("Could not open current directory");
return;
}
printf("----\n");
while((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);
printf("----\n");
closedir(dr);
}
int main(void) {
if(unshare(CLONE_FS | CLONE_NEWPID))
perror("unshare");
if(chroot("/tmp/new-root") < 0)
diep("chroot");
printf("PID: %d\n", getpid());
pid_t pid = fork();
if(pid < 0)
perror("fork");
if(pid == 0) {
printf("Executing init\n");
printf("CHILD PID: %d\n", getpid());
fileslist("/");
if(execl("/init", "/init", NULL) < 0)
perror("system");
}
printf("Parent: waiting...\n");
int value;
wait(&value);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment