Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * ldt_gdt.c - Test cases for LDT and GDT access |
| 3 | * Copyright (c) 2015 Andrew Lutomirski |
| 4 | */ |
| 5 | |
| 6 | #define _GNU_SOURCE |
| 7 | #include <err.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdint.h> |
| 10 | #include <signal.h> |
| 11 | #include <setjmp.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <errno.h> |
| 15 | #include <unistd.h> |
| 16 | #include <sys/syscall.h> |
| 17 | #include <asm/ldt.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/wait.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <pthread.h> |
| 22 | #include <sched.h> |
| 23 | #include <linux/futex.h> |
| 24 | |
| 25 | #define AR_ACCESSED (1<<8) |
| 26 | |
| 27 | #define AR_TYPE_RODATA (0 * (1<<9)) |
| 28 | #define AR_TYPE_RWDATA (1 * (1<<9)) |
| 29 | #define AR_TYPE_RODATA_EXPDOWN (2 * (1<<9)) |
| 30 | #define AR_TYPE_RWDATA_EXPDOWN (3 * (1<<9)) |
| 31 | #define AR_TYPE_XOCODE (4 * (1<<9)) |
| 32 | #define AR_TYPE_XRCODE (5 * (1<<9)) |
| 33 | #define AR_TYPE_XOCODE_CONF (6 * (1<<9)) |
| 34 | #define AR_TYPE_XRCODE_CONF (7 * (1<<9)) |
| 35 | |
| 36 | #define AR_DPL3 (3 * (1<<13)) |
| 37 | |
| 38 | #define AR_S (1 << 12) |
| 39 | #define AR_P (1 << 15) |
| 40 | #define AR_AVL (1 << 20) |
| 41 | #define AR_L (1 << 21) |
| 42 | #define AR_DB (1 << 22) |
| 43 | #define AR_G (1 << 23) |
| 44 | |
| 45 | static int nerrs; |
| 46 | |
| 47 | static void check_invalid_segment(uint16_t index, int ldt) |
| 48 | { |
| 49 | uint32_t has_limit = 0, has_ar = 0, limit, ar; |
| 50 | uint32_t selector = (index << 3) | (ldt << 2) | 3; |
| 51 | |
| 52 | asm ("lsl %[selector], %[limit]\n\t" |
| 53 | "jnz 1f\n\t" |
| 54 | "movl $1, %[has_limit]\n\t" |
| 55 | "1:" |
| 56 | : [limit] "=r" (limit), [has_limit] "+rm" (has_limit) |
| 57 | : [selector] "r" (selector)); |
| 58 | asm ("larl %[selector], %[ar]\n\t" |
| 59 | "jnz 1f\n\t" |
| 60 | "movl $1, %[has_ar]\n\t" |
| 61 | "1:" |
| 62 | : [ar] "=r" (ar), [has_ar] "+rm" (has_ar) |
| 63 | : [selector] "r" (selector)); |
| 64 | |
| 65 | if (has_limit || has_ar) { |
| 66 | printf("[FAIL]\t%s entry %hu is valid but should be invalid\n", |
| 67 | (ldt ? "LDT" : "GDT"), index); |
| 68 | nerrs++; |
| 69 | } else { |
| 70 | printf("[OK]\t%s entry %hu is invalid\n", |
| 71 | (ldt ? "LDT" : "GDT"), index); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void check_valid_segment(uint16_t index, int ldt, |
| 76 | uint32_t expected_ar, uint32_t expected_limit, |
| 77 | bool verbose) |
| 78 | { |
| 79 | uint32_t has_limit = 0, has_ar = 0, limit, ar; |
| 80 | uint32_t selector = (index << 3) | (ldt << 2) | 3; |
| 81 | |
| 82 | asm ("lsl %[selector], %[limit]\n\t" |
| 83 | "jnz 1f\n\t" |
| 84 | "movl $1, %[has_limit]\n\t" |
| 85 | "1:" |
| 86 | : [limit] "=r" (limit), [has_limit] "+rm" (has_limit) |
| 87 | : [selector] "r" (selector)); |
| 88 | asm ("larl %[selector], %[ar]\n\t" |
| 89 | "jnz 1f\n\t" |
| 90 | "movl $1, %[has_ar]\n\t" |
| 91 | "1:" |
| 92 | : [ar] "=r" (ar), [has_ar] "+rm" (has_ar) |
| 93 | : [selector] "r" (selector)); |
| 94 | |
| 95 | if (!has_limit || !has_ar) { |
| 96 | printf("[FAIL]\t%s entry %hu is invalid but should be valid\n", |
| 97 | (ldt ? "LDT" : "GDT"), index); |
| 98 | nerrs++; |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | if (ar != expected_ar) { |
| 103 | printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n", |
| 104 | (ldt ? "LDT" : "GDT"), index, ar, expected_ar); |
| 105 | nerrs++; |
| 106 | } else if (limit != expected_limit) { |
| 107 | printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n", |
| 108 | (ldt ? "LDT" : "GDT"), index, limit, expected_limit); |
| 109 | nerrs++; |
| 110 | } else if (verbose) { |
| 111 | printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n", |
| 112 | (ldt ? "LDT" : "GDT"), index, ar, limit); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static bool install_valid_mode(const struct user_desc *desc, uint32_t ar, |
| 117 | bool oldmode) |
| 118 | { |
| 119 | int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11, |
| 120 | desc, sizeof(*desc)); |
| 121 | if (ret < -1) |
| 122 | errno = -ret; |
| 123 | if (ret == 0) { |
| 124 | uint32_t limit = desc->limit; |
| 125 | if (desc->limit_in_pages) |
| 126 | limit = (limit << 12) + 4095; |
| 127 | check_valid_segment(desc->entry_number, 1, ar, limit, true); |
| 128 | return true; |
| 129 | } else if (errno == ENOSYS) { |
| 130 | printf("[OK]\tmodify_ldt returned -ENOSYS\n"); |
| 131 | return false; |
| 132 | } else { |
| 133 | if (desc->seg_32bit) { |
| 134 | printf("[FAIL]\tUnexpected modify_ldt failure %d\n", |
| 135 | errno); |
| 136 | nerrs++; |
| 137 | return false; |
| 138 | } else { |
| 139 | printf("[OK]\tmodify_ldt rejected 16 bit segment\n"); |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static bool install_valid(const struct user_desc *desc, uint32_t ar) |
| 146 | { |
| 147 | return install_valid_mode(desc, ar, false); |
| 148 | } |
| 149 | |
| 150 | static void install_invalid(const struct user_desc *desc, bool oldmode) |
| 151 | { |
| 152 | int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11, |
| 153 | desc, sizeof(*desc)); |
| 154 | if (ret < -1) |
| 155 | errno = -ret; |
| 156 | if (ret == 0) { |
| 157 | check_invalid_segment(desc->entry_number, 1); |
| 158 | } else if (errno == ENOSYS) { |
| 159 | printf("[OK]\tmodify_ldt returned -ENOSYS\n"); |
| 160 | } else { |
| 161 | if (desc->seg_32bit) { |
| 162 | printf("[FAIL]\tUnexpected modify_ldt failure %d\n", |
| 163 | errno); |
| 164 | nerrs++; |
| 165 | } else { |
| 166 | printf("[OK]\tmodify_ldt rejected 16 bit segment\n"); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static int safe_modify_ldt(int func, struct user_desc *ptr, |
| 172 | unsigned long bytecount) |
| 173 | { |
| 174 | int ret = syscall(SYS_modify_ldt, 0x11, ptr, bytecount); |
| 175 | if (ret < -1) |
| 176 | errno = -ret; |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static void fail_install(struct user_desc *desc) |
| 181 | { |
| 182 | if (safe_modify_ldt(0x11, desc, sizeof(*desc)) == 0) { |
| 183 | printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n"); |
| 184 | nerrs++; |
| 185 | } else if (errno == ENOSYS) { |
| 186 | printf("[OK]\tmodify_ldt returned -ENOSYS\n"); |
| 187 | } else { |
| 188 | printf("[OK]\tmodify_ldt failure %d\n", errno); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void do_simple_tests(void) |
| 193 | { |
| 194 | struct user_desc desc = { |
| 195 | .entry_number = 0, |
| 196 | .base_addr = 0, |
| 197 | .limit = 10, |
| 198 | .seg_32bit = 1, |
| 199 | .contents = 2, /* Code, not conforming */ |
| 200 | .read_exec_only = 0, |
| 201 | .limit_in_pages = 0, |
| 202 | .seg_not_present = 0, |
| 203 | .useable = 0 |
| 204 | }; |
| 205 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB); |
| 206 | |
| 207 | desc.limit_in_pages = 1; |
| 208 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | |
| 209 | AR_S | AR_P | AR_DB | AR_G); |
| 210 | |
| 211 | check_invalid_segment(1, 1); |
| 212 | |
| 213 | desc.entry_number = 2; |
| 214 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | |
| 215 | AR_S | AR_P | AR_DB | AR_G); |
| 216 | |
| 217 | check_invalid_segment(1, 1); |
| 218 | |
| 219 | desc.base_addr = 0xf0000000; |
| 220 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | |
| 221 | AR_S | AR_P | AR_DB | AR_G); |
| 222 | |
| 223 | desc.useable = 1; |
| 224 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | |
| 225 | AR_S | AR_P | AR_DB | AR_G | AR_AVL); |
| 226 | |
| 227 | desc.seg_not_present = 1; |
| 228 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | |
| 229 | AR_S | AR_DB | AR_G | AR_AVL); |
| 230 | |
| 231 | desc.seg_32bit = 0; |
| 232 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | |
| 233 | AR_S | AR_G | AR_AVL); |
| 234 | |
| 235 | desc.seg_32bit = 1; |
| 236 | desc.contents = 0; |
| 237 | install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | |
| 238 | AR_S | AR_DB | AR_G | AR_AVL); |
| 239 | |
| 240 | desc.read_exec_only = 1; |
| 241 | install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | |
| 242 | AR_S | AR_DB | AR_G | AR_AVL); |
| 243 | |
| 244 | desc.contents = 1; |
| 245 | install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN | |
| 246 | AR_S | AR_DB | AR_G | AR_AVL); |
| 247 | |
| 248 | desc.read_exec_only = 0; |
| 249 | desc.limit_in_pages = 0; |
| 250 | install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN | |
| 251 | AR_S | AR_DB | AR_AVL); |
| 252 | |
| 253 | desc.contents = 3; |
| 254 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE_CONF | |
| 255 | AR_S | AR_DB | AR_AVL); |
| 256 | |
| 257 | desc.read_exec_only = 1; |
| 258 | install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE_CONF | |
| 259 | AR_S | AR_DB | AR_AVL); |
| 260 | |
| 261 | desc.read_exec_only = 0; |
| 262 | desc.contents = 2; |
| 263 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | |
| 264 | AR_S | AR_DB | AR_AVL); |
| 265 | |
| 266 | desc.read_exec_only = 1; |
| 267 | |
| 268 | #ifdef __x86_64__ |
| 269 | desc.lm = 1; |
| 270 | install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE | |
| 271 | AR_S | AR_DB | AR_AVL); |
| 272 | desc.lm = 0; |
| 273 | #endif |
| 274 | |
| 275 | bool entry1_okay = install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE | |
| 276 | AR_S | AR_DB | AR_AVL); |
| 277 | |
| 278 | if (entry1_okay) { |
| 279 | printf("[RUN]\tTest fork\n"); |
| 280 | pid_t child = fork(); |
| 281 | if (child == 0) { |
| 282 | nerrs = 0; |
| 283 | check_valid_segment(desc.entry_number, 1, |
| 284 | AR_DPL3 | AR_TYPE_XOCODE | |
| 285 | AR_S | AR_DB | AR_AVL, desc.limit, |
| 286 | true); |
| 287 | check_invalid_segment(1, 1); |
| 288 | exit(nerrs ? 1 : 0); |
| 289 | } else { |
| 290 | int status; |
| 291 | if (waitpid(child, &status, 0) != child || |
| 292 | !WIFEXITED(status)) { |
| 293 | printf("[FAIL]\tChild died\n"); |
| 294 | nerrs++; |
| 295 | } else if (WEXITSTATUS(status) != 0) { |
| 296 | printf("[FAIL]\tChild failed\n"); |
| 297 | nerrs++; |
| 298 | } else { |
| 299 | printf("[OK]\tChild succeeded\n"); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | printf("[RUN]\tTest size\n"); |
| 304 | int i; |
| 305 | for (i = 0; i < 8192; i++) { |
| 306 | desc.entry_number = i; |
| 307 | desc.limit = i; |
| 308 | if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) { |
| 309 | printf("[FAIL]\tFailed to install entry %d\n", i); |
| 310 | nerrs++; |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | for (int j = 0; j < i; j++) { |
| 315 | check_valid_segment(j, 1, AR_DPL3 | AR_TYPE_XOCODE | |
| 316 | AR_S | AR_DB | AR_AVL, j, false); |
| 317 | } |
| 318 | printf("[DONE]\tSize test\n"); |
| 319 | } else { |
| 320 | printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n"); |
| 321 | } |
| 322 | |
| 323 | /* Test entry_number too high. */ |
| 324 | desc.entry_number = 8192; |
| 325 | fail_install(&desc); |
| 326 | |
| 327 | /* Test deletion and actions mistakeable for deletion. */ |
| 328 | memset(&desc, 0, sizeof(desc)); |
| 329 | install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P); |
| 330 | |
| 331 | desc.seg_not_present = 1; |
| 332 | install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S); |
| 333 | |
| 334 | desc.seg_not_present = 0; |
| 335 | desc.read_exec_only = 1; |
| 336 | install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P); |
| 337 | |
| 338 | desc.read_exec_only = 0; |
| 339 | desc.seg_not_present = 1; |
| 340 | install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S); |
| 341 | |
| 342 | desc.read_exec_only = 1; |
| 343 | desc.limit = 1; |
| 344 | install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S); |
| 345 | |
| 346 | desc.limit = 0; |
| 347 | desc.base_addr = 1; |
| 348 | install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S); |
| 349 | |
| 350 | desc.base_addr = 0; |
| 351 | install_invalid(&desc, false); |
| 352 | |
| 353 | desc.seg_not_present = 0; |
| 354 | desc.read_exec_only = 0; |
| 355 | desc.seg_32bit = 1; |
| 356 | install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB); |
| 357 | install_invalid(&desc, true); |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * 0: thread is idle |
| 362 | * 1: thread armed |
| 363 | * 2: thread should clear LDT entry 0 |
| 364 | * 3: thread should exit |
| 365 | */ |
| 366 | static volatile unsigned int ftx; |
| 367 | |
| 368 | static void *threadproc(void *ctx) |
| 369 | { |
| 370 | cpu_set_t cpuset; |
| 371 | CPU_ZERO(&cpuset); |
| 372 | CPU_SET(1, &cpuset); |
| 373 | if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) |
| 374 | err(1, "sched_setaffinity to CPU 1"); /* should never fail */ |
| 375 | |
| 376 | while (1) { |
| 377 | syscall(SYS_futex, &ftx, FUTEX_WAIT, 0, NULL, NULL, 0); |
| 378 | while (ftx != 2) { |
| 379 | if (ftx >= 3) |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | /* clear LDT entry 0 */ |
| 384 | const struct user_desc desc = {}; |
| 385 | if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) != 0) |
| 386 | err(1, "modify_ldt"); |
| 387 | |
| 388 | /* If ftx == 2, set it to zero. If ftx == 100, quit. */ |
| 389 | unsigned int x = -2; |
| 390 | asm volatile ("lock xaddl %[x], %[ftx]" : |
| 391 | [x] "+r" (x), [ftx] "+m" (ftx)); |
| 392 | if (x != 2) |
| 393 | return NULL; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | #ifdef __i386__ |
| 398 | |
| 399 | #ifndef SA_RESTORE |
| 400 | #define SA_RESTORER 0x04000000 |
| 401 | #endif |
| 402 | |
| 403 | /* |
| 404 | * The UAPI header calls this 'struct sigaction', which conflicts with |
| 405 | * glibc. Sigh. |
| 406 | */ |
| 407 | struct fake_ksigaction { |
| 408 | void *handler; /* the real type is nasty */ |
| 409 | unsigned long sa_flags; |
| 410 | void (*sa_restorer)(void); |
| 411 | unsigned char sigset[8]; |
| 412 | }; |
| 413 | |
| 414 | static void fix_sa_restorer(int sig) |
| 415 | { |
| 416 | struct fake_ksigaction ksa; |
| 417 | |
| 418 | if (syscall(SYS_rt_sigaction, sig, NULL, &ksa, 8) == 0) { |
| 419 | /* |
| 420 | * glibc has a nasty bug: it sometimes writes garbage to |
| 421 | * sa_restorer. This interacts quite badly with anything |
| 422 | * that fiddles with SS because it can trigger legacy |
| 423 | * stack switching. Patch it up. See: |
| 424 | * |
| 425 | * https://sourceware.org/bugzilla/show_bug.cgi?id=21269 |
| 426 | */ |
| 427 | if (!(ksa.sa_flags & SA_RESTORER) && ksa.sa_restorer) { |
| 428 | ksa.sa_restorer = NULL; |
| 429 | if (syscall(SYS_rt_sigaction, sig, &ksa, NULL, |
| 430 | sizeof(ksa.sigset)) != 0) |
| 431 | err(1, "rt_sigaction"); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | #else |
| 436 | static void fix_sa_restorer(int sig) |
| 437 | { |
| 438 | /* 64-bit glibc works fine. */ |
| 439 | } |
| 440 | #endif |
| 441 | |
| 442 | static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), |
| 443 | int flags) |
| 444 | { |
| 445 | struct sigaction sa; |
| 446 | memset(&sa, 0, sizeof(sa)); |
| 447 | sa.sa_sigaction = handler; |
| 448 | sa.sa_flags = SA_SIGINFO | flags; |
| 449 | sigemptyset(&sa.sa_mask); |
| 450 | if (sigaction(sig, &sa, 0)) |
| 451 | err(1, "sigaction"); |
| 452 | |
| 453 | fix_sa_restorer(sig); |
| 454 | } |
| 455 | |
| 456 | static jmp_buf jmpbuf; |
| 457 | |
| 458 | static void sigsegv(int sig, siginfo_t *info, void *ctx_void) |
| 459 | { |
| 460 | siglongjmp(jmpbuf, 1); |
| 461 | } |
| 462 | |
| 463 | static void do_multicpu_tests(void) |
| 464 | { |
| 465 | cpu_set_t cpuset; |
| 466 | pthread_t thread; |
| 467 | int failures = 0, iters = 5, i; |
| 468 | unsigned short orig_ss; |
| 469 | |
| 470 | CPU_ZERO(&cpuset); |
| 471 | CPU_SET(1, &cpuset); |
| 472 | if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) { |
| 473 | printf("[SKIP]\tCannot set affinity to CPU 1\n"); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | CPU_ZERO(&cpuset); |
| 478 | CPU_SET(0, &cpuset); |
| 479 | if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) { |
| 480 | printf("[SKIP]\tCannot set affinity to CPU 0\n"); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | sethandler(SIGSEGV, sigsegv, 0); |
| 485 | #ifdef __i386__ |
| 486 | /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */ |
| 487 | sethandler(SIGILL, sigsegv, 0); |
| 488 | #endif |
| 489 | |
| 490 | printf("[RUN]\tCross-CPU LDT invalidation\n"); |
| 491 | |
| 492 | if (pthread_create(&thread, 0, threadproc, 0) != 0) |
| 493 | err(1, "pthread_create"); |
| 494 | |
| 495 | asm volatile ("mov %%ss, %0" : "=rm" (orig_ss)); |
| 496 | |
| 497 | for (i = 0; i < 5; i++) { |
| 498 | if (sigsetjmp(jmpbuf, 1) != 0) |
| 499 | continue; |
| 500 | |
| 501 | /* Make sure the thread is ready after the last test. */ |
| 502 | while (ftx != 0) |
| 503 | ; |
| 504 | |
| 505 | struct user_desc desc = { |
| 506 | .entry_number = 0, |
| 507 | .base_addr = 0, |
| 508 | .limit = 0xfffff, |
| 509 | .seg_32bit = 1, |
| 510 | .contents = 0, /* Data */ |
| 511 | .read_exec_only = 0, |
| 512 | .limit_in_pages = 1, |
| 513 | .seg_not_present = 0, |
| 514 | .useable = 0 |
| 515 | }; |
| 516 | |
| 517 | if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) { |
| 518 | if (errno != ENOSYS) |
| 519 | err(1, "modify_ldt"); |
| 520 | printf("[SKIP]\tmodify_ldt unavailable\n"); |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | /* Arm the thread. */ |
| 525 | ftx = 1; |
| 526 | syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0); |
| 527 | |
| 528 | asm volatile ("mov %0, %%ss" : : "r" (0x7)); |
| 529 | |
| 530 | /* Go! */ |
| 531 | ftx = 2; |
| 532 | |
| 533 | while (ftx != 0) |
| 534 | ; |
| 535 | |
| 536 | /* |
| 537 | * On success, modify_ldt will segfault us synchronously, |
| 538 | * and we'll escape via siglongjmp. |
| 539 | */ |
| 540 | |
| 541 | failures++; |
| 542 | asm volatile ("mov %0, %%ss" : : "rm" (orig_ss)); |
| 543 | }; |
| 544 | |
| 545 | ftx = 100; /* Kill the thread. */ |
| 546 | syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0); |
| 547 | |
| 548 | if (pthread_join(thread, NULL) != 0) |
| 549 | err(1, "pthread_join"); |
| 550 | |
| 551 | if (failures) { |
| 552 | printf("[FAIL]\t%d of %d iterations failed\n", failures, iters); |
| 553 | nerrs++; |
| 554 | } else { |
| 555 | printf("[OK]\tAll %d iterations succeeded\n", iters); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | static int finish_exec_test(void) |
| 560 | { |
| 561 | /* |
| 562 | * In a sensible world, this would be check_invalid_segment(0, 1); |
| 563 | * For better or for worse, though, the LDT is inherited across exec. |
| 564 | * We can probably change this safely, but for now we test it. |
| 565 | */ |
| 566 | check_valid_segment(0, 1, |
| 567 | AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB, |
| 568 | 42, true); |
| 569 | |
| 570 | return nerrs ? 1 : 0; |
| 571 | } |
| 572 | |
| 573 | static void do_exec_test(void) |
| 574 | { |
| 575 | printf("[RUN]\tTest exec\n"); |
| 576 | |
| 577 | struct user_desc desc = { |
| 578 | .entry_number = 0, |
| 579 | .base_addr = 0, |
| 580 | .limit = 42, |
| 581 | .seg_32bit = 1, |
| 582 | .contents = 2, /* Code, not conforming */ |
| 583 | .read_exec_only = 0, |
| 584 | .limit_in_pages = 0, |
| 585 | .seg_not_present = 0, |
| 586 | .useable = 0 |
| 587 | }; |
| 588 | install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB); |
| 589 | |
| 590 | pid_t child = fork(); |
| 591 | if (child == 0) { |
| 592 | execl("/proc/self/exe", "ldt_gdt_test_exec", NULL); |
| 593 | printf("[FAIL]\tCould not exec self\n"); |
| 594 | exit(1); /* exec failed */ |
| 595 | } else { |
| 596 | int status; |
| 597 | if (waitpid(child, &status, 0) != child || |
| 598 | !WIFEXITED(status)) { |
| 599 | printf("[FAIL]\tChild died\n"); |
| 600 | nerrs++; |
| 601 | } else if (WEXITSTATUS(status) != 0) { |
| 602 | printf("[FAIL]\tChild failed\n"); |
| 603 | nerrs++; |
| 604 | } else { |
| 605 | printf("[OK]\tChild succeeded\n"); |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | int main(int argc, char **argv) |
| 611 | { |
| 612 | if (argc == 1 && !strcmp(argv[0], "ldt_gdt_test_exec")) |
| 613 | return finish_exec_test(); |
| 614 | |
| 615 | do_simple_tests(); |
| 616 | |
| 617 | do_multicpu_tests(); |
| 618 | |
| 619 | do_exec_test(); |
| 620 | |
| 621 | return nerrs ? 1 : 0; |
| 622 | } |