xref: /xnu-12377.81.4/tests/posix_sem.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2025 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions #include <darwintest.h>
29*043036a2SApple OSS Distributions #include <darwintest_utils.h>
30*043036a2SApple OSS Distributions #include <semaphore.h>
31*043036a2SApple OSS Distributions #include <unistd.h>
32*043036a2SApple OSS Distributions #include <libgen.h>
33*043036a2SApple OSS Distributions #include <sys/posix_sem.h>
34*043036a2SApple OSS Distributions #include <sys/code_signing.h>
35*043036a2SApple OSS Distributions #include <mach-o/dyld.h>
36*043036a2SApple OSS Distributions 
37*043036a2SApple OSS Distributions 
38*043036a2SApple OSS Distributions T_GLOBAL_META(
39*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
40*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("bsd"),
41*043036a2SApple OSS Distributions 	T_META_OWNER("m_staveleytaylor"),
42*043036a2SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
43*043036a2SApple OSS Distributions 
44*043036a2SApple OSS Distributions #define NUM_TEST_SEMAPHORES 50
45*043036a2SApple OSS Distributions 
46*043036a2SApple OSS Distributions static char open_test_prefix[PSEMNAMLEN + 1];
47*043036a2SApple OSS Distributions static char open_sem_invalid[PSEMNAMLEN + 1];
48*043036a2SApple OSS Distributions static char open_sem_a[PSEMNAMLEN + 1];
49*043036a2SApple OSS Distributions static char open_sem_b[PSEMNAMLEN + 1];
50*043036a2SApple OSS Distributions 
51*043036a2SApple OSS Distributions static void
cleanup_open()52*043036a2SApple OSS Distributions cleanup_open()
53*043036a2SApple OSS Distributions {
54*043036a2SApple OSS Distributions 	sem_unlink(open_sem_invalid);
55*043036a2SApple OSS Distributions 	sem_unlink(open_sem_a);
56*043036a2SApple OSS Distributions 	sem_unlink(open_sem_b);
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions 	for (int i = 0; i < NUM_TEST_SEMAPHORES; i++) {
59*043036a2SApple OSS Distributions 		char name_buf[PSEMNAMLEN];
60*043036a2SApple OSS Distributions 		snprintf(name_buf, sizeof(name_buf), "%s/many%d", open_test_prefix, i);
61*043036a2SApple OSS Distributions 		sem_unlink(name_buf);
62*043036a2SApple OSS Distributions 	}
63*043036a2SApple OSS Distributions }
64*043036a2SApple OSS Distributions 
65*043036a2SApple OSS Distributions T_DECL(posix_sem_open, "POSIX sem_open",
66*043036a2SApple OSS Distributions     T_META_TAG_VM_PREFERRED)
67*043036a2SApple OSS Distributions {
68*043036a2SApple OSS Distributions 	sem_t *sem;
69*043036a2SApple OSS Distributions 
70*043036a2SApple OSS Distributions 	T_SETUPBEGIN;
71*043036a2SApple OSS Distributions 	srand(time(NULL));
72*043036a2SApple OSS Distributions 	snprintf(open_test_prefix, sizeof(open_test_prefix), "xnutest%d", rand() % 10000);
73*043036a2SApple OSS Distributions 	snprintf(open_sem_invalid, sizeof(open_sem_invalid), "%s/invalid", open_test_prefix);
74*043036a2SApple OSS Distributions 	snprintf(open_sem_a, sizeof(open_sem_a), "%s/a", open_test_prefix);
75*043036a2SApple OSS Distributions 	snprintf(open_sem_b, sizeof(open_sem_b), "%s/b", open_test_prefix);
76*043036a2SApple OSS Distributions 
77*043036a2SApple OSS Distributions 	T_ATEND(cleanup_open);
78*043036a2SApple OSS Distributions 	T_SETUPEND;
79*043036a2SApple OSS Distributions 
80*043036a2SApple OSS Distributions 	sem = sem_open(open_sem_invalid, 0);
81*043036a2SApple OSS Distributions 	T_EXPECT_EQ_PTR(sem, SEM_FAILED, "sem_open without O_CREAT fails");
82*043036a2SApple OSS Distributions 	T_EXPECT_EQ(errno, ENOENT, "sem_open without O_CREAT gives ENOENT");
83*043036a2SApple OSS Distributions 
84*043036a2SApple OSS Distributions 	sem = sem_open(open_sem_a, O_CREAT, 0755, 0);
85*043036a2SApple OSS Distributions 	T_WITH_ERRNO;
86*043036a2SApple OSS Distributions 	T_EXPECT_NE_PTR(sem, SEM_FAILED, "sem_open(O_CREAT) succeeds");
87*043036a2SApple OSS Distributions 
88*043036a2SApple OSS Distributions 	sem = sem_open(open_sem_a, O_CREAT, 0755, 0);
89*043036a2SApple OSS Distributions 	T_WITH_ERRNO;
90*043036a2SApple OSS Distributions 	T_EXPECT_NE_PTR(sem, SEM_FAILED, "sem_open(O_CREAT) on existing succeeds");
91*043036a2SApple OSS Distributions 
92*043036a2SApple OSS Distributions 	sem = sem_open(open_sem_a, O_CREAT | O_EXCL, 0755, 0);
93*043036a2SApple OSS Distributions 	T_EXPECT_EQ_PTR(sem, SEM_FAILED, "sem_open(O_CREAT | O_EXCL) on existing fails");
94*043036a2SApple OSS Distributions 	T_EXPECT_EQ(errno, EEXIST, "sem_open(O_CREAT | O_EXCL) on existing gives EEXIST");
95*043036a2SApple OSS Distributions 
96*043036a2SApple OSS Distributions 	sem = sem_open(open_sem_b, O_CREAT | O_EXCL, 0755, 0);
97*043036a2SApple OSS Distributions 	T_WITH_ERRNO;
98*043036a2SApple OSS Distributions 	T_EXPECT_NE_PTR(sem, SEM_FAILED, "sem_open(O_CREAT | O_EXCL) on non-existing succeeds");
99*043036a2SApple OSS Distributions 
100*043036a2SApple OSS Distributions 	for (int i = 0; i < NUM_TEST_SEMAPHORES; i++) {
101*043036a2SApple OSS Distributions 		char name_buf[PSEMNAMLEN];
102*043036a2SApple OSS Distributions 		snprintf(name_buf, sizeof(name_buf), "%s/many%d", open_test_prefix, i);
103*043036a2SApple OSS Distributions 
104*043036a2SApple OSS Distributions 		int oflag = O_CREAT;
105*043036a2SApple OSS Distributions 		if (rand() % 2 == 0) {
106*043036a2SApple OSS Distributions 			oflag |= O_EXCL;
107*043036a2SApple OSS Distributions 		}
108*043036a2SApple OSS Distributions 
109*043036a2SApple OSS Distributions 		sem = sem_open(name_buf, oflag, 0755, 0);
110*043036a2SApple OSS Distributions 		T_WITH_ERRNO;
111*043036a2SApple OSS Distributions 		T_EXPECT_NE_PTR(sem, SEM_FAILED, "sem_open name=%s oflag=%d succeeds", name_buf, oflag);
112*043036a2SApple OSS Distributions 	}
113*043036a2SApple OSS Distributions 
114*043036a2SApple OSS Distributions 	/* Fisher-Yates shuffle to randomize order in which we unlink semaphores */
115*043036a2SApple OSS Distributions 	int unlink_order[NUM_TEST_SEMAPHORES] = { 0 };
116*043036a2SApple OSS Distributions 	for (int i = 0; i < NUM_TEST_SEMAPHORES; i++) {
117*043036a2SApple OSS Distributions 		unlink_order[i] = i;
118*043036a2SApple OSS Distributions 	}
119*043036a2SApple OSS Distributions 	for (int i = 0; i < NUM_TEST_SEMAPHORES; i++) {
120*043036a2SApple OSS Distributions 		int next_index = rand() % (NUM_TEST_SEMAPHORES - i);
121*043036a2SApple OSS Distributions 
122*043036a2SApple OSS Distributions 		int semaphore = unlink_order[i + next_index];
123*043036a2SApple OSS Distributions 		unlink_order[i + next_index] = unlink_order[i];
124*043036a2SApple OSS Distributions 
125*043036a2SApple OSS Distributions 		char name_buf[PSEMNAMLEN + 1];
126*043036a2SApple OSS Distributions 		snprintf(name_buf, sizeof(name_buf), "%s/many%d", open_test_prefix, semaphore);
127*043036a2SApple OSS Distributions 
128*043036a2SApple OSS Distributions 		T_WITH_ERRNO;
129*043036a2SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(sem_unlink(name_buf), "sem_unlink(%s)", name_buf);
130*043036a2SApple OSS Distributions 	}
131*043036a2SApple OSS Distributions }
132*043036a2SApple OSS Distributions 
133*043036a2SApple OSS Distributions static char namespace_test_sem_name[PSEMNAMLEN + 1];
134*043036a2SApple OSS Distributions 
135*043036a2SApple OSS Distributions static int
find_helper(char * test_path,int team_id)136*043036a2SApple OSS Distributions find_helper(char* test_path, int team_id)
137*043036a2SApple OSS Distributions {
138*043036a2SApple OSS Distributions 	char binpath[MAXPATHLEN];
139*043036a2SApple OSS Distributions 	char* dirpath;
140*043036a2SApple OSS Distributions 	uint32_t size = sizeof(binpath);
141*043036a2SApple OSS Distributions 	int retval;
142*043036a2SApple OSS Distributions 
143*043036a2SApple OSS Distributions 	retval = _NSGetExecutablePath(binpath, &size);
144*043036a2SApple OSS Distributions 	assert(retval == 0);
145*043036a2SApple OSS Distributions 	dirpath = dirname(binpath);
146*043036a2SApple OSS Distributions 
147*043036a2SApple OSS Distributions 	snprintf(test_path, MAXPATHLEN, "%s/posix_sem_namespace_helper_team%d", dirpath, team_id);
148*043036a2SApple OSS Distributions 	if (access(test_path, F_OK) == 0) {
149*043036a2SApple OSS Distributions 		return 0;
150*043036a2SApple OSS Distributions 	} else {
151*043036a2SApple OSS Distributions 		return -1;
152*043036a2SApple OSS Distributions 	}
153*043036a2SApple OSS Distributions }
154*043036a2SApple OSS Distributions 
155*043036a2SApple OSS Distributions static void
do_namespace_op(const char * namespace,const char * op)156*043036a2SApple OSS Distributions do_namespace_op(const char *namespace, const char *op)
157*043036a2SApple OSS Distributions {
158*043036a2SApple OSS Distributions 	int ret, exit_status, signum;
159*043036a2SApple OSS Distributions 
160*043036a2SApple OSS Distributions 	dt_pipe_data_handler_t stdout_handler = ^bool (char *data, __unused size_t data_size, __unused dt_pipe_data_handler_context_t *context) {
161*043036a2SApple OSS Distributions 		T_LOG("%s: %s", (char *)context->user_context, data);
162*043036a2SApple OSS Distributions 		return false;
163*043036a2SApple OSS Distributions 	};
164*043036a2SApple OSS Distributions 	dt_pipe_data_handler_t stderr_handler = ^bool (char *data, __unused size_t data_size, __unused dt_pipe_data_handler_context_t *context) {
165*043036a2SApple OSS Distributions 		T_LOG("%s (stderr): %s", (char *)context->user_context, data);
166*043036a2SApple OSS Distributions 		return false;
167*043036a2SApple OSS Distributions 	};
168*043036a2SApple OSS Distributions 
169*043036a2SApple OSS Distributions 	pid_t pid = dt_launch_tool_pipe((char *[]){ (char *)namespace, namespace_test_sem_name, (char *)op, NULL}, false, NULL, stdout_handler, stderr_handler, BUFFER_PATTERN_LINE, (void *)namespace);
170*043036a2SApple OSS Distributions 
171*043036a2SApple OSS Distributions 	T_QUIET;
172*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pid, "dt_launch_tool_pipe %s (%s) - %s", op, namespace, namespace_test_sem_name);
173*043036a2SApple OSS Distributions 
174*043036a2SApple OSS Distributions 	ret = dt_waitpid(pid, &exit_status, &signum, 60 * 5);
175*043036a2SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 1, "dt_waitpid (exit=%d,signum=%d)", exit_status, signum);
176*043036a2SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
177*043036a2SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
178*043036a2SApple OSS Distributions }
179*043036a2SApple OSS Distributions 
180*043036a2SApple OSS Distributions static void
cleanup_namespace()181*043036a2SApple OSS Distributions cleanup_namespace()
182*043036a2SApple OSS Distributions {
183*043036a2SApple OSS Distributions 	sem_unlink(namespace_test_sem_name);
184*043036a2SApple OSS Distributions }
185*043036a2SApple OSS Distributions 
186*043036a2SApple OSS Distributions /*
187*043036a2SApple OSS Distributions  * Unfortunately this test suffers from two issues that mean we must leave it disabled on BATS:
188*043036a2SApple OSS Distributions  *   1. rdar://75835929 means that XBS strips the team ID from our helper binaries after we've signed them.
189*043036a2SApple OSS Distributions  *   2. BATS infrastructure boots with amfi_get_out_of_my_way=1, which treats signatures as CS_PLATFORM_BINARY and causes the team ID to be ignored.
190*043036a2SApple OSS Distributions  */
191*043036a2SApple OSS Distributions T_DECL(posix_sem_open_team_id_namespace, "POSIX sem_open team ID namespace",
192*043036a2SApple OSS Distributions     T_META_BOOTARGS_SET("amfi_allow_any_signature=1"),
193*043036a2SApple OSS Distributions     T_META_ENABLED(FALSE),
194*043036a2SApple OSS Distributions     T_META_TAG_VM_PREFERRED)
195*043036a2SApple OSS Distributions {
196*043036a2SApple OSS Distributions 	T_SETUPBEGIN;
197*043036a2SApple OSS Distributions 	srand(time(NULL));
198*043036a2SApple OSS Distributions 	snprintf(namespace_test_sem_name, sizeof(namespace_test_sem_name), "xnutest%d/ns", rand() % 10000);
199*043036a2SApple OSS Distributions 
200*043036a2SApple OSS Distributions 	T_ATEND(cleanup_namespace);
201*043036a2SApple OSS Distributions 
202*043036a2SApple OSS Distributions 	char team0_helper[MAXPATHLEN], team1_helper[MAXPATHLEN];
203*043036a2SApple OSS Distributions 	find_helper(team0_helper, 0);
204*043036a2SApple OSS Distributions 	find_helper(team1_helper, 1);
205*043036a2SApple OSS Distributions 	printf("found helpers at '%s' and '%s'\n", team0_helper, team1_helper);
206*043036a2SApple OSS Distributions 
207*043036a2SApple OSS Distributions 	/* Quite difficult to register cleanup handlers for this, so we'll perform cleanup now */
208*043036a2SApple OSS Distributions 	T_LOG("Performing sem_unlink cleanup");
209*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "unlink_force");
210*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "unlink_force");
211*043036a2SApple OSS Distributions 
212*043036a2SApple OSS Distributions 	T_SETUPEND;
213*043036a2SApple OSS Distributions 
214*043036a2SApple OSS Distributions 	/* Check that semaphores created by 1st party applications can be discovered by 3rd party applications. */
215*043036a2SApple OSS Distributions 	T_LOG("Check 3rd party sees 1st party");
216*043036a2SApple OSS Distributions 
217*043036a2SApple OSS Distributions 	sem_t *sem = sem_open(namespace_test_sem_name, O_CREAT | O_EXCL, 0755, 0);
218*043036a2SApple OSS Distributions 	T_EXPECT_NE_PTR(sem, SEM_FAILED, "sem_open(O_CREAT | O_EXCL)");
219*043036a2SApple OSS Distributions 	sem_close(sem);
220*043036a2SApple OSS Distributions 
221*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_access");
222*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sem_unlink(namespace_test_sem_name), "sem_unlink");
223*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_no_access");
224*043036a2SApple OSS Distributions 
225*043036a2SApple OSS Distributions #if TARGET_OS_OSX
226*043036a2SApple OSS Distributions 	T_LOG("macOS only: check 1st party sees 3rd party");
227*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "open_excl");
228*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_access");
229*043036a2SApple OSS Distributions 
230*043036a2SApple OSS Distributions 	sem = sem_open(namespace_test_sem_name, 0);
231*043036a2SApple OSS Distributions 	T_EXPECT_NE_PTR(sem, SEM_FAILED, "sem_open on 3rd party semaphore");
232*043036a2SApple OSS Distributions 	sem_close(sem);
233*043036a2SApple OSS Distributions 
234*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "unlink");
235*043036a2SApple OSS Distributions 
236*043036a2SApple OSS Distributions 	T_LOG("macOS only: check 3rd party sees other 3rd party" );
237*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_no_access");
238*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_no_access");
239*043036a2SApple OSS Distributions 
240*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "open_excl");
241*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_access");
242*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_access");
243*043036a2SApple OSS Distributions 
244*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "unlink");
245*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_no_access");
246*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_no_access");
247*043036a2SApple OSS Distributions #else
248*043036a2SApple OSS Distributions 	/* 1st party applications should not be able to look up semaphores created by 3rd party applications. */
249*043036a2SApple OSS Distributions 	T_LOG("Check 1st party doesn't see 3rd party");
250*043036a2SApple OSS Distributions 
251*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "open_excl");
252*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_access");
253*043036a2SApple OSS Distributions 
254*043036a2SApple OSS Distributions 	sem = sem_open(namespace_test_sem_name, 0);
255*043036a2SApple OSS Distributions 	T_EXPECT_EQ_PTR(sem, SEM_FAILED, "sem_open on 3rd party semaphore");
256*043036a2SApple OSS Distributions 	sem_close(sem);
257*043036a2SApple OSS Distributions 
258*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "unlink");
259*043036a2SApple OSS Distributions 
260*043036a2SApple OSS Distributions 	/* 3rd party applications should not be able to interfere with eachother. */
261*043036a2SApple OSS Distributions 	T_LOG("Check 3rd party doesn't see other 3rd party");
262*043036a2SApple OSS Distributions 
263*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_no_access");
264*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_no_access");
265*043036a2SApple OSS Distributions 
266*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "open_excl");
267*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_access");
268*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_no_access");
269*043036a2SApple OSS Distributions 
270*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "open_excl");
271*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_access");
272*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_access");
273*043036a2SApple OSS Distributions 
274*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "unlink");
275*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_no_access");
276*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_access");
277*043036a2SApple OSS Distributions 
278*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "unlink");
279*043036a2SApple OSS Distributions 	do_namespace_op(team0_helper, "check_no_access");
280*043036a2SApple OSS Distributions 	do_namespace_op(team1_helper, "check_no_access");
281*043036a2SApple OSS Distributions 	#endif
282*043036a2SApple OSS Distributions }
283