0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #define _GNU_SOURCE
0014
0015 #include <sys/time.h>
0016 #include <time.h>
0017 #include <stdlib.h>
0018 #include <sys/syscall.h>
0019 #include <unistd.h>
0020 #include <stdio.h>
0021 #include <string.h>
0022 #include <inttypes.h>
0023 #include <sys/mman.h>
0024 #include <sys/signal.h>
0025 #include <sys/ucontext.h>
0026 #include <asm/ldt.h>
0027 #include <err.h>
0028 #include <setjmp.h>
0029 #include <stddef.h>
0030 #include <stdbool.h>
0031 #include <sys/ptrace.h>
0032 #include <sys/user.h>
0033
0034 #include "helpers.h"
0035
0036 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
0037 int flags)
0038 {
0039 struct sigaction sa;
0040 memset(&sa, 0, sizeof(sa));
0041 sa.sa_sigaction = handler;
0042 sa.sa_flags = SA_SIGINFO | flags;
0043 sigemptyset(&sa.sa_mask);
0044 if (sigaction(sig, &sa, 0))
0045 err(1, "sigaction");
0046 }
0047
0048 static void clearhandler(int sig)
0049 {
0050 struct sigaction sa;
0051 memset(&sa, 0, sizeof(sa));
0052 sa.sa_handler = SIG_DFL;
0053 sigemptyset(&sa.sa_mask);
0054 if (sigaction(sig, &sa, 0))
0055 err(1, "sigaction");
0056 }
0057
0058 static volatile sig_atomic_t sig_traps, sig_eflags;
0059 sigjmp_buf jmpbuf;
0060
0061 #ifdef __x86_64__
0062 # define REG_IP REG_RIP
0063 # define WIDTH "q"
0064 # define INT80_CLOBBERS "r8", "r9", "r10", "r11"
0065 #else
0066 # define REG_IP REG_EIP
0067 # define WIDTH "l"
0068 # define INT80_CLOBBERS
0069 #endif
0070
0071 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
0072 {
0073 ucontext_t *ctx = (ucontext_t*)ctx_void;
0074
0075 if (get_eflags() & X86_EFLAGS_TF) {
0076 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
0077 printf("[WARN]\tSIGTRAP handler had TF set\n");
0078 _exit(1);
0079 }
0080
0081 sig_traps++;
0082
0083 if (sig_traps == 10000 || sig_traps == 10001) {
0084 printf("[WARN]\tHit %d SIGTRAPs with si_addr 0x%lx, ip 0x%lx\n",
0085 (int)sig_traps,
0086 (unsigned long)info->si_addr,
0087 (unsigned long)ctx->uc_mcontext.gregs[REG_IP]);
0088 }
0089 }
0090
0091 static char const * const signames[] = {
0092 [SIGSEGV] = "SIGSEGV",
0093 [SIGBUS] = "SIBGUS",
0094 [SIGTRAP] = "SIGTRAP",
0095 [SIGILL] = "SIGILL",
0096 };
0097
0098 static void print_and_longjmp(int sig, siginfo_t *si, void *ctx_void)
0099 {
0100 ucontext_t *ctx = ctx_void;
0101
0102 printf("\tGot %s with RIP=%lx, TF=%ld\n", signames[sig],
0103 (unsigned long)ctx->uc_mcontext.gregs[REG_IP],
0104 (unsigned long)ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_TF);
0105
0106 sig_eflags = (unsigned long)ctx->uc_mcontext.gregs[REG_EFL];
0107 siglongjmp(jmpbuf, 1);
0108 }
0109
0110 static void check_result(void)
0111 {
0112 unsigned long new_eflags = get_eflags();
0113 set_eflags(new_eflags & ~X86_EFLAGS_TF);
0114
0115 if (!sig_traps) {
0116 printf("[FAIL]\tNo SIGTRAP\n");
0117 exit(1);
0118 }
0119
0120 if (!(new_eflags & X86_EFLAGS_TF)) {
0121 printf("[FAIL]\tTF was cleared\n");
0122 exit(1);
0123 }
0124
0125 printf("[OK]\tSurvived with TF set and %d traps\n", (int)sig_traps);
0126 sig_traps = 0;
0127 }
0128
0129 static void fast_syscall_no_tf(void)
0130 {
0131 sig_traps = 0;
0132 printf("[RUN]\tFast syscall with TF cleared\n");
0133 fflush(stdout);
0134 if (get_eflags() & X86_EFLAGS_TF) {
0135 printf("[FAIL]\tTF is now set\n");
0136 exit(1);
0137 }
0138 if (sig_traps) {
0139 printf("[FAIL]\tGot SIGTRAP\n");
0140 exit(1);
0141 }
0142 printf("[OK]\tNothing unexpected happened\n");
0143 }
0144
0145 int main()
0146 {
0147 #ifdef CAN_BUILD_32
0148 int tmp;
0149 #endif
0150
0151 sethandler(SIGTRAP, sigtrap, 0);
0152
0153 printf("[RUN]\tSet TF and check nop\n");
0154 set_eflags(get_eflags() | X86_EFLAGS_TF);
0155 asm volatile ("nop");
0156 check_result();
0157
0158 #ifdef __x86_64__
0159 printf("[RUN]\tSet TF and check syscall-less opportunistic sysret\n");
0160 set_eflags(get_eflags() | X86_EFLAGS_TF);
0161 extern unsigned char post_nop[];
0162 asm volatile ("pushf" WIDTH "\n\t"
0163 "pop" WIDTH " %%r11\n\t"
0164 "nop\n\t"
0165 "post_nop:"
0166 : : "c" (post_nop) : "r11");
0167 check_result();
0168 #endif
0169 #ifdef CAN_BUILD_32
0170 printf("[RUN]\tSet TF and check int80\n");
0171 set_eflags(get_eflags() | X86_EFLAGS_TF);
0172 asm volatile ("int $0x80" : "=a" (tmp) : "a" (SYS_getpid)
0173 : INT80_CLOBBERS);
0174 check_result();
0175 #endif
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 syscall(SYS_getpid);
0193 printf("[RUN]\tSet TF and check a fast syscall\n");
0194 set_eflags(get_eflags() | X86_EFLAGS_TF);
0195 syscall(SYS_getpid);
0196 check_result();
0197
0198
0199 fast_syscall_no_tf();
0200
0201
0202
0203
0204
0205
0206
0207
0208 if (sigsetjmp(jmpbuf, 1) == 0) {
0209 unsigned long nr = SYS_getpid;
0210 printf("[RUN]\tSet TF and check SYSENTER\n");
0211 stack_t stack = {
0212 .ss_sp = malloc(sizeof(char) * SIGSTKSZ),
0213 .ss_size = SIGSTKSZ,
0214 };
0215 if (sigaltstack(&stack, NULL) != 0)
0216 err(1, "sigaltstack");
0217 sethandler(SIGSEGV, print_and_longjmp,
0218 SA_RESETHAND | SA_ONSTACK);
0219 sethandler(SIGILL, print_and_longjmp, SA_RESETHAND);
0220 set_eflags(get_eflags() | X86_EFLAGS_TF);
0221 free(stack.ss_sp);
0222
0223 asm volatile ("xorl %%ebp, %%ebp; SYSENTER" : "+a" (nr) :: "flags", "rcx"
0224 #ifdef __x86_64__
0225 , "r11"
0226 #endif
0227 );
0228
0229
0230 }
0231 clearhandler(SIGSEGV);
0232 clearhandler(SIGILL);
0233 if (!(sig_eflags & X86_EFLAGS_TF)) {
0234 printf("[FAIL]\tTF was cleared\n");
0235 exit(1);
0236 }
0237
0238
0239 fast_syscall_no_tf();
0240
0241 return 0;
0242 }