1 // SPDX-License-Identifier: GPL-2.0
3 * ldt_gdt.c - Test cases for LDT and GDT access
4 * Copyright (c) 2015 Andrew Lutomirski
17 #include <sys/syscall.h>
19 #include <sys/types.h>
24 #include <linux/futex.h>
26 #include <asm/prctl.h>
27 #include <sys/prctl.h>
29 #define AR_ACCESSED (1<<8)
31 #define AR_TYPE_RODATA (0 * (1<<9))
32 #define AR_TYPE_RWDATA (1 * (1<<9))
33 #define AR_TYPE_RODATA_EXPDOWN (2 * (1<<9))
34 #define AR_TYPE_RWDATA_EXPDOWN (3 * (1<<9))
35 #define AR_TYPE_XOCODE (4 * (1<<9))
36 #define AR_TYPE_XRCODE (5 * (1<<9))
37 #define AR_TYPE_XOCODE_CONF (6 * (1<<9))
38 #define AR_TYPE_XRCODE_CONF (7 * (1<<9))
40 #define AR_DPL3 (3 * (1<<13))
42 #define AR_S (1 << 12)
43 #define AR_P (1 << 15)
44 #define AR_AVL (1 << 20)
45 #define AR_L (1 << 21)
46 #define AR_DB (1 << 22)
47 #define AR_G (1 << 23)
50 # define INT80_CLOBBERS "r8", "r9", "r10", "r11"
52 # define INT80_CLOBBERS
57 /* Points to an array of 1024 ints, each holding its own index. */
58 static const unsigned int *counter_page;
59 static struct user_desc *low_user_desc;
60 static struct user_desc *low_user_desc_clear; /* Use to delete GDT entry */
61 static int gdt_entry_num;
63 static void check_invalid_segment(uint16_t index, int ldt)
65 uint32_t has_limit = 0, has_ar = 0, limit, ar;
66 uint32_t selector = (index << 3) | (ldt << 2) | 3;
68 asm ("lsl %[selector], %[limit]\n\t"
70 "movl $1, %[has_limit]\n\t"
72 : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
73 : [selector] "r" (selector));
74 asm ("larl %[selector], %[ar]\n\t"
76 "movl $1, %[has_ar]\n\t"
78 : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
79 : [selector] "r" (selector));
81 if (has_limit || has_ar) {
82 printf("[FAIL]\t%s entry %hu is valid but should be invalid\n",
83 (ldt ? "LDT" : "GDT"), index);
86 printf("[OK]\t%s entry %hu is invalid\n",
87 (ldt ? "LDT" : "GDT"), index);
91 static void check_valid_segment(uint16_t index, int ldt,
92 uint32_t expected_ar, uint32_t expected_limit,
95 uint32_t has_limit = 0, has_ar = 0, limit, ar;
96 uint32_t selector = (index << 3) | (ldt << 2) | 3;
98 asm ("lsl %[selector], %[limit]\n\t"
100 "movl $1, %[has_limit]\n\t"
102 : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
103 : [selector] "r" (selector));
104 asm ("larl %[selector], %[ar]\n\t"
106 "movl $1, %[has_ar]\n\t"
108 : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
109 : [selector] "r" (selector));
111 if (!has_limit || !has_ar) {
112 printf("[FAIL]\t%s entry %hu is invalid but should be valid\n",
113 (ldt ? "LDT" : "GDT"), index);
118 /* The SDM says "bits 19:16 are undefined". Thanks. */
122 * NB: Different Linux versions do different things with the
123 * accessed bit in set_thread_area().
125 if (ar != expected_ar &&
126 (ldt || ar != (expected_ar | AR_ACCESSED))) {
127 printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n",
128 (ldt ? "LDT" : "GDT"), index, ar, expected_ar);
130 } else if (limit != expected_limit) {
131 printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n",
132 (ldt ? "LDT" : "GDT"), index, limit, expected_limit);
134 } else if (verbose) {
135 printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n",
136 (ldt ? "LDT" : "GDT"), index, ar, limit);
140 static bool install_valid_mode(const struct user_desc *d, uint32_t ar,
141 bool oldmode, bool ldt)
143 struct user_desc desc = *d;
148 /* No point testing set_thread_area in a 64-bit build */
153 desc.entry_number = gdt_entry_num;
155 ret = syscall(SYS_set_thread_area, &desc);
157 ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
158 &desc, sizeof(desc));
163 if (ret != 0 && errno == ENOSYS) {
164 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
170 uint32_t limit = desc.limit;
171 if (desc.limit_in_pages)
172 limit = (limit << 12) + 4095;
173 check_valid_segment(desc.entry_number, ldt, ar, limit, true);
176 if (desc.seg_32bit) {
177 printf("[FAIL]\tUnexpected %s failure %d\n",
178 ldt ? "modify_ldt" : "set_thread_area",
183 printf("[OK]\t%s rejected 16 bit segment\n",
184 ldt ? "modify_ldt" : "set_thread_area");
190 static bool install_valid(const struct user_desc *desc, uint32_t ar)
192 bool ret = install_valid_mode(desc, ar, false, true);
194 if (desc->contents <= 1 && desc->seg_32bit &&
195 !desc->seg_not_present) {
196 /* Should work in the GDT, too. */
197 install_valid_mode(desc, ar, false, false);
203 static void install_invalid(const struct user_desc *desc, bool oldmode)
205 int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
206 desc, sizeof(*desc));
210 check_invalid_segment(desc->entry_number, 1);
211 } else if (errno == ENOSYS) {
212 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
214 if (desc->seg_32bit) {
215 printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
219 printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
224 static int safe_modify_ldt(int func, struct user_desc *ptr,
225 unsigned long bytecount)
227 int ret = syscall(SYS_modify_ldt, 0x11, ptr, bytecount);
233 static void fail_install(struct user_desc *desc)
235 if (safe_modify_ldt(0x11, desc, sizeof(*desc)) == 0) {
236 printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n");
238 } else if (errno == ENOSYS) {
239 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
241 printf("[OK]\tmodify_ldt failure %d\n", errno);
245 static void do_simple_tests(void)
247 struct user_desc desc = {
252 .contents = 2, /* Code, not conforming */
255 .seg_not_present = 0,
258 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
260 desc.limit_in_pages = 1;
261 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
262 AR_S | AR_P | AR_DB | AR_G);
264 check_invalid_segment(1, 1);
266 desc.entry_number = 2;
267 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
268 AR_S | AR_P | AR_DB | AR_G);
270 check_invalid_segment(1, 1);
272 desc.base_addr = 0xf0000000;
273 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
274 AR_S | AR_P | AR_DB | AR_G);
277 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
278 AR_S | AR_P | AR_DB | AR_G | AR_AVL);
280 desc.seg_not_present = 1;
281 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
282 AR_S | AR_DB | AR_G | AR_AVL);
285 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
286 AR_S | AR_G | AR_AVL);
290 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA |
291 AR_S | AR_DB | AR_G | AR_AVL);
293 desc.read_exec_only = 1;
294 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA |
295 AR_S | AR_DB | AR_G | AR_AVL);
298 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN |
299 AR_S | AR_DB | AR_G | AR_AVL);
301 desc.read_exec_only = 0;
302 desc.limit_in_pages = 0;
303 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN |
304 AR_S | AR_DB | AR_AVL);
307 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE_CONF |
308 AR_S | AR_DB | AR_AVL);
310 desc.read_exec_only = 1;
311 install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE_CONF |
312 AR_S | AR_DB | AR_AVL);
314 desc.read_exec_only = 0;
316 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
317 AR_S | AR_DB | AR_AVL);
319 desc.read_exec_only = 1;
323 install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
324 AR_S | AR_DB | AR_AVL);
328 bool entry1_okay = install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
329 AR_S | AR_DB | AR_AVL);
332 printf("[RUN]\tTest fork\n");
333 pid_t child = fork();
336 check_valid_segment(desc.entry_number, 1,
337 AR_DPL3 | AR_TYPE_XOCODE |
338 AR_S | AR_DB | AR_AVL, desc.limit,
340 check_invalid_segment(1, 1);
344 if (waitpid(child, &status, 0) != child ||
345 !WIFEXITED(status)) {
346 printf("[FAIL]\tChild died\n");
348 } else if (WEXITSTATUS(status) != 0) {
349 printf("[FAIL]\tChild failed\n");
352 printf("[OK]\tChild succeeded\n");
356 printf("[RUN]\tTest size\n");
358 for (i = 0; i < 8192; i++) {
359 desc.entry_number = i;
361 if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
362 printf("[FAIL]\tFailed to install entry %d\n", i);
367 for (int j = 0; j < i; j++) {
368 check_valid_segment(j, 1, AR_DPL3 | AR_TYPE_XOCODE |
369 AR_S | AR_DB | AR_AVL, j, false);
371 printf("[DONE]\tSize test\n");
373 printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n");
376 /* Test entry_number too high. */
377 desc.entry_number = 8192;
380 /* Test deletion and actions mistakeable for deletion. */
381 memset(&desc, 0, sizeof(desc));
382 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P);
384 desc.seg_not_present = 1;
385 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
387 desc.seg_not_present = 0;
388 desc.read_exec_only = 1;
389 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P);
391 desc.read_exec_only = 0;
392 desc.seg_not_present = 1;
393 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
395 desc.read_exec_only = 1;
397 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
401 install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
404 install_invalid(&desc, false);
406 desc.seg_not_present = 0;
407 desc.read_exec_only = 0;
409 install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB);
410 install_invalid(&desc, true);
416 * 2: thread should clear LDT entry 0
417 * 3: thread should exit
419 static volatile unsigned int ftx;
421 static void *threadproc(void *ctx)
426 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
427 err(1, "sched_setaffinity to CPU 1"); /* should never fail */
430 syscall(SYS_futex, &ftx, FUTEX_WAIT, 0, NULL, NULL, 0);
436 /* clear LDT entry 0 */
437 const struct user_desc desc = {};
438 if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) != 0)
439 err(1, "modify_ldt");
441 /* If ftx == 2, set it to zero. If ftx == 100, quit. */
443 asm volatile ("lock xaddl %[x], %[ftx]" :
444 [x] "+r" (x), [ftx] "+m" (ftx));
453 #define SA_RESTORER 0x04000000
457 * The UAPI header calls this 'struct sigaction', which conflicts with
460 struct fake_ksigaction {
461 void *handler; /* the real type is nasty */
462 unsigned long sa_flags;
463 void (*sa_restorer)(void);
464 unsigned char sigset[8];
467 static void fix_sa_restorer(int sig)
469 struct fake_ksigaction ksa;
471 if (syscall(SYS_rt_sigaction, sig, NULL, &ksa, 8) == 0) {
473 * glibc has a nasty bug: it sometimes writes garbage to
474 * sa_restorer. This interacts quite badly with anything
475 * that fiddles with SS because it can trigger legacy
476 * stack switching. Patch it up. See:
478 * https://sourceware.org/bugzilla/show_bug.cgi?id=21269
480 if (!(ksa.sa_flags & SA_RESTORER) && ksa.sa_restorer) {
481 ksa.sa_restorer = NULL;
482 if (syscall(SYS_rt_sigaction, sig, &ksa, NULL,
483 sizeof(ksa.sigset)) != 0)
484 err(1, "rt_sigaction");
489 static void fix_sa_restorer(int sig)
491 /* 64-bit glibc works fine. */
495 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
499 memset(&sa, 0, sizeof(sa));
500 sa.sa_sigaction = handler;
501 sa.sa_flags = SA_SIGINFO | flags;
502 sigemptyset(&sa.sa_mask);
503 if (sigaction(sig, &sa, 0))
506 fix_sa_restorer(sig);
509 static jmp_buf jmpbuf;
511 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
513 siglongjmp(jmpbuf, 1);
516 static void do_multicpu_tests(void)
520 int failures = 0, iters = 5, i;
521 unsigned short orig_ss;
525 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
526 printf("[SKIP]\tCannot set affinity to CPU 1\n");
532 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
533 printf("[SKIP]\tCannot set affinity to CPU 0\n");
537 sethandler(SIGSEGV, sigsegv, 0);
539 /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
540 sethandler(SIGILL, sigsegv, 0);
543 printf("[RUN]\tCross-CPU LDT invalidation\n");
545 if (pthread_create(&thread, 0, threadproc, 0) != 0)
546 err(1, "pthread_create");
548 asm volatile ("mov %%ss, %0" : "=rm" (orig_ss));
550 for (i = 0; i < 5; i++) {
551 if (sigsetjmp(jmpbuf, 1) != 0)
554 /* Make sure the thread is ready after the last test. */
558 struct user_desc desc = {
563 .contents = 0, /* Data */
566 .seg_not_present = 0,
570 if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
572 err(1, "modify_ldt");
573 printf("[SKIP]\tmodify_ldt unavailable\n");
577 /* Arm the thread. */
579 syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
581 asm volatile ("mov %0, %%ss" : : "r" (0x7));
590 * On success, modify_ldt will segfault us synchronously,
591 * and we'll escape via siglongjmp.
595 asm volatile ("mov %0, %%ss" : : "rm" (orig_ss));
598 ftx = 100; /* Kill the thread. */
599 syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
601 if (pthread_join(thread, NULL) != 0)
602 err(1, "pthread_join");
605 printf("[FAIL]\t%d of %d iterations failed\n", failures, iters);
608 printf("[OK]\tAll %d iterations succeeded\n", iters);
612 static int finish_exec_test(void)
615 * In a sensible world, this would be check_invalid_segment(0, 1);
616 * For better or for worse, though, the LDT is inherited across exec.
617 * We can probably change this safely, but for now we test it.
619 check_valid_segment(0, 1,
620 AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB,
623 return nerrs ? 1 : 0;
626 static void do_exec_test(void)
628 printf("[RUN]\tTest exec\n");
630 struct user_desc desc = {
635 .contents = 2, /* Code, not conforming */
638 .seg_not_present = 0,
641 install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
643 pid_t child = fork();
645 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL);
646 printf("[FAIL]\tCould not exec self\n");
647 exit(1); /* exec failed */
650 if (waitpid(child, &status, 0) != child ||
651 !WIFEXITED(status)) {
652 printf("[FAIL]\tChild died\n");
654 } else if (WEXITSTATUS(status) != 0) {
655 printf("[FAIL]\tChild failed\n");
658 printf("[OK]\tChild succeeded\n");
663 static void setup_counter_page(void)
665 unsigned int *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
666 MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
667 if (page == MAP_FAILED)
670 for (int i = 0; i < 1024; i++)
675 static int invoke_set_thread_area(void)
678 asm volatile ("int $0x80"
679 : "=a" (ret), "+m" (low_user_desc) :
680 "a" (243), "b" (low_user_desc)
685 static void setup_low_user_desc(void)
687 low_user_desc = mmap(NULL, 2 * sizeof(struct user_desc),
688 PROT_READ | PROT_WRITE,
689 MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
690 if (low_user_desc == MAP_FAILED)
693 low_user_desc->entry_number = -1;
694 low_user_desc->base_addr = (unsigned long)&counter_page[1];
695 low_user_desc->limit = 0xfffff;
696 low_user_desc->seg_32bit = 1;
697 low_user_desc->contents = 0; /* Data, grow-up*/
698 low_user_desc->read_exec_only = 0;
699 low_user_desc->limit_in_pages = 1;
700 low_user_desc->seg_not_present = 0;
701 low_user_desc->useable = 0;
703 if (invoke_set_thread_area() == 0) {
704 gdt_entry_num = low_user_desc->entry_number;
705 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num);
707 printf("[NOTE]\tset_thread_area is unavailable\n");
710 low_user_desc_clear = low_user_desc + 1;
711 low_user_desc_clear->entry_number = gdt_entry_num;
712 low_user_desc_clear->read_exec_only = 1;
713 low_user_desc_clear->seg_not_present = 1;
716 static void test_gdt_invalidation(void)
719 return; /* 64-bit only system -- we can't use set_thread_area */
721 unsigned short prev_sel;
726 unsigned long saved_base;
727 unsigned long new_base;
731 invoke_set_thread_area();
733 sel = (gdt_entry_num << 3) | 3;
734 asm volatile ("movw %%ds, %[prev_sel]\n\t"
735 "movw %[sel], %%ds\n\t"
739 "movl %[arg1], %%ebx\n\t"
740 "int $0x80\n\t" /* Should invalidate ds */
744 "movw %%ds, %[sel]\n\t"
745 "movw %[prev_sel], %%ds"
746 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
748 : "m" (low_user_desc_clear),
749 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
758 printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
762 invoke_set_thread_area();
764 sel = (gdt_entry_num << 3) | 3;
765 asm volatile ("movw %%es, %[prev_sel]\n\t"
766 "movw %[sel], %%es\n\t"
770 "movl %[arg1], %%ebx\n\t"
771 "int $0x80\n\t" /* Should invalidate es */
775 "movw %%es, %[sel]\n\t"
776 "movw %[prev_sel], %%es"
777 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
779 : "m" (low_user_desc_clear),
780 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
789 printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
793 invoke_set_thread_area();
795 sel = (gdt_entry_num << 3) | 3;
797 syscall(SYS_arch_prctl, ARCH_GET_FS, &saved_base);
799 asm volatile ("movw %%fs, %[prev_sel]\n\t"
800 "movw %[sel], %%fs\n\t"
804 "movl %[arg1], %%ebx\n\t"
805 "int $0x80\n\t" /* Should invalidate fs */
809 "movw %%fs, %[sel]\n\t"
810 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
812 : "m" (low_user_desc_clear),
813 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
817 syscall(SYS_arch_prctl, ARCH_GET_FS, &new_base);
820 /* Restore FS/BASE for glibc */
821 asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel] "rm" (prev_sel));
824 syscall(SYS_arch_prctl, ARCH_SET_FS, saved_base);
833 printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
837 if (sel == 0 && new_base != 0) {
839 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base);
841 printf("[OK]\tNew FSBASE was zero\n");
846 invoke_set_thread_area();
848 sel = (gdt_entry_num << 3) | 3;
850 syscall(SYS_arch_prctl, ARCH_GET_GS, &saved_base);
852 asm volatile ("movw %%gs, %[prev_sel]\n\t"
853 "movw %[sel], %%gs\n\t"
857 "movl %[arg1], %%ebx\n\t"
858 "int $0x80\n\t" /* Should invalidate gs */
862 "movw %%gs, %[sel]\n\t"
863 : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
865 : "m" (low_user_desc_clear),
866 [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
870 syscall(SYS_arch_prctl, ARCH_GET_GS, &new_base);
873 /* Restore GS/BASE for glibc */
874 asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel] "rm" (prev_sel));
877 syscall(SYS_arch_prctl, ARCH_SET_GS, saved_base);
886 printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
890 if (sel == 0 && new_base != 0) {
892 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base);
894 printf("[OK]\tNew GSBASE was zero\n");
899 int main(int argc, char **argv)
901 if (argc == 1 && !strcmp(argv[0], "ldt_gdt_test_exec"))
902 return finish_exec_test();
904 setup_counter_page();
905 setup_low_user_desc();
913 test_gdt_invalidation();
915 return nerrs ? 1 : 0;