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
|
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (c) 2026 Christian Brauner <brauner@kernel.org>
#define _GNU_SOURCE
#include <errno.h>
#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 "../wrappers.h"
#include "../utils.h"
#include "../statmount/statmount.h"
#include "../../kselftest_harness.h"
#include <linux/stat.h>
#ifndef MOVE_MOUNT_BENEATH
#define MOVE_MOUNT_BENEATH 0x00000200
#endif
static uint64_t get_unique_mnt_id_fd(int fd)
{
struct statx sx;
int ret;
ret = statx(fd, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &sx);
if (ret)
return 0;
if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE))
return 0;
return sx.stx_mnt_id;
}
/*
* Create a locked overmount stack at /mnt_dir for testing MNT_LOCKED
* transfer on non-rootfs mounts.
*
* Mounts tmpfs A at /mnt_dir, overmounts with tmpfs B, then enters a
* new user+mount namespace where both become locked. Returns the exit
* code to use on failure, or 0 on success.
*/
static int setup_locked_overmount(void)
{
/* Isolate so mounts don't leak. */
if (unshare(CLONE_NEWNS))
return 1;
if (mount("", "/", NULL, MS_REC | MS_PRIVATE, NULL))
return 2;
/*
* Create mounts while still in the initial user namespace so
* they become locked after the subsequent user namespace
* unshare.
*/
rmdir("/mnt_dir");
if (mkdir("/mnt_dir", 0755))
return 3;
/* Mount tmpfs A */
if (mount("tmpfs", "/mnt_dir", "tmpfs", 0, NULL))
return 4;
/* Overmount with tmpfs B */
if (mount("tmpfs", "/mnt_dir", "tmpfs", 0, NULL))
return 5;
/*
* Create user+mount namespace. Mounts A and B become locked
* because they might be covering something that is not supposed
* to be revealed.
*/
if (setup_userns())
return 6;
/* Sanity check: B must be locked */
if (!umount2("/mnt_dir", MNT_DETACH) || errno != EINVAL)
return 7;
return 0;
}
/*
* Create a detached tmpfs mount and return its fd, or -1 on failure.
*/
static int create_detached_tmpfs(void)
{
int fs_fd, mnt_fd;
fs_fd = sys_fsopen("tmpfs", FSOPEN_CLOEXEC);
if (fs_fd < 0)
return -1;
if (sys_fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)) {
close(fs_fd);
return -1;
}
mnt_fd = sys_fsmount(fs_fd, FSMOUNT_CLOEXEC, 0);
close(fs_fd);
return mnt_fd;
}
FIXTURE(move_mount) {
uint64_t orig_root_id;
};
FIXTURE_SETUP(move_mount)
{
ASSERT_EQ(unshare(CLONE_NEWNS), 0);
ASSERT_EQ(mount("", "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
self->orig_root_id = get_unique_mnt_id("/");
ASSERT_NE(self->orig_root_id, 0);
}
FIXTURE_TEARDOWN(move_mount)
{
}
/*
* Test successful MOVE_MOUNT_BENEATH on the rootfs.
* Mount a clone beneath /, fchdir to the clone, chroot to switch root,
* then detach the old root.
*/
TEST_F(move_mount, beneath_rootfs_success)
{
int fd_tree, ret;
uint64_t clone_id, root_id;
fd_tree = sys_open_tree(AT_FDCWD, "/",
OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
ASSERT_GE(fd_tree, 0);
clone_id = get_unique_mnt_id_fd(fd_tree);
ASSERT_NE(clone_id, 0);
ASSERT_NE(clone_id, self->orig_root_id);
ASSERT_EQ(fchdir(fd_tree), 0);
ret = sys_move_mount(fd_tree, "", AT_FDCWD, "/",
MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, 0);
close(fd_tree);
/* Switch root to the clone */
ASSERT_EQ(chroot("."), 0);
/* Verify "/" is now the clone */
root_id = get_unique_mnt_id("/");
ASSERT_NE(root_id, 0);
ASSERT_EQ(root_id, clone_id);
/* Detach old root */
ASSERT_EQ(umount2(".", MNT_DETACH), 0);
}
/*
* Test that after MOVE_MOUNT_BENEATH on the rootfs the old root is
* stacked on top of the clone. Verify via statmount that the old
* root's parent is the clone.
*/
TEST_F(move_mount, beneath_rootfs_old_root_stacked)
{
int fd_tree, ret;
uint64_t clone_id;
struct statmount sm;
fd_tree = sys_open_tree(AT_FDCWD, "/",
OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
ASSERT_GE(fd_tree, 0);
clone_id = get_unique_mnt_id_fd(fd_tree);
ASSERT_NE(clone_id, 0);
ASSERT_NE(clone_id, self->orig_root_id);
ASSERT_EQ(fchdir(fd_tree), 0);
ret = sys_move_mount(fd_tree, "", AT_FDCWD, "/",
MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, 0);
close(fd_tree);
ASSERT_EQ(chroot("."), 0);
/* Old root's parent should now be the clone */
ASSERT_EQ(statmount(self->orig_root_id, 0, 0,
STATMOUNT_MNT_BASIC, &sm, sizeof(sm), 0), 0);
ASSERT_EQ(sm.mnt_parent_id, clone_id);
ASSERT_EQ(umount2(".", MNT_DETACH), 0);
}
/*
* Test that MOVE_MOUNT_BENEATH on rootfs fails when chroot'd into a
* subdirectory of the same mount. The caller's fs->root.dentry doesn't
* match mnt->mnt_root so the kernel rejects it.
*/
TEST_F(move_mount, beneath_rootfs_in_chroot_fail)
{
int fd_tree, ret;
uint64_t chroot_id, clone_id;
rmdir("/chroot_dir");
ASSERT_EQ(mkdir("/chroot_dir", 0755), 0);
chroot_id = get_unique_mnt_id("/chroot_dir");
ASSERT_NE(chroot_id, 0);
ASSERT_EQ(self->orig_root_id, chroot_id);
ASSERT_EQ(chdir("/chroot_dir"), 0);
ASSERT_EQ(chroot("."), 0);
fd_tree = sys_open_tree(AT_FDCWD, "/",
OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
ASSERT_GE(fd_tree, 0);
clone_id = get_unique_mnt_id_fd(fd_tree);
ASSERT_NE(clone_id, 0);
ASSERT_NE(clone_id, chroot_id);
ASSERT_EQ(fchdir(fd_tree), 0);
/*
* Should fail: fs->root.dentry (/chroot_dir) doesn't match
* the mount's mnt_root (/).
*/
ret = sys_move_mount(fd_tree, "", AT_FDCWD, "/",
MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, EINVAL);
close(fd_tree);
}
/*
* Test that MOVE_MOUNT_BENEATH on rootfs succeeds when chroot'd into a
* separate tmpfs mount. The caller's root dentry matches the mount's
* mnt_root since it's a dedicated mount.
*/
TEST_F(move_mount, beneath_rootfs_in_chroot_success)
{
int fd_tree, ret;
uint64_t chroot_id, clone_id, root_id;
struct statmount sm;
rmdir("/chroot_dir");
ASSERT_EQ(mkdir("/chroot_dir", 0755), 0);
ASSERT_EQ(mount("tmpfs", "/chroot_dir", "tmpfs", 0, NULL), 0);
chroot_id = get_unique_mnt_id("/chroot_dir");
ASSERT_NE(chroot_id, 0);
ASSERT_EQ(chdir("/chroot_dir"), 0);
ASSERT_EQ(chroot("."), 0);
ASSERT_EQ(get_unique_mnt_id("/"), chroot_id);
fd_tree = sys_open_tree(AT_FDCWD, "/",
OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
ASSERT_GE(fd_tree, 0);
clone_id = get_unique_mnt_id_fd(fd_tree);
ASSERT_NE(clone_id, 0);
ASSERT_NE(clone_id, chroot_id);
ASSERT_EQ(fchdir(fd_tree), 0);
ret = sys_move_mount(fd_tree, "", AT_FDCWD, "/",
MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, 0);
close(fd_tree);
ASSERT_EQ(chroot("."), 0);
root_id = get_unique_mnt_id("/");
ASSERT_NE(root_id, 0);
ASSERT_EQ(root_id, clone_id);
ASSERT_EQ(statmount(chroot_id, 0, 0,
STATMOUNT_MNT_BASIC, &sm, sizeof(sm), 0), 0);
ASSERT_EQ(sm.mnt_parent_id, clone_id);
ASSERT_EQ(umount2(".", MNT_DETACH), 0);
}
/*
* Test MNT_LOCKED transfer when mounting beneath rootfs in a user+mount
* namespace. After mount-beneath the new root gets MNT_LOCKED and the
* old root has MNT_LOCKED cleared so it can be unmounted.
*/
TEST_F(move_mount, beneath_rootfs_locked_transfer)
{
int fd_tree, ret;
uint64_t clone_id, root_id;
ASSERT_EQ(setup_userns(), 0);
ASSERT_EQ(mount("", "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
fd_tree = sys_open_tree(AT_FDCWD, "/",
OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC |
AT_RECURSIVE);
ASSERT_GE(fd_tree, 0);
clone_id = get_unique_mnt_id_fd(fd_tree);
ASSERT_NE(clone_id, 0);
ASSERT_EQ(fchdir(fd_tree), 0);
ret = sys_move_mount(fd_tree, "", AT_FDCWD, "/",
MOVE_MOUNT_F_EMPTY_PATH |
MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, 0);
close(fd_tree);
ASSERT_EQ(chroot("."), 0);
root_id = get_unique_mnt_id("/");
ASSERT_EQ(root_id, clone_id);
/*
* The old root should be unmountable (MNT_LOCKED was
* transferred to the clone). If MNT_LOCKED wasn't
* cleared, this would fail with EINVAL.
*/
ASSERT_EQ(umount2(".", MNT_DETACH), 0);
/* Verify "/" is still the clone after detaching old root */
root_id = get_unique_mnt_id("/");
ASSERT_EQ(root_id, clone_id);
}
/*
* Test containment invariant: after mount-beneath rootfs in a user+mount
* namespace, the new root must be MNT_LOCKED. The lock transfer from the
* old root preserves containment -- the process cannot unmount the new root
* to escape the namespace.
*/
TEST_F(move_mount, beneath_rootfs_locked_containment)
{
int fd_tree, ret;
uint64_t clone_id, root_id;
ASSERT_EQ(setup_userns(), 0);
ASSERT_EQ(mount("", "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
/* Sanity: rootfs must be locked in the new userns */
ASSERT_EQ(umount2("/", MNT_DETACH), -1);
ASSERT_EQ(errno, EINVAL);
fd_tree = sys_open_tree(AT_FDCWD, "/",
OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC |
AT_RECURSIVE);
ASSERT_GE(fd_tree, 0);
clone_id = get_unique_mnt_id_fd(fd_tree);
ASSERT_NE(clone_id, 0);
ASSERT_EQ(fchdir(fd_tree), 0);
ret = sys_move_mount(fd_tree, "", AT_FDCWD, "/",
MOVE_MOUNT_F_EMPTY_PATH |
MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, 0);
close(fd_tree);
ASSERT_EQ(chroot("."), 0);
root_id = get_unique_mnt_id("/");
ASSERT_EQ(root_id, clone_id);
/* Detach old root (MNT_LOCKED was cleared from it) */
ASSERT_EQ(umount2(".", MNT_DETACH), 0);
/* Verify "/" is still the clone after detaching old root */
root_id = get_unique_mnt_id("/");
ASSERT_EQ(root_id, clone_id);
/*
* The new root must be locked (MNT_LOCKED was transferred
* from the old root). Attempting to unmount it must fail
* with EINVAL, preserving the containment invariant.
*/
ASSERT_EQ(umount2("/", MNT_DETACH), -1);
ASSERT_EQ(errno, EINVAL);
}
/*
* Test MNT_LOCKED transfer when mounting beneath a non-rootfs locked mount.
* Mounts created before unshare(CLONE_NEWUSER | CLONE_NEWNS) become locked
* in the new namespace. Mount-beneath transfers the lock from the displaced
* mount to the new mount, so the displaced mount can be unmounted.
*/
TEST_F(move_mount, beneath_non_rootfs_locked_transfer)
{
int mnt_fd, ret;
uint64_t mnt_new_id, mnt_visible_id;
ASSERT_EQ(setup_locked_overmount(), 0);
mnt_fd = create_detached_tmpfs();
ASSERT_GE(mnt_fd, 0);
mnt_new_id = get_unique_mnt_id_fd(mnt_fd);
ASSERT_NE(mnt_new_id, 0);
/* Move mount beneath B (which is locked) */
ret = sys_move_mount(mnt_fd, "", AT_FDCWD, "/mnt_dir",
MOVE_MOUNT_F_EMPTY_PATH |
MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, 0);
close(mnt_fd);
/*
* B should now be unmountable (MNT_LOCKED was transferred
* to the new mount beneath it). If MNT_LOCKED wasn't
* cleared from B, this would fail with EINVAL.
*/
ASSERT_EQ(umount2("/mnt_dir", MNT_DETACH), 0);
/* Verify the new mount is now visible */
mnt_visible_id = get_unique_mnt_id("/mnt_dir");
ASSERT_EQ(mnt_visible_id, mnt_new_id);
}
/*
* Test MNT_LOCKED containment when mounting beneath a non-rootfs mount
* that was locked during unshare(CLONE_NEWUSER | CLONE_NEWNS).
* Mounts created before unshare become locked in the new namespace.
* Mount-beneath transfers the lock, preserving containment: the new
* mount cannot be unmounted, but the displaced mount can.
*/
TEST_F(move_mount, beneath_non_rootfs_locked_containment)
{
int mnt_fd, ret;
uint64_t mnt_new_id, mnt_visible_id;
ASSERT_EQ(setup_locked_overmount(), 0);
mnt_fd = create_detached_tmpfs();
ASSERT_GE(mnt_fd, 0);
mnt_new_id = get_unique_mnt_id_fd(mnt_fd);
ASSERT_NE(mnt_new_id, 0);
/*
* Move new tmpfs beneath B at /mnt_dir.
* Stack becomes: A -> new -> B
* Lock transfers from B to new.
*/
ret = sys_move_mount(mnt_fd, "", AT_FDCWD, "/mnt_dir",
MOVE_MOUNT_F_EMPTY_PATH |
MOVE_MOUNT_BENEATH);
ASSERT_EQ(ret, 0);
close(mnt_fd);
/*
* B lost MNT_LOCKED -- unmounting it must succeed.
* This reveals the new mount at /mnt_dir.
*/
ASSERT_EQ(umount2("/mnt_dir", MNT_DETACH), 0);
/* Verify the new mount is now visible */
mnt_visible_id = get_unique_mnt_id("/mnt_dir");
ASSERT_EQ(mnt_visible_id, mnt_new_id);
/*
* The new mount gained MNT_LOCKED -- unmounting it must
* fail with EINVAL, preserving the containment invariant.
*/
ASSERT_EQ(umount2("/mnt_dir", MNT_DETACH), -1);
ASSERT_EQ(errno, EINVAL);
}
TEST_HARNESS_MAIN
|