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 /* Our sigaltstack scratch space. */
21 static unsigned char altstack_data
[SIGSTKSZ
];
23 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
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))
35 static volatile sig_atomic_t sig_traps
;
36 static sigjmp_buf jmpbuf
;
38 static volatile sig_atomic_t n_errs
;
41 #define REG_AX REG_RAX
42 #define REG_IP REG_RIP
44 #define REG_AX REG_EAX
45 #define REG_IP REG_EIP
48 static void sigsegv_or_sigbus(int sig
, siginfo_t
*info
, void *ctx_void
)
50 ucontext_t
*ctx
= (ucontext_t
*)ctx_void
;
51 long ax
= (long)ctx
->uc_mcontext
.gregs
[REG_AX
];
53 if (ax
!= -EFAULT
&& ax
!= -ENOSYS
) {
54 printf("[FAIL]\tAX had the wrong value: 0x%lx\n",
56 printf("\tIP = 0x%lx\n", (unsigned long)ctx
->uc_mcontext
.gregs
[REG_IP
]);
59 printf("[OK]\tSeems okay\n");
62 siglongjmp(jmpbuf
, 1);
65 static volatile sig_atomic_t sigtrap_consecutive_syscalls
;
67 static void sigtrap(int sig
, siginfo_t
*info
, void *ctx_void
)
70 * KVM has some bugs that can cause us to stop making progress.
71 * detect them and complain, but don't infinite loop or fail the
75 ucontext_t
*ctx
= (ucontext_t
*)ctx_void
;
76 unsigned short *ip
= (unsigned short *)ctx
->uc_mcontext
.gregs
[REG_IP
];
78 if (*ip
== 0x340f || *ip
== 0x050f) {
79 /* The trap was on SYSCALL or SYSENTER */
80 sigtrap_consecutive_syscalls
++;
81 if (sigtrap_consecutive_syscalls
> 3) {
82 printf("[WARN]\tGot stuck single-stepping -- you probably have a KVM bug\n");
83 siglongjmp(jmpbuf
, 1);
86 sigtrap_consecutive_syscalls
= 0;
90 static void sigill(int sig
, siginfo_t
*info
, void *ctx_void
)
92 ucontext_t
*ctx
= (ucontext_t
*)ctx_void
;
93 unsigned short *ip
= (unsigned short *)ctx
->uc_mcontext
.gregs
[REG_IP
];
96 /* one of the ud2 instructions faulted */
97 printf("[OK]\tSYSCALL returned normally\n");
99 printf("[SKIP]\tIllegal instruction\n");
101 siglongjmp(jmpbuf
, 1);
107 .ss_sp
= altstack_data
,
110 if (sigaltstack(&stack
, NULL
) != 0)
111 err(1, "sigaltstack");
113 sethandler(SIGSEGV
, sigsegv_or_sigbus
, SA_ONSTACK
);
115 * The actual exception can vary. On Atom CPUs, we get #SS
116 * instead of #PF when the vDSO fails to access the stack when
117 * ESP is too close to 2^32, and #SS causes SIGBUS.
119 sethandler(SIGBUS
, sigsegv_or_sigbus
, SA_ONSTACK
);
120 sethandler(SIGILL
, sigill
, SA_ONSTACK
);
123 * Exercise another nasty special case. The 32-bit SYSCALL
124 * and SYSENTER instructions (even in compat mode) each
125 * clobber one register. A Linux system call has a syscall
126 * number and six arguments, and the user stack pointer
127 * needs to live in some register on return. That means
128 * that we need eight registers, but SYSCALL and SYSENTER
129 * only preserve seven registers. As a result, one argument
130 * ends up on the stack. The stack is user memory, which
131 * means that the kernel can fail to read it.
133 * The 32-bit fast system calls don't have a defined ABI:
134 * we're supposed to invoke them through the vDSO. So we'll
135 * fudge it: we set all regs to invalid pointer values and
136 * invoke the entry instruction. The return will fail no
137 * matter what, and we completely lose our program state,
138 * but we can fix it up with a signal handler.
141 printf("[RUN]\tSYSENTER with invalid state\n");
142 if (sigsetjmp(jmpbuf
, 1) == 0) {
144 "movl $-1, %%eax\n\t"
145 "movl $-1, %%ebx\n\t"
146 "movl $-1, %%ecx\n\t"
147 "movl $-1, %%edx\n\t"
148 "movl $-1, %%esi\n\t"
149 "movl $-1, %%edi\n\t"
150 "movl $-1, %%ebp\n\t"
151 "movl $-1, %%esp\n\t"
153 : : : "memory", "flags");
156 printf("[RUN]\tSYSCALL with invalid state\n");
157 if (sigsetjmp(jmpbuf
, 1) == 0) {
159 "movl $-1, %%eax\n\t"
160 "movl $-1, %%ebx\n\t"
161 "movl $-1, %%ecx\n\t"
162 "movl $-1, %%edx\n\t"
163 "movl $-1, %%esi\n\t"
164 "movl $-1, %%edi\n\t"
165 "movl $-1, %%ebp\n\t"
166 "movl $-1, %%esp\n\t"
168 "ud2" /* make sure we recover cleanly */
169 : : : "memory", "flags");
172 printf("[RUN]\tSYSENTER with TF and invalid state\n");
173 sethandler(SIGTRAP
, sigtrap
, SA_ONSTACK
);
175 if (sigsetjmp(jmpbuf
, 1) == 0) {
176 sigtrap_consecutive_syscalls
= 0;
177 set_eflags(get_eflags() | X86_EFLAGS_TF
);
179 "movl $-1, %%eax\n\t"
180 "movl $-1, %%ebx\n\t"
181 "movl $-1, %%ecx\n\t"
182 "movl $-1, %%edx\n\t"
183 "movl $-1, %%esi\n\t"
184 "movl $-1, %%edi\n\t"
185 "movl $-1, %%ebp\n\t"
186 "movl $-1, %%esp\n\t"
188 : : : "memory", "flags");
190 set_eflags(get_eflags() & ~X86_EFLAGS_TF
);
192 printf("[RUN]\tSYSCALL with TF and invalid state\n");
193 if (sigsetjmp(jmpbuf
, 1) == 0) {
194 sigtrap_consecutive_syscalls
= 0;
195 set_eflags(get_eflags() | X86_EFLAGS_TF
);
197 "movl $-1, %%eax\n\t"
198 "movl $-1, %%ebx\n\t"
199 "movl $-1, %%ecx\n\t"
200 "movl $-1, %%edx\n\t"
201 "movl $-1, %%esi\n\t"
202 "movl $-1, %%edi\n\t"
203 "movl $-1, %%ebp\n\t"
204 "movl $-1, %%esp\n\t"
206 "ud2" /* make sure we recover cleanly */
207 : : : "memory", "flags");
209 set_eflags(get_eflags() & ~X86_EFLAGS_TF
);
212 printf("[RUN]\tSYSENTER with TF, invalid state, and GSBASE < 0\n");
214 if (sigsetjmp(jmpbuf
, 1) == 0) {
215 sigtrap_consecutive_syscalls
= 0;
217 asm volatile ("wrgsbase %%rax\n\t"
218 :: "a" (0xffffffffffff0000UL
));
220 set_eflags(get_eflags() | X86_EFLAGS_TF
);
222 "movl $-1, %%eax\n\t"
223 "movl $-1, %%ebx\n\t"
224 "movl $-1, %%ecx\n\t"
225 "movl $-1, %%edx\n\t"
226 "movl $-1, %%esi\n\t"
227 "movl $-1, %%edi\n\t"
228 "movl $-1, %%ebp\n\t"
229 "movl $-1, %%esp\n\t"
231 : : : "memory", "flags");
233 set_eflags(get_eflags() & ~X86_EFLAGS_TF
);