x86: compat_siginfo fixes and build-time tests
cover x86: compat_siginfo fixes and build-time tests
We have not been updating the x86 compat_siginfo when updating the generic one. These patches fix the issue, and then add a bunch of build-time checks to try to avoid this happening again in the future (suggested by Ingo).
PATCH 1/3 x86, signals: add missing signal_compat code for x86 features
From: Dave Hansen <[email protected]>
The 32-bit siginfo is a different binary format than the 64-bit one. So, when running 32-bit binaries on 64-bit kernels, we have to convert the kernel's 64-bit version to a 32-bit version that userspace can grok.
We've added a few features to siginfo over the past few years and neglected to add them to arch/x86/kernel/signal_compat.c:
1. The si_addr_lsb used in SIGBUS's sent for machine checks
2. The upper/lower bounds for MPX SIGSEGV faults
3. The protection key for pkey faults
I caught this with some protection keys unit tests and realized it affected a few more features.
This was tested only with my protection keys patch that looks for a proper value in si_pkey. I didn't actually test the machine check or MPX code.
---
b/arch/x86/include/asm/compat.h | 11 +++++++++++ b/arch/x86/kernel/signal_compat.c | 15 +++++++++++++++ 2 files changed, 26 insertions(+)
arch/x86/include/asm/compat.h renamed from arch/x86/include/asm/compat.h~add-signal-compat-for-mpx-pkeys +11 -0
@@ -40,6 +40,7 @@ typedef s32 compat_long_t; typedef s64 __attribute__((aligned(4))) compat_s64;typedef u32 compat_uint_t ;typedef u32 compat_ulong_t ; +typedef u32 compat_u32 ; typedef u64 __attribute__((aligned(4))) compat_u64;typedef u32 compat_uptr_t ;
@@ -181,6 +182,16 @@ typedef struct compat_siginfo {/* SIGILL, SIGFPE, SIGSEGV, SIGBUS */ struct {unsigned int _addr;/* faulting insn/memory ref. */ +short int _addr_lsb;/* Valid LSB of the reported address. */ +union { +/* used when si_code=SEGV_BNDERR */ +struct { +compat_uptr_t _lower; +compat_uptr_t _upper; + } _addr_bnd; +/* used when si_code=SEGV_PKUERR */ +compat_u32 _pkey; + }; } _sigfault;/* SIGPOLL */
arch/x86/kernel/signal_compat.c renamed from arch/x86/kernel/signal_compat.c~add-signal-compat-for-mpx-pkeys +15 -0
@@ -32,6 +32,21 @@ int copy_siginfo_to_user32(compat_siginf &to->_sifields._pad[0]);switch (from->si_code >>16 ) {case __SI_FAULT >>16 : +if (from->si_signo ==SIGBUS && + (from->si_code ==BUS_MCEERR_AR || + from->si_code ==BUS_MCEERR_AO )) +put_user_ex (from->si_addr_lsb, &to->si_addr_lsb); + +if (from->si_signo ==SIGSEGV ) { +if (from->si_code ==SEGV_BNDERR ) { +compat_uptr_t lower = (unsigned long )&to->si_lower; +compat_uptr_t upper = (unsigned long )&to->si_upper; +put_user_ex (lower, &to->si_lower); +put_user_ex (upper, &to->si_upper); + } +if (from->si_code ==SEGV_PKUERR ) +put_user_ex (from->si_pkey, &to->si_pkey); + }break ;case __SI_SYS >>16 :put_user_ex (from->si_syscall, &to->si_syscall);
_
PATCH 2/3 x86, siginfo: add build-time checks in siginfo compat code
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
There were at least 3 features added to the __SI_FAULT area of the siginfo struct that did not make it to the compat siginfo:
1. The si_addr_lsb used in SIGBUS's sent for machine checks
2. The upper/lower bounds for MPX SIGSEGV faults
3. The protection key for pkey faults
There was also some turmoil when I was attempting to add the pkey field because it needs to be a fixed size on 32 and 64-bit and not have any alignment constraints.
This patch adds some compile-time checks to the compat code to make it harder to screw this up. Basically, the checks are supposed to trip any time someone changes the siginfo structure. That sounds bad, but it's what we want. If someone changes siginfo, we want them to also be _forced_ to go look at the compat code.
The details are in the comments.
---
b/arch/x86/kernel/signal_compat.c | 93 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+)
arch/x86/kernel/signal_compat.c renamed from arch/x86/kernel/signal_compat.c~x86-siginfo-compat-checks +93 -0
@@ -1,11 +1,104 @@#include <linux/compat.h> #include <linux/uaccess.h> +/* +* The compat_siginfo_t structure and handing code is very easy +* to break in several ways. It must always be updated when new +* updates are made to the main siginfo_t, and +* copy_siginfo_to_user32() must be updated when the +* (arch-independent) copy_siginfo_to_user() is updated. +* +* It is also easy to put a new member in the compat_siginfo_t +* which has implicit alignment which can move internal structure +* alignment around breaking the ABI. This can happen if you, +* for instance, put a plain 64-bit value in there. +*/ +static inline void signal_compat_build_tests (void ) +{ +int _sifields_offset = offsetof(compat_siginfo_t , _sifields); + +/* +* If adding a new si_code, there is probably new data in +* the siginfo. Make sure folks bumping the si_code +* limits also have to look at this code. Make sure any +* new fields are handled in copy_siginfo_to_user32()! +*/ +BUILD_BUG_ON (NSIGILL !=8 ); +BUILD_BUG_ON (NSIGFPE !=8 ); +BUILD_BUG_ON (NSIGSEGV !=4 ); +BUILD_BUG_ON (NSIGBUS !=5 ); +BUILD_BUG_ON (NSIGTRAP !=4 ); +BUILD_BUG_ON (NSIGCHLD !=6 ); +BUILD_BUG_ON (NSIGSYS !=1 ); + +/* This is part of the ABI and can never change in size: */ +BUILD_BUG_ON (sizeof (compat_siginfo_t) !=128 ); +/* +* The offsets of all the (unioned) si_fields are fixed +* in the ABI, of course. Make sure none of them ever +* move and are always at the beginning: +*/ +BUILD_BUG_ON (offsetof(compat_siginfo_t , _sifields) !=3 *sizeof (int )); +#define CHECK_CSI_OFFSET (name) BUILD_BUG_ON(_sifields_offset != offsetof(compat_siginfo_t, _sifields.name)) + +/* +* Ensure that the size of each si_field never changes. +* If it does, it is a sign that the +* copy_siginfo_to_user32() code below needs to updated +* along with the size in the CHECK_SI_SIZE(). +* +* We repeat this check for both the generic and compat +* siginfos. +* +* Note: it is OK for these to grow as long as the whole +* structure stays within the padding size (checked +* above). +*/ +#define CHECK_CSI_SIZE (name, size) BUILD_BUG_ON(size != sizeof(((compat_siginfo_t *)0)->_sifields.name)) +#define CHECK_SI_SIZE (name, size) BUILD_BUG_ON(size != sizeof(((siginfo_t *)0)->_sifields.name)) + +CHECK_CSI_OFFSET (_kill); +CHECK_CSI_SIZE (_kill,2 *sizeof (int )); +CHECK_SI_SIZE (_kill,2 *sizeof (int )); + +CHECK_CSI_OFFSET (_timer); +CHECK_CSI_SIZE (_timer,5 *sizeof (int )); +CHECK_SI_SIZE (_timer,6 *sizeof (int )); + +CHECK_CSI_OFFSET (_rt); +CHECK_CSI_SIZE (_rt,3 *sizeof (int )); +CHECK_SI_SIZE (_rt,4 *sizeof (int )); + +CHECK_CSI_OFFSET (_sigchld); +CHECK_CSI_SIZE (_sigchld,5 *sizeof (int )); +CHECK_SI_SIZE (_sigchld,8 *sizeof (int )); + +CHECK_CSI_OFFSET (_sigchld_x32); +CHECK_CSI_SIZE (_sigchld_x32,7 *sizeof (int )); +/* no _sigchld_x32 in the generic siginfo_t */ + +CHECK_CSI_OFFSET (_sigfault); +CHECK_CSI_SIZE (_sigfault,4 *sizeof (int )); +CHECK_SI_SIZE (_sigfault,8 *sizeof (int )); + +CHECK_CSI_OFFSET (_sigpoll); +CHECK_CSI_SIZE (_sigpoll,2 *sizeof (int )); +CHECK_SI_SIZE (_sigpoll,4 *sizeof (int )); + +CHECK_CSI_OFFSET (_sigsys); +CHECK_CSI_SIZE (_sigsys,3 *sizeof (int )); +CHECK_SI_SIZE (_sigsys,4 *sizeof (int )); + +/* any new si_fields should be added here */ +} +int copy_siginfo_to_user32 (compat_siginfo_t __user *to,const siginfo_t *from) {int err =0 ;bool ia32 =test_thread_flag (TIF_IA32 ); +signal_compat_build_tests (); +if (!access_ok (VERIFY_WRITE , to,sizeof (compat_siginfo_t)))return -EFAULT ;
_
PATCH 3/3 x86, mpx, selftests: add MPX self test
In reply to Dave HansenFrom: Dave Hansen <[email protected]>
I've had this code for a while, but never submitted it upstream. Now that Skylake hardware is out in the wild, folks can actually run this for real. It tests the following:
1. The MPX hardware is enabled by the kernel and doing what it is supposed to
2. The MPX management code is present and enabled in the kernel
3. MPX Signal handling
4. The MPX bounds table population code (on-demand population)
5. The MPX bounds table unmapping code (kernel-initiated freeing when unused)
This has also caught bugs in the XSAVE code because MPX state is saved/restored with XSAVE.
I'm submitting it now because it would have caught the recent issues with the compat_siginfo code not being properly augmented when new siginfo state is added.
---
b/tools/testing/selftests/x86/Makefile | 2 b/tools/testing/selftests/x86/mpx-debug.h | 13 b/tools/testing/selftests/x86/mpx-dig.c | 480 +++++++ b/tools/testing/selftests/x86/mpx-hw.h | 119 + b/tools/testing/selftests/x86/mpx-mini-test.c | 1637 ++++++++++++++++++++++++++ b/tools/testing/selftests/x86/mpx-mm.h | 9 6 files changed, 2259 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/Makefile renamed from tools/testing/selftests/x86/Makefile~mpx-selftest +1 -1
@@ -5,7 +5,7 @@ include ../lib.mk .PHONY: all all_32 all_64 warn_32bit_failure clean TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt ptrace_syscall \ - check_initial_reg_state sigreturn ldt_gdt iopl + check_initial_reg_state sigreturn ldt_gdt iopl mpx-mini-test TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer
tools/testing/selftests/x86/mpx-debug.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef _MPX_DEBUG_H +#define _MPX_DEBUG_H + +#ifndef DEBUG_LEVEL +#define DEBUG_LEVEL 0 +#endif +#define dprintf_level(level, args...) do { if(level <= DEBUG_LEVEL) printf(args); } while(0) +#define dprintf1(args...) dprintf_level(1, args) +#define dprintf2(args...) dprintf_level(2, args) +#define dprintf3(args...) dprintf_level(3, args) +#define dprintf4(args...) dprintf_level(4, args) + +#endif /* _MPX_DEBUG_H */
tools/testing/selftests/x86/mpx-dig.c new file +480 -0
480 lines not on this page · raw message
tools/testing/selftests/x86/mpx-hw.h new file +119 -0
119 lines not on this page · raw message
tools/testing/selftests/x86/mpx-mini-test.c new file +1637 -0
1,637 lines not on this page · raw message
tools/testing/selftests/x86/mpx-mm.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef _MPX_MM_H +#define _MPX_MM_H + +#define PAGE_SIZE 4096 +#define MB (1UL<<20) + +extern long nr_incore (void *ptr,unsigned long size_bytes); + +#endif /* _MPX_MM_H */
_
Re: [PATCH 3/3] x86, mpx, selftests: add MPX self test
These patches look good to me, but it would be nice to get rid of uglies like:
tools/testing/selftests/x86/mpx-dig.c line 35
Quoted 9 lines
tools/testing/selftests/x86/mpx-dig.c new file +9
-0+/* +* Written by Dave Hansen <[email protected]> +* +* run like this: +pid=31390; BDIR="$(cat /proc/$pid/smaps | grep -B1 2097152 | head -1 | awk -F- '{print $1}')"; ./mpx-dig $pid 0x$BDIR + +NOTE: +assumes that the only 2097152-kb VMA is the bounds dir +*/
(weird vertical alignment)
tools/testing/selftests/x86/mpx-dig.c line 44
Quoted 7 lines
tools/testing/selftests/x86/mpx-dig.c new file +7
-0+long nr_incore (void *ptr,unsigned long size_bytes) +{ +int i; +long ret =0 ; +long vec_len = size_bytes /PAGE_SIZE ; +unsigned char *vec =malloc (vec_len); +if (!vec)
(missing newline)
tools/testing/selftests/x86/mpx-dig.c line 47
Quoted 3 lines
tools/testing/selftests/x86/mpx-dig.c new file +3
-0+mpx_dig_abort (); + +int incore_ret =mincore (ptr, size_bytes, vec);
(weird non-kernel-style definition.)
tools/testing/selftests/x86/mpx-dig.c line 60
Quoted 2 lines
tools/testing/selftests/x86/mpx-dig.c new file +2
-0+char buf[100 ]; +int open_proc (int pid,char *file)
(missing newline)
tools/testing/selftests/x86/mpx-dig.c line 63
Quoted 3 lines
tools/testing/selftests/x86/mpx-dig.c new file +3
-0+{ +int fd; +sprintf (&buf[0 ],"/proc/%d/%s" , pid, file);
(ditto, also unsafe looking sprintf())
tools/testing/selftests/x86/mpx-dig.c line 77
Quoted 7 lines
tools/testing/selftests/x86/mpx-dig.c new file +7
-0+void __dave_abort (int line) +{ +perror ("abort" ); +printf ("abort @ %d\n" , line); +mpx_dig_abort (); +} +#define dave_mpx_dig_abort () __dave_abort(__LINE__);
maybe s/dave// ? ;-)
tools/testing/selftests/x86/mpx-dig.c line 206
Quoted 2 lines
tools/testing/selftests/x86/mpx-dig.c new file +2
-0+//This mincore stuff works, but the bounds tables are not +//sparse enough to make it worthwhile
bad comment style.
tools/testing/selftests/x86/mpx-dig.c line 208
Quoted 2 lines
tools/testing/selftests/x86/mpx-dig.c new file +2
-0+unsigned char incore_vec[MPX_BOUNDS_TABLE_SIZE_BYTES /PAGE_SIZE ]; +int incore_ret =mincore (bt_buf,MPX_BOUNDS_TABLE_SIZE_BYTES , &incore_vec[0 ]);
stray whitespace.
tools/testing/selftests/x86/mpx-dig.c line 246
Quoted 2 lines
tools/testing/selftests/x86/mpx-dig.c new file +2
-0+//if (!incore_vec[page_nr]) +// continue;
ugly.
tools/testing/selftests/x86/mpx-dig.c line 263
Quoted 3 lines
tools/testing/selftests/x86/mpx-dig.c new file +3
-0+if (this_bt_entry_for_vaddr >0x00007fffffffffffUL ) { + this_bt_entry_for_vaddr |=0xffff800000000000 ; + }
unnecessary curly braces.
tools/testing/selftests/x86/mpx-dig.c line 334
Quoted 5 lines
tools/testing/selftests/x86/mpx-dig.c new file +5
-0+// Each bounds directory entry controls 1MB of +// virtual address space. This variable is the +// virtual address in the process of the +// beginning of the area controlled by this +// bounds_dir.
ugly.
tools/testing/selftests/x86/mpx-dig.c line 337
Quoted 3 lines
tools/testing/selftests/x86/mpx-dig.c new file +3
-0+unsigned long bd_for_vaddr = bd_index * (1UL <<20 ); +//printf("%s() at bd index: %lx for vaddr: %lx\n", __func__, bd_index, bd_for_vaddr); +//printf("dir entry[%4ld @ %p]\n", bd_index, bounds_dir_global+i);
Ditto.
tools/testing/selftests/x86/mpx-dig.c line 343
Quoted 6 lines
tools/testing/selftests/x86/mpx-dig.c new file +6
-0+int nr_entries =dump_table (bounds_dir_entry, bd_for_vaddr, bounds_dir_global+bd_offset_bytes+i); + total_entries += nr_entries; +continue ; +printf ("dir entry[%4ld @ %p]: 0x%lx %6d entries total this buf: %7d bd_for_vaddrs: 0x%lx -> 0x%lx\n" , + bd_index, buf+i, + bounds_dir_entry, nr_entries, total_entries, bd_for_vaddr, bd_for_vaddr + (1UL <<20 ));
hm?
tools/testing/selftests/x86/mpx-dig.c line 383
Quoted 4 lines
tools/testing/selftests/x86/mpx-dig.c new file +3
-0+// there shouldn't practically be short reads of /proc/$pid/mem + + incore_ret =mincore (dig_bounds_dir_ptr, buffer_size_bytes, &vec[0 ]);
stray space.
tools/testing/selftests/x86/mpx-dig.c line 436
Quoted 2 lines
tools/testing/selftests/x86/mpx-dig.c new file +2
-0+//if (this_entries) +// printf("entries this loop: %d total: %d (bufs skipped: %d)\n", this_entries, total_entries, bufs_skipped);
You know the drill!
tools/testing/selftests/x86/mpx-dig.c line 447
Quoted 5 lines
tools/testing/selftests/x86/mpx-dig.c new file +5
-0+#ifdef MPX_DIG_REMOTE +int main (int argc,char **argv) +{ +int err; +char *c;
hm...
etc. - ad nauseum. Would be nice to tidy this all up a bit.
Thanks,
Ingo