1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * sigreturn.c - tests that x86 avoids Intel SYSRET pitfalls
4*4882a593Smuzhiyun * Copyright (c) 2014-2016 Andrew Lutomirski
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #define _GNU_SOURCE
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <stdlib.h>
10*4882a593Smuzhiyun #include <unistd.h>
11*4882a593Smuzhiyun #include <stdio.h>
12*4882a593Smuzhiyun #include <string.h>
13*4882a593Smuzhiyun #include <inttypes.h>
14*4882a593Smuzhiyun #include <sys/signal.h>
15*4882a593Smuzhiyun #include <sys/ucontext.h>
16*4882a593Smuzhiyun #include <sys/syscall.h>
17*4882a593Smuzhiyun #include <err.h>
18*4882a593Smuzhiyun #include <stddef.h>
19*4882a593Smuzhiyun #include <stdbool.h>
20*4882a593Smuzhiyun #include <setjmp.h>
21*4882a593Smuzhiyun #include <sys/user.h>
22*4882a593Smuzhiyun #include <sys/mman.h>
23*4882a593Smuzhiyun #include <assert.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun asm (
27*4882a593Smuzhiyun ".pushsection \".text\", \"ax\"\n\t"
28*4882a593Smuzhiyun ".balign 4096\n\t"
29*4882a593Smuzhiyun "test_page: .globl test_page\n\t"
30*4882a593Smuzhiyun ".fill 4094,1,0xcc\n\t"
31*4882a593Smuzhiyun "test_syscall_insn:\n\t"
32*4882a593Smuzhiyun "syscall\n\t"
33*4882a593Smuzhiyun ".ifne . - test_page - 4096\n\t"
34*4882a593Smuzhiyun ".error \"test page is not one page long\"\n\t"
35*4882a593Smuzhiyun ".endif\n\t"
36*4882a593Smuzhiyun ".popsection"
37*4882a593Smuzhiyun );
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun extern const char test_page[];
40*4882a593Smuzhiyun static void const *current_test_page_addr = test_page;
41*4882a593Smuzhiyun
sethandler(int sig,void (* handler)(int,siginfo_t *,void *),int flags)42*4882a593Smuzhiyun static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
43*4882a593Smuzhiyun int flags)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun struct sigaction sa;
46*4882a593Smuzhiyun memset(&sa, 0, sizeof(sa));
47*4882a593Smuzhiyun sa.sa_sigaction = handler;
48*4882a593Smuzhiyun sa.sa_flags = SA_SIGINFO | flags;
49*4882a593Smuzhiyun sigemptyset(&sa.sa_mask);
50*4882a593Smuzhiyun if (sigaction(sig, &sa, 0))
51*4882a593Smuzhiyun err(1, "sigaction");
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
clearhandler(int sig)54*4882a593Smuzhiyun static void clearhandler(int sig)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun struct sigaction sa;
57*4882a593Smuzhiyun memset(&sa, 0, sizeof(sa));
58*4882a593Smuzhiyun sa.sa_handler = SIG_DFL;
59*4882a593Smuzhiyun sigemptyset(&sa.sa_mask);
60*4882a593Smuzhiyun if (sigaction(sig, &sa, 0))
61*4882a593Smuzhiyun err(1, "sigaction");
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* State used by our signal handlers. */
65*4882a593Smuzhiyun static gregset_t initial_regs;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun static volatile unsigned long rip;
68*4882a593Smuzhiyun
sigsegv_for_sigreturn_test(int sig,siginfo_t * info,void * ctx_void)69*4882a593Smuzhiyun static void sigsegv_for_sigreturn_test(int sig, siginfo_t *info, void *ctx_void)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun ucontext_t *ctx = (ucontext_t*)ctx_void;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (rip != ctx->uc_mcontext.gregs[REG_RIP]) {
74*4882a593Smuzhiyun printf("[FAIL]\tRequested RIP=0x%lx but got RIP=0x%lx\n",
75*4882a593Smuzhiyun rip, (unsigned long)ctx->uc_mcontext.gregs[REG_RIP]);
76*4882a593Smuzhiyun fflush(stdout);
77*4882a593Smuzhiyun _exit(1);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun memcpy(&ctx->uc_mcontext.gregs, &initial_regs, sizeof(gregset_t));
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun printf("[OK]\tGot SIGSEGV at RIP=0x%lx\n", rip);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
sigusr1(int sig,siginfo_t * info,void * ctx_void)85*4882a593Smuzhiyun static void sigusr1(int sig, siginfo_t *info, void *ctx_void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun ucontext_t *ctx = (ucontext_t*)ctx_void;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun memcpy(&initial_regs, &ctx->uc_mcontext.gregs, sizeof(gregset_t));
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* Set IP and CX to match so that SYSRET can happen. */
92*4882a593Smuzhiyun ctx->uc_mcontext.gregs[REG_RIP] = rip;
93*4882a593Smuzhiyun ctx->uc_mcontext.gregs[REG_RCX] = rip;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* R11 and EFLAGS should already match. */
96*4882a593Smuzhiyun assert(ctx->uc_mcontext.gregs[REG_EFL] ==
97*4882a593Smuzhiyun ctx->uc_mcontext.gregs[REG_R11]);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun sethandler(SIGSEGV, sigsegv_for_sigreturn_test, SA_RESETHAND);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
test_sigreturn_to(unsigned long ip)104*4882a593Smuzhiyun static void test_sigreturn_to(unsigned long ip)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun rip = ip;
107*4882a593Smuzhiyun printf("[RUN]\tsigreturn to 0x%lx\n", ip);
108*4882a593Smuzhiyun raise(SIGUSR1);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun static jmp_buf jmpbuf;
112*4882a593Smuzhiyun
sigsegv_for_fallthrough(int sig,siginfo_t * info,void * ctx_void)113*4882a593Smuzhiyun static void sigsegv_for_fallthrough(int sig, siginfo_t *info, void *ctx_void)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun ucontext_t *ctx = (ucontext_t*)ctx_void;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun if (rip != ctx->uc_mcontext.gregs[REG_RIP]) {
118*4882a593Smuzhiyun printf("[FAIL]\tExpected SIGSEGV at 0x%lx but got RIP=0x%lx\n",
119*4882a593Smuzhiyun rip, (unsigned long)ctx->uc_mcontext.gregs[REG_RIP]);
120*4882a593Smuzhiyun fflush(stdout);
121*4882a593Smuzhiyun _exit(1);
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun siglongjmp(jmpbuf, 1);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
test_syscall_fallthrough_to(unsigned long ip)127*4882a593Smuzhiyun static void test_syscall_fallthrough_to(unsigned long ip)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun void *new_address = (void *)(ip - 4096);
130*4882a593Smuzhiyun void *ret;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun printf("[RUN]\tTrying a SYSCALL that falls through to 0x%lx\n", ip);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun ret = mremap((void *)current_test_page_addr, 4096, 4096,
135*4882a593Smuzhiyun MREMAP_MAYMOVE | MREMAP_FIXED, new_address);
136*4882a593Smuzhiyun if (ret == MAP_FAILED) {
137*4882a593Smuzhiyun if (ip <= (1UL << 47) - PAGE_SIZE) {
138*4882a593Smuzhiyun err(1, "mremap to %p", new_address);
139*4882a593Smuzhiyun } else {
140*4882a593Smuzhiyun printf("[OK]\tmremap to %p failed\n", new_address);
141*4882a593Smuzhiyun return;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (ret != new_address)
146*4882a593Smuzhiyun errx(1, "mremap malfunctioned: asked for %p but got %p\n",
147*4882a593Smuzhiyun new_address, ret);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun current_test_page_addr = new_address;
150*4882a593Smuzhiyun rip = ip;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (sigsetjmp(jmpbuf, 1) == 0) {
153*4882a593Smuzhiyun asm volatile ("call *%[syscall_insn]" :: "a" (SYS_getpid),
154*4882a593Smuzhiyun [syscall_insn] "rm" (ip - 2));
155*4882a593Smuzhiyun errx(1, "[FAIL]\tSyscall trampoline returned");
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun printf("[OK]\tWe survived\n");
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
main()161*4882a593Smuzhiyun int main()
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun * When the kernel returns from a slow-path syscall, it will
165*4882a593Smuzhiyun * detect whether SYSRET is appropriate. If it incorrectly
166*4882a593Smuzhiyun * thinks that SYSRET is appropriate when RIP is noncanonical,
167*4882a593Smuzhiyun * it'll crash on Intel CPUs.
168*4882a593Smuzhiyun */
169*4882a593Smuzhiyun sethandler(SIGUSR1, sigusr1, 0);
170*4882a593Smuzhiyun for (int i = 47; i < 64; i++)
171*4882a593Smuzhiyun test_sigreturn_to(1UL<<i);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun clearhandler(SIGUSR1);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun sethandler(SIGSEGV, sigsegv_for_fallthrough, 0);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /* One extra test to check that we didn't screw up the mremap logic. */
178*4882a593Smuzhiyun test_syscall_fallthrough_to((1UL << 47) - 2*PAGE_SIZE);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* These are the interesting cases. */
181*4882a593Smuzhiyun for (int i = 47; i < 64; i++) {
182*4882a593Smuzhiyun test_syscall_fallthrough_to((1UL<<i) - PAGE_SIZE);
183*4882a593Smuzhiyun test_syscall_fallthrough_to(1UL<<i);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun return 0;
187*4882a593Smuzhiyun }
188