Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp814726imu; Tue, 11 Dec 2018 08:01:09 -0800 (PST) X-Google-Smtp-Source: AFSGD/VMcc00eDQ6OUW2KXmvDhB2JB+jCLhPQcKEI05ZP3/PTEJwgNlKGUN6fIVg/Z2IJBoZMX0P X-Received: by 2002:a62:fc52:: with SMTP id e79mr17015726pfh.8.1544544069345; Tue, 11 Dec 2018 08:01:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544544069; cv=none; d=google.com; s=arc-20160816; b=xuU92bmJoJdFfetF45ofDqlH46glOPHMYuSSQ9yviPMg9FhikGEonaRHd3Hw8GZdIX timRPl/ndBBTib5Gee/yto9yJRF2/TqqmsYsEFQK80DzYlhav9O6Bfpg5gIiwHIP/kTZ quPm1Ubd32OzY01UD8W4sNjTMC6buuWAC23nk0EmbrazjVgzp9lixG+Hotz+4bk7wlMw 6yZxJuBH5peOWT9LzGjhfTDzivqxbPKnsNDIABjnorqVKE8jtYbp5EfB6iMeU7yM4KUt kv+cTWmx/x48XQEH5W0/jyTVdldacLDb4z5BsF05o2TuTdlMXhMdIK28C3tbD2gDQlsG 4KNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Wp3BB1sATcoivmFaVWYJt3hwz75sUVvFBglqu97iy5g=; b=oqwg8QQYXemZ3EnGpTCJGJLN2ZpCWU2czooiWWcM75AshbtsoG5XL7oUDUtssqO107 ZQKh8x7utREt/TunP1oWmsduB3EUxt6AxnDpO33Fpj4ao4E9SAq17yQ21JjnoaLUjy0L ryY39mXT2XpSjTcGMTLHrOnWkmggB0z41fDUfTYmIxNyZc2c/rjWfPJJ/N08bnsvR38G 63bXtG8dbFzqcNKALKIHofUYWFsM6QxfKynbfdEyyLjUIhoCKvKCyKuD9o9qABjFX9XG ZjLiE7ESI6kf8xnI1tKSYNQiKOBFXT0lsDHCkB1in7d5A3xDjdtbAu7qxcqDpMWkIbS6 xCvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pRLW82Wj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3si12193755pgl.108.2018.12.11.08.00.54; Tue, 11 Dec 2018 08:01:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pRLW82Wj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730983AbeLKP6O (ORCPT + 99 others); Tue, 11 Dec 2018 10:58:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:47386 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726759AbeLKP6N (ORCPT ); Tue, 11 Dec 2018 10:58:13 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7805A205C9; Tue, 11 Dec 2018 15:58:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544543892; bh=TLZNYALW5mviJQ/Cf/6y9bW21uJxXnfDmtk05Hhtg0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pRLW82WjueGUs3AVaG1iGXJOUVL4RDY+Z/JkE1YjDNIvc13PD7YIgM8+FysIa2aTF FgfEH6xCiwdmElXGrqOFDQWMd1P5+5N4XcSFqpEw2aYM5rbNwBTO/p+xKbQj1aA7eN HJMO4L5NtXnwVvI4yPrkfCn3U+djd4fQIoh5F4a0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Michael Rodin , Masami Hiramatsu , "Steven Rostedt (VMware)" , Arnaldo Carvalho de Melo , Linus Torvalds , Peter Zijlstra , Ravi Bangoria , Thomas Gleixner , Ingo Molnar Subject: [PATCH 4.19 103/118] kprobes/x86: Fix instruction patching corruption when copying more than one RIP-relative instruction Date: Tue, 11 Dec 2018 16:42:02 +0100 Message-Id: <20181211151648.426536452@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181211151644.216668863@linuxfoundation.org> References: <20181211151644.216668863@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Masami Hiramatsu commit 43a1b0cb4cd6dbfd3cd9c10da663368394d299d8 upstream. After copy_optimized_instructions() copies several instructions to the working buffer it tries to fix up the real RIP address, but it adjusts the RIP-relative instruction with an incorrect RIP address for the 2nd and subsequent instructions due to a bug in the logic. This will break the kernel pretty badly (with likely outcomes such as a kernel freeze, a crash, or worse) because probed instructions can refer to the wrong data. For example putting kprobes on cpumask_next() typically hits this bug. cpumask_next() is normally like below if CONFIG_CPUMASK_OFFSTACK=y (in this case nr_cpumask_bits is an alias of nr_cpu_ids): : 48 89 f0 mov %rsi,%rax 8b 35 7b fb e2 00 mov 0xe2fb7b(%rip),%esi # ffffffff82db9e64 55 push %rbp ... If we put a kprobe on it and it gets jump-optimized, it gets patched by the kprobes code like this: : e9 95 7d 07 1e jmpq 0xffffffffa000207a 7b fb jnp 0xffffffff81f8a2e2 e2 00 loop 0xffffffff81f8a2e9 55 push %rbp This shows that the first two MOV instructions were copied to a trampoline buffer at 0xffffffffa000207a. Here is the disassembled result of the trampoline, skipping the optprobe template instructions: # Dump of assembly code from 0xffffffffa000207a to 0xffffffffa00020ea: 54 push %rsp ... 48 83 c4 08 add $0x8,%rsp 9d popfq 48 89 f0 mov %rsi,%rax 8b 35 82 7d db e2 mov -0x1d24827e(%rip),%esi # 0xffffffff82db9e67 This dump shows that the second MOV accesses *(nr_cpu_ids+3) instead of the original *nr_cpu_ids. This leads to a kernel freeze because cpumask_next() always returns 0 and for_each_cpu() never ends. Fix this by adding 'len' correctly to the real RIP address while copying. [ mingo: Improved the changelog. ] Reported-by: Michael Rodin Signed-off-by: Masami Hiramatsu Reviewed-by: Steven Rostedt (VMware) Cc: Arnaldo Carvalho de Melo Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Ravi Bangoria Cc: Steven Rostedt Cc: Thomas Gleixner Cc: stable@vger.kernel.org # v4.15+ Fixes: 63fef14fc98a ("kprobes/x86: Make insn buffer always ROX and use text_poke()") Link: http://lkml.kernel.org/r/153504457253.22602.1314289671019919596.stgit@devbox Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/kprobes/opt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/arch/x86/kernel/kprobes/opt.c +++ b/arch/x86/kernel/kprobes/opt.c @@ -189,7 +189,7 @@ static int copy_optimized_instructions(u int len = 0, ret; while (len < RELATIVEJUMP_SIZE) { - ret = __copy_instruction(dest + len, src + len, real, &insn); + ret = __copy_instruction(dest + len, src + len, real + len, &insn); if (!ret || !can_boost(&insn, src + len)) return -EINVAL; len += ret;