xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/mountns/mountns04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 an unbindable mount: unbindable mount is an unbindable
11  * private mount.
12  *
13  * - Creates directories DIRA, DIRB and files DIRA/"A", DIRB/"B"
14  * - Unshares mount namespace and makes it private (so mounts/umounts have no
15  *   effect on a real system)
16  * - Bind mounts directory DIRA to DIRA
17  * - Makes directory DIRA unbindable
18  * - Check if bind mount unbindable DIRA to DIRB fails as expected
19  */
20 
21 #include <sys/wait.h>
22 #include <sys/mount.h>
23 #include "mountns.h"
24 #include "tst_test.h"
25 #include "lapi/sched.h"
26 
run(void)27 static void run(void)
28 {
29 	SAFE_UNSHARE(CLONE_NEWNS);
30 
31 	/* makes sure mounts/umounts have no effect on a real system */
32 	SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
33 
34 	SAFE_MOUNT(DIRA, DIRA, "none", MS_BIND, NULL);
35 
36 	SAFE_MOUNT("none", DIRA, "none", MS_UNBINDABLE, NULL);
37 
38 	if (mount(DIRA, DIRB, "none", MS_BIND, NULL) == -1) {
39 		tst_res(TPASS, "unbindable mount passed");
40 	} else {
41 		SAFE_UMOUNT(DIRB);
42 		tst_res(TFAIL, "unbindable mount faled");
43 	}
44 
45 	SAFE_UMOUNT(DIRA);
46 }
47 
setup(void)48 static void setup(void)
49 {
50 	create_folders();
51 }
52 
cleanup(void)53 static void cleanup(void)
54 {
55 	umount_folders();
56 }
57 
58 static struct tst_test test = {
59 	.setup = setup,
60 	.cleanup = cleanup,
61 	.test_all = run,
62 	.needs_root = 1,
63 	.needs_tmpdir = 1,
64 	.needs_kconfigs = (const char *[]) {
65 		"CONFIG_USER_NS",
66 		NULL,
67 	},
68 };
69