xref: /xnu-8796.121.2/tests/vm_test_code_signing_helper.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions #include <TargetConditionals.h>
2*c54f35caSApple OSS Distributions #include <errno.h>
3*c54f35caSApple OSS Distributions #include <stdio.h>
4*c54f35caSApple OSS Distributions #include <stdlib.h>
5*c54f35caSApple OSS Distributions #include <string.h>
6*c54f35caSApple OSS Distributions #include <unistd.h>
7*c54f35caSApple OSS Distributions 
8*c54f35caSApple OSS Distributions #if __has_include(<ptrauth.h>)
9*c54f35caSApple OSS Distributions #include <ptrauth.h>
10*c54f35caSApple OSS Distributions #endif
11*c54f35caSApple OSS Distributions 
12*c54f35caSApple OSS Distributions #include <sys/mman.h>
13*c54f35caSApple OSS Distributions #include <sys/syslimits.h>
14*c54f35caSApple OSS Distributions 
15*c54f35caSApple OSS Distributions char *cmdname;
16*c54f35caSApple OSS Distributions 
17*c54f35caSApple OSS Distributions int
main(int argc,char * argv[])18*c54f35caSApple OSS Distributions main(
19*c54f35caSApple OSS Distributions 	int argc,
20*c54f35caSApple OSS Distributions 	char *argv[])
21*c54f35caSApple OSS Distributions {
22*c54f35caSApple OSS Distributions 	uint32_t page_size;
23*c54f35caSApple OSS Distributions 	void *page;
24*c54f35caSApple OSS Distributions 	int ch;
25*c54f35caSApple OSS Distributions 	int opt_interactive;
26*c54f35caSApple OSS Distributions 
27*c54f35caSApple OSS Distributions 	cmdname = argv[0];
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions 	opt_interactive = 0;
30*c54f35caSApple OSS Distributions 	while ((ch = getopt(argc, argv, "i")) != -1) {
31*c54f35caSApple OSS Distributions 		switch (ch) {
32*c54f35caSApple OSS Distributions 		case 'i':
33*c54f35caSApple OSS Distributions 			opt_interactive = 1;
34*c54f35caSApple OSS Distributions 			break;
35*c54f35caSApple OSS Distributions 		case '?':
36*c54f35caSApple OSS Distributions 		default:
37*c54f35caSApple OSS Distributions 			fprintf(stdout,
38*c54f35caSApple OSS Distributions 			    "Usage: %s [-i]\n"
39*c54f35caSApple OSS Distributions 			    "\t-i: interactive\n",
40*c54f35caSApple OSS Distributions 			    cmdname);
41*c54f35caSApple OSS Distributions 			exit(1);
42*c54f35caSApple OSS Distributions 		}
43*c54f35caSApple OSS Distributions 	}
44*c54f35caSApple OSS Distributions 
45*c54f35caSApple OSS Distributions 	page_size = getpagesize();
46*c54f35caSApple OSS Distributions 	page = mmap(NULL, page_size, PROT_READ | PROT_EXEC, MAP_ANON | MAP_SHARED, -1, 0);
47*c54f35caSApple OSS Distributions 	if (!page) {
48*c54f35caSApple OSS Distributions 		fprintf(stderr, "%s:%d mmap() error %d (%s)\n",
49*c54f35caSApple OSS Distributions 		    cmdname, __LINE__,
50*c54f35caSApple OSS Distributions 		    errno, strerror(errno));
51*c54f35caSApple OSS Distributions 		exit(1);
52*c54f35caSApple OSS Distributions 	}
53*c54f35caSApple OSS Distributions 	if (opt_interactive) {
54*c54f35caSApple OSS Distributions 		fprintf(stdout, "allocated page at %p\n",
55*c54f35caSApple OSS Distributions 		    page);
56*c54f35caSApple OSS Distributions 	}
57*c54f35caSApple OSS Distributions 
58*c54f35caSApple OSS Distributions 	if (mprotect(page, page_size, PROT_READ | PROT_WRITE) != 0) {
59*c54f35caSApple OSS Distributions 		fprintf(stderr, "%s:%d mprotect(RW) error %d (%s)\n",
60*c54f35caSApple OSS Distributions 		    cmdname, __LINE__,
61*c54f35caSApple OSS Distributions 		    errno, strerror(errno));
62*c54f35caSApple OSS Distributions 		exit(1);
63*c54f35caSApple OSS Distributions 	}
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions #if __arm64__
66*c54f35caSApple OSS Distributions 	// arm64 chdir() syscall
67*c54f35caSApple OSS Distributions 	char chdir_code[] =  {
68*c54f35caSApple OSS Distributions 		0x90, 0x01, 0x80, 0xd2, // movz   x16, #0xc
69*c54f35caSApple OSS Distributions 		0x01, 0x10, 0x00, 0xd4, // svc    #0x80
70*c54f35caSApple OSS Distributions 		0xc0, 0x03, 0x5f, 0xd6, // ret
71*c54f35caSApple OSS Distributions 	};
72*c54f35caSApple OSS Distributions #elif __x86_64__
73*c54f35caSApple OSS Distributions 	// x86_64 chdir() syscall
74*c54f35caSApple OSS Distributions 	char chdir_code[] = {
75*c54f35caSApple OSS Distributions 		0xb8, 0x0c, 0x00, 0x00, 0x02,   // movl   $0x200000c, %eax
76*c54f35caSApple OSS Distributions 		0x49, 0x89, 0xca,               // movq   %rcx, %r10
77*c54f35caSApple OSS Distributions 		0x0f, 0x05,                     // syscall
78*c54f35caSApple OSS Distributions 		0xc3,                           // retq
79*c54f35caSApple OSS Distributions 	};
80*c54f35caSApple OSS Distributions #elif __i386__
81*c54f35caSApple OSS Distributions 	// i386 chdir() syscall
82*c54f35caSApple OSS Distributions 	char chdir_code[] = {
83*c54f35caSApple OSS Distributions 		0x90,   // nop
84*c54f35caSApple OSS Distributions 		0xc3,   // retq
85*c54f35caSApple OSS Distributions 	};
86*c54f35caSApple OSS Distributions #endif
87*c54f35caSApple OSS Distributions 	memcpy(page, chdir_code, sizeof chdir_code);
88*c54f35caSApple OSS Distributions 
89*c54f35caSApple OSS Distributions 	if (opt_interactive) {
90*c54f35caSApple OSS Distributions 		fprintf(stdout,
91*c54f35caSApple OSS Distributions 		    "changed page protection to r/w and copied code at %p\n",
92*c54f35caSApple OSS Distributions 		    page);
93*c54f35caSApple OSS Distributions 		fprintf(stdout, "pausing...\n");
94*c54f35caSApple OSS Distributions 		fflush(stdout);
95*c54f35caSApple OSS Distributions 		getchar();
96*c54f35caSApple OSS Distributions 	}
97*c54f35caSApple OSS Distributions 
98*c54f35caSApple OSS Distributions 	if (mprotect(page, page_size, PROT_READ | PROT_EXEC) != 0) {
99*c54f35caSApple OSS Distributions 		fprintf(stderr, "%s:%d mprotect(RX) error %d (%s)\n",
100*c54f35caSApple OSS Distributions 		    cmdname, __LINE__,
101*c54f35caSApple OSS Distributions 		    errno, strerror(errno));
102*c54f35caSApple OSS Distributions 		exit(1);
103*c54f35caSApple OSS Distributions 	}
104*c54f35caSApple OSS Distributions 
105*c54f35caSApple OSS Distributions 	if (opt_interactive) {
106*c54f35caSApple OSS Distributions 		fprintf(stdout,
107*c54f35caSApple OSS Distributions 		    "changed page protection to r/x at %p\n",
108*c54f35caSApple OSS Distributions 		    page);
109*c54f35caSApple OSS Distributions 		fprintf(stdout, "pausing...\n");
110*c54f35caSApple OSS Distributions 		fflush(stdout);
111*c54f35caSApple OSS Distributions 		getchar();
112*c54f35caSApple OSS Distributions 	}
113*c54f35caSApple OSS Distributions 
114*c54f35caSApple OSS Distributions 	char origdir[PATH_MAX];
115*c54f35caSApple OSS Distributions 	getcwd(origdir, sizeof(origdir) - 1);
116*c54f35caSApple OSS Distributions 
117*c54f35caSApple OSS Distributions 	chdir("/");
118*c54f35caSApple OSS Distributions 	if (opt_interactive) {
119*c54f35caSApple OSS Distributions 		fprintf(stdout, "cwd before = %s\n", getwd(NULL));
120*c54f35caSApple OSS Distributions 	}
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions 	void (*mychdir)(char *) = page;
123*c54f35caSApple OSS Distributions #if __has_feature(ptrauth_calls)
124*c54f35caSApple OSS Distributions 	mychdir = ptrauth_sign_unauthenticated(mychdir, ptrauth_key_function_pointer, 0);
125*c54f35caSApple OSS Distributions #endif
126*c54f35caSApple OSS Distributions 	mychdir(getenv("HOME"));
127*c54f35caSApple OSS Distributions 	if (opt_interactive) {
128*c54f35caSApple OSS Distributions 		fprintf(stdout, "cwd after = %s\n", getwd(NULL));
129*c54f35caSApple OSS Distributions 		fprintf(stdout, "pausing...\n");
130*c54f35caSApple OSS Distributions 		fflush(stdout);
131*c54f35caSApple OSS Distributions 		getchar();
132*c54f35caSApple OSS Distributions 	}
133*c54f35caSApple OSS Distributions 
134*c54f35caSApple OSS Distributions 	fprintf(stdout, "%s: WARNING: unsigned code was executed\n",
135*c54f35caSApple OSS Distributions 	    cmdname);
136*c54f35caSApple OSS Distributions 
137*c54f35caSApple OSS Distributions 	/* fail: unsigned code was executed */
138*c54f35caSApple OSS Distributions 	fprintf(stdout, "%s: FAIL\n", cmdname);
139*c54f35caSApple OSS Distributions 	exit(1);
140*c54f35caSApple OSS Distributions }
141