• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Red Hat, Inc.
4  * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
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 
child_func(LTP_ATTRIBUTE_UNUSED void * arg)38 static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
39 {
40 	TST_CHECKPOINT_WAIT(0);
41 
42 	if ((access(DIRA "/A", F_OK) != 0) || (access(DIRA "/B", F_OK) == 0))
43 		tst_res(TFAIL, "private mount in parent failed");
44 	else
45 		tst_res(TPASS, "private mount in parent passed");
46 
47 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
48 
49 	SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
50 
51 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
52 
53 	SAFE_UMOUNT(DIRA);
54 
55 	return 0;
56 }
57 
run(void)58 static void run(void)
59 {
60 	int ret;
61 
62 	SAFE_UNSHARE(CLONE_NEWNS);
63 
64 	/* makes sure parent mounts/umounts have no effect on a real system */
65 	SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
66 
67 	SAFE_MOUNT(DIRA, DIRA, "none", MS_BIND, NULL);
68 
69 	SAFE_MOUNT("none", DIRA, "none", MS_PRIVATE, NULL);
70 
71 	ret = ltp_clone_quick(CLONE_NEWNS | SIGCHLD, child_func, NULL);
72 	if (ret < 0)
73 		tst_brk(TBROK, "clone failed");
74 
75 	SAFE_MOUNT(DIRB, DIRA, "none", MS_BIND, NULL);
76 
77 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
78 
79 	SAFE_UMOUNT(DIRA);
80 
81 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
82 
83 	if ((access(DIRA "/A", F_OK) != 0) || (access(DIRA "/B", F_OK) == 0))
84 		tst_res(TFAIL, "private mount in child failed");
85 	else
86 		tst_res(TPASS, "private mount in child passed");
87 
88 	TST_CHECKPOINT_WAKE(0);
89 
90 	SAFE_WAIT(NULL);
91 
92 	SAFE_UMOUNT(DIRA);
93 }
94 
setup(void)95 static void setup(void)
96 {
97 	check_newns();
98 	create_folders();
99 }
100 
cleanup(void)101 static void cleanup(void)
102 {
103 	umount_folders();
104 }
105 
106 static struct tst_test test = {
107 	.setup = setup,
108 	.cleanup = cleanup,
109 	.test_all = run,
110 	.needs_root = 1,
111 	.needs_checkpoints = 1,
112 };
113