Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/mpx: remove MPX from arch/x86

From: Dave Hansen <dave.hansen@linux.intel.com>

MPX is being removed from the kernel due to a lack of support
in the toolchain going forward (gcc).

This removes all the remaining (dead at this point) MPX handling
code remaining in the tree. The only remaining code is the XSAVE
support for MPX state which is currently needd for KVM to handle
VMs which might use MPX.

Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: x86@kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>

authored by

Dave Hansen and committed by
Dave Hansen
45fc24e8 42222eae

+1 -1567
-252
Documentation/x86/intel_mpx.rst
··· 1 - .. SPDX-License-Identifier: GPL-2.0 2 - 3 - =========================================== 4 - Intel(R) Memory Protection Extensions (MPX) 5 - =========================================== 6 - 7 - Intel(R) MPX Overview 8 - ===================== 9 - 10 - Intel(R) Memory Protection Extensions (Intel(R) MPX) is a new capability 11 - introduced into Intel Architecture. Intel MPX provides hardware features 12 - that can be used in conjunction with compiler changes to check memory 13 - references, for those references whose compile-time normal intentions are 14 - usurped at runtime due to buffer overflow or underflow. 15 - 16 - You can tell if your CPU supports MPX by looking in /proc/cpuinfo:: 17 - 18 - cat /proc/cpuinfo | grep ' mpx ' 19 - 20 - For more information, please refer to Intel(R) Architecture Instruction 21 - Set Extensions Programming Reference, Chapter 9: Intel(R) Memory Protection 22 - Extensions. 23 - 24 - Note: As of December 2014, no hardware with MPX is available but it is 25 - possible to use SDE (Intel(R) Software Development Emulator) instead, which 26 - can be downloaded from 27 - http://software.intel.com/en-us/articles/intel-software-development-emulator 28 - 29 - 30 - How to get the advantage of MPX 31 - =============================== 32 - 33 - For MPX to work, changes are required in the kernel, binutils and compiler. 34 - No source changes are required for applications, just a recompile. 35 - 36 - There are a lot of moving parts of this to all work right. The following 37 - is how we expect the compiler, application and kernel to work together. 38 - 39 - 1) Application developer compiles with -fmpx. The compiler will add the 40 - instrumentation as well as some setup code called early after the app 41 - starts. New instruction prefixes are noops for old CPUs. 42 - 2) That setup code allocates (virtual) space for the "bounds directory", 43 - points the "bndcfgu" register to the directory (must also set the valid 44 - bit) and notifies the kernel (via the new prctl(PR_MPX_ENABLE_MANAGEMENT)) 45 - that the app will be using MPX. The app must be careful not to access 46 - the bounds tables between the time when it populates "bndcfgu" and 47 - when it calls the prctl(). This might be hard to guarantee if the app 48 - is compiled with MPX. You can add "__attribute__((bnd_legacy))" to 49 - the function to disable MPX instrumentation to help guarantee this. 50 - Also be careful not to call out to any other code which might be 51 - MPX-instrumented. 52 - 3) The kernel detects that the CPU has MPX, allows the new prctl() to 53 - succeed, and notes the location of the bounds directory. Userspace is 54 - expected to keep the bounds directory at that location. We note it 55 - instead of reading it each time because the 'xsave' operation needed 56 - to access the bounds directory register is an expensive operation. 57 - 4) If the application needs to spill bounds out of the 4 registers, it 58 - issues a bndstx instruction. Since the bounds directory is empty at 59 - this point, a bounds fault (#BR) is raised, the kernel allocates a 60 - bounds table (in the user address space) and makes the relevant entry 61 - in the bounds directory point to the new table. 62 - 5) If the application violates the bounds specified in the bounds registers, 63 - a separate kind of #BR is raised which will deliver a signal with 64 - information about the violation in the 'struct siginfo'. 65 - 6) Whenever memory is freed, we know that it can no longer contain valid 66 - pointers, and we attempt to free the associated space in the bounds 67 - tables. If an entire table becomes unused, we will attempt to free 68 - the table and remove the entry in the directory. 69 - 70 - To summarize, there are essentially three things interacting here: 71 - 72 - GCC with -fmpx: 73 - * enables annotation of code with MPX instructions and prefixes 74 - * inserts code early in the application to call in to the "gcc runtime" 75 - GCC MPX Runtime: 76 - * Checks for hardware MPX support in cpuid leaf 77 - * allocates virtual space for the bounds directory (malloc() essentially) 78 - * points the hardware BNDCFGU register at the directory 79 - * calls a new prctl(PR_MPX_ENABLE_MANAGEMENT) to notify the kernel to 80 - start managing the bounds directories 81 - Kernel MPX Code: 82 - * Checks for hardware MPX support in cpuid leaf 83 - * Handles #BR exceptions and sends SIGSEGV to the app when it violates 84 - bounds, like during a buffer overflow. 85 - * When bounds are spilled in to an unallocated bounds table, the kernel 86 - notices in the #BR exception, allocates the virtual space, then 87 - updates the bounds directory to point to the new table. It keeps 88 - special track of the memory with a VM_MPX flag. 89 - * Frees unused bounds tables at the time that the memory they described 90 - is unmapped. 91 - 92 - 93 - How does MPX kernel code work 94 - ============================= 95 - 96 - Handling #BR faults caused by MPX 97 - --------------------------------- 98 - 99 - When MPX is enabled, there are 2 new situations that can generate 100 - #BR faults. 101 - 102 - * new bounds tables (BT) need to be allocated to save bounds. 103 - * bounds violation caused by MPX instructions. 104 - 105 - We hook #BR handler to handle these two new situations. 106 - 107 - On-demand kernel allocation of bounds tables 108 - -------------------------------------------- 109 - 110 - MPX only has 4 hardware registers for storing bounds information. If 111 - MPX-enabled code needs more than these 4 registers, it needs to spill 112 - them somewhere. It has two special instructions for this which allow 113 - the bounds to be moved between the bounds registers and some new "bounds 114 - tables". 115 - 116 - #BR exceptions are a new class of exceptions just for MPX. They are 117 - similar conceptually to a page fault and will be raised by the MPX 118 - hardware during both bounds violations or when the tables are not 119 - present. The kernel handles those #BR exceptions for not-present tables 120 - by carving the space out of the normal processes address space and then 121 - pointing the bounds-directory over to it. 122 - 123 - The tables need to be accessed and controlled by userspace because 124 - the instructions for moving bounds in and out of them are extremely 125 - frequent. They potentially happen every time a register points to 126 - memory. Any direct kernel involvement (like a syscall) to access the 127 - tables would obviously destroy performance. 128 - 129 - Why not do this in userspace? MPX does not strictly require anything in 130 - the kernel. It can theoretically be done completely from userspace. Here 131 - are a few ways this could be done. We don't think any of them are practical 132 - in the real-world, but here they are. 133 - 134 - :Q: Can virtual space simply be reserved for the bounds tables so that we 135 - never have to allocate them? 136 - :A: MPX-enabled application will possibly create a lot of bounds tables in 137 - process address space to save bounds information. These tables can take 138 - up huge swaths of memory (as much as 80% of the memory on the system) 139 - even if we clean them up aggressively. In the worst-case scenario, the 140 - tables can be 4x the size of the data structure being tracked. IOW, a 141 - 1-page structure can require 4 bounds-table pages. An X-GB virtual 142 - area needs 4*X GB of virtual space, plus 2GB for the bounds directory. 143 - If we were to preallocate them for the 128TB of user virtual address 144 - space, we would need to reserve 512TB+2GB, which is larger than the 145 - entire virtual address space today. This means they can not be reserved 146 - ahead of time. Also, a single process's pre-populated bounds directory 147 - consumes 2GB of virtual *AND* physical memory. IOW, it's completely 148 - infeasible to prepopulate bounds directories. 149 - 150 - :Q: Can we preallocate bounds table space at the same time memory is 151 - allocated which might contain pointers that might eventually need 152 - bounds tables? 153 - :A: This would work if we could hook the site of each and every memory 154 - allocation syscall. This can be done for small, constrained applications. 155 - But, it isn't practical at a larger scale since a given app has no 156 - way of controlling how all the parts of the app might allocate memory 157 - (think libraries). The kernel is really the only place to intercept 158 - these calls. 159 - 160 - :Q: Could a bounds fault be handed to userspace and the tables allocated 161 - there in a signal handler instead of in the kernel? 162 - :A: mmap() is not on the list of safe async handler functions and even 163 - if mmap() would work it still requires locking or nasty tricks to 164 - keep track of the allocation state there. 165 - 166 - Having ruled out all of the userspace-only approaches for managing 167 - bounds tables that we could think of, we create them on demand in 168 - the kernel. 169 - 170 - Decoding MPX instructions 171 - ------------------------- 172 - 173 - If a #BR is generated due to a bounds violation caused by MPX. 174 - We need to decode MPX instructions to get violation address and 175 - set this address into extended struct siginfo. 176 - 177 - The _sigfault field of struct siginfo is extended as follow:: 178 - 179 - 87 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */ 180 - 88 struct { 181 - 89 void __user *_addr; /* faulting insn/memory ref. */ 182 - 90 #ifdef __ARCH_SI_TRAPNO 183 - 91 int _trapno; /* TRAP # which caused the signal */ 184 - 92 #endif 185 - 93 short _addr_lsb; /* LSB of the reported address */ 186 - 94 struct { 187 - 95 void __user *_lower; 188 - 96 void __user *_upper; 189 - 97 } _addr_bnd; 190 - 98 } _sigfault; 191 - 192 - The '_addr' field refers to violation address, and new '_addr_and' 193 - field refers to the upper/lower bounds when a #BR is caused. 194 - 195 - Glibc will be also updated to support this new siginfo. So user 196 - can get violation address and bounds when bounds violations occur. 197 - 198 - Cleanup unused bounds tables 199 - ---------------------------- 200 - 201 - When a BNDSTX instruction attempts to save bounds to a bounds directory 202 - entry marked as invalid, a #BR is generated. This is an indication that 203 - no bounds table exists for this entry. In this case the fault handler 204 - will allocate a new bounds table on demand. 205 - 206 - Since the kernel allocated those tables on-demand without userspace 207 - knowledge, it is also responsible for freeing them when the associated 208 - mappings go away. 209 - 210 - Here, the solution for this issue is to hook do_munmap() to check 211 - whether one process is MPX enabled. If yes, those bounds tables covered 212 - in the virtual address region which is being unmapped will be freed also. 213 - 214 - Adding new prctl commands 215 - ------------------------- 216 - 217 - Two new prctl commands are added to enable and disable MPX bounds tables 218 - management in kernel. 219 - :: 220 - 221 - 155 #define PR_MPX_ENABLE_MANAGEMENT 43 222 - 156 #define PR_MPX_DISABLE_MANAGEMENT 44 223 - 224 - Runtime library in userspace is responsible for allocation of bounds 225 - directory. So kernel have to use XSAVE instruction to get the base 226 - of bounds directory from BNDCFG register. 227 - 228 - But XSAVE is expected to be very expensive. In order to do performance 229 - optimization, we have to get the base of bounds directory and save it 230 - into struct mm_struct to be used in future during PR_MPX_ENABLE_MANAGEMENT 231 - command execution. 232 - 233 - 234 - Special rules 235 - ============= 236 - 237 - 1) If userspace is requesting help from the kernel to do the management 238 - of bounds tables, it may not create or modify entries in the bounds directory. 239 - 240 - Certainly users can allocate bounds tables and forcibly point the bounds 241 - directory at them through XSAVE instruction, and then set valid bit 242 - of bounds entry to have this entry valid. But, the kernel will decline 243 - to assist in managing these tables. 244 - 245 - 2) Userspace may not take multiple bounds directory entries and point 246 - them at the same bounds table. 247 - 248 - This is allowed architecturally. See more information "Intel(R) Architecture 249 - Instruction Set Extensions Programming Reference" (9.3.4). 250 - 251 - However, if users did this, the kernel might be fooled in to unmapping an 252 - in-use bounds table since it does not recognize sharing.
-6
arch/x86/include/asm/bugs.h
··· 6 6 7 7 extern void check_bugs(void); 8 8 9 - #if defined(CONFIG_CPU_SUP_INTEL) 10 - void check_mpx_erratum(struct cpuinfo_x86 *c); 11 - #else 12 - static inline void check_mpx_erratum(struct cpuinfo_x86 *c) {} 13 - #endif 14 - 15 9 #if defined(CONFIG_CPU_SUP_INTEL) && defined(CONFIG_X86_32) 16 10 int ppro_with_ram_bug(void); 17 11 #else
+1 -7
arch/x86/include/asm/disabled-features.h
··· 10 10 * cpu_feature_enabled(). 11 11 */ 12 12 13 - #ifdef CONFIG_X86_INTEL_MPX 14 - # define DISABLE_MPX 0 15 - #else 16 - # define DISABLE_MPX (1<<(X86_FEATURE_MPX & 31)) 17 - #endif 18 - 19 13 #ifdef CONFIG_X86_SMAP 20 14 # define DISABLE_SMAP 0 21 15 #else ··· 68 74 #define DISABLED_MASK6 0 69 75 #define DISABLED_MASK7 (DISABLE_PTI) 70 76 #define DISABLED_MASK8 0 71 - #define DISABLED_MASK9 (DISABLE_MPX|DISABLE_SMAP) 77 + #define DISABLED_MASK9 (DISABLE_SMAP) 72 78 #define DISABLED_MASK10 0 73 79 #define DISABLED_MASK11 0 74 80 #define DISABLED_MASK12 0
-4
arch/x86/include/asm/mmu.h
··· 50 50 u16 pkey_allocation_map; 51 51 s16 execute_only_pkey; 52 52 #endif 53 - #ifdef CONFIG_X86_INTEL_MPX 54 - /* address of the bounds directory */ 55 - void __user *bd_addr; 56 - #endif 57 53 } mm_context_t; 58 54 59 55 #define INIT_MM_CONTEXT(mm) \
-20
arch/x86/include/asm/mmu_context.h
··· 12 12 #include <asm/pgalloc.h> 13 13 #include <asm/tlbflush.h> 14 14 #include <asm/paravirt.h> 15 - #include <asm/mpx.h> 16 15 #include <asm/debugreg.h> 17 16 18 17 extern atomic64_t last_mm_ctx_id; ··· 274 275 static inline void arch_unmap(struct mm_struct *mm, unsigned long start, 275 276 unsigned long end) 276 277 { 277 - /* 278 - * mpx_notify_unmap() goes and reads a rarely-hot 279 - * cacheline in the mm_struct. That can be expensive 280 - * enough to be seen in profiles. 281 - * 282 - * The mpx_notify_unmap() call and its contents have been 283 - * observed to affect munmap() performance on hardware 284 - * where MPX is not present. 285 - * 286 - * The unlikely() optimizes for the fast case: no MPX 287 - * in the CPU, or no MPX use in the process. Even if 288 - * we get this wrong (in the unlikely event that MPX 289 - * is widely enabled on some system) the overhead of 290 - * MPX itself (reading bounds tables) is expected to 291 - * overwhelm the overhead of getting this unlikely() 292 - * consistently wrong. 293 - */ 294 - if (unlikely(cpu_feature_enabled(X86_FEATURE_MPX))) 295 - mpx_notify_unmap(mm, start, end); 296 278 } 297 279 298 280 /*
-116
arch/x86/include/asm/mpx.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _ASM_X86_MPX_H 3 - #define _ASM_X86_MPX_H 4 - 5 - #include <linux/types.h> 6 - #include <linux/mm_types.h> 7 - 8 - #include <asm/ptrace.h> 9 - #include <asm/insn.h> 10 - 11 - /* 12 - * NULL is theoretically a valid place to put the bounds 13 - * directory, so point this at an invalid address. 14 - */ 15 - #define MPX_INVALID_BOUNDS_DIR ((void __user *)-1) 16 - #define MPX_BNDCFG_ENABLE_FLAG 0x1 17 - #define MPX_BD_ENTRY_VALID_FLAG 0x1 18 - 19 - /* 20 - * The upper 28 bits [47:20] of the virtual address in 64-bit 21 - * are used to index into bounds directory (BD). 22 - * 23 - * The directory is 2G (2^31) in size, and with 8-byte entries 24 - * it has 2^28 entries. 25 - */ 26 - #define MPX_BD_SIZE_BYTES_64 (1UL<<31) 27 - #define MPX_BD_ENTRY_BYTES_64 8 28 - #define MPX_BD_NR_ENTRIES_64 (MPX_BD_SIZE_BYTES_64/MPX_BD_ENTRY_BYTES_64) 29 - 30 - /* 31 - * The 32-bit directory is 4MB (2^22) in size, and with 4-byte 32 - * entries it has 2^20 entries. 33 - */ 34 - #define MPX_BD_SIZE_BYTES_32 (1UL<<22) 35 - #define MPX_BD_ENTRY_BYTES_32 4 36 - #define MPX_BD_NR_ENTRIES_32 (MPX_BD_SIZE_BYTES_32/MPX_BD_ENTRY_BYTES_32) 37 - 38 - /* 39 - * A 64-bit table is 4MB total in size, and an entry is 40 - * 4 64-bit pointers in size. 41 - */ 42 - #define MPX_BT_SIZE_BYTES_64 (1UL<<22) 43 - #define MPX_BT_ENTRY_BYTES_64 32 44 - #define MPX_BT_NR_ENTRIES_64 (MPX_BT_SIZE_BYTES_64/MPX_BT_ENTRY_BYTES_64) 45 - 46 - /* 47 - * A 32-bit table is 16kB total in size, and an entry is 48 - * 4 32-bit pointers in size. 49 - */ 50 - #define MPX_BT_SIZE_BYTES_32 (1UL<<14) 51 - #define MPX_BT_ENTRY_BYTES_32 16 52 - #define MPX_BT_NR_ENTRIES_32 (MPX_BT_SIZE_BYTES_32/MPX_BT_ENTRY_BYTES_32) 53 - 54 - #define MPX_BNDSTA_TAIL 2 55 - #define MPX_BNDCFG_TAIL 12 56 - #define MPX_BNDSTA_ADDR_MASK (~((1UL<<MPX_BNDSTA_TAIL)-1)) 57 - #define MPX_BNDCFG_ADDR_MASK (~((1UL<<MPX_BNDCFG_TAIL)-1)) 58 - #define MPX_BNDSTA_ERROR_CODE 0x3 59 - 60 - struct mpx_fault_info { 61 - void __user *addr; 62 - void __user *lower; 63 - void __user *upper; 64 - }; 65 - 66 - #ifdef CONFIG_X86_INTEL_MPX 67 - 68 - extern int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs); 69 - extern int mpx_handle_bd_fault(void); 70 - 71 - static inline int kernel_managing_mpx_tables(struct mm_struct *mm) 72 - { 73 - return (mm->context.bd_addr != MPX_INVALID_BOUNDS_DIR); 74 - } 75 - 76 - static inline void mpx_mm_init(struct mm_struct *mm) 77 - { 78 - /* 79 - * NULL is theoretically a valid place to put the bounds 80 - * directory, so point this at an invalid address. 81 - */ 82 - mm->context.bd_addr = MPX_INVALID_BOUNDS_DIR; 83 - } 84 - 85 - extern void mpx_notify_unmap(struct mm_struct *mm, unsigned long start, unsigned long end); 86 - extern unsigned long mpx_unmapped_area_check(unsigned long addr, unsigned long len, unsigned long flags); 87 - 88 - #else 89 - static inline int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs) 90 - { 91 - return -EINVAL; 92 - } 93 - static inline int mpx_handle_bd_fault(void) 94 - { 95 - return -EINVAL; 96 - } 97 - static inline int kernel_managing_mpx_tables(struct mm_struct *mm) 98 - { 99 - return 0; 100 - } 101 - static inline void mpx_mm_init(struct mm_struct *mm) 102 - { 103 - } 104 - static inline void mpx_notify_unmap(struct mm_struct *mm, 105 - unsigned long start, unsigned long end) 106 - { 107 - } 108 - 109 - static inline unsigned long mpx_unmapped_area_check(unsigned long addr, 110 - unsigned long len, unsigned long flags) 111 - { 112 - return addr; 113 - } 114 - #endif /* CONFIG_X86_INTEL_MPX */ 115 - 116 - #endif /* _ASM_X86_MPX_H */
-18
arch/x86/include/asm/processor.h
··· 915 915 916 916 DECLARE_PER_CPU(u64, msr_misc_features_shadow); 917 917 918 - /* Register/unregister a process' MPX related resource */ 919 - #define MPX_ENABLE_MANAGEMENT() mpx_enable_management() 920 - #define MPX_DISABLE_MANAGEMENT() mpx_disable_management() 921 - 922 - #ifdef CONFIG_X86_INTEL_MPX 923 - extern int mpx_enable_management(void); 924 - extern int mpx_disable_management(void); 925 - #else 926 - static inline int mpx_enable_management(void) 927 - { 928 - return -EINVAL; 929 - } 930 - static inline int mpx_disable_management(void) 931 - { 932 - return -EINVAL; 933 - } 934 - #endif /* CONFIG_X86_INTEL_MPX */ 935 - 936 918 #ifdef CONFIG_CPU_SUP_AMD 937 919 extern u16 amd_get_nb_id(int cpu); 938 920 extern u32 amd_get_nodes_per_socket(void);
-134
arch/x86/include/asm/trace/mpx.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #undef TRACE_SYSTEM 3 - #define TRACE_SYSTEM mpx 4 - 5 - #if !defined(_TRACE_MPX_H) || defined(TRACE_HEADER_MULTI_READ) 6 - #define _TRACE_MPX_H 7 - 8 - #include <linux/tracepoint.h> 9 - 10 - #ifdef CONFIG_X86_INTEL_MPX 11 - 12 - TRACE_EVENT(mpx_bounds_register_exception, 13 - 14 - TP_PROTO(void __user *addr_referenced, 15 - const struct mpx_bndreg *bndreg), 16 - TP_ARGS(addr_referenced, bndreg), 17 - 18 - TP_STRUCT__entry( 19 - __field(void __user *, addr_referenced) 20 - __field(u64, lower_bound) 21 - __field(u64, upper_bound) 22 - ), 23 - 24 - TP_fast_assign( 25 - __entry->addr_referenced = addr_referenced; 26 - __entry->lower_bound = bndreg->lower_bound; 27 - __entry->upper_bound = bndreg->upper_bound; 28 - ), 29 - /* 30 - * Note that we are printing out the '~' of the upper 31 - * bounds register here. It is actually stored in its 32 - * one's complement form so that its 'init' state 33 - * corresponds to all 0's. But, that looks like 34 - * gibberish when printed out, so print out the 1's 35 - * complement instead of the actual value here. Note 36 - * though that you still need to specify filters for the 37 - * actual value, not the displayed one. 38 - */ 39 - TP_printk("address referenced: 0x%p bounds: lower: 0x%llx ~upper: 0x%llx", 40 - __entry->addr_referenced, 41 - __entry->lower_bound, 42 - ~__entry->upper_bound 43 - ) 44 - ); 45 - 46 - TRACE_EVENT(bounds_exception_mpx, 47 - 48 - TP_PROTO(const struct mpx_bndcsr *bndcsr), 49 - TP_ARGS(bndcsr), 50 - 51 - TP_STRUCT__entry( 52 - __field(u64, bndcfgu) 53 - __field(u64, bndstatus) 54 - ), 55 - 56 - TP_fast_assign( 57 - /* need to get rid of the 'const' on bndcsr */ 58 - __entry->bndcfgu = (u64)bndcsr->bndcfgu; 59 - __entry->bndstatus = (u64)bndcsr->bndstatus; 60 - ), 61 - 62 - TP_printk("bndcfgu:0x%llx bndstatus:0x%llx", 63 - __entry->bndcfgu, 64 - __entry->bndstatus) 65 - ); 66 - 67 - DECLARE_EVENT_CLASS(mpx_range_trace, 68 - 69 - TP_PROTO(unsigned long start, 70 - unsigned long end), 71 - TP_ARGS(start, end), 72 - 73 - TP_STRUCT__entry( 74 - __field(unsigned long, start) 75 - __field(unsigned long, end) 76 - ), 77 - 78 - TP_fast_assign( 79 - __entry->start = start; 80 - __entry->end = end; 81 - ), 82 - 83 - TP_printk("[0x%p:0x%p]", 84 - (void *)__entry->start, 85 - (void *)__entry->end 86 - ) 87 - ); 88 - 89 - DEFINE_EVENT(mpx_range_trace, mpx_unmap_zap, 90 - TP_PROTO(unsigned long start, unsigned long end), 91 - TP_ARGS(start, end) 92 - ); 93 - 94 - DEFINE_EVENT(mpx_range_trace, mpx_unmap_search, 95 - TP_PROTO(unsigned long start, unsigned long end), 96 - TP_ARGS(start, end) 97 - ); 98 - 99 - TRACE_EVENT(mpx_new_bounds_table, 100 - 101 - TP_PROTO(unsigned long table_vaddr), 102 - TP_ARGS(table_vaddr), 103 - 104 - TP_STRUCT__entry( 105 - __field(unsigned long, table_vaddr) 106 - ), 107 - 108 - TP_fast_assign( 109 - __entry->table_vaddr = table_vaddr; 110 - ), 111 - 112 - TP_printk("table vaddr:%p", (void *)__entry->table_vaddr) 113 - ); 114 - 115 - #else 116 - 117 - /* 118 - * This gets used outside of MPX-specific code, so we need a stub. 119 - */ 120 - static inline 121 - void trace_bounds_exception_mpx(const struct mpx_bndcsr *bndcsr) 122 - { 123 - } 124 - 125 - #endif /* CONFIG_X86_INTEL_MPX */ 126 - 127 - #undef TRACE_INCLUDE_PATH 128 - #define TRACE_INCLUDE_PATH asm/trace/ 129 - #undef TRACE_INCLUDE_FILE 130 - #define TRACE_INCLUDE_FILE mpx 131 - #endif /* _TRACE_MPX_H */ 132 - 133 - /* This part must be outside protection */ 134 - #include <trace/define_trace.h>
-18
arch/x86/kernel/cpu/common.c
··· 165 165 } }; 166 166 EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); 167 167 168 - static int __init x86_mpx_setup(char *s) 169 - { 170 - /* require an exact match without trailing characters */ 171 - if (strlen(s)) 172 - return 0; 173 - 174 - /* do not emit a message if the feature is not present */ 175 - if (!boot_cpu_has(X86_FEATURE_MPX)) 176 - return 1; 177 - 178 - setup_clear_cpu_cap(X86_FEATURE_MPX); 179 - pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n"); 180 - return 1; 181 - } 182 - __setup("nompx", x86_mpx_setup); 183 - 184 168 #ifdef CONFIG_X86_64 185 169 static int __init x86_nopcid_setup(char *s) 186 170 { ··· 291 307 static __init int setup_disable_smep(char *arg) 292 308 { 293 309 setup_clear_cpu_cap(X86_FEATURE_SMEP); 294 - /* Check for things that depend on SMEP being enabled: */ 295 - check_mpx_erratum(&boot_cpu_data); 296 310 return 1; 297 311 } 298 312 __setup("nosmep", setup_disable_smep);
-36
arch/x86/kernel/cpu/intel.c
··· 32 32 #endif 33 33 34 34 /* 35 - * Just in case our CPU detection goes bad, or you have a weird system, 36 - * allow a way to override the automatic disabling of MPX. 37 - */ 38 - static int forcempx; 39 - 40 - static int __init forcempx_setup(char *__unused) 41 - { 42 - forcempx = 1; 43 - 44 - return 1; 45 - } 46 - __setup("intel-skd-046-workaround=disable", forcempx_setup); 47 - 48 - void check_mpx_erratum(struct cpuinfo_x86 *c) 49 - { 50 - if (forcempx) 51 - return; 52 - /* 53 - * Turn off the MPX feature on CPUs where SMEP is not 54 - * available or disabled. 55 - * 56 - * Works around Intel Erratum SKD046: "Branch Instructions 57 - * May Initialize MPX Bound Registers Incorrectly". 58 - * 59 - * This might falsely disable MPX on systems without 60 - * SMEP, like Atom processors without SMEP. But there 61 - * is no such hardware known at the moment. 62 - */ 63 - if (cpu_has(c, X86_FEATURE_MPX) && !cpu_has(c, X86_FEATURE_SMEP)) { 64 - setup_clear_cpu_cap(X86_FEATURE_MPX); 65 - pr_warn("x86/mpx: Disabling MPX since SMEP not present\n"); 66 - } 67 - } 68 - 69 - /* 70 35 * Processors which have self-snooping capability can handle conflicting 71 36 * memory type across CPUs by snooping its own cache. However, there exists 72 37 * CPU models in which having conflicting memory types still leads to ··· 295 330 c->x86_coreid_bits = get_count_order((ebx >> 16) & 0xff); 296 331 } 297 332 298 - check_mpx_erratum(c); 299 333 check_memory_type_self_snoop_errata(c); 300 334 301 335 /*
-2
arch/x86/kernel/setup.c
··· 947 947 init_mm.end_data = (unsigned long) _edata; 948 948 init_mm.brk = _brk_end; 949 949 950 - mpx_mm_init(&init_mm); 951 - 952 950 code_resource.start = __pa_symbol(_text); 953 951 code_resource.end = __pa_symbol(_etext)-1; 954 952 data_resource.start = __pa_symbol(_etext);
-9
arch/x86/kernel/sys_x86_64.c
··· 22 22 #include <asm/elf.h> 23 23 #include <asm/ia32.h> 24 24 #include <asm/syscalls.h> 25 - #include <asm/mpx.h> 26 25 27 26 /* 28 27 * Align a virtual address to avoid aliasing in the I$ on AMD F15h. ··· 136 137 struct vm_unmapped_area_info info; 137 138 unsigned long begin, end; 138 139 139 - addr = mpx_unmapped_area_check(addr, len, flags); 140 - if (IS_ERR_VALUE(addr)) 141 - return addr; 142 - 143 140 if (flags & MAP_FIXED) 144 141 return addr; 145 142 ··· 174 179 struct mm_struct *mm = current->mm; 175 180 unsigned long addr = addr0; 176 181 struct vm_unmapped_area_info info; 177 - 178 - addr = mpx_unmapped_area_check(addr, len, flags); 179 - if (IS_ERR_VALUE(addr)) 180 - return addr; 181 182 182 183 /* requested length too big for entire address space */ 183 184 if (len > TASK_SIZE)
-5
arch/x86/mm/hugetlbpage.c
··· 19 19 #include <asm/tlbflush.h> 20 20 #include <asm/pgalloc.h> 21 21 #include <asm/elf.h> 22 - #include <asm/mpx.h> 23 22 24 23 #if 0 /* This is just for testing */ 25 24 struct page * ··· 149 150 150 151 if (len & ~huge_page_mask(h)) 151 152 return -EINVAL; 152 - 153 - addr = mpx_unmapped_area_check(addr, len, flags); 154 - if (IS_ERR_VALUE(addr)) 155 - return addr; 156 153 157 154 if (len > TASK_SIZE) 158 155 return -ENOMEM;
-2
arch/x86/mm/mmap.c
··· 163 163 164 164 const char *arch_vma_name(struct vm_area_struct *vma) 165 165 { 166 - if (vma->vm_flags & VM_MPX) 167 - return "[mpx]"; 168 166 return NULL; 169 167 } 170 168
-938
arch/x86/mm/mpx.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - /* 3 - * mpx.c - Memory Protection eXtensions 4 - * 5 - * Copyright (c) 2014, Intel Corporation. 6 - * Qiaowei Ren <qiaowei.ren@intel.com> 7 - * Dave Hansen <dave.hansen@intel.com> 8 - */ 9 - #include <linux/kernel.h> 10 - #include <linux/slab.h> 11 - #include <linux/mm_types.h> 12 - #include <linux/mman.h> 13 - #include <linux/syscalls.h> 14 - #include <linux/sched/sysctl.h> 15 - 16 - #include <asm/insn.h> 17 - #include <asm/insn-eval.h> 18 - #include <asm/mmu_context.h> 19 - #include <asm/mpx.h> 20 - #include <asm/processor.h> 21 - #include <asm/fpu/internal.h> 22 - 23 - #define CREATE_TRACE_POINTS 24 - #include <asm/trace/mpx.h> 25 - 26 - static inline unsigned long mpx_bd_size_bytes(struct mm_struct *mm) 27 - { 28 - if (is_64bit_mm(mm)) 29 - return MPX_BD_SIZE_BYTES_64; 30 - else 31 - return MPX_BD_SIZE_BYTES_32; 32 - } 33 - 34 - static inline unsigned long mpx_bt_size_bytes(struct mm_struct *mm) 35 - { 36 - if (is_64bit_mm(mm)) 37 - return MPX_BT_SIZE_BYTES_64; 38 - else 39 - return MPX_BT_SIZE_BYTES_32; 40 - } 41 - 42 - /* 43 - * This is really a simplified "vm_mmap". it only handles MPX 44 - * bounds tables (the bounds directory is user-allocated). 45 - */ 46 - static unsigned long mpx_mmap(unsigned long len) 47 - { 48 - struct mm_struct *mm = current->mm; 49 - unsigned long addr, populate; 50 - 51 - /* Only bounds table can be allocated here */ 52 - if (len != mpx_bt_size_bytes(mm)) 53 - return -EINVAL; 54 - 55 - down_write(&mm->mmap_sem); 56 - addr = do_mmap(NULL, 0, len, PROT_READ | PROT_WRITE, 57 - MAP_ANONYMOUS | MAP_PRIVATE, VM_MPX, 0, &populate, NULL); 58 - up_write(&mm->mmap_sem); 59 - if (populate) 60 - mm_populate(addr, populate); 61 - 62 - return addr; 63 - } 64 - 65 - static int mpx_insn_decode(struct insn *insn, 66 - struct pt_regs *regs) 67 - { 68 - unsigned char buf[MAX_INSN_SIZE]; 69 - int x86_64 = !test_thread_flag(TIF_IA32); 70 - int not_copied; 71 - int nr_copied; 72 - 73 - not_copied = copy_from_user(buf, (void __user *)regs->ip, sizeof(buf)); 74 - nr_copied = sizeof(buf) - not_copied; 75 - /* 76 - * The decoder _should_ fail nicely if we pass it a short buffer. 77 - * But, let's not depend on that implementation detail. If we 78 - * did not get anything, just error out now. 79 - */ 80 - if (!nr_copied) 81 - return -EFAULT; 82 - insn_init(insn, buf, nr_copied, x86_64); 83 - insn_get_length(insn); 84 - /* 85 - * copy_from_user() tries to get as many bytes as we could see in 86 - * the largest possible instruction. If the instruction we are 87 - * after is shorter than that _and_ we attempt to copy from 88 - * something unreadable, we might get a short read. This is OK 89 - * as long as the read did not stop in the middle of the 90 - * instruction. Check to see if we got a partial instruction. 91 - */ 92 - if (nr_copied < insn->length) 93 - return -EFAULT; 94 - 95 - insn_get_opcode(insn); 96 - /* 97 - * We only _really_ need to decode bndcl/bndcn/bndcu 98 - * Error out on anything else. 99 - */ 100 - if (insn->opcode.bytes[0] != 0x0f) 101 - goto bad_opcode; 102 - if ((insn->opcode.bytes[1] != 0x1a) && 103 - (insn->opcode.bytes[1] != 0x1b)) 104 - goto bad_opcode; 105 - 106 - return 0; 107 - bad_opcode: 108 - return -EINVAL; 109 - } 110 - 111 - /* 112 - * If a bounds overflow occurs then a #BR is generated. This 113 - * function decodes MPX instructions to get violation address 114 - * and set this address into extended struct siginfo. 115 - * 116 - * Note that this is not a super precise way of doing this. 117 - * Userspace could have, by the time we get here, written 118 - * anything it wants in to the instructions. We can not 119 - * trust anything about it. They might not be valid 120 - * instructions or might encode invalid registers, etc... 121 - */ 122 - int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs) 123 - { 124 - const struct mpx_bndreg_state *bndregs; 125 - const struct mpx_bndreg *bndreg; 126 - struct insn insn; 127 - uint8_t bndregno; 128 - int err; 129 - 130 - err = mpx_insn_decode(&insn, regs); 131 - if (err) 132 - goto err_out; 133 - 134 - /* 135 - * We know at this point that we are only dealing with 136 - * MPX instructions. 137 - */ 138 - insn_get_modrm(&insn); 139 - bndregno = X86_MODRM_REG(insn.modrm.value); 140 - if (bndregno > 3) { 141 - err = -EINVAL; 142 - goto err_out; 143 - } 144 - /* get bndregs field from current task's xsave area */ 145 - bndregs = get_xsave_field_ptr(XFEATURE_BNDREGS); 146 - if (!bndregs) { 147 - err = -EINVAL; 148 - goto err_out; 149 - } 150 - /* now go select the individual register in the set of 4 */ 151 - bndreg = &bndregs->bndreg[bndregno]; 152 - 153 - /* 154 - * The registers are always 64-bit, but the upper 32 155 - * bits are ignored in 32-bit mode. Also, note that the 156 - * upper bounds are architecturally represented in 1's 157 - * complement form. 158 - * 159 - * The 'unsigned long' cast is because the compiler 160 - * complains when casting from integers to different-size 161 - * pointers. 162 - */ 163 - info->lower = (void __user *)(unsigned long)bndreg->lower_bound; 164 - info->upper = (void __user *)(unsigned long)~bndreg->upper_bound; 165 - info->addr = insn_get_addr_ref(&insn, regs); 166 - 167 - /* 168 - * We were not able to extract an address from the instruction, 169 - * probably because there was something invalid in it. 170 - */ 171 - if (info->addr == (void __user *)-1) { 172 - err = -EINVAL; 173 - goto err_out; 174 - } 175 - trace_mpx_bounds_register_exception(info->addr, bndreg); 176 - return 0; 177 - err_out: 178 - /* info might be NULL, but kfree() handles that */ 179 - return err; 180 - } 181 - 182 - static __user void *mpx_get_bounds_dir(void) 183 - { 184 - const struct mpx_bndcsr *bndcsr; 185 - 186 - if (!cpu_feature_enabled(X86_FEATURE_MPX)) 187 - return MPX_INVALID_BOUNDS_DIR; 188 - 189 - /* 190 - * The bounds directory pointer is stored in a register 191 - * only accessible if we first do an xsave. 192 - */ 193 - bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR); 194 - if (!bndcsr) 195 - return MPX_INVALID_BOUNDS_DIR; 196 - 197 - /* 198 - * Make sure the register looks valid by checking the 199 - * enable bit. 200 - */ 201 - if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG)) 202 - return MPX_INVALID_BOUNDS_DIR; 203 - 204 - /* 205 - * Lastly, mask off the low bits used for configuration 206 - * flags, and return the address of the bounds table. 207 - */ 208 - return (void __user *)(unsigned long) 209 - (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK); 210 - } 211 - 212 - int mpx_enable_management(void) 213 - { 214 - void __user *bd_base = MPX_INVALID_BOUNDS_DIR; 215 - struct mm_struct *mm = current->mm; 216 - int ret = 0; 217 - 218 - /* 219 - * runtime in the userspace will be responsible for allocation of 220 - * the bounds directory. Then, it will save the base of the bounds 221 - * directory into XSAVE/XRSTOR Save Area and enable MPX through 222 - * XRSTOR instruction. 223 - * 224 - * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is 225 - * expected to be relatively expensive. Storing the bounds 226 - * directory here means that we do not have to do xsave in the 227 - * unmap path; we can just use mm->context.bd_addr instead. 228 - */ 229 - bd_base = mpx_get_bounds_dir(); 230 - down_write(&mm->mmap_sem); 231 - 232 - /* MPX doesn't support addresses above 47 bits yet. */ 233 - if (find_vma(mm, DEFAULT_MAP_WINDOW)) { 234 - pr_warn_once("%s (%d): MPX cannot handle addresses " 235 - "above 47-bits. Disabling.", 236 - current->comm, current->pid); 237 - ret = -ENXIO; 238 - goto out; 239 - } 240 - mm->context.bd_addr = bd_base; 241 - if (mm->context.bd_addr == MPX_INVALID_BOUNDS_DIR) 242 - ret = -ENXIO; 243 - out: 244 - up_write(&mm->mmap_sem); 245 - return ret; 246 - } 247 - 248 - int mpx_disable_management(void) 249 - { 250 - struct mm_struct *mm = current->mm; 251 - 252 - if (!cpu_feature_enabled(X86_FEATURE_MPX)) 253 - return -ENXIO; 254 - 255 - down_write(&mm->mmap_sem); 256 - mm->context.bd_addr = MPX_INVALID_BOUNDS_DIR; 257 - up_write(&mm->mmap_sem); 258 - return 0; 259 - } 260 - 261 - static int mpx_cmpxchg_bd_entry(struct mm_struct *mm, 262 - unsigned long *curval, 263 - unsigned long __user *addr, 264 - unsigned long old_val, unsigned long new_val) 265 - { 266 - int ret; 267 - /* 268 - * user_atomic_cmpxchg_inatomic() actually uses sizeof() 269 - * the pointer that we pass to it to figure out how much 270 - * data to cmpxchg. We have to be careful here not to 271 - * pass a pointer to a 64-bit data type when we only want 272 - * a 32-bit copy. 273 - */ 274 - if (is_64bit_mm(mm)) { 275 - ret = user_atomic_cmpxchg_inatomic(curval, 276 - addr, old_val, new_val); 277 - } else { 278 - u32 uninitialized_var(curval_32); 279 - u32 old_val_32 = old_val; 280 - u32 new_val_32 = new_val; 281 - u32 __user *addr_32 = (u32 __user *)addr; 282 - 283 - ret = user_atomic_cmpxchg_inatomic(&curval_32, 284 - addr_32, old_val_32, new_val_32); 285 - *curval = curval_32; 286 - } 287 - return ret; 288 - } 289 - 290 - /* 291 - * With 32-bit mode, a bounds directory is 4MB, and the size of each 292 - * bounds table is 16KB. With 64-bit mode, a bounds directory is 2GB, 293 - * and the size of each bounds table is 4MB. 294 - */ 295 - static int allocate_bt(struct mm_struct *mm, long __user *bd_entry) 296 - { 297 - unsigned long expected_old_val = 0; 298 - unsigned long actual_old_val = 0; 299 - unsigned long bt_addr; 300 - unsigned long bd_new_entry; 301 - int ret = 0; 302 - 303 - /* 304 - * Carve the virtual space out of userspace for the new 305 - * bounds table: 306 - */ 307 - bt_addr = mpx_mmap(mpx_bt_size_bytes(mm)); 308 - if (IS_ERR((void *)bt_addr)) 309 - return PTR_ERR((void *)bt_addr); 310 - /* 311 - * Set the valid flag (kinda like _PAGE_PRESENT in a pte) 312 - */ 313 - bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG; 314 - 315 - /* 316 - * Go poke the address of the new bounds table in to the 317 - * bounds directory entry out in userspace memory. Note: 318 - * we may race with another CPU instantiating the same table. 319 - * In that case the cmpxchg will see an unexpected 320 - * 'actual_old_val'. 321 - * 322 - * This can fault, but that's OK because we do not hold 323 - * mmap_sem at this point, unlike some of the other part 324 - * of the MPX code that have to pagefault_disable(). 325 - */ 326 - ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, bd_entry, 327 - expected_old_val, bd_new_entry); 328 - if (ret) 329 - goto out_unmap; 330 - 331 - /* 332 - * The user_atomic_cmpxchg_inatomic() will only return nonzero 333 - * for faults, *not* if the cmpxchg itself fails. Now we must 334 - * verify that the cmpxchg itself completed successfully. 335 - */ 336 - /* 337 - * We expected an empty 'expected_old_val', but instead found 338 - * an apparently valid entry. Assume we raced with another 339 - * thread to instantiate this table and desclare succecss. 340 - */ 341 - if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) { 342 - ret = 0; 343 - goto out_unmap; 344 - } 345 - /* 346 - * We found a non-empty bd_entry but it did not have the 347 - * VALID_FLAG set. Return an error which will result in 348 - * a SEGV since this probably means that somebody scribbled 349 - * some invalid data in to a bounds table. 350 - */ 351 - if (expected_old_val != actual_old_val) { 352 - ret = -EINVAL; 353 - goto out_unmap; 354 - } 355 - trace_mpx_new_bounds_table(bt_addr); 356 - return 0; 357 - out_unmap: 358 - vm_munmap(bt_addr, mpx_bt_size_bytes(mm)); 359 - return ret; 360 - } 361 - 362 - /* 363 - * When a BNDSTX instruction attempts to save bounds to a bounds 364 - * table, it will first attempt to look up the table in the 365 - * first-level bounds directory. If it does not find a table in 366 - * the directory, a #BR is generated and we get here in order to 367 - * allocate a new table. 368 - * 369 - * With 32-bit mode, the size of BD is 4MB, and the size of each 370 - * bound table is 16KB. With 64-bit mode, the size of BD is 2GB, 371 - * and the size of each bound table is 4MB. 372 - */ 373 - static int do_mpx_bt_fault(void) 374 - { 375 - unsigned long bd_entry, bd_base; 376 - const struct mpx_bndcsr *bndcsr; 377 - struct mm_struct *mm = current->mm; 378 - 379 - bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR); 380 - if (!bndcsr) 381 - return -EINVAL; 382 - /* 383 - * Mask off the preserve and enable bits 384 - */ 385 - bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK; 386 - /* 387 - * The hardware provides the address of the missing or invalid 388 - * entry via BNDSTATUS, so we don't have to go look it up. 389 - */ 390 - bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK; 391 - /* 392 - * Make sure the directory entry is within where we think 393 - * the directory is. 394 - */ 395 - if ((bd_entry < bd_base) || 396 - (bd_entry >= bd_base + mpx_bd_size_bytes(mm))) 397 - return -EINVAL; 398 - 399 - return allocate_bt(mm, (long __user *)bd_entry); 400 - } 401 - 402 - int mpx_handle_bd_fault(void) 403 - { 404 - /* 405 - * Userspace never asked us to manage the bounds tables, 406 - * so refuse to help. 407 - */ 408 - if (!kernel_managing_mpx_tables(current->mm)) 409 - return -EINVAL; 410 - 411 - return do_mpx_bt_fault(); 412 - } 413 - 414 - /* 415 - * A thin wrapper around get_user_pages(). Returns 0 if the 416 - * fault was resolved or -errno if not. 417 - */ 418 - static int mpx_resolve_fault(long __user *addr, int write) 419 - { 420 - long gup_ret; 421 - int nr_pages = 1; 422 - 423 - gup_ret = get_user_pages((unsigned long)addr, nr_pages, 424 - write ? FOLL_WRITE : 0, NULL, NULL); 425 - /* 426 - * get_user_pages() returns number of pages gotten. 427 - * 0 means we failed to fault in and get anything, 428 - * probably because 'addr' is bad. 429 - */ 430 - if (!gup_ret) 431 - return -EFAULT; 432 - /* Other error, return it */ 433 - if (gup_ret < 0) 434 - return gup_ret; 435 - /* must have gup'd a page and gup_ret>0, success */ 436 - return 0; 437 - } 438 - 439 - static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm, 440 - unsigned long bd_entry) 441 - { 442 - unsigned long bt_addr = bd_entry; 443 - int align_to_bytes; 444 - /* 445 - * Bit 0 in a bt_entry is always the valid bit. 446 - */ 447 - bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG; 448 - /* 449 - * Tables are naturally aligned at 8-byte boundaries 450 - * on 64-bit and 4-byte boundaries on 32-bit. The 451 - * documentation makes it appear that the low bits 452 - * are ignored by the hardware, so we do the same. 453 - */ 454 - if (is_64bit_mm(mm)) 455 - align_to_bytes = 8; 456 - else 457 - align_to_bytes = 4; 458 - bt_addr &= ~(align_to_bytes-1); 459 - return bt_addr; 460 - } 461 - 462 - /* 463 - * We only want to do a 4-byte get_user() on 32-bit. Otherwise, 464 - * we might run off the end of the bounds table if we are on 465 - * a 64-bit kernel and try to get 8 bytes. 466 - */ 467 - static int get_user_bd_entry(struct mm_struct *mm, unsigned long *bd_entry_ret, 468 - long __user *bd_entry_ptr) 469 - { 470 - u32 bd_entry_32; 471 - int ret; 472 - 473 - if (is_64bit_mm(mm)) 474 - return get_user(*bd_entry_ret, bd_entry_ptr); 475 - 476 - /* 477 - * Note that get_user() uses the type of the *pointer* to 478 - * establish the size of the get, not the destination. 479 - */ 480 - ret = get_user(bd_entry_32, (u32 __user *)bd_entry_ptr); 481 - *bd_entry_ret = bd_entry_32; 482 - return ret; 483 - } 484 - 485 - /* 486 - * Get the base of bounds tables pointed by specific bounds 487 - * directory entry. 488 - */ 489 - static int get_bt_addr(struct mm_struct *mm, 490 - long __user *bd_entry_ptr, 491 - unsigned long *bt_addr_result) 492 - { 493 - int ret; 494 - int valid_bit; 495 - unsigned long bd_entry; 496 - unsigned long bt_addr; 497 - 498 - if (!access_ok((bd_entry_ptr), sizeof(*bd_entry_ptr))) 499 - return -EFAULT; 500 - 501 - while (1) { 502 - int need_write = 0; 503 - 504 - pagefault_disable(); 505 - ret = get_user_bd_entry(mm, &bd_entry, bd_entry_ptr); 506 - pagefault_enable(); 507 - if (!ret) 508 - break; 509 - if (ret == -EFAULT) 510 - ret = mpx_resolve_fault(bd_entry_ptr, need_write); 511 - /* 512 - * If we could not resolve the fault, consider it 513 - * userspace's fault and error out. 514 - */ 515 - if (ret) 516 - return ret; 517 - } 518 - 519 - valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG; 520 - bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry); 521 - 522 - /* 523 - * When the kernel is managing bounds tables, a bounds directory 524 - * entry will either have a valid address (plus the valid bit) 525 - * *OR* be completely empty. If we see a !valid entry *and* some 526 - * data in the address field, we know something is wrong. This 527 - * -EINVAL return will cause a SIGSEGV. 528 - */ 529 - if (!valid_bit && bt_addr) 530 - return -EINVAL; 531 - /* 532 - * Do we have an completely zeroed bt entry? That is OK. It 533 - * just means there was no bounds table for this memory. Make 534 - * sure to distinguish this from -EINVAL, which will cause 535 - * a SEGV. 536 - */ 537 - if (!valid_bit) 538 - return -ENOENT; 539 - 540 - *bt_addr_result = bt_addr; 541 - return 0; 542 - } 543 - 544 - static inline int bt_entry_size_bytes(struct mm_struct *mm) 545 - { 546 - if (is_64bit_mm(mm)) 547 - return MPX_BT_ENTRY_BYTES_64; 548 - else 549 - return MPX_BT_ENTRY_BYTES_32; 550 - } 551 - 552 - /* 553 - * Take a virtual address and turns it in to the offset in bytes 554 - * inside of the bounds table where the bounds table entry 555 - * controlling 'addr' can be found. 556 - */ 557 - static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct *mm, 558 - unsigned long addr) 559 - { 560 - unsigned long bt_table_nr_entries; 561 - unsigned long offset = addr; 562 - 563 - if (is_64bit_mm(mm)) { 564 - /* Bottom 3 bits are ignored on 64-bit */ 565 - offset >>= 3; 566 - bt_table_nr_entries = MPX_BT_NR_ENTRIES_64; 567 - } else { 568 - /* Bottom 2 bits are ignored on 32-bit */ 569 - offset >>= 2; 570 - bt_table_nr_entries = MPX_BT_NR_ENTRIES_32; 571 - } 572 - /* 573 - * We know the size of the table in to which we are 574 - * indexing, and we have eliminated all the low bits 575 - * which are ignored for indexing. 576 - * 577 - * Mask out all the high bits which we do not need 578 - * to index in to the table. Note that the tables 579 - * are always powers of two so this gives us a proper 580 - * mask. 581 - */ 582 - offset &= (bt_table_nr_entries-1); 583 - /* 584 - * We now have an entry offset in terms of *entries* in 585 - * the table. We need to scale it back up to bytes. 586 - */ 587 - offset *= bt_entry_size_bytes(mm); 588 - return offset; 589 - } 590 - 591 - /* 592 - * How much virtual address space does a single bounds 593 - * directory entry cover? 594 - * 595 - * Note, we need a long long because 4GB doesn't fit in 596 - * to a long on 32-bit. 597 - */ 598 - static inline unsigned long bd_entry_virt_space(struct mm_struct *mm) 599 - { 600 - unsigned long long virt_space; 601 - unsigned long long GB = (1ULL << 30); 602 - 603 - /* 604 - * This covers 32-bit emulation as well as 32-bit kernels 605 - * running on 64-bit hardware. 606 - */ 607 - if (!is_64bit_mm(mm)) 608 - return (4ULL * GB) / MPX_BD_NR_ENTRIES_32; 609 - 610 - /* 611 - * 'x86_virt_bits' returns what the hardware is capable 612 - * of, and returns the full >32-bit address space when 613 - * running 32-bit kernels on 64-bit hardware. 614 - */ 615 - virt_space = (1ULL << boot_cpu_data.x86_virt_bits); 616 - return virt_space / MPX_BD_NR_ENTRIES_64; 617 - } 618 - 619 - /* 620 - * Free the backing physical pages of bounds table 'bt_addr'. 621 - * Assume start...end is within that bounds table. 622 - */ 623 - static noinline int zap_bt_entries_mapping(struct mm_struct *mm, 624 - unsigned long bt_addr, 625 - unsigned long start_mapping, unsigned long end_mapping) 626 - { 627 - struct vm_area_struct *vma; 628 - unsigned long addr, len; 629 - unsigned long start; 630 - unsigned long end; 631 - 632 - /* 633 - * if we 'end' on a boundary, the offset will be 0 which 634 - * is not what we want. Back it up a byte to get the 635 - * last bt entry. Then once we have the entry itself, 636 - * move 'end' back up by the table entry size. 637 - */ 638 - start = bt_addr + mpx_get_bt_entry_offset_bytes(mm, start_mapping); 639 - end = bt_addr + mpx_get_bt_entry_offset_bytes(mm, end_mapping - 1); 640 - /* 641 - * Move end back up by one entry. Among other things 642 - * this ensures that it remains page-aligned and does 643 - * not screw up zap_page_range() 644 - */ 645 - end += bt_entry_size_bytes(mm); 646 - 647 - /* 648 - * Find the first overlapping vma. If vma->vm_start > start, there 649 - * will be a hole in the bounds table. This -EINVAL return will 650 - * cause a SIGSEGV. 651 - */ 652 - vma = find_vma(mm, start); 653 - if (!vma || vma->vm_start > start) 654 - return -EINVAL; 655 - 656 - /* 657 - * A NUMA policy on a VM_MPX VMA could cause this bounds table to 658 - * be split. So we need to look across the entire 'start -> end' 659 - * range of this bounds table, find all of the VM_MPX VMAs, and 660 - * zap only those. 661 - */ 662 - addr = start; 663 - while (vma && vma->vm_start < end) { 664 - /* 665 - * We followed a bounds directory entry down 666 - * here. If we find a non-MPX VMA, that's bad, 667 - * so stop immediately and return an error. This 668 - * probably results in a SIGSEGV. 669 - */ 670 - if (!(vma->vm_flags & VM_MPX)) 671 - return -EINVAL; 672 - 673 - len = min(vma->vm_end, end) - addr; 674 - zap_page_range(vma, addr, len); 675 - trace_mpx_unmap_zap(addr, addr+len); 676 - 677 - vma = vma->vm_next; 678 - addr = vma->vm_start; 679 - } 680 - return 0; 681 - } 682 - 683 - static unsigned long mpx_get_bd_entry_offset(struct mm_struct *mm, 684 - unsigned long addr) 685 - { 686 - /* 687 - * There are several ways to derive the bd offsets. We 688 - * use the following approach here: 689 - * 1. We know the size of the virtual address space 690 - * 2. We know the number of entries in a bounds table 691 - * 3. We know that each entry covers a fixed amount of 692 - * virtual address space. 693 - * So, we can just divide the virtual address by the 694 - * virtual space used by one entry to determine which 695 - * entry "controls" the given virtual address. 696 - */ 697 - if (is_64bit_mm(mm)) { 698 - int bd_entry_size = 8; /* 64-bit pointer */ 699 - /* 700 - * Take the 64-bit addressing hole in to account. 701 - */ 702 - addr &= ((1UL << boot_cpu_data.x86_virt_bits) - 1); 703 - return (addr / bd_entry_virt_space(mm)) * bd_entry_size; 704 - } else { 705 - int bd_entry_size = 4; /* 32-bit pointer */ 706 - /* 707 - * 32-bit has no hole so this case needs no mask 708 - */ 709 - return (addr / bd_entry_virt_space(mm)) * bd_entry_size; 710 - } 711 - /* 712 - * The two return calls above are exact copies. If we 713 - * pull out a single copy and put it in here, gcc won't 714 - * realize that we're doing a power-of-2 divide and use 715 - * shifts. It uses a real divide. If we put them up 716 - * there, it manages to figure it out (gcc 4.8.3). 717 - */ 718 - } 719 - 720 - static int unmap_entire_bt(struct mm_struct *mm, 721 - long __user *bd_entry, unsigned long bt_addr) 722 - { 723 - unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG; 724 - unsigned long uninitialized_var(actual_old_val); 725 - int ret; 726 - 727 - while (1) { 728 - int need_write = 1; 729 - unsigned long cleared_bd_entry = 0; 730 - 731 - pagefault_disable(); 732 - ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, 733 - bd_entry, expected_old_val, cleared_bd_entry); 734 - pagefault_enable(); 735 - if (!ret) 736 - break; 737 - if (ret == -EFAULT) 738 - ret = mpx_resolve_fault(bd_entry, need_write); 739 - /* 740 - * If we could not resolve the fault, consider it 741 - * userspace's fault and error out. 742 - */ 743 - if (ret) 744 - return ret; 745 - } 746 - /* 747 - * The cmpxchg was performed, check the results. 748 - */ 749 - if (actual_old_val != expected_old_val) { 750 - /* 751 - * Someone else raced with us to unmap the table. 752 - * That is OK, since we were both trying to do 753 - * the same thing. Declare success. 754 - */ 755 - if (!actual_old_val) 756 - return 0; 757 - /* 758 - * Something messed with the bounds directory 759 - * entry. We hold mmap_sem for read or write 760 - * here, so it could not be a _new_ bounds table 761 - * that someone just allocated. Something is 762 - * wrong, so pass up the error and SIGSEGV. 763 - */ 764 - return -EINVAL; 765 - } 766 - /* 767 - * Note, we are likely being called under do_munmap() already. To 768 - * avoid recursion, do_munmap() will check whether it comes 769 - * from one bounds table through VM_MPX flag. 770 - */ 771 - return do_munmap(mm, bt_addr, mpx_bt_size_bytes(mm), NULL); 772 - } 773 - 774 - static int try_unmap_single_bt(struct mm_struct *mm, 775 - unsigned long start, unsigned long end) 776 - { 777 - struct vm_area_struct *next; 778 - struct vm_area_struct *prev; 779 - /* 780 - * "bta" == Bounds Table Area: the area controlled by the 781 - * bounds table that we are unmapping. 782 - */ 783 - unsigned long bta_start_vaddr = start & ~(bd_entry_virt_space(mm)-1); 784 - unsigned long bta_end_vaddr = bta_start_vaddr + bd_entry_virt_space(mm); 785 - unsigned long uninitialized_var(bt_addr); 786 - void __user *bde_vaddr; 787 - int ret; 788 - /* 789 - * We already unlinked the VMAs from the mm's rbtree so 'start' 790 - * is guaranteed to be in a hole. This gets us the first VMA 791 - * before the hole in to 'prev' and the next VMA after the hole 792 - * in to 'next'. 793 - */ 794 - next = find_vma_prev(mm, start, &prev); 795 - /* 796 - * Do not count other MPX bounds table VMAs as neighbors. 797 - * Although theoretically possible, we do not allow bounds 798 - * tables for bounds tables so our heads do not explode. 799 - * If we count them as neighbors here, we may end up with 800 - * lots of tables even though we have no actual table 801 - * entries in use. 802 - */ 803 - while (next && (next->vm_flags & VM_MPX)) 804 - next = next->vm_next; 805 - while (prev && (prev->vm_flags & VM_MPX)) 806 - prev = prev->vm_prev; 807 - /* 808 - * We know 'start' and 'end' lie within an area controlled 809 - * by a single bounds table. See if there are any other 810 - * VMAs controlled by that bounds table. If there are not 811 - * then we can "expand" the are we are unmapping to possibly 812 - * cover the entire table. 813 - */ 814 - next = find_vma_prev(mm, start, &prev); 815 - if ((!prev || prev->vm_end <= bta_start_vaddr) && 816 - (!next || next->vm_start >= bta_end_vaddr)) { 817 - /* 818 - * No neighbor VMAs controlled by same bounds 819 - * table. Try to unmap the whole thing 820 - */ 821 - start = bta_start_vaddr; 822 - end = bta_end_vaddr; 823 - } 824 - 825 - bde_vaddr = mm->context.bd_addr + mpx_get_bd_entry_offset(mm, start); 826 - ret = get_bt_addr(mm, bde_vaddr, &bt_addr); 827 - /* 828 - * No bounds table there, so nothing to unmap. 829 - */ 830 - if (ret == -ENOENT) { 831 - ret = 0; 832 - return 0; 833 - } 834 - if (ret) 835 - return ret; 836 - /* 837 - * We are unmapping an entire table. Either because the 838 - * unmap that started this whole process was large enough 839 - * to cover an entire table, or that the unmap was small 840 - * but was the area covered by a bounds table. 841 - */ 842 - if ((start == bta_start_vaddr) && 843 - (end == bta_end_vaddr)) 844 - return unmap_entire_bt(mm, bde_vaddr, bt_addr); 845 - return zap_bt_entries_mapping(mm, bt_addr, start, end); 846 - } 847 - 848 - static int mpx_unmap_tables(struct mm_struct *mm, 849 - unsigned long start, unsigned long end) 850 - { 851 - unsigned long one_unmap_start; 852 - trace_mpx_unmap_search(start, end); 853 - 854 - one_unmap_start = start; 855 - while (one_unmap_start < end) { 856 - int ret; 857 - unsigned long next_unmap_start = ALIGN(one_unmap_start+1, 858 - bd_entry_virt_space(mm)); 859 - unsigned long one_unmap_end = end; 860 - /* 861 - * if the end is beyond the current bounds table, 862 - * move it back so we only deal with a single one 863 - * at a time 864 - */ 865 - if (one_unmap_end > next_unmap_start) 866 - one_unmap_end = next_unmap_start; 867 - ret = try_unmap_single_bt(mm, one_unmap_start, one_unmap_end); 868 - if (ret) 869 - return ret; 870 - 871 - one_unmap_start = next_unmap_start; 872 - } 873 - return 0; 874 - } 875 - 876 - /* 877 - * Free unused bounds tables covered in a virtual address region being 878 - * munmap()ed. Assume end > start. 879 - * 880 - * This function will be called by do_munmap(), and the VMAs covering 881 - * the virtual address region start...end have already been split if 882 - * necessary, and the 'vma' is the first vma in this range (start -> end). 883 - */ 884 - void mpx_notify_unmap(struct mm_struct *mm, unsigned long start, 885 - unsigned long end) 886 - { 887 - struct vm_area_struct *vma; 888 - int ret; 889 - 890 - /* 891 - * Refuse to do anything unless userspace has asked 892 - * the kernel to help manage the bounds tables, 893 - */ 894 - if (!kernel_managing_mpx_tables(current->mm)) 895 - return; 896 - /* 897 - * This will look across the entire 'start -> end' range, 898 - * and find all of the non-VM_MPX VMAs. 899 - * 900 - * To avoid recursion, if a VM_MPX vma is found in the range 901 - * (start->end), we will not continue follow-up work. This 902 - * recursion represents having bounds tables for bounds tables, 903 - * which should not occur normally. Being strict about it here 904 - * helps ensure that we do not have an exploitable stack overflow. 905 - */ 906 - vma = find_vma(mm, start); 907 - while (vma && vma->vm_start < end) { 908 - if (vma->vm_flags & VM_MPX) 909 - return; 910 - vma = vma->vm_next; 911 - } 912 - 913 - ret = mpx_unmap_tables(mm, start, end); 914 - if (ret) 915 - force_sig(SIGSEGV); 916 - } 917 - 918 - /* MPX cannot handle addresses above 47 bits yet. */ 919 - unsigned long mpx_unmapped_area_check(unsigned long addr, unsigned long len, 920 - unsigned long flags) 921 - { 922 - if (!kernel_managing_mpx_tables(current->mm)) 923 - return addr; 924 - if (addr + len <= DEFAULT_MAP_WINDOW) 925 - return addr; 926 - if (flags & MAP_FIXED) 927 - return -ENOMEM; 928 - 929 - /* 930 - * Requested len is larger than the whole area we're allowed to map in. 931 - * Resetting hinting address wouldn't do much good -- fail early. 932 - */ 933 - if (len > DEFAULT_MAP_WINDOW) 934 - return -ENOMEM; 935 - 936 - /* Look for unmap area within DEFAULT_MAP_WINDOW */ 937 - return 0; 938 - }