Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | #include <darwintest.h> #include <stdlib.h> #include <mach/mach_init.h> #include <mach/mach_vm.h> #include <mach/vm_map.h> T_GLOBAL_META( T_META_NAMESPACE("xnu.vm"), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("VM")); char *_prot_str[] = { /* 0 */ "---", /* 1 */ "r--", /* 2 */ "-w-", /* 3 */ "rw-", /* 4 */ "--x", /* 5 */ "r-x", /* 6 */ "-wx", /* 7 */ "rwx" }; char * prot_str(vm_prot_t prot) { return _prot_str[prot & VM_PROT_ALL]; } void print_region_info( mach_vm_address_t vmaddr, mach_vm_size_t vmsize, vm_region_submap_short_info_64_t ri, mach_vm_address_t vmaddr2, mach_vm_size_t vmsize2, vm_region_submap_short_info_64_t ri2) { T_LOG(" [ 0x%016llx - 0x%016llx ] size 0x%016llx prot 0x%x/0x%x %s/%s submap %d\n", (uint64_t)vmaddr, (uint64_t)(vmaddr + vmsize), (uint64_t)vmsize, ri->protection, ri->max_protection, prot_str(ri->protection), prot_str(ri->max_protection), ri->is_submap); if (ri2) { T_LOG("-> [ 0x%016llx - 0x%016llx ] size 0x%016llx prot 0x%x/0x%x %s/%s submap %d\n", (uint64_t)vmaddr2, (uint64_t)(vmaddr2 + vmsize2), (uint64_t)vmsize2, ri2->protection, ri2->max_protection, prot_str(ri2->protection), prot_str(ri2->max_protection), ri2->is_submap); } } static bool find_nested_read_only_mapping( mach_vm_address_t *vmaddr_p, mach_vm_size_t *vmsize_p, vm_region_submap_short_info_64_t ri) { kern_return_t kr; mach_vm_address_t vmaddr_sub; mach_vm_size_t vmsize_sub; natural_t depth; mach_msg_type_number_t count; T_LOG("===== Looking for read-only mapping in shared region"); /* find a read-only mapping in the shared region */ for (vmaddr_sub = 0;; vmaddr_sub += vmsize_sub) { depth = 1; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_sub, &vmsize_sub, &depth, (vm_region_recurse_info_t)ri, &count); T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); if (kr != KERN_SUCCESS) { /* end of address space */ T_FAIL("could not find shared region"); return false; } if (depth == 0) { /* not the shared region, keep looking */ continue; } if (ri->max_protection != VM_PROT_READ) { /* not read-only: keep looking */ continue; } T_PASS("Found read-only mapping in shared region at 0x%llx size 0x%llx\n", (uint64_t)vmaddr_sub, (uint64_t)vmsize_sub); *vmaddr_p = vmaddr_sub; *vmsize_p = vmsize_sub; return true; } return false; } T_DECL(vm_test_shreg_ro, "Tests that read-only shared-region mappings can't be overwritten", T_META_TAG_VM_PREFERRED) { kern_return_t kr; mach_vm_address_t vmaddr, vmaddr_sub, vmaddr_tmp, vmaddr_buf; mach_vm_size_t vmsize, vmsize_sub, vmsize_tmp; natural_t depth1, depth2; mach_msg_type_number_t count; vm_region_submap_short_info_data_64_t ri1, ri2; vm_prot_t cur_prot, max_prot; #if __x86_64__ T_SKIP("x86_64: read-only shared region mappings are not protected"); return; #endif /* __x86_64 */ #define ASSERT_UNCHANGED(clip_ok) \ do { \ if (clip_ok) { \ T_EXPECT_GE(vmaddr_tmp, vmaddr, \ "vmaddr clipped 0x%llx -> 0x%llx", \ (uint64_t)vmaddr, (uint64_t)vmaddr_tmp); \ T_EXPECT_LE(vmsize_tmp, vmsize, \ "vmsize clipped 0x%llx -> 0x%llx", \ (uint64_t)vmsize, (uint64_t)vmsize_tmp); \ } else { \ T_EXPECT_EQ(vmaddr_tmp, vmaddr, \ "vmaddr unchanged 0x%llx -> 0x%llx", \ (uint64_t)vmaddr, (uint64_t)vmaddr_tmp); \ T_EXPECT_EQ(vmsize_tmp, vmsize, \ "vmsize unchanged 0x%llx -> 0x%llx", \ (uint64_t)vmsize, (uint64_t)vmsize_tmp); \ } \ T_EXPECT_LE(ri2.protection, VM_PROT_READ, \ "should not become writable"); \ T_EXPECT_LE(ri2.max_protection, VM_PROT_READ, \ "should not become able to become writable"); \ } while (0) T_LOG("=========================================="); if (!find_nested_read_only_mapping(&vmaddr_sub, &vmsize_sub, &ri2)) { T_FAIL("could not find appropriate mapping"); return; } T_LOG("=========================================="); /* get top-level mapping protections */ vmaddr = vmaddr_sub; depth1 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr, &vmsize, &depth1, (vm_region_recurse_info_t)&ri1, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_sub, vmsize_sub, &ri2); /* vm_allocate(VM_FLAGS_OVERWRITE) on top of submap */ T_LOG("=========================================="); T_LOG("===== vm_allocate(VM_FLAGS_OVERWRITE) on nested mapping"); T_LOG("=========================================="); T_EXPECT_EQ(ri1.is_submap, 1, "mapping should be nested"); vmaddr_tmp = vmaddr_sub; kr = mach_vm_allocate(mach_task_self(), &vmaddr_tmp, vmsize_sub, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE); T_EXPECT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_allocate(OVERWRITE) fails with KERN_PROTECTION_FAILURE"); T_EXPECT_EQ(vmaddr_sub, vmaddr_tmp, "vmaddr is unchanged"); /* check protections again */ depth2 = 0; vmaddr_tmp = vmaddr_sub; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false); T_EXPECT_EQ(ri2.is_submap, 1, "mapping should still be nested"); /* vm_remap(VM_FLAGS_OVERWRITE) on top of submap */ T_LOG("=========================================="); T_LOG("===== vm_remap(VM_FLAGS_OVERWRITE) on nested mapping"); T_LOG("=========================================="); T_EXPECT_EQ(ri1.is_submap, 1, "mapping should be nested"); vmaddr_tmp = vmaddr_sub; kr = mach_vm_remap(mach_task_self(), &vmaddr_tmp, vmsize_sub, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, mach_task_self(), vmaddr_sub, TRUE, &cur_prot, &max_prot, VM_INHERIT_DEFAULT); T_EXPECT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_remap(OVERWRITE) fails with KERN_PROTECTION_FAILURE"); T_EXPECT_EQ(vmaddr_sub, vmaddr_tmp, "vmaddr is unchanged"); /* check protections again */ depth2 = 0; vmaddr_tmp = vmaddr_sub; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false); T_EXPECT_EQ(ri2.is_submap, 1, "mapping should still be nested"); /* vm_protect(VM_PROT_COPY) on submap mapping */ T_LOG("=========================================="); T_LOG("===== vm_protect(VM_PROT_COPY) on nested mapping"); T_LOG("=========================================="); ri1 = ri2; T_EXPECT_EQ(ri1.is_submap, 1, "mapping should be nested"); kr = mach_vm_protect(mach_task_self(), vmaddr_sub, vmsize_sub, FALSE, /* set_maximum */ VM_PROT_COPY | VM_PROT_READ | VM_PROT_WRITE); T_EXPECT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_protect(VM_PROT_COPY) fails with KERN_PROTECTION_FAILURE"); /* check protections again */ depth2 = 0; vmaddr_tmp = vmaddr_sub; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(true); /* clipping expected (pmap unnesting), so reset expectations */ vmaddr = vmaddr_tmp; vmsize = vmsize_tmp; T_EXPECT_EQ(ri2.is_submap, 0, "mapping should now be unnnested"); /* vm_protect(VM_PROT_COPY) on unnested mapping */ T_LOG("=========================================="); T_LOG("===== vm_protect(VM_PROT_COPY) on unnested mapping"); T_LOG("=========================================="); ri1 = ri2; T_EXPECT_EQ(ri1.is_submap, 0, "mapping should not be nested"); kr = mach_vm_protect(mach_task_self(), vmaddr_sub, vmsize_sub, FALSE, /* set_maximum */ VM_PROT_COPY | VM_PROT_READ | VM_PROT_WRITE); T_EXPECT_MACH_ERROR(kr, KERN_NO_SPACE, "vm_protect(VM_PROT_COPY) fails with KERN_NO_SPACE"); /* check protections again */ depth2 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; vmaddr_tmp = vmaddr_sub; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false); T_EXPECT_EQ(ri2.is_submap, 0, "mapping should still be unnested"); /* vm_allocate(VM_FLAGS_OVERWRITE) on top of unnested mapping */ T_LOG("=========================================="); T_LOG("===== vm_allocate(VM_FLAGS_OVERWRITE) on unnested mapping"); T_LOG("=========================================="); ri1 = ri2; T_EXPECT_EQ(ri1.is_submap, 0, "mapping should be unnested"); vmaddr_tmp = vmaddr_sub; kr = mach_vm_allocate(mach_task_self(), &vmaddr_tmp, vmsize_sub, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE); T_EXPECT_MACH_ERROR(kr, KERN_NO_SPACE, "vm_allocate(OVERWRITE) fails with KERN_NO_SPACE"); T_EXPECT_EQ(vmaddr, vmaddr_tmp, "vmaddr is unchanged"); /* check protections again */ depth2 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; vmaddr_tmp = vmaddr_sub; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false); T_EXPECT_EQ(ri2.is_submap, 0, "mapping should still be unnested"); /* find a new nested read-only mapping */ T_LOG("=========================================="); T_LOG(""); T_LOG("=========================================="); if (!find_nested_read_only_mapping(&vmaddr_sub, &vmsize_sub, &ri2)) { T_FAIL("could not find appropriate mapping"); return; } T_LOG("=========================================="); /* get top-level mapping protections */ vmaddr = vmaddr_sub; depth1 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr, &vmsize, &depth1, (vm_region_recurse_info_t)&ri1, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_sub, vmsize_sub, &ri2); /* vm_write() on top of submap */ T_LOG("=========================================="); T_LOG("===== vm_write() on nested mapping"); T_LOG("=========================================="); T_EXPECT_EQ(ri1.is_submap, 1, "mapping should be nested"); /* get a temporary buffer */ kr = mach_vm_allocate(mach_task_self(), &vmaddr_buf, vmsize_sub, VM_FLAGS_ANYWHERE); T_EXPECT_MACH_SUCCESS(kr, "vm_allocate(0x%llx)", vmsize_sub); /* copy the data to avoid undue crash */ memcpy((char *)(uintptr_t)vmaddr_buf, (char *)(uintptr_t)vmaddr_sub, (size_t)vmsize_sub); kr = mach_vm_write(mach_task_self(), vmaddr_sub, /* destination address */ vmaddr_buf, /* source buffer */ vmsize_sub); T_EXPECT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_write() on nested mapping fails with KERN_PROTECTION_FAILURE"); /* check protections again */ depth2 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; vmaddr_tmp = vmaddr_sub; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr_sub, vmsize_sub, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(true); T_EXPECT_EQ(ri2.is_submap, 1, "mapping should still be nested"); /* clipping expected (pmap unnesting), so reset expectations */ vmaddr = vmaddr_tmp; vmsize = vmsize_tmp; /* force un-nesting of that mapping */ T_LOG("=========================================="); T_LOG("===== unnesting the mapping"); T_LOG("=========================================="); ri1 = ri2; T_EXPECT_EQ(ri1.is_submap, 1, "mapping should be nested"); kr = mach_vm_protect(mach_task_self(), vmaddr_sub, vmsize_sub, FALSE, /* set_maximum */ VM_PROT_COPY | VM_PROT_READ | VM_PROT_WRITE); T_EXPECT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_protect(0x%llx,0x%llx,VM_PROT_COPY) fails with KERN_PROTECTION_FAILURE", (uint64_t)vmaddr_sub, (uint64_t)vmsize_sub); /* check protections again */ depth2 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; vmaddr_tmp = vmaddr_sub; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(true); /* clipping expected (pmap unnesting), so reset expectations */ vmaddr = vmaddr_tmp; vmsize = vmsize_tmp; T_EXPECT_EQ(ri2.is_submap, 0, "mapping should now be unnested"); /* vm_write() on top of unnested mapping */ T_LOG("=========================================="); T_LOG("===== vm_write() on unnested mapping"); T_LOG("=========================================="); ri1 = ri2; T_EXPECT_EQ(ri1.is_submap, 0, "mapping should be unnested"); /* we re-use vmaddr_buf from test above... */ kr = mach_vm_write(mach_task_self(), vmaddr_sub, /* destination address */ vmaddr_buf, /* source buffer */ vmsize_sub); T_EXPECT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_write() on unnested mapping fails with KERN_PROTECTION_FAILURE"); /* check protections again */ depth2 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; vmaddr_tmp = vmaddr_sub; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr_tmp, &vmsize_tmp, &depth2, (vm_region_recurse_info_t)&ri2, &count); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr_sub); print_region_info(vmaddr_sub, vmsize_sub, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false); T_EXPECT_EQ(ri2.is_submap, 0, "mapping should still be unnested"); // T_LOG("pausing..."); getchar(); } T_DECL(shared_region_x86_writable, "Tests shared region PROT_WRITE is permitted on Intel only, and fails on all other architectures", T_META_ALL_VALID_ARCHS(true), T_META_TAG_VM_PREFERRED) { mach_vm_address_t vmaddr_sub; mach_vm_size_t vmsize_sub; vm_region_submap_short_info_data_64_t ri; if (!find_nested_read_only_mapping(&vmaddr_sub, &vmsize_sub, &ri)) { T_FAIL("could not find appropriate mapping"); return; } kern_return_t kr; kr = mach_vm_protect(mach_task_self(), vmaddr_sub, vmsize_sub, FALSE, VM_PROT_READ | VM_PROT_WRITE); #if defined(__x86_64__) || defined(__i386__) T_ASSERT_MACH_SUCCESS(kr, "mach_vm_protect()"); #else /* defined(__x86_64__) || defined(__i386__) */ T_ASSERT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "mach_vm_protect()"); #endif /* defined(__x86_64__) || defined(__i386__) */ } |