Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp459386pxu; Thu, 3 Dec 2020 04:52:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJx8RRQQMJXynyEovUbZNAodTQMiwInyuYVnMzjyGTgy0XmHf9tgT31oKicbgl/sdkeybmsA X-Received: by 2002:a17:906:c289:: with SMTP id r9mr2261143ejz.311.1606999946234; Thu, 03 Dec 2020 04:52:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606999946; cv=none; d=google.com; s=arc-20160816; b=Q94YtTwUljlLK5l8HvOssXe+xIQLVYzre6xUjZ1pIZRhi3Xfw2QIVmqVlwbjDmSMv3 ZzTz7Tt+V5N8sVhchsksY9fcGGgzli7ZSvuVhgqPqAZs5+o39hcG4Cv7lLHLqmclGmDk e2z6aB0ER3tJAoERjcJKG5avr920IAbyBebZSdFTfun5yaxzljoP2mUU/eROnoPFs+7s 9gIDgDdiK9GSypfC+iQs7ybgKkWL7JM3rdRynDyJI45gqRb6X9aoo5NTW6OTFUDqkHGq qKNeie+UlseW8706nqCJpKPSLlJfrr3rWDTi5RsjFJhdiR4XK42S3TN17aw9c8R7OHE6 MaqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=wgap4O9lM1H6N1i1P+35A4XMd/puQPXDxz/23uMUBd0=; b=mIYkjNfVuPUX6JpksFhZ6M0Laz3KLKDlajhfV0x60DZxzijLRtILvWtHWiWdi1dBRU ZphIYiAls1XuM3dun47qYHj7I9oHiqZgjk9KgBTIikdTiBxYJ2yun5giiQMWMkAFpGNj Vf42xJoOFrZI7mc83wqJZfOOariP+hd3g/PuzlQ/PuEZhjoHiRSf8tADTq8oI5VcLkTr pwwygv0eIzToEVvuPR6o67VpDHhlyvelhuXU/7wb/Gzo2EhPgtEjT1wsm7iOPc57Gc4V OLxyhhagoaM6N8YD1++Bx1kOEtaHOthHSEfnC6z6jg9PjMvkh9qawIz69IOId6tL5R0l 1OkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=TmKRZ9yT; 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=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 11si887649edv.53.2020.12.03.04.52.01; Thu, 03 Dec 2020 04:52:26 -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=@alien8.de header.s=dkim header.b=TmKRZ9yT; 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=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730446AbgLCMtB (ORCPT + 99 others); Thu, 3 Dec 2020 07:49:01 -0500 Received: from mail.skyhub.de ([5.9.137.197]:46440 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730437AbgLCMtB (ORCPT ); Thu, 3 Dec 2020 07:49:01 -0500 Received: from zn.tnic (p200300ec2f0dc500b89b6474ddf31320.dip0.t-ipconnect.de [IPv6:2003:ec:2f0d:c500:b89b:6474:ddf3:1320]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 4521B1EC011B; Thu, 3 Dec 2020 13:48:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606999699; h=from:from: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:in-reply-to:in-reply-to: references:references; bh=wgap4O9lM1H6N1i1P+35A4XMd/puQPXDxz/23uMUBd0=; b=TmKRZ9yT4j6B0OaUILGIOto6Fi19jSqfS8shROVHw4svnbZ4X5EeY/NfFghECUCUwFTDTl 6HK940bah32MfDDRy31YdLOMQlqFwUqoDISwNy2sQtJi5aROw2ORZd+SMfHhIoz0n2A6Rw hGhEP7xbcazI/yawmYPdL8H75GFbjJE= Date: Thu, 3 Dec 2020 13:48:20 +0100 From: Borislav Petkov To: Masami Hiramatsu Cc: x86@kernel.org, Thomas Gleixner , Ingo Molnar , Kees Cook , "H . Peter Anvin" , Joerg Roedel , Tom Lendacky , "Gustavo A . R . Silva" , Jann Horn , Srikar Dronamraju , Ricardo Neri , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes Message-ID: <20201203124820.GJ3059@zn.tnic> References: <160697102582.3146288.10127018634865687932.stgit@devnote2> <160697103739.3146288.7437620795200799020.stgit@devnote2> <20201203123757.GH3059@zn.tnic> <20201203124121.GI3059@zn.tnic> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20201203124121.GI3059@zn.tnic> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org So it ended up like this: --- From 5014e4e902778d63ce392f864b3654baa4b72384 Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Thu, 3 Dec 2020 13:50:37 +0900 Subject: [PATCH] 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, add NUM_LEGACY_PREFIXES, 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 | 16 ++++++++++++++++ arch/x86/kernel/uprobes.c | 10 ++++++---- tools/arch/x86/include/asm/insn.h | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index 5c1ae3eff9d4..fe8e862004d3 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -58,6 +58,7 @@ struct insn { }; #define MAX_INSN_SIZE 15 +#define NUM_LEGACY_PREFIXES 4 #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6) #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3) @@ -201,6 +202,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 NUM_LEGACY_PREFIXES 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 < NUM_LEGACY_PREFIXES && (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; } diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm/insn.h index 568854b14d0a..fe8e862004d3 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/arch/x86/include/asm/insn.h @@ -8,7 +8,7 @@ */ /* insn_attr_t is defined in inat.h */ -#include "inat.h" +#include struct insn_field { union { @@ -58,6 +58,7 @@ struct insn { }; #define MAX_INSN_SIZE 15 +#define NUM_LEGACY_PREFIXES 4 #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6) #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3) @@ -201,6 +202,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 NUM_LEGACY_PREFIXES 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 < NUM_LEGACY_PREFIXES && (prefix = insn->prefixes.bytes[idx]) != 0; idx++) + #define POP_SS_OPCODE 0x1f #define MOV_SREG_OPCODE 0x8e -- 2.21.0 -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette