1 // SPDX-License-Identifier: GPL-2.0-or-later
10 #include <sys/param.h>
11 #include <sys/mount.h>
13 #include <sys/statfs.h>
14 #include <linux/mount.h>
15 #include <linux/stat.h>
16 #include <asm/unistd.h>
18 #include "../../kselftest.h"
20 static const char *const known_fs[] = {
21 "9p", "adfs", "affs", "afs", "aio", "anon_inodefs", "apparmorfs",
22 "autofs", "bcachefs", "bdev", "befs", "bfs", "binder", "binfmt_misc",
23 "bpf", "btrfs", "btrfs_test_fs", "ceph", "cgroup", "cgroup2", "cifs",
24 "coda", "configfs", "cpuset", "cramfs", "cxl", "dax", "debugfs",
25 "devpts", "devtmpfs", "dmabuf", "drm", "ecryptfs", "efivarfs", "efs",
26 "erofs", "exfat", "ext2", "ext3", "ext4", "f2fs", "functionfs",
27 "fuse", "fuseblk", "fusectl", "gadgetfs", "gfs2", "gfs2meta", "hfs",
28 "hfsplus", "hostfs", "hpfs", "hugetlbfs", "ibmasmfs", "iomem",
29 "ipathfs", "iso9660", "jffs2", "jfs", "minix", "mqueue", "msdos",
30 "nfs", "nfs4", "nfsd", "nilfs2", "nsfs", "ntfs", "ntfs3", "ocfs2",
31 "ocfs2_dlmfs", "ocxlflash", "omfs", "openpromfs", "overlay", "pipefs",
32 "proc", "pstore", "pvfs2", "qnx4", "qnx6", "ramfs", "reiserfs",
33 "resctrl", "romfs", "rootfs", "rpc_pipefs", "s390_hypfs", "secretmem",
34 "securityfs", "selinuxfs", "smackfs", "smb3", "sockfs", "spufs",
35 "squashfs", "sysfs", "sysv", "tmpfs", "tracefs", "ubifs", "udf",
36 "ufs", "v7", "vboxsf", "vfat", "virtiofs", "vxfs", "xenfs", "xfs",
39 static int statmount(uint64_t mnt_id, uint64_t mask, struct statmount *buf,
40 size_t bufsize, unsigned int flags)
42 struct mnt_id_req req = {
43 .size = MNT_ID_REQ_SIZE_VER0,
48 return syscall(__NR_statmount, &req, buf, bufsize, flags);
51 static struct statmount *statmount_alloc(uint64_t mnt_id, uint64_t mask, unsigned int flags)
53 size_t bufsize = 1 << 15;
54 struct statmount *buf = NULL, *tmp = alloca(bufsize);
59 ret = statmount(mnt_id, mask, tmp, bufsize, flags);
64 if (errno != EOVERFLOW)
68 tmp = malloc(bufsize);
72 buf = malloc(tmp->size);
74 memcpy(buf, tmp, tmp->size);
81 static void write_file(const char *path, const char *val)
83 int fd = open(path, O_WRONLY);
84 size_t len = strlen(val);
88 ksft_exit_fail_msg("opening %s for write: %s\n", path, strerror(errno));
90 ret = write(fd, val, len);
92 ksft_exit_fail_msg("writing to %s: %s\n", path, strerror(errno));
94 ksft_exit_fail_msg("short write to %s\n", path);
98 ksft_exit_fail_msg("closing %s\n", path);
101 static uint64_t get_mnt_id(const char *name, const char *path, uint64_t mask)
106 ret = statx(AT_FDCWD, path, 0, mask, &sx);
108 ksft_exit_fail_msg("retrieving %s mount ID for %s: %s\n",
109 mask & STATX_MNT_ID_UNIQUE ? "unique" : "old",
110 name, strerror(errno));
111 if (!(sx.stx_mask & mask))
112 ksft_exit_fail_msg("no %s mount ID available for %s\n",
113 mask & STATX_MNT_ID_UNIQUE ? "unique" : "old",
116 return sx.stx_mnt_id;
120 static char root_mntpoint[] = "/tmp/statmount_test_root.XXXXXX";
121 static int orig_root;
122 static uint64_t root_id, parent_id;
123 static uint32_t old_root_id, old_parent_id;
126 static void cleanup_namespace(void)
130 umount2(root_mntpoint, MNT_DETACH);
131 rmdir(root_mntpoint);
134 static void setup_namespace(void)
138 uid_t uid = getuid();
139 gid_t gid = getgid();
141 ret = unshare(CLONE_NEWNS|CLONE_NEWUSER);
143 ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
146 sprintf(buf, "0 %d 1", uid);
147 write_file("/proc/self/uid_map", buf);
148 write_file("/proc/self/setgroups", "deny");
149 sprintf(buf, "0 %d 1", gid);
150 write_file("/proc/self/gid_map", buf);
152 ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
154 ksft_exit_fail_msg("making mount tree private: %s\n",
157 if (!mkdtemp(root_mntpoint))
158 ksft_exit_fail_msg("creating temporary directory %s: %s\n",
159 root_mntpoint, strerror(errno));
161 old_parent_id = get_mnt_id("parent", root_mntpoint, STATX_MNT_ID);
162 parent_id = get_mnt_id("parent", root_mntpoint, STATX_MNT_ID_UNIQUE);
164 orig_root = open("/", O_PATH);
166 ksft_exit_fail_msg("opening root directory: %s",
169 atexit(cleanup_namespace);
171 ret = mount(root_mntpoint, root_mntpoint, NULL, MS_BIND, NULL);
173 ksft_exit_fail_msg("mounting temp root %s: %s\n",
174 root_mntpoint, strerror(errno));
176 ret = chroot(root_mntpoint);
178 ksft_exit_fail_msg("chroot to temp root %s: %s\n",
179 root_mntpoint, strerror(errno));
183 ksft_exit_fail_msg("chdir to root: %s\n", strerror(errno));
185 old_root_id = get_mnt_id("root", "/", STATX_MNT_ID);
186 root_id = get_mnt_id("root", "/", STATX_MNT_ID_UNIQUE);
189 static int setup_mount_tree(int log2_num)
193 ret = mount("", "/", NULL, MS_REC|MS_SHARED, NULL);
195 ksft_test_result_fail("making mount tree shared: %s\n",
200 for (i = 0; i < log2_num; i++) {
201 ret = mount("/", "/", NULL, MS_BIND, NULL);
203 ksft_test_result_fail("mounting submount %s: %s\n",
204 root_mntpoint, strerror(errno));
211 static ssize_t listmount(uint64_t mnt_id, uint64_t last_mnt_id,
212 uint64_t list[], size_t num, unsigned int flags)
214 struct mnt_id_req req = {
215 .size = MNT_ID_REQ_SIZE_VER0,
217 .param = last_mnt_id,
220 return syscall(__NR_listmount, &req, list, num, flags);
223 static void test_listmount_empty_root(void)
226 const unsigned int size = 32;
229 res = listmount(LSMT_ROOT, 0, list, size, 0);
231 ksft_test_result_fail("listmount: %s\n", strerror(errno));
235 ksft_test_result_fail("listmount result is %zi != 1\n", res);
239 if (list[0] != root_id) {
240 ksft_test_result_fail("listmount ID doesn't match 0x%llx != 0x%llx\n",
241 (unsigned long long) list[0],
242 (unsigned long long) root_id);
246 ksft_test_result_pass("listmount empty root\n");
249 static void test_statmount_zero_mask(void)
254 ret = statmount(root_id, 0, &sm, sizeof(sm), 0);
256 ksft_test_result_fail("statmount zero mask: %s\n",
260 if (sm.size != sizeof(sm)) {
261 ksft_test_result_fail("unexpected size: %u != %u\n",
262 sm.size, (uint32_t) sizeof(sm));
266 ksft_test_result_fail("unexpected mask: 0x%llx != 0x0\n",
267 (unsigned long long) sm.mask);
271 ksft_test_result_pass("statmount zero mask\n");
274 static void test_statmount_mnt_basic(void)
278 uint64_t mask = STATMOUNT_MNT_BASIC;
280 ret = statmount(root_id, mask, &sm, sizeof(sm), 0);
282 ksft_test_result_fail("statmount mnt basic: %s\n",
286 if (sm.size != sizeof(sm)) {
287 ksft_test_result_fail("unexpected size: %u != %u\n",
288 sm.size, (uint32_t) sizeof(sm));
291 if (sm.mask != mask) {
292 ksft_test_result_skip("statmount mnt basic unavailable\n");
296 if (sm.mnt_id != root_id) {
297 ksft_test_result_fail("unexpected root ID: 0x%llx != 0x%llx\n",
298 (unsigned long long) sm.mnt_id,
299 (unsigned long long) root_id);
303 if (sm.mnt_id_old != old_root_id) {
304 ksft_test_result_fail("unexpected old root ID: %u != %u\n",
305 sm.mnt_id_old, old_root_id);
309 if (sm.mnt_parent_id != parent_id) {
310 ksft_test_result_fail("unexpected parent ID: 0x%llx != 0x%llx\n",
311 (unsigned long long) sm.mnt_parent_id,
312 (unsigned long long) parent_id);
316 if (sm.mnt_parent_id_old != old_parent_id) {
317 ksft_test_result_fail("unexpected old parent ID: %u != %u\n",
318 sm.mnt_parent_id_old, old_parent_id);
322 if (sm.mnt_propagation != MS_PRIVATE) {
323 ksft_test_result_fail("unexpected propagation: 0x%llx\n",
324 (unsigned long long) sm.mnt_propagation);
328 ksft_test_result_pass("statmount mnt basic\n");
332 static void test_statmount_sb_basic(void)
336 uint64_t mask = STATMOUNT_SB_BASIC;
340 ret = statmount(root_id, mask, &sm, sizeof(sm), 0);
342 ksft_test_result_fail("statmount sb basic: %s\n",
346 if (sm.size != sizeof(sm)) {
347 ksft_test_result_fail("unexpected size: %u != %u\n",
348 sm.size, (uint32_t) sizeof(sm));
351 if (sm.mask != mask) {
352 ksft_test_result_skip("statmount sb basic unavailable\n");
356 ret = statx(AT_FDCWD, "/", 0, 0, &sx);
358 ksft_test_result_fail("stat root failed: %s\n",
363 if (sm.sb_dev_major != sx.stx_dev_major ||
364 sm.sb_dev_minor != sx.stx_dev_minor) {
365 ksft_test_result_fail("unexpected sb dev %u:%u != %u:%u\n",
366 sm.sb_dev_major, sm.sb_dev_minor,
367 sx.stx_dev_major, sx.stx_dev_minor);
371 ret = statfs("/", &sf);
373 ksft_test_result_fail("statfs root failed: %s\n",
378 if (sm.sb_magic != sf.f_type) {
379 ksft_test_result_fail("unexpected sb magic: 0x%llx != 0x%lx\n",
380 (unsigned long long) sm.sb_magic,
385 ksft_test_result_pass("statmount sb basic\n");
388 static void test_statmount_mnt_point(void)
390 struct statmount *sm;
392 sm = statmount_alloc(root_id, STATMOUNT_MNT_POINT, 0);
394 ksft_test_result_fail("statmount mount point: %s\n",
399 if (strcmp(sm->str + sm->mnt_point, "/") != 0) {
400 ksft_test_result_fail("unexpected mount point: '%s' != '/'\n",
401 sm->str + sm->mnt_point);
404 ksft_test_result_pass("statmount mount point\n");
409 static void test_statmount_mnt_root(void)
411 struct statmount *sm;
412 const char *mnt_root, *last_dir, *last_root;
414 last_dir = strrchr(root_mntpoint, '/');
418 sm = statmount_alloc(root_id, STATMOUNT_MNT_ROOT, 0);
420 ksft_test_result_fail("statmount mount root: %s\n",
424 mnt_root = sm->str + sm->mnt_root;
425 last_root = strrchr(mnt_root, '/');
429 last_root = mnt_root;
431 if (strcmp(last_dir, last_root) != 0) {
432 ksft_test_result_fail("unexpected mount root last component: '%s' != '%s'\n",
433 last_root, last_dir);
436 ksft_test_result_pass("statmount mount root\n");
441 static void test_statmount_fs_type(void)
443 struct statmount *sm;
445 const char *const *s;
447 sm = statmount_alloc(root_id, STATMOUNT_FS_TYPE, 0);
449 ksft_test_result_fail("statmount fs type: %s\n",
453 fs_type = sm->str + sm->fs_type;
454 for (s = known_fs; s != NULL; s++) {
455 if (strcmp(fs_type, *s) == 0)
459 ksft_print_msg("unknown filesystem type: %s\n", fs_type);
461 ksft_test_result_pass("statmount fs type\n");
465 static void test_statmount_string(uint64_t mask, size_t off, const char *name)
467 struct statmount *sm;
468 size_t len, shortsize, exactsize;
472 sm = statmount_alloc(root_id, mask, 0);
474 ksft_test_result_fail("statmount %s: %s\n", name,
478 if (sm->size < sizeof(*sm)) {
479 ksft_test_result_fail("unexpected size: %u < %u\n",
480 sm->size, (uint32_t) sizeof(*sm));
483 if (sm->mask != mask) {
484 ksft_test_result_skip("statmount %s unavailable\n", name);
487 len = sm->size - sizeof(*sm);
488 start = ((uint32_t *) sm)[off];
490 for (i = start;; i++) {
492 ksft_test_result_fail("string out of bounds\n");
498 exactsize = sm->size;
499 shortsize = sizeof(*sm) + i;
501 ret = statmount(root_id, mask, sm, exactsize, 0);
503 ksft_test_result_fail("statmount exact size: %s\n",
508 ret = statmount(root_id, mask, sm, shortsize, 0);
509 if (ret != -1 || errno != EOVERFLOW) {
510 ksft_test_result_fail("should have failed with EOVERFLOW: %s\n",
515 ksft_test_result_pass("statmount string %s\n", name);
520 static void test_listmount_tree(void)
523 const unsigned int log2_num = 4;
524 const unsigned int step = 3;
525 const unsigned int size = (1 << log2_num) + step + 1;
526 size_t num, expect = 1 << log2_num;
528 uint64_t list2[size];
532 res = setup_mount_tree(log2_num);
536 num = res = listmount(LSMT_ROOT, 0, list, size, 0);
538 ksft_test_result_fail("listmount: %s\n", strerror(errno));
542 ksft_test_result_fail("listmount result is %zi != %zi\n",
547 for (i = 0; i < size - step;) {
548 res = listmount(LSMT_ROOT, i ? list2[i - 1] : 0, list2 + i, step, 0);
550 ksft_test_result_fail("short listmount: %s\n",
557 ksft_test_result_fail("different number of entries: %zu != %zu\n",
561 for (i = 0; i < num; i++) {
562 if (list2[i] != list[i]) {
563 ksft_test_result_fail("different value for entry %zu: 0x%llx != 0x%llx\n",
565 (unsigned long long) list2[i],
566 (unsigned long long) list[i]);
570 ksft_test_result_pass("listmount tree\n");
573 #define str_off(memb) (offsetof(struct statmount, memb) / sizeof(uint32_t))
578 uint64_t all_mask = STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC |
579 STATMOUNT_PROPAGATE_FROM | STATMOUNT_MNT_ROOT |
580 STATMOUNT_MNT_POINT | STATMOUNT_FS_TYPE;
584 ret = statmount(0, 0, NULL, 0, 0);
587 ksft_exit_skip("statmount() syscall not supported\n");
592 test_listmount_empty_root();
593 test_statmount_zero_mask();
594 test_statmount_mnt_basic();
595 test_statmount_sb_basic();
596 test_statmount_mnt_root();
597 test_statmount_mnt_point();
598 test_statmount_fs_type();
599 test_statmount_string(STATMOUNT_MNT_ROOT, str_off(mnt_root), "mount root");
600 test_statmount_string(STATMOUNT_MNT_POINT, str_off(mnt_point), "mount point");
601 test_statmount_string(STATMOUNT_FS_TYPE, str_off(fs_type), "fs type");
602 test_statmount_string(all_mask, str_off(mnt_root), "mount root & all");
603 test_statmount_string(all_mask, str_off(mnt_point), "mount point & all");
604 test_statmount_string(all_mask, str_off(fs_type), "fs type & all");
606 test_listmount_tree();
609 if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)