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