1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020 Cyril Hrubis <chrbis@suse.cz>
4 *
5 * After fix for CVE-2018-1000199 (see ptrace08.c) subsequent calls to POKEUSER
6 * for x86 debug registers were ignored silently.
7 *
8 * This is a regression test for commit:
9 *
10 * commit bd14406b78e6daa1ea3c1673bda1ffc9efdeead0
11 * Author: Jiri Olsa <jolsa@kernel.org>
12 * Date: Mon Aug 27 11:12:25 2018 +0200
13 *
14 * perf/hw_breakpoint: Modify breakpoint even if the new attr has disabled set
15 */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <stddef.h>
20 #include <sys/ptrace.h>
21 #include <sys/user.h>
22 #include <signal.h>
23 #include "tst_test.h"
24
25 static pid_t child_pid;
26
child_main(void)27 static void child_main(void)
28 {
29 raise(SIGSTOP);
30 exit(0);
31 }
32
run(void)33 static void run(void)
34 {
35 int status;
36 unsigned long addr;
37
38 child_pid = SAFE_FORK();
39
40 if (!child_pid)
41 child_main();
42
43 if (SAFE_WAITPID(child_pid, &status, WUNTRACED) != child_pid)
44 tst_brk(TBROK, "Received event from unexpected PID");
45
46 #if defined(__i386__) || defined(__x86_64__)
47 SAFE_PTRACE(PTRACE_ATTACH, child_pid, NULL, NULL);
48 SAFE_PTRACE(PTRACE_POKEUSER, child_pid,
49 (void *)offsetof(struct user, u_debugreg[0]), (void *)1);
50 SAFE_PTRACE(PTRACE_POKEUSER, child_pid,
51 (void *)offsetof(struct user, u_debugreg[0]), (void *)2);
52
53 addr = ptrace(PTRACE_PEEKUSER, child_pid,
54 (void*)offsetof(struct user, u_debugreg[0]), NULL);
55 #endif
56
57 if (addr == 2)
58 tst_res(TPASS, "The rd0 was set on second PTRACE_POKEUSR");
59 else
60 tst_res(TFAIL, "The rd0 wasn't set on second PTRACE_POKEUSER");
61
62 SAFE_PTRACE(PTRACE_DETACH, child_pid, NULL, NULL);
63 SAFE_KILL(child_pid, SIGCONT);
64 child_pid = 0;
65 tst_reap_children();
66 }
67
cleanup(void)68 static void cleanup(void)
69 {
70 /* Main process terminated by tst_brk() with child still paused */
71 if (child_pid)
72 SAFE_KILL(child_pid, SIGKILL);
73 }
74
75 static struct tst_test test = {
76 .test_all = run,
77 .cleanup = cleanup,
78 .forks_child = 1,
79 .supported_archs = (const char *const []) {
80 "x86",
81 "x86_64",
82 NULL
83 },
84 .tags = (const struct tst_tag[]) {
85 {"linux-git", "bd14406b78e6"},
86 {}
87 }
88 };
89