xref: /xnu-10002.81.5/tests/vm/vm_tainted_executable.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2023 Apple Computer, Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #include <darwintest.h>
30*5e3eaea3SApple OSS Distributions 
31*5e3eaea3SApple OSS Distributions #include <fcntl.h>
32*5e3eaea3SApple OSS Distributions #include <stdlib.h>
33*5e3eaea3SApple OSS Distributions #include <spawn.h>
34*5e3eaea3SApple OSS Distributions #include <string.h>
35*5e3eaea3SApple OSS Distributions #include <unistd.h>
36*5e3eaea3SApple OSS Distributions #include <sys/codesign.h>
37*5e3eaea3SApple OSS Distributions #include <sys/mman.h>
38*5e3eaea3SApple OSS Distributions #include <sys/stat.h>
39*5e3eaea3SApple OSS Distributions #include <sys/wait.h>
40*5e3eaea3SApple OSS Distributions 
41*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
42*5e3eaea3SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
43*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
44*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"));
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions T_DECL(vm_tainted_executable, "Test that a tainted executable gets killed")
47*5e3eaea3SApple OSS Distributions {
48*5e3eaea3SApple OSS Distributions 	char tmp_path[] = "/tmp/hell0-XXXXXX";
49*5e3eaea3SApple OSS Distributions 	int fd1, fd2;
50*5e3eaea3SApple OSS Distributions 	struct stat fs;
51*5e3eaea3SApple OSS Distributions 	char *mapaddr1;
52*5e3eaea3SApple OSS Distributions 	size_t fsize;
53*5e3eaea3SApple OSS Distributions 	char *big_sp, *big_cp, *big_ep, *little_cp;
54*5e3eaea3SApple OSS Distributions 	size_t little_len;
55*5e3eaea3SApple OSS Distributions 	char *child_argv[2];
56*5e3eaea3SApple OSS Distributions 	pid_t child_pid;
57*5e3eaea3SApple OSS Distributions 	int child_status;
58*5e3eaea3SApple OSS Distributions 	int cs_status;
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions 	T_SETUPBEGIN;
61*5e3eaea3SApple OSS Distributions 	/* copy "./hello" to "/tmp/hell0" */
62*5e3eaea3SApple OSS Distributions 	fd1 = open("./hello", O_RDONLY);
63*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fd1, "open(./hello)");
64*5e3eaea3SApple OSS Distributions 	fd2 = mkstemp(tmp_path);
65*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fd2, "mkstemp(%s)", tmp_path);
66*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fstat(fd1, &fs), NULL);
67*5e3eaea3SApple OSS Distributions 	fsize = (size_t)fs.st_size;
68*5e3eaea3SApple OSS Distributions 	mapaddr1 = mmap(NULL, fsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd1, 0);
69*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(mapaddr1, NULL);
70*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(write(fd2, mapaddr1, fsize), NULL);
71*5e3eaea3SApple OSS Distributions 	/* change "hello, world!" to "hell0, world!" */
72*5e3eaea3SApple OSS Distributions 	big_sp = &mapaddr1[0]; /* start pointer in "big" byte string */
73*5e3eaea3SApple OSS Distributions 	big_ep = &mapaddr1[fsize]; /* end pointer in "big" byte string */
74*5e3eaea3SApple OSS Distributions 	little_cp = "hello, world!"; /* little byte string */
75*5e3eaea3SApple OSS Distributions 	little_len = strlen(little_cp); /* length of little byte string */
76*5e3eaea3SApple OSS Distributions 	big_cp = big_sp; /* start pointer in "big" byte string */
77*5e3eaea3SApple OSS Distributions 	for (;;) {
78*5e3eaea3SApple OSS Distributions 		char zero = '0';
79*5e3eaea3SApple OSS Distributions 		big_cp = memmem(big_cp, (size_t)(big_ep - big_cp),
80*5e3eaea3SApple OSS Distributions 		    little_cp, little_len);
81*5e3eaea3SApple OSS Distributions 		if (big_cp == NULL) {
82*5e3eaea3SApple OSS Distributions 			break;
83*5e3eaea3SApple OSS Distributions 		}
84*5e3eaea3SApple OSS Distributions 		T_LOG("found string at offset 0x%llx", (off_t) (big_cp - big_sp));
85*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(pwrite(fd2, &zero, 1,
86*5e3eaea3SApple OSS Distributions 		    (big_cp - big_sp + 4)), NULL);
87*5e3eaea3SApple OSS Distributions 		big_cp += little_len;
88*5e3eaea3SApple OSS Distributions 	}
89*5e3eaea3SApple OSS Distributions 	/* make the new binary "r-x" */
90*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(fchmod(fd2, S_IRUSR | S_IXUSR), NULL);
91*5e3eaea3SApple OSS Distributions 	/* cleanup */
92*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd1), NULL);
93*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd2), NULL);
94*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(munmap(mapaddr1, fsize), NULL);
95*5e3eaea3SApple OSS Distributions 	T_SETUPEND;
96*5e3eaea3SApple OSS Distributions 	/* spawn the newly-tainted binary */
97*5e3eaea3SApple OSS Distributions 	T_LOG("launching '%s'", tmp_path);
98*5e3eaea3SApple OSS Distributions 	child_argv[0] = tmp_path;
99*5e3eaea3SApple OSS Distributions 	child_argv[1] = NULL;
100*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_spawn(&child_pid, tmp_path, NULL, NULL, child_argv, NULL), NULL);
101*5e3eaea3SApple OSS Distributions 	/* check our code-signing policy, assuming the child has same policy */
102*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(csops(getpid(), CS_OPS_STATUS, &cs_status, sizeof(cs_status)), NULL);
103*5e3eaea3SApple OSS Distributions 	T_LOG("parent %d cs status 0x%x CS_KILL:%s", getpid(), cs_status,
104*5e3eaea3SApple OSS Distributions 	    (cs_status & CS_KILL) ? "yes" : "no");
105*5e3eaea3SApple OSS Distributions 	/* get child's exit status */
106*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(child_pid, &child_status, 0), NULL);
107*5e3eaea3SApple OSS Distributions 	T_LOG("child %d exit status 0x%x", child_pid, child_status);
108*5e3eaea3SApple OSS Distributions 	/* we no longer need our modified binary */
109*5e3eaea3SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(unlink(tmp_path), NULL);
110*5e3eaea3SApple OSS Distributions 	if (cs_status & CS_KILL) {
111*5e3eaea3SApple OSS Distributions 		/* check that child got SIGKILL */
112*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_TRUE(WIFSIGNALED(child_status), NULL);
113*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_TRUE(WTERMSIG(child_status) == SIGKILL, NULL);
114*5e3eaea3SApple OSS Distributions 		T_PASS("enforced process launched from modified binary got SIGKILL");
115*5e3eaea3SApple OSS Distributions 	} else {
116*5e3eaea3SApple OSS Distributions 		/* check that child exited with 0 */
117*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_TRUE(WIFEXITED(child_status), NULL);
118*5e3eaea3SApple OSS Distributions 		T_QUIET; T_ASSERT_TRUE(WEXITSTATUS(child_status) == 0, NULL);
119*5e3eaea3SApple OSS Distributions 		T_PASS("non-enforced process launched from modified binary exited with 0");
120*5e3eaea3SApple OSS Distributions 	}
121*5e3eaea3SApple OSS Distributions }
122