1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 Federico Bonfiglio <[email protected]>
4 * Copyright (c) Linux Test Project, 2019-2022
5 */
6
7 /*\
8 * [Description]
9 *
10 * Test ioctl_ns with NS_GET_USERNS request.
11 *
12 * After the call to clone with the CLONE_NEWUSER flag,
13 * child is created in a new user namespace. That's checked by
14 * comparing its /proc/self/ns/user symlink and the parent's one,
15 * which should be different.
16 */
17
18 #define _GNU_SOURCE
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "tst_test.h"
24 #include "lapi/ioctl_ns.h"
25 #include "lapi/sched.h"
26
27 #define STACK_SIZE (1024 * 1024)
28
29 static char *child_stack;
30
setup(void)31 static void setup(void)
32 {
33 int exists = access("/proc/self/ns/user", F_OK);
34
35 if (exists < 0)
36 tst_res(TCONF, "namespace not available");
37
38 child_stack = ltp_alloc_stack(STACK_SIZE);
39 if (!child_stack)
40 tst_brk(TBROK|TERRNO, "stack alloc");
41 }
42
cleanup(void)43 static void cleanup(void)
44 {
45 free(child_stack);
46 }
47
child(void * arg LTP_ATTRIBUTE_UNUSED)48 static int child(void *arg LTP_ATTRIBUTE_UNUSED)
49 {
50 TST_CHECKPOINT_WAIT(0);
51 return 0;
52 }
53
run(void)54 static void run(void)
55 {
56 char child_namespace[30];
57
58 pid_t pid = ltp_clone(CLONE_NEWUSER | SIGCHLD, &child, 0,
59 STACK_SIZE, child_stack);
60 if (pid == -1)
61 tst_brk(TBROK | TERRNO, "ltp_clone failed");
62
63 snprintf(child_namespace, sizeof(child_namespace), "/proc/%i/ns/user", pid);
64 int my_fd, child_fd, parent_fd;
65
66 my_fd = SAFE_OPEN("/proc/self/ns/user", O_RDONLY);
67 child_fd = SAFE_OPEN(child_namespace, O_RDONLY);
68 parent_fd = ioctl(child_fd, NS_GET_USERNS);
69
70 if (parent_fd == -1) {
71 TST_CHECKPOINT_WAKE(0);
72
73 if (errno == ENOTTY)
74 tst_brk(TCONF, "ioctl(NS_GET_USERNS) not implemented");
75
76 tst_brk(TBROK | TERRNO, "ioctl(NS_GET_USERNS) failed");
77 }
78
79 struct stat my_stat, child_stat, parent_stat;
80
81 SAFE_FSTAT(my_fd, &my_stat);
82 SAFE_FSTAT(child_fd, &child_stat);
83 SAFE_FSTAT(parent_fd, &parent_stat);
84 if (my_stat.st_ino != parent_stat.st_ino)
85 tst_res(TFAIL, "parents have different inodes");
86 else if (parent_stat.st_ino == child_stat.st_ino)
87 tst_res(TFAIL, "child and parent have same inode");
88 else
89 tst_res(TPASS, "child and parent are consistent");
90 SAFE_CLOSE(my_fd);
91 SAFE_CLOSE(parent_fd);
92 SAFE_CLOSE(child_fd);
93 TST_CHECKPOINT_WAKE(0);
94 }
95
96 static struct tst_test test = {
97 .test_all = run,
98 .forks_child = 1,
99 .needs_root = 1,
100 .needs_checkpoints = 1,
101 .min_kver = "4.9",
102 .setup = setup,
103 .cleanup = cleanup,
104 };
105