]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob
bcab942fc3b50b4d0cbf35b25d5caa8c3b3d8cb7
[thirdparty/kernel/stable-queue.git] /
1 From 43a1b0cb4cd6dbfd3cd9c10da663368394d299d8 Mon Sep 17 00:00:00 2001
2 From: Masami Hiramatsu <mhiramat@kernel.org>
3 Date: Fri, 24 Aug 2018 02:16:12 +0900
4 Subject: kprobes/x86: Fix instruction patching corruption when copying more than one RIP-relative instruction
5
6 From: Masami Hiramatsu <mhiramat@kernel.org>
7
8 commit 43a1b0cb4cd6dbfd3cd9c10da663368394d299d8 upstream.
9
10 After copy_optimized_instructions() copies several instructions
11 to the working buffer it tries to fix up the real RIP address, but it
12 adjusts the RIP-relative instruction with an incorrect RIP address
13 for the 2nd and subsequent instructions due to a bug in the logic.
14
15 This will break the kernel pretty badly (with likely outcomes such as
16 a kernel freeze, a crash, or worse) because probed instructions can refer
17 to the wrong data.
18
19 For example putting kprobes on cpumask_next() typically hits this bug.
20
21 cpumask_next() is normally like below if CONFIG_CPUMASK_OFFSTACK=y
22 (in this case nr_cpumask_bits is an alias of nr_cpu_ids):
23
24 <cpumask_next>:
25 48 89 f0 mov %rsi,%rax
26 8b 35 7b fb e2 00 mov 0xe2fb7b(%rip),%esi # ffffffff82db9e64 <nr_cpu_ids>
27 55 push %rbp
28 ...
29
30 If we put a kprobe on it and it gets jump-optimized, it gets
31 patched by the kprobes code like this:
32
33 <cpumask_next>:
34 e9 95 7d 07 1e jmpq 0xffffffffa000207a
35 7b fb jnp 0xffffffff81f8a2e2 <cpumask_next+2>
36 e2 00 loop 0xffffffff81f8a2e9 <cpumask_next+9>
37 55 push %rbp
38
39 This shows that the first two MOV instructions were copied to a
40 trampoline buffer at 0xffffffffa000207a.
41
42 Here is the disassembled result of the trampoline, skipping
43 the optprobe template instructions:
44
45 # Dump of assembly code from 0xffffffffa000207a to 0xffffffffa00020ea:
46
47 54 push %rsp
48 ...
49 48 83 c4 08 add $0x8,%rsp
50 9d popfq
51 48 89 f0 mov %rsi,%rax
52 8b 35 82 7d db e2 mov -0x1d24827e(%rip),%esi # 0xffffffff82db9e67 <nr_cpu_ids+3>
53
54 This dump shows that the second MOV accesses *(nr_cpu_ids+3) instead of
55 the original *nr_cpu_ids. This leads to a kernel freeze because
56 cpumask_next() always returns 0 and for_each_cpu() never ends.
57
58 Fix this by adding 'len' correctly to the real RIP address while
59 copying.
60
61 [ mingo: Improved the changelog. ]
62
63 Reported-by: Michael Rodin <michael@rodin.online>
64 Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
65 Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
66 Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
67 Cc: Linus Torvalds <torvalds@linux-foundation.org>
68 Cc: Peter Zijlstra <peterz@infradead.org>
69 Cc: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
70 Cc: Steven Rostedt <rostedt@goodmis.org>
71 Cc: Thomas Gleixner <tglx@linutronix.de>
72 Cc: stable@vger.kernel.org # v4.15+
73 Fixes: 63fef14fc98a ("kprobes/x86: Make insn buffer always ROX and use text_poke()")
74 Link: http://lkml.kernel.org/r/153504457253.22602.1314289671019919596.stgit@devbox
75 Signed-off-by: Ingo Molnar <mingo@kernel.org>
76 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
77
78 ---
79 arch/x86/kernel/kprobes/opt.c | 2 +-
80 1 file changed, 1 insertion(+), 1 deletion(-)
81
82 --- a/arch/x86/kernel/kprobes/opt.c
83 +++ b/arch/x86/kernel/kprobes/opt.c
84 @@ -189,7 +189,7 @@ static int copy_optimized_instructions(u
85 int len = 0, ret;
86
87 while (len < RELATIVEJUMP_SIZE) {
88 - ret = __copy_instruction(dest + len, src + len, real, &insn);
89 + ret = __copy_instruction(dest + len, src + len, real + len, &insn);
90 if (!ret || !can_boost(&insn, src + len))
91 return -EINVAL;
92 len += ret;