1 /* Copyright (c) 2014 Red Hat, Inc.
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of version 2 the GNU General Public License as
5  * published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  ***********************************************************************
15  * File: mountns04.c
16  *
17  * Tests an unbindable mount: unbindable mount is an unbindable
18  * private mount.
19  * Description:
20  * 1. Creates directories "A", "B" and files "A/A", "B/B"
21  * 2. Unshares mount namespace and makes it private (so mounts/umounts
22  *    have no effect on a real system)
23  * 3. Bind mounts directory "A" to "A"
24  * 4. Makes directory directory "A" unbindable
25  * 5. Tries to bind mount unbindable "A" to "B":
26  *    - if it fails, test passes
27  *    - if it passes, test fails
28  ***********************************************************************/
29 
30 #define _GNU_SOURCE
31 #include <sys/wait.h>
32 #include <sys/mount.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include "mountns_helper.h"
36 #include "test.h"
37 #include "safe_macros.h"
38 
39 char *TCID	= "mountns04";
40 int TST_TOTAL	= 1;
41 
42 #if defined(MS_SHARED) && defined(MS_PRIVATE) \
43     && defined(MS_REC) && defined(MS_UNBINDABLE)
44 
test(void)45 static void test(void)
46 {
47 	/* unshares the mount ns */
48 	if (unshare(CLONE_NEWNS) == -1)
49 		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
50 	/* makes sure mounts/umounts have no effect on a real system */
51 	SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
52 
53 	/* bind mounts DIRA to itself */
54 	SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
55 	/* makes mount DIRA unbindable */
56 	SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_UNBINDABLE, NULL);
57 
58 	/* tries to bind mount unbindable DIRA to DIRB which should fail */
59 	if (mount(DIRA, DIRB, "none", MS_BIND, NULL) == -1) {
60 		tst_resm(TPASS, "unbindable mount passed");
61 	} else {
62 		SAFE_UMOUNT(cleanup, DIRB);
63 		tst_resm(TFAIL, "unbindable mount faled");
64 	}
65 
66 	SAFE_UMOUNT(cleanup, DIRA);
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 	int lc;
72 
73 	tst_parse_opts(argc, argv, NULL, NULL);
74 
75 	setup();
76 
77 	for (lc = 0; TEST_LOOPING(lc); lc++)
78 		test();
79 
80 	cleanup();
81 	tst_exit();
82 }
83 
84 #else
main(void)85 int main(void)
86 {
87 	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
88 }
89 #endif
90