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