1 // SPDX-License-Identifier: GPL-2.0-or-later
10 #include <sys/mount.h>
13 #include <linux/nsfs.h>
14 #include <linux/stat.h>
16 #include "statmount.h"
17 #include "../../kselftest.h"
24 static void handle_result(int ret, const char *testname)
27 ksft_test_result_pass("%s\n", testname);
28 else if (ret == NSID_FAIL)
29 ksft_test_result_fail("%s\n", testname);
30 else if (ret == NSID_ERROR)
31 ksft_exit_fail_msg("%s\n", testname);
33 ksft_test_result_skip("%s\n", testname);
36 static inline int wait_for_pid(pid_t pid)
41 ret = waitpid(pid, &status, 0);
46 ksft_print_msg("waitpid returned -1, errno=%d\n", errno);
50 if (!WIFEXITED(status)) {
52 "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n",
53 WIFSIGNALED(status), WTERMSIG(status));
57 ret = WEXITSTATUS(status);
61 static int get_mnt_ns_id(const char *mnt_ns, uint64_t *mnt_ns_id)
63 int fd = open(mnt_ns, O_RDONLY);
66 ksft_print_msg("failed to open for ns %s: %s\n",
67 mnt_ns, strerror(errno));
72 if (ioctl(fd, NS_GET_MNTNS_ID, mnt_ns_id) < 0) {
73 ksft_print_msg("failed to get the nsid for ns %s: %s\n",
74 mnt_ns, strerror(errno));
81 static int get_mnt_id(const char *path, uint64_t *mnt_id)
86 ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
88 ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,
93 if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {
94 ksft_print_msg("no unique mount ID available for %s\n", path);
98 *mnt_id = sx.stx_mnt_id;
102 static int write_file(const char *path, const char *val)
104 int fd = open(path, O_WRONLY);
105 size_t len = strlen(val);
109 ksft_print_msg("opening %s for write: %s\n", path, strerror(errno));
113 ret = write(fd, val, len);
115 ksft_print_msg("writing to %s: %s\n", path, strerror(errno));
119 ksft_print_msg("short write to %s\n", path);
125 ksft_print_msg("closing %s\n", path);
132 static int setup_namespace(void)
136 uid_t uid = getuid();
137 gid_t gid = getgid();
139 ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);
141 ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
144 sprintf(buf, "0 %d 1", uid);
145 ret = write_file("/proc/self/uid_map", buf);
146 if (ret != NSID_PASS)
148 ret = write_file("/proc/self/setgroups", "deny");
149 if (ret != NSID_PASS)
151 sprintf(buf, "0 %d 1", gid);
152 ret = write_file("/proc/self/gid_map", buf);
153 if (ret != NSID_PASS)
156 ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
158 ksft_print_msg("making mount tree private: %s\n",
166 static int _test_statmount_mnt_ns_id(void)
173 ret = get_mnt_ns_id("/proc/self/ns/mnt", &mnt_ns_id);
174 if (ret != NSID_PASS)
177 ret = get_mnt_id("/", &root_id);
178 if (ret != NSID_PASS)
181 ret = statmount(root_id, 0, STATMOUNT_MNT_NS_ID, &sm, sizeof(sm), 0);
183 ksft_print_msg("statmount mnt ns id: %s\n", strerror(errno));
187 if (sm.size != sizeof(sm)) {
188 ksft_print_msg("unexpected size: %u != %u\n", sm.size,
189 (uint32_t)sizeof(sm));
192 if (sm.mask != STATMOUNT_MNT_NS_ID) {
193 ksft_print_msg("statmount mnt ns id unavailable\n");
197 if (sm.mnt_ns_id != mnt_ns_id) {
198 ksft_print_msg("unexpected mnt ns ID: 0x%llx != 0x%llx\n",
199 (unsigned long long)sm.mnt_ns_id,
200 (unsigned long long)mnt_ns_id);
207 static void test_statmount_mnt_ns_id(void)
214 ksft_exit_fail_msg("failed to fork: %s\n", strerror(errno));
216 /* We're the original pid, wait for the result. */
218 ret = wait_for_pid(pid);
219 handle_result(ret, "test statmount ns id");
223 ret = setup_namespace();
224 if (ret != NSID_PASS)
226 ret = _test_statmount_mnt_ns_id();
230 static int validate_external_listmount(pid_t pid, uint64_t child_nr_mounts)
238 /* Get the mount ns id for our child. */
239 snprintf(buf, sizeof(buf), "/proc/%lu/ns/mnt", (unsigned long)pid);
240 ret = get_mnt_ns_id(buf, &mnt_ns_id);
242 nr_mounts = listmount(LSMT_ROOT, mnt_ns_id, 0, list, 256, 0);
243 if (nr_mounts == (uint64_t)-1) {
244 ksft_print_msg("listmount: %s\n", strerror(errno));
248 if (nr_mounts != child_nr_mounts) {
249 ksft_print_msg("listmount results is %zi != %zi\n", nr_mounts,
254 /* Validate that all of our entries match our mnt_ns_id. */
255 for (int i = 0; i < nr_mounts; i++) {
258 ret = statmount(list[i], mnt_ns_id, STATMOUNT_MNT_NS_ID, &sm,
261 ksft_print_msg("statmount mnt ns id: %s\n", strerror(errno));
265 if (sm.mask != STATMOUNT_MNT_NS_ID) {
266 ksft_print_msg("statmount mnt ns id unavailable\n");
270 if (sm.mnt_ns_id != mnt_ns_id) {
271 ksft_print_msg("listmount gave us the wrong ns id: 0x%llx != 0x%llx\n",
272 (unsigned long long)sm.mnt_ns_id,
273 (unsigned long long)mnt_ns_id);
281 static void test_listmount_ns(void)
285 int child_ready_pipe[2];
286 int parent_ready_pipe[2];
290 if (pipe(child_ready_pipe) < 0)
291 ksft_exit_fail_msg("failed to create the child pipe: %s\n",
293 if (pipe(parent_ready_pipe) < 0)
294 ksft_exit_fail_msg("failed to create the parent pipe: %s\n",
299 ksft_exit_fail_msg("failed to fork: %s\n", strerror(errno));
305 close(child_ready_pipe[0]);
306 close(parent_ready_pipe[1]);
308 ret = setup_namespace();
309 if (ret != NSID_PASS)
312 nr_mounts = listmount(LSMT_ROOT, 0, 0, list, 256, 0);
313 if (nr_mounts == (uint64_t)-1) {
314 ksft_print_msg("listmount: %s\n", strerror(errno));
319 * Tell our parent how many mounts we have, and then wait for it
320 * to tell us we're done.
322 if (write(child_ready_pipe[1], &nr_mounts, sizeof(nr_mounts)) !=
325 if (read(parent_ready_pipe[0], &cval, sizeof(cval)) != sizeof(cval))
330 close(child_ready_pipe[1]);
331 close(parent_ready_pipe[0]);
333 /* Wait until the child has created everything. */
334 if (read(child_ready_pipe[0], &nr_mounts, sizeof(nr_mounts)) !=
338 ret = validate_external_listmount(pid, nr_mounts);
340 if (write(parent_ready_pipe[1], &pval, sizeof(pval)) != sizeof(pval))
343 child_ret = wait_for_pid(pid);
344 if (child_ret != NSID_PASS)
346 handle_result(ret, "test listmount ns id");
354 ret = statmount(0, 0, 0, NULL, 0, 0);
357 ksft_exit_skip("statmount() syscall not supported\n");
360 test_statmount_mnt_ns_id();
363 if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)