From nobody Thu Apr 25 06:47:01 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=1567731091; cv=none; d=zoho.com; s=zohoarc; b=XJgphziAKjHpr/CpzAAvaKuVcJTHsWMxCZ8JSCvdKU2BcZVBntq4P7sWsEcaHXsRk+f2rPFeDSCKZiO8uA+kCH2nlMTogHo429p5sZOoRqJ2g8pt/IvHCPlAI2uei9hQVUju9gumt8CPZSYsU2UGzJPcJI9yg9r8W1CuDyoF+y0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567731091; 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=Kkb++iJ4EtcS1AknKslIorK0/TS3mCP2PZuP859t3lo=; b=XKV0iuDox8Ym3OTiRdrPBbQ/i3AvnRqLSipXVzRkqaRVB/YqZpKPHFX7WBuIYFNtQnaeF/Xk6CGtfun6srURbMksFlquzNqpz611GAuHvozEY0020wnTwQwbNR/oeaBPYy6zRGl6aBTQm71XPp48rS0KwHmR1bs3nf2bFIJFcgs= 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 1567731091304101.04782932961268; Thu, 5 Sep 2019 17:51:31 -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 1i62SH-0005b0-VQ; Fri, 06 Sep 2019 00:50:25 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i62SH-0005at-LW for xen-devel@lists.xenproject.org; Fri, 06 Sep 2019 00:50:25 +0000 Received: from mail.kernel.org (unknown [198.145.29.99]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 4fa9e430-d040-11e9-b299-bc764e2007e4; Fri, 06 Sep 2019 00:50:25 +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 99009207E0; Fri, 6 Sep 2019 00:50:21 +0000 (UTC) X-Inumbo-ID: 4fa9e430-d040-11e9-b299-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567731024; bh=+Nnz0FIHQJ3jHYwiPpXCPIDVXG7NsASc8gVWuOyip0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zsuXaaqGyaOfDGQ7DyKPVO1u6gl1Yd+AH8J/Sa9mAthkCyT1oiMDaKAt1ycfwvcNS AodFvZKMyOLo4Xw1WRvjL+fHn4N8sYado5rBcUSR3LW6LJAlO3rDmt+9kfiNbqEsvM yss5dQEs77VIhrrrRqt1aWZU+aPjool/Ok5y60EA= From: Masami Hiramatsu To: Ingo Molnar Date: Fri, 6 Sep 2019 09:50:19 +0900 Message-Id: <156773101914.29031.4027232648773934988.stgit@devnote2> X-Mailer: git-send-email 2.20.1 In-Reply-To: <156773100816.29031.12557431294039450779.stgit@devnote2> References: <156773100816.29031.12557431294039450779.stgit@devnote2> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Subject: [Xen-devel] [PATCH -tip v2 1/2] x86: xen: insn: Decode Xen and KVM emulate-prefix signature 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 , Andrew Cooper , 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 and KVM's emulate-prefix signature by x86 insn decoder. It is called "prefix" but actually not x86 instruction prefix, so this adds insn.emulate_prefix_size field instead of reusing insn.prefixes. If x86 decoder finds a special sequence of instructions of XEN_EMULATE_PREFIX and 'ud2a; .ascii "kvm"', it just counts the length, set insn.emulate_prefix_size and fold it with the next instruction. In other words, the signature and the next instruction is treated as a single instruction. Reported-by: Josh Poimboeuf Signed-off-by: Masami Hiramatsu Acked-by: Josh Poimboeuf --- Changes in v2: - Generalize the emulate-prefix handling not only for Xen but KVM. - Introduce insn.emulate_prefix_size instead of using insn.prefixes. --- arch/x86/include/asm/insn.h | 6 +++++ arch/x86/include/asm/xen/interface.h | 7 ++++-- arch/x86/include/asm/xen/prefix.h | 10 +++++++++ arch/x86/lib/insn.c | 36 +++++++++++++++++++++++++++= ++++ tools/arch/x86/include/asm/insn.h | 6 +++++ tools/arch/x86/include/asm/xen/prefix.h | 10 +++++++++ tools/arch/x86/lib/insn.c | 36 +++++++++++++++++++++++++++= ++++ tools/objtool/sync-check.sh | 3 ++- 8 files changed, 111 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..5c1ae3eff9d4 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -45,6 +45,7 @@ struct insn { struct insn_field immediate2; /* for 64bit imm or seg16 */ }; =20 + int emulate_prefix_size; insn_attr_t attr; unsigned char opnd_bytes; unsigned char addr_bytes; @@ -128,6 +129,11 @@ static inline int insn_is_evex(struct insn *insn) return (insn->vex_prefix.nbytes =3D=3D 4); } =20 +static inline int insn_has_emulate_prefix(struct insn *insn) +{ + return !!insn->emulate_prefix_size; +} + /* 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..b7eb50187db9 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,37 @@ 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 }; +/* See handle_ud()@arch/x86/kvm/x86.c */ +static const insn_byte_t kvm_prefix[] =3D "\xf\xbkvm"; + +static int __insn_get_emulate_prefix(struct insn *insn, + const insn_byte_t *prefix, size_t len) +{ + size_t i; + + for (i =3D 0; i < len; i++) { + if (peek_nbyte_next(insn_byte_t, insn, i) !=3D prefix[i]) + goto err_out; + } + + insn->emulate_prefix_size =3D len; + insn->next_byte +=3D len; + + return 1; + +err_out: + return 0; +} + +static void insn_get_emulate_prefix(struct insn *insn) +{ + if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix))) + return; + + __insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix)); +} + /** * insn_get_prefixes - scan x86 instruction prefix bytes * @insn: &struct insn containing instruction @@ -76,6 +110,8 @@ void insn_get_prefixes(struct insn *insn) if (prefixes->got) return; =20 + insn_get_emulate_prefix(insn); + nb =3D 0; lb =3D 0; b =3D peek_next(insn_byte_t, insn); diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm= /insn.h index 37a4c390750b..568854b14d0a 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/arch/x86/include/asm/insn.h @@ -45,6 +45,7 @@ struct insn { struct insn_field immediate2; /* for 64bit imm or seg16 */ }; =20 + int emulate_prefix_size; insn_attr_t attr; unsigned char opnd_bytes; unsigned char addr_bytes; @@ -128,6 +129,11 @@ static inline int insn_is_evex(struct insn *insn) return (insn->vex_prefix.nbytes =3D=3D 4); } =20 +static inline int insn_has_emulate_prefix(struct insn *insn) +{ + return !!insn->emulate_prefix_size; +} + /* 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..ce04e43e0749 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,37 @@ 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 }; +/* See handle_ud()@arch/x86/kvm/x86.c */ +static const insn_byte_t kvm_prefix[] =3D "\xf\xbkvm"; + +static int __insn_get_emulate_prefix(struct insn *insn, + const insn_byte_t *prefix, size_t len) +{ + size_t i; + + for (i =3D 0; i < len; i++) { + if (peek_nbyte_next(insn_byte_t, insn, i) !=3D prefix[i]) + goto err_out; + } + + insn->emulate_prefix_size =3D len; + insn->next_byte +=3D len; + + return 1; + +err_out: + return 0; +} + +static void insn_get_emulate_prefix(struct insn *insn) +{ + if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix))) + return; + + __insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix)); +} + /** * insn_get_prefixes - scan x86 instruction prefix bytes * @insn: &struct insn containing instruction @@ -76,6 +110,8 @@ void insn_get_prefixes(struct insn *insn) if (prefixes->got) return; =20 + insn_get_emulate_prefix(insn); + nb =3D 0; lb =3D 0; b =3D peek_next(insn_byte_t, insn); 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 Thu Apr 25 06:47:01 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=1567731094; cv=none; d=zoho.com; s=zohoarc; b=Z/WBt10NAsqvIRFZAi0TCB7D0BbKTyA5DXz5q6o5L+o+320Y7zCScqMj6d4Wy/EMLhldA/EmrGlgUxxyyeyP/5X4gVhAo0EOjK6urrw6xapIl8vVGPG6dokYVfaacLgPEW/kecMND/uU2p+KCk/86cez1/KclMfKHhxwbTT4smg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567731094; 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=6885yqWl8lobZhOnlGlT6UlsQ3UGcVYI/MMtWcJZ/LU=; b=O2zQ1h+vcQYG+e1y6NI39O4mTFLCmd80xE0Pj/pilV3iN7w3X50hpffyXWl7sLbyiUMZcKqSMORgfhlGzl2vmSNKgdwc3frimSU4bFQe4ULV2QpmW/xY5REs6jr/18D119chRY0VVsOjWtw0v51wmZuMR4OMzrrtM/mpPX2osRI= 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 1567731094197129.58035800521372; Thu, 5 Sep 2019 17:51:34 -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 1i62ST-0005cp-8U; Fri, 06 Sep 2019 00:50:37 +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 1i62SR-0005cZ-Uc for xen-devel@lists.xenproject.org; Fri, 06 Sep 2019 00:50:35 +0000 Received: from mail.kernel.org (unknown [198.145.29.99]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 5601ea4e-d040-11e9-abe4-12813bfff9fa; Fri, 06 Sep 2019 00:50:35 +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 42DA8207E0; Fri, 6 Sep 2019 00:50:32 +0000 (UTC) X-Inumbo-ID: 5601ea4e-d040-11e9-abe4-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567731035; bh=dTI/PD6Zp/AXRe7dx+Oz3QClHlFgk/sT4V/cXDhsk68=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fx7VJE8ob8mqtjb5+wF3Z5mBYga5Hvdg9BwlDSIKQKNDGzXS2/OmvsLpgr442JCE8 V40l0RzRj8vWAozINBkY2HgNYNd9yitc2/ltw+m4usRPnC+UJ3d9I0cgLr7XchxmcT CQAYZzOO7/KokO2b+nAOt+nVmpJUUDe2ybYMWfI0= From: Masami Hiramatsu To: Ingo Molnar Date: Fri, 6 Sep 2019 09:50:29 +0900 Message-Id: <156773102959.29031.18377408063770100148.stgit@devnote2> X-Mailer: git-send-email 2.20.1 In-Reply-To: <156773100816.29031.12557431294039450779.stgit@devnote2> References: <156773100816.29031.12557431294039450779.stgit@devnote2> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Subject: [Xen-devel] [PATCH -tip v2 2/2] x86: kprobes: Prohibit probing on instruction which has emulate 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 , Andrew Cooper , 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 or KVM's emulate prefix. Since that prefix is a marker for Xen and KVM, if we modify the marker by kprobe's int3, that doesn't work as expected. Signed-off-by: Masami Hiramatsu --- 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..4f13af7cbcdb 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 force emulate prefixed instruction */ + if (insn_has_emulate_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