• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2009
4  * Copyright (c) Serge Hallyn <serue@us.ibm.com>
5  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Test mqueuefs manipulation from child/parent namespaces.
12  *
13  * [Algorithm]
14  *
15  * - parent creates mqueue folder in <tmpdir>
16  * - child mq_open() /MQ1 mqueue
17  * - child mounts mqueue there
18  * - parent checks for <tmpdir>/mqueue/MQ1 existence
19  * - child exits
20  * - parent checks for <tmpdir>/mqueue/MQ1 existence
21  * - parent tries 'touch <tmpdir>/mqueue/MQ2' -> should fail
22  * - parent umount mqueuefs
23  */
24 
25 #include <sys/wait.h>
26 #include "tst_test.h"
27 #include "lapi/sched.h"
28 #include "tst_safe_posix_ipc.h"
29 #include "tst_safe_stdio.h"
30 #include "tst_safe_macros.h"
31 
32 #define CHECK_MQ_OPEN_RET(x) ((x) >= 0 || ((x) == -1 && errno != EMFILE))
33 
34 #define DEVDIR "ltp_mqueue"
35 #define MQNAME1 "/MQ1"
36 #define MQNAME2 "/MQ2"
37 #define MQUEUE1 DEVDIR MQNAME1
38 #define MQUEUE2 DEVDIR MQNAME2
39 
40 static char *str_op;
41 
check_mqueue(void)42 static void check_mqueue(void)
43 {
44 	mqd_t mqd;
45 
46 	tst_res(TINFO, "Creating %s mqueue from within child process", MQNAME1);
47 
48 	mqd = TST_RETRY_FUNC(
49 		mq_open(MQNAME1, O_RDWR | O_CREAT | O_EXCL, 0755, NULL),
50 		CHECK_MQ_OPEN_RET);
51 	if (mqd == -1)
52 		tst_brk(TBROK | TERRNO, "mq_open failed");
53 
54 	SAFE_MQ_CLOSE(mqd);
55 
56 	tst_res(TINFO, "Mount %s from within child process", DEVDIR);
57 
58 	SAFE_MOUNT("mqueue", DEVDIR, "mqueue", 0, NULL);
59 
60 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
61 }
62 
run(void)63 static void run(void)
64 {
65 	const struct tst_clone_args clone_args = {
66 		.flags = CLONE_NEWIPC,
67 		.exit_signal = SIGCHLD
68 	};
69 
70 	if (str_op && !strcmp(str_op, "clone")) {
71 		tst_res(TINFO, "Spawning isolated process");
72 
73 		if (!SAFE_CLONE(&clone_args)) {
74 			check_mqueue();
75 			return;
76 		}
77 	} else if (str_op && !strcmp(str_op, "unshare")) {
78 		tst_res(TINFO, "Spawning unshared process");
79 
80 		if (!SAFE_FORK()) {
81 			SAFE_UNSHARE(CLONE_NEWIPC);
82 			check_mqueue();
83 			return;
84 		}
85 	}
86 
87 	TST_CHECKPOINT_WAIT(0);
88 
89 	if (access(MQUEUE1, F_OK))
90 		tst_res(TFAIL, MQUEUE1 " can't be accessed from parent");
91 	else
92 		tst_res(TPASS, MQUEUE1 " can be accessed from parent");
93 
94 	TST_CHECKPOINT_WAKE(0);
95 
96 	tst_res(TINFO, "Waiting child to exit");
97 
98 	tst_reap_children();
99 
100 	if (access(MQUEUE1, F_OK))
101 		tst_res(TFAIL, MQUEUE1 " can't be accessed from parent");
102 	else
103 		tst_res(TPASS, MQUEUE1 " can be accessed from parent");
104 
105 	tst_res(TINFO, "Try to create %s from parent", MQUEUE2);
106 
107 	TST_EXP_FAIL(creat(MQUEUE2, 0755), EACCES);
108 
109 	SAFE_UMOUNT(DEVDIR);
110 }
111 
setup(void)112 static void setup(void)
113 {
114 	if (!str_op || (strcmp(str_op, "clone") && strcmp(str_op, "unshare")))
115 		tst_brk(TCONF, "Please specify clone|unshare child isolation");
116 
117 	SAFE_MKDIR(DEVDIR, 0755);
118 }
119 
cleanup(void)120 static void cleanup(void)
121 {
122 	if (!access(MQUEUE1, F_OK))
123 		SAFE_MQ_UNLINK(MQNAME1);
124 
125 	if (!access(MQUEUE2, F_OK))
126 		SAFE_MQ_UNLINK(MQNAME2);
127 
128 	if (tst_is_mounted(DEVDIR))
129 		SAFE_UMOUNT(DEVDIR);
130 }
131 
132 static struct tst_test test = {
133 	.test_all = run,
134 	.setup = setup,
135 	.cleanup = cleanup,
136 	.needs_root = 1,
137 	.forks_child = 1,
138 	.needs_checkpoints = 1,
139 	.options = (struct tst_option[]) {
140 		{ "m:", &str_op, "Child process isolation <clone|unshare>" },
141 		{},
142 	},
143 	.needs_kconfigs = (const char *[]) {
144 		"CONFIG_USER_NS",
145 		NULL
146 	},
147 };
148