1
2
3
4
5
6
7#define _GNU_SOURCE
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <sys/signal.h>
13#include <sys/ucontext.h>
14#include <err.h>
15#include <setjmp.h>
16#include <errno.h>
17
18#include "helpers.h"
19
20
21static unsigned char altstack_data[SIGSTKSZ];
22
23static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
24 int flags)
25{
26 struct sigaction sa;
27 memset(&sa, 0, sizeof(sa));
28 sa.sa_sigaction = handler;
29 sa.sa_flags = SA_SIGINFO | flags;
30 sigemptyset(&sa.sa_mask);
31 if (sigaction(sig, &sa, 0))
32 err(1, "sigaction");
33}
34
35static volatile sig_atomic_t sig_traps;
36static sigjmp_buf jmpbuf;
37
38static volatile sig_atomic_t n_errs;
39
40#ifdef __x86_64__
41#define REG_AX REG_RAX
42#define REG_IP REG_RIP
43#else
44#define REG_AX REG_EAX
45#define REG_IP REG_EIP
46#endif
47
48static void sigsegv_or_sigbus(int sig, siginfo_t *info, void *ctx_void)
49{
50 ucontext_t *ctx = (ucontext_t*)ctx_void;
51 long ax = (long)ctx->uc_mcontext.gregs[REG_AX];
52
53 if (ax != -EFAULT && ax != -ENOSYS) {
54 printf("[FAIL]\tAX had the wrong value: 0x%lx\n",
55 (unsigned long)ax);
56 n_errs++;
57 } else {
58 printf("[OK]\tSeems okay\n");
59 }
60
61 siglongjmp(jmpbuf, 1);
62}
63
64static volatile sig_atomic_t sigtrap_consecutive_syscalls;
65
66static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
67{
68
69
70
71
72
73
74 ucontext_t *ctx = (ucontext_t*)ctx_void;
75 unsigned short *ip = (unsigned short *)ctx->uc_mcontext.gregs[REG_IP];
76
77 if (*ip == 0x340f || *ip == 0x050f) {
78
79 sigtrap_consecutive_syscalls++;
80 if (sigtrap_consecutive_syscalls > 3) {
81 printf("[WARN]\tGot stuck single-stepping -- you probably have a KVM bug\n");
82 siglongjmp(jmpbuf, 1);
83 }
84 } else {
85 sigtrap_consecutive_syscalls = 0;
86 }
87}
88
89static void sigill(int sig, siginfo_t *info, void *ctx_void)
90{
91 ucontext_t *ctx = (ucontext_t*)ctx_void;
92 unsigned short *ip = (unsigned short *)ctx->uc_mcontext.gregs[REG_IP];
93
94 if (*ip == 0x0b0f) {
95
96 printf("[OK]\tSYSCALL returned normally\n");
97 } else {
98 printf("[SKIP]\tIllegal instruction\n");
99 }
100 siglongjmp(jmpbuf, 1);
101}
102
103int main()
104{
105 stack_t stack = {
106 .ss_sp = altstack_data,
107 .ss_size = SIGSTKSZ,
108 };
109 if (sigaltstack(&stack, NULL) != 0)
110 err(1, "sigaltstack");
111
112 sethandler(SIGSEGV, sigsegv_or_sigbus, SA_ONSTACK);
113
114
115
116
117
118 sethandler(SIGBUS, sigsegv_or_sigbus, SA_ONSTACK);
119 sethandler(SIGILL, sigill, SA_ONSTACK);
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 printf("[RUN]\tSYSENTER with invalid state\n");
141 if (sigsetjmp(jmpbuf, 1) == 0) {
142 asm volatile (
143 "movl $-1, %%eax\n\t"
144 "movl $-1, %%ebx\n\t"
145 "movl $-1, %%ecx\n\t"
146 "movl $-1, %%edx\n\t"
147 "movl $-1, %%esi\n\t"
148 "movl $-1, %%edi\n\t"
149 "movl $-1, %%ebp\n\t"
150 "movl $-1, %%esp\n\t"
151 "sysenter"
152 : : : "memory", "flags");
153 }
154
155 printf("[RUN]\tSYSCALL with invalid state\n");
156 if (sigsetjmp(jmpbuf, 1) == 0) {
157 asm volatile (
158 "movl $-1, %%eax\n\t"
159 "movl $-1, %%ebx\n\t"
160 "movl $-1, %%ecx\n\t"
161 "movl $-1, %%edx\n\t"
162 "movl $-1, %%esi\n\t"
163 "movl $-1, %%edi\n\t"
164 "movl $-1, %%ebp\n\t"
165 "movl $-1, %%esp\n\t"
166 "syscall\n\t"
167 "ud2"
168 : : : "memory", "flags");
169 }
170
171 printf("[RUN]\tSYSENTER with TF and invalid state\n");
172 sethandler(SIGTRAP, sigtrap, SA_ONSTACK);
173
174 if (sigsetjmp(jmpbuf, 1) == 0) {
175 sigtrap_consecutive_syscalls = 0;
176 set_eflags(get_eflags() | X86_EFLAGS_TF);
177 asm volatile (
178 "movl $-1, %%eax\n\t"
179 "movl $-1, %%ebx\n\t"
180 "movl $-1, %%ecx\n\t"
181 "movl $-1, %%edx\n\t"
182 "movl $-1, %%esi\n\t"
183 "movl $-1, %%edi\n\t"
184 "movl $-1, %%ebp\n\t"
185 "movl $-1, %%esp\n\t"
186 "sysenter"
187 : : : "memory", "flags");
188 }
189 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
190
191 printf("[RUN]\tSYSCALL with TF and invalid state\n");
192 if (sigsetjmp(jmpbuf, 1) == 0) {
193 sigtrap_consecutive_syscalls = 0;
194 set_eflags(get_eflags() | X86_EFLAGS_TF);
195 asm volatile (
196 "movl $-1, %%eax\n\t"
197 "movl $-1, %%ebx\n\t"
198 "movl $-1, %%ecx\n\t"
199 "movl $-1, %%edx\n\t"
200 "movl $-1, %%esi\n\t"
201 "movl $-1, %%edi\n\t"
202 "movl $-1, %%ebp\n\t"
203 "movl $-1, %%esp\n\t"
204 "syscall\n\t"
205 "ud2"
206 : : : "memory", "flags");
207 }
208 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
209
210 return 0;
211}
212