xref: /xnu-8020.121.3/tests/test_note_exec.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions #include <assert.h>
2*fdd8201dSApple OSS Distributions #include <stdio.h>
3*fdd8201dSApple OSS Distributions #include <pthread.h>
4*fdd8201dSApple OSS Distributions #include <signal.h>
5*fdd8201dSApple OSS Distributions #include <unistd.h>
6*fdd8201dSApple OSS Distributions #include <errno.h>
7*fdd8201dSApple OSS Distributions #include <string.h>
8*fdd8201dSApple OSS Distributions #include <sys/wait.h>
9*fdd8201dSApple OSS Distributions #include <sys/types.h>
10*fdd8201dSApple OSS Distributions #include <sys/time.h>
11*fdd8201dSApple OSS Distributions #include <sys/event.h>
12*fdd8201dSApple OSS Distributions #include <sys/ptrace.h>
13*fdd8201dSApple OSS Distributions #include <sys/proc.h>
14*fdd8201dSApple OSS Distributions #include <stdlib.h>
15*fdd8201dSApple OSS Distributions #include <System/sys/codesign.h>
16*fdd8201dSApple OSS Distributions #include <darwintest.h>
17*fdd8201dSApple OSS Distributions 
18*fdd8201dSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.note_exec"),
19*fdd8201dSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
20*fdd8201dSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("spawn"));
21*fdd8201dSApple OSS Distributions 
22*fdd8201dSApple OSS Distributions static int kq;
23*fdd8201dSApple OSS Distributions static int pid;
24*fdd8201dSApple OSS Distributions 
25*fdd8201dSApple OSS Distributions static void
do_exec(void)26*fdd8201dSApple OSS Distributions do_exec(void)
27*fdd8201dSApple OSS Distributions {
28*fdd8201dSApple OSS Distributions 	char echo_arg[50] = "";
29*fdd8201dSApple OSS Distributions 
30*fdd8201dSApple OSS Distributions 	snprintf(echo_arg, sizeof(echo_arg), "Child[%d] says hello after exec", getpid());
31*fdd8201dSApple OSS Distributions 
32*fdd8201dSApple OSS Distributions 	char * new_argv[] = {
33*fdd8201dSApple OSS Distributions 		"/bin/echo",
34*fdd8201dSApple OSS Distributions 		echo_arg,
35*fdd8201dSApple OSS Distributions 		NULL
36*fdd8201dSApple OSS Distributions 	};
37*fdd8201dSApple OSS Distributions 
38*fdd8201dSApple OSS Distributions 	int ret = execv(new_argv[0], new_argv);
39*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "execv()");
40*fdd8201dSApple OSS Distributions }
41*fdd8201dSApple OSS Distributions 
42*fdd8201dSApple OSS Distributions static void *
thread_wait_exec(void * arg __unused)43*fdd8201dSApple OSS Distributions thread_wait_exec(void *arg __unused)
44*fdd8201dSApple OSS Distributions {
45*fdd8201dSApple OSS Distributions 	int ret;
46*fdd8201dSApple OSS Distributions 	struct kevent64_s kev;
47*fdd8201dSApple OSS Distributions 	int csret;
48*fdd8201dSApple OSS Distributions 	uint32_t status = 0;
49*fdd8201dSApple OSS Distributions 
50*fdd8201dSApple OSS Distributions 	while (1) {
51*fdd8201dSApple OSS Distributions 		ret = kevent64(kq, NULL, 0, &kev, 1, 0, NULL);
52*fdd8201dSApple OSS Distributions 		if (ret == -1) {
53*fdd8201dSApple OSS Distributions 			if (errno == EINTR) {
54*fdd8201dSApple OSS Distributions 				continue;
55*fdd8201dSApple OSS Distributions 			}
56*fdd8201dSApple OSS Distributions 		}
57*fdd8201dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kevent64()");
58*fdd8201dSApple OSS Distributions 		break;
59*fdd8201dSApple OSS Distributions 	}
60*fdd8201dSApple OSS Distributions 
61*fdd8201dSApple OSS Distributions 	/* Try to get the csops of child before we print anything */
62*fdd8201dSApple OSS Distributions 	csret = csops(pid, CS_OPS_STATUS, &status, sizeof(status));
63*fdd8201dSApple OSS Distributions 	if (csret != 0) {
64*fdd8201dSApple OSS Distributions 		T_QUIET; T_LOG("Child exited before parent could call csops. The race didn't happen");
65*fdd8201dSApple OSS Distributions 		return NULL;
66*fdd8201dSApple OSS Distributions 	}
67*fdd8201dSApple OSS Distributions 
68*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 1, "kevent64 returned 1 event as expected");
69*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ((int)kev.filter, EVFILT_PROC, "EVFILT_PROC event received");
70*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ((int)kev.udata, pid, "EVFILT_PROC event received for child pid");
71*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ((kev.fflags & NOTE_EXEC), NOTE_EXEC, "NOTE_EXEC event received");
72*fdd8201dSApple OSS Distributions 
73*fdd8201dSApple OSS Distributions 	/* Check that the platform binary bit is set */
74*fdd8201dSApple OSS Distributions 	T_EXPECT_BITS_SET(status, CS_PLATFORM_BINARY, "CS_PLATFORM_BINARY should be set on child");
75*fdd8201dSApple OSS Distributions 
76*fdd8201dSApple OSS Distributions 	return NULL;
77*fdd8201dSApple OSS Distributions }
78*fdd8201dSApple OSS Distributions 
79*fdd8201dSApple OSS Distributions static void
run_test(void)80*fdd8201dSApple OSS Distributions run_test(void)
81*fdd8201dSApple OSS Distributions {
82*fdd8201dSApple OSS Distributions 	struct kevent64_s kev;
83*fdd8201dSApple OSS Distributions 	int ret;
84*fdd8201dSApple OSS Distributions 	int fd[2];
85*fdd8201dSApple OSS Distributions 
86*fdd8201dSApple OSS Distributions 	ret = pipe(fd);
87*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pipe()");
88*fdd8201dSApple OSS Distributions 	close(fd[0]);
89*fdd8201dSApple OSS Distributions 
90*fdd8201dSApple OSS Distributions 	T_QUIET; T_LOG("Forking child");
91*fdd8201dSApple OSS Distributions 
92*fdd8201dSApple OSS Distributions 	pid = fork();
93*fdd8201dSApple OSS Distributions 
94*fdd8201dSApple OSS Distributions 	if (pid == 0) {
95*fdd8201dSApple OSS Distributions 		char buf[10];
96*fdd8201dSApple OSS Distributions 
97*fdd8201dSApple OSS Distributions 		close(fd[1]);
98*fdd8201dSApple OSS Distributions 		ret = (int)read(fd[0], buf, sizeof(buf));
99*fdd8201dSApple OSS Distributions 		close(fd[0]);
100*fdd8201dSApple OSS Distributions 
101*fdd8201dSApple OSS Distributions 		do_exec();
102*fdd8201dSApple OSS Distributions 		exit(1);
103*fdd8201dSApple OSS Distributions 	}
104*fdd8201dSApple OSS Distributions 
105*fdd8201dSApple OSS Distributions 	T_QUIET; T_LOG("Setting up NOTE_EXEC Handler for child pid %d", pid);
106*fdd8201dSApple OSS Distributions 	kq = kqueue();
107*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kq, "kqueue()");
108*fdd8201dSApple OSS Distributions 
109*fdd8201dSApple OSS Distributions 	EV_SET64(&kev, pid, EVFILT_PROC, EV_ADD | EV_ENABLE,
110*fdd8201dSApple OSS Distributions 	    NOTE_EXEC, 0, pid, 0, 0);
111*fdd8201dSApple OSS Distributions 	ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL);
112*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kevent64()");
113*fdd8201dSApple OSS Distributions 
114*fdd8201dSApple OSS Distributions 	pthread_t thread;
115*fdd8201dSApple OSS Distributions 	ret = pthread_create(&thread, NULL, thread_wait_exec, NULL);
116*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create()");
117*fdd8201dSApple OSS Distributions 
118*fdd8201dSApple OSS Distributions 	T_QUIET; T_LOG("Signalling child to call exec");
119*fdd8201dSApple OSS Distributions 	close(fd[1]);
120*fdd8201dSApple OSS Distributions 
121*fdd8201dSApple OSS Distributions 	T_QUIET; T_LOG("Waiting for child to exit");
122*fdd8201dSApple OSS Distributions 	pid = waitpid(pid, NULL, 0);
123*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "waitpid()");
124*fdd8201dSApple OSS Distributions 
125*fdd8201dSApple OSS Distributions 	T_QUIET; T_LOG("Waiting for note exec thread to exit");
126*fdd8201dSApple OSS Distributions 	ret = pthread_join(thread, NULL);
127*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_join()");
128*fdd8201dSApple OSS Distributions 
129*fdd8201dSApple OSS Distributions 	close(kq);
130*fdd8201dSApple OSS Distributions }
131*fdd8201dSApple OSS Distributions 
132*fdd8201dSApple OSS Distributions T_DECL(test_note_exec, "test NOTE_EXEC race with setting csops") {
133*fdd8201dSApple OSS Distributions 	T_QUIET; T_LOG("Testing race for NOTE_EXEC with csops");
134*fdd8201dSApple OSS Distributions 
135*fdd8201dSApple OSS Distributions 	for (int i = 0; i < 100; i++) {
136*fdd8201dSApple OSS Distributions 		T_QUIET; T_LOG("Running iteration %d", i);
137*fdd8201dSApple OSS Distributions 		run_test();
138*fdd8201dSApple OSS Distributions 	}
139*fdd8201dSApple OSS Distributions 	T_END;
140*fdd8201dSApple OSS Distributions }
141