]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/testing/selftests/x86/ldt_gdt.c
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;
369 desc
.read_exec_only
= 0;
371 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
| AR_DB
);
372 install_invalid(&desc
, true);
378 * 2: thread should clear LDT entry 0
379 * 3: thread should exit
381 static volatile unsigned int ftx
;
383 static void *threadproc(void *ctx
)
388 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
389 err(1, "sched_setaffinity to CPU 1"); /* should never fail */
392 syscall(SYS_futex
, &ftx
, FUTEX_WAIT
, 0, NULL
, NULL
, 0);
398 /* clear LDT entry 0 */
399 const struct user_desc desc
= {};
400 if (syscall(SYS_modify_ldt
, 1, &desc
, sizeof(desc
)) != 0)
401 err(1, "modify_ldt");
403 /* If ftx == 2, set it to zero. If ftx == 100, quit. */
405 asm volatile ("lock xaddl %[x], %[ftx]" :
406 [x
] "+r" (x
), [ftx
] "+m" (ftx
));
412 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
416 memset(&sa
, 0, sizeof(sa
));
417 sa
.sa_sigaction
= handler
;
418 sa
.sa_flags
= SA_SIGINFO
| flags
;
419 sigemptyset(&sa
.sa_mask
);
420 if (sigaction(sig
, &sa
, 0))
425 static jmp_buf jmpbuf
;
427 static void sigsegv(int sig
, siginfo_t
*info
, void *ctx_void
)
429 siglongjmp(jmpbuf
, 1);
432 static void do_multicpu_tests(void)
436 int failures
= 0, iters
= 5, i
;
437 unsigned short orig_ss
;
441 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
442 printf("[SKIP]\tCannot set affinity to CPU 1\n");
448 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
449 printf("[SKIP]\tCannot set affinity to CPU 0\n");
453 sethandler(SIGSEGV
, sigsegv
, 0);
455 /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
456 sethandler(SIGILL
, sigsegv
, 0);
459 printf("[RUN]\tCross-CPU LDT invalidation\n");
461 if (pthread_create(&thread
, 0, threadproc
, 0) != 0)
462 err(1, "pthread_create");
464 asm volatile ("mov %%ss, %0" : "=rm" (orig_ss
));
466 for (i
= 0; i
< 5; i
++) {
467 if (sigsetjmp(jmpbuf
, 1) != 0)
470 /* Make sure the thread is ready after the last test. */
474 struct user_desc desc
= {
479 .contents
= 0, /* Data */
482 .seg_not_present
= 0,
486 if (safe_modify_ldt(0x11, &desc
, sizeof(desc
)) != 0) {
488 err(1, "modify_ldt");
489 printf("[SKIP]\tmodify_ldt unavailable\n");
493 /* Arm the thread. */
495 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
497 asm volatile ("mov %0, %%ss" : : "r" (0x7));
506 * On success, modify_ldt will segfault us synchronously,
507 * and we'll escape via siglongjmp.
511 asm volatile ("mov %0, %%ss" : : "rm" (orig_ss
));
514 ftx
= 100; /* Kill the thread. */
515 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
517 if (pthread_join(thread
, NULL
) != 0)
518 err(1, "pthread_join");
521 printf("[FAIL]\t%d of %d iterations failed\n", failures
, iters
);
524 printf("[OK]\tAll %d iterations succeeded\n", iters
);
528 static int finish_exec_test(void)
531 * In a sensible world, this would be check_invalid_segment(0, 1);
532 * For better or for worse, though, the LDT is inherited across exec.
533 * We can probably change this safely, but for now we test it.
535 check_valid_segment(0, 1,
536 AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
,
539 return nerrs
? 1 : 0;
542 static void do_exec_test(void)
544 printf("[RUN]\tTest exec\n");
546 struct user_desc desc
= {
551 .contents
= 2, /* Code, not conforming */
554 .seg_not_present
= 0,
557 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
559 pid_t child
= fork();
561 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL
);
562 printf("[FAIL]\tCould not exec self\n");
563 exit(1); /* exec failed */
566 if (waitpid(child
, &status
, 0) != child
||
567 !WIFEXITED(status
)) {
568 printf("[FAIL]\tChild died\n");
570 } else if (WEXITSTATUS(status
) != 0) {
571 printf("[FAIL]\tChild failed\n");
574 printf("[OK]\tChild succeeded\n");
579 static void setup_counter_page(void)
581 unsigned int *page
= mmap(NULL
, 4096, PROT_READ
| PROT_WRITE
,
582 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
583 if (page
== MAP_FAILED
)
586 for (int i
= 0; i
< 1024; i
++)
591 static int invoke_set_thread_area(void)
594 asm volatile ("int $0x80"
595 : "=a" (ret
), "+m" (low_user_desc
) :
596 "a" (243), "b" (low_user_desc
)
601 static void setup_low_user_desc(void)
603 low_user_desc
= mmap(NULL
, 2 * sizeof(struct user_desc
),
604 PROT_READ
| PROT_WRITE
,
605 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
606 if (low_user_desc
== MAP_FAILED
)
609 low_user_desc
->entry_number
= -1;
610 low_user_desc
->base_addr
= (unsigned long)&counter_page
[1];
611 low_user_desc
->limit
= 0xfffff;
612 low_user_desc
->seg_32bit
= 1;
613 low_user_desc
->contents
= 0; /* Data, grow-up*/
614 low_user_desc
->read_exec_only
= 0;
615 low_user_desc
->limit_in_pages
= 1;
616 low_user_desc
->seg_not_present
= 0;
617 low_user_desc
->useable
= 0;
619 if (invoke_set_thread_area() == 0) {
620 gdt_entry_num
= low_user_desc
->entry_number
;
621 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num
);
623 printf("[NOTE]\tset_thread_area is unavailable\n");
626 low_user_desc_clear
= low_user_desc
+ 1;
627 low_user_desc_clear
->entry_number
= gdt_entry_num
;
628 low_user_desc_clear
->read_exec_only
= 1;
629 low_user_desc_clear
->seg_not_present
= 1;
632 static void test_gdt_invalidation(void)
635 return; /* 64-bit only system -- we can't use set_thread_area */
637 unsigned short prev_sel
;
642 unsigned long saved_base
;
643 unsigned long new_base
;
647 invoke_set_thread_area();
649 sel
= (gdt_entry_num
<< 3) | 3;
650 asm volatile ("movw %%ds, %[prev_sel]\n\t"
651 "movw %[sel], %%ds\n\t"
655 "movl %[arg1], %%ebx\n\t"
656 "int $0x80\n\t" /* Should invalidate ds */
660 "movw %%ds, %[sel]\n\t"
661 "movw %[prev_sel], %%ds"
662 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
664 : "m" (low_user_desc_clear
),
665 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
674 printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
678 invoke_set_thread_area();
680 sel
= (gdt_entry_num
<< 3) | 3;
681 asm volatile ("movw %%es, %[prev_sel]\n\t"
682 "movw %[sel], %%es\n\t"
686 "movl %[arg1], %%ebx\n\t"
687 "int $0x80\n\t" /* Should invalidate es */
691 "movw %%es, %[sel]\n\t"
692 "movw %[prev_sel], %%es"
693 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
695 : "m" (low_user_desc_clear
),
696 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
705 printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
709 invoke_set_thread_area();
711 sel
= (gdt_entry_num
<< 3) | 3;
713 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &saved_base
);
715 asm volatile ("movw %%fs, %[prev_sel]\n\t"
716 "movw %[sel], %%fs\n\t"
720 "movl %[arg1], %%ebx\n\t"
721 "int $0x80\n\t" /* Should invalidate fs */
725 "movw %%fs, %[sel]\n\t"
726 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
728 : "m" (low_user_desc_clear
),
729 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
733 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &new_base
);
736 /* Restore FS/BASE for glibc */
737 asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel
] "rm" (prev_sel
));
740 syscall(SYS_arch_prctl
, ARCH_SET_FS
, saved_base
);
749 printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
753 if (sel
== 0 && new_base
!= 0) {
755 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base
);
757 printf("[OK]\tNew FSBASE was zero\n");
762 invoke_set_thread_area();
764 sel
= (gdt_entry_num
<< 3) | 3;
766 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &saved_base
);
768 asm volatile ("movw %%gs, %[prev_sel]\n\t"
769 "movw %[sel], %%gs\n\t"
773 "movl %[arg1], %%ebx\n\t"
774 "int $0x80\n\t" /* Should invalidate gs */
778 "movw %%gs, %[sel]\n\t"
779 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
781 : "m" (low_user_desc_clear
),
782 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
786 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &new_base
);
789 /* Restore GS/BASE for glibc */
790 asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel
] "rm" (prev_sel
));
793 syscall(SYS_arch_prctl
, ARCH_SET_GS
, saved_base
);
802 printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
806 if (sel
== 0 && new_base
!= 0) {
808 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base
);
810 printf("[OK]\tNew GSBASE was zero\n");
815 int main(int argc
, char **argv
)
817 if (argc
== 1 && !strcmp(argv
[0], "ldt_gdt_test_exec"))
818 return finish_exec_test();
820 setup_counter_page();
821 setup_low_user_desc();
829 test_gdt_invalidation();
831 return nerrs
? 1 : 0;