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 | #include <darwintest.h> #include <darwintest_utils.h> #include <string.h> #include <sys/types.h> #include <sys/sysctl.h> #include <mach/mach.h> #include <mach/mach_vm.h> #include <mach/vm_types.h> #include <sys/mman.h> #include <unistd.h> #include <TargetConditionals.h> T_GLOBAL_META( T_META_NAMESPACE("xnu.vm"), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("VM") ); struct child_rc { int ret; int sig; }; static struct child_rc fork_child_test(void (^block)(void)) { struct child_rc rc = { }; pid_t child_pid; child_pid = fork(); if (child_pid == 0) { block(); exit(0); } T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "fork process"); /* wait for child process to exit */ dt_waitpid(child_pid, &rc.ret, &rc.sig, 30); return rc; } static mach_vm_address_t get_permanent_mapping(mach_vm_size_t size) { kern_return_t kr; mach_vm_address_t addr; kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_PERMANENT); T_ASSERT_MACH_SUCCESS(kr, "mach_vm_allocate(%lld, PERMANENT) == %p", size, (void *)addr); *(int *)addr = 42; kr = mach_vm_protect(mach_task_self(), addr, size, FALSE, VM_PROT_READ); T_EXPECT_MACH_SUCCESS(kr, "mach_vm_protect(PERMANENT, READ)"); T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); return addr; } T_DECL(permanent_mapping, "check permanent mappings semantics", T_META_TAG_VM_PREFERRED) { mach_vm_size_t size = 1 << 20; struct child_rc rc; T_LOG("try to bypass permanent mappings with VM_FLAGS_OVERWRITE"); rc = fork_child_test(^{ mach_vm_address_t addr, addr2; kern_return_t kr2; addr = get_permanent_mapping(size); T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); addr2 = addr; kr2 = mach_vm_allocate(mach_task_self(), &addr2, size, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE); /* * because the permanent mapping wasn't removed, * we should get an error. */ T_ASSERT_MACH_ERROR(kr2, KERN_NO_SPACE, "mach_vm_allocate(VM_FLAGS_OVERWRITE)"); /* * because the permanent mapping was neutered, * accessing it should crash. */ T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); }); T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS"); T_LOG("try to bypass permanent mappings with a VM_PROT_COPY mprotect"); rc = fork_child_test(^{ kern_return_t kr2; mach_vm_address_t addr; addr = get_permanent_mapping(size); T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); kr2 = mach_vm_protect(mach_task_self(), addr, size, TRUE, VM_PROT_COPY | VM_PROT_DEFAULT); /* * because the permanent mapping wasn't removed, * we should get an error. */ T_ASSERT_MACH_ERROR(kr2, KERN_NO_SPACE, "mach_vm_protect(VM_PROT_COPY)"); /* * because the permanent mapping was neutered, * accessing it should crash. */ T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); }); T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS"); T_LOG("try to bypass permanent mappings with a vm_remap"); rc = fork_child_test(^{ kern_return_t kr2; mach_vm_address_t addr, remap_addr, addr2; vm_prot_t cur_prot, max_prot; addr = get_permanent_mapping(size); T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); addr2 = 0; kr2 = mach_vm_allocate(mach_task_self(), &addr2, size, VM_FLAGS_ANYWHERE); T_QUIET; T_EXPECT_MACH_SUCCESS(kr2, "vm_allocate()"); remap_addr = addr; kr2 = mach_vm_remap(mach_task_self(), &remap_addr, size, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, mach_task_self(), addr2, TRUE, &cur_prot, &max_prot, VM_INHERIT_DEFAULT); /* * because the permanent mapping wasn't removed, * we should get an error. */ T_ASSERT_MACH_ERROR(kr2, KERN_NO_SPACE, "mach_vm_remap()"); /* * because the permanent mapping was neutered, * accessing it should crash. */ T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); }); T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS"); T_LOG("try to bypass permanent mappings with a vm_deallocate"); rc = fork_child_test(^{ kern_return_t kr2; mach_vm_address_t addr; addr = get_permanent_mapping(size); T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); kr2 = mach_vm_deallocate(mach_task_self(), addr, size); /* * the permanent mapping wasn't removed but was made * inaccessible; we should not get an error. */ T_ASSERT_MACH_SUCCESS(kr2, "mach_vm_deallocate()"); /* * because the permanent mapping was neutered, * accessing it should crash. */ T_QUIET; T_EXPECT_EQ(*(int *)addr, 42, "we can still read what we wrote"); }); T_EXPECT_EQ(rc.sig, SIGBUS, "accessing the mapping caused a SIGBUS"); } T_DECL(vm_tag_describe, "test mach_vm_tag_describe()", T_META_TAG_VM_PREFERRED) { for (unsigned int i = 0; i <= VM_MEMORY_COUNT; i++) { const char *desc = mach_vm_tag_describe(i); T_LOG("%i: %s", i, desc); T_ASSERT_NOTNULL(desc, "Tag description (%i) is non-null", i); T_EXPECT_NE_STR(desc, "", "Tag description (%i) is non-empty", i); T_EXPECT_LE(strlen(desc), 24UL, "Tag description must be less than 24 characters"); } } |