x86, mpx: Support 32-bit binaries on 64-bit kernels
cover x86, mpx: Support 32-bit binaries on 64-bit kernels
The MPX hardware structures differ in layout in 32 and 64-bit mode. A 32-bit binary running on a 64-bit kernel needs the 32-bit structures, so we need code which switches between the two modes.
x86: make is_64bit_mm() widely available
x86: make __VIRTUAL_MASK safe to use on 32 bit x86, mpx: we do not allocate the bounds directory x86, mpx: remove redundant MPX_BNDCFG_ADDR_MASK x86, mpx: Add temporary variable to reduce masking x86, mpx: new directory entry to addr helper x86, mpx: do 32-bit-only cmpxchg for 32-bit apps x86, mpx: support 32bit binaries on 64bit kernel
include/asm/mmu_context.h | 13 ++ include/asm/mpx.h | 66 ++++++------ include/asm/page_types.h | 8 + kernel/uprobes.c | 10 - mm/mpx.c | 241 ++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 255 insertions(+), 83 deletions(-)
PATCH 1/9 x86: make is_64bit_mm() widely available
From: Dave Hansen <[email protected]>
The uprobes code has a nice helper, is_64bit_mm(), that consults both the runtime and compile-time flags for 32-bit support. Instead of reinventing the wheel, pull it in to an x86 header so we can use it for MPX.
I prefer passing the mm around to test_thread_flag(TIF_IA32) beacuse it makes it explicit where the context is coming from. It will also make it easier if we ever need to access the MPX structures from another process.
---
b/arch/x86/include/asm/mmu_context.h | 13 +++++++++++++ b/arch/x86/kernel/uprobes.c | 10 +--------- 2 files changed, 14 insertions(+), 9 deletions(-)
arch/x86/include/asm/mmu_context.h renamed from arch/x86/include/asm/mmu_context.h~x86-make-is_64bit_mm-available +13 -0
@@ -121,6 +121,19 @@ static inline void arch_exit_mmap(structparavirt_arch_exit_mmap (mm); } +#ifdef CONFIG_X86_64 +static inline bool is_64bit_mm (struct mm_struct *mm) +{ +return !config_enabled (CONFIG_IA32_EMULATION ) || + !(mm->context.ia32_compat ==TIF_IA32 ); +} +#else +static inline bool is_64bit_mm (struct mm_struct *mm) +{ +return false; +} +#endif +static inline void arch_bprm_mm_init (struct mm_struct *mm,struct vm_area_struct *vma) {
arch/x86/kernel/uprobes.c renamed from arch/x86/kernel/uprobes.c~x86-make-is_64bit_mm-available +1 -9
@@ -29,6 +29,7 @@#include <linux/kdebug.h> #include <asm/processor.h> #include <asm/insn.h> +#include <asm/mmu_context.h> /* Post-execution fixups. */
@@ -245,11 +246,6 @@ static int uprobe_init_insn(struct arch_ }#ifdef CONFIG_X86_64 -static inline bool is_64bit_mm (struct mm_struct *mm) -{ -return !config_enabled (CONFIG_IA32_EMULATION ) || - !(mm->context.ia32_compat ==TIF_IA32 ); -}/* * If arch_uprobe->insn doesn't use rip-relative addressing, return * immediately. Otherwise, rewrite the instruction so that it accesses
@@ -430,10 +426,6 @@ static void riprel_post_xol(struct arch_ } }#else /* 32-bit: */ -static inline bool is_64bit_mm (struct mm_struct *mm) -{ -return false; -}/* * No RIP-relative addressing on 32-bit */
_
PATCH 2/9 x86: make __VIRTUAL_MASK safe to use on 32 bit
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
We are going to do some calculations in a moment that are based on the size of the virtual address space. __VIRTUAL_MASK is currently unsafe to use on 32-bit since it overflows an unsigned long with its shift.
The current version will emit a warning if used at all on 32-bit kernels.
Add a version which is safe on 32-bit and consequently does not spit out a warning.
---
b/arch/x86/include/asm/page_types.h | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/include/asm/page_types.h renamed from arch/x86/include/asm/page_types.h~x86-make-__VIRTUAL_MASK-safe-to-use-on-32-bit +8 -0
@@ -10,7 +10,15 @@#define PAGE_MASK (~(PAGE_SIZE-1))#define __PHYSICAL_MASK ((phys_addr_t)((1ULL << __PHYSICAL_MASK_SHIFT) - 1)) +#ifdef CONFIG_X86_64 +/* +* This version doesn't work on 32-bit because __VIRTUAL_MASK_SHIFT=32 +* overflows the unsigned long we're trying to shift. +*/ #define __VIRTUAL_MASK ((1UL << __VIRTUAL_MASK_SHIFT) - 1) +#else +#define __VIRTUAL_MASK (~0UL) +#endif /* Cast PAGE_MASK to a signed type so that it is sign-extended if virtual addresses are 32-bits but physical addresses are larger
_
PATCH 3/9 x86, mpx: we do not allocate the bounds directory
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
The comment and code here are confusing. We do not currently allocate the bounds directory in the kernel.
---
b/arch/x86/mm/mpx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~x86-mpx-we-do-not-allocate-the-bounds-directory +2 -2
@@ -47,8 +47,8 @@ static unsigned long mpx_mmap(unsigned lvm_flags_t vm_flags;struct vm_area_struct *vma; -/* Only bounds table and bounds directory can be allocated here */ -if (len !=MPX_BD_SIZE_BYTES && len !=MPX_BT_SIZE_BYTES ) +/* Only bounds table can be allocated here */ +if (len !=MPX_BT_SIZE_BYTES )return -EINVAL ;down_write (&mm->mmap_sem);
_
PATCH 4/9 x86, mpx: remove redundant MPX_BNDCFG_ADDR_MASK
In reply to Dave HansenFrom: Qiaowei Ren <[email protected]>
MPX_BNDCFG_ADDR_MASK is defined two times, so this patch removes redundant one.
---
b/arch/x86/include/asm/mpx.h | 1 - 1 file changed, 1 deletion(-)
arch/x86/include/asm/mpx.h renamed from arch/x86/include/asm/mpx.h~0001-x86-mpx-remove-redundant-MPX_BNDCFG_ADDR_MASK +0 -1
@@ -45,7 +45,6 @@#define MPX_BNDSTA_TAIL 2#define MPX_BNDCFG_TAIL 12#define MPX_BNDSTA_ADDR_MASK (~((1UL<<MPX_BNDSTA_TAIL)-1)) -#define MPX_BNDCFG_ADDR_MASK (~((1UL<<MPX_BNDCFG_TAIL)-1))#define MPX_BT_ADDR_MASK (~((1UL<<MPX_BD_ENTRY_TAIL)-1))#define MPX_BNDCFG_ADDR_MASK (~((1UL<<MPX_BNDCFG_TAIL)-1))
_
PATCH 5/9 x86, mpx: Add temporary variable to reduce masking
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
When we allocate a bounds table, we call mmap(), then add a "valid" bit to the value before storing it in to the bounds directory.
If we fail along the way, we go and mask that valid bit _back_ out. That seems a little silly, and this makes it much more clear when we have a plain address versus an actual table _entry_.
---
b/arch/x86/mm/mpx.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~mpx-remove-unnecessary-masking +4 -3
@@ -427,6 +427,7 @@ static int allocate_bt(long __user *bd_eunsigned long expected_old_val =0 ;unsigned long actual_old_val =0 ;unsigned long bt_addr; +unsigned long bd_new_entry;int ret =0 ;/*
@@ -439,7 +440,7 @@ static int allocate_bt(long __user *bd_e/* * Set the valid flag (kinda like _PAGE_PRESENT in a pte) */ - bt_addr = bt_addr |MPX_BD_ENTRY_VALID_FLAG ; + bd_new_entry = bt_addr |MPX_BD_ENTRY_VALID_FLAG ;/* * Go poke the address of the new bounds table in to the
@@ -453,7 +454,7 @@ static int allocate_bt(long __user *bd_e* of the MPX code that have to pagefault_disable(). */ ret =user_atomic_cmpxchg_inatomic (&actual_old_val, bd_entry, - expected_old_val, bt_addr); + expected_old_val, bd_new_entry);if (ret) goto out_unmap;
@@ -483,7 +484,7 @@ static int allocate_bt(long __user *bd_e }return 0 ; out_unmap: -vm_munmap (bt_addr &MPX_BT_ADDR_MASK ,MPX_BT_SIZE_BYTES ); +vm_munmap (bt_addr,MPX_BT_SIZE_BYTES );return ret; }
_
PATCH 6/9 x86, mpx: new directory entry to addr helper
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
Currently, to get from a bounds directory entry to the virtual address of a bounds table, we simply mask off a few low bits. However, the set of bits we mask off is different for 32 and 64-bit binaries.
This breaks the operation out in to a helper function and also adds a temporary variable to store the result until we are sure we are returning one.
---
b/arch/x86/include/asm/mpx.h | 1 - b/arch/x86/mm/mpx.c | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 8 deletions(-)
arch/x86/include/asm/mpx.h renamed from arch/x86/include/asm/mpx.h~mpx-new-entry-to-addr-helper +0 -1
@@ -45,7 +45,6 @@#define MPX_BNDSTA_TAIL 2#define MPX_BNDCFG_TAIL 12#define MPX_BNDSTA_ADDR_MASK (~((1UL<<MPX_BNDSTA_TAIL)-1)) -#define MPX_BT_ADDR_MASK (~((1UL<<MPX_BD_ENTRY_TAIL)-1))#define MPX_BNDCFG_ADDR_MASK (~((1UL<<MPX_BNDCFG_TAIL)-1))#define MPX_BNDSTA_ERROR_CODE 0x3
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~mpx-new-entry-to-addr-helper +34 -7
84 lines not on this page · raw message
_
PATCH 7/9 x86, mpx: do 32-bit-only cmpxchg for 32-bit apps
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
user_atomic_cmpxchg_inatomic() actually looks at sizeof(*ptr) to figure out how many bytes to copy. If we run it on a 64-bit kernel with a 64-bit pointer, it will copy a 64-bit bounds directory entry. That's fine, except when we have 32-bit programs with 32-bit bounds directory entries and we only *want* 32-bits.
This patch breaks the cmpxchg operation out in to its own function and performs the 32-bit type swizzling in there.
Note, the "64-bit" version of this code _would_ work on a 32-bit-only kernel. The issue this patch addresses is only for when the kernel's 'long' is mismatched from the size of the bounds directory entry of the process we are working on.
The new helper modifies 'actual_old_val' or returns an error. But gcc doesn't know this, so it warns about 'actual_old_val' being unused. Shut it up with an uninitialized_var().
---
b/arch/x86/mm/mpx.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-)
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~mpx-variable-sized-userspace-pokes +36 -5
@@ -417,6 +417,35 @@ int mpx_disable_management(struct task_sreturn 0 ; } +static int mpx_cmpxchg_bd_entry (struct mm_struct *mm, +unsigned long *actual_old_val_ptr,long __user *bd_entry_addr, +unsigned long expected_old_val,unsigned long new_bd_entry) +{ +int ret; +/* +* user_atomic_cmpxchg_inatomic() actually uses sizeof() +* the pointer thatt we pass to it to figure out how much +* data to cmpxchg. We have to be careful here not to +* pass a pointer to a 64-bit data type when we only want +* a 32-bit copy. +*/ +if (is_64bit_mm (mm)) { + ret =user_atomic_cmpxchg_inatomic (actual_old_val_ptr, + bd_entry_addr, expected_old_val, new_bd_entry); + }else { +u32 uninitialized_var (actual_old_val_32 ); +u32 expected_old_val_32 = expected_old_val; +u32 new_bd_entry_32 = new_bd_entry; +u32 __user *bd_entry_32 = (u32 __user *)bd_entry_addr; + ret =user_atomic_cmpxchg_inatomic (&actual_old_val_32, + bd_entry_32, expected_old_val_32, + new_bd_entry_32); +if (!ret) + *actual_old_val_ptr = actual_old_val_32; + } +return ret; +} +/* * With 32-bit mode, MPX_BT_SIZE_BYTES is 4MB, and the size of each * bounds table is 16KB. With 64-bit mode, MPX_BT_SIZE_BYTES is 2GB,
@@ -424,6 +453,7 @@ int mpx_disable_management(struct task_s*/ static int allocate_bt (long __user *bd_entry) { +struct mm_struct *mm = current->mm;unsigned long expected_old_val =0 ;unsigned long actual_old_val =0 ;unsigned long bt_addr;
@@ -453,8 +483,8 @@ static int allocate_bt(long __user *bd_e* mmap_sem at this point, unlike some of the other part * of the MPX code that have to pagefault_disable(). */ - ret =user_atomic_cmpxchg_inatomic (&actual_old_val, bd_entry, - expected_old_val, bd_new_entry); + ret =mpx_cmpxchg_bd_entry (mm, &actual_old_val, bd_entry, + expected_old_val, bd_new_entry);if (ret) goto out_unmap;
@@ -706,15 +736,16 @@ static int unmap_single_bt(struct mm_str long __user *bd_entry, unsigned long bt_addr) {unsigned long expected_old_val = bt_addr |MPX_BD_ENTRY_VALID_FLAG ; -unsigned long actual_old_val =0 ; +unsigned long uninitialized_var (actual_old_val );int ret;while (1 ) {int need_write =1 ; +unsigned long cleared_bd_entry =0 ;pagefault_disable (); - ret =user_atomic_cmpxchg_inatomic (&actual_old_val, bd_entry, - expected_old_val,0 ); + ret =mpx_cmpxchg_bd_entry (mm, &actual_old_val, + bd_entry, bt_addr, cleared_bd_entry);pagefault_enable ();if (!ret)break ;
_
PATCH 8/9 x86, mpx: support 32bit binaries on 64bit kernel
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
Right now, the kernel can only switch between 64-bit and 32-bit binaries at compile time. This patch adds support for 32-bit binaries on 64-bit kernels when we support ia32 emulation.
We essentially choose which set of table sizes to use when doing arithmetic for the bounds table calculations.
This also uses a different approach for calculating the table indexes than before. I think the new one makes it much more clear what is going on, and allows us to share more code between the 32 and 64-bit cases.
---
b/arch/x86/include/asm/mpx.h | 68 +++++++++---------- b/arch/x86/mm/mpx.c | 150 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 163 insertions(+), 55 deletions(-)
arch/x86/include/asm/mpx.h renamed from arch/x86/include/asm/mpx.h~0002-x86-mpx-support-32bit-binaries-on-64bit-kernel +34 -34
@@ -13,49 +13,49 @@#define MPX_BNDCFG_ENABLE_FLAG 0x1#define MPX_BD_ENTRY_VALID_FLAG 0x1 -#ifdef CONFIG_X86_64 - -/* upper 28 bits [47:20] of the virtual address in 64-bit used to -* index into bounds directory (BD). +/* +* The upper 28 bits [47:20] of the virtual address in 64-bit +* are used to index into bounds directory (BD). +* +* The directory is 2G (2^31) in size, and with 8-byte entries +* it has 2^28 entries. */ -#define MPX_BD_ENTRY_OFFSET 28 -#define MPX_BD_ENTRY_SHIFT 3 -/* bits [19:3] of the virtual address in 64-bit used to index into -* bounds table (BT). +#define MPX_BD_SIZE_BYTES_64 (1UL<<31) +/* An entry is a long, so 8 bytes and a shift of 3 */ +#define MPX_BD_ENTRY_BYTES_64 8 +#define MPX_BD_NR_ENTRIES_64 (MPX_BD_SIZE_BYTES_64/MPX_BD_ENTRY_BYTES_64) + +/* +* The 32-bit directory is 4MB (2^22) in size, and with 4-byte +* entries it has 2^20 entries. */ -#define MPX_BT_ENTRY_OFFSET 17 -#define MPX_BT_ENTRY_SHIFT 5 -#define MPX_IGN_BITS 3 -#define MPX_BD_ENTRY_TAIL 3 - -#else - -#define MPX_BD_ENTRY_OFFSET 20 -#define MPX_BD_ENTRY_SHIFT 2 -#define MPX_BT_ENTRY_OFFSET 10 -#define MPX_BT_ENTRY_SHIFT 4 -#define MPX_IGN_BITS 2 -#define MPX_BD_ENTRY_TAIL 2 - -#endif - -#define MPX_BD_SIZE_BYTES (1UL<<(MPX_BD_ENTRY_OFFSET+MPX_BD_ENTRY_SHIFT)) -#define MPX_BT_SIZE_BYTES (1UL<<(MPX_BT_ENTRY_OFFSET+MPX_BT_ENTRY_SHIFT)) +#define MPX_BD_SIZE_BYTES_32 (1UL<<22) +/* An entry is a long, so 4 bytes and a shift of 2 */ +#define MPX_BD_ENTRY_BYTES_32 4 +#define MPX_BD_NR_ENTRIES_32 (MPX_BD_SIZE_BYTES_32/MPX_BD_ENTRY_BYTES_32) + +/* +* A 64-bit table is 4MB total in size, and an entry is +* 4 64-bit pointers in size. +*/ +#define MPX_BT_SIZE_BYTES_64 (1UL<<22) +#define MPX_BT_ENTRY_BYTES_64 32 +#define MPX_BT_NR_ENTRIES_64 (MPX_BD_SIZE_BYTES_64/MPX_BD_ENTRY_BYTES_64) + +/* +* A 32-bit table is 16kB total in size, and an entry is +* 4 32-bit pointers in size. +*/ +#define MPX_BT_SIZE_BYTES_32 (1UL<<14) +#define MPX_BT_ENTRY_BYTES_32 16 +#define MPX_BT_NR_ENTRIES_32 (MPX_BD_SIZE_BYTES_32/MPX_BD_ENTRY_BYTES_32)#define MPX_BNDSTA_TAIL 2#define MPX_BNDCFG_TAIL 12#define MPX_BNDSTA_ADDR_MASK (~((1UL<<MPX_BNDSTA_TAIL)-1)) -#define MPX_BNDCFG_ADDR_MASK (~((1UL<<MPX_BNDCFG_TAIL)-1))#define MPX_BNDSTA_ERROR_CODE 0x3 -#define MPX_BD_ENTRY_MASK ((1<<MPX_BD_ENTRY_OFFSET)-1) -#define MPX_BT_ENTRY_MASK ((1<<MPX_BT_ENTRY_OFFSET)-1) -#define MPX_GET_BD_ENTRY_OFFSET (addr) ((((addr)>>(MPX_BT_ENTRY_OFFSET+ \ - MPX_IGN_BITS)) & MPX_BD_ENTRY_MASK) << MPX_BD_ENTRY_SHIFT) -#define MPX_GET_BT_ENTRY_OFFSET (addr) ((((addr)>>MPX_IGN_BITS) & \ - MPX_BT_ENTRY_MASK) << MPX_BT_ENTRY_SHIFT) -#ifdef CONFIG_X86_INTEL_MPX siginfo_t *mpx_generate_siginfo (struct pt_regs *regs,struct xsave_struct *xsave_buf);
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~0002-x86-mpx-support-32bit-binaries-on-64bit-kernel +129 -21
241 lines not on this page · raw message
_
PATCH 9/9 x86, mpx: allow mixed binaries again
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
We explicitly disable allowing 32-bit binaries to enable MPX on 64-bit kernels. Re-allow that.
---
b/arch/x86/mm/mpx.c | 6 ------ 1 file changed, 6 deletions(-)
arch/x86/mm/mpx.c renamed from arch/x86/mm/mpx.c~x86-mpx-allow-mixed-binaries-again +0 -6
@@ -365,12 +365,6 @@ static __user void *task_get_bounds_dir(return MPX_INVALID_BOUNDS_DIR ;/* -* 32-bit binaries on 64-bit kernels are currently -* unsupported. -*/ -if (IS_ENABLED (CONFIG_X86_64 ) &&test_thread_flag (TIF_IA32 )) -return MPX_INVALID_BOUNDS_DIR ; -/* * The bounds directory pointer is stored in a register * only accessible if we first do an xsave. */
_