|
| 1 | +/* |
| 2 | + * Check appending auxstr of unshare(2) when --namespace=switch is given |
| 3 | + * |
| 4 | + * This is derrived from unshare.c. |
| 5 | + * |
| 6 | + * Copyright (c) 2024 The strace developers. |
| 7 | + * All rights reserved. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 10 | + */ |
| 11 | + |
| 12 | +#include "tests.h" |
| 13 | +#include "scno.h" |
| 14 | + |
| 15 | +#include <limits.h> |
| 16 | +#include <stdio.h> |
| 17 | +#include <unistd.h> |
| 18 | + |
| 19 | +int |
| 20 | +main(void) |
| 21 | +{ |
| 22 | + static const kernel_ulong_t bogus_flags = |
| 23 | + (kernel_ulong_t) 0xbadc0ded0000000fULL; |
| 24 | + |
| 25 | + static struct { |
| 26 | + kernel_ulong_t val; |
| 27 | + const char *str; |
| 28 | + const char *fnames[8]; |
| 29 | + } unshare_flags[] = { |
| 30 | + { ARG_STR(0), { NULL } }, |
| 31 | + { 0x10000000, "CLONE_NEWUSER", { "user", NULL } }, |
| 32 | + { 0x00020000, "CLONE_NEWNS", { "mnt", NULL } }, |
| 33 | + { 0x00000080|0x00020000|0x02000000|0x04000000|0x08000000|0x20000000|0x40000000, |
| 34 | + "CLONE_NEWTIME|CLONE_NEWNS|CLONE_NEWCGROUP" |
| 35 | + "|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWPID" |
| 36 | + "|CLONE_NEWNET", { "cgroup", "ipc", "mnt", "net", "pid", "time", "uts", NULL } }, |
| 37 | + }; |
| 38 | + |
| 39 | + long rc = syscall(__NR_unshare, bogus_flags); |
| 40 | + printf("unshare(%#llx /* CLONE_??? */) = %s\n", |
| 41 | + (unsigned long long) bogus_flags, sprintrc(rc)); |
| 42 | + |
| 43 | + if (chdir("/proc/self/ns") != 0) |
| 44 | + perror_msg_and_skip("chdir"); |
| 45 | + |
| 46 | + for (unsigned int i = 0; i < ARRAY_SIZE(unshare_flags); ++i) { |
| 47 | + rc = syscall(__NR_unshare, unshare_flags[i].val); |
| 48 | + printf("unshare(%s) = %s%s", |
| 49 | + unshare_flags[i].str, sprintrc(rc), rc == 0? "": "\n"); |
| 50 | + |
| 51 | + if (rc == 0) { |
| 52 | + unsigned int j; |
| 53 | + for (j = 0; unshare_flags[i].fnames[j]; j++) { |
| 54 | + char buf[PATH_MAX + 1]; |
| 55 | + int n = readlink(unshare_flags[i].fnames[j], buf, sizeof(buf)); |
| 56 | + if (n < 0 || (size_t)n >= sizeof(buf)) |
| 57 | + perror_msg_and_skip("readlink"); |
| 58 | + buf[n] = '\0'; |
| 59 | + if (j == 0) |
| 60 | + printf(" (%s", buf); |
| 61 | + else |
| 62 | + printf(", %s", buf); |
| 63 | + } |
| 64 | + printf("%s", j == 0? "\n": ")\n"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + puts("+++ exited with 0 +++"); |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
0 commit comments