Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp225732pxu; Wed, 2 Dec 2020 20:54:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJwhIngB+ZzIRwMezQ+x9dPiAknlrqUVwS+2Qf/7gMMkMtbePfUb6c6D7paREtfsggn9Lg9c X-Received: by 2002:a50:b941:: with SMTP id m59mr1245543ede.371.1606971261515; Wed, 02 Dec 2020 20:54:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606971261; cv=none; d=google.com; s=arc-20160816; b=vBADVKwK7mUwEP0mjOiUDBNZdxPC/A2uqHFgTaTa9DGhWih6vovvgosUfwEnJ25JPd qqb/KpehD75N3FHPO1QPXtqW601Eo/cZRhnewt0DztUgirFnLuItAWKePim7gNuJGsS1 oMupwPVP8sFStliX8Xu0wct0rdv2piFkj+LETFBJ2WP7N7RRqS+IhmpBTcQdeKjaxuY1 Le3diTJOn5/oi93FiO+D4Y5kGfc19pvTFJFcbYx5R2Lph3xoDUvwEnvqbPbZzjVg++1e 66t4sevrpzlrqiSYIQYR3FVl4QY/yhFOjHq8guDG++TrHkfDGWvPQy1cKN0h51At8f6r jYPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=10YlnXA91/kwbILYXQ7bGBsT8C48/m0QNRLrUUvfBHc=; b=cvMWbWAPAjyQveJzJrV68n41n0s7psXL7jsM7Pb+TV64TzyE+o7ldxhRs7tAou4ulL XpocmWRQct72ERagDGQLt+QsFCzwU9FjzH5vxSQynWQxwH4swEs5qKMjYip8ciIIJrr9 1Ag+YFIMa7BVATN5ERopQtCQ6RXtKfjHwdMX1MnnIDj9DHgi0iOeMjjPsy8jNH46YkTf tynm5MpsVBExkTdfZmN1nn+F7GyKSTjF5zLshFScBsw/5Np2+WXcPWZNU/I7544fj27X YdZRord7WYEEe5WqliTQl3T9N9+lIXhtu0gLhaYW8Pk13PCk/jl6IZn62LMvnGvFLopd 6sKw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce25si329497edb.301.2020.12.02.20.53.59; Wed, 02 Dec 2020 20:54:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727386AbgLCEvZ (ORCPT + 99 others); Wed, 2 Dec 2020 23:51:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:47888 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725933AbgLCEvZ (ORCPT ); Wed, 2 Dec 2020 23:51:25 -0500 From: Masami Hiramatsu Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: x86@kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov Cc: Kees Cook , Masami Hiramatsu , "H . Peter Anvin" , Joerg Roedel , Tom Lendacky , "Gustavo A . R . Silva" , Jann Horn , Srikar Dronamraju , Ricardo Neri , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes Date: Thu, 3 Dec 2020 13:50:37 +0900 Message-Id: <160697103739.3146288.7437620795200799020.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <160697102582.3146288.10127018634865687932.stgit@devnote2> References: <160697102582.3146288.10127018634865687932.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Since the insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a same prefix is repeated, we have to check whether the insn.prefixes.bytes[i] != 0 and i < 4 instead of insn.prefixes.nbytes. This introduces for_each_insn_prefix() macro for this purpose. Fixes: 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints") Reported-by: syzbot+9b64b619f10f19d19a7c@syzkaller.appspotmail.com Debugged-by: Kees Cook Reviewed-by: Srikar Dronamraju Signed-off-by: Masami Hiramatsu Cc: stable@vger.kernel.org --- Changes in v2: - Add for_each_insn_prefix() macro and fix to check index first. --- arch/x86/include/asm/insn.h | 15 +++++++++++++++ arch/x86/kernel/uprobes.c | 10 ++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index 5c1ae3eff9d4..e6b38ebd3a1c 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn) return insn_offset_displacement(insn) + insn->displacement.nbytes; } +/** + * for_each_insn_prefix() -- Iterate prefixes in the instruction + * @insn: Pointer to struct insn. + * @idx: Index storage. + * @prefix: Prefix byte. + * + * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix + * and the index is stored in @idx (note that this @idx is just for a cursor, + * do not change it.) + * Since prefixes.nbytes can be bigger than 4 if some prefixes are repeated, + * we can not use it for looping over the prefixes. + */ +#define for_each_insn_prefix(insn, idx, prefix) \ + for (idx = 0; idx < 4 && (prefix = insn->prefixes.bytes[idx]) != 0; idx++) + #define POP_SS_OPCODE 0x1f #define MOV_SREG_OPCODE 0x8e diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c index 3fdaa042823d..138bdb1fd136 100644 --- a/arch/x86/kernel/uprobes.c +++ b/arch/x86/kernel/uprobes.c @@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = { static bool is_prefix_bad(struct insn *insn) { + insn_byte_t p; int i; - for (i = 0; i < insn->prefixes.nbytes; i++) { + for_each_insn_prefix(insn, i, p) { insn_attr_t attr; - attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]); + attr = inat_get_opcode_attribute(p); switch (attr) { case INAT_MAKE_PREFIX(INAT_PFX_ES): case INAT_MAKE_PREFIX(INAT_PFX_CS): @@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = { static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn) { u8 opc1 = OPCODE1(insn); + insn_byte_t p; int i; switch (opc1) { @@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn) * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix. * No one uses these insns, reject any branch insns with such prefix. */ - for (i = 0; i < insn->prefixes.nbytes; i++) { - if (insn->prefixes.bytes[i] == 0x66) + for_each_insn_prefix(insn, i, p) { + if (p == 0x66) return -ENOTSUPP; }