1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2009
4 * Veerendra C <[email protected]>
5 * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Test if SysV IPC shared memory is properly used between two namespaces.
12 *
13 * [Algorithm]
14 *
15 * Create two 'containers'.
16 * In container1, create Shared Memory segment with a specific key.
17 * In container2, try to access the MQ created in container1.
18 *
19 * Test is PASS if flag = none and the shmem seg is accessible in container2.
20 * If flag = unshare/clone and the shmem seg is not accessible in container2.
21 * If shmem seg is not accessible in container2, creates new shmem with same
22 * key to double check isloation in IPCNS.
23 *
24 * Test is FAIL if flag = none and the shmem seg is not accessible, if
25 * flag = unshare/clone and shmem seg is accessible in container2 or if the new
26 * shmem seg creation Fails.
27 */
28
29 #define _GNU_SOURCE
30
31 #include <sys/wait.h>
32 #include <sys/msg.h>
33 #include <sys/types.h>
34 #include "tst_safe_sysv_ipc.h"
35 #include "tst_test.h"
36 #include "common.h"
37
38 #define TESTKEY 124426L
39
40 static char *str_op;
41 static int use_clone;
42
check_shmem1(void)43 static void check_shmem1(void)
44 {
45 int id;
46
47 id = SAFE_SHMGET(TESTKEY, 100, IPC_CREAT);
48
49 tst_res(TINFO, "container1: able to create shared mem segment");
50
51 TST_CHECKPOINT_WAKE_AND_WAIT(0);
52
53 SAFE_SHMCTL(id, IPC_RMID, NULL);
54 }
55
check_shmem2(void)56 static void check_shmem2(void)
57 {
58 TST_CHECKPOINT_WAIT(0);
59
60 TEST(shmget(TESTKEY, 100, 0));
61
62 if (TST_RET < 0) {
63 if (use_clone == T_NONE)
64 tst_res(TFAIL, "Plain cloned process didn't find shmem segment");
65 else
66 tst_res(TPASS, "%s: in namespace2 unable to access the shmem seg created in namespace1", str_op);
67 } else {
68 if (use_clone == T_NONE)
69 tst_res(TPASS, "Plain cloned process able to access shmem segment created");
70 else
71 tst_res(TFAIL, "%s: in namespace2 found the shmem segment created in namespace1", str_op);
72 }
73
74 TST_CHECKPOINT_WAKE(0);
75 }
76
run(void)77 static void run(void)
78 {
79 clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem1);
80 clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem2);
81 }
82
setup(void)83 static void setup(void)
84 {
85 use_clone = get_clone_unshare_enum(str_op);
86 }
87
88 static struct tst_test test = {
89 .test_all = run,
90 .setup = setup,
91 .forks_child = 1,
92 .needs_root = 1,
93 .needs_checkpoints = 1,
94 .options = (struct tst_option[]) {
95 { "m:", &str_op, "Test execution mode <clone|unshare|none>" },
96 {},
97 },
98 };
99