blob: 81ce80fb740057bfd17158f328d82ce46c9d00a5 [file] [log] [blame] [view]
andybonsad92aa32015-08-31 02:27:441# Linux PID Namespace Support
andybons3322f762015-08-24 21:37:092
andybonsad92aa32015-08-31 02:27:443The [LinuxSUIDSandbox](linux_suid_sandbox.md) currently relies on support for
4the `CLONE_NEWPID` flag in Linux's
5[clone() system call](http://www.kernel.org/doc/man-pages/online/pages/man2/clone.2.html).
6You can check whether your system supports PID namespaces with the code below,
7which must be run as root:
8
9```c
andybons3322f762015-08-24 21:37:0910#define _GNU_SOURCE
11#include <unistd.h>
12#include <sched.h>
13#include <stdio.h>
14#include <sys/wait.h>
15
16#if !defined(CLONE_NEWPID)
17#define CLONE_NEWPID 0x20000000
18#endif
19
20int worker(void* arg) {
21 const pid_t pid = getpid();
22 if (pid == 1) {
23 printf("PID namespaces are working\n");
24 } else {
25 printf("PID namespaces ARE NOT working. Child pid: %d\n", pid);
26 }
27
28 return 0;
29}
30
31int main() {
32 if (getuid()) {
33 fprintf(stderr, "Must be run as root.\n");
34 return 1;
35 }
36
37 char stack[8192];
38 const pid_t child = clone(worker, stack + sizeof(stack), CLONE_NEWPID, NULL);
39 if (child == -1) {
40 perror("clone");
41 fprintf(stderr, "Clone failed. PID namespaces ARE NOT supported\n");
42 }
43
44 waitpid(child, NULL, 0);
45
46 return 0;
47}
andybonsad92aa32015-08-31 02:27:4448```