1 /*
2 * Copyright (c) 2024 Apple 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 <net/if_utun.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #include <sys/kern_control.h>
33 #include <sys/sys_domain.h>
34 #include <sys/ioctl.h>
35
36 #include <darwintest.h>
37 #include <pthread.h>
38 #include <unistd.h>
39
40 T_GLOBAL_META(
41 T_META_NAMESPACE("xnu.net"),
42 T_META_RADAR_COMPONENT_NAME("xnu"),
43 T_META_RADAR_COMPONENT_VERSION("networking"),
44 T_META_CHECK_LEAKS(false));
45
46 #define SECONDS_TO_SLEEP 3
47
48 static int fd = -1;
49 static bool finished = false;
50
51 static void *
call_setsockopt(void * arg)52 call_setsockopt(void *arg)
53 {
54 #pragma unused(arg)
55
56 while (finished == false) {
57 (void)setsockopt(fd, SYSPROTO_CONTROL, SO_DEFUNCTIT, 0, 0);
58 }
59 return NULL;
60 }
61
62 T_DECL(kctl_disconnect_race, "Race mutliple threads triggering ctl_disconnect")
63 {
64 struct ctl_info ctl_info = { 0 };
65
66 struct sockaddr_ctl sockaddr_ctl = {
67 .sc_len = sizeof(struct sockaddr_ctl),
68 .sc_family = AF_SYSTEM,
69 .ss_sysaddr = AF_SYS_CONTROL,
70 .sc_unit = 0
71 };
72
73 pthread_t runner1, runner2;
74
75 T_ASSERT_POSIX_SUCCESS(fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), NULL);
76
77 strlcpy(ctl_info.ctl_name, UTUN_CONTROL_NAME, sizeof(ctl_info.ctl_name));
78 T_ASSERT_POSIX_SUCCESS(ioctl(fd, CTLIOCGINFO, &ctl_info), NULL);
79
80 sockaddr_ctl.sc_id = ctl_info.ctl_id;
81
82 T_ASSERT_POSIX_SUCCESS(connect(fd, (const struct sockaddr *)&sockaddr_ctl, sizeof(struct sockaddr_ctl)), NULL);
83
84 if (pthread_create(&runner1, NULL, call_setsockopt, NULL)) {
85 T_ASSERT_FAIL("pthread_create failed");
86 }
87
88 if (pthread_create(&runner2, NULL, call_setsockopt, NULL)) {
89 T_ASSERT_FAIL("pthread_create failed");
90 }
91
92 sleep(SECONDS_TO_SLEEP);
93
94 finished = true;
95
96 pthread_join(runner1, 0);
97 pthread_join(runner2, 0);
98 }
99