]> git.proxmox.com Git - pve-kernel.git/blame - patches/kernel/0136-x86-insn-eval-Add-utility-functions-to-get-segment-s.patch
KPTI: add follow-up fixes
[pve-kernel.git] / patches / kernel / 0136-x86-insn-eval-Add-utility-functions-to-get-segment-s.patch
CommitLineData
321d628a
FG
1From 27298389d9d09bcebf06ea47206141b22fabcaca Mon Sep 17 00:00:00 2001
2From: Ingo Molnar <mingo@kernel.org>
3Date: Sat, 23 Dec 2017 13:14:25 +0100
e4cdf2a5 4Subject: [PATCH 136/241] x86/insn-eval: Add utility functions to get segment
321d628a
FG
5 selector
6MIME-Version: 1.0
7Content-Type: text/plain; charset=UTF-8
8Content-Transfer-Encoding: 8bit
9
10CVE-2017-5754
11
12When computing a linear address and segmentation is used, we need to know
13the base address of the segment involved in the computation. In most of
14the cases, the segment base address will be zero as in USER_DS/USER32_DS.
15However, it may be possible that a user space program defines its own
16segments via a local descriptor table. In such a case, the segment base
17address may not be zero. Thus, the segment base address is needed to
18calculate correctly the linear address.
19
20If running in protected mode, the segment selector to be used when
21computing a linear address is determined by either any of segment override
22prefixes in the instruction or inferred from the registers involved in the
23computation of the effective address; in that order. Also, there are cases
24when the segment override prefixes shall be ignored (i.e., code segments
25are always selected by the CS segment register; string instructions always
26use the ES segment register when using rDI register as operand). In long
27mode, segment registers are ignored, except for FS and GS. In these two
28cases, base addresses are obtained from the respective MSRs.
29
30For clarity, this process can be split into four steps (and an equal
31number of functions): determine if segment prefixes overrides can be used;
32parse the segment override prefixes, and use them if found; if not found
33or cannot be used, use the default segment registers associated with the
34operand registers. Once the segment register to use has been identified,
35read its value to obtain the segment selector.
36
37The method to obtain the segment selector depends on several factors. In
3832-bit builds, segment selectors are saved into a pt_regs structure
39when switching to kernel mode. The same is also true for virtual-8086
40mode. In 64-bit builds, segmentation is mostly ignored, except when
41running a program in 32-bit legacy mode. In this case, CS and SS can be
42obtained from pt_regs. DS, ES, FS and GS can be read directly from
43the respective segment registers.
44
45In order to identify the segment registers, a new set of #defines is
46introduced. It also includes two special identifiers. One of them
47indicates when the default segment register associated with instruction
48operands shall be used. Another one indicates that the contents of the
49segment register shall be ignored; this identifier is used when in long
50mode.
51
52Improvements-by: Borislav Petkov <bp@suse.de>
53Signed-off-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
54Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
55Reviewed-by: Borislav Petkov <bp@suse.de>
56Cc: "Michael S. Tsirkin" <mst@redhat.com>
57Cc: Peter Zijlstra <peterz@infradead.org>
58Cc: Dave Hansen <dave.hansen@linux.intel.com>
59Cc: ricardo.neri@intel.com
60Cc: Adrian Hunter <adrian.hunter@intel.com>
61Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
62Cc: Huang Rui <ray.huang@amd.com>
63Cc: Qiaowei Ren <qiaowei.ren@intel.com>
64Cc: Shuah Khan <shuah@kernel.org>
65Cc: Kees Cook <keescook@chromium.org>
66Cc: Jonathan Corbet <corbet@lwn.net>
67Cc: Jiri Slaby <jslaby@suse.cz>
68Cc: Dmitry Vyukov <dvyukov@google.com>
69Cc: "Ravi V. Shankar" <ravi.v.shankar@intel.com>
70Cc: Chris Metcalf <cmetcalf@mellanox.com>
71Cc: Brian Gerst <brgerst@gmail.com>
72Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
73Cc: Andy Lutomirski <luto@kernel.org>
74Cc: Colin Ian King <colin.king@canonical.com>
75Cc: Chen Yucong <slaoub@gmail.com>
76Cc: Adam Buchbinder <adam.buchbinder@gmail.com>
77Cc: Vlastimil Babka <vbabka@suse.cz>
78Cc: Lorenzo Stoakes <lstoakes@gmail.com>
79Cc: Masami Hiramatsu <mhiramat@kernel.org>
80Cc: Paolo Bonzini <pbonzini@redhat.com>
81Cc: Andrew Morton <akpm@linux-foundation.org>
82Cc: Thomas Garnier <thgarnie@google.com>
83Link: https://lkml.kernel.org/r/1509135945-13762-14-git-send-email-ricardo.neri-calderon@linux.intel.com
84Signed-off-by: Ingo Molnar <mingo@kernel.org>
85
86(Partially cherry picked from commit 32d0b95300db03c2b23b2ea2c94769a4a138e79d)
87
88(cherry picked from commit ca2c18cb10c8beb56dfe21321abdddc724cec4de)
89Signed-off-by: Andy Whitcroft <apw@canonical.com>
90Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
91(cherry picked from commit abd7780592a3687eacc0a295d4d2959bb11ff75f)
92Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
93---
94 arch/x86/include/asm/inat.h | 10 ++++++++++
95 1 file changed, 10 insertions(+)
96
97diff --git a/arch/x86/include/asm/inat.h b/arch/x86/include/asm/inat.h
98index 02aff0867211..1c78580e58be 100644
99--- a/arch/x86/include/asm/inat.h
100+++ b/arch/x86/include/asm/inat.h
101@@ -97,6 +97,16 @@
102 #define INAT_MAKE_GROUP(grp) ((grp << INAT_GRP_OFFS) | INAT_MODRM)
103 #define INAT_MAKE_IMM(imm) (imm << INAT_IMM_OFFS)
104
105+/* Identifiers for segment registers */
106+#define INAT_SEG_REG_IGNORE 0
107+#define INAT_SEG_REG_DEFAULT 1
108+#define INAT_SEG_REG_CS 2
109+#define INAT_SEG_REG_SS 3
110+#define INAT_SEG_REG_DS 4
111+#define INAT_SEG_REG_ES 5
112+#define INAT_SEG_REG_FS 6
113+#define INAT_SEG_REG_GS 7
114+
115 /* Attribute search APIs */
116 extern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode);
117 extern int inat_get_last_prefix_id(insn_byte_t last_pfx);
118--
1192.14.2
120