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)
50 /* Points to an array of 1024 ints, each holding its own index. */
51 static const unsigned int *counter_page
;
52 static struct user_desc
*low_user_desc
;
53 static struct user_desc
*low_user_desc_clear
; /* Use to delete GDT entry */
54 static int gdt_entry_num
;
56 static void check_invalid_segment(uint16_t index
, int ldt
)
58 uint32_t has_limit
= 0, has_ar
= 0, limit
, ar
;
59 uint32_t selector
= (index
<< 3) | (ldt
<< 2) | 3;
61 asm ("lsl %[selector], %[limit]\n\t"
63 "movl $1, %[has_limit]\n\t"
65 : [limit
] "=r" (limit
), [has_limit
] "+rm" (has_limit
)
66 : [selector
] "r" (selector
));
67 asm ("larl %[selector], %[ar]\n\t"
69 "movl $1, %[has_ar]\n\t"
71 : [ar
] "=r" (ar
), [has_ar
] "+rm" (has_ar
)
72 : [selector
] "r" (selector
));
74 if (has_limit
|| has_ar
) {
75 printf("[FAIL]\t%s entry %hu is valid but should be invalid\n",
76 (ldt
? "LDT" : "GDT"), index
);
79 printf("[OK]\t%s entry %hu is invalid\n",
80 (ldt
? "LDT" : "GDT"), index
);
84 static void check_valid_segment(uint16_t index
, int ldt
,
85 uint32_t expected_ar
, uint32_t expected_limit
,
88 uint32_t has_limit
= 0, has_ar
= 0, limit
, ar
;
89 uint32_t selector
= (index
<< 3) | (ldt
<< 2) | 3;
91 asm ("lsl %[selector], %[limit]\n\t"
93 "movl $1, %[has_limit]\n\t"
95 : [limit
] "=r" (limit
), [has_limit
] "+rm" (has_limit
)
96 : [selector
] "r" (selector
));
97 asm ("larl %[selector], %[ar]\n\t"
99 "movl $1, %[has_ar]\n\t"
101 : [ar
] "=r" (ar
), [has_ar
] "+rm" (has_ar
)
102 : [selector
] "r" (selector
));
104 if (!has_limit
|| !has_ar
) {
105 printf("[FAIL]\t%s entry %hu is invalid but should be valid\n",
106 (ldt
? "LDT" : "GDT"), index
);
111 if (ar
!= expected_ar
) {
112 printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n",
113 (ldt
? "LDT" : "GDT"), index
, ar
, expected_ar
);
115 } else if (limit
!= expected_limit
) {
116 printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n",
117 (ldt
? "LDT" : "GDT"), index
, limit
, expected_limit
);
119 } else if (verbose
) {
120 printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n",
121 (ldt
? "LDT" : "GDT"), index
, ar
, limit
);
125 static bool install_valid_mode(const struct user_desc
*desc
, uint32_t ar
,
128 int ret
= syscall(SYS_modify_ldt
, oldmode
? 1 : 0x11,
129 desc
, sizeof(*desc
));
133 uint32_t limit
= desc
->limit
;
134 if (desc
->limit_in_pages
)
135 limit
= (limit
<< 12) + 4095;
136 check_valid_segment(desc
->entry_number
, 1, ar
, limit
, true);
138 } else if (errno
== ENOSYS
) {
139 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
142 if (desc
->seg_32bit
) {
143 printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
148 printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
154 static bool install_valid(const struct user_desc
*desc
, uint32_t ar
)
156 return install_valid_mode(desc
, ar
, false);
159 static void install_invalid(const struct user_desc
*desc
, bool oldmode
)
161 int ret
= syscall(SYS_modify_ldt
, oldmode
? 1 : 0x11,
162 desc
, sizeof(*desc
));
166 check_invalid_segment(desc
->entry_number
, 1);
167 } else if (errno
== ENOSYS
) {
168 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
170 if (desc
->seg_32bit
) {
171 printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
175 printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
180 static int safe_modify_ldt(int func
, struct user_desc
*ptr
,
181 unsigned long bytecount
)
183 int ret
= syscall(SYS_modify_ldt
, 0x11, ptr
, bytecount
);
189 static void fail_install(struct user_desc
*desc
)
191 if (safe_modify_ldt(0x11, desc
, sizeof(*desc
)) == 0) {
192 printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n");
194 } else if (errno
== ENOSYS
) {
195 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
197 printf("[OK]\tmodify_ldt failure %d\n", errno
);
201 static void do_simple_tests(void)
203 struct user_desc desc
= {
208 .contents
= 2, /* Code, not conforming */
211 .seg_not_present
= 0,
214 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
216 desc
.limit_in_pages
= 1;
217 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
218 AR_S
| AR_P
| AR_DB
| AR_G
);
220 check_invalid_segment(1, 1);
222 desc
.entry_number
= 2;
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
.base_addr
= 0xf0000000;
229 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
230 AR_S
| AR_P
| AR_DB
| AR_G
);
233 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
234 AR_S
| AR_P
| AR_DB
| AR_G
| AR_AVL
);
236 desc
.seg_not_present
= 1;
237 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
238 AR_S
| AR_DB
| AR_G
| AR_AVL
);
241 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
242 AR_S
| AR_G
| AR_AVL
);
246 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
|
247 AR_S
| AR_DB
| AR_G
| AR_AVL
);
249 desc
.read_exec_only
= 1;
250 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
|
251 AR_S
| AR_DB
| AR_G
| AR_AVL
);
254 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA_EXPDOWN
|
255 AR_S
| AR_DB
| AR_G
| AR_AVL
);
257 desc
.read_exec_only
= 0;
258 desc
.limit_in_pages
= 0;
259 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA_EXPDOWN
|
260 AR_S
| AR_DB
| AR_AVL
);
263 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE_CONF
|
264 AR_S
| AR_DB
| AR_AVL
);
266 desc
.read_exec_only
= 1;
267 install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE_CONF
|
268 AR_S
| AR_DB
| AR_AVL
);
270 desc
.read_exec_only
= 0;
272 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
273 AR_S
| AR_DB
| AR_AVL
);
275 desc
.read_exec_only
= 1;
279 install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE
|
280 AR_S
| AR_DB
| AR_AVL
);
284 bool entry1_okay
= install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE
|
285 AR_S
| AR_DB
| AR_AVL
);
288 printf("[RUN]\tTest fork\n");
289 pid_t child
= fork();
292 check_valid_segment(desc
.entry_number
, 1,
293 AR_DPL3
| AR_TYPE_XOCODE
|
294 AR_S
| AR_DB
| AR_AVL
, desc
.limit
,
296 check_invalid_segment(1, 1);
300 if (waitpid(child
, &status
, 0) != child
||
301 !WIFEXITED(status
)) {
302 printf("[FAIL]\tChild died\n");
304 } else if (WEXITSTATUS(status
) != 0) {
305 printf("[FAIL]\tChild failed\n");
308 printf("[OK]\tChild succeeded\n");
312 printf("[RUN]\tTest size\n");
314 for (i
= 0; i
< 8192; i
++) {
315 desc
.entry_number
= i
;
317 if (safe_modify_ldt(0x11, &desc
, sizeof(desc
)) != 0) {
318 printf("[FAIL]\tFailed to install entry %d\n", i
);
323 for (int j
= 0; j
< i
; j
++) {
324 check_valid_segment(j
, 1, AR_DPL3
| AR_TYPE_XOCODE
|
325 AR_S
| AR_DB
| AR_AVL
, j
, false);
327 printf("[DONE]\tSize test\n");
329 printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n");
332 /* Test entry_number too high. */
333 desc
.entry_number
= 8192;
336 /* Test deletion and actions mistakeable for deletion. */
337 memset(&desc
, 0, sizeof(desc
));
338 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
);
340 desc
.seg_not_present
= 1;
341 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
);
343 desc
.seg_not_present
= 0;
344 desc
.read_exec_only
= 1;
345 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
| AR_P
);
347 desc
.read_exec_only
= 0;
348 desc
.seg_not_present
= 1;
349 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
);
351 desc
.read_exec_only
= 1;
353 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
);
357 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
);
360 install_invalid(&desc
, false);
362 desc
.seg_not_present
= 0;
363 desc
.read_exec_only
= 0;
365 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
| AR_DB
);
366 install_invalid(&desc
, true);
372 * 2: thread should clear LDT entry 0
373 * 3: thread should exit
375 static volatile unsigned int ftx
;
377 static void *threadproc(void *ctx
)
382 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
383 err(1, "sched_setaffinity to CPU 1"); /* should never fail */
386 syscall(SYS_futex
, &ftx
, FUTEX_WAIT
, 0, NULL
, NULL
, 0);
392 /* clear LDT entry 0 */
393 const struct user_desc desc
= {};
394 if (syscall(SYS_modify_ldt
, 1, &desc
, sizeof(desc
)) != 0)
395 err(1, "modify_ldt");
397 /* If ftx == 2, set it to zero. If ftx == 100, quit. */
399 asm volatile ("lock xaddl %[x], %[ftx]" :
400 [x
] "+r" (x
), [ftx
] "+m" (ftx
));
406 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
410 memset(&sa
, 0, sizeof(sa
));
411 sa
.sa_sigaction
= handler
;
412 sa
.sa_flags
= SA_SIGINFO
| flags
;
413 sigemptyset(&sa
.sa_mask
);
414 if (sigaction(sig
, &sa
, 0))
419 static jmp_buf jmpbuf
;
421 static void sigsegv(int sig
, siginfo_t
*info
, void *ctx_void
)
423 siglongjmp(jmpbuf
, 1);
426 static void do_multicpu_tests(void)
430 int failures
= 0, iters
= 5, i
;
431 unsigned short orig_ss
;
435 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
436 printf("[SKIP]\tCannot set affinity to CPU 1\n");
442 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
443 printf("[SKIP]\tCannot set affinity to CPU 0\n");
447 sethandler(SIGSEGV
, sigsegv
, 0);
449 /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
450 sethandler(SIGILL
, sigsegv
, 0);
453 printf("[RUN]\tCross-CPU LDT invalidation\n");
455 if (pthread_create(&thread
, 0, threadproc
, 0) != 0)
456 err(1, "pthread_create");
458 asm volatile ("mov %%ss, %0" : "=rm" (orig_ss
));
460 for (i
= 0; i
< 5; i
++) {
461 if (sigsetjmp(jmpbuf
, 1) != 0)
464 /* Make sure the thread is ready after the last test. */
468 struct user_desc desc
= {
473 .contents
= 0, /* Data */
476 .seg_not_present
= 0,
480 if (safe_modify_ldt(0x11, &desc
, sizeof(desc
)) != 0) {
482 err(1, "modify_ldt");
483 printf("[SKIP]\tmodify_ldt unavailable\n");
487 /* Arm the thread. */
489 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
491 asm volatile ("mov %0, %%ss" : : "r" (0x7));
500 * On success, modify_ldt will segfault us synchronously,
501 * and we'll escape via siglongjmp.
505 asm volatile ("mov %0, %%ss" : : "rm" (orig_ss
));
508 ftx
= 100; /* Kill the thread. */
509 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
511 if (pthread_join(thread
, NULL
) != 0)
512 err(1, "pthread_join");
515 printf("[FAIL]\t%d of %d iterations failed\n", failures
, iters
);
518 printf("[OK]\tAll %d iterations succeeded\n", iters
);
522 static int finish_exec_test(void)
525 * In a sensible world, this would be check_invalid_segment(0, 1);
526 * For better or for worse, though, the LDT is inherited across exec.
527 * We can probably change this safely, but for now we test it.
529 check_valid_segment(0, 1,
530 AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
,
533 return nerrs
? 1 : 0;
536 static void do_exec_test(void)
538 printf("[RUN]\tTest exec\n");
540 struct user_desc desc
= {
545 .contents
= 2, /* Code, not conforming */
548 .seg_not_present
= 0,
551 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
553 pid_t child
= fork();
555 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL
);
556 printf("[FAIL]\tCould not exec self\n");
557 exit(1); /* exec failed */
560 if (waitpid(child
, &status
, 0) != child
||
561 !WIFEXITED(status
)) {
562 printf("[FAIL]\tChild died\n");
564 } else if (WEXITSTATUS(status
) != 0) {
565 printf("[FAIL]\tChild failed\n");
568 printf("[OK]\tChild succeeded\n");
573 static void setup_counter_page(void)
575 unsigned int *page
= mmap(NULL
, 4096, PROT_READ
| PROT_WRITE
,
576 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
577 if (page
== MAP_FAILED
)
580 for (int i
= 0; i
< 1024; i
++)
585 static int invoke_set_thread_area(void)
588 asm volatile ("int $0x80"
589 : "=a" (ret
), "+m" (low_user_desc
) :
590 "a" (243), "b" (low_user_desc
)
595 static void setup_low_user_desc(void)
597 low_user_desc
= mmap(NULL
, 2 * sizeof(struct user_desc
),
598 PROT_READ
| PROT_WRITE
,
599 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
600 if (low_user_desc
== MAP_FAILED
)
603 low_user_desc
->entry_number
= -1;
604 low_user_desc
->base_addr
= (unsigned long)&counter_page
[1];
605 low_user_desc
->limit
= 0xfffff;
606 low_user_desc
->seg_32bit
= 1;
607 low_user_desc
->contents
= 0; /* Data, grow-up*/
608 low_user_desc
->read_exec_only
= 0;
609 low_user_desc
->limit_in_pages
= 1;
610 low_user_desc
->seg_not_present
= 0;
611 low_user_desc
->useable
= 0;
613 if (invoke_set_thread_area() == 0) {
614 gdt_entry_num
= low_user_desc
->entry_number
;
615 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num
);
617 printf("[NOTE]\tset_thread_area is unavailable\n");
620 low_user_desc_clear
= low_user_desc
+ 1;
621 low_user_desc_clear
->entry_number
= gdt_entry_num
;
622 low_user_desc_clear
->read_exec_only
= 1;
623 low_user_desc_clear
->seg_not_present
= 1;
626 static void test_gdt_invalidation(void)
629 return; /* 64-bit only system -- we can't use set_thread_area */
631 unsigned short prev_sel
;
636 unsigned long saved_base
;
637 unsigned long new_base
;
641 invoke_set_thread_area();
643 sel
= (gdt_entry_num
<< 3) | 3;
644 asm volatile ("movw %%ds, %[prev_sel]\n\t"
645 "movw %[sel], %%ds\n\t"
649 "movl %[arg1], %%ebx\n\t"
650 "int $0x80\n\t" /* Should invalidate ds */
654 "movw %%ds, %[sel]\n\t"
655 "movw %[prev_sel], %%ds"
656 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
658 : "m" (low_user_desc_clear
),
659 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
668 printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
672 invoke_set_thread_area();
674 sel
= (gdt_entry_num
<< 3) | 3;
675 asm volatile ("movw %%es, %[prev_sel]\n\t"
676 "movw %[sel], %%es\n\t"
680 "movl %[arg1], %%ebx\n\t"
681 "int $0x80\n\t" /* Should invalidate es */
685 "movw %%es, %[sel]\n\t"
686 "movw %[prev_sel], %%es"
687 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
689 : "m" (low_user_desc_clear
),
690 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
699 printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
703 invoke_set_thread_area();
705 sel
= (gdt_entry_num
<< 3) | 3;
707 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &saved_base
);
709 asm volatile ("movw %%fs, %[prev_sel]\n\t"
710 "movw %[sel], %%fs\n\t"
714 "movl %[arg1], %%ebx\n\t"
715 "int $0x80\n\t" /* Should invalidate fs */
719 "movw %%fs, %[sel]\n\t"
720 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
722 : "m" (low_user_desc_clear
),
723 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
727 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &new_base
);
730 /* Restore FS/BASE for glibc */
731 asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel
] "rm" (prev_sel
));
734 syscall(SYS_arch_prctl
, ARCH_SET_FS
, saved_base
);
743 printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
747 if (sel
== 0 && new_base
!= 0) {
749 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base
);
751 printf("[OK]\tNew FSBASE was zero\n");
756 invoke_set_thread_area();
758 sel
= (gdt_entry_num
<< 3) | 3;
760 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &saved_base
);
762 asm volatile ("movw %%gs, %[prev_sel]\n\t"
763 "movw %[sel], %%gs\n\t"
767 "movl %[arg1], %%ebx\n\t"
768 "int $0x80\n\t" /* Should invalidate gs */
772 "movw %%gs, %[sel]\n\t"
773 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
775 : "m" (low_user_desc_clear
),
776 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
780 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &new_base
);
783 /* Restore GS/BASE for glibc */
784 asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel
] "rm" (prev_sel
));
787 syscall(SYS_arch_prctl
, ARCH_SET_GS
, saved_base
);
796 printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
800 if (sel
== 0 && new_base
!= 0) {
802 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base
);
804 printf("[OK]\tNew GSBASE was zero\n");
809 int main(int argc
, char **argv
)
811 if (argc
== 1 && !strcmp(argv
[0], "ldt_gdt_test_exec"))
812 return finish_exec_test();
814 setup_counter_page();
815 setup_low_user_desc();
823 test_gdt_invalidation();
825 return nerrs
? 1 : 0;