1 /*
2 * Copyright (c) 2025 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <darwintest.h>
30 #include <fcntl.h>
31 #include <mach/mach.h>
32 #include <mach/mach_vm.h>
33 #include <mach/vm_map.h>
34 #include <mach-o/dyld.h>
35 #include <spawn_private.h>
36 #include <sys/aio.h>
37 #include <sys/spawn_internal.h>
38 #include <stdlib.h>
39 #include <sys/mman.h>
40 #include <sys/sysctl.h>
41 #include <sys/wait.h>
42 #include <signal.h>
43
44 T_GLOBAL_META(
45 T_META_NAMESPACE("xnu.proc"),
46 T_META_RADAR_COMPONENT_NAME("xnu"),
47 T_META_RADAR_COMPONENT_VERSION("proc"),
48 T_META_OWNER("s_musaev"),
49 T_META_RUN_CONCURRENTLY(true));
50
51 #define UID (0xbadc0ffee)
52
53 static volatile int concurrent_setreuid_running = 0;
54
55 void
concurrent_setreuid_th1(void * arg __unused)56 concurrent_setreuid_th1(void* arg __unused)
57 {
58 while (concurrent_setreuid_running) {
59 T_ASSERT_POSIX_SUCCESS(setreuid(1, 0), "setreuid");
60 T_ASSERT_POSIX_SUCCESS(setreuid(UID, 0), "setreuid");
61 }
62 }
63
64 void
concurrent_setreuid_th2(void * arg __unused)65 concurrent_setreuid_th2(void* arg __unused)
66 {
67 while (concurrent_setreuid_running) {
68 T_ASSERT_POSIX_SUCCESS(setreuid(UID, 0), "setreuid");
69 }
70 }
71
72 T_DECL(concurrent_setreuid,
73 "Ensure concurrent calls to setreuid and setuid working correctly",
74 T_META_ASROOT(true),
75 #if TARGET_OS_OSX
76 T_META_ENABLED(true)
77 #else
78 T_META_ENABLED(false)
79 #endif
80 )
81 {
82 pthread_t t1, t2;
83
84 concurrent_setreuid_running = 1;
85 pthread_create(&t1, NULL, (void*(*)(void*)) & concurrent_setreuid_th1, NULL);
86 pthread_create(&t2, NULL, (void*(*)(void*)) & concurrent_setreuid_th2, NULL);
87
88 sleep(1);
89 concurrent_setreuid_running = 0;
90 pthread_join(t1, NULL);
91 pthread_join(t2, NULL);
92
93 unsigned long long uid = UID;
94 size_t uid_size = sizeof(unsigned long long);
95 int count = 0;
96 size_t count_size = sizeof(unsigned long long);
97 sysctlbyname("debug.test.proccnt_uid", &count, &count_size, &uid, uid_size);
98 T_ASSERT_EQ(count, 1, "Expected to be exactly 1 proc (current proc) for given uid");
99 T_PASS("INFO: passed test_concurrent_setreuid");
100 }
101