1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (c) 2025 Miklos Szeredi <miklos@szeredi.hu>
#define _GNU_SOURCE
// Needed for linux/fanotify.h
typedef struct {
int val[2];
} __kernel_fsid_t;
#define __kernel_fsid_t __kernel_fsid_t
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/fanotify.h>
#include "../../kselftest_harness.h"
#include "../statmount/statmount.h"
#include "../utils.h"
static const char root_mntpoint_templ[] = "/tmp/mount-notify_test_root.XXXXXX";
static const int mark_cmds[] = {
FAN_MARK_ADD,
FAN_MARK_REMOVE,
FAN_MARK_FLUSH
};
#define NUM_FAN_FDS ARRAY_SIZE(mark_cmds)
FIXTURE(fanotify) {
int fan_fd[NUM_FAN_FDS];
char buf[256];
unsigned int rem;
void *next;
char root_mntpoint[sizeof(root_mntpoint_templ)];
int orig_root;
int ns_fd;
uint64_t root_id;
};
FIXTURE_SETUP(fanotify)
{
int i, ret;
ASSERT_EQ(unshare(CLONE_NEWNS), 0);
self->ns_fd = open("/proc/self/ns/mnt", O_RDONLY);
ASSERT_GE(self->ns_fd, 0);
ASSERT_EQ(mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL), 0);
strcpy(self->root_mntpoint, root_mntpoint_templ);
ASSERT_NE(mkdtemp(self->root_mntpoint), NULL);
self->orig_root = open("/", O_PATH | O_CLOEXEC);
ASSERT_GE(self->orig_root, 0);
ASSERT_EQ(mount("tmpfs", self->root_mntpoint, "tmpfs", 0, NULL), 0);
ASSERT_EQ(chroot(self->root_mntpoint), 0);
ASSERT_EQ(chdir("/"), 0);
ASSERT_EQ(mkdir("a", 0700), 0);
ASSERT_EQ(mkdir("b", 0700), 0);
self->root_id = get_unique_mnt_id("/");
ASSERT_NE(self->root_id, 0);
for (i = 0; i < NUM_FAN_FDS; i++) {
self->fan_fd[i] = fanotify_init(FAN_REPORT_MNT | FAN_NONBLOCK,
0);
ASSERT_GE(self->fan_fd[i], 0);
ret = fanotify_mark(self->fan_fd[i], FAN_MARK_ADD |
FAN_MARK_MNTNS,
FAN_MNT_ATTACH | FAN_MNT_DETACH,
self->ns_fd, NULL);
ASSERT_EQ(ret, 0);
// On fd[0] we do an extra ADD that changes nothing.
// On fd[1]/fd[2] we REMOVE/FLUSH which removes the mark.
ret = fanotify_mark(self->fan_fd[i], mark_cmds[i] |
FAN_MARK_MNTNS,
FAN_MNT_ATTACH | FAN_MNT_DETACH,
self->ns_fd, NULL);
ASSERT_EQ(ret, 0);
}
self->rem = 0;
}
FIXTURE_TEARDOWN(fanotify)
{
int i;
ASSERT_EQ(self->rem, 0);
for (i = 0; i < NUM_FAN_FDS; i++)
close(self->fan_fd[i]);
ASSERT_EQ(fchdir(self->orig_root), 0);
ASSERT_EQ(chroot("."), 0);
EXPECT_EQ(umount2(self->root_mntpoint, MNT_DETACH), 0);
EXPECT_EQ(chdir(self->root_mntpoint), 0);
EXPECT_EQ(chdir("/"), 0);
EXPECT_EQ(rmdir(self->root_mntpoint), 0);
}
static uint64_t expect_notify(struct __test_metadata *const _metadata,
FIXTURE_DATA(fanotify) *self,
uint64_t *mask)
{
struct fanotify_event_metadata *meta;
struct fanotify_event_info_mnt *mnt;
unsigned int thislen;
if (!self->rem) {
ssize_t len;
int i;
for (i = NUM_FAN_FDS - 1; i >= 0; i--) {
len = read(self->fan_fd[i], self->buf,
sizeof(self->buf));
if (i > 0) {
// Groups 1,2 should get EAGAIN
ASSERT_EQ(len, -1);
ASSERT_EQ(errno, EAGAIN);
} else {
// Group 0 should get events
ASSERT_GT(len, 0);
}
}
self->rem = len;
self->next = (void *) self->buf;
}
meta = self->next;
ASSERT_TRUE(FAN_EVENT_OK(meta, self->rem));
thislen = meta->event_len;
self->rem -= thislen;
self->next += thislen;
*mask = meta->mask;
thislen -= sizeof(*meta);
mnt = ((void *) meta) + meta->event_len - thislen;
ASSERT_EQ(thislen, sizeof(*mnt));
return mnt->mnt_id;
}
static void expect_notify_n(struct __test_metadata *const _metadata,
FIXTURE_DATA(fanotify) *self,
unsigned int n, uint64_t mask[], uint64_t mnts[])
{
unsigned int i;
for (i = 0; i < n; i++)
mnts[i] = expect_notify(_metadata, self, &mask[i]);
}
static uint64_t expect_notify_mask(struct __test_metadata *const _metadata,
FIXTURE_DATA(fanotify) *self,
uint64_t expect_mask)
{
uint64_t mntid, mask;
mntid = expect_notify(_metadata, self, &mask);
ASSERT_EQ(expect_mask, mask);
return mntid;
}
static void expect_notify_mask_n(struct __test_metadata *const _metadata,
FIXTURE_DATA(fanotify) *self,
uint64_t mask, unsigned int n, uint64_t mnts[])
{
unsigned int i;
for (i = 0; i < n; i++)
mnts[i] = expect_notify_mask(_metadata, self, mask);
}
static void verify_mount_ids(struct __test_metadata *const _metadata,
const uint64_t list1[], const uint64_t list2[],
size_t num)
{
unsigned int i, j;
// Check that neither list has any duplicates
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
if (i != j) {
ASSERT_NE(list1[i], list1[j]);
ASSERT_NE(list2[i], list2[j]);
}
}
}
// Check that all list1 memebers can be found in list2. Together with
// the above it means that the list1 and list2 represent the same sets.
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
if (list1[i] == list2[j])
break;
}
ASSERT_NE(j, num);
}
}
static void check_mounted(struct __test_metadata *const _metadata,
const uint64_t mnts[], size_t num)
{
ssize_t ret;
uint64_t *list;
list = malloc((num + 1) * sizeof(list[0]));
ASSERT_NE(list, NULL);
ret = listmount(LSMT_ROOT, 0, 0, list, num + 1, 0);
ASSERT_EQ(ret, num);
verify_mount_ids(_metadata, mnts, list, num);
free(list);
}
static void setup_mount_tree(struct __test_metadata *const _metadata,
int log2_num)
{
int ret, i;
ret = mount("", "/", NULL, MS_SHARED, NULL);
ASSERT_EQ(ret, 0);
for (i = 0; i < log2_num; i++) {
ret = mount("/", "/", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
}
}
TEST_F(fanotify, bind)
{
int ret;
uint64_t mnts[2] = { self->root_id };
ret = mount("/", "/", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
mnts[1] = expect_notify_mask(_metadata, self, FAN_MNT_ATTACH);
ASSERT_NE(mnts[0], mnts[1]);
check_mounted(_metadata, mnts, 2);
// Cleanup
uint64_t detach_id;
ret = umount("/");
ASSERT_EQ(ret, 0);
detach_id = expect_notify_mask(_metadata, self, FAN_MNT_DETACH);
ASSERT_EQ(detach_id, mnts[1]);
check_mounted(_metadata, mnts, 1);
}
TEST_F(fanotify, move)
{
int ret;
uint64_t mnts[2] = { self->root_id };
uint64_t move_id;
ret = mount("/", "/a", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
mnts[1] = expect_notify_mask(_metadata, self, FAN_MNT_ATTACH);
ASSERT_NE(mnts[0], mnts[1]);
check_mounted(_metadata, mnts, 2);
ret = move_mount(AT_FDCWD, "/a", AT_FDCWD, "/b", 0);
ASSERT_EQ(ret, 0);
move_id = expect_notify_mask(_metadata, self, FAN_MNT_ATTACH | FAN_MNT_DETACH);
ASSERT_EQ(move_id, mnts[1]);
// Cleanup
ret = umount("/b");
ASSERT_EQ(ret, 0);
check_mounted(_metadata, mnts, 1);
}
TEST_F(fanotify, propagate)
{
const unsigned int log2_num = 4;
const unsigned int num = (1 << log2_num);
uint64_t mnts[num];
setup_mount_tree(_metadata, log2_num);
expect_notify_mask_n(_metadata, self, FAN_MNT_ATTACH, num - 1, mnts + 1);
mnts[0] = self->root_id;
check_mounted(_metadata, mnts, num);
// Cleanup
int ret;
uint64_t mnts2[num];
ret = umount2("/", MNT_DETACH);
ASSERT_EQ(ret, 0);
ret = mount("", "/", NULL, MS_PRIVATE, NULL);
ASSERT_EQ(ret, 0);
mnts2[0] = self->root_id;
expect_notify_mask_n(_metadata, self, FAN_MNT_DETACH, num - 1, mnts2 + 1);
verify_mount_ids(_metadata, mnts, mnts2, num);
check_mounted(_metadata, mnts, 1);
}
TEST_F(fanotify, fsmount)
{
int ret, fs, mnt;
uint64_t mnts[2] = { self->root_id };
fs = fsopen("tmpfs", 0);
ASSERT_GE(fs, 0);
ret = fsconfig(fs, FSCONFIG_CMD_CREATE, 0, 0, 0);
ASSERT_EQ(ret, 0);
mnt = fsmount(fs, 0, 0);
ASSERT_GE(mnt, 0);
close(fs);
ret = move_mount(mnt, "", AT_FDCWD, "/a", MOVE_MOUNT_F_EMPTY_PATH);
ASSERT_EQ(ret, 0);
close(mnt);
mnts[1] = expect_notify_mask(_metadata, self, FAN_MNT_ATTACH);
ASSERT_NE(mnts[0], mnts[1]);
check_mounted(_metadata, mnts, 2);
// Cleanup
uint64_t detach_id;
ret = umount("/a");
ASSERT_EQ(ret, 0);
detach_id = expect_notify_mask(_metadata, self, FAN_MNT_DETACH);
ASSERT_EQ(detach_id, mnts[1]);
check_mounted(_metadata, mnts, 1);
}
TEST_F(fanotify, reparent)
{
uint64_t mnts[6] = { self->root_id };
uint64_t dmnts[3];
uint64_t masks[3];
unsigned int i;
int ret;
// Create setup with a[1] -> b[2] propagation
ret = mount("/", "/a", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
ret = mount("", "/a", NULL, MS_SHARED, NULL);
ASSERT_EQ(ret, 0);
ret = mount("/a", "/b", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
ret = mount("", "/b", NULL, MS_SLAVE, NULL);
ASSERT_EQ(ret, 0);
expect_notify_mask_n(_metadata, self, FAN_MNT_ATTACH, 2, mnts + 1);
check_mounted(_metadata, mnts, 3);
// Mount on a[3], which is propagated to b[4]
ret = mount("/", "/a", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
expect_notify_mask_n(_metadata, self, FAN_MNT_ATTACH, 2, mnts + 3);
check_mounted(_metadata, mnts, 5);
// Mount on b[5], not propagated
ret = mount("/", "/b", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
mnts[5] = expect_notify_mask(_metadata, self, FAN_MNT_ATTACH);
check_mounted(_metadata, mnts, 6);
// Umount a[3], which is propagated to b[4], but not b[5]
// This will result in b[5] "falling" on b[2]
ret = umount("/a");
ASSERT_EQ(ret, 0);
expect_notify_n(_metadata, self, 3, masks, dmnts);
verify_mount_ids(_metadata, mnts + 3, dmnts, 3);
for (i = 0; i < 3; i++) {
if (dmnts[i] == mnts[5]) {
ASSERT_EQ(masks[i], FAN_MNT_ATTACH | FAN_MNT_DETACH);
} else {
ASSERT_EQ(masks[i], FAN_MNT_DETACH);
}
}
mnts[3] = mnts[5];
check_mounted(_metadata, mnts, 4);
// Cleanup
ret = umount("/b");
ASSERT_EQ(ret, 0);
ret = umount("/a");
ASSERT_EQ(ret, 0);
ret = umount("/b");
ASSERT_EQ(ret, 0);
expect_notify_mask_n(_metadata, self, FAN_MNT_DETACH, 3, dmnts);
verify_mount_ids(_metadata, mnts + 1, dmnts, 3);
check_mounted(_metadata, mnts, 1);
}
TEST_F(fanotify, rmdir)
{
uint64_t mnts[3] = { self->root_id };
int ret;
ret = mount("/", "/a", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
ret = mount("/", "/a/b", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
expect_notify_mask_n(_metadata, self, FAN_MNT_ATTACH, 2, mnts + 1);
check_mounted(_metadata, mnts, 3);
ret = chdir("/a");
ASSERT_EQ(ret, 0);
ret = fork();
ASSERT_GE(ret, 0);
if (ret == 0) {
chdir("/");
unshare(CLONE_NEWNS);
mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
umount2("/a", MNT_DETACH);
// This triggers a detach in the other namespace
rmdir("/a");
exit(0);
}
wait(NULL);
expect_notify_mask_n(_metadata, self, FAN_MNT_DETACH, 2, mnts + 1);
check_mounted(_metadata, mnts, 1);
// Cleanup
ret = chdir("/");
ASSERT_EQ(ret, 0);
}
TEST_F(fanotify, pivot_root)
{
uint64_t mnts[3] = { self->root_id };
uint64_t mnts2[3];
int ret;
ret = mount("tmpfs", "/a", "tmpfs", 0, NULL);
ASSERT_EQ(ret, 0);
mnts[2] = expect_notify_mask(_metadata, self, FAN_MNT_ATTACH);
ret = mkdir("/a/new", 0700);
ASSERT_EQ(ret, 0);
ret = mkdir("/a/old", 0700);
ASSERT_EQ(ret, 0);
ret = mount("/a", "/a/new", NULL, MS_BIND, NULL);
ASSERT_EQ(ret, 0);
mnts[1] = expect_notify_mask(_metadata, self, FAN_MNT_ATTACH);
check_mounted(_metadata, mnts, 3);
ret = syscall(SYS_pivot_root, "/a/new", "/a/new/old");
ASSERT_EQ(ret, 0);
expect_notify_mask_n(_metadata, self, FAN_MNT_ATTACH | FAN_MNT_DETACH, 2, mnts2);
verify_mount_ids(_metadata, mnts, mnts2, 2);
check_mounted(_metadata, mnts, 3);
// Cleanup
ret = syscall(SYS_pivot_root, "/old", "/old/a/new");
ASSERT_EQ(ret, 0);
ret = umount("/a/new");
ASSERT_EQ(ret, 0);
ret = umount("/a");
ASSERT_EQ(ret, 0);
check_mounted(_metadata, mnts, 1);
}
TEST_HARNESS_MAIN
|