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 private mount: private mount does not forward or receive
11 * propagation.
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 private
20 * - Clones a new child process with CLONE_NEWNS flag
21 * - There are two test cases (where X is parent namespace and Y child
22 * namespace):
23 * 1. First test case
24 * .. X: bind mounts DIR_B to DIR_A
25 * .. Y: must see DIR_A/"A" and must not see DIR_A/"B"
26 * .. X: umounts DIR_A
27 * 2. Second test case
28 * .. Y: bind mounts DIR_B to DIR_A
29 * .. X: must see DIR_A/"A" and must not see DIR_A/"B"
30 * .. Y: umounts DIRA
31 */
32
33 #include <sys/wait.h>
34 #include <sys/mount.h>
35 #include "mountns.h"
36 #include "tst_test.h"
37 #include "lapi/sched.h"
38
child_func(void)39 static void child_func(void)
40 {
41 TST_CHECKPOINT_WAIT(0);
42
43 if ((access(DIRA "/A", F_OK) != 0) || (access(DIRA "/B", F_OK) == 0))
44 tst_res(TFAIL, "private mount in parent failed");
45 else
46 tst_res(TPASS, "private mount in parent passed");
47
48 TST_CHECKPOINT_WAKE_AND_WAIT(0);
49
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
71 SAFE_MOUNT("none", DIRA, "none", MS_PRIVATE, NULL);
72
73 if (!SAFE_CLONE(&args)) {
74 child_func();
75 return;
76 }
77
78 SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
79
80 TST_CHECKPOINT_WAKE_AND_WAIT(0);
81
82 SAFE_UMOUNT(DIRA);
83
84 TST_CHECKPOINT_WAKE_AND_WAIT(0);
85
86 if ((access(DIRA "/A", F_OK) != 0) || (access(DIRA "/B", F_OK) == 0))
87 tst_res(TFAIL, "private mount in child failed");
88 else
89 tst_res(TPASS, "private mount in child passed");
90
91 TST_CHECKPOINT_WAKE(0);
92
93 SAFE_WAIT(NULL);
94
95 SAFE_UMOUNT(DIRA);
96 }
97
setup(void)98 static void setup(void)
99 {
100 create_folders();
101 }
102
cleanup(void)103 static void cleanup(void)
104 {
105 umount_folders();
106 }
107
108 static struct tst_test test = {
109 .setup = setup,
110 .cleanup = cleanup,
111 .test_all = run,
112 .needs_root = 1,
113 .forks_child = 1,
114 .needs_checkpoints = 1,
115 .needs_kconfigs = (const char *[]) {
116 "CONFIG_USER_NS",
117 NULL,
118 },
119 };
120