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 | /* * Copyright (c) 2024 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * try_read_write_test.c * * Test the testing helper functions in try_read_write.h. */ #include <stdlib.h> #include <stdint.h> #include <stdbool.h> #include <darwintest.h> #include <mach/mach.h> #include <mach/mach_vm.h> #include <ptrauth.h> #include "try_read_write.h" T_GLOBAL_META( T_META_NAMESPACE("xnu"), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("vm"), T_META_RUN_CONCURRENTLY(true), T_META_ALL_VALID_ARCHS(true) ); #define MAYBE_QUIET(quiet) \ do { \ if (quiet) { \ T_QUIET; \ } \ } while (0) static void test_try_read_byte_maybe_quietly( mach_vm_address_t addr, uint8_t expected_byte, kern_return_t expected_error, bool quiet, const char *message) { bool expected_result = (expected_error == 0); bool actual_result; uint8_t actual_byte; kern_return_t actual_error; actual_result = try_read_byte(addr, &actual_byte, &actual_error); MAYBE_QUIET(quiet); T_EXPECT_EQ(expected_result, actual_result, "%s: try_read_byte return value", message); MAYBE_QUIET(quiet); T_EXPECT_EQ(expected_error, actual_error, "%s: try_read_byte error code", message); if (expected_error == 0 && actual_error == 0) { MAYBE_QUIET(quiet); T_EXPECT_EQ(expected_byte, actual_byte, "%s: try_read_byte value read", message); } } static void test_try_read_byte( mach_vm_address_t addr, uint8_t expected_byte, kern_return_t expected_error, const char *message) { test_try_read_byte_maybe_quietly(addr, expected_byte, expected_error, false /* quiet */, message); } static void test_try_read_byte_quietly( mach_vm_address_t addr, uint8_t expected_byte, kern_return_t expected_error, const char *message) { test_try_read_byte_maybe_quietly(addr, expected_byte, expected_error, true /* quiet */, message); } static void test_try_write_byte_maybe_quietly( mach_vm_address_t addr, uint8_t expected_byte, kern_return_t expected_error, bool quiet, const char *message) { bool expected_result = (expected_error == 0); bool actual_result; uint8_t actual_byte; kern_return_t actual_error; actual_result = try_write_byte(addr, expected_byte, &actual_error); MAYBE_QUIET(quiet); T_EXPECT_EQ(expected_result, actual_result, "%s: try_write_byte return value", message); MAYBE_QUIET(quiet); T_EXPECT_EQ(expected_error, actual_error, "%s: try_write_byte error code", message); if (expected_error == 0 && actual_error == 0) { actual_byte = *(volatile uint8_t *)addr; MAYBE_QUIET(quiet); T_EXPECT_EQ(expected_byte, actual_byte, "%s: try_write_byte value written", message); } } static void test_try_write_byte( mach_vm_address_t addr, uint8_t expected_byte, kern_return_t expected_error, const char *message) { test_try_write_byte_maybe_quietly(addr, expected_byte, expected_error, false /* quiet */, message); } static void test_try_write_byte_quietly( mach_vm_address_t addr, uint8_t expected_byte, kern_return_t expected_error, const char *message) { test_try_write_byte_maybe_quietly(addr, expected_byte, expected_error, true /* quiet */, message); } static mach_vm_address_t allocate_page_with_prot(vm_prot_t prot) { mach_vm_address_t addr; kern_return_t kr; kr = mach_vm_allocate(mach_task_self(), &addr, PAGE_SIZE, VM_FLAGS_ANYWHERE); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_allocate"); kr = mach_vm_protect(mach_task_self(), addr, PAGE_SIZE, false /* set max */, prot); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_protect"); return addr; } static void deallocate_page(mach_vm_address_t addr) { kern_return_t kr = mach_vm_deallocate(mach_task_self(), addr, PAGE_SIZE); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate"); } /* * Generate some r-x memory with a known value. */ static void __attribute__((naked)) instruction_byte_ff(void) { asm(".quad 0xffffffff"); } T_DECL(try_read_write_test, "test the test helper functions try_read_byte and try_write_byte") { mach_vm_address_t addr; /* read and write an unmapped address */ test_try_read_byte(0, 0, KERN_INVALID_ADDRESS, "read unmapped address"); test_try_write_byte(0, 0, KERN_INVALID_ADDRESS, "write unmapped address"); /* read and write --- */ addr = allocate_page_with_prot(VM_PROT_NONE); test_try_read_byte(addr, 0, KERN_PROTECTION_FAILURE, "read prot ---"); test_try_write_byte(addr, 1, KERN_PROTECTION_FAILURE, "write prot ---"); deallocate_page(addr); /* read and write r-- */ addr = allocate_page_with_prot(VM_PROT_READ); test_try_read_byte(addr, 0, KERN_SUCCESS, "read prot r--"); test_try_write_byte(addr, 1, KERN_PROTECTION_FAILURE, "write prot r--"); deallocate_page(addr); /* read and write -w- */ addr = allocate_page_with_prot(VM_PROT_WRITE); test_try_read_byte(addr, 0, KERN_PROTECTION_FAILURE, "read prot -w-"); test_try_write_byte(addr, 1, KERN_PROTECTION_FAILURE, "write prot -w-"); deallocate_page(addr); /* read and write rw- */ addr = allocate_page_with_prot(VM_PROT_READ | VM_PROT_WRITE); *(uint8_t *)addr = 1; test_try_read_byte(addr, 1, KERN_SUCCESS, "read prot rw-"); test_try_write_byte(addr, 2, KERN_SUCCESS, "write prot rw-"); test_try_read_byte(addr, 2, KERN_SUCCESS, "read prot rw- again"); deallocate_page(addr); /* read and write r-x */ addr = (mach_vm_address_t)ptrauth_strip(&instruction_byte_ff, ptrauth_key_function_pointer); test_try_read_byte(addr, 0xff, KERN_SUCCESS, "read prot r-x"); test_try_write_byte(addr, 1, KERN_PROTECTION_FAILURE, "write prot r-x"); } /* this test provokes THREAD_COUNT * REP_COUNT * PAGE_SIZE exceptions */ #define THREAD_COUNT 10 #define REP_COUNT 5 struct test_alloc { mach_vm_address_t addr; vm_prot_t prot; kern_return_t expected_read_error; kern_return_t expected_write_error; }; static struct test_alloc allocate_page_with_random_prot(void) { struct test_alloc result; switch (random() % 4) { case 0: result.prot = VM_PROT_NONE; result.expected_read_error = KERN_PROTECTION_FAILURE; result.expected_write_error = KERN_PROTECTION_FAILURE; break; case 1: result.prot = VM_PROT_READ; result.expected_read_error = KERN_SUCCESS; result.expected_write_error = KERN_PROTECTION_FAILURE; break; case 2: result.prot = VM_PROT_WRITE; result.expected_read_error = KERN_PROTECTION_FAILURE; result.expected_write_error = KERN_PROTECTION_FAILURE; break; case 3: result.prot = VM_PROT_READ | VM_PROT_WRITE; result.expected_read_error = KERN_SUCCESS; result.expected_write_error = KERN_SUCCESS; break; } result.addr = allocate_page_with_prot(result.prot); return result; } static void * multithreaded_test(void *arg) { struct test_alloc alloc = *(struct test_alloc *)arg; /* Read and write a lot from our page. */ for (int reps = 0; reps < REP_COUNT; reps++) { for (int offset = 0; offset < PAGE_SIZE; offset++) { test_try_read_byte_quietly(alloc.addr + offset, 0, alloc.expected_read_error, "thread read"); test_try_write_byte_quietly(alloc.addr + offset, 0, alloc.expected_write_error, "thread write"); } } return NULL; } T_DECL(try_read_write_test_multithreaded, "test try_read_byte and try_write_byte from multiple threads") { verbose_exc_helper = false; pthread_t threads[THREAD_COUNT]; struct test_alloc allocs[THREAD_COUNT]; /* each thread gets a page with a random prot to read and write on */ for (int i = 0; i < THREAD_COUNT; i++) { allocs[i] = allocate_page_with_random_prot(); } T_LOG("running %d threads each %d times", THREAD_COUNT, REP_COUNT); for (int i = 0; i < THREAD_COUNT; i++) { pthread_create(&threads[i], NULL, multithreaded_test, &allocs[i]); } for (int i = 0; i < THREAD_COUNT; i++) { pthread_join(threads[i], NULL); deallocate_page(allocs[i].addr); } } |