xref: /xnu-10002.1.13/tests/fd_aio_fsync_uaf.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Proof of Concept / Test Case
3*1031c584SApple OSS Distributions  * XNU: aio_work_thread use-after-free for AIO_FSYNC entries
4*1031c584SApple OSS Distributions  */
5*1031c584SApple OSS Distributions #include <err.h>
6*1031c584SApple OSS Distributions #include <stdarg.h>
7*1031c584SApple OSS Distributions #include <stdint.h>
8*1031c584SApple OSS Distributions #include <stdio.h>
9*1031c584SApple OSS Distributions #include <stdlib.h>
10*1031c584SApple OSS Distributions #include <string.h>
11*1031c584SApple OSS Distributions #include <strings.h>
12*1031c584SApple OSS Distributions 
13*1031c584SApple OSS Distributions #include <sys/aio.h>
14*1031c584SApple OSS Distributions #include <unistd.h>
15*1031c584SApple OSS Distributions #include <darwintest.h>
16*1031c584SApple OSS Distributions #include <time.h>
17*1031c584SApple OSS Distributions 
18*1031c584SApple OSS Distributions T_GLOBAL_META(
19*1031c584SApple OSS Distributions 	T_META_NAMESPACE("xnu.fd"),
20*1031c584SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
21*1031c584SApple OSS Distributions 
22*1031c584SApple OSS Distributions #define NREQUESTS 8
23*1031c584SApple OSS Distributions 
24*1031c584SApple OSS Distributions static void
attempt(int fd)25*1031c584SApple OSS Distributions attempt(int fd)
26*1031c584SApple OSS Distributions {
27*1031c584SApple OSS Distributions 	struct aiocb ap[NREQUESTS];
28*1031c584SApple OSS Distributions 	size_t n;
29*1031c584SApple OSS Distributions 	unsigned char c;
30*1031c584SApple OSS Distributions 
31*1031c584SApple OSS Distributions 	for (n = 0; n < NREQUESTS; ++n) {
32*1031c584SApple OSS Distributions 		ap[n].aio_fildes = fd;
33*1031c584SApple OSS Distributions 		ap[n].aio_nbytes = 1;
34*1031c584SApple OSS Distributions 		ap[n].aio_buf = &c;
35*1031c584SApple OSS Distributions 		ap[n].aio_sigevent.sigev_notify = SIGEV_NONE;
36*1031c584SApple OSS Distributions 	}
37*1031c584SApple OSS Distributions 
38*1031c584SApple OSS Distributions 	/*
39*1031c584SApple OSS Distributions 	 * fire them off and exit.
40*1031c584SApple OSS Distributions 	 */
41*1031c584SApple OSS Distributions 	for (n = 0; n < NREQUESTS; ++n) {
42*1031c584SApple OSS Distributions 		aio_fsync((n & 1) ? O_SYNC : O_DSYNC, &ap[n]);
43*1031c584SApple OSS Distributions 	}
44*1031c584SApple OSS Distributions 
45*1031c584SApple OSS Distributions 	exit(0);
46*1031c584SApple OSS Distributions }
47*1031c584SApple OSS Distributions 
48*1031c584SApple OSS Distributions T_DECL(lio_listio_race_63669270, "test for the lightspeed/unc0ver UaF")
49*1031c584SApple OSS Distributions {
50*1031c584SApple OSS Distributions 	pid_t child;
51*1031c584SApple OSS Distributions 	int fd;
52*1031c584SApple OSS Distributions 	char path[128];
53*1031c584SApple OSS Distributions 	uint64_t end = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) + 10 * NSEC_PER_SEC;
54*1031c584SApple OSS Distributions 
55*1031c584SApple OSS Distributions 	/* we need a valid fd: */
56*1031c584SApple OSS Distributions 	strcpy(path, "/tmp/aio_fsync_uaf.XXXXXX");
57*1031c584SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(fd = mkstemp(path), "mkstemp");
58*1031c584SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(unlink(path), "unlink");
59*1031c584SApple OSS Distributions 
60*1031c584SApple OSS Distributions 	T_LOG("starting...");
61*1031c584SApple OSS Distributions 	do {
62*1031c584SApple OSS Distributions 		switch ((child = fork())) {
63*1031c584SApple OSS Distributions 		case -1: T_FAIL("fork");
64*1031c584SApple OSS Distributions 		case 0: attempt(fd);
65*1031c584SApple OSS Distributions 		}
66*1031c584SApple OSS Distributions 
67*1031c584SApple OSS Distributions 		T_QUIET; T_EXPECT_POSIX_SUCCESS(waitpid(child, NULL, 0), "waitpid");
68*1031c584SApple OSS Distributions 	} while (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) < end);
69*1031c584SApple OSS Distributions 
70*1031c584SApple OSS Distributions 	T_PASS("the system didn't panic");
71*1031c584SApple OSS Distributions }
72