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