1*e3723e1fSApple OSS Distributions #include <darwintest.h>
2*e3723e1fSApple OSS Distributions #include <darwintest_utils.h>
3*e3723e1fSApple OSS Distributions
4*e3723e1fSApple OSS Distributions #include <sys/types.h>
5*e3723e1fSApple OSS Distributions #include <sys/sysctl.h>
6*e3723e1fSApple OSS Distributions #include <mach/mach.h>
7*e3723e1fSApple OSS Distributions #include <mach/mach_vm.h>
8*e3723e1fSApple OSS Distributions #include <mach/vm_types.h>
9*e3723e1fSApple OSS Distributions #include <sys/mman.h>
10*e3723e1fSApple OSS Distributions #include <unistd.h>
11*e3723e1fSApple OSS Distributions #include <TargetConditionals.h>
12*e3723e1fSApple OSS Distributions
13*e3723e1fSApple OSS Distributions T_GLOBAL_META(
14*e3723e1fSApple OSS Distributions T_META_NAMESPACE("xnu.vm"),
15*e3723e1fSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
16*e3723e1fSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM")
17*e3723e1fSApple OSS Distributions );
18*e3723e1fSApple OSS Distributions
19*e3723e1fSApple OSS Distributions struct child_rc {
20*e3723e1fSApple OSS Distributions int ret;
21*e3723e1fSApple OSS Distributions int sig;
22*e3723e1fSApple OSS Distributions };
23*e3723e1fSApple OSS Distributions
24*e3723e1fSApple OSS Distributions static struct child_rc
25*e3723e1fSApple OSS Distributions fork_child_test(void (^block)(void))
26*e3723e1fSApple OSS Distributions {
27*e3723e1fSApple OSS Distributions struct child_rc rc = { };
28*e3723e1fSApple OSS Distributions pid_t child_pid;
29*e3723e1fSApple OSS Distributions
30*e3723e1fSApple OSS Distributions child_pid = fork();
31*e3723e1fSApple OSS Distributions
32*e3723e1fSApple OSS Distributions if (child_pid == 0) {
33*e3723e1fSApple OSS Distributions block();
34*e3723e1fSApple OSS Distributions exit(0);
35*e3723e1fSApple OSS Distributions }
36*e3723e1fSApple OSS Distributions
37*e3723e1fSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "fork process");
38*e3723e1fSApple OSS Distributions
39*e3723e1fSApple OSS Distributions /* wait for child process to exit */
40*e3723e1fSApple OSS Distributions dt_waitpid(child_pid, &rc.ret, &rc.sig, 30);
41*e3723e1fSApple OSS Distributions return rc;
42*e3723e1fSApple OSS Distributions }
43*e3723e1fSApple OSS Distributions
44*e3723e1fSApple OSS Distributions static mach_vm_address_t
get_permanent_mapping(mach_vm_size_t size)45*e3723e1fSApple OSS Distributions get_permanent_mapping(mach_vm_size_t size)
46*e3723e1fSApple OSS Distributions {
47*e3723e1fSApple OSS Distributions kern_return_t kr;
48*e3723e1fSApple OSS Distributions mach_vm_address_t addr;
49*e3723e1fSApple OSS Distributions
50*e3723e1fSApple OSS Distributions kr = mach_vm_allocate(mach_task_self(), &addr, size,
51*e3723e1fSApple OSS Distributions VM_FLAGS_ANYWHERE | VM_FLAGS_PERMANENT);
52*e3723e1fSApple OSS Distributions
53*e3723e1fSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_vm_allocate(%lld, PERMANENT) == %p",
54*e3723e1fSApple OSS Distributions size, (void *)addr);
55*e3723e1fSApple OSS Distributions
56*e3723e1fSApple OSS Distributions *(int *)addr = 42;
57*e3723e1fSApple OSS Distributions
58*e3723e1fSApple OSS Distributions kr = mach_vm_protect(mach_task_self(), addr, size, FALSE, VM_PROT_READ);
59*e3723e1fSApple OSS Distributions
60*e3723e1fSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_vm_protect(PERMANENT, READ)");
61*e3723e1fSApple OSS Distributions
62*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
63*e3723e1fSApple OSS Distributions
64*e3723e1fSApple OSS Distributions return addr;
65*e3723e1fSApple OSS Distributions }
66*e3723e1fSApple OSS Distributions
67*e3723e1fSApple OSS Distributions T_DECL(permanent_mapping, "check permanent mappings semantics", T_META_TAG_VM_PREFERRED)
68*e3723e1fSApple OSS Distributions {
69*e3723e1fSApple OSS Distributions kern_return_t kr;
70*e3723e1fSApple OSS Distributions mach_vm_size_t size = 1 << 20;
71*e3723e1fSApple OSS Distributions struct child_rc rc;
72*e3723e1fSApple OSS Distributions
73*e3723e1fSApple OSS Distributions T_LOG("try to bypass permanent mappings with VM_FLAGS_OVERWRITE");
74*e3723e1fSApple OSS Distributions rc = fork_child_test(^{
75*e3723e1fSApple OSS Distributions mach_vm_address_t addr, addr2;
76*e3723e1fSApple OSS Distributions kern_return_t kr2;
77*e3723e1fSApple OSS Distributions
78*e3723e1fSApple OSS Distributions addr = get_permanent_mapping(size);
79*e3723e1fSApple OSS Distributions
80*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
81*e3723e1fSApple OSS Distributions
82*e3723e1fSApple OSS Distributions addr2 = addr;
83*e3723e1fSApple OSS Distributions kr2 = mach_vm_allocate(mach_task_self(), &addr2, size,
84*e3723e1fSApple OSS Distributions VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE);
85*e3723e1fSApple OSS Distributions
86*e3723e1fSApple OSS Distributions /*
87*e3723e1fSApple OSS Distributions * because the permanent mapping wasn't removed,
88*e3723e1fSApple OSS Distributions * we should get an error.
89*e3723e1fSApple OSS Distributions */
90*e3723e1fSApple OSS Distributions T_ASSERT_MACH_ERROR(kr2, KERN_NO_SPACE,
91*e3723e1fSApple OSS Distributions "mach_vm_allocate(VM_FLAGS_OVERWRITE)");
92*e3723e1fSApple OSS Distributions
93*e3723e1fSApple OSS Distributions /*
94*e3723e1fSApple OSS Distributions * because the permanent mapping was neutered,
95*e3723e1fSApple OSS Distributions * accessing it should crash.
96*e3723e1fSApple OSS Distributions */
97*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
98*e3723e1fSApple OSS Distributions });
99*e3723e1fSApple OSS Distributions T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS");
100*e3723e1fSApple OSS Distributions
101*e3723e1fSApple OSS Distributions T_LOG("try to bypass permanent mappings with a VM_PROT_COPY mprotect");
102*e3723e1fSApple OSS Distributions rc = fork_child_test(^{
103*e3723e1fSApple OSS Distributions kern_return_t kr2;
104*e3723e1fSApple OSS Distributions mach_vm_address_t addr;
105*e3723e1fSApple OSS Distributions
106*e3723e1fSApple OSS Distributions addr = get_permanent_mapping(size);
107*e3723e1fSApple OSS Distributions
108*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
109*e3723e1fSApple OSS Distributions
110*e3723e1fSApple OSS Distributions kr2 = mach_vm_protect(mach_task_self(), addr, size, TRUE,
111*e3723e1fSApple OSS Distributions VM_PROT_COPY | VM_PROT_DEFAULT);
112*e3723e1fSApple OSS Distributions
113*e3723e1fSApple OSS Distributions /*
114*e3723e1fSApple OSS Distributions * because the permanent mapping wasn't removed,
115*e3723e1fSApple OSS Distributions * we should get an error.
116*e3723e1fSApple OSS Distributions */
117*e3723e1fSApple OSS Distributions T_ASSERT_MACH_ERROR(kr2, KERN_NO_SPACE,
118*e3723e1fSApple OSS Distributions "mach_vm_protect(VM_PROT_COPY)");
119*e3723e1fSApple OSS Distributions
120*e3723e1fSApple OSS Distributions /*
121*e3723e1fSApple OSS Distributions * because the permanent mapping was neutered,
122*e3723e1fSApple OSS Distributions * accessing it should crash.
123*e3723e1fSApple OSS Distributions */
124*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
125*e3723e1fSApple OSS Distributions });
126*e3723e1fSApple OSS Distributions T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS");
127*e3723e1fSApple OSS Distributions
128*e3723e1fSApple OSS Distributions T_LOG("try to bypass permanent mappings with a vm_remap");
129*e3723e1fSApple OSS Distributions rc = fork_child_test(^{
130*e3723e1fSApple OSS Distributions kern_return_t kr2;
131*e3723e1fSApple OSS Distributions mach_vm_address_t addr, remap_addr, addr2;
132*e3723e1fSApple OSS Distributions vm_prot_t cur_prot, max_prot;
133*e3723e1fSApple OSS Distributions
134*e3723e1fSApple OSS Distributions addr = get_permanent_mapping(size);
135*e3723e1fSApple OSS Distributions
136*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
137*e3723e1fSApple OSS Distributions
138*e3723e1fSApple OSS Distributions addr2 = 0;
139*e3723e1fSApple OSS Distributions kr2 = mach_vm_allocate(mach_task_self(), &addr2, size,
140*e3723e1fSApple OSS Distributions VM_FLAGS_ANYWHERE);
141*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr2, "vm_allocate()");
142*e3723e1fSApple OSS Distributions
143*e3723e1fSApple OSS Distributions remap_addr = addr;
144*e3723e1fSApple OSS Distributions kr2 = mach_vm_remap(mach_task_self(), &remap_addr, size, 0,
145*e3723e1fSApple OSS Distributions VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
146*e3723e1fSApple OSS Distributions mach_task_self(), addr2, TRUE,
147*e3723e1fSApple OSS Distributions &cur_prot, &max_prot, VM_INHERIT_DEFAULT);
148*e3723e1fSApple OSS Distributions
149*e3723e1fSApple OSS Distributions /*
150*e3723e1fSApple OSS Distributions * because the permanent mapping wasn't removed,
151*e3723e1fSApple OSS Distributions * we should get an error.
152*e3723e1fSApple OSS Distributions */
153*e3723e1fSApple OSS Distributions T_ASSERT_MACH_ERROR(kr2, KERN_NO_SPACE,
154*e3723e1fSApple OSS Distributions "mach_vm_remap()");
155*e3723e1fSApple OSS Distributions
156*e3723e1fSApple OSS Distributions /*
157*e3723e1fSApple OSS Distributions * because the permanent mapping was neutered,
158*e3723e1fSApple OSS Distributions * accessing it should crash.
159*e3723e1fSApple OSS Distributions */
160*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
161*e3723e1fSApple OSS Distributions });
162*e3723e1fSApple OSS Distributions T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS");
163*e3723e1fSApple OSS Distributions
164*e3723e1fSApple OSS Distributions T_LOG("try to bypass permanent mappings with a vm_deallocate");
165*e3723e1fSApple OSS Distributions rc = fork_child_test(^{
166*e3723e1fSApple OSS Distributions kern_return_t kr2;
167*e3723e1fSApple OSS Distributions mach_vm_address_t addr;
168*e3723e1fSApple OSS Distributions
169*e3723e1fSApple OSS Distributions addr = get_permanent_mapping(size);
170*e3723e1fSApple OSS Distributions
171*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
172*e3723e1fSApple OSS Distributions
173*e3723e1fSApple OSS Distributions kr2 = mach_vm_deallocate(mach_task_self(), addr, size);
174*e3723e1fSApple OSS Distributions
175*e3723e1fSApple OSS Distributions /*
176*e3723e1fSApple OSS Distributions * the permanent mapping wasn't removed but was made
177*e3723e1fSApple OSS Distributions * inaccessible; we should not get an error.
178*e3723e1fSApple OSS Distributions */
179*e3723e1fSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr2, "mach_vm_deallocate()");
180*e3723e1fSApple OSS Distributions
181*e3723e1fSApple OSS Distributions /*
182*e3723e1fSApple OSS Distributions * because the permanent mapping was neutered,
183*e3723e1fSApple OSS Distributions * accessing it should crash.
184*e3723e1fSApple OSS Distributions */
185*e3723e1fSApple OSS Distributions T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote");
186*e3723e1fSApple OSS Distributions });
187*e3723e1fSApple OSS Distributions T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS");
188*e3723e1fSApple OSS Distributions }
189