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 | #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")); static char *_prot_str[] = { /* 0 */ "---", /* 1 */ "r--", /* 2 */ "-w-", /* 3 */ "rw-", /* 4 */ "--x", /* 5 */ "r-x", /* 6 */ "-wx", /* 7 */ "rwx" }; static char * prot_str(vm_prot_t prot) { return _prot_str[prot & VM_PROT_ALL]; } static 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_first_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; mach_vm_size_t vmsize; natural_t depth; mach_msg_type_number_t count; T_LOG("===== Looking for first read-only mapping"); /* find the first read-only mapping */ for (vmaddr = 0;; vmaddr += vmsize) { depth = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; kr = mach_vm_region_recurse(mach_task_self(), &vmaddr, &vmsize, &depth, (vm_region_recurse_info_t)ri, &count); T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_vm_region_recurse(0x%llx)\n", vmaddr); if (kr != KERN_SUCCESS) { /* end of address space */ T_FAIL("could not find first read-only mapping"); return false; } if (ri->is_submap) { /* submap: keep looking */ continue; } if (ri->max_protection != VM_PROT_READ) { /* not read-only: keep looking */ continue; } T_PASS("Found first read-only mapping at 0x%llx size 0x%llx\n", (uint64_t)vmaddr, (uint64_t)vmsize); *vmaddr_p = vmaddr; *vmsize_p = vmsize; return true; } return false; } T_DECL(vm_test_linkedit_permanent, "Tests that LINKEDIT mapping can't be overwritten", T_META_TAG_VM_PREFERRED) { kern_return_t kr; mach_vm_address_t vmaddr, vmaddr_linkedit, vmaddr_tmp, vmaddr_buf; mach_vm_size_t vmsize, vmsize_linkedit, 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: LINKEDIT mappings are not protected"); return; #endif /* __x86_64 */ #define ASSERT_UNCHANGED(clip_ok, object_change_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"); \ if (!object_change_ok) { \ T_EXPECT_EQ(ri2.object_id, ri1.object_id, \ "object id should not change"); \ } \ } while (0) T_LOG("=========================================="); if (!find_first_read_only_mapping(&vmaddr_linkedit, &vmsize_linkedit, &ri2)) { T_FAIL("could not find appropriate mapping"); return; } T_LOG("=========================================="); /* get top-level mapping protections */ vmaddr = vmaddr_linkedit; 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_linkedit); print_region_info(vmaddr, vmsize, &ri1, vmaddr_linkedit, vmsize_linkedit, &ri2); /* vm_write() on LINKEDIT mapping */ T_LOG("=========================================="); T_LOG("===== vm_write() on LINKEDIT mapping"); T_LOG("=========================================="); T_EXPECT_EQ(ri1.is_submap, 0, "mapping should be nested"); /* get a temporary buffer */ vmaddr_buf = 0; kr = mach_vm_allocate(mach_task_self(), &vmaddr_buf, vmsize_linkedit, VM_FLAGS_ANYWHERE); T_EXPECT_MACH_SUCCESS(kr, "vm_allocate(0x%llx)", vmsize_linkedit); /* copy the data to avoid undue crash */ memcpy((char *)(uintptr_t)vmaddr_buf, (char *)(uintptr_t)vmaddr_linkedit, (size_t)vmsize_linkedit); kr = mach_vm_write(mach_task_self(), vmaddr_linkedit, /* destination address */ vmaddr_buf, /* source buffer */ (mach_msg_type_number_t) vmsize_linkedit); T_EXPECT_MACH_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_write() on LINKEDIT mapping fails with KERN_PROTECTION_FAILURE"); /* check protections again */ depth2 = 0; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; vmaddr_tmp = vmaddr_linkedit; 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_linkedit); print_region_info(vmaddr_linkedit, vmsize_linkedit, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false, false); T_EXPECT_EQ(ri2.is_submap, 0, "mapping should not be nested"); /* vm_allocate(VM_FLAGS_OVERWRITE) on top of LINKEDIT */ T_LOG("=========================================="); T_LOG("===== vm_allocate(VM_FLAGS_OVERWRITE) on LINKEDIT mapping"); T_LOG("=========================================="); T_EXPECT_EQ(ri1.is_submap, 0, "mapping should not be nested"); vmaddr_tmp = vmaddr_linkedit; kr = mach_vm_allocate(mach_task_self(), &vmaddr_tmp, vmsize_linkedit, 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_linkedit, vmaddr_tmp, "vmaddr is unchanged"); /* check protections again */ depth2 = 0; vmaddr_tmp = vmaddr_linkedit; 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_linkedit); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false, false); T_EXPECT_EQ(ri2.is_submap, 0, "mapping should not be nested"); /* vm_remap(VM_FLAGS_OVERWRITE) on top of submap */ T_LOG("=========================================="); T_LOG("===== vm_remap(VM_FLAGS_OVERWRITE) on LINKEDIT mapping"); T_LOG("=========================================="); T_EXPECT_EQ(ri1.is_submap, 0, "mapping should not be nested"); vmaddr_tmp = vmaddr_linkedit; kr = mach_vm_remap(mach_task_self(), &vmaddr_tmp, vmsize_linkedit, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, mach_task_self(), vmaddr_buf, 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_linkedit, vmaddr_tmp, "vmaddr is unchanged"); /* check protections again */ depth2 = 0; vmaddr_tmp = vmaddr_linkedit; 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_linkedit); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false, false); T_EXPECT_EQ(ri2.is_submap, 0, "mapping should not be nested"); /* vm_protect(VM_PROT_COPY) on LINKEDIT mapping */ T_LOG("=========================================="); T_LOG("===== vm_protect(VM_PROT_COPY) on LINKEDIT 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_linkedit, vmsize_linkedit, 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; count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; vmaddr_tmp = vmaddr_linkedit; 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_linkedit); print_region_info(vmaddr, vmsize, &ri1, vmaddr_tmp, vmsize_tmp, &ri2); ASSERT_UNCHANGED(false, true); T_EXPECT_EQ(ri2.is_submap, 0, "mapping should not be nested"); // T_LOG("pausing..."); getchar(); } |