x86/kprobes: Fix optprobe to detect INT3 padding correctly

Commit

7705dc855797 ("x86/vmlinux: Use INT3 instead of NOP for linker fill bytes")

changed the padding bytes between functions from NOP to INT3. However,
when optprobe decodes a target function it finds INT3 and gives up the
jump optimization.

Instead of giving up any INT3 detection, check whether the rest of the
bytes to the end of the function are INT3. If all of them are INT3,
those come from the linker. In that case, continue the optprobe jump
optimization.

[ bp: Massage commit message. ]

Fixes: 7705dc855797 ("x86/vmlinux: Use INT3 instead of NOP for linker fill bytes")
Reported-by: Adam Zabrocki <pi3@pi3.com.pl>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/160767025681.3880685.16021570341428835411.stgit@devnote2

authored by Masami Hiramatsu and committed by Borislav Petkov 0d07c0ec 190113b4

Changed files
+20 -2
arch
x86
kernel
kprobes
+20 -2
arch/x86/kernel/kprobes/opt.c
··· 272 272 return ret; 273 273 } 274 274 275 + static bool is_padding_int3(unsigned long addr, unsigned long eaddr) 276 + { 277 + unsigned char ops; 278 + 279 + for (; addr < eaddr; addr++) { 280 + if (get_kernel_nofault(ops, (void *)addr) < 0 || 281 + ops != INT3_INSN_OPCODE) 282 + return false; 283 + } 284 + 285 + return true; 286 + } 287 + 275 288 /* Decode whole function to ensure any instructions don't jump into target */ 276 289 static int can_optimize(unsigned long paddr) 277 290 { ··· 323 310 return 0; 324 311 kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE); 325 312 insn_get_length(&insn); 326 - /* Another subsystem puts a breakpoint */ 313 + /* 314 + * In the case of detecting unknown breakpoint, this could be 315 + * a padding INT3 between functions. Let's check that all the 316 + * rest of the bytes are also INT3. 317 + */ 327 318 if (insn.opcode.bytes[0] == INT3_INSN_OPCODE) 328 - return 0; 319 + return is_padding_int3(addr, paddr - offset + size) ? 1 : 0; 320 + 329 321 /* Recover address */ 330 322 insn.kaddr = (void *)addr; 331 323 insn.next_byte = (void *)(addr + insn.length);