andybons | ad92aa3 | 2015-08-31 02:27:44 | [diff] [blame^] | 1 | # Linux PID Namespace Support |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
andybons | ad92aa3 | 2015-08-31 02:27:44 | [diff] [blame^] | 3 | The [LinuxSUIDSandbox](linux_suid_sandbox.md) currently relies on support for |
| 4 | the `CLONE_NEWPID` flag in Linux's |
| 5 | [clone() system call](http://www.kernel.org/doc/man-pages/online/pages/man2/clone.2.html). |
| 6 | You can check whether your system supports PID namespaces with the code below, |
| 7 | which must be run as root: |
| 8 | |
| 9 | ```c |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 10 | #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 | |
| 20 | int 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 | |
| 31 | int 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 | } |
andybons | ad92aa3 | 2015-08-31 02:27:44 | [diff] [blame^] | 48 | ``` |