0001
0002 #define _GNU_SOURCE
0003 #include <sched.h>
0004 #include <unistd.h>
0005 #include <stdio.h>
0006 #include <stdlib.h>
0007 #include <signal.h>
0008 #include <errno.h>
0009 #include <sys/types.h>
0010 #include <sys/stat.h>
0011 #include <fcntl.h>
0012 #include <sys/ioctl.h>
0013 #include <sys/prctl.h>
0014 #include <sys/wait.h>
0015
0016 #define pr_err(fmt, ...) \
0017 ({ \
0018 fprintf(stderr, "%s:%d:" fmt ": %m\n", \
0019 __func__, __LINE__, ##__VA_ARGS__); \
0020 1; \
0021 })
0022
0023 #define NSIO 0xb7
0024 #define NS_GET_USERNS _IO(NSIO, 0x1)
0025 #define NS_GET_PARENT _IO(NSIO, 0x2)
0026
0027 #define __stack_aligned__ __attribute__((aligned(16)))
0028 struct cr_clone_arg {
0029 char stack[128] __stack_aligned__;
0030 char stack_ptr[];
0031 };
0032
0033 static int child(void *args)
0034 {
0035 prctl(PR_SET_PDEATHSIG, SIGKILL);
0036 while (1)
0037 sleep(1);
0038 exit(0);
0039 }
0040
0041 int main(int argc, char *argv[])
0042 {
0043 char *ns_strs[] = {"pid", "user"};
0044 char path[] = "/proc/0123456789/ns/pid";
0045 struct cr_clone_arg ca;
0046 struct stat st1, st2;
0047 int ns, pns, i;
0048 pid_t pid;
0049
0050 pid = clone(child, ca.stack_ptr, CLONE_NEWUSER | CLONE_NEWPID | SIGCHLD, NULL);
0051 if (pid < 0)
0052 return pr_err("clone");
0053
0054 for (i = 0; i < 2; i++) {
0055 snprintf(path, sizeof(path), "/proc/%d/ns/%s", pid, ns_strs[i]);
0056 ns = open(path, O_RDONLY);
0057 if (ns < 0)
0058 return pr_err("Unable to open %s", path);
0059
0060 pns = ioctl(ns, NS_GET_PARENT);
0061 if (pns < 0)
0062 return pr_err("Unable to get a parent pidns");
0063
0064 snprintf(path, sizeof(path), "/proc/self/ns/%s", ns_strs[i]);
0065 if (stat(path, &st2))
0066 return pr_err("Unable to stat %s", path);
0067 if (fstat(pns, &st1))
0068 return pr_err("Unable to stat the parent pidns");
0069 if (st1.st_ino != st2.st_ino)
0070 return pr_err("NS_GET_PARENT returned a wrong namespace");
0071
0072 if (ioctl(pns, NS_GET_PARENT) >= 0 || errno != EPERM)
0073 return pr_err("Don't get EPERM");
0074 }
0075
0076 kill(pid, SIGKILL);
0077 wait(NULL);
0078 return 0;
0079 }