at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * A layer of an overlay is a private clone of the mount it was given and |
| 4 | * belongs to no mount namespace. fanotify hands out descriptors on it. An |
| 5 | * automount triggered through one has no namespace to go into: the open has |
| 6 | * to fail, not oops with namespace_sem held. |
| 7 | */ |
| 8 | #define _GNU_SOURCE |
| 9 | #include <dirent.h> |
| 10 | #include <errno.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <poll.h> |
| 13 | #include <sched.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <unistd.h> |
| 18 | #include <linux/magic.h> |
| 19 | #include <sys/fanotify.h> |
| 20 | #include <sys/mount.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/vfs.h> |
| 23 | |
| 24 | #include "../../kselftest_harness.h" |
| 25 | |
| 26 | #define DIR_LEN 64 |
| 27 | #define PATH_LEN 192 |
| 28 | |
| 29 | static bool have_fs(const char *name) |
| 30 | { |
| 31 | char line[128]; |
| 32 | bool found = false; |
| 33 | FILE *f; |
| 34 | |
| 35 | f = fopen("/proc/filesystems", "re"); |
| 36 | if (!f) |
| 37 | return false; |
| 38 | while (fgets(line, sizeof(line), f)) { |
| 39 | char *nl = strchr(line, '\n'); |
| 40 | char *tab = strchr(line, '\t'); |
| 41 | |
| 42 | if (nl) |
| 43 | *nl = 0; |
| 44 | if (tab && !strcmp(tab + 1, name)) |
| 45 | found = true; |
| 46 | } |
| 47 | fclose(f); |
| 48 | return found; |
| 49 | } |
| 50 | |
| 51 | static int mnt_id_of(int fd) |
| 52 | { |
| 53 | char path[64], buf[4096], *p; |
| 54 | ssize_t n; |
| 55 | int info; |
| 56 | |
| 57 | snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd); |
| 58 | info = open(path, O_RDONLY | O_CLOEXEC); |
| 59 | if (info < 0) |
| 60 | return -1; |
| 61 | n = read(info, buf, sizeof(buf) - 1); |
| 62 | close(info); |
| 63 | if (n <= 0) |
| 64 | return -1; |
| 65 | buf[n] = 0; |
| 66 | p = strstr(buf, "mnt_id:"); |
| 67 | return p ? atoi(p + strlen("mnt_id:")) : -1; |
| 68 | } |
| 69 | |
| 70 | static bool on_debugfs(int fd) |
| 71 | { |
| 72 | struct statfs sf; |
| 73 | |
| 74 | return !fstatfs(fd, &sf) && sf.f_type == DEBUGFS_MAGIC; |
| 75 | } |
| 76 | |
| 77 | FIXTURE(layer) { |
| 78 | char base[DIR_LEN]; |
| 79 | int fan; |
| 80 | int evfd; /* on the layer clone of the lower debugfs */ |
| 81 | }; |
| 82 | |
| 83 | FIXTURE_SETUP(layer) |
| 84 | { |
| 85 | char lower[PATH_LEN], other[PATH_LEN], ovl[PATH_LEN], opts[2 * PATH_LEN + 16]; |
| 86 | struct fanotify_event_metadata *ev; |
| 87 | struct pollfd pfd; |
| 88 | char buf[4096]; |
| 89 | int lfd, lower_id; |
| 90 | ssize_t n; |
| 91 | DIR *d; |
| 92 | |
| 93 | self->fan = -1; |
| 94 | self->evfd = -1; |
| 95 | if (geteuid()) |
| 96 | SKIP(return, "test requires root"); |
| 97 | if (!have_fs("debugfs") || !have_fs("tracefs")) |
| 98 | SKIP(return, "test requires debugfs with the tracefs automount"); |
| 99 | if (!have_fs("overlay")) |
| 100 | SKIP(return, "test requires overlayfs"); |
| 101 | |
| 102 | snprintf(self->base, sizeof(self->base), "/tmp/layer.XXXXXX"); |
| 103 | ASSERT_NE(mkdtemp(self->base), NULL); |
| 104 | ASSERT_EQ(unshare(CLONE_NEWNS), 0); |
| 105 | ASSERT_EQ(mount("", "/", NULL, MS_REC | MS_PRIVATE, NULL), 0); |
| 106 | ASSERT_EQ(mount("tmpfs", self->base, "tmpfs", 0, "mode=0755"), 0); |
| 107 | snprintf(lower, sizeof(lower), "%s/lower", self->base); |
| 108 | snprintf(other, sizeof(other), "%s/other", self->base); |
| 109 | snprintf(ovl, sizeof(ovl), "%s/ovl", self->base); |
| 110 | ASSERT_EQ(mkdir(lower, 0755), 0); |
| 111 | ASSERT_EQ(mkdir(other, 0755), 0); |
| 112 | ASSERT_EQ(mkdir(ovl, 0755), 0); |
| 113 | ASSERT_EQ(mount("debugfs", lower, "debugfs", 0, NULL), 0); |
| 114 | snprintf(opts, sizeof(opts), "lowerdir=%s:%s", lower, other); |
| 115 | ASSERT_EQ(mount("overlay", ovl, "overlay", MS_RDONLY, opts), 0); |
| 116 | |
| 117 | self->fan = fanotify_init(FAN_CLASS_NOTIF | FAN_NONBLOCK | FAN_CLOEXEC, |
| 118 | O_RDONLY | O_CLOEXEC); |
| 119 | ASSERT_GE(self->fan, 0); |
| 120 | ASSERT_EQ(fanotify_mark(self->fan, FAN_MARK_ADD | FAN_MARK_FILESYSTEM, |
| 121 | FAN_OPEN | FAN_ONDIR, AT_FDCWD, lower), 0); |
| 122 | lfd = open(lower, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 123 | ASSERT_GE(lfd, 0); |
| 124 | lower_id = mnt_id_of(lfd); |
| 125 | close(lfd); |
| 126 | |
| 127 | /* the overlay opens its lower directory through the layer clone */ |
| 128 | d = opendir(ovl); |
| 129 | ASSERT_NE(d, NULL); |
| 130 | closedir(d); |
| 131 | pfd.fd = self->fan; |
| 132 | pfd.events = POLLIN; |
| 133 | ASSERT_EQ(poll(&pfd, 1, 5000), 1); |
| 134 | n = read(self->fan, buf, sizeof(buf)); |
| 135 | ASSERT_GT(n, 0); |
| 136 | for (ev = (void *)buf; FAN_EVENT_OK(ev, n); ev = FAN_EVENT_NEXT(ev, n)) { |
| 137 | if (ev->fd < 0) |
| 138 | continue; |
| 139 | if (self->evfd < 0 && on_debugfs(ev->fd) && |
| 140 | mnt_id_of(ev->fd) != lower_id) |
| 141 | self->evfd = ev->fd; |
| 142 | else |
| 143 | close(ev->fd); |
| 144 | } |
| 145 | ASSERT_GE(self->evfd, 0) |
| 146 | TH_LOG("no event on the layer clone"); |
| 147 | } |
| 148 | |
| 149 | FIXTURE_TEARDOWN(layer) |
| 150 | { |
| 151 | if (self->evfd >= 0) |
| 152 | close(self->evfd); |
| 153 | if (self->fan >= 0) |
| 154 | close(self->fan); |
| 155 | umount2(self->base, MNT_DETACH); |
| 156 | rmdir(self->base); |
| 157 | } |
| 158 | |
| 159 | TEST_F(layer, automount_below_the_clone_is_refused) |
| 160 | { |
| 161 | struct stat st; |
| 162 | int fd; |
| 163 | |
| 164 | /* the automount point is there */ |
| 165 | ASSERT_EQ(fstatat(self->evfd, "tracing", &st, AT_NO_AUTOMOUNT), 0); |
| 166 | /* the clone is in no namespace, so the automount has nowhere to go */ |
| 167 | fd = openat(self->evfd, "tracing", O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 168 | EXPECT_LT(fd, 0); |
| 169 | EXPECT_EQ(errno, EINVAL); |
| 170 | if (fd >= 0) |
| 171 | close(fd); |
| 172 | } |
| 173 | |
| 174 | TEST_HARNESS_MAIN |