1 // SPDX-License-Identifier: GPL-2.0-only
3 * syscall_arg_fault.c - tests faults 32-bit fast syscall stack args
4 * Copyright (c) 2015 Andrew Lutomirski
12 #include <sys/signal.h>
13 #include <sys/ucontext.h>
20 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
24 memset(&sa, 0, sizeof(sa));
25 sa.sa_sigaction = handler;
26 sa.sa_flags = SA_SIGINFO | flags;
27 sigemptyset(&sa.sa_mask);
28 if (sigaction(sig, &sa, 0))
32 static sigjmp_buf jmpbuf;
34 static volatile sig_atomic_t n_errs;
37 #define REG_AX REG_RAX
38 #define REG_IP REG_RIP
40 #define REG_AX REG_EAX
41 #define REG_IP REG_EIP
44 static void sigsegv_or_sigbus(int sig, siginfo_t *info, void *ctx_void)
46 ucontext_t *ctx = (ucontext_t*)ctx_void;
47 long ax = (long)ctx->uc_mcontext.gregs[REG_AX];
49 if (ax != -EFAULT && ax != -ENOSYS) {
50 printf("[FAIL]\tAX had the wrong value: 0x%lx\n",
52 printf("\tIP = 0x%lx\n", (unsigned long)ctx->uc_mcontext.gregs[REG_IP]);
55 printf("[OK]\tSeems okay\n");
58 siglongjmp(jmpbuf, 1);
61 static volatile sig_atomic_t sigtrap_consecutive_syscalls;
63 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
66 * KVM has some bugs that can cause us to stop making progress.
67 * detect them and complain, but don't infinite loop or fail the
71 ucontext_t *ctx = (ucontext_t*)ctx_void;
72 unsigned short *ip = (unsigned short *)ctx->uc_mcontext.gregs[REG_IP];
74 if (*ip == 0x340f || *ip == 0x050f) {
75 /* The trap was on SYSCALL or SYSENTER */
76 sigtrap_consecutive_syscalls++;
77 if (sigtrap_consecutive_syscalls > 3) {
78 printf("[WARN]\tGot stuck single-stepping -- you probably have a KVM bug\n");
79 siglongjmp(jmpbuf, 1);
82 sigtrap_consecutive_syscalls = 0;
86 static void sigill(int sig, siginfo_t *info, void *ctx_void)
88 ucontext_t *ctx = (ucontext_t*)ctx_void;
89 unsigned short *ip = (unsigned short *)ctx->uc_mcontext.gregs[REG_IP];
92 /* one of the ud2 instructions faulted */
93 printf("[OK]\tSYSCALL returned normally\n");
95 printf("[SKIP]\tIllegal instruction\n");
97 siglongjmp(jmpbuf, 1);
103 /* Our sigaltstack scratch space. */
104 .ss_sp = malloc(sizeof(char) * SIGSTKSZ),
107 if (sigaltstack(&stack, NULL) != 0)
108 err(1, "sigaltstack");
110 sethandler(SIGSEGV, sigsegv_or_sigbus, SA_ONSTACK);
112 * The actual exception can vary. On Atom CPUs, we get #SS
113 * instead of #PF when the vDSO fails to access the stack when
114 * ESP is too close to 2^32, and #SS causes SIGBUS.
116 sethandler(SIGBUS, sigsegv_or_sigbus, SA_ONSTACK);
117 sethandler(SIGILL, sigill, SA_ONSTACK);
120 * Exercise another nasty special case. The 32-bit SYSCALL
121 * and SYSENTER instructions (even in compat mode) each
122 * clobber one register. A Linux system call has a syscall
123 * number and six arguments, and the user stack pointer
124 * needs to live in some register on return. That means
125 * that we need eight registers, but SYSCALL and SYSENTER
126 * only preserve seven registers. As a result, one argument
127 * ends up on the stack. The stack is user memory, which
128 * means that the kernel can fail to read it.
130 * The 32-bit fast system calls don't have a defined ABI:
131 * we're supposed to invoke them through the vDSO. So we'll
132 * fudge it: we set all regs to invalid pointer values and
133 * invoke the entry instruction. The return will fail no
134 * matter what, and we completely lose our program state,
135 * but we can fix it up with a signal handler.
138 printf("[RUN]\tSYSENTER with invalid state\n");
139 if (sigsetjmp(jmpbuf, 1) == 0) {
141 "movl $-1, %%eax\n\t"
142 "movl $-1, %%ebx\n\t"
143 "movl $-1, %%ecx\n\t"
144 "movl $-1, %%edx\n\t"
145 "movl $-1, %%esi\n\t"
146 "movl $-1, %%edi\n\t"
147 "movl $-1, %%ebp\n\t"
148 "movl $-1, %%esp\n\t"
150 : : : "memory", "flags");
153 printf("[RUN]\tSYSCALL with invalid state\n");
154 if (sigsetjmp(jmpbuf, 1) == 0) {
156 "movl $-1, %%eax\n\t"
157 "movl $-1, %%ebx\n\t"
158 "movl $-1, %%ecx\n\t"
159 "movl $-1, %%edx\n\t"
160 "movl $-1, %%esi\n\t"
161 "movl $-1, %%edi\n\t"
162 "movl $-1, %%ebp\n\t"
163 "movl $-1, %%esp\n\t"
165 "ud2" /* make sure we recover cleanly */
166 : : : "memory", "flags");
169 printf("[RUN]\tSYSENTER with TF and invalid state\n");
170 sethandler(SIGTRAP, sigtrap, SA_ONSTACK);
172 if (sigsetjmp(jmpbuf, 1) == 0) {
173 sigtrap_consecutive_syscalls = 0;
174 set_eflags(get_eflags() | X86_EFLAGS_TF);
176 "movl $-1, %%eax\n\t"
177 "movl $-1, %%ebx\n\t"
178 "movl $-1, %%ecx\n\t"
179 "movl $-1, %%edx\n\t"
180 "movl $-1, %%esi\n\t"
181 "movl $-1, %%edi\n\t"
182 "movl $-1, %%ebp\n\t"
183 "movl $-1, %%esp\n\t"
185 : : : "memory", "flags");
187 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
189 printf("[RUN]\tSYSCALL with TF and invalid state\n");
190 if (sigsetjmp(jmpbuf, 1) == 0) {
191 sigtrap_consecutive_syscalls = 0;
192 set_eflags(get_eflags() | X86_EFLAGS_TF);
194 "movl $-1, %%eax\n\t"
195 "movl $-1, %%ebx\n\t"
196 "movl $-1, %%ecx\n\t"
197 "movl $-1, %%edx\n\t"
198 "movl $-1, %%esi\n\t"
199 "movl $-1, %%edi\n\t"
200 "movl $-1, %%ebp\n\t"
201 "movl $-1, %%esp\n\t"
203 "ud2" /* make sure we recover cleanly */
204 : : : "memory", "flags");
206 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
209 printf("[RUN]\tSYSENTER with TF, invalid state, and GSBASE < 0\n");
211 if (sigsetjmp(jmpbuf, 1) == 0) {
212 sigtrap_consecutive_syscalls = 0;
214 asm volatile ("wrgsbase %%rax\n\t"
215 :: "a" (0xffffffffffff0000UL));
217 set_eflags(get_eflags() | X86_EFLAGS_TF);
219 "movl $-1, %%eax\n\t"
220 "movl $-1, %%ebx\n\t"
221 "movl $-1, %%ecx\n\t"
222 "movl $-1, %%edx\n\t"
223 "movl $-1, %%esi\n\t"
224 "movl $-1, %%edi\n\t"
225 "movl $-1, %%ebp\n\t"
226 "movl $-1, %%esp\n\t"
228 : : : "memory", "flags");
230 set_eflags(get_eflags() & ~X86_EFLAGS_TF);