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