xref: /xnu-8796.141.3/tests/tcp_input_outputopts_uaf_56155583.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <stdint.h>
2*1b191cb5SApple OSS Distributions #include <stdio.h>
3*1b191cb5SApple OSS Distributions #include <stdlib.h>
4*1b191cb5SApple OSS Distributions #include <string.h>
5*1b191cb5SApple OSS Distributions 
6*1b191cb5SApple OSS Distributions #include <arpa/inet.h>
7*1b191cb5SApple OSS Distributions #include <fcntl.h>
8*1b191cb5SApple OSS Distributions #include <pthread.h>
9*1b191cb5SApple OSS Distributions #include <netinet/in.h>
10*1b191cb5SApple OSS Distributions #include <sys/ioctl.h>
11*1b191cb5SApple OSS Distributions #include <sys/socket.h>
12*1b191cb5SApple OSS Distributions #include <unistd.h>
13*1b191cb5SApple OSS Distributions 
14*1b191cb5SApple OSS Distributions #include <darwintest.h>
15*1b191cb5SApple OSS Distributions 
16*1b191cb5SApple OSS Distributions /* sizeof(struct ip6_pktopts) */
17*1b191cb5SApple OSS Distributions #define SIZEOF_STRUCT_IP6_PKTOPTS 192
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions static int finished = 0;
20*1b191cb5SApple OSS Distributions 
21*1b191cb5SApple OSS Distributions static void *
setopt_thread(void * data)22*1b191cb5SApple OSS Distributions setopt_thread(void *data)
23*1b191cb5SApple OSS Distributions {
24*1b191cb5SApple OSS Distributions 	int s = *(int *)data;
25*1b191cb5SApple OSS Distributions 	uint8_t optbuf[CMSG_LEN(0)];
26*1b191cb5SApple OSS Distributions 	uint8_t spraybuf[SIZEOF_STRUCT_IP6_PKTOPTS];
27*1b191cb5SApple OSS Distributions 
28*1b191cb5SApple OSS Distributions 	memset(optbuf, 0, sizeof(optbuf));
29*1b191cb5SApple OSS Distributions 	memset(spraybuf, 0x41, sizeof(spraybuf));
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions 	while (!finished) {
32*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setsockopt(s, IPPROTO_IPV6, IPV6_2292PKTOPTIONS, optbuf, sizeof(optbuf)), NULL);
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions 		/* force an error to free: */
35*1b191cb5SApple OSS Distributions 		T_ASSERT_EQ(setsockopt(s, IPPROTO_IPV6, IPV6_2292PKTOPTIONS, optbuf, 1), -1, NULL);
36*1b191cb5SApple OSS Distributions 
37*1b191cb5SApple OSS Distributions 		/* realloc: */
38*1b191cb5SApple OSS Distributions 		T_ASSERT_EQ(ioctl(-1, _IOW('x', 0, spraybuf), spraybuf), -1, NULL);
39*1b191cb5SApple OSS Distributions 	}
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions 	return NULL;
42*1b191cb5SApple OSS Distributions }
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions static void *
connect_thread(void * data)45*1b191cb5SApple OSS Distributions connect_thread(void *data)
46*1b191cb5SApple OSS Distributions {
47*1b191cb5SApple OSS Distributions 	struct sockaddr_in6 *dst = data;
48*1b191cb5SApple OSS Distributions 	int s;
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions 	while (!finished) {
51*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP), NULL);
52*1b191cb5SApple OSS Distributions 		connect(s, (const struct sockaddr *)dst, sizeof(*dst));
53*1b191cb5SApple OSS Distributions 		close(s);
54*1b191cb5SApple OSS Distributions 	}
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions 	return NULL;
57*1b191cb5SApple OSS Distributions }
58*1b191cb5SApple OSS Distributions 
59*1b191cb5SApple OSS Distributions T_DECL(tcp_input_outputopts_uaf_56155583, "Use-after-free when accepting TCP6 connections.")
60*1b191cb5SApple OSS Distributions {
61*1b191cb5SApple OSS Distributions 	int s;
62*1b191cb5SApple OSS Distributions 	struct sockaddr_in6 sin6 = {
63*1b191cb5SApple OSS Distributions 		.sin6_family = AF_INET6,
64*1b191cb5SApple OSS Distributions 		.sin6_port = htons(1337)
65*1b191cb5SApple OSS Distributions 	};
66*1b191cb5SApple OSS Distributions 	struct sockaddr_in6 addr;
67*1b191cb5SApple OSS Distributions 	socklen_t addr_len;
68*1b191cb5SApple OSS Distributions 	pthread_t threads[20];
69*1b191cb5SApple OSS Distributions 	int nthreads = 0;
70*1b191cb5SApple OSS Distributions 	int n;
71*1b191cb5SApple OSS Distributions 
72*1b191cb5SApple OSS Distributions 	T_SETUPBEGIN;
73*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(inet_pton(AF_INET6, "::1", &sin6.sin6_addr), 1, NULL);
74*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP), NULL);
75*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(bind(s, (const struct sockaddr *)&sin6, sizeof(sin6)), NULL);
76*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(listen(s, 32), NULL);
77*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK), NULL);
78*1b191cb5SApple OSS Distributions 	T_SETUPEND;
79*1b191cb5SApple OSS Distributions 
80*1b191cb5SApple OSS Distributions 	for (n = 0; n < 16; ++n) {
81*1b191cb5SApple OSS Distributions 		if (pthread_create(&threads[nthreads++], NULL, setopt_thread, &s)) {
82*1b191cb5SApple OSS Distributions 			T_ASSERT_FAIL("pthread_create failed");
83*1b191cb5SApple OSS Distributions 		}
84*1b191cb5SApple OSS Distributions 	}
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions 	for (n = 0; n < 4; ++n) {
87*1b191cb5SApple OSS Distributions 		if (pthread_create(&threads[nthreads++], NULL, connect_thread, &sin6)) {
88*1b191cb5SApple OSS Distributions 			T_ASSERT_FAIL("pthread_create failed");
89*1b191cb5SApple OSS Distributions 		}
90*1b191cb5SApple OSS Distributions 	}
91*1b191cb5SApple OSS Distributions 
92*1b191cb5SApple OSS Distributions 	for (n = 0; n < 200000; ++n) {
93*1b191cb5SApple OSS Distributions 		addr_len = sizeof(addr);
94*1b191cb5SApple OSS Distributions 		close(accept(s, (struct sockaddr *)&addr, &addr_len));
95*1b191cb5SApple OSS Distributions 	}
96*1b191cb5SApple OSS Distributions 
97*1b191cb5SApple OSS Distributions 	finished = 1;
98*1b191cb5SApple OSS Distributions 
99*1b191cb5SApple OSS Distributions 	for (n = 0; n < nthreads; ++n) {
100*1b191cb5SApple OSS Distributions 		pthread_join(threads[n], NULL);
101*1b191cb5SApple OSS Distributions 	}
102*1b191cb5SApple OSS Distributions }
103