Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1604648pxu; Sun, 6 Dec 2020 01:11:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJwnP5vxRCzui4VbGfxyAsxsPkC5WO5Ua7G4l2TK3p0zbjUqMCr/vwCk6F4RxDTcCvXmONLB X-Received: by 2002:a17:906:c51:: with SMTP id t17mr14846056ejf.523.1607245912745; Sun, 06 Dec 2020 01:11:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607245912; cv=none; d=google.com; s=arc-20160816; b=VN6vZlcSBStifXmKthpyvHNcib9qDVSmor3XUef+GVWxaz04u8OaeYnSolQMkgGGnO GZDdPOKYghy32bZQ2k3lDKwtvvtazHsuM2zOuQDqI+TR8078SV7PWL9W4YSxq7pRA2PS haRr/8Kk1DRHwK4bMxC5DSgGhfrAMp0XaANMq+iA/M4MkmbIoUh157qJoYYNpDIqt4BT i8TLav2+IhARC6ytRVIg7TfqIWsfy0/Uqsyu4EerXCfEWs5sXczIinmyVowntd2JjuWQ etgXM+jgGtdkLtXdIcl08n8anj9Src/WrG7+LNfypsZES2p7wQ9AY1XwR658Qb3X/nct ng2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=eY7RW9wRlLumLKbgtN+8B1XUsllLVvHZSgoZDgKOR9Q=; b=zHKGhYVUW38OIILFes+5lnytvrGZ4yYoyjG7u8MexVlHrTCfhqAgys4MZyNViRgEsK UZG60ACRQ7oSIk2EFS+FeN1K9rrPa7kNlc1wn+eiLrshGaC7DfC+xBEWmItc/WHyVTQJ CGMpLWvQPojWVSPwI5xev2li+m4FESmULp7dWWZaLxyviU+FcK1jGmhcm32rvJGAcChb cpRSi4gtRRZmfxJ6vjjrC4ntkGjj9r5eo5V8PZoMAeoMw6/h9qsk35Rb+uI9T7EuG0Ph f0mlhp018w0WQ7QSYGYBDBMlvZHX4Ha6lf2V2I3eF/7dmf+yAQlRZ9XdkOOjdGWmh/E5 cTTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="Wy01ux/B"; dkim=neutral (no key) header.i=@linutronix.de header.b=2WcGV9cd; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i6si97283edl.178.2020.12.06.01.11.29; Sun, 06 Dec 2020 01:11:52 -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; dkim=pass header.i=@linutronix.de header.s=2020 header.b="Wy01ux/B"; dkim=neutral (no key) header.i=@linutronix.de header.b=2WcGV9cd; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727151AbgLFJKP (ORCPT + 99 others); Sun, 6 Dec 2020 04:10:15 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:57308 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726209AbgLFJKN (ORCPT ); Sun, 6 Dec 2020 04:10:13 -0500 Date: Sun, 06 Dec 2020 09:09:30 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1607245771; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eY7RW9wRlLumLKbgtN+8B1XUsllLVvHZSgoZDgKOR9Q=; b=Wy01ux/BJ0ZbZiUePS8O7VgFkfKVsqr5qbunWjRinZdgzGq5om3nUvg4SjrjJj8sr8WbWB iInlaCw9CexqQoefZ73fSk7jixpnFDony0ZrOcz2mpKOytV8u9d9ygt3D5ZllCzRiwrzMn qJWLlHP6ZGhf3Mu8gtljJTLR+Ajs77V1oDJBfgVZpqxVDSxiAjODsUyMZthxVDVickrx5P Cw55QKssavGKhoW2+FLtQfubpAs8tcfh4sVNwLx68oN9hzJH3wcn8/WmWT8wN8KrRCnYYi /gh7EKy4NFHPe9Zk/DqvF+qlYRKGZ376gTowNohqbhbbk1pEWvDPqrGSWGDl+g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1607245771; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eY7RW9wRlLumLKbgtN+8B1XUsllLVvHZSgoZDgKOR9Q=; b=2WcGV9cdads9H/dgce/ucbXISjlcR4m51XpZkMn0EEt8OvQCHEl0R6ZJDJBxrBqJIh9CTD Xt1MlM6QWq3A5OBg== From: "tip-bot2 for Masami Hiramatsu" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes Cc: syzbot+9b64b619f10f19d19a7c@syzkaller.appspotmail.com, Masami Hiramatsu , Borislav Petkov , Srikar Dronamraju , stable@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <160697103739.3146288.7437620795200799020.stgit@devnote2> References: <160697103739.3146288.7437620795200799020.stgit@devnote2> MIME-Version: 1.0 Message-ID: <160724577086.3364.16453446433031160252.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/urgent branch of tip: Commit-ID: 4e9a5ae8df5b3365183150f6df49e49dece80d8c Gitweb: https://git.kernel.org/tip/4e9a5ae8df5b3365183150f6df49e49dece80d8c Author: Masami Hiramatsu AuthorDate: Thu, 03 Dec 2020 13:50:37 +09:00 Committer: Borislav Petkov CommitterDate: Sun, 06 Dec 2020 09:58:13 +01:00 x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be insn.prefixes.bytes[i] != 0 and i < 4 instead of using insn.prefixes.nbytes. Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook . [ bp: Massage commit message, sync with the respective header in tools/ and drop "we". ] Fixes: 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints") Reported-by: syzbot+9b64b619f10f19d19a7c@syzkaller.appspotmail.com Signed-off-by: Masami Hiramatsu Signed-off-by: Borislav Petkov Reviewed-by: Srikar Dronamraju Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/160697103739.3146288.7437620795200799020.stgit@devnote2 --- arch/x86/include/asm/insn.h | 15 +++++++++++++++ arch/x86/kernel/uprobes.c | 10 ++++++---- tools/arch/x86/include/asm/insn.h | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index 5c1ae3e..a8c3d28 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, it cannot be used for looping over the prefixes. + */ +#define for_each_insn_prefix(insn, idx, prefix) \ + for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (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 3fdaa04..138bdb1 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; } diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm/insn.h index 568854b..52c6262 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/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, it cannot be used for looping over the prefixes. + */ +#define for_each_insn_prefix(insn, idx, prefix) \ + for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++) + #define POP_SS_OPCODE 0x1f #define MOV_SREG_OPCODE 0x8e