xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/sysvipc/semtest_2ns.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 SysV IPC semaphore usage between namespaces and processes.
12  *
13  * [Algorithm]
14  *
15  * Create 2 'containers'
16  * In container1 create semaphore with a specific key and lock it
17  * In container2 try to access the semaphore created in container1 and try to
18  * unlock it.
19  *
20  * If mode = None, test will PASS when semaphore created process1 can be read
21  * and unlocked from process2.
22  * If mode = Clone, test will PASS when semaphore created in container1 can't
23  * be accessed from container2.
24  * If mode = Unshare, test will PASS when semaphore created in container2 can't
25  * be accessed from container2.
26  */
27 
28 #define _GNU_SOURCE
29 
30 #include <sys/wait.h>
31 #include <sys/msg.h>
32 #include <sys/types.h>
33 #include <sys/sem.h>
34 #include "tst_safe_sysv_ipc.h"
35 #include "tst_test.h"
36 #include "common.h"
37 
38 #define MY_KEY 124326L
39 
40 static char *str_op;
41 static int use_clone;
42 
check_sem1(void)43 static void check_sem1(void)
44 {
45 	int id;
46 	struct sembuf sm = {
47 		.sem_num = 0,
48 		.sem_op = 1,
49 		.sem_flg = SEM_UNDO,
50 	};
51 
52 	id = SAFE_SEMGET(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666);
53 
54 	tst_res(TINFO, "%s: created key in child1", str_op);
55 
56 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
57 
58 	tst_res(TINFO, "Lock semaphore in container1");
59 
60 	SAFE_SEMOP(id, &sm, 1);
61 
62 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
63 
64 	SAFE_SEMCTL(id, IPC_RMID, 0);
65 }
66 
check_sem2(void)67 static void check_sem2(void)
68 {
69 	int id;
70 	struct sembuf sm = {
71 		.sem_num = 0,
72 		.sem_op = -1,
73 		.sem_flg = IPC_NOWAIT | SEM_UNDO,
74 	};
75 
76 	TST_CHECKPOINT_WAIT(0);
77 
78 	tst_res(TINFO, "%s: reading key in child2", str_op);
79 
80 	id = semget(MY_KEY, 1, 0);
81 	if (id >= 0) {
82 		if (use_clone == T_NONE)
83 			tst_res(TPASS, "Plain cloned process able to access the semaphore created");
84 		else
85 			tst_res(TFAIL, "%s: In namespace2 found semaphore created in namespace1", str_op);
86 	} else {
87 		if (use_clone == T_NONE)
88 			tst_res(TFAIL, "Plain cloned process didn't find semaphore");
89 		else
90 			tst_res(TPASS, "%s: In namespace2 unable to access semaphore created in namespace1", str_op);
91 	}
92 
93 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
94 
95 	if (id >= 0) {
96 		tst_res(TINFO, "Trying to unlock semaphore in container2");
97 		TEST(semop(id, &sm, 1));
98 
99 		if (TST_RET >= 0) {
100 			if (use_clone == T_NONE)
101 				tst_res(TPASS, "Plain cloned process able to unlock semaphore");
102 			else
103 				tst_res(TFAIL, "%s: In namespace2 able to unlock the semaphore created in an namespace1", str_op);
104 		} else {
105 			if (use_clone == T_NONE)
106 				tst_res(TFAIL, "Plain cloned process unable to unlock semaphore");
107 			else
108 				tst_res(TPASS, "%s: In namespace2 unable to unlock the semaphore created in an namespace1", str_op);
109 		}
110 	}
111 
112 	TST_CHECKPOINT_WAKE(0);
113 }
114 
run(void)115 static void run(void)
116 {
117 	clone_unshare_test(use_clone, CLONE_NEWIPC, check_sem1);
118 	clone_unshare_test(use_clone, CLONE_NEWIPC, check_sem2);
119 }
120 
setup(void)121 static void setup(void)
122 {
123 	use_clone = get_clone_unshare_enum(str_op);
124 }
125 
cleanup(void)126 static void cleanup(void)
127 {
128 	int id;
129 
130 	id = semget(MY_KEY, 1, 0);
131 	if (id >= 0) {
132 		tst_res(TINFO, "Destroy semaphore");
133 		SAFE_SEMCTL(id, IPC_RMID, 0);
134 	}
135 }
136 
137 static struct tst_test test = {
138 	.test_all = run,
139 	.setup = setup,
140 	.cleanup = cleanup,
141 	.needs_root = 1,
142 	.forks_child = 1,
143 	.needs_checkpoints = 1,
144 	.options = (struct tst_option[]) {
145 		{ "m:", &str_op, "Test execution mode <clone|unshare|none>" },
146 		{},
147 	},
148 };
149