1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2014 Red Hat, Inc.
4 * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Tests a shared mount: shared mount can be replicated to as many
11 * mountpoints and all the replicas continue to be exactly same.
12 *
13 * [Algorithm]
14 *
15 * - Creates directories DIR_A, DIR_B and files DIR_A/"A", DIR_B/"B"
16 * - Unshares mount namespace and makes it private (so mounts/umounts have no
17 * effect on a real system)
18 * - Bind mounts directory DIR_A to DIR_A
19 * - Makes directory DIR_A shared
20 * - Clones a new child process with CLONE_NEWNS flag
21 * - There are two test cases (where X is parent namespace and Y child namespace):
22 * 1. First test case
23 * .. X: bind mounts DIR_B to DIR_A
24 * .. Y: must see DIR_A/"B"
25 * .. X: umounts DIR_A
26 * 2. Second test case
27 * .. Y: bind mounts DIR_B to DIR_A
28 * .. X: must see DIR_A/"B"
29 * .. Y: umounts DIR_A
30 */
31
32 #include <sys/wait.h>
33 #include <sys/mount.h>
34 #include "mountns.h"
35 #include "tst_test.h"
36 #include "lapi/sched.h"
37
child_func(void)38 static void child_func(void)
39 {
40 TST_CHECKPOINT_WAIT(0);
41
42 if (access(DIRA "/B", F_OK) == 0)
43 tst_res(TPASS, "shared mount in parent passed");
44 else
45 tst_res(TFAIL, "shared mount in parent failed");
46
47 TST_CHECKPOINT_WAKE_AND_WAIT(0);
48
49 /* bind mounts DIRB to DIRA making contents of DIRB visible in DIRA */
50 SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
51
52 TST_CHECKPOINT_WAKE_AND_WAIT(0);
53
54 SAFE_UMOUNT(DIRA);
55 }
56
run(void)57 static void run(void)
58 {
59 const struct tst_clone_args args = {
60 .flags = CLONE_NEWNS,
61 .exit_signal = SIGCHLD,
62 };
63
64 SAFE_UNSHARE(CLONE_NEWNS);
65
66 /* makes sure parent mounts/umounts have no effect on a real system */
67 SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
68
69 SAFE_MOUNT(DIRA, DIRA, "none", MS_BIND, NULL);
70 SAFE_MOUNT("none", DIRA, "none", MS_SHARED, NULL);
71
72 if (!SAFE_CLONE(&args)) {
73 child_func();
74 return;
75 }
76
77 SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
78
79 TST_CHECKPOINT_WAKE_AND_WAIT(0);
80
81 SAFE_UMOUNT(DIRA);
82
83 TST_CHECKPOINT_WAKE_AND_WAIT(0);
84
85 if (access(DIRA "/B", F_OK) == 0)
86 tst_res(TPASS, "shared mount in child passed");
87 else
88 tst_res(TFAIL, "shared mount in child failed");
89
90 TST_CHECKPOINT_WAKE(0);
91
92 SAFE_WAIT(NULL);
93
94 SAFE_UMOUNT(DIRA);
95 }
96
setup(void)97 static void setup(void)
98 {
99 create_folders();
100 }
101
cleanup(void)102 static void cleanup(void)
103 {
104 umount_folders();
105 }
106
107 static struct tst_test test = {
108 .setup = setup,
109 .cleanup = cleanup,
110 .test_all = run,
111 .needs_root = 1,
112 .forks_child = 1,
113 .needs_checkpoints = 1,
114 .needs_kconfigs = (const char *[]) {
115 "CONFIG_USER_NS",
116 NULL,
117 },
118 };
119