^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define pr_err(fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) fprintf(stderr, "%s:%d:" fmt ": %m\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) __func__, __LINE__, ##__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define NSIO 0xb7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define NS_GET_USERNS _IO(NSIO, 0x1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define NS_GET_PARENT _IO(NSIO, 0x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define __stack_aligned__ __attribute__((aligned(16)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct cr_clone_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) char stack[128] __stack_aligned__;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) char stack_ptr[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int child(void *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) prctl(PR_SET_PDEATHSIG, SIGKILL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) while (1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) sleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) char *ns_strs[] = {"pid", "user"};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) char path[] = "/proc/0123456789/ns/pid";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct cr_clone_arg ca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct stat st1, st2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int ns, pns, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) pid = clone(child, ca.stack_ptr, CLONE_NEWUSER | CLONE_NEWPID | SIGCHLD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (pid < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return pr_err("clone");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) snprintf(path, sizeof(path), "/proc/%d/ns/%s", pid, ns_strs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ns = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (ns < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return pr_err("Unable to open %s", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) pns = ioctl(ns, NS_GET_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (pns < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return pr_err("Unable to get a parent pidns");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) snprintf(path, sizeof(path), "/proc/self/ns/%s", ns_strs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (stat(path, &st2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return pr_err("Unable to stat %s", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (fstat(pns, &st1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return pr_err("Unable to stat the parent pidns");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (st1.st_ino != st2.st_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return pr_err("NS_GET_PARENT returned a wrong namespace");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (ioctl(pns, NS_GET_PARENT) >= 0 || errno != EPERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return pr_err("Don't get EPERM");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) kill(pid, SIGKILL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) wait(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }