Commit

eadfe727dd800fc5fca33e45dbfd392ade5bbbd6

selftests/filesystems: test that a mntns file can't take over a lock

Add a case to the locked mount test:

- a bind of the mount namespace file of a grandchild moved beneath a
  locked cover must fail with EINVAL
- a bind of an ordinary file beneath it takes the lock over, the old
  cover can go and the new one can't
- the file is still covered after unshare(CLONE_NEWNS)

Link: https://patch.msgid.link/20261014-work-namei-locked-mnt_ns_file-v1-2-24035bd5d8e9@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Christian Brauner <brauner@kernel.org> · 2026-10-14 19:45 UTC

Browse files
1 file changed, +122 −0
modified tools/testing/selftests/filesystems/umount_propagation/locked_mount_test.c
@@ -9,6 +9,8 @@
9 9 #include <errno.h>
10 10 #include <fcntl.h>
11 11 #include <sched.h>
12 + #include <stdbool.h>
13 + #include <signal.h>
12 14 #include <stdio.h>
13 15 #include <stdlib.h>
14 16 #include <string.h>
@@ -27,6 +29,12 @@
27 29 #ifndef MOVE_MOUNT_BENEATH
28 30 #define MOVE_MOUNT_BENEATH 0x00000200
29 31 #endif
32 + #ifndef OPEN_TREE_CLONE
33 + #define OPEN_TREE_CLONE 1
34 + #endif
35 + #ifndef OPEN_TREE_CLOEXEC
36 + #define OPEN_TREE_CLOEXEC O_CLOEXEC
37 + #endif
30 38
31 39 #define DIR_LEN 64
32 40 #define PATH_LEN 192
@@ -120,6 +128,35 @@ static bool covered(const char *path)
120 128 return fd < 0;
121 129 }
122 130
131 + /* What @path reads, "" if it can't be read. */
132 + static int read_word(const char *path, char *buf, size_t len)
133 + {
134 + ssize_t n = 0;
135 + int fd;
136 +
137 + fd = open(path, O_RDONLY | O_CLOEXEC);
138 + if (fd >= 0) {
139 + n = read(fd, buf, len - 1);
140 + close(fd);
141 + }
142 + buf[n > 0 ? n : 0] = 0;
143 + return fd < 0 ? -1 : 0;
144 + }
145 +
146 + /* Put a clone of the mount at @src beneath the top mount at @where. */
147 + static int move_beneath(const char *src, const char *where)
148 + {
149 + int fd, ret;
150 +
151 + fd = syscall(__NR_open_tree, AT_FDCWD, src, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
152 + if (fd < 0)
153 + return -1;
154 + ret = syscall(__NR_move_mount, fd, "", AT_FDCWD, where,
155 + MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH);
156 + close(fd);
157 + return ret;
158 + }
159 +
123 160 FIXTURE(locked_mount) {
124 161 char base[DIR_LEN];
125 162 bool tracing; /* debugfs with the tracefs automount is there */
@@ -429,4 +466,89 @@ TEST_F(locked_mount, propagated_copy_keeps_the_cover_locked)
429 466 EXPECT_EQ(ret, CHILD_OK);
430 467 }
431 468
469 + /*
470 + * A mount of a mount namespace file is left out of the copy that
471 + * unshare(CLONE_NEWNS) makes, together with everything mounted on it, so
472 + * it can't take over the lock of a cover it is moved beneath. The child
473 + * tries just that with the namespace file of a grandchild and gets EINVAL.
474 + * A bind of an ordinary file takes the lock over, and the file is still
475 + * covered in the next copy of the mount namespace.
476 + */
477 + static int ns_file_child(const char *base)
478 + {
479 + char secret[PATH_LEN], mine[PATH_LEN], nsfile[PATH_LEN], buf[32];
480 + int ret = CHILD_OK, pfd[2];
481 + pid_t pid;
482 +
483 + snprintf(secret, sizeof(secret), "%s/P/secret", base);
484 + snprintf(mine, sizeof(mine), "%s/mine", base);
485 +
486 + if (enter_userns() || pipe(pfd))
487 + return CHILD_SETUP;
488 + /* the cover is locked */
489 + if (!umount2(secret, 0) || errno != EINVAL)
490 + return CHILD_STEP1;
491 +
492 + /* a mount namespace newer than our own, so that a bind of it is allowed */
493 + pid = fork();
494 + if (pid < 0)
495 + return CHILD_SETUP;
496 + if (pid == 0) {
497 + if (unshare(CLONE_NEWNS) || send_byte(pfd[1]))
498 + _exit(1);
499 + pause();
500 + _exit(0);
501 + }
502 + if (wait_byte(pfd[0]))
503 + return CHILD_SETUP;
504 + snprintf(nsfile, sizeof(nsfile), "/proc/%d/ns/mnt", pid);
505 + if (!move_beneath(nsfile, secret) || errno != EINVAL)
506 + ret = CHILD_STEP2;
507 + kill(pid, SIGKILL);
508 + waitpid(pid, NULL, 0);
509 + if (ret)
510 + return ret;
511 +
512 + /* an ordinary file beneath the cover takes the lock over */
513 + if (move_beneath(mine, secret))
514 + return CHILD_STEP3;
515 + if (umount2(secret, 0))
516 + return CHILD_STEP3;
517 + if (!umount2(secret, 0) || errno != EINVAL)
518 + return CHILD_STEP3;
519 + /* and it is in the next copy of the namespace */
520 + if (unshare(CLONE_NEWNS))
521 + return CHILD_SETUP;
522 + if (read_word(secret, buf, sizeof(buf)) || strcmp(buf, "mine"))
523 + return CHILD_STEP4;
524 + return CHILD_OK;
525 + }
526 +
527 + TEST_F(locked_mount, mount_namespace_file_cant_take_the_lock)
528 + {
529 + char p[PATH_LEN], q[PATH_LEN];
530 + pid_t pid;
531 + int ret;
532 +
533 + snprintf(p, sizeof(p), "%s/P", self->base);
534 + ASSERT_EQ(mkdir(p, 0755), 0);
535 + ASSERT_EQ(mount("tmpfs", p, "tmpfs", 0, "mode=0755"), 0);
536 + snprintf(p, sizeof(p), "%s/P/secret", self->base);
537 + ASSERT_EQ(create_file(p, "covered-by-root\n"), 0);
538 + snprintf(q, sizeof(q), "%s/P/empty", self->base);
539 + ASSERT_EQ(create_file(q, ""), 0);
540 + /* the cover: an empty file over the secret */
541 + ASSERT_EQ(mount(q, p, NULL, MS_BIND, NULL), 0);
542 + snprintf(q, sizeof(q), "%s/mine", self->base);
543 + ASSERT_EQ(create_file(q, "mine"), 0);
544 +
545 + pid = fork();
546 + ASSERT_GE(pid, 0);
547 + if (pid == 0)
548 + _exit(ns_file_child(self->base));
549 + ret = wait_child(pid);
550 + TH_LOG("child exit code %d", ret);
551 + EXPECT_EQ(ret, CHILD_OK);
552 + }
553 +
432 554 TEST_HARNESS_MAIN