2 * ldt_gdt.c - Test cases for LDT and GDT access
3 * Copyright (c) 2015 Andrew Lutomirski
16 #include <sys/syscall.h>
18 #include <sys/types.h>
23 #include <linux/futex.h>
25 #include <asm/prctl.h>
26 #include <sys/prctl.h>
28 #define AR_ACCESSED (1<<8)
30 #define AR_TYPE_RODATA (0 * (1<<9))
31 #define AR_TYPE_RWDATA (1 * (1<<9))
32 #define AR_TYPE_RODATA_EXPDOWN (2 * (1<<9))
33 #define AR_TYPE_RWDATA_EXPDOWN (3 * (1<<9))
34 #define AR_TYPE_XOCODE (4 * (1<<9))
35 #define AR_TYPE_XRCODE (5 * (1<<9))
36 #define AR_TYPE_XOCODE_CONF (6 * (1<<9))
37 #define AR_TYPE_XRCODE_CONF (7 * (1<<9))
39 #define AR_DPL3 (3 * (1<<13))
41 #define AR_S (1 << 12)
42 #define AR_P (1 << 15)
43 #define AR_AVL (1 << 20)
44 #define AR_L (1 << 21)
45 #define AR_DB (1 << 22)
46 #define AR_G (1 << 23)
49 # define INT80_CLOBBERS "r8", "r9", "r10", "r11"
51 # define INT80_CLOBBERS
56 /* Points to an array of 1024 ints, each holding its own index. */
57 static const unsigned int *counter_page;
58 static struct user_desc *low_user_desc;
59 static struct user_desc *low_user_desc_clear; /* Use to delete GDT entry */
60 static int gdt_entry_num;
62 static void check_invalid_segment(uint16_t index, int ldt)
64 uint32_t has_limit = 0, has_ar = 0, limit, ar;
65 uint32_t selector = (index << 3) | (ldt << 2) | 3;
67 asm ("lsl %[selector], %[limit]\n\t"
69 "movl $1, %[has_limit]\n\t"
71 : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
72 : [selector] "r" (selector));
73 asm ("larl %[selector], %[ar]\n\t"
75 "movl $1, %[has_ar]\n\t"
77 : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
78 : [selector] "r" (selector));
80 if (has_limit || has_ar) {
81 printf("[FAIL]\t%s entry %hu is valid but should be invalid\n",
82 (ldt ? "LDT" : "GDT"), index);
85 printf("[OK]\t%s entry %hu is invalid\n",
86 (ldt ? "LDT" : "GDT"), index);
90 static void check_valid_segment(uint16_t index, int ldt,
91 uint32_t expected_ar, uint32_t expected_limit,
94 uint32_t has_limit = 0, has_ar = 0, limit, ar;
95 uint32_t selector = (index << 3) | (ldt << 2) | 3;
97 asm ("lsl %[selector], %[limit]\n\t"
99 "movl $1, %[has_limit]\n\t"
101 : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
102 : [selector] "r" (selector));
103 asm ("larl %[selector], %[ar]\n\t"
105 "movl $1, %[has_ar]\n\t"
107 : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
108 : [selector] "r" (selector));
110 if (!has_limit || !has_ar) {
111 printf("[FAIL]\t%s entry %hu is invalid but should be valid\n",
112 (ldt ? "LDT" : "GDT"), index);
117 if (ar != expected_ar) {
118 printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n",
119 (ldt ? "LDT" : "GDT"), index, ar, expected_ar);
121 } else if (limit != expected_limit) {
122 printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n",
123 (ldt ? "LDT" : "GDT"), index, limit, expected_limit);
125 } else if (verbose) {
126 printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n",
127 (ldt ? "LDT" : "GDT"), index, ar, limit);
131 static bool install_valid_mode(const struct user_desc *desc, uint32_t ar,
134 int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
135 desc, sizeof(*desc));
139 uint32_t limit = desc->limit;
140 if (desc->limit_in_pages)
141 limit = (limit << 12) + 4095;
142 check_valid_segment(desc->entry_number, 1, ar, limit, true);
144 } else if (errno == ENOSYS) {
145 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
148 if (desc->seg_32bit) {
149 printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
154 printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
160 static bool install_valid(const struct user_desc *desc, uint32_t ar)
162 return install_valid_mode(desc, ar, false);
165 static void install_invalid(const struct user_desc *desc, bool oldmode)
167 int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
168 desc, sizeof(*desc));
172 check_invalid_segment(desc->entry_number, 1);
173 } else if (errno == ENOSYS) {
174 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
176 if (desc->seg_32bit) {
177 printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
181 printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
186 static int safe_modify_ldt(int func, struct user_desc *ptr,
187 unsigned long bytecount)
189 int ret = syscall(SYS_modify_ldt, 0x11, ptr, bytecount);
195 static void fail_install(struct user_desc *desc)
197 if (safe_modify_ldt(0x11, desc, sizeof(*desc)) == 0) {
198 printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n");
200 } else if (errno == ENOSYS) {
201 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
203 printf("[OK]\tmodify_ldt failure %d\n", errno);
207 static void do_simple_tests(void)
209 struct user_desc desc = {
214 .contents = 2, /* Code, not conforming */
217 .seg_not_present = 0,
220 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
222 desc.limit_in_pages = 1;
223 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
224 AR_S | AR_P | AR_DB | AR_G);
226 check_invalid_segment(1, 1);
228 desc.entry_number = 2;
229 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
230 AR_S | AR_P | AR_DB | AR_G);
232 check_invalid_segment(1, 1);
234 desc.base_addr = 0xf0000000;
235 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
236 AR_S | AR_P | AR_DB | AR_G);
239 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
240 AR_S | AR_P | AR_DB | AR_G | AR_AVL);
242 desc.seg_not_present = 1;
243 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
244 AR_S | AR_DB | AR_G | AR_AVL);
247 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
248 AR_S | AR_G | AR_AVL);
252 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA |
253 AR_S | AR_DB | AR_G | AR_AVL);
255 desc.read_exec_only = 1;
256 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA |
257 AR_S | AR_DB | AR_G | AR_AVL);
260 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN |
261 AR_S | AR_DB | AR_G | AR_AVL);
263 desc.read_exec_only = 0;
264 desc.limit_in_pages = 0;
265 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN |
266 AR_S | AR_DB | AR_AVL);
269 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE_CONF |
270 AR_S | AR_DB | AR_AVL);
272 desc.read_exec_only = 1;
273 install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE_CONF |
274 AR_S | AR_DB | AR_AVL);
276 desc.read_exec_only = 0;
278 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
279 AR_S | AR_DB | AR_AVL);
281 desc.read_exec_only = 1;
285 install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
286 AR_S | AR_DB | AR_AVL);
290 bool entry1_okay = install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
291 AR_S | AR_DB | AR_AVL);
294 printf("[RUN]\tTest fork\n");
295 pid_t child = fork();
298 check_valid_segment(desc.entry_number, 1,
299 AR_DPL3 | AR_TYPE_XOCODE |
300 AR_S | AR_DB | AR_AVL, desc.limit,
302 check_invalid_segment(1, 1);
306 if (waitpid(child, &status, 0) != child ||
307 !WIFEXITED(status)) {
308 printf("[FAIL]\tChild died\n");
310 } else if (WEXITSTATUS(status) != 0) {
311 printf("[FAIL]\tChild failed\n");
314 printf("[OK]\tChild succeeded\n");
318 printf("[RUN]\tTest size\n");
320 for (i = 0; i < 8192; i++) {
321 desc.entry_number = i;
323 if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
324 printf("[FAIL]\tFailed to install entry %d\n", i);
329 for (int j = 0; j < i; j++) {
330 check_valid_segment(j, 1, AR_DPL3 | AR_TYPE_XOCODE |
331 AR_S | AR_DB | AR_AVL, j, false);
333 printf("[DONE]\tSize test\n");
335 printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n");
338 /* Test entry_number too high. */
339 desc.entry_number = 8192;
342 /* Test deletion and actions mistakeable for deletion. */
343 memset(&desc, 0, sizeof(desc));
344 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P);
346 desc.seg_not_present = 1;
347 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
349 desc.seg_not_present = 0;
350 desc.read_exec_only = 1;
351 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P);
353 desc.read_exec_only = 0;
354 desc.seg_not_present = 1;
355 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
357 desc.read_exec_only = 1;
359 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
363 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
366 install_invalid(&desc, false);
368 desc.seg_not_present = 0;
370 desc.read_exec_only = 0;
371 desc.limit = 0xfffff;
373 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB);
375 desc.limit_in_pages = 1;
377 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB | AR_G);
378 desc.read_exec_only = 1;
379 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P | AR_DB | AR_G);
381 desc.read_exec_only = 0;
382 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN | AR_S | AR_P | AR_DB | AR_G);
383 desc.read_exec_only = 1;
384 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN | AR_S | AR_P | AR_DB | AR_G);
387 install_invalid(&desc, true);
393 * 2: thread should clear LDT entry 0
394 * 3: thread should exit
396 static volatile unsigned int ftx;
398 static void *threadproc(void *ctx)
403 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
404 err(1, "sched_setaffinity to CPU 1"); /* should never fail */
407 syscall(SYS_futex, &ftx, FUTEX_WAIT, 0, NULL, NULL, 0);
413 /* clear LDT entry 0 */
414 const struct user_desc desc = {};
415 if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) != 0)
416 err(1, "modify_ldt");
418 /* If ftx == 2, set it to zero. If ftx == 100, quit. */
420 asm volatile ("lock xaddl %[x], %[ftx]" :
421 [x] "+r" (x), [ftx] "+m" (ftx));
430 #define SA_RESTORER 0x04000000
434 * The UAPI header calls this 'struct sigaction', which conflicts with
437 struct fake_ksigaction {
438 void *handler; /* the real type is nasty */
439 unsigned long sa_flags;
440 void (*sa_restorer)(void);
441 unsigned char sigset[8];
444 static void fix_sa_restorer(int sig)
446 struct fake_ksigaction ksa;
448 if (syscall(SYS_rt_sigaction, sig, NULL, &ksa, 8) == 0) {
450 * glibc has a nasty bug: it sometimes writes garbage to
451 * sa_restorer. This interacts quite badly with anything
452 * that fiddles with SS because it can trigger legacy
453 * stack switching. Patch it up. See:
455 * https://sourceware.org/bugzilla/show_bug.cgi?id=21269
457 if (!(ksa.sa_flags & SA_RESTORER) && ksa.sa_restorer) {
458 ksa.sa_restorer = NULL;
459 if (syscall(SYS_rt_sigaction, sig, &ksa, NULL,
460 sizeof(ksa.sigset)) != 0)
461 err(1, "rt_sigaction");
466 static void fix_sa_restorer(int sig)
468 /* 64-bit glibc works fine. */
472 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
476 memset(&sa, 0, sizeof(sa));
477 sa.sa_sigaction = handler;
478 sa.sa_flags = SA_SIGINFO | flags;
479 sigemptyset(&sa.sa_mask);
480 if (sigaction(sig, &sa, 0))
483 fix_sa_restorer(sig);
486 static jmp_buf jmpbuf;
488 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
490 siglongjmp(jmpbuf, 1);
493 static void do_multicpu_tests(void)
497 int failures = 0, iters = 5, i;
498 unsigned short orig_ss;
502 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
503 printf("[SKIP]\tCannot set affinity to CPU 1\n");
509 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
510 printf("[SKIP]\tCannot set affinity to CPU 0\n");
514 sethandler(SIGSEGV, sigsegv, 0);
516 /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
517 sethandler(SIGILL, sigsegv, 0);
520 printf("[RUN]\tCross-CPU LDT invalidation\n");
522 if (pthread_create(&thread, 0, threadproc, 0) != 0)
523 err(1, "pthread_create");
525 asm volatile ("mov %%ss, %0" : "=rm" (orig_ss));
527 for (i = 0; i < 5; i++) {
528 if (sigsetjmp(jmpbuf, 1) != 0)
531 /* Make sure the thread is ready after the last test. */
535 struct user_desc desc = {
540 .contents = 0, /* Data */
543 .seg_not_present = 0,
547 if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
549 err(1, "modify_ldt");
550 printf("[SKIP]\tmodify_ldt unavailable\n");
554 /* Arm the thread. */
556 syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
558 asm volatile ("mov %0, %%ss" : : "r" (0x7));
567 * On success, modify_ldt will segfault us synchronously,
568 * and we'll escape via siglongjmp.
572 asm volatile ("mov %0, %%ss" : : "rm" (orig_ss));
575 ftx = 100; /* Kill the thread. */
576 syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
578 if (pthread_join(thread, NULL) != 0)
579 err(1, "pthread_join");
582 printf("[FAIL]\t%d of %d iterations failed\n", failures, iters);
585 printf("[OK]\tAll %d iterations succeeded\n", iters);
589 static int finish_exec_test(void)
592 * In a sensible world, this would be check_invalid_segment(0, 1);
593 * For better or for worse, though, the LDT is inherited across exec.
594 * We can probably change this safely, but for now we test it.
596 check_valid_segment(0, 1,
597 AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB,
600 return nerrs ? 1 : 0;
603 static void do_exec_test(void)
605 printf("[RUN]\tTest exec\n");
607 struct user_desc desc = {
612 .contents = 2, /* Code, not conforming */
615 .seg_not_present = 0,
618 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
620 pid_t child = fork();
622 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL);
623 printf("[FAIL]\tCould not exec self\n");
624 exit(1); /* exec failed */
627 if (waitpid(child, &status, 0) != child ||
628 !WIFEXITED(status)) {
629 printf("[FAIL]\tChild died\n");
631 } else if (WEXITSTATUS(status) != 0) {
632 printf("[FAIL]\tChild failed\n");
635 printf("[OK]\tChild succeeded\n");
640 static void setup_counter_page(void)
642 unsigned int *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
643 MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
644 if (page == MAP_FAILED)
647 for (int i = 0; i < 1024; i++)
652 static int invoke_set_thread_area(void)
655 asm volatile ("int $0x80"
656 : "=a" (ret), "+m" (low_user_desc) :
657 "a" (243), "b" (low_user_desc)
662 static void setup_low_user_desc(void)
664 low_user_desc = mmap(NULL, 2 * sizeof(struct user_desc),
665 PROT_READ | PROT_WRITE,
666 MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
667 if (low_user_desc == MAP_FAILED)
670 low_user_desc->entry_number = -1;
671 low_user_desc->base_addr = (unsigned long)&counter_page[1];
672 low_user_desc->limit = 0xfffff;
673 low_user_desc->seg_32bit = 1;
674 low_user_desc->contents = 0; /* Data, grow-up*/
675 low_user_desc->read_exec_only = 0;
676 low_user_desc->limit_in_pages = 1;
677 low_user_desc->seg_not_present = 0;
678 low_user_desc->useable = 0;
680 if (invoke_set_thread_area() == 0) {
681 gdt_entry_num = low_user_desc->entry_number;
682 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num);
684 printf("[NOTE]\tset_thread_area is unavailable\n");
687 low_user_desc_clear = low_user_desc + 1;
688 low_user_desc_clear->entry_number = gdt_entry_num;
689 low_user_desc_clear->read_exec_only = 1;
690 low_user_desc_clear->seg_not_present = 1;
693 static void test_gdt_invalidation(void)
696 return; /* 64-bit only system -- we can't use set_thread_area */
698 unsigned short prev_sel;
703 unsigned long saved_base;
704 unsigned long new_base;
708 invoke_set_thread_area();
710 sel = (gdt_entry_num << 3) | 3;
711 asm volatile ("movw %%ds, %[prev_sel]\n\t"
712 "movw %[sel], %%ds\n\t"
716 "movl %[arg1], %%ebx\n\t"
717 "int $0x80\n\t" /* Should invalidate ds */
721 "movw %%ds, %[sel]\n\t"
722 "movw %[prev_sel], %%ds"
723 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
725 : "m" (low_user_desc_clear),
726 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
735 printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
739 invoke_set_thread_area();
741 sel = (gdt_entry_num << 3) | 3;
742 asm volatile ("movw %%es, %[prev_sel]\n\t"
743 "movw %[sel], %%es\n\t"
747 "movl %[arg1], %%ebx\n\t"
748 "int $0x80\n\t" /* Should invalidate es */
752 "movw %%es, %[sel]\n\t"
753 "movw %[prev_sel], %%es"
754 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
756 : "m" (low_user_desc_clear),
757 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
766 printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
770 invoke_set_thread_area();
772 sel = (gdt_entry_num << 3) | 3;
774 syscall(SYS_arch_prctl, ARCH_GET_FS, &saved_base);
776 asm volatile ("movw %%fs, %[prev_sel]\n\t"
777 "movw %[sel], %%fs\n\t"
781 "movl %[arg1], %%ebx\n\t"
782 "int $0x80\n\t" /* Should invalidate fs */
786 "movw %%fs, %[sel]\n\t"
787 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
789 : "m" (low_user_desc_clear),
790 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
794 syscall(SYS_arch_prctl, ARCH_GET_FS, &new_base);
797 /* Restore FS/BASE for glibc */
798 asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel] "rm" (prev_sel));
801 syscall(SYS_arch_prctl, ARCH_SET_FS, saved_base);
810 printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
814 if (sel == 0 && new_base != 0) {
816 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base);
818 printf("[OK]\tNew FSBASE was zero\n");
823 invoke_set_thread_area();
825 sel = (gdt_entry_num << 3) | 3;
827 syscall(SYS_arch_prctl, ARCH_GET_GS, &saved_base);
829 asm volatile ("movw %%gs, %[prev_sel]\n\t"
830 "movw %[sel], %%gs\n\t"
834 "movl %[arg1], %%ebx\n\t"
835 "int $0x80\n\t" /* Should invalidate gs */
839 "movw %%gs, %[sel]\n\t"
840 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
842 : "m" (low_user_desc_clear),
843 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
847 syscall(SYS_arch_prctl, ARCH_GET_GS, &new_base);
850 /* Restore GS/BASE for glibc */
851 asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel] "rm" (prev_sel));
854 syscall(SYS_arch_prctl, ARCH_SET_GS, saved_base);
863 printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
867 if (sel == 0 && new_base != 0) {
869 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base);
871 printf("[OK]\tNew GSBASE was zero\n");
876 int main(int argc, char **argv)
878 if (argc == 1 && !strcmp(argv[0], "ldt_gdt_test_exec"))
879 return finish_exec_test();
881 setup_counter_page();
882 setup_low_user_desc();
890 test_gdt_invalidation();
892 return nerrs ? 1 : 0;