1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2009
4f08c3bdfSopenharmony_ci * Copyright (c) Nadia Derbey, 2009 <Nadia.Derbey@bull.net>
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 * Create a mqueue with the same name in both parent and isolated/forked child,
12f08c3bdfSopenharmony_ci * then check namespace isolation.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci#include "lapi/sched.h"
17f08c3bdfSopenharmony_ci#include "tst_safe_posix_ipc.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#define MQNAME "/MQ1"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic mqd_t mqd;
22f08c3bdfSopenharmony_cistatic char *str_op;
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic int create_message_queue(void)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	return mq_open(MQNAME, O_RDWR | O_CREAT | O_EXCL, 0777, NULL);
27f08c3bdfSopenharmony_ci}
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void shared_child(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	mqd_t mqd1 = -1;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	TST_EXP_FAIL(mqd1 = create_message_queue(), EEXIST);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	if (mqd1 != -1) {
36f08c3bdfSopenharmony_ci		SAFE_MQ_CLOSE(mqd1);
37f08c3bdfSopenharmony_ci		SAFE_MQ_UNLINK(MQNAME);
38f08c3bdfSopenharmony_ci	}
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void isolated_child(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	mqd_t mqd1 = -1;
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	TST_EXP_POSITIVE(mqd1 = create_message_queue());
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (mqd1 != -1) {
48f08c3bdfSopenharmony_ci		SAFE_MQ_CLOSE(mqd1);
49f08c3bdfSopenharmony_ci		SAFE_MQ_UNLINK(MQNAME);
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void run(void)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	const struct tst_clone_args clone_args = {
56f08c3bdfSopenharmony_ci		.flags = CLONE_NEWIPC,
57f08c3bdfSopenharmony_ci		.exit_signal = SIGCHLD,
58f08c3bdfSopenharmony_ci	};
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	tst_res(TINFO, "Checking namespaces isolation from parent to child");
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	if (str_op && !strcmp(str_op, "clone")) {
63f08c3bdfSopenharmony_ci		tst_res(TINFO, "Spawning isolated process");
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci		if (!SAFE_CLONE(&clone_args)) {
66f08c3bdfSopenharmony_ci			isolated_child();
67f08c3bdfSopenharmony_ci			return;
68f08c3bdfSopenharmony_ci		}
69f08c3bdfSopenharmony_ci	} else if (str_op && !strcmp(str_op, "unshare")) {
70f08c3bdfSopenharmony_ci		tst_res(TINFO, "Spawning unshared process");
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		if (!SAFE_FORK()) {
73f08c3bdfSopenharmony_ci			SAFE_UNSHARE(CLONE_NEWIPC);
74f08c3bdfSopenharmony_ci			isolated_child();
75f08c3bdfSopenharmony_ci			return;
76f08c3bdfSopenharmony_ci		}
77f08c3bdfSopenharmony_ci	} else {
78f08c3bdfSopenharmony_ci		tst_res(TINFO, "Spawning plain process");
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci		if (!SAFE_FORK()) {
81f08c3bdfSopenharmony_ci			shared_child();
82f08c3bdfSopenharmony_ci			return;
83f08c3bdfSopenharmony_ci		}
84f08c3bdfSopenharmony_ci	}
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic void setup(void)
88f08c3bdfSopenharmony_ci{
89f08c3bdfSopenharmony_ci	mqd = SAFE_MQ_OPEN(MQNAME, O_RDWR | O_CREAT | O_EXCL, 0777, NULL);
90f08c3bdfSopenharmony_ci}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_cistatic void cleanup(void)
93f08c3bdfSopenharmony_ci{
94f08c3bdfSopenharmony_ci	if (mqd != -1) {
95f08c3bdfSopenharmony_ci		SAFE_MQ_CLOSE(mqd);
96f08c3bdfSopenharmony_ci		SAFE_MQ_UNLINK(MQNAME);
97f08c3bdfSopenharmony_ci	}
98f08c3bdfSopenharmony_ci}
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_cistatic struct tst_test test = {
101f08c3bdfSopenharmony_ci	.test_all = run,
102f08c3bdfSopenharmony_ci	.setup = setup,
103f08c3bdfSopenharmony_ci	.cleanup = cleanup,
104f08c3bdfSopenharmony_ci	.needs_root = 1,
105f08c3bdfSopenharmony_ci	.forks_child = 1,
106f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
107f08c3bdfSopenharmony_ci		{ "m:", &str_op, "Child process isolation <clone|unshare>" },
108f08c3bdfSopenharmony_ci		{},
109f08c3bdfSopenharmony_ci	},
110f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
111f08c3bdfSopenharmony_ci		"CONFIG_USER_NS",
112f08c3bdfSopenharmony_ci		NULL
113f08c3bdfSopenharmony_ci	},
114f08c3bdfSopenharmony_ci};
115