1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2018 Andrew Lutomirski
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2020 SUSE LLC <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * CVE-2018-1000199
7*49cdfc7eSAndroid Build Coastguard Worker *
8*49cdfc7eSAndroid Build Coastguard Worker * Test error handling when ptrace(POKEUSER) modified x86 debug registers even
9*49cdfc7eSAndroid Build Coastguard Worker * when the call returned error.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * When the bug was present we could create breakpoint in the kernel code,
12*49cdfc7eSAndroid Build Coastguard Worker * which shoudn't be possible at all. The original CVE caused a kernel crash by
13*49cdfc7eSAndroid Build Coastguard Worker * setting a breakpoint on do_debug kernel function which, when triggered,
14*49cdfc7eSAndroid Build Coastguard Worker * caused an infinite loop. However we do not have to crash the kernel in order
15*49cdfc7eSAndroid Build Coastguard Worker * to assert if kernel has been fixed or not.
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * On newer kernels all we have to do is to try to set a breakpoint, on any
18*49cdfc7eSAndroid Build Coastguard Worker * kernel address, then read it back and check if the value has been set or
19*49cdfc7eSAndroid Build Coastguard Worker * not.
20*49cdfc7eSAndroid Build Coastguard Worker *
21*49cdfc7eSAndroid Build Coastguard Worker * The original fix to the CVE however disabled a breakpoint on address change
22*49cdfc7eSAndroid Build Coastguard Worker * and the check was deffered to write dr7 that enabled the breakpoint again.
23*49cdfc7eSAndroid Build Coastguard Worker * So on older kernels we have to write to dr7 which should fail instead.
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * Kernel crash partially fixed in:
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * commit f67b15037a7a50c57f72e69a6d59941ad90a0f0f
28*49cdfc7eSAndroid Build Coastguard Worker * Author: Linus Torvalds <[email protected]>
29*49cdfc7eSAndroid Build Coastguard Worker * Date: Mon Mar 26 15:39:07 2018 -1000
30*49cdfc7eSAndroid Build Coastguard Worker *
31*49cdfc7eSAndroid Build Coastguard Worker * perf/hwbp: Simplify the perf-hwbp code, fix documentation
32*49cdfc7eSAndroid Build Coastguard Worker *
33*49cdfc7eSAndroid Build Coastguard Worker * On Centos7, this is also a regression test for
34*49cdfc7eSAndroid Build Coastguard Worker * commit 27747f8bc355 ("perf/x86/hw_breakpoints: Fix check for kernel-space breakpoints").
35*49cdfc7eSAndroid Build Coastguard Worker */
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <stddef.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ptrace.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/user.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
44*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_stdio.h"
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker static pid_t child_pid;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #if defined(__i386__)
49*49cdfc7eSAndroid Build Coastguard Worker # define KERN_ADDR_MIN 0xc0000000
50*49cdfc7eSAndroid Build Coastguard Worker # define KERN_ADDR_MAX 0xffffffff
51*49cdfc7eSAndroid Build Coastguard Worker # define KERN_ADDR_BITS 32
52*49cdfc7eSAndroid Build Coastguard Worker #else
53*49cdfc7eSAndroid Build Coastguard Worker # define KERN_ADDR_MIN 0xffff800000000000
54*49cdfc7eSAndroid Build Coastguard Worker # define KERN_ADDR_MAX 0xffffffffffffffff
55*49cdfc7eSAndroid Build Coastguard Worker # define KERN_ADDR_BITS 64
56*49cdfc7eSAndroid Build Coastguard Worker #endif
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker
child_main(void)59*49cdfc7eSAndroid Build Coastguard Worker static void child_main(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker raise(SIGSTOP);
62*49cdfc7eSAndroid Build Coastguard Worker exit(0);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
ptrace_try_kern_addr(unsigned long kern_addr)65*49cdfc7eSAndroid Build Coastguard Worker static void ptrace_try_kern_addr(unsigned long kern_addr)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker int status;
68*49cdfc7eSAndroid Build Coastguard Worker unsigned long addr;
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Trying address 0x%lx", kern_addr);
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker child_pid = SAFE_FORK();
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker if (!child_pid)
75*49cdfc7eSAndroid Build Coastguard Worker child_main();
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker if (SAFE_WAITPID(child_pid, &status, WUNTRACED) != child_pid)
78*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Received event from unexpected PID");
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker #if defined(__i386__) || defined(__x86_64__)
81*49cdfc7eSAndroid Build Coastguard Worker SAFE_PTRACE(PTRACE_ATTACH, child_pid, NULL, NULL);
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_PTRACE(PTRACE_POKEUSER, child_pid,
83*49cdfc7eSAndroid Build Coastguard Worker (void *)offsetof(struct user, u_debugreg[0]), (void *)1);
84*49cdfc7eSAndroid Build Coastguard Worker SAFE_PTRACE(PTRACE_POKEUSER, child_pid,
85*49cdfc7eSAndroid Build Coastguard Worker (void *)offsetof(struct user, u_debugreg[7]), (void *)1);
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker TEST(ptrace(PTRACE_POKEUSER, child_pid,
88*49cdfc7eSAndroid Build Coastguard Worker (void *)offsetof(struct user, u_debugreg[0]),
89*49cdfc7eSAndroid Build Coastguard Worker (void *)kern_addr));
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
92*49cdfc7eSAndroid Build Coastguard Worker addr = ptrace(PTRACE_PEEKUSER, child_pid,
93*49cdfc7eSAndroid Build Coastguard Worker (void *)offsetof(struct user, u_debugreg[0]), NULL);
94*49cdfc7eSAndroid Build Coastguard Worker if (addr == kern_addr) {
95*49cdfc7eSAndroid Build Coastguard Worker TEST(ptrace(PTRACE_POKEUSER, child_pid,
96*49cdfc7eSAndroid Build Coastguard Worker (void *)offsetof(struct user, u_debugreg[7]), (void *)1));
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
101*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "ptrace() breakpoint with kernel addr succeeded");
102*49cdfc7eSAndroid Build Coastguard Worker } else {
103*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EINVAL) {
104*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO,
105*49cdfc7eSAndroid Build Coastguard Worker "ptrace() breakpoint with kernel addr failed");
106*49cdfc7eSAndroid Build Coastguard Worker } else {
107*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
108*49cdfc7eSAndroid Build Coastguard Worker "ptrace() breakpoint on kernel addr should return EINVAL, got");
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker }
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker #endif
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker SAFE_PTRACE(PTRACE_DETACH, child_pid, NULL, NULL);
115*49cdfc7eSAndroid Build Coastguard Worker SAFE_KILL(child_pid, SIGCONT);
116*49cdfc7eSAndroid Build Coastguard Worker child_pid = 0;
117*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker
run(void)120*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
121*49cdfc7eSAndroid Build Coastguard Worker {
122*49cdfc7eSAndroid Build Coastguard Worker ptrace_try_kern_addr(KERN_ADDR_MIN);
123*49cdfc7eSAndroid Build Coastguard Worker ptrace_try_kern_addr(KERN_ADDR_MAX);
124*49cdfc7eSAndroid Build Coastguard Worker ptrace_try_kern_addr(KERN_ADDR_MIN + (KERN_ADDR_MAX - KERN_ADDR_MIN)/2);
125*49cdfc7eSAndroid Build Coastguard Worker }
126*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)127*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
128*49cdfc7eSAndroid Build Coastguard Worker {
129*49cdfc7eSAndroid Build Coastguard Worker /* Main process terminated by tst_brk() with child still paused */
130*49cdfc7eSAndroid Build Coastguard Worker if (child_pid)
131*49cdfc7eSAndroid Build Coastguard Worker SAFE_KILL(child_pid, SIGKILL);
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
135*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
136*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
137*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
138*49cdfc7eSAndroid Build Coastguard Worker /*
139*49cdfc7eSAndroid Build Coastguard Worker * When running in compat mode we can't pass 64 address to ptrace so we
140*49cdfc7eSAndroid Build Coastguard Worker * have to skip the test.
141*49cdfc7eSAndroid Build Coastguard Worker */
142*49cdfc7eSAndroid Build Coastguard Worker .skip_in_compat = 1,
143*49cdfc7eSAndroid Build Coastguard Worker .supported_archs = (const char *const []) {
144*49cdfc7eSAndroid Build Coastguard Worker "x86",
145*49cdfc7eSAndroid Build Coastguard Worker "x86_64",
146*49cdfc7eSAndroid Build Coastguard Worker NULL
147*49cdfc7eSAndroid Build Coastguard Worker },
148*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
149*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "f67b15037a7a"},
150*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2018-1000199"},
151*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "27747f8bc355"},
152*49cdfc7eSAndroid Build Coastguard Worker {}
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker };
155