From nobody Fri Apr 19 23:32:14 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1567597629; cv=none; d=zoho.com; s=zohoarc; b=HPM3sD2lC4WCp7+QtT0pJ6+gmkR7N46D7hG3kDLOt0py8fIo6bBtmq6a1SlV52zyOABswxgY9C22ltV2+NBx4hONP3rCbZdLahvjtbYReeCW5tlVhVTjOSdEXxlJhfPx+3LJikdMZLLaxh+Dp2oWg9O8qa9baHlpuBd2ei5eq4E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567597629; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=F45vxyLiPioUdAipXxT10QMc7FJYFioFjHfU1FmwApQ=; b=odte232Cv886unGitkgHcBl6Z2UGpvltjRP5EwS8p4Sgqhw93MllpS+iPMkLoA7sIWu1RTDbd+c23cu/NY5B8o/bzk5F8n/a0p5ws6HVGh8fbtabV/jTFoPULmoW7V+mCYU1aut27xkhdvC/53tY7Wjes+ioVU2FJgZENvzu7k0= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1567597629709498.02113478369836; Wed, 4 Sep 2019 04:47:09 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i5Tji-00080k-SQ; Wed, 04 Sep 2019 11:46:06 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i5Tjh-00080W-AK for xen-devel@lists.xenproject.org; Wed, 04 Sep 2019 11:46:05 +0000 Received: from mail.kernel.org (unknown [198.145.29.99]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 93321200-cf09-11e9-b76c-bc764e2007e4; Wed, 04 Sep 2019 11:46:04 +0000 (UTC) Received: from localhost.localdomain (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 86AAF20820; Wed, 4 Sep 2019 11:46:01 +0000 (UTC) X-Inumbo-ID: 93321200-cf09-11e9-b76c-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567597564; bh=i9BrEoVqV9CTOcIwZGpJ4tmN6Njby5luY7L+lyftqxs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fBxx4VXIJQ8pDwWp2+Udn2j49bsUCyS4O02V6SjiW4Yk/KDhiXpn2E/mOrswU5FJr MGUjnHZ9B8SX/AE9kwl829Wwx3MvGlCXfcK/lO53StjvVgULFkFM/+yBzCjfDM+hyQ de4+kszuD2OFpbiVqDtSKrMwlHGjsD5kcfeUZBag= From: Masami Hiramatsu To: Ingo Molnar Date: Wed, 4 Sep 2019 20:45:59 +0900 Message-Id: <156759755900.24473.5182879905681416105.stgit@devnote2> X-Mailer: git-send-email 2.20.1 In-Reply-To: <156759754770.24473.11832897710080799131.stgit@devnote2> References: <156759754770.24473.11832897710080799131.stgit@devnote2> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Subject: [Xen-devel] [PATCH -tip 1/2] x86: xen: insn: Decode XEN_EMULATE_PREFIX correctly X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Stefano Stabellini , Peter Zijlstra , Randy Dunlap , x86@kernel.org, linux-kernel@vger.kernel.org, Borislav Petkov , Josh Poimboeuf , xen-devel@lists.xenproject.org, Boris Ostrovsky Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Decode XEN_EMULATE_PREFIX prefix by x86 insn decoder. This treats a special sequence of instructions of XEN_EMULATE_PREFIX as a prefix bytes in x86 insn decoder. User can test whether the instruction has the XEN_EMULATE_PREFIX or not by insn_is_xen_prefixed(). Note that this prefix is treated as just a dummy prefix, so no affect for decoding opcode and operand. Reported-by: Josh Poimboeuf Signed-off-by: Masami Hiramatsu Acked-by: Peter Zijlstra (Intel) --- arch/x86/include/asm/insn.h | 2 + arch/x86/include/asm/xen/interface.h | 7 ++++- arch/x86/include/asm/xen/prefix.h | 10 +++++++ arch/x86/lib/insn.c | 43 +++++++++++++++++++++++++++= ++++ tools/arch/x86/include/asm/insn.h | 2 + tools/arch/x86/include/asm/xen/prefix.h | 10 +++++++ tools/arch/x86/lib/insn.c | 43 +++++++++++++++++++++++++++= ++++ tools/objtool/sync-check.sh | 3 +- 8 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 arch/x86/include/asm/xen/prefix.h create mode 100644 tools/arch/x86/include/asm/xen/prefix.h diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index 154f27be8bfc..ac42efd4295a 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -128,6 +128,8 @@ static inline int insn_is_evex(struct insn *insn) return (insn->vex_prefix.nbytes =3D=3D 4); } =20 +extern int insn_has_xen_prefix(struct insn *insn); + /* Ensure this instruction is decoded completely */ static inline int insn_complete(struct insn *insn) { diff --git a/arch/x86/include/asm/xen/interface.h b/arch/x86/include/asm/xe= n/interface.h index 62ca03ef5c65..fe33a9798708 100644 --- a/arch/x86/include/asm/xen/interface.h +++ b/arch/x86/include/asm/xen/interface.h @@ -379,12 +379,15 @@ struct xen_pmu_arch { * Prefix forces emulation of some non-trapping instructions. * Currently only CPUID. */ +#include + #ifdef __ASSEMBLY__ -#define XEN_EMULATE_PREFIX .byte 0x0f,0x0b,0x78,0x65,0x6e ; +#define XEN_EMULATE_PREFIX .byte __XEN_EMULATE_PREFIX ; #define XEN_CPUID XEN_EMULATE_PREFIX cpuid #else -#define XEN_EMULATE_PREFIX ".byte 0x0f,0x0b,0x78,0x65,0x6e ; " +#define XEN_EMULATE_PREFIX ".byte " __XEN_EMULATE_PREFIX_STR " ; " #define XEN_CPUID XEN_EMULATE_PREFIX "cpuid" + #endif =20 #endif /* _ASM_X86_XEN_INTERFACE_H */ diff --git a/arch/x86/include/asm/xen/prefix.h b/arch/x86/include/asm/xen/p= refix.h new file mode 100644 index 000000000000..f901be0d7a95 --- /dev/null +++ b/arch/x86/include/asm/xen/prefix.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_ASM_X86_XEN_PREFIX_H +#define _TOOLS_ASM_X86_XEN_PREFIX_H + +#include + +#define __XEN_EMULATE_PREFIX 0x0f,0x0b,0x78,0x65,0x6e +#define __XEN_EMULATE_PREFIX_STR __stringify(__XEN_EMULATE_PREFIX) + +#endif diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c index 0b5862ba6a75..310a609ee382 100644 --- a/arch/x86/lib/insn.c +++ b/arch/x86/lib/insn.c @@ -13,6 +13,9 @@ #include #include =20 +/* For special Xen prefix */ +#include + /* Verify next sizeof(t) bytes can be on the same instruction */ #define validate_next(t, insn, n) \ ((insn)->next_byte + sizeof(t) + n <=3D (insn)->end_kaddr) @@ -58,6 +61,42 @@ void insn_init(struct insn *insn, const void *kaddr, int= buf_len, int x86_64) insn->addr_bytes =3D 4; } =20 +static const insn_byte_t xen_prefix[] =3D { __XEN_EMULATE_PREFIX }; + +static int insn_xen_prefix(struct insn *insn, insn_byte_t b) +{ + struct insn_field *prefixes =3D &insn->prefixes; + size_t i; + + for (i =3D 0; i < sizeof(xen_prefix); i++) { + b =3D peek_nbyte_next(insn_byte_t, insn, i); + if (b !=3D xen_prefix[i]) + goto err_out; + } + + memcpy(prefixes->bytes, xen_prefix, 3); + prefixes->bytes[3] =3D xen_prefix[sizeof(xen_prefix) - 1]; + prefixes->nbytes =3D sizeof(xen_prefix); + insn->next_byte +=3D prefixes->nbytes; + prefixes->got =3D 1; + + return 1; + +err_out: + return 0; +} + +int insn_has_xen_prefix(struct insn *insn) +{ + if (unlikely(insn->prefixes.nbytes =3D=3D sizeof(xen_prefix))) { + return !memcmp(insn->prefixes.bytes, xen_prefix, 3) && + insn->prefixes.bytes[3] =3D=3D + xen_prefix[sizeof(xen_prefix) - 1]; + } + + return 0; +} + /** * insn_get_prefixes - scan x86 instruction prefix bytes * @insn: &struct insn containing instruction @@ -79,6 +118,10 @@ void insn_get_prefixes(struct insn *insn) nb =3D 0; lb =3D 0; b =3D peek_next(insn_byte_t, insn); + + if (insn_xen_prefix(insn, b)) + return; + attr =3D inat_get_opcode_attribute(b); while (inat_is_legacy_prefix(attr)) { /* Skip if same prefix */ diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm= /insn.h index 37a4c390750b..1c723fb5c6ee 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/arch/x86/include/asm/insn.h @@ -128,6 +128,8 @@ static inline int insn_is_evex(struct insn *insn) return (insn->vex_prefix.nbytes =3D=3D 4); } =20 +extern int insn_has_xen_prefix(struct insn *insn); + /* Ensure this instruction is decoded completely */ static inline int insn_complete(struct insn *insn) { diff --git a/tools/arch/x86/include/asm/xen/prefix.h b/tools/arch/x86/inclu= de/asm/xen/prefix.h new file mode 100644 index 000000000000..f901be0d7a95 --- /dev/null +++ b/tools/arch/x86/include/asm/xen/prefix.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_ASM_X86_XEN_PREFIX_H +#define _TOOLS_ASM_X86_XEN_PREFIX_H + +#include + +#define __XEN_EMULATE_PREFIX 0x0f,0x0b,0x78,0x65,0x6e +#define __XEN_EMULATE_PREFIX_STR __stringify(__XEN_EMULATE_PREFIX) + +#endif diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c index 79e048f1d902..b34cd1cefb1c 100644 --- a/tools/arch/x86/lib/insn.c +++ b/tools/arch/x86/lib/insn.c @@ -13,6 +13,9 @@ #include "../include/asm/inat.h" #include "../include/asm/insn.h" =20 +/* For special Xen prefix */ +#include "../include/asm/xen/prefix.h" + /* Verify next sizeof(t) bytes can be on the same instruction */ #define validate_next(t, insn, n) \ ((insn)->next_byte + sizeof(t) + n <=3D (insn)->end_kaddr) @@ -58,6 +61,42 @@ void insn_init(struct insn *insn, const void *kaddr, int= buf_len, int x86_64) insn->addr_bytes =3D 4; } =20 +static const insn_byte_t xen_prefix[] =3D { __XEN_EMULATE_PREFIX }; + +static int insn_xen_prefix(struct insn *insn, insn_byte_t b) +{ + struct insn_field *prefixes =3D &insn->prefixes; + size_t i; + + for (i =3D 0; i < sizeof(xen_prefix); i++) { + b =3D peek_nbyte_next(insn_byte_t, insn, i); + if (b !=3D xen_prefix[i]) + goto err_out; + } + + memcpy(prefixes->bytes, xen_prefix, 3); + prefixes->bytes[3] =3D xen_prefix[sizeof(xen_prefix) - 1]; + prefixes->nbytes =3D sizeof(xen_prefix); + insn->next_byte +=3D prefixes->nbytes; + prefixes->got =3D 1; + + return 1; + +err_out: + return 0; +} + +int insn_has_xen_prefix(struct insn *insn) +{ + if (unlikely(insn->prefixes.nbytes =3D=3D sizeof(xen_prefix))) { + return !memcmp(insn->prefixes.bytes, xen_prefix, 3) && + insn->prefixes.bytes[3] =3D=3D + xen_prefix[sizeof(xen_prefix) - 1]; + } + + return 0; +} + /** * insn_get_prefixes - scan x86 instruction prefix bytes * @insn: &struct insn containing instruction @@ -79,6 +118,10 @@ void insn_get_prefixes(struct insn *insn) nb =3D 0; lb =3D 0; b =3D peek_next(insn_byte_t, insn); + + if (insn_xen_prefix(insn, b)) + return; + attr =3D inat_get_opcode_attribute(b); while (inat_is_legacy_prefix(attr)) { /* Skip if same prefix */ diff --git a/tools/objtool/sync-check.sh b/tools/objtool/sync-check.sh index 0a832e265a50..34143ea3d477 100755 --- a/tools/objtool/sync-check.sh +++ b/tools/objtool/sync-check.sh @@ -4,6 +4,7 @@ FILES=3D' arch/x86/include/asm/inat_types.h arch/x86/include/asm/orc_types.h +arch/x86/include/asm/xen/prefix.h arch/x86/lib/x86-opcode-map.txt arch/x86/tools/gen-insn-attr-x86.awk ' @@ -46,6 +47,6 @@ done check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_ty= pes.h[\">]"' check arch/x86/include/asm/insn.h '-I "^#include [\"<]\(asm/\)*inat.h[= \">]"' check arch/x86/lib/inat.c '-I "^#include [\"<]\(../include/\)*= asm/insn.h[\">]"' -check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*= asm/in\(at\|sn\).h[\">]"' +check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*= asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/xen/prefix.= h[\">]"' =20 cd - _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel From nobody Fri Apr 19 23:32:14 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1567597636; cv=none; d=zoho.com; s=zohoarc; b=Wmqj4HWc/I1Mf24xX4Jkh+gif8hm1SecK1Culatt+Xp2vg6NFO4zywHN1RaMiVaEpbd1ORSamuA75FvHiodGq09a903a7Lg2eZsRC6kE4DES0TNd3ynd3yVGDi+DJMnFBtg2aACRaEpnuK8MlASq9bXrymA8g2r85gCBMBzVLMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567597636; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=E4cLtaMj3oDJ6Kp+iZG9WfymOZueVvqvjVPx8BdjiDg=; b=AMzOKQX5CpjL9KQYyp3ZVhN8UXp5P9+Ra/IuqvARFGu7vsx8nrELafgqqffEBhsSqbCqCAY+imiQht+AzsT5xvQ0Swj5qcwm27L2I8NwQV4oeNEiAWNhI+/I3YI06bwh00f9z1MssvebF7InsYmSAVTqyVjmZfuPm92l3Y3aD7I= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 156759763622941.82075213185999; Wed, 4 Sep 2019 04:47:16 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i5Tjt-000830-5i; Wed, 04 Sep 2019 11:46:17 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i5Tjr-00082V-BA for xen-devel@lists.xenproject.org; Wed, 04 Sep 2019 11:46:15 +0000 Received: from mail.kernel.org (unknown [198.145.29.99]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 992aa230-cf09-11e9-abb0-12813bfff9fa; Wed, 04 Sep 2019 11:46:14 +0000 (UTC) Received: from localhost.localdomain (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EC0A920820; Wed, 4 Sep 2019 11:46:11 +0000 (UTC) X-Inumbo-ID: 992aa230-cf09-11e9-abb0-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567597574; bh=tHqEkww86KOLAmW5EQ0TIw1FCRFKi6/kyBIAI58FgmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tCvirIxsPyDLuECE+MpjKKpTgje2DwSZK2A5hmcbdQiW1cyxc+Pvk1Lo7atnCLVqd CosZL3Ic2nMvEv6WwnDL97wUaNn4N5A+Gkfebg70iepgH2JEVTQIdksucUonvRQeuo 3wD1KK8jLdHLqfGD0e+FSyAXNsQrfLCOzsdDnJDs= From: Masami Hiramatsu To: Ingo Molnar Date: Wed, 4 Sep 2019 20:46:09 +0900 Message-Id: <156759756944.24473.4664241966878257420.stgit@devnote2> X-Mailer: git-send-email 2.20.1 In-Reply-To: <156759754770.24473.11832897710080799131.stgit@devnote2> References: <156759754770.24473.11832897710080799131.stgit@devnote2> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Subject: [Xen-devel] [PATCH -tip 2/2] x86: kprobes: Prohibit probing on instruction which has Xen prefix X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Stefano Stabellini , Peter Zijlstra , Randy Dunlap , x86@kernel.org, linux-kernel@vger.kernel.org, Borislav Petkov , Josh Poimboeuf , xen-devel@lists.xenproject.org, Boris Ostrovsky Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Prohibit probing on instruction which has XEN_EMULATE_PREFIX (it must be cpuid.) Since that prefix is a marker for Xen, if we modify the marker by kprobe's int3, that doesn't work as expected. Signed-off-by: Masami Hiramatsu Acked-by: Peter Zijlstra (Intel) --- arch/x86/kernel/kprobes/core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 43fc13c831af..b1e86af4a985 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -351,6 +351,10 @@ int __copy_instruction(u8 *dest, u8 *src, u8 *real, st= ruct insn *insn) kernel_insn_init(insn, dest, MAX_INSN_SIZE); insn_get_length(insn); =20 + /* We can not probe XEN_EMULATE_PREFIX instruction */ + if (insn_has_xen_prefix(insn)) + return 0; + /* Another subsystem puts a breakpoint, failed to recover */ if (insn->opcode.bytes[0] =3D=3D BREAKPOINT_INSTRUCTION) return 0; _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel