xref: /xnu-8796.141.3/tests/test_knote_use_after_free.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Test based on POC attached to rdar://96567281 (Knote Use-after-Free in XNU)
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  */
5*1b191cb5SApple OSS Distributions #include <darwintest.h>
6*1b191cb5SApple OSS Distributions #include <mach/mach.h>
7*1b191cb5SApple OSS Distributions #include <pthread.h>
8*1b191cb5SApple OSS Distributions #include <sys/event.h>
9*1b191cb5SApple OSS Distributions #include <stdlib.h>
10*1b191cb5SApple OSS Distributions 
11*1b191cb5SApple OSS Distributions T_GLOBAL_META(
12*1b191cb5SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
13*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
14*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
15*1b191cb5SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE));
16*1b191cb5SApple OSS Distributions 
17*1b191cb5SApple OSS Distributions typedef struct knote_context_s knote_context_t;
18*1b191cb5SApple OSS Distributions struct knote_context_s {
19*1b191cb5SApple OSS Distributions 	volatile int initialized;
20*1b191cb5SApple OSS Distributions 	volatile int start;
21*1b191cb5SApple OSS Distributions };
22*1b191cb5SApple OSS Distributions 
23*1b191cb5SApple OSS Distributions static void *
th_allocate_knotes(void * arg)24*1b191cb5SApple OSS Distributions th_allocate_knotes(void *arg)
25*1b191cb5SApple OSS Distributions {
26*1b191cb5SApple OSS Distributions 	knote_context_t *context = (knote_context_t *)arg;
27*1b191cb5SApple OSS Distributions 	kern_return_t kr = KERN_SUCCESS;
28*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(context->initialized, (int)0, "th_allocate_knotes context is initialized.");
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions 	mach_port_t sync_port = MACH_PORT_NULL;
31*1b191cb5SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &sync_port);
32*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_allocate sync_port");
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions 	mach_port_t kq_port = MACH_PORT_NULL;
35*1b191cb5SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &kq_port);
36*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_allocate kq_port");
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions 	int kq = kqueue();
39*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kq, "kqueue");
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions #define PORTS_COUNT 0x1000
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions 	mach_port_t *ports = calloc(PORTS_COUNT, sizeof(mach_port_t));
44*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(ports, "calloc");
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < PORTS_COUNT; i++) {
47*1b191cb5SApple OSS Distributions 		mach_port_t port = MACH_PORT_NULL;
48*1b191cb5SApple OSS Distributions 		kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
49*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_allocate");
50*1b191cb5SApple OSS Distributions 
51*1b191cb5SApple OSS Distributions 		typedef struct move_receive_request_s move_receive_request_t;
52*1b191cb5SApple OSS Distributions 		typedef struct move_receive_reply_s move_receive_reply_t;
53*1b191cb5SApple OSS Distributions 
54*1b191cb5SApple OSS Distributions 		struct move_receive_request_s {
55*1b191cb5SApple OSS Distributions 			mach_msg_header_t header;
56*1b191cb5SApple OSS Distributions 			mach_msg_body_t body;
57*1b191cb5SApple OSS Distributions 			mach_msg_port_descriptor_t port;
58*1b191cb5SApple OSS Distributions 			mach_msg_port_descriptor_t kq_port;
59*1b191cb5SApple OSS Distributions 		};
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions 		struct move_receive_reply_s {
62*1b191cb5SApple OSS Distributions 			mach_msg_header_t header;
63*1b191cb5SApple OSS Distributions 			mach_msg_body_t body;
64*1b191cb5SApple OSS Distributions 			mach_msg_port_descriptor_t port;
65*1b191cb5SApple OSS Distributions 			mach_msg_port_descriptor_t kq_port;
66*1b191cb5SApple OSS Distributions 			mach_msg_trailer_t trailer;
67*1b191cb5SApple OSS Distributions 		};
68*1b191cb5SApple OSS Distributions 
69*1b191cb5SApple OSS Distributions 		union {
70*1b191cb5SApple OSS Distributions 			move_receive_request_t request;
71*1b191cb5SApple OSS Distributions 			move_receive_reply_t reply;
72*1b191cb5SApple OSS Distributions 		} message;
73*1b191cb5SApple OSS Distributions 
74*1b191cb5SApple OSS Distributions 		move_receive_request_t *request = &message.request;
75*1b191cb5SApple OSS Distributions 		move_receive_reply_t *reply = &message.reply;
76*1b191cb5SApple OSS Distributions 
77*1b191cb5SApple OSS Distributions 		request->header = (mach_msg_header_t){
78*1b191cb5SApple OSS Distributions 			.msgh_remote_port = sync_port,
79*1b191cb5SApple OSS Distributions 			.msgh_local_port = MACH_PORT_NULL,
80*1b191cb5SApple OSS Distributions 			.msgh_voucher_port = MACH_PORT_NULL,
81*1b191cb5SApple OSS Distributions 			.msgh_id = (mach_msg_id_t)0x88888888,
82*1b191cb5SApple OSS Distributions 			.msgh_size = sizeof(*request),
83*1b191cb5SApple OSS Distributions 			.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MAKE_SEND, 0, 0, MACH_MSGH_BITS_COMPLEX),
84*1b191cb5SApple OSS Distributions 		};
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions 		request->body = (mach_msg_body_t){
87*1b191cb5SApple OSS Distributions 			.msgh_descriptor_count = 2,
88*1b191cb5SApple OSS Distributions 		};
89*1b191cb5SApple OSS Distributions 
90*1b191cb5SApple OSS Distributions 		request->port = (mach_msg_port_descriptor_t){
91*1b191cb5SApple OSS Distributions 			.name = port,
92*1b191cb5SApple OSS Distributions 			.disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
93*1b191cb5SApple OSS Distributions 			.type = MACH_MSG_PORT_DESCRIPTOR,
94*1b191cb5SApple OSS Distributions 		};
95*1b191cb5SApple OSS Distributions 		request->kq_port = (mach_msg_port_descriptor_t){
96*1b191cb5SApple OSS Distributions 			.name = kq_port,
97*1b191cb5SApple OSS Distributions 			.disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
98*1b191cb5SApple OSS Distributions 			.type = MACH_MSG_PORT_DESCRIPTOR,
99*1b191cb5SApple OSS Distributions 		};
100*1b191cb5SApple OSS Distributions 
101*1b191cb5SApple OSS Distributions 		kr = mach_msg(&request->header, MACH_SEND_MSG, sizeof(*request), 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
102*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(kr, MACH_MSG_SUCCESS, "mach_msg");
103*1b191cb5SApple OSS Distributions 
104*1b191cb5SApple OSS Distributions 		struct kevent_qos_s event = {
105*1b191cb5SApple OSS Distributions 			.ident = sync_port,
106*1b191cb5SApple OSS Distributions 			.filter = EVFILT_MACHPORT,
107*1b191cb5SApple OSS Distributions 			.flags = EV_ADD | EV_ENABLE | EV_DISPATCH,
108*1b191cb5SApple OSS Distributions 			.qos = 0xA00,
109*1b191cb5SApple OSS Distributions 			.udata = 0x42424242,
110*1b191cb5SApple OSS Distributions 			.fflags = MACH_RCV_MSG,
111*1b191cb5SApple OSS Distributions 			.xflags = 0x00,
112*1b191cb5SApple OSS Distributions 			.data = 0x00,
113*1b191cb5SApple OSS Distributions 			.ext = {(uint64_t)reply, sizeof(*reply), 0, 0},
114*1b191cb5SApple OSS Distributions 		};
115*1b191cb5SApple OSS Distributions 
116*1b191cb5SApple OSS Distributions 		struct kevent_qos_s out_events[1];
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions 		int nevents = kevent_qos(kq, &event, 1, out_events, 1, NULL, NULL, 0);
119*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(nevents, (int)1, "kevent_qos");
120*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(out_events[0].udata, (uint64_t)0x42424242, "kevent_qos");
121*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_BITS_SET(reply->header.msgh_bits, MACH_MSGH_BITS_COMPLEX, "message is complex");
122*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(reply->body.msgh_descriptor_count, (mach_msg_size_t)2, "mach_msg");
123*1b191cb5SApple OSS Distributions 
124*1b191cb5SApple OSS Distributions 		ports[i] = reply->port.name;
125*1b191cb5SApple OSS Distributions 		kq_port = reply->kq_port.name;
126*1b191cb5SApple OSS Distributions 	}
127*1b191cb5SApple OSS Distributions 
128*1b191cb5SApple OSS Distributions 	context->initialized = 1;
129*1b191cb5SApple OSS Distributions 	while (!context->start) {
130*1b191cb5SApple OSS Distributions 	}
131*1b191cb5SApple OSS Distributions 
132*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < PORTS_COUNT; i++) {
133*1b191cb5SApple OSS Distributions 		uint32_t wl_id = (uint32_t)0x99999999;
134*1b191cb5SApple OSS Distributions 
135*1b191cb5SApple OSS Distributions 		struct kevent_qos_s event = {
136*1b191cb5SApple OSS Distributions 			.ident = ports[i],
137*1b191cb5SApple OSS Distributions 			.filter = EVFILT_WORKLOOP,
138*1b191cb5SApple OSS Distributions 			.flags = EV_ADD | EV_DISABLE,
139*1b191cb5SApple OSS Distributions 			.qos = 0x00,
140*1b191cb5SApple OSS Distributions 			.udata = 0x88888888,
141*1b191cb5SApple OSS Distributions 			.fflags = NOTE_WL_SYNC_IPC,
142*1b191cb5SApple OSS Distributions 			.xflags = 0x00,
143*1b191cb5SApple OSS Distributions 			.data = 0x66666666,
144*1b191cb5SApple OSS Distributions 			.ext = {0x00, 0x00, 0x00, 0x00},
145*1b191cb5SApple OSS Distributions 		};
146*1b191cb5SApple OSS Distributions 		struct kevent_qos_s output = { };
147*1b191cb5SApple OSS Distributions 		int ret = kevent_id(wl_id, &event, 1, &output, 1, NULL, NULL,
148*1b191cb5SApple OSS Distributions 		    KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
149*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kevent_id");
150*1b191cb5SApple OSS Distributions 	}
151*1b191cb5SApple OSS Distributions 	return NULL;
152*1b191cb5SApple OSS Distributions }
153*1b191cb5SApple OSS Distributions 
154*1b191cb5SApple OSS Distributions T_DECL(test_knote_use_after_free,
155*1b191cb5SApple OSS Distributions     "Verify knote use-after-free issue does not reproduce - rdar://96567281 (Knote Use-after-Free in XNU)",
156*1b191cb5SApple OSS Distributions     T_META_CHECK_LEAKS(false))
157*1b191cb5SApple OSS Distributions {
158*1b191cb5SApple OSS Distributions 	mach_port_t task = mach_task_self();
159*1b191cb5SApple OSS Distributions 
160*1b191cb5SApple OSS Distributions 	knote_context_t context = {
161*1b191cb5SApple OSS Distributions 		.initialized = 0,
162*1b191cb5SApple OSS Distributions 		.start = 0,
163*1b191cb5SApple OSS Distributions 	};
164*1b191cb5SApple OSS Distributions 
165*1b191cb5SApple OSS Distributions 	pthread_t thknote;
166*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_create(&thknote, NULL, th_allocate_knotes, &context), "pthread_create");
167*1b191cb5SApple OSS Distributions 
168*1b191cb5SApple OSS Distributions 	int kq = kqueue();
169*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kq, "kqueue");
170*1b191cb5SApple OSS Distributions 
171*1b191cb5SApple OSS Distributions #define KNOTE_PORT_COUNT 2
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions 	kern_return_t kr = KERN_SUCCESS;
174*1b191cb5SApple OSS Distributions 	mach_port_t sync_port = MACH_PORT_NULL, knote_port[KNOTE_PORT_COUNT];
175*1b191cb5SApple OSS Distributions 	kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &sync_port);
176*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_allocate sync_port");
177*1b191cb5SApple OSS Distributions 
178*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < KNOTE_PORT_COUNT; i++) {
179*1b191cb5SApple OSS Distributions 		knote_port[i] = MACH_PORT_NULL;
180*1b191cb5SApple OSS Distributions 		kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &knote_port[i]);
181*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_allocate knote_port");
182*1b191cb5SApple OSS Distributions 	}
183*1b191cb5SApple OSS Distributions 
184*1b191cb5SApple OSS Distributions 	typedef struct sync_knote_msg_local_s sync_knote_msg_local_t;
185*1b191cb5SApple OSS Distributions 	typedef struct sync_knote_msg_remote_s sync_knote_msg_remote_t;
186*1b191cb5SApple OSS Distributions 
187*1b191cb5SApple OSS Distributions #pragma pack(4)
188*1b191cb5SApple OSS Distributions 	struct sync_knote_msg_local_s {
189*1b191cb5SApple OSS Distributions 		mach_msg_header_t header;
190*1b191cb5SApple OSS Distributions 		mach_msg_body_t body;
191*1b191cb5SApple OSS Distributions 		mach_msg_port_descriptor_t port[KNOTE_PORT_COUNT];
192*1b191cb5SApple OSS Distributions 		uint64_t sequence;
193*1b191cb5SApple OSS Distributions 	};
194*1b191cb5SApple OSS Distributions #pragma pack(0)
195*1b191cb5SApple OSS Distributions 
196*1b191cb5SApple OSS Distributions #pragma pack(4)
197*1b191cb5SApple OSS Distributions 	struct sync_knote_msg_remote_s {
198*1b191cb5SApple OSS Distributions 		mach_msg_header_t header;
199*1b191cb5SApple OSS Distributions 		mach_msg_body_t body;
200*1b191cb5SApple OSS Distributions 		mach_msg_port_descriptor_t port[KNOTE_PORT_COUNT];
201*1b191cb5SApple OSS Distributions 		uint64_t sequence;
202*1b191cb5SApple OSS Distributions 		mach_msg_trailer_t trailer;
203*1b191cb5SApple OSS Distributions 	};
204*1b191cb5SApple OSS Distributions #pragma pack(0)
205*1b191cb5SApple OSS Distributions 
206*1b191cb5SApple OSS Distributions 	union {
207*1b191cb5SApple OSS Distributions 		sync_knote_msg_local_t local;
208*1b191cb5SApple OSS Distributions 		sync_knote_msg_remote_t remote;
209*1b191cb5SApple OSS Distributions 	} message;
210*1b191cb5SApple OSS Distributions 
211*1b191cb5SApple OSS Distributions 	sync_knote_msg_local_t *local = &message.local;
212*1b191cb5SApple OSS Distributions 	sync_knote_msg_remote_t *remote = &message.remote;
213*1b191cb5SApple OSS Distributions 
214*1b191cb5SApple OSS Distributions 	local->header = (mach_msg_header_t){
215*1b191cb5SApple OSS Distributions 		.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MAKE_SEND, 0, 0, MACH_MSGH_BITS_COMPLEX),
216*1b191cb5SApple OSS Distributions 		.msgh_remote_port = sync_port,
217*1b191cb5SApple OSS Distributions 		.msgh_local_port = MACH_PORT_NULL,
218*1b191cb5SApple OSS Distributions 		.msgh_voucher_port = MACH_PORT_NULL,
219*1b191cb5SApple OSS Distributions 		.msgh_size = sizeof(sync_knote_msg_local_t),
220*1b191cb5SApple OSS Distributions 		.msgh_id =  (mach_msg_id_t)0x88888888,
221*1b191cb5SApple OSS Distributions 	};
222*1b191cb5SApple OSS Distributions 	local->body.msgh_descriptor_count = KNOTE_PORT_COUNT;
223*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < KNOTE_PORT_COUNT; i++) {
224*1b191cb5SApple OSS Distributions 		local->port[i] = (mach_msg_port_descriptor_t){
225*1b191cb5SApple OSS Distributions 			.name = knote_port[i],
226*1b191cb5SApple OSS Distributions 			.disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
227*1b191cb5SApple OSS Distributions 			.type = MACH_MSG_PORT_DESCRIPTOR,
228*1b191cb5SApple OSS Distributions 		};
229*1b191cb5SApple OSS Distributions 	}
230*1b191cb5SApple OSS Distributions 	local->sequence = 0x6666666666666666;
231*1b191cb5SApple OSS Distributions 	kr = mach_msg(&local->header, MACH_SEND_MSG, sizeof(sync_knote_msg_local_t), 0, MACH_PORT_NULL,
232*1b191cb5SApple OSS Distributions 	    0, MACH_PORT_NULL);
233*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(kr, MACH_MSG_SUCCESS, "mach_msg");
234*1b191cb5SApple OSS Distributions 
235*1b191cb5SApple OSS Distributions 	struct kevent_qos_s event = {
236*1b191cb5SApple OSS Distributions 		.ident = sync_port,
237*1b191cb5SApple OSS Distributions 		.filter = EVFILT_MACHPORT,
238*1b191cb5SApple OSS Distributions 		.flags = EV_ADD | EV_ENABLE | EV_DISPATCH,
239*1b191cb5SApple OSS Distributions 		.qos = 0xA00,
240*1b191cb5SApple OSS Distributions 		.udata = 42424242,
241*1b191cb5SApple OSS Distributions 		.fflags = MACH_RCV_MSG,
242*1b191cb5SApple OSS Distributions 		.xflags = 0x00,
243*1b191cb5SApple OSS Distributions 		.data = 0x00,
244*1b191cb5SApple OSS Distributions 		.ext = {(uint64_t)remote, sizeof(*remote), 0, 0},
245*1b191cb5SApple OSS Distributions 	};
246*1b191cb5SApple OSS Distributions 
247*1b191cb5SApple OSS Distributions 	struct kevent_qos_s out_events[1];
248*1b191cb5SApple OSS Distributions 
249*1b191cb5SApple OSS Distributions 	int nevents = kevent_qos(kq, &event, 1, out_events, 1, NULL, NULL, 0);
250*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(nevents, (int)1, "kevent_qos nevents");
251*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(remote->sequence, (uint64_t)0x6666666666666666, "kevent_qos remote->sequence");
252*1b191cb5SApple OSS Distributions 
253*1b191cb5SApple OSS Distributions 	int ret = 0;
254*1b191cb5SApple OSS Distributions 	struct kevent_qos_s del_event = {
255*1b191cb5SApple OSS Distributions 		.ident = sync_port,
256*1b191cb5SApple OSS Distributions 		.filter = EVFILT_MACHPORT,
257*1b191cb5SApple OSS Distributions 		.flags = EV_DELETE,
258*1b191cb5SApple OSS Distributions 		.qos = 0xA00,
259*1b191cb5SApple OSS Distributions 		.udata = 0x00,
260*1b191cb5SApple OSS Distributions 		.fflags = MACH_RCV_MSG,
261*1b191cb5SApple OSS Distributions 		.xflags = 0x00,
262*1b191cb5SApple OSS Distributions 		.data = 0x00,
263*1b191cb5SApple OSS Distributions 		.ext = {0, 0, 0, 0},
264*1b191cb5SApple OSS Distributions 	};
265*1b191cb5SApple OSS Distributions 
266*1b191cb5SApple OSS Distributions 	ret = kevent_qos(kq, &del_event, 1, NULL, 0, NULL, NULL, 0);
267*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, (int)0, "kevent_qos return");
268*1b191cb5SApple OSS Distributions 
269*1b191cb5SApple OSS Distributions 	while (!context.initialized) {
270*1b191cb5SApple OSS Distributions 	}
271*1b191cb5SApple OSS Distributions 
272*1b191cb5SApple OSS Distributions 	context.start = 1;
273*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_join(thknote, NULL), "pthread_join");
274*1b191cb5SApple OSS Distributions 
275*1b191cb5SApple OSS Distributions 	kr = _kernelrpc_mach_port_insert_right_trap(task, sync_port, sync_port, MACH_MSG_TYPE_MOVE_RECEIVE);
276*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "_kernelrpc_mach_port_insert_right_trap");
277*1b191cb5SApple OSS Distributions }
278