1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Google, Inc.
4 */
5
6 /*
7 * Regression test for commit 814fb7bb7db5 ("x86/fpu: Don't let userspace set
8 * bogus xcomp_bv"), or CVE-2017-15537. This bug allowed ptrace(pid,
9 * PTRACE_SETREGSET, NT_X86_XSTATE, &iov) to assign a task an invalid FPU state
10 * --- specifically, by setting reserved bits in xstate_header.xcomp_bv. This
11 * made restoring the FPU registers fail when switching to the task, causing the
12 * FPU registers to take on the values from other tasks.
13 *
14 * To detect the bug, we have a subprocess run a loop checking its xmm0 register
15 * for corruption. This detects the case where the FPU state became invalid and
16 * the kernel is not restoring the process's registers. Note that we have to
17 * set the expected value of xmm0 to all 0's since it is acceptable behavior for
18 * the kernel to simply reinitialize the FPU state upon seeing that it is
19 * invalid. To increase the chance of detecting the problem, we also create
20 * additional subprocesses that spin with different xmm0 contents.
21 *
22 * Thus bug affected the x86 architecture only. Other architectures could have
23 * similar bugs as well, but this test has to be x86-specific because it has to
24 * know about the architecture-dependent FPU state.
25 */
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <sched.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <sys/uio.h>
33 #include <sys/wait.h>
34
35 #include "config.h"
36 #include "ptrace.h"
37 #include "tst_test.h"
38
39 #ifndef PTRACE_GETREGSET
40 # define PTRACE_GETREGSET 0x4204
41 #endif
42
43 #ifndef PTRACE_SETREGSET
44 # define PTRACE_SETREGSET 0x4205
45 #endif
46
47 #ifndef NT_X86_XSTATE
48 # define NT_X86_XSTATE 0x202
49 #endif
50
check_regs_loop(uint32_t initval)51 static void check_regs_loop(uint32_t initval)
52 {
53 const unsigned long num_iters = 1000000000;
54 uint32_t xmm0[4] = { initval, initval, initval, initval };
55 int status = 1;
56
57 #ifdef __x86_64__
58 asm volatile(" movdqu %0, %%xmm0\n"
59 " mov %0, %%rbx\n"
60 "1: dec %2\n"
61 " jz 2f\n"
62 " movdqu %%xmm0, %0\n"
63 " mov %0, %%rax\n"
64 " cmp %%rax, %%rbx\n"
65 " je 1b\n"
66 " jmp 3f\n"
67 "2: mov $0, %1\n"
68 "3:\n"
69 : "+m" (xmm0), "+r" (status)
70 : "r" (num_iters) : "rax", "rbx", "xmm0");
71 #endif
72
73 if (status) {
74 tst_res(TFAIL,
75 "xmm registers corrupted! initval=%08X, xmm0=%08X%08X%08X%08X\n",
76 initval, xmm0[0], xmm0[1], xmm0[2], xmm0[3]);
77 }
78 exit(status);
79 }
80
do_test(void)81 static void do_test(void)
82 {
83 int i;
84 int num_cpus = tst_ncpus();
85 pid_t pid;
86 uint64_t xstate[512];
87 struct iovec iov = { .iov_base = xstate, .iov_len = sizeof(xstate) };
88 int status;
89 bool okay;
90
91 pid = SAFE_FORK();
92 if (pid == 0) {
93 TST_CHECKPOINT_WAKE(0);
94 check_regs_loop(0x00000000);
95 }
96 for (i = 0; i < num_cpus; i++) {
97 if (SAFE_FORK() == 0)
98 check_regs_loop(0xDEADBEEF);
99 }
100
101 TST_CHECKPOINT_WAIT(0);
102 sched_yield();
103
104 TEST(ptrace(PTRACE_ATTACH, pid, 0, 0));
105 if (TST_RET != 0)
106 tst_brk(TBROK | TTERRNO, "PTRACE_ATTACH failed");
107
108 SAFE_WAITPID(pid, NULL, 0);
109 TEST(ptrace(PTRACE_GETREGSET, pid, NT_X86_XSTATE, &iov));
110 if (TST_RET != 0) {
111 if (TST_ERR == EIO)
112 tst_brk(TCONF, "GETREGSET/SETREGSET is unsupported");
113
114 if (TST_ERR == EINVAL)
115 tst_brk(TCONF, "NT_X86_XSTATE is unsupported");
116
117 if (TST_ERR == ENODEV)
118 tst_brk(TCONF, "CPU doesn't support XSAVE instruction");
119
120 tst_brk(TBROK | TTERRNO,
121 "PTRACE_GETREGSET failed with unexpected error");
122 }
123
124 xstate[65] = -1; /* sets all bits in xstate_header.xcomp_bv */
125
126 /*
127 * Old kernels simply masked out all the reserved bits in the xstate
128 * header (causing the PTRACE_SETREGSET command here to succeed), while
129 * new kernels will reject them (causing the PTRACE_SETREGSET command
130 * here to fail with EINVAL). We accept either behavior, as neither
131 * behavior reliably tells us whether the real bug (which we test for
132 * below in either case) is present.
133 */
134 TEST(ptrace(PTRACE_SETREGSET, pid, NT_X86_XSTATE, &iov));
135 if (TST_RET == 0) {
136 tst_res(TINFO, "PTRACE_SETREGSET with reserved bits succeeded");
137 } else if (TST_ERR == EINVAL) {
138 tst_res(TINFO,
139 "PTRACE_SETREGSET with reserved bits failed with EINVAL");
140 } else {
141 tst_brk(TBROK | TTERRNO,
142 "PTRACE_SETREGSET failed with unexpected error");
143 }
144
145 /*
146 * It is possible for test child 'pid' to crash on AMD
147 * systems (e.g. AMD Opteron(TM) Processor 6234) with
148 * older kernels. This causes tracee to stop and sleep
149 * in ptrace_stop(). Without resuming the tracee, the
150 * test hangs at do_test()->tst_reap_children() called
151 * by the library. Use detach here, so we don't need to
152 * worry about potential stops after this point.
153 */
154 TEST(ptrace(PTRACE_DETACH, pid, 0, 0));
155 if (TST_RET != 0)
156 tst_brk(TBROK | TTERRNO, "PTRACE_DETACH failed");
157
158 /* If child 'pid' crashes, only report it as info. */
159 SAFE_WAITPID(pid, &status, 0);
160 if (WIFEXITED(status)) {
161 tst_res(TINFO, "test child %d exited, retcode: %d",
162 pid, WEXITSTATUS(status));
163 }
164 if (WIFSIGNALED(status)) {
165 tst_res(TINFO, "test child %d exited, termsig: %d",
166 pid, WTERMSIG(status));
167 }
168
169 okay = true;
170 for (i = 0; i < num_cpus; i++) {
171 SAFE_WAIT(&status);
172 okay &= (WIFEXITED(status) && WEXITSTATUS(status) == 0);
173 }
174 if (okay)
175 tst_res(TPASS, "wasn't able to set invalid FPU state");
176 }
177
178 static struct tst_test test = {
179 .test_all = do_test,
180 .forks_child = 1,
181 .needs_checkpoints = 1,
182 .supported_archs = (const char *const []) {
183 "x86_64",
184 NULL
185 },
186 .tags = (const struct tst_tag[]) {
187 {"linux-git", "814fb7bb7db5"},
188 {"CVE", "2017-15537"},
189 {}
190 }
191
192 };
193