|
| 1 | +/* |
| 2 | + * Check decoding of clone flags. |
| 3 | + * |
| 4 | + * Copyright (c) 2024 The strace developers. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +#include "tests.h" |
| 11 | +#include "xmalloc.h" |
| 12 | + |
| 13 | +#include <limits.h> |
| 14 | +#include <linux/sched.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <sys/socket.h> |
| 17 | +#include <sys/syscall.h> |
| 18 | +#include <unistd.h> |
| 19 | + |
| 20 | +static void |
| 21 | +retrieve_userns(int pid, char *userns_buf, size_t userns_buflen, int fd) |
| 22 | +{ |
| 23 | + char c = 0; |
| 24 | + if (read(fd, &c, 1) != 1) |
| 25 | + perror_msg_and_fail("read from the child"); |
| 26 | + |
| 27 | + char *fname = xasprintf("/proc/%d/ns/user", pid); |
| 28 | + int rc = readlink(fname, userns_buf, userns_buflen - 1); |
| 29 | + if ((unsigned int) rc >= userns_buflen) |
| 30 | + perror_msg_and_fail("readlink"); |
| 31 | + userns_buf[rc] = '\0'; |
| 32 | + |
| 33 | + if (write(fd, &c, 1) != 1) |
| 34 | + perror_msg_and_fail("write to the child"); |
| 35 | +} |
| 36 | + |
| 37 | +static void |
| 38 | +child_pause(int fd) |
| 39 | +{ |
| 40 | + char c = 0; |
| 41 | + if (write(fd, &c, 1) != 1) |
| 42 | + return; |
| 43 | + if (read(fd, &c, 1) != 1) |
| 44 | + return; |
| 45 | +} |
| 46 | + |
| 47 | +int |
| 48 | +main(void) |
| 49 | +{ |
| 50 | + static int child_sockpair[2]; |
| 51 | + if (socketpair(AF_UNIX, SOCK_STREAM, 0, child_sockpair) < 0) |
| 52 | + perror_msg_and_fail("socketpair"); |
| 53 | + |
| 54 | + struct clone_args arg = { .flags = CLONE_NEWUSER }; |
| 55 | + int pid = syscall(__NR_clone3, &arg, sizeof(arg)); |
| 56 | + if (pid < 0) |
| 57 | + perror_msg_and_fail("clone3"); |
| 58 | + if (pid > 0) { |
| 59 | + printf("clone3({flags=CLONE_NEWUSER, exit_signal=0, stack=NULL, stack_size=0}" |
| 60 | + ", %zu) = %s ", sizeof(arg), sprintrc(pid)); |
| 61 | + |
| 62 | + char userns[PATH_MAX]; |
| 63 | + retrieve_userns(pid, userns, sizeof(userns), child_sockpair[0]); |
| 64 | + printf("(%s)\n", userns); |
| 65 | + puts("+++ exited with 0 +++"); |
| 66 | + } else |
| 67 | + child_pause(child_sockpair[1]); |
| 68 | + return 0; |
| 69 | +} |
0 commit comments