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