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