1*5c2921b0SApple OSS Distributions /* This tests thread_t uaf vulnerability in the XNU kernel due to
2*5c2921b0SApple OSS Distributions * a race condition in unp_connect
3*5c2921b0SApple OSS Distributions */
4*5c2921b0SApple OSS Distributions
5*5c2921b0SApple OSS Distributions #include <sys/un.h>
6*5c2921b0SApple OSS Distributions #include <sys/socket.h>
7*5c2921b0SApple OSS Distributions #include <pthread.h>
8*5c2921b0SApple OSS Distributions #include <sys/proc_info.h>
9*5c2921b0SApple OSS Distributions #include <libproc.h>
10*5c2921b0SApple OSS Distributions #include <darwintest.h>
11*5c2921b0SApple OSS Distributions #include <unistd.h>
12*5c2921b0SApple OSS Distributions
13*5c2921b0SApple OSS Distributions int g_start = 0;
14*5c2921b0SApple OSS Distributions int g_client = 0;
15*5c2921b0SApple OSS Distributions int g_sever1 = 0;
16*5c2921b0SApple OSS Distributions int g_sever2 = 0;
17*5c2921b0SApple OSS Distributions
18*5c2921b0SApple OSS Distributions static void
server_thread1(char * path)19*5c2921b0SApple OSS Distributions server_thread1(char* path)
20*5c2921b0SApple OSS Distributions {
21*5c2921b0SApple OSS Distributions struct sockaddr_un server_sockaddr;
22*5c2921b0SApple OSS Distributions memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
23*5c2921b0SApple OSS Distributions server_sockaddr.sun_family = AF_UNIX;
24*5c2921b0SApple OSS Distributions strcpy(server_sockaddr.sun_path, path);
25*5c2921b0SApple OSS Distributions unlink(server_sockaddr.sun_path);
26*5c2921b0SApple OSS Distributions
27*5c2921b0SApple OSS Distributions int server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
28*5c2921b0SApple OSS Distributions g_sever1 = server_sock;
29*5c2921b0SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(server_sock,
30*5c2921b0SApple OSS Distributions (struct sockaddr *) &server_sockaddr, sizeof(server_sockaddr)), NULL);
31*5c2921b0SApple OSS Distributions
32*5c2921b0SApple OSS Distributions /*********************************/
33*5c2921b0SApple OSS Distributions /* Listen for any client sockets */
34*5c2921b0SApple OSS Distributions /*********************************/
35*5c2921b0SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(listen(server_sock, -1), NULL);
36*5c2921b0SApple OSS Distributions
37*5c2921b0SApple OSS Distributions return;
38*5c2921b0SApple OSS Distributions }
39*5c2921b0SApple OSS Distributions
40*5c2921b0SApple OSS Distributions static void
server_thread2(char * path)41*5c2921b0SApple OSS Distributions server_thread2(char* path)
42*5c2921b0SApple OSS Distributions {
43*5c2921b0SApple OSS Distributions struct sockaddr_un server_sockaddr;
44*5c2921b0SApple OSS Distributions memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
45*5c2921b0SApple OSS Distributions server_sockaddr.sun_family = AF_UNIX;
46*5c2921b0SApple OSS Distributions strcpy(server_sockaddr.sun_path, path);
47*5c2921b0SApple OSS Distributions unlink(server_sockaddr.sun_path);
48*5c2921b0SApple OSS Distributions
49*5c2921b0SApple OSS Distributions int server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
50*5c2921b0SApple OSS Distributions g_sever2 = server_sock;
51*5c2921b0SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(server_sock,
52*5c2921b0SApple OSS Distributions (struct sockaddr *) &server_sockaddr, sizeof(server_sockaddr)), NULL);
53*5c2921b0SApple OSS Distributions
54*5c2921b0SApple OSS Distributions /*********************************/
55*5c2921b0SApple OSS Distributions /* Listen for any client sockets */
56*5c2921b0SApple OSS Distributions /*********************************/
57*5c2921b0SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(listen(server_sock, -1), NULL);
58*5c2921b0SApple OSS Distributions
59*5c2921b0SApple OSS Distributions return;
60*5c2921b0SApple OSS Distributions }
61*5c2921b0SApple OSS Distributions
62*5c2921b0SApple OSS Distributions static void
try_to_connect(char * path)63*5c2921b0SApple OSS Distributions try_to_connect(char* path)
64*5c2921b0SApple OSS Distributions {
65*5c2921b0SApple OSS Distributions struct sockaddr_un server_sockaddr;
66*5c2921b0SApple OSS Distributions memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
67*5c2921b0SApple OSS Distributions server_sockaddr.sun_family = AF_UNIX;
68*5c2921b0SApple OSS Distributions strcpy(server_sockaddr.sun_path, path);
69*5c2921b0SApple OSS Distributions //unlink(server_sockaddr.sun_path);
70*5c2921b0SApple OSS Distributions
71*5c2921b0SApple OSS Distributions while (g_start == 0) {
72*5c2921b0SApple OSS Distributions usleep(100);
73*5c2921b0SApple OSS Distributions }
74*5c2921b0SApple OSS Distributions int ret = connect(g_client, (struct sockaddr *)&server_sockaddr,
75*5c2921b0SApple OSS Distributions sizeof(server_sockaddr));
76*5c2921b0SApple OSS Distributions
77*5c2921b0SApple OSS Distributions T_ASSERT_TRUE(ret == 0 || errno == EALREADY || errno == EISCONN,
78*5c2921b0SApple OSS Distributions "connect with ret: %d(%d)", ret, errno);
79*5c2921b0SApple OSS Distributions }
80*5c2921b0SApple OSS Distributions
81*5c2921b0SApple OSS Distributions
82*5c2921b0SApple OSS Distributions static void
test_unp_connect_multithread()83*5c2921b0SApple OSS Distributions test_unp_connect_multithread()
84*5c2921b0SApple OSS Distributions {
85*5c2921b0SApple OSS Distributions int client_sock;
86*5c2921b0SApple OSS Distributions char path[] = "/tmp/";
87*5c2921b0SApple OSS Distributions char path1[256];
88*5c2921b0SApple OSS Distributions char path2[256];
89*5c2921b0SApple OSS Distributions char path3[256];
90*5c2921b0SApple OSS Distributions
91*5c2921b0SApple OSS Distributions strncpy(path1, path, 255);
92*5c2921b0SApple OSS Distributions strcat(path1, "/1");
93*5c2921b0SApple OSS Distributions strncpy(path2, path, 255);
94*5c2921b0SApple OSS Distributions strcat(path2, "/2");
95*5c2921b0SApple OSS Distributions strncpy(path3, path, 255);
96*5c2921b0SApple OSS Distributions strcat(path3, "/3");
97*5c2921b0SApple OSS Distributions
98*5c2921b0SApple OSS Distributions
99*5c2921b0SApple OSS Distributions for (int i = 0; i < 1024; i++) {
100*5c2921b0SApple OSS Distributions T_SETUPBEGIN;
101*5c2921b0SApple OSS Distributions server_thread1(path1);
102*5c2921b0SApple OSS Distributions server_thread2(path2);
103*5c2921b0SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(client_sock = socket(AF_UNIX, SOCK_STREAM, 0), NULL);
104*5c2921b0SApple OSS Distributions
105*5c2921b0SApple OSS Distributions unlink(path3);
106*5c2921b0SApple OSS Distributions struct sockaddr_un client_sockaddr;
107*5c2921b0SApple OSS Distributions client_sockaddr.sun_family = AF_UNIX;
108*5c2921b0SApple OSS Distributions strcpy(client_sockaddr.sun_path, path3);
109*5c2921b0SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(client_sock, (struct sockaddr *)&client_sockaddr,
110*5c2921b0SApple OSS Distributions sizeof(client_sockaddr)), NULL);
111*5c2921b0SApple OSS Distributions T_SETUPEND;
112*5c2921b0SApple OSS Distributions g_client = client_sock;
113*5c2921b0SApple OSS Distributions g_start = 0;
114*5c2921b0SApple OSS Distributions pthread_t runner1;
115*5c2921b0SApple OSS Distributions pthread_t runner2;
116*5c2921b0SApple OSS Distributions if (pthread_create(&runner1, 0, (void*)try_to_connect, path1)) {
117*5c2921b0SApple OSS Distributions T_ASSERT_FAIL("pthread_create failed");
118*5c2921b0SApple OSS Distributions }
119*5c2921b0SApple OSS Distributions
120*5c2921b0SApple OSS Distributions if (pthread_create(&runner2, 0, (void*)try_to_connect, path2)) {
121*5c2921b0SApple OSS Distributions T_ASSERT_FAIL("pthread_create failed");
122*5c2921b0SApple OSS Distributions }
123*5c2921b0SApple OSS Distributions usleep(300);
124*5c2921b0SApple OSS Distributions g_start = 1;
125*5c2921b0SApple OSS Distributions pthread_join(runner1, 0);
126*5c2921b0SApple OSS Distributions pthread_join(runner2, 0);
127*5c2921b0SApple OSS Distributions
128*5c2921b0SApple OSS Distributions usleep(3000);
129*5c2921b0SApple OSS Distributions
130*5c2921b0SApple OSS Distributions struct socket_fdinfo si_1 = {0};
131*5c2921b0SApple OSS Distributions proc_pidfdinfo(getpid(), g_sever1, PROC_PIDFDSOCKETINFO, &si_1,
132*5c2921b0SApple OSS Distributions sizeof(si_1));
133*5c2921b0SApple OSS Distributions struct socket_fdinfo si_2 = {0};
134*5c2921b0SApple OSS Distributions proc_pidfdinfo(getpid(), g_sever2, PROC_PIDFDSOCKETINFO, &si_2,
135*5c2921b0SApple OSS Distributions sizeof(si_2));
136*5c2921b0SApple OSS Distributions if (si_1.psi.soi_incqlen || si_2.psi.soi_incqlen) {
137*5c2921b0SApple OSS Distributions close(g_sever2);
138*5c2921b0SApple OSS Distributions close(g_sever1);
139*5c2921b0SApple OSS Distributions }
140*5c2921b0SApple OSS Distributions close(client_sock);
141*5c2921b0SApple OSS Distributions close(g_sever2);
142*5c2921b0SApple OSS Distributions close(g_sever1);
143*5c2921b0SApple OSS Distributions }
144*5c2921b0SApple OSS Distributions }
145*5c2921b0SApple OSS Distributions
146*5c2921b0SApple OSS Distributions T_DECL(unp_connect_thread_uaf, "Uaf due to multithreaded unp_connect")
147*5c2921b0SApple OSS Distributions {
148*5c2921b0SApple OSS Distributions test_unp_connect_multithread();
149*5c2921b0SApple OSS Distributions }
150