x86: oops on uaccess faults outside of user addresses
Subsystems (10)
cover x86: oops on uaccess faults outside of user addresses
Currently, __get_user can't trigger an OOPS -- any exception will be caught and return -EFAULT. This means that, if an access_ok check is missing somewhere, then an attacker can freely use it to probe for valid kernel mappings.
This series annotates all of the exception fixups as "catch anything" or "catch valid uaccess faults", and skips the fixup (and hence oopses) if an instruction of the latter type faults for any reason other than a page fault to a user address.
I know of only one bug of this type; it's fixed in patch 5.
Perhaps surprisingly, this seems to survive Trinity fairly well.
Andy Lutomirski (5):
x86: Split "utter crap" pnpbios fixup out of fixup_exception
x86: Clean up extable entry format (and free up a bit)
x86: Annotate _ASM_EXTABLE users to distinguish uaccess from
everything else
x86: Don't fixup uaccess faults to kernel or non-canonical addresses
net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
arch/x86/ia32/ia32entry.S | 4 +- arch/x86/include/asm/asm.h | 75 ++++++++---- arch/x86/include/asm/fpu-internal.h | 6 +- arch/x86/include/asm/futex.h | 8 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/msr.h | 4 +- arch/x86/include/asm/segment.h | 2 +- arch/x86/include/asm/special_insns.h | 2 +- arch/x86/include/asm/traps.h | 6 + arch/x86/include/asm/uaccess.h | 10 +- arch/x86/include/asm/word-at-a-time.h | 2 +- arch/x86/include/asm/xsave.h | 6 +- arch/x86/kernel/entry_32.S | 26 ++--- arch/x86/kernel/entry_64.S | 6 +- arch/x86/kernel/ftrace.c | 4 +- arch/x86/kernel/kprobes/core.c | 4 +- arch/x86/kernel/test_nx.c | 2 +- arch/x86/kernel/test_rodata.c | 2 +- arch/x86/kernel/traps.c | 12 +- arch/x86/kvm/emulate.c | 4 +- arch/x86/lib/checksum_32.S | 4 +- arch/x86/lib/copy_user_64.S | 50 ++++---- arch/x86/lib/copy_user_nocache_64.S | 44 +++---- arch/x86/lib/csum-copy_64.S | 6 +- arch/x86/lib/getuser.S | 12 +- arch/x86/lib/mmx_32.c | 12 +- arch/x86/lib/msr-reg.S | 4 +- arch/x86/lib/putuser.S | 10 +- arch/x86/lib/usercopy_32.c | 212 +++++++++++++++++----------------- arch/x86/lib/usercopy_64.c | 4 +- arch/x86/mm/extable.c | 41 +++---- arch/x86/mm/fault.c | 36 ++++-- arch/x86/mm/init_32.c | 2 +- arch/x86/um/checksum_32.S | 4 +- arch/x86/xen/xen-asm_32.S | 2 +- net/socket.c | 33 +++++- 36 files changed, 375 insertions(+), 288 deletions(-)
Signature · 1 line
1.8.1.4
PATCH 1/5 x86: Split "utter crap" pnpbios fixup out of fixup_exception
It has odd semantics, and it's only applicable (AFAICS) to #GP and #PF.
--- arch/x86/include/asm/traps.h | 6 ++++++ arch/x86/kernel/traps.c | 2 ++ arch/x86/mm/extable.c | 14 -------------- arch/x86/mm/fault.c | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 14 deletions(-)
arch/x86/include/asm/traps.h +6 -0
@@ -89,6 +89,12 @@ asmlinkage void smp_thermal_interrupt(void); asmlinkage void mce_threshold_interrupt(void);#endif +#ifdef CONFIG_PNPBIOS +extern void fixup_pnpbios_exception (struct pt_regs *regs); +#else +static inline void fixup_pnpbios_exception (struct pt_regs *regs) {} +#endif +/* Interrupts/Exceptions */ enum {X86_TRAP_DE =0 ,/* 0, Divide-by-zero */
arch/x86/kernel/traps.c +2 -0
@@ -275,6 +275,8 @@ do_general_protection(struct pt_regs *regs, long error_code) tsk = current;if (!user_mode (regs)) { +fixup_pnpbios_exception (regs);/* Might not return */ +if (fixup_exception (regs)) goto exit;
arch/x86/mm/extable.c +0 -14
@@ -19,20 +19,6 @@ int fixup_exception(struct pt_regs *regs)const struct exception_table_entry *fixup;unsigned long new_ip; -#ifdef CONFIG_PNPBIOS -if (unlikely (SEGMENT_IS_PNP_CODE (regs->cs))) { -extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp; -extern u32 pnp_bios_is_utter_crap; - pnp_bios_is_utter_crap =1 ; -printk (KERN_CRIT "PNPBIOS fault.. attempting recovery.\n" ); - __asm__volatile ( -"movl %0, %%esp\n\t" -"jmp *%1\n\t" - : :"g" (pnp_bios_fault_esp),"g" (pnp_bios_fault_eip)); -panic ("do_trap: can't hit this" ); - } -#endif - fixup =search_exception_tables (regs->ip);if (fixup) { new_ip =ex_fixup_addr (fixup);
arch/x86/mm/fault.c +19 -0
@@ -569,6 +569,23 @@ static int is_f00f_bug(struct pt_regs *regs, unsigned long address)return 0 ; } +#ifdef CONFIG_PNPBIOS +void fixup_pnpbios_exception (struct pt_regs *regs) +{ +if (unlikely (SEGMENT_IS_PNP_CODE (regs->cs))) { +extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp; +extern u32 pnp_bios_is_utter_crap; + pnp_bios_is_utter_crap =1 ; +printk (KERN_CRIT "PNPBIOS fault.. attempting recovery.\n" ); + __asm__volatile ( +"movl %0, %%esp\n\t" +"jmp *%1\n\t" + : :"g" (pnp_bios_fault_esp),"g" (pnp_bios_fault_eip)); +panic ("do_trap: can't hit this" ); + } +} +#endif +static const char nx_warning[] =KERN_CRIT "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n" ;
@@ -636,6 +653,8 @@ no_context(struct pt_regs *regs, unsigned long error_code,unsigned long flags;int sig; +fixup_pnpbios_exception (regs);/* Might not return */ +/* Are we prepared to handle this kernel fault? */ if (fixup_exception (regs)) {if (current_thread_info ()->sig_on_uaccess_error && signal) {
Signature · 1 line
1.8.1.4
PATCH 2/5 x86: Clean up extable entry format (and free up a bit)
In reply to Andy LutomirskiThis adds two bits of fixup class information to a fixup entry, generalizing the uaccess_err hack currently in place.
--- arch/x86/include/asm/asm.h | 70 ++++++++++++++++++++++++++++++---------------- arch/x86/mm/extable.c | 22 +++++++++------ 2 files changed, 60 insertions(+), 32 deletions(-)
arch/x86/include/asm/asm.h +46 -24
@@ -39,34 +39,56 @@#define _ASM_DI __ASM_REG(di)/* Exception table entry */ -#ifdef __ASSEMBLY__ -# define _ASM_EXTABLE (from,to) \ - .pushsection "__ex_table","a" ; \ - .balign 8 ; \ - .long (from) - . ; \ - .long (to) - . ; \ - .popsection -# define _ASM_EXTABLE_EX (from,to) \ - .pushsection "__ex_table","a" ; \ - .balign 8 ; \ - .long (from) - . ; \ - .long (to) - . + 0x7ffffff0 ; \ +/* +* An exception table entry is 64 bits. The first 32 bits are the offset +* from that entry to the potentially faulting instruction. sortextable +* relies on that exact encoding. The second 32 bits encode the fault +* handler address. +* +* We want to stick two extra bits of handler class into the fault handler +* address. All of these are generated by relocations, so we can only +* rely on addition. We therefore emit: +* +* (target - here) + (class) + 0x20000000 +* +* This has the property that the two high bits are the class and the +* rest is easy to decode. +*/ + +/* There are two bits of extable entry class, added to a signed offset. */ +#define _EXTABLE_CLASS_DEFAULT 0/* standard uaccess fixup */ +#define _EXTABLE_CLASS_EX 0x80000000/* uaccess + set uaccess_err */ + +/* +* The biases are the class constants + 0x20000000, as signed integers. +* This can't use ordinary arithmetic -- the assembler isn't that smart. +*/ +#define _EXTABLE_BIAS_DEFAULT 0x20000000 +#define _EXTABLE_BIAS_EX 0x20000000 - 0x80000000 + +#ifdef __ASSEMBLY__ +# define _EXPAND_EXTABLE_BIAS (x) x +# define _ASM_EXTABLE_CLASS (from,to,bias) \ + .pushsection "__ex_table","a" ; \ + .balign 8 ; \ + .long (from) - . ; \ + .long (to) - . + _EXPAND_EXTABLE_BIAS(bias) ; \ .popsection#else -# define _ASM_EXTABLE (from,to) \ - " .pushsection \"__ex_table\",\"a\"\n" \ - " .balign 8\n" \ - " .long (" #from ") - .\n" \ - " .long (" #to ") - .\n" \ - " .popsection\n" - -# define _ASM_EXTABLE_EX (from,to) \ - " .pushsection \"__ex_table\",\"a\"\n" \ - " .balign 8\n" \ - " .long (" #from ") - .\n" \ - " .long (" #to ") - . + 0x7ffffff0\n" \ +# define _EXPAND_EXTABLE_BIAS (x) #x +# define _ASM_EXTABLE_CLASS (from,to,bias) \ + " .pushsection \"__ex_table\",\"a\"\n" \ + " .balign 8\n" \ + " .long (" #from ") - .\n" \ + " .long (" #to ") - . + " _EXPAND_EXTABLE_BIAS(bias) "\n" \ " .popsection\n"#endif +#define _ASM_EXTABLE (from,to) \ + _ASM_EXTABLE_CLASS(from, to, _EXTABLE_BIAS_DEFAULT) + +#define _ASM_EXTABLE_EX (from,to) \ + _ASM_EXTABLE_CLASS(from, to, _EXTABLE_BIAS_EX) +#endif /* _ASM_X86_ASM_H */
arch/x86/mm/extable.c +14 -8
@@ -8,25 +8,34 @@ ex_insn_addr(const struct exception_table_entry *x) {return (unsigned long )&x->insn + x->insn; } + +static inline unsigned int +ex_class (const struct exception_table_entry *x) +{ +return (unsigned int )x->fixup &0xC0000000 ; +} +static inline unsigned long ex_fixup_addr (const struct exception_table_entry *x) { -return (unsigned long )&x->fixup + x->fixup; +long offset = (long )((u32 )x->fixup &0x3fffffff ) - (long )0x20000000 ; +return (unsigned long )&x->fixup + offset; }int fixup_exception (struct pt_regs *regs) {const struct exception_table_entry *fixup;unsigned long new_ip; +unsigned int class; fixup =search_exception_tables (regs->ip);if (fixup) { + class =ex_class (fixup); new_ip =ex_fixup_addr (fixup); -if (fixup->fixup - fixup->insn >=0x7ffffff0 -4 ) { +if (class == _EXTABLE_CLASS_EX) {/* Special hack for uaccess_err */ current_thread_info ()->uaccess_err =1 ; - new_ip -=0x7ffffff0 ; } regs->ip = new_ip;return 1 ;
@@ -39,18 +48,15 @@ int fixup_exception(struct pt_regs *regs)int __init early_fixup_exception (unsigned long *ip) {const struct exception_table_entry *fixup; -unsigned long new_ip; fixup =search_exception_tables (*ip);if (fixup) { - new_ip =ex_fixup_addr (fixup); - -if (fixup->fixup - fixup->insn >=0x7ffffff0 -4 ) { +if (ex_class (fixup) == _EXTABLE_CLASS_EX) {/* uaccess handling not supported during early boot */ return 0 ; } - *ip = new_ip; + *ip =ex_fixup_addr (fixup);return 1 ; }
Signature · 1 line
1.8.1.4
PATCH 3/5 x86: Annotate _ASM_EXTABLE users to distinguish uaccess from everything else
In reply to Andy LutomirskiThe idea is that the kernel can be much more careful fixing up uaccess exceptions -- page faults on user addresses are the only legitimate reason for a uaccess instruction to fault.
---
I'm not 100% sure what's happening in the KVM code. Can someone familiar with it take a look?
arch/x86/ia32/ia32entry.S | 4 +- arch/x86/include/asm/asm.h | 13 ++- arch/x86/include/asm/fpu-internal.h | 6 +- arch/x86/include/asm/futex.h | 8 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/msr.h | 4 +- arch/x86/include/asm/segment.h | 2 +- arch/x86/include/asm/special_insns.h | 2 +- arch/x86/include/asm/uaccess.h | 8 +- arch/x86/include/asm/word-at-a-time.h | 2 +- arch/x86/include/asm/xsave.h | 6 +- arch/x86/kernel/entry_32.S | 26 ++--- arch/x86/kernel/entry_64.S | 6 +- arch/x86/kernel/ftrace.c | 4 +- arch/x86/kernel/test_nx.c | 2 +- arch/x86/kernel/test_rodata.c | 2 +- arch/x86/kvm/emulate.c | 4 +- arch/x86/lib/checksum_32.S | 4 +- arch/x86/lib/copy_user_64.S | 50 ++++---- arch/x86/lib/copy_user_nocache_64.S | 44 +++---- arch/x86/lib/csum-copy_64.S | 6 +- arch/x86/lib/getuser.S | 12 +- arch/x86/lib/mmx_32.c | 12 +- arch/x86/lib/msr-reg.S | 4 +- arch/x86/lib/putuser.S | 10 +- arch/x86/lib/usercopy_32.c | 212 +++++++++++++++++----------------- arch/x86/lib/usercopy_64.c | 4 +- arch/x86/mm/init_32.c | 2 +- arch/x86/um/checksum_32.S | 4 +- arch/x86/xen/xen-asm_32.S | 2 +- 30 files changed, 236 insertions(+), 231 deletions(-)
arch/x86/ia32/ia32entry.S +2 -2
@@ -149,7 +149,7 @@ ENTRY(ia32_sysenter_target) 32bit zero extended */ ASM_STAC 1: movl (%rbp),%ebp - _ASM_EXTABLE(1b,ia32_badarg) + _ASM_EXTABLE_UACCESS(1b,ia32_badarg) ASM_CLAC orl $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP-ARGOFFSET) testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
@@ -306,7 +306,7 @@ ENTRY(ia32_cstar_target) /* hardware stack frame is complete now */ ASM_STAC 1: movl (%r8),%r9d - _ASM_EXTABLE(1b,ia32_badarg) + _ASM_EXTABLE_UACCESS(1b,ia32_badarg) ASM_CLAC orl $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP-ARGOFFSET) testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
arch/x86/include/asm/asm.h +9 -4
@@ -57,14 +57,16 @@*/ /* There are two bits of extable entry class, added to a signed offset. */ -#define _EXTABLE_CLASS_DEFAULT 0/* standard uaccess fixup */ +#define _EXTABLE_CLASS_UACCESS 0/* standard uaccess fixup */ +#define _EXTABLE_CLASS_ANY 0x40000000/* catch any exception */ #define _EXTABLE_CLASS_EX 0x80000000/* uaccess + set uaccess_err */ /* * The biases are the class constants + 0x20000000, as signed integers. * This can't use ordinary arithmetic -- the assembler isn't that smart. */ -#define _EXTABLE_BIAS_DEFAULT 0x20000000 +#define _EXTABLE_BIAS_UACCESS 0x20000000 +#define _EXTABLE_BIAS_ANY 0x20000000 + 0x40000000#define _EXTABLE_BIAS_EX 0x20000000 - 0x80000000#ifdef __ASSEMBLY__
@@ -85,8 +87,11 @@ " .popsection\n"#endif -#define _ASM_EXTABLE (from,to) \ - _ASM_EXTABLE_CLASS(from, to, _EXTABLE_BIAS_DEFAULT) +#define _ASM_EXTABLE_UACCESS (from,to) \ + _ASM_EXTABLE_CLASS(from, to, _EXTABLE_BIAS_UACCESS) + +#define _ASM_EXTABLE_ANY (from,to) \ + _ASM_EXTABLE_CLASS(from, to, _EXTABLE_BIAS_ANY)#define _ASM_EXTABLE_EX (from,to) \ _ASM_EXTABLE_CLASS(from, to, _EXTABLE_BIAS_EX)
arch/x86/include/asm/fpu-internal.h +3 -3
@@ -133,7 +133,7 @@ static inline void sanitize_i387_state(struct task_struct *tsk)"3: movl $-1,%[err]\n" \" jmp 2b\n" \".previous\n" \ - _ASM_EXTABLE(1b ,3b ) \ + _ASM_EXTABLE_UACCESS(1b ,3b ) \ : [err] "=r" (err), output \ : "0"(0), input); \ err; \
@@ -148,7 +148,7 @@ static inline void sanitize_i387_state(struct task_struct *tsk)"3: movl $-1,%[err]\n" \" jmp 2b\n" \".previous\n" \ - _ASM_EXTABLE(1b ,3b ) \ + _ASM_EXTABLE_ANY(1b ,3b ) \ : [err] "=r" (err), output \ : "0"(0), input); \ err; \
@@ -356,7 +356,7 @@ static inline void __drop_fpu(struct task_struct *tsk)/* Ignore delayed exceptions from user space */ asmvolatile ("1: fwait\n" "2:\n" - _ASM_EXTABLE(1b, 2b)); + _ASM_EXTABLE_ANY(1b, 2b));__thread_fpu_end (tsk); } }
arch/x86/include/asm/futex.h +4 -4
@@ -19,7 +19,7 @@"3:\tmov\t%3, %1\n" \"\tjmp\t2b\n" \"\t.previous\n" \ - _ASM_EXTABLE(1b ,3b ) \ + _ASM_EXTABLE_UACCESS(1b ,3b ) \ : "=r" (oldval), "=r" (ret), "+m" (*uaddr) \ : "i" (-EFAULT), "0" (oparg), "1" (0))
@@ -35,8 +35,8 @@"4:\tmov\t%5, %1\n" \"\tjmp\t3b\n" \"\t.previous\n" \ - _ASM_EXTABLE(1b, 4b) \ -_ASM_EXTABLE (2b ,4b ) \ + _ASM_EXTABLE_UACCESS(1b, 4b) \ +_ASM_EXTABLE_UACCESS (2b ,4b ) \ : "=&a" (oldval), "=&r" (ret), \ "+m" (*uaddr), "=&r" (tem) \ : "r" (oparg), "i" (-EFAULT), "1" (0))
@@ -122,7 +122,7 @@ static inline int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,"3:\tmov %3, %0\n" "\tjmp 2b\n" "\t.previous\n" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_UACCESS(1b ,3b ) : "+r" (ret), "=a" (oldval), "+m" (*uaddr) : "i" (-EFAULT), "r" (newval), "1" (oldval) : "memory"
arch/x86/include/asm/kvm_host.h +1 -1
@@ -986,7 +986,7 @@ extern bool kvm_rebooting; __ASM_SIZE(push) " $666b \n\t" \"call kvm_spurious_fault \n\t" \".popsection \n\t" \ - _ASM_EXTABLE(666b, 667b) + _ASM_EXTABLE_ANY(666b, 667b)#define __kvm_handle_fault_on_reboot (insn) \ ____kvm_handle_fault_on_reboot(insn, "")
arch/x86/include/asm/msr.h +2 -2
@@ -75,7 +75,7 @@ static inline unsigned long long native_read_msr_safe(unsigned int msr,".section .fixup,\"ax\"\n\t" "3: mov %[fault],%[err] ; jmp 1b\n\t" ".previous\n\t" - _ASM_EXTABLE(2b ,3b ) + _ASM_EXTABLE_ANY(2b ,3b ) : [err] "=r" (*err), EAX_EDX_RET(val, low, high) : "c" (msr), [fault] "i" (-EIO)); returnEAX_EDX_VAL (val, low, high);
@@ -97,7 +97,7 @@ notrace static inline int native_write_msr_safe(unsigned int msr,".section .fixup,\"ax\"\n\t" "3: mov %[fault],%[err] ; jmp 1b\n\t" ".previous\n\t" - _ASM_EXTABLE(2b ,3b ) + _ASM_EXTABLE_ANY(2b ,3b ) : [err] "=a" (err) : "c" (msr), "0" (low), "d" (high), [fault] "i" (-EIO)
arch/x86/include/asm/segment.h +1 -1
@@ -231,7 +231,7 @@ do { \" jmp 1b \n" \".previous \n" \ \ - _ASM_EXTABLE(1b ,2b ) \ + _ASM_EXTABLE_ANY(1b ,2b ) \ \ : "+r" (__val) : : "memory"); \ } while (0)
arch/x86/include/asm/special_insns.h +1 -1
@@ -69,7 +69,7 @@ static inline unsigned long native_read_cr4_safe(void)#ifdef CONFIG_X86_32 asm volatile("1: mov %%cr4, %0\n" "2:\n" - _ASM_EXTABLE(1b, 2b) + _ASM_EXTABLE_ANY(1b, 2b) : "=r" (val), "=m" (__force_order) : "0" (0));#else val =native_read_cr4 ();
arch/x86/include/asm/uaccess.h +4 -4
@@ -188,8 +188,8 @@ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))"4: movl %3,%0\n" \" jmp 3b\n" \".previous\n" \ - _ASM_EXTABLE(1b, 4b) \ -_ASM_EXTABLE (2b ,4b ) \ + _ASM_EXTABLE_UACCESS(1b, 4b) \ +_ASM_EXTABLE_UACCESS (2b ,4b ) \ : "=r" (err) \ : "A" (x), "r" (addr), "i" (errret), "0" (err))
@@ -352,7 +352,7 @@ do { \" xor" itype" %" rtype"1,%" rtype"1\n" \" jmp 2b\n" \".previous\n" \ - _ASM_EXTABLE(1b ,3b ) \ + _ASM_EXTABLE_UACCESS(1b ,3b ) \ : "=r" (err), ltype(x) \ : "m" (__m(addr)), "i" (errret), "0" (err))
@@ -416,7 +416,7 @@ struct __large_struct { unsigned long buf[100]; };"3: mov %3,%0\n" \" jmp 2b\n" \".previous\n" \ - _ASM_EXTABLE(1b ,3b ) \ + _ASM_EXTABLE_UACCESS(1b ,3b ) \ : "=r"(err) \ : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
arch/x86/include/asm/word-at-a-time.h +1 -1
@@ -94,7 +94,7 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)"shr %%cl,%0\n\t" "jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_ANY(1b ,3b ) :"=&r" (ret),"=&c" (dummy) :"m" (*(unsigned long *)addr), "i" (-sizeof(unsigned long)),
arch/x86/include/asm/xsave.h +3 -3
@@ -50,7 +50,7 @@ static inline int fpu_xrstor_checking(struct xsave_struct *fx)"3: movl $-1,%[err]\n" " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_ANY(1b ,3b ) : [err] "=r" (err) : "D" (fx), "m" (*fx), "a" (-1), "d" (-1), "0" (0) : "memory");
@@ -77,7 +77,7 @@ static inline int xsave_user(struct xsave_struct __user *buf)"3: movl $-1,%[err]\n" " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_UACCESS(1b ,3b ) : [err] "=r" (err) : "D" (buf), "a" (-1), "d" (-1), "0" (0) : "memory");
@@ -98,7 +98,7 @@ static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)"3: movl $-1,%[err]\n" " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_UACCESS(1b ,3b ) : [err] "=r" (err) : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0) : "memory"); /* memory required? */
arch/x86/kernel/entry_32.S +13 -13
@@ -154,7 +154,7 @@ 99: movl $0, (%esp) jmp 98b .popsection - _ASM_EXTABLE(98b,99b) + _ASM_EXTABLE_ANY(98b,99b) .endm .macro PTGS_TO_GS
@@ -165,7 +165,7 @@ 99: movl $0, PT_GS(%esp) jmp 98b .popsection - _ASM_EXTABLE(98b,99b) + _ASM_EXTABLE_ANY(98b,99b) .endm .macro GS_TO_REG reg
@@ -248,9 +248,9 @@ 6: movl $0, (%esp) jmp 3b .popsection - _ASM_EXTABLE(1b,4b) - _ASM_EXTABLE(2b,5b) - _ASM_EXTABLE(3b,6b) + _ASM_EXTABLE_ANY(1b,4b) + _ASM_EXTABLE_ANY(2b,5b) + _ASM_EXTABLE_ANY(3b,6b) POP_GS_EX .endm
@@ -426,7 +426,7 @@ sysenter_past_esp: 1: movl (%ebp),%ebp ASM_CLAC movl %ebp,PT_EBP(%esp) - _ASM_EXTABLE(1b,syscall_fault) + _ASM_EXTABLE_UACCESS(1b,syscall_fault) GET_THREAD_INFO(%ebp)
@@ -494,7 +494,7 @@ sysexit_audit: 2: movl $0,PT_FS(%esp) jmp 1b .popsection - _ASM_EXTABLE(1b,2b) + _ASM_EXTABLE_ANY(1b,2b) PTGS_TO_GS_EX ENDPROC(ia32_sysenter_target)
@@ -550,7 +550,7 @@ ENTRY(iret_exc) pushl $do_iret_error jmp error_code .previous - _ASM_EXTABLE(irq_return,iret_exc) + _ASM_EXTABLE_ANY(irq_return,iret_exc) CFI_RESTORE_STATE ldt_ss:
@@ -849,7 +849,7 @@ END(device_not_available) #ifdef CONFIG_PARAVIRT ENTRY(native_iret) iret - _ASM_EXTABLE(native_iret, iret_exc) + _ASM_EXTABLE_ANY(native_iret, iret_exc) END(native_iret) ENTRY(native_irq_enable_sysexit)
@@ -1040,10 +1040,10 @@ ENTRY(xen_failsafe_callback) movl %eax,16(%esp) jmp 4b .previous - _ASM_EXTABLE(1b,6b) - _ASM_EXTABLE(2b,7b) - _ASM_EXTABLE(3b,8b) - _ASM_EXTABLE(4b,9b) + _ASM_EXTABLE_ANY(1b,6b) + _ASM_EXTABLE_ANY(2b,7b) + _ASM_EXTABLE_ANY(3b,8b) + _ASM_EXTABLE_ANY(4b,9b) ENDPROC(xen_failsafe_callback) BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
arch/x86/kernel/entry_64.S +3 -3
@@ -1056,12 +1056,12 @@ restore_args: irq_return: INTERRUPT_RETURN - _ASM_EXTABLE(irq_return, bad_iret) + _ASM_EXTABLE_ANY(irq_return, bad_iret) #ifdef CONFIG_PARAVIRT ENTRY(native_iret) iretq - _ASM_EXTABLE(native_iret, bad_iret) + _ASM_EXTABLE_ANY(native_iret, bad_iret) #endif .section .fixup,"ax"
@@ -1319,7 +1319,7 @@ gs_change: CFI_ENDPROC END(native_load_gs_index) - _ASM_EXTABLE(gs_change,bad_gs) + _ASM_EXTABLE_ANY(gs_change,bad_gs) .section .fixup,"ax" /* running with kernelgs */ bad_gs:
arch/x86/kernel/ftrace.c +2 -2
@@ -740,8 +740,8 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr," jmp 3b\n" ".previous\n" - _ASM_EXTABLE(1b, 4b) -_ASM_EXTABLE (2b ,4b ) + _ASM_EXTABLE_ANY(1b, 4b) +_ASM_EXTABLE_ANY (2b ,4b ) : [old] "=&r" (old), [faulted] "=r" (faulted) : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
arch/x86/kernel/test_nx.c +1 -1
@@ -92,7 +92,7 @@ static noinline int test_address(void *address)"2: mov %[zero], %[rslt]\n" " ret\n" ".previous\n" - _ASM_EXTABLE(0b ,2b ) + _ASM_EXTABLE_ANY(0b ,2b ) : [rslt] "=r" (result) : [fake_code] "r" (address), [zero] "r" (0UL), "0" (result) );
arch/x86/kernel/test_rodata.c +1 -1
@@ -43,7 +43,7 @@ int rodata_test(void)".section .fixup,\"ax\"\n" "2: jmp 1b\n" ".previous\n" - _ASM_EXTABLE(0b ,2b ) + _ASM_EXTABLE_ANY(0b ,2b ) : [rslt] "=r" (result) : [rodata_test] "r" (&rodata_test_data), [zero] "r" (0UL) );
arch/x86/kvm/emulate.c +2 -2
@@ -548,7 +548,7 @@ FOP_END;"3: movb $1, %4 \n\t" \"jmp 2b \n\t" \".popsection \n\t" \ - _ASM_EXTABLE(1b ,3b ) \ + _ASM_EXTABLE_UACCESS(1b ,3b ) \ : "=m" ((ctxt)->eflags), "=&r" (_tmp), \" +a" (*rax), " +d" (*rdx), " +qm"(_ex) \ : " i" (EFLAGS_MASK), " m" ((ctxt)->src.val)); \
@@ -4479,7 +4479,7 @@ static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)"movb $1, %[fault] \n\t" "jmp 2b \n\t" ".popsection \n\t" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_ANY(1b ,3b ) : [fault]"+qm"(fault)); ctxt->ops->put_fpu (ctxt);
arch/x86/lib/checksum_32.S +2 -2
@@ -283,11 +283,11 @@ unsigned int csum_partial_copy_generic (const char *src, char *dst, #define SRC(y...) \ 9999: y; \ - _ASM_EXTABLE(9999b, 6001f) + _ASM_EXTABLE_UACCESS(9999b, 6001f) #define DST(y...) \ 9999: y; \ - _ASM_EXTABLE(9999b, 6002f) + _ASM_EXTABLE_UACCESS(9999b, 6002f) #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
arch/x86/lib/copy_user_64.S +25 -25
73 lines not on this page · raw message
arch/x86/lib/copy_user_nocache_64.S +22 -22
55 lines not on this page · raw message
arch/x86/lib/csum-copy_64.S +3 -3
@@ -32,17 +32,17 @@ .macro source 10: - _ASM_EXTABLE(10b, .Lbad_source) + _ASM_EXTABLE_UACCESS(10b, .Lbad_source) .endm .macro dest 20: - _ASM_EXTABLE(20b, .Lbad_dest) + _ASM_EXTABLE_UACCESS(20b, .Lbad_dest) .endm .macro ignore L=.Lignore 30: - _ASM_EXTABLE(30b, \L) + _ASM_EXTABLE_UACCESS(30b, \L) .endm
arch/x86/lib/getuser.S +6 -6
@@ -129,12 +129,12 @@ bad_get_user_8: END(bad_get_user_8) #endif - _ASM_EXTABLE(1b,bad_get_user) - _ASM_EXTABLE(2b,bad_get_user) - _ASM_EXTABLE(3b,bad_get_user) + _ASM_EXTABLE_UACCESS(1b,bad_get_user) + _ASM_EXTABLE_UACCESS(2b,bad_get_user) + _ASM_EXTABLE_UACCESS(3b,bad_get_user) #ifdef CONFIG_X86_64 - _ASM_EXTABLE(4b,bad_get_user) + _ASM_EXTABLE_UACCESS(4b,bad_get_user) #else - _ASM_EXTABLE(4b,bad_get_user_8) - _ASM_EXTABLE(5b,bad_get_user_8) + _ASM_EXTABLE_UACCESS(4b,bad_get_user_8) + _ASM_EXTABLE_UACCESS(5b,bad_get_user_8) #endif
arch/x86/lib/mmx_32.c +6 -6
@@ -49,7 +49,7 @@ void *_mmx_memcpy(void *to, const void *from, size_t len) "3: movw $0x1AEB, 1b\n" /* jmp on 26 bytes */ " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b, 3b) + _ASM_EXTABLE_UACCESS(1b, 3b) : : "r" (from));for ( ; i >5 ; i--) {
@@ -75,7 +75,7 @@ void *_mmx_memcpy(void *to, const void *from, size_t len)"3: movw $0x05EB, 1b\n" /* jmp on 5 bytes */ " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_UACCESS(1b ,3b ) : : "r" (from), "r" (to) : "memory"); from +=64 ;
@@ -176,7 +176,7 @@ static void fast_copy_page(void *to, void *from) "3: movw $0x1AEB, 1b\n" /* jmp on 26 bytes */ " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b, 3b) : : "r" (from)); + _ASM_EXTABLE_UACCESS(1b, 3b) : : "r" (from));for (i =0 ; i < (4096 -320 )/64 ; i++) { __asm__ __volatile__ (
@@ -201,7 +201,7 @@ static void fast_copy_page(void *to, void *from) "3: movw $0x05EB, 1b\n" /* jmp on 5 bytes */ " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b, 3b) : : "r" (from), "r" (to) : "memory"); + _ASM_EXTABLE_UACCESS(1b, 3b) : : "r" (from), "r" (to) : "memory"); from +=64 ; to +=64 ;
@@ -294,7 +294,7 @@ static void fast_copy_page(void *to, void *from) "3: movw $0x1AEB, 1b\n" /* jmp on 26 bytes */ " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b, 3b) : : "r" (from)); + _ASM_EXTABLE_UACCESS(1b, 3b) : : "r" (from));for (i =0 ; i <4096 /64 ; i++) { __asm__ __volatile__ (
@@ -319,7 +319,7 @@ static void fast_copy_page(void *to, void *from)"3: movw $0x05EB, 1b\n" /* jmp on 5 bytes */ " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(1b ,3b ) + _ASM_EXTABLE_UACCESS(1b ,3b ) : : "r" (from), "r" (to) : "memory"); from +=64 ;
arch/x86/lib/msr-reg.S +2 -2
@@ -43,7 +43,7 @@ ENTRY(\op\()_safe_regs) movl $-EIO, %r11d jmp 2b - _ASM_EXTABLE(1b, 3b) + _ASM_EXTABLE_ANY(1b, 3b) CFI_ENDPROC ENDPROC(\op\()_safe_regs) .endm
@@ -90,7 +90,7 @@ ENTRY(\op\()_safe_regs) movl $-EIO, 4(%esp) jmp 2b - _ASM_EXTABLE(1b, 3b) + _ASM_EXTABLE_ANY(1b, 3b) CFI_ENDPROC ENDPROC(\op\()_safe_regs) .endm
arch/x86/lib/putuser.S +5 -5
@@ -92,10 +92,10 @@ bad_put_user: EXIT END(bad_put_user) - _ASM_EXTABLE(1b,bad_put_user) - _ASM_EXTABLE(2b,bad_put_user) - _ASM_EXTABLE(3b,bad_put_user) - _ASM_EXTABLE(4b,bad_put_user) + _ASM_EXTABLE_UACCESS(1b,bad_put_user) + _ASM_EXTABLE_UACCESS(2b,bad_put_user) + _ASM_EXTABLE_UACCESS(3b,bad_put_user) + _ASM_EXTABLE_UACCESS(4b,bad_put_user) #ifdef CONFIG_X86_32 - _ASM_EXTABLE(5b,bad_put_user) + _ASM_EXTABLE_UACCESS(5b,bad_put_user) #endif
arch/x86/lib/usercopy_32.c +106 -106
254 lines not on this page · raw message
arch/x86/lib/usercopy_64.c +2 -2
@@ -36,8 +36,8 @@ unsigned long __clear_user(void __user *addr, unsigned long size)"3: lea 0(%[size1],%[size8],8),%[size8]\n" " jmp 2b\n" ".previous\n" - _ASM_EXTABLE(0b,3b) -_ASM_EXTABLE (1b ,2b ) + _ASM_EXTABLE_UACCESS(0b,3b) +_ASM_EXTABLE_UACCESS (1b ,2b ) : [size8] "=&c"(size), [dst] "=&D" (__d0) : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr), [zero] "r" (0UL), [eight] "r" (8UL));
arch/x86/mm/init_32.c +1 -1
@@ -890,7 +890,7 @@ static noinline int do_test_wp_bit(void)"1: movb %1, %0 \n" " xorl %2, %2 \n" "2: \n" - _ASM_EXTABLE(1b ,2b ) + _ASM_EXTABLE_ANY(1b ,2b ) :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)), "=q" (tmp_reg), "=r" (flag)
arch/x86/um/checksum_32.S +2 -2
@@ -233,11 +233,11 @@ unsigned int csum_partial_copy_generic (const char *src, char *dst, #define SRC(y...) \ 9999: y; \ - _ASM_EXTABLE(9999b, 6001f) + _ASM_EXTABLE_UACCESS(9999b, 6001f) #define DST(y...) \ 9999: y; \ - _ASM_EXTABLE(9999b, 6002f) + _ASM_EXTABLE_UACCESS(9999b, 6002f) .align 4
arch/x86/xen/xen-asm_32.S +1 -1
@@ -138,7 +138,7 @@ iret_restore_end: 1: iret xen_iret_end_crit: - _ASM_EXTABLE(1b, iret_exc) + _ASM_EXTABLE_ANY(1b, iret_exc) hyper_iret: /* put this out of line since its very rarely used */
Signature · 1 line
1.8.1.4
PATCH 4/5 x86: Don't fixup uaccess faults to kernel or non-canonical addresses
In reply to Andy LutomirskiThese don't inherently cause crashes, but they're bugs and, if there's one triggerable from userspace, it can be used to probe KASLR.
---
I don't know what the kprobes code is doing, so I made the conservative change. This can probably be improved.
Kprobes people: what does this code do?
arch/x86/include/asm/uaccess.h | 2 +- arch/x86/kernel/kprobes/core.c | 4 +++- arch/x86/kernel/traps.c | 10 +++++++--- arch/x86/mm/extable.c | 7 +++++-- arch/x86/mm/fault.c | 17 ++++++++++------- 5 files changed, 26 insertions(+), 14 deletions(-)
arch/x86/include/asm/uaccess.h +1 -1
@@ -101,7 +101,7 @@ struct exception_table_entry {#define ARCH_HAS_SORT_EXTABLE #define ARCH_HAS_SEARCH_EXTABLE -extern int fixup_exception (struct pt_regs *regs); +extern int fixup_exception (struct pt_regs *regs,bool uaccess_ok);extern int early_fixup_exception (unsigned long *ip);/*
arch/x86/kernel/kprobes/core.c +3 -1
@@ -927,8 +927,10 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)/* * In case the user-specified fault handler returned * zero, try to fix up. +* +* XXX: This could be much more conservative. */ -if (fixup_exception (regs)) +if (fixup_exception (regs, true))return 1 ;/*
arch/x86/kernel/traps.c +7 -3
@@ -124,7 +124,7 @@ do_trap_no_signal(struct task_struct *tsk, int trapnr, char *str, }#endif if (!user_mode (regs)) { -if (!fixup_exception (regs)) { +if (!fixup_exception (regs, false)) { tsk->thread.error_code = error_code; tsk->thread.trap_nr = trapnr;die (str, regs, error_code);
@@ -277,7 +277,11 @@ do_general_protection(struct pt_regs *regs, long error_code)if (!user_mode (regs)) {fixup_pnpbios_exception (regs);/* Might not return */ -if (fixup_exception (regs)) +/* +* This could be a non-canonical address in uaccess. If so, +* this is bad. +*/ +if (fixup_exception (regs, false)) goto exit; tsk->thread.error_code = error_code;
@@ -491,7 +495,7 @@ void math_error(struct pt_regs *regs, int error_code, int trapnr)if (!user_mode_vm (regs)) { -if (!fixup_exception (regs)) { +if (!fixup_exception (regs, false)) { task->thread.error_code = error_code; task->thread.trap_nr = trapnr;die (str, regs, error_code);
arch/x86/mm/extable.c +5 -2
@@ -22,7 +22,7 @@ ex_fixup_addr(const struct exception_table_entry *x)return (unsigned long )&x->fixup + offset; } -int fixup_exception (struct pt_regs *regs) +int fixup_exception (struct pt_regs *regs,bool uaccess_ok) {const struct exception_table_entry *fixup;unsigned long new_ip;
@@ -33,6 +33,9 @@ int fixup_exception(struct pt_regs *regs) class =ex_class (fixup); new_ip =ex_fixup_addr (fixup); +if (class != _EXTABLE_CLASS_ANY && !uaccess_ok) +return 0 ; +if (class == _EXTABLE_CLASS_EX) {/* Special hack for uaccess_err */ current_thread_info ()->uaccess_err =1 ;
@@ -51,7 +54,7 @@ int __init early_fixup_exception(unsigned long *ip) fixup =search_exception_tables (*ip);if (fixup) { -if (ex_class (fixup) == _EXTABLE_CLASS_EX) { +if (ex_class (fixup) != _EXTABLE_CLASS_ANY) {/* uaccess handling not supported during early boot */ return 0 ; }
arch/x86/mm/fault.c +10 -7
@@ -644,6 +644,11 @@ pgtable_bad(struct pt_regs *regs, unsigned long error_code,oops_end (flags, regs, sig); } +static int fault_in_kernel_space (unsigned long address) +{ +return address >=TASK_SIZE_MAX ; +} + static noinline voidno_context (struct pt_regs *regs,unsigned long error_code,unsigned long address,int signal,int si_code)
@@ -655,8 +660,11 @@ no_context(struct pt_regs *regs, unsigned long error_code,fixup_pnpbios_exception (regs);/* Might not return */ -/* Are we prepared to handle this kernel fault? */ -if (fixup_exception (regs)) { +/* +* Are we prepared to handle this kernel fault? If this is a +* uaccess fault, then the faulting address must be in user space. +*/ +if (fixup_exception (regs, !fault_in_kernel_space (address))) {if (current_thread_info ()->sig_on_uaccess_error && signal) { tsk->thread.trap_nr =X86_TRAP_PF ; tsk->thread.error_code = error_code |PF_USER ;
@@ -1001,11 +1009,6 @@ access_error(unsigned long error_code, struct vm_area_struct *vma)return 0 ; } -static int fault_in_kernel_space (unsigned long address) -{ -return address >=TASK_SIZE_MAX ; -} -static inline bool smap_violation (int error_code,struct pt_regs *regs) {if (error_code &PF_USER )
Signature · 1 line
1.8.1.4
PATCH 5/5 net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
In reply to Andy LutomirskiMSG_CMSG_COMPAT is (AFAIK) not intended to be part of the API -- it's a hack that steals a bit to indicate to other networking code that a compat entry was used. So don't allow it from a non-compat syscall.
This prevents an oops when running this code:
int main()
{
int s;
struct sockaddr_in addr;
struct msghdr *hdr;
char *highpage = mmap((void*)(TASK_SIZE_MAX - 4096), 4096,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (highpage == MAP_FAILED)
err(1, "mmap");
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == -1)
err(1, "socket");
addr.sin_family = AF_INET;
addr.sin_port = htons(1);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) != 0)
err(1, "connect");
void *evil = highpage + 4096 - COMPAT_MSGHDR_SIZE;
printf("Evil address is %p\n", evil);
if (syscall(__NR_sendmmsg, s, evil, 1, MSG_CMSG_COMPAT) < 0)
err(1, "sendmmsg");
return 0;
}--- net/socket.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)
net/socket.c +31 -2
@@ -2097,8 +2097,12 @@ SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, fla {int fput_needed, err;struct msghdr msg_sys; -struct socket *sock =sockfd_lookup_light (fd, &err, &fput_needed); +struct socket *sock; + +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; + sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out;
@@ -2171,6 +2175,8 @@ int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen, SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg, unsigned int, vlen, unsigned int, flags) { +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ;return __sys_sendmmsg (fd, mmsg, vlen, flags); }
@@ -2271,8 +2277,12 @@ SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, {int fput_needed, err;struct msghdr msg_sys; -struct socket *sock =sockfd_lookup_light (fd, &err, &fput_needed); +struct socket *sock; + +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; + sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out;
@@ -2397,6 +2407,9 @@ SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,int datagrams;struct timespec timeout_sys; +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +if (!timeout)return __sys_recvmmsg (fd, mmsg, vlen, flags,NULL );
@@ -2512,15 +2525,31 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) (int __user *)a[4]);break ;case SYS_SENDMSG : +if (a[2 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_sendmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_SENDMMSG : +if (a[3 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_sendmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ]);break ;case SYS_RECVMSG : +if (a[2 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_recvmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_RECVMMSG : +if (a[3 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_recvmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ], (struct timespec __user *)a[4 ]);break ;
Signature · 1 line
1.8.1.4
[PATCH/FIX] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
(edited the title to make sure everyone sees that this fix is standalone.)
----- Forwarded message from Andy Lutomirski <[email protected]> -----
Date: Wed, 22 May 2013 14:07:44 -0700
From: Andy Lutomirski <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], Andy Lutomirski <[email protected]>, [email protected], "David S.
Miller" <[email protected]>
Subject: [PATCH 5/5] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
MSG_CMSG_COMPAT is (AFAIK) not intended to be part of the API -- it's a hack that steals a bit to indicate to other networking code that a compat entry was used. So don't allow it from a non-compat syscall.
This prevents an oops when running this code:
int main()
{
int s;
struct sockaddr_in addr;
struct msghdr *hdr;
char *highpage = mmap((void*)(TASK_SIZE_MAX - 4096), 4096,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (highpage == MAP_FAILED)
err(1, "mmap");
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == -1)
err(1, "socket");
addr.sin_family = AF_INET;
addr.sin_port = htons(1);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) != 0)
err(1, "connect");
void *evil = highpage + 4096 - COMPAT_MSGHDR_SIZE;
printf("Evil address is %p\n", evil);
if (syscall(__NR_sendmmsg, s, evil, 1, MSG_CMSG_COMPAT) < 0)
err(1, "sendmmsg");
return 0;
}--- net/socket.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)
net/socket.c +31 -2
@@ -2097,8 +2097,12 @@ SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, fla {int fput_needed, err;struct msghdr msg_sys; -struct socket *sock =sockfd_lookup_light (fd, &err, &fput_needed); +struct socket *sock; + +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; + sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out;
@@ -2171,6 +2175,8 @@ int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen, SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg, unsigned int, vlen, unsigned int, flags) { +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ;return __sys_sendmmsg (fd, mmsg, vlen, flags); }
@@ -2271,8 +2277,12 @@ SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, {int fput_needed, err;struct msghdr msg_sys; -struct socket *sock =sockfd_lookup_light (fd, &err, &fput_needed); +struct socket *sock; + +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; + sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out;
@@ -2397,6 +2407,9 @@ SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,int datagrams;struct timespec timeout_sys; +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +if (!timeout)return __sys_recvmmsg (fd, mmsg, vlen, flags,NULL );
@@ -2512,15 +2525,31 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) (int __user *)a[4]);break ;case SYS_SENDMSG : +if (a[2 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_sendmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_SENDMMSG : +if (a[3 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_sendmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ]);break ;case SYS_RECVMSG : +if (a[2 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_recvmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_RECVMMSG : +if (a[3 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_recvmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ], (struct timespec __user *)a[4 ]);break ;
Signature · 1 line
1.8.1.4
David Miller
maintainerRe: [PATCH/FIX] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
From: Ingo Molnar <[email protected]>
Date: Tue, 28 May 2013 10:56:00 +0200
Quoted 6 lines
MSG_CMSG_COMPATis (AFAIK) not intended to be part of the API -- it's a hack that steals a bit to indicate to other networking code that a compat entry was used. So don't allow it from a non-compat syscall.This prevents an oops when running this code:
...
Quoted 2 lines
Cc David S. Miller <[email protected]>Signed-off-by Andy Lutomirski <[email protected]>
Applied and queued up for -stable.
Re: [PATCH/FIX] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
Quoted 8 lines · David Miller
From: Ingo Molnar <[email protected]>
Date: Tue, 28 May 2013 10:56:00 +0200Quoted 5 lines
MSG_CMSG_COMPATis (AFAIK) not intended to be part of the API -- it's a hack that steals a bit to indicate to other networking code that a compat entry was used. So don't allow it from a non-compat syscall.
Quoted 6 lines
Quoted 1 line
This prevents an oops when running this code:
...
Quoted 2 lines
Cc David S. Miller <[email protected]>Signed-off-by Andy Lutomirski <[email protected]>Applied and queued up for -stable.
Thanks David!
Ingo
Re: [PATCH 5/5] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
In reply to Andy LutomirskiQuoted 4 lines · Andy Lutomirski
MSG_CMSG_COMPATis (AFAIK) not intended to be part of the API -- it's a hack that steals a bit to indicate to other networking code that a compat entry was used. So don't allow it from a non-compat syscall.
Dave & Linus
This is causing a regression on 64bit powerpc with 32bit usermode. When I hit userspace, udev is broken and I suspect all networking is broken as well.
Can we please revert 1be374a0518a288147c6a7398792583200a67261 upstream?
Found via bisect.
Mikey
Quoted 133 lines
This prevents an oops when running this code:
int
main(){ int s; struct sockaddr_in addr; struct msghdr *hdr; char *highpage = mmap((void*)(TASK_SIZE_MAX - 4096), 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); if (highpage == MAP_FAILED) err(1, "mmap"); s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (s == -1) err(1, "socket"); addr.sin_family = AF_INET; addr.sin_port = htons(1); addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);This is upster if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) != 0) err(1, "connect"); void *evil = highpage + 4096 - COMPAT_MSGHDR_SIZE; printf("Evil address is %p\n", evil); if (syscall(__NR_sendmmsg, s, evil, 1, MSG_CMSG_COMPAT) < 0) err(1, "sendmmsg"); return 0; }Cc David S. Miller <[email protected]>Signed-off-by Andy Lutomirski <[email protected]>--- net/socket.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)net/socket.c +31
-3@@ -2097,8 +2097,12 @@ SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, fla {int fput_needed, err;struct msghdr msg_sys; -struct socket *sock =sockfd_lookup_light (fd, &err, &fput_needed); +struct socket *sock; + +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; + sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out;@@ -2171,6 +2175,8 @@ int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen, SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg, unsigned int, vlen, unsigned int, flags) { +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ;return __sys_sendmmsg (fd, mmsg, vlen, flags); }@@ -2271,8 +2277,12 @@ SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, {int fput_needed, err;struct msghdr msg_sys; -struct socket *sock =sockfd_lookup_light (fd, &err, &fput_needed); +struct socket *sock; + +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; + sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out;@@ -2397,6 +2407,9 @@ SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,int datagrams;struct timespec timeout_sys; +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +if (!timeout)return __sys_recvmmsg (fd, mmsg, vlen, flags,NULL );@@ -2512,15 +2525,31 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) (int __user *)a[4]);break ;case SYS_SENDMSG : +if (a[2 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_sendmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_SENDMMSG : +if (a[3 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_sendmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ]);break ;case SYS_RECVMSG : +if (a[2 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_recvmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_RECVMMSG : +if (a[3 ] &MSG_CMSG_COMPAT ) { + err = -EINVAL ; +break ; + } err =sys_recvmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ], (struct timespec __user *)a[4 ]);break ; --1.8.1.4
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 5/5] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
Hi,
Quoted 8 lines
This is causing a regression on 64bit powerpc with 32bit usermode. When I hit userspace, udev is broken and I suspect all networking is broken as well.
Can we please revert
1be374a0518a288147c6a7398792583200a67261upstream?Found via bisect.
Doesn't this patch break compat_sys_sendmsg and compat_sys_recvmsg? We'd need to move the guts of sys_* into compat_sys_* to fix it.
Anton
Re: [PATCH 5/5] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
Hi Anton,
Quoted 5 lines · On Thu, 6 Jun 2013 13:01:05 +1000 Anton Blanchard <[email protected]> wrote:
Quoted 5 lines
This is causing a regression on 64bit powerpc with 32bit usermode. When I hit userspace, udev is broken and I suspect all networking is broken as well.
Quoted 7 lines
Quoted 4 lines
Can we please revert
1be374a0518a288147c6a7398792583200a67261upstream?Found via bisect.
Doesn't this patch break
compat_sys_sendmsgandcompat_sys_recvmsg? We'd need to move the guts of sys_* into compat_sys_* to fix it.
What you really need is a set of common functions that the sys_ and compat_sys_ functions can call - with the sys_ funtions forbidding MSG_CMSG_COMPAT and the compat_sys_ functions setting it.
Signature · 3 lines
Cheers, Stephen Rothwell [email protected] http://www.canb.auug.org.au/~sfr/
PATCH 1/1 net: Unbreak compat_sys_{send,recv}msg
I broke them in this commit:
commit 1be374a0518a288147c6a7398792583200a67261
Author: Andy Lutomirski <[email protected]>
Date: Wed May 22 14:07:44 2013 -0700
net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
This patch adds __sys_sendmsg and __sys_sendmsg as common helpers that accept MSG_CMSG_COMPAT and blocks MSG_CMSG_COMPAT at the syscall entrypoints. It also reverts some unnecessary checks in sys_socketcall.
Apparently I was suffering from underscore blindness the first time around.
---
I've tested this a little, but I'm not sure I have a great test case.
If the decision is that it's better to leave this for the 3.11, I can send a squashed version. Note that the oops that this fixes is only an oops if the other patches in the original series are applied.
(FWIW, I wasn't sure how to submit this stuff in the first place. I submitted some kernel hardening patches for the x86 tree that converted an access_ok oddity in the net code into an actual oops. In a bit of looking, I couldn't find any failure mode other than a -EFAULT return without the other patches applied. This was clear in the patch series description but not in the change log message for the net part.)
include/linux/socket.h | 3 +++ net/compat.c | 13 +++++++-- net/socket.c | 72 +++++++++++++++++++++++--------------------------- 3 files changed, 47 insertions(+), 41 deletions(-)
include/linux/socket.h +3 -0
@@ -321,6 +321,9 @@ extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);struct timespec ; +/* The __sys_...msg variants allow MSG_CMSG_COMPAT */ +extern long __sys_recvmsg (int fd,struct msghdr __user *msg,unsigned flags); +extern long __sys_sendmsg (int fd,struct msghdr __user *msg,unsigned flags);extern int __sys_recvmmsg (int fd,struct mmsghdr __user *mmsg,unsigned int vlen,unsigned int flags,struct timespec *timeout); extern int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg,
net/compat.c +11 -2
@@ -734,19 +734,25 @@ static unsigned char nas[21] = {asmlinkage long compat_sys_sendmsg (int fd,struct compat_msghdr __user *msg,unsigned int flags) { -return sys_sendmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_sendmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_sendmmsg (int fd,struct compat_mmsghdr __user *mmsg,unsigned int vlen,unsigned int flags) { +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ;return __sys_sendmmsg (fd, (struct mmsghdr __user *)mmsg, vlen, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_recvmsg (int fd,struct compat_msghdr __user *msg,unsigned int flags) { -return sys_recvmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_recvmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_recv (int fd,void __user *buf,size_t len,unsigned int flags)
@@ -768,6 +774,9 @@ asmlinkage long compat_sys_recvmmsg(int fd, struct compat_mmsghdr __user *mmsg,int datagrams;struct timespec ktspec; +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +if (COMPAT_USE_64BIT_TIME ) return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, flags | MSG_CMSG_COMPAT,
net/socket.c +33 -39
163 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH] net: Unbreak compat_sys_{send,recv}msg
Quoted 6 lines · Andy Lutomirski
I broke them in this commit:
commit
1be374a0518a288147c6a7398792583200a67261
Author: Andy Lutomirski <[email protected]>
Date: Wed May 22 14:07:44 2013 -0700
Quoted 7 lines
net: Block
MSG_CMSG_COMPATin send(m)msg and recv(m)msgThis patch adds
__sys_sendmsgand__sys_sendmsgas common helpers that acceptMSG_CMSG_COMPATand blocksMSG_CMSG_COMPATat the syscall entrypoints. It also reverts some unnecessary checks insys_socketcall.Apparently I was suffering from underscore blindness the first time around.
FWIW This fixes the problem I was seeing with powerpc 32bit user on 64 bit kernel.
Mikey
Quoted 255 lines
Signed-off-by Andy Lutomirski <[email protected]>---
I've tested this a little, but I'm not sure I have a great test case.
If the decision is that it's better to leave this for the 3.11, I can send a squashed version. Note that the oops that this fixes is only an oops if the other patches in the original series are applied.
(FWIW, I wasn't sure how to submit this stuff in the first place. I submitted some kernel hardening patches for the x86 tree that converted an
access_okoddity in the net code into an actual oops. In a bit of looking, I couldn't find any failure mode other than a-EFAULTreturn without the other patches applied. This was clear in the patch series description but not in the change log message for the net part.)include/linux/socket.h | 3 +++ net/compat.c | 13 +++++++-- net/socket.c | 72 +++++++++++++++++++++++--------------------------- 3 files changed, 47 insertions(+), 41 deletions(-)include/linux/socket.h +3
-0@@ -321,6 +321,9 @@ extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);struct timespec ; +/* The __sys_...msg variants allow MSG_CMSG_COMPAT */ +extern long __sys_recvmsg (int fd,struct msghdr __user *msg,unsigned flags); +extern long __sys_sendmsg (int fd,struct msghdr __user *msg,unsigned flags);extern int __sys_recvmmsg (int fd,struct mmsghdr __user *mmsg,unsigned int vlen,unsigned int flags,struct timespec *timeout); extern int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg,net/compat.c +11
-2@@ -734,19 +734,25 @@ static unsigned char nas[21] = {asmlinkage long compat_sys_sendmsg (int fd,struct compat_msghdr __user *msg,unsigned int flags) { -return sys_sendmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_sendmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_sendmmsg (int fd,struct compat_mmsghdr __user *mmsg,unsigned int vlen,unsigned int flags) { +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ;return __sys_sendmmsg (fd, (struct mmsghdr __user *)mmsg, vlen, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_recvmsg (int fd,struct compat_msghdr __user *msg,unsigned int flags) { -return sys_recvmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_recvmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_recv (int fd,void __user *buf,size_t len,unsigned int flags)@@ -768,6 +774,9 @@ asmlinkage long compat_sys_recvmmsg(int fd, struct compat_mmsghdr __user *mmsg,int datagrams;struct timespec ktspec; +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +if (COMPAT_USE_64BIT_TIME ) return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, flags | MSG_CMSG_COMPAT,net/socket.c +33
-39@@ -1978,7 +1978,7 @@ struct used_address {unsigned int name_len; }; -static int __sys_sendmsg (struct socket *sock,struct msghdr __user *msg, +static int ___sys_sendmsg (struct socket *sock,struct msghdr __user *msg,struct msghdr *msg_sys,unsigned int flags,struct used_address *used_address) {@@ -2093,26 +2093,30 @@ out:* BSD sendmsg interface */ -SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags) +long __sys_sendmsg (int fd,struct msghdr __user *msg,unsigned flags) {int fput_needed, err;struct msghdr msg_sys;struct socket *sock; -if (flags &MSG_CMSG_COMPAT ) -return -EINVAL ; - sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out; - err =__sys_sendmsg (sock, msg, &msg_sys, flags,NULL ); + err =___sys_sendmsg (sock, msg, &msg_sys, flags,NULL );fput_light (sock->file, fput_needed); out:return err; } +SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags) +{ +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_sendmsg (fd, msg, flags); +} +/* * Linux sendmmsg interface */ @@ -2143,15 +2147,16 @@ int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,while (datagrams < vlen) {if (MSG_CMSG_COMPAT & flags) { - err =__sys_sendmsg (sock, (struct msghdr __user *)compat_entry, - &msg_sys, flags, &used_address); + err =___sys_sendmsg (sock, (struct msghdr __user *)compat_entry, + &msg_sys, flags, &used_address);if (err <0 )break ; err =__put_user (err, &compat_entry->msg_len); ++compat_entry; }else { - err =__sys_sendmsg (sock, (struct msghdr __user *)entry, - &msg_sys, flags, &used_address); + err =___sys_sendmsg (sock, + (struct msghdr __user *)entry, + &msg_sys, flags, &used_address);if (err <0 )break ; err =put_user (err, &entry->msg_len);@@ -2180,7 +2185,7 @@ SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,return __sys_sendmmsg (fd, mmsg, vlen, flags); } -static int __sys_recvmsg (struct socket *sock,struct msghdr __user *msg, +static int ___sys_recvmsg (struct socket *sock,struct msghdr __user *msg,struct msghdr *msg_sys,unsigned int flags,int nosec) { struct compat_msghdr __user *msg_compat =@@ -2272,27 +2277,31 @@ out:* BSD recvmsg interface */ -SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, - unsigned int, flags) +long __sys_recvmsg (int fd,struct msghdr __user *msg,unsigned flags) {int fput_needed, err;struct msghdr msg_sys;struct socket *sock; -if (flags &MSG_CMSG_COMPAT ) -return -EINVAL ; - sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out; - err =__sys_recvmsg (sock, msg, &msg_sys, flags,0 ); + err =___sys_recvmsg (sock, msg, &msg_sys, flags,0 );fput_light (sock->file, fput_needed); out:return err; } +SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, + unsigned int, flags) +{ +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_recvmsg (fd, msg, flags); +} +/* * Linux recvmmsg interface */ @@ -2330,17 +2339,18 @@ int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,* No need to ask LSM for more than the first datagram. */ if (MSG_CMSG_COMPAT & flags) { - err =__sys_recvmsg (sock, (struct msghdr __user *)compat_entry, - &msg_sys, flags & ~MSG_WAITFORONE , - datagrams); + err =___sys_recvmsg (sock, (struct msghdr __user *)compat_entry, + &msg_sys, flags & ~MSG_WAITFORONE , + datagrams);if (err <0 )break ; err =__put_user (err, &compat_entry->msg_len); ++compat_entry; }else { - err =__sys_recvmsg (sock, (struct msghdr __user *)entry, - &msg_sys, flags & ~MSG_WAITFORONE , - datagrams); + err =___sys_recvmsg (sock, + (struct msghdr __user *)entry, + &msg_sys, flags & ~MSG_WAITFORONE , + datagrams);if (err <0 )break ; err =put_user (err, &entry->msg_len);@@ -2525,31 +2535,15 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) (int __user *)a[4]);break ;case SYS_SENDMSG : -if (a[2 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_sendmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_SENDMMSG : -if (a[3 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_sendmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ]);break ;case SYS_RECVMSG : -if (a[2 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_recvmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_RECVMMSG : -if (a[3 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_recvmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ], (struct timespec __user *)a[4 ]);break ;Signature · 1 line
1.8.1.4
David Miller
maintainerRe: [PATCH] net: Unbreak compat_sys_{send,recv}msg
In reply to Andy LutomirskiFrom: Andy Lutomirski <[email protected]>
Date: Wed, 5 Jun 2013 22:38:26 -0700
Quoted 8 lines
I broke them in this commit:
commit
1be374a0518a288147c6a7398792583200a67261
Author: Andy Lutomirski <[email protected]>
Date: Wed May 22 14:07:44 2013 -0700net: Block
MSG_CMSG_COMPATin send(m)msg and recv(m)msg
Quoted 7 lines
This patch adds
__sys_sendmsgand__sys_sendmsgas common helpers that acceptMSG_CMSG_COMPATand blocksMSG_CMSG_COMPATat the syscall entrypoints. It also reverts some unnecessary checks insys_socketcall.Apparently I was suffering from underscore blindness the first time around.
Signed-off-by Andy Lutomirski <[email protected]>
Eric, can you test this patch too?
Thanks.
Quoted 258 lines
---
I've tested this a little, but I'm not sure I have a great test case.
If the decision is that it's better to leave this for the 3.11, I can send a squashed version. Note that the oops that this fixes is only an oops if the other patches in the original series are applied.
(FWIW, I wasn't sure how to submit this stuff in the first place. I submitted some kernel hardening patches for the x86 tree that converted an
access_okoddity in the net code into an actual oops. In a bit of looking, I couldn't find any failure mode other than a-EFAULTreturn without the other patches applied. This was clear in the patch series description but not in the change log message for the net part.)include/linux/socket.h | 3 +++ net/compat.c | 13 +++++++-- net/socket.c | 72 +++++++++++++++++++++++--------------------------- 3 files changed, 47 insertions(+), 41 deletions(-)include/linux/socket.h +3
-0@@ -321,6 +321,9 @@ extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);struct timespec ; +/* The __sys_...msg variants allow MSG_CMSG_COMPAT */ +extern long __sys_recvmsg (int fd,struct msghdr __user *msg,unsigned flags); +extern long __sys_sendmsg (int fd,struct msghdr __user *msg,unsigned flags);extern int __sys_recvmmsg (int fd,struct mmsghdr __user *mmsg,unsigned int vlen,unsigned int flags,struct timespec *timeout); extern int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg,net/compat.c +11
-2@@ -734,19 +734,25 @@ static unsigned char nas[21] = {asmlinkage long compat_sys_sendmsg (int fd,struct compat_msghdr __user *msg,unsigned int flags) { -return sys_sendmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_sendmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_sendmmsg (int fd,struct compat_mmsghdr __user *mmsg,unsigned int vlen,unsigned int flags) { +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ;return __sys_sendmmsg (fd, (struct mmsghdr __user *)mmsg, vlen, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_recvmsg (int fd,struct compat_msghdr __user *msg,unsigned int flags) { -return sys_recvmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_recvmsg (fd, (struct msghdr __user *)msg, flags |MSG_CMSG_COMPAT ); }asmlinkage long compat_sys_recv (int fd,void __user *buf,size_t len,unsigned int flags)@@ -768,6 +774,9 @@ asmlinkage long compat_sys_recvmmsg(int fd, struct compat_mmsghdr __user *mmsg,int datagrams;struct timespec ktspec; +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +if (COMPAT_USE_64BIT_TIME ) return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, flags | MSG_CMSG_COMPAT,net/socket.c +33
-39@@ -1978,7 +1978,7 @@ struct used_address {unsigned int name_len; }; -static int __sys_sendmsg (struct socket *sock,struct msghdr __user *msg, +static int ___sys_sendmsg (struct socket *sock,struct msghdr __user *msg,struct msghdr *msg_sys,unsigned int flags,struct used_address *used_address) {@@ -2093,26 +2093,30 @@ out:* BSD sendmsg interface */ -SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags) +long __sys_sendmsg (int fd,struct msghdr __user *msg,unsigned flags) {int fput_needed, err;struct msghdr msg_sys;struct socket *sock; -if (flags &MSG_CMSG_COMPAT ) -return -EINVAL ; - sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out; - err =__sys_sendmsg (sock, msg, &msg_sys, flags,NULL ); + err =___sys_sendmsg (sock, msg, &msg_sys, flags,NULL );fput_light (sock->file, fput_needed); out:return err; } +SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags) +{ +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_sendmsg (fd, msg, flags); +} +/* * Linux sendmmsg interface */ @@ -2143,15 +2147,16 @@ int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,while (datagrams < vlen) {if (MSG_CMSG_COMPAT & flags) { - err =__sys_sendmsg (sock, (struct msghdr __user *)compat_entry, - &msg_sys, flags, &used_address); + err =___sys_sendmsg (sock, (struct msghdr __user *)compat_entry, + &msg_sys, flags, &used_address);if (err <0 )break ; err =__put_user (err, &compat_entry->msg_len); ++compat_entry; }else { - err =__sys_sendmsg (sock, (struct msghdr __user *)entry, - &msg_sys, flags, &used_address); + err =___sys_sendmsg (sock, + (struct msghdr __user *)entry, + &msg_sys, flags, &used_address);if (err <0 )break ; err =put_user (err, &entry->msg_len);@@ -2180,7 +2185,7 @@ SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,return __sys_sendmmsg (fd, mmsg, vlen, flags); } -static int __sys_recvmsg (struct socket *sock,struct msghdr __user *msg, +static int ___sys_recvmsg (struct socket *sock,struct msghdr __user *msg,struct msghdr *msg_sys,unsigned int flags,int nosec) { struct compat_msghdr __user *msg_compat =@@ -2272,27 +2277,31 @@ out:* BSD recvmsg interface */ -SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, - unsigned int, flags) +long __sys_recvmsg (int fd,struct msghdr __user *msg,unsigned flags) {int fput_needed, err;struct msghdr msg_sys;struct socket *sock; -if (flags &MSG_CMSG_COMPAT ) -return -EINVAL ; - sock =sockfd_lookup_light (fd, &err, &fput_needed);if (!sock) goto out; - err =__sys_recvmsg (sock, msg, &msg_sys, flags,0 ); + err =___sys_recvmsg (sock, msg, &msg_sys, flags,0 );fput_light (sock->file, fput_needed); out:return err; } +SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg, + unsigned int, flags) +{ +if (flags &MSG_CMSG_COMPAT ) +return -EINVAL ; +return __sys_recvmsg (fd, msg, flags); +} +/* * Linux recvmmsg interface */ @@ -2330,17 +2339,18 @@ int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,* No need to ask LSM for more than the first datagram. */ if (MSG_CMSG_COMPAT & flags) { - err =__sys_recvmsg (sock, (struct msghdr __user *)compat_entry, - &msg_sys, flags & ~MSG_WAITFORONE , - datagrams); + err =___sys_recvmsg (sock, (struct msghdr __user *)compat_entry, + &msg_sys, flags & ~MSG_WAITFORONE , + datagrams);if (err <0 )break ; err =__put_user (err, &compat_entry->msg_len); ++compat_entry; }else { - err =__sys_recvmsg (sock, (struct msghdr __user *)entry, - &msg_sys, flags & ~MSG_WAITFORONE , - datagrams); + err =___sys_recvmsg (sock, + (struct msghdr __user *)entry, + &msg_sys, flags & ~MSG_WAITFORONE , + datagrams);if (err <0 )break ; err =put_user (err, &entry->msg_len);@@ -2525,31 +2535,15 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) (int __user *)a[4]);break ;case SYS_SENDMSG : -if (a[2 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_sendmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_SENDMMSG : -if (a[3 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_sendmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ]);break ;case SYS_RECVMSG : -if (a[2 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_recvmsg (a0, (struct msghdr __user *)a1, a[2 ]);break ;case SYS_RECVMMSG : -if (a[3 ] &MSG_CMSG_COMPAT ) { - err = -EINVAL ; -break ; - } err =sys_recvmmsg (a0, (struct mmsghdr __user *)a1, a[2 ], a[3 ], (struct timespec __user *)a[4 ]);break ;Signature · 6 lines
1.8.1.4 -- To unsubscribe from this list: send the line "unsubscribe trinity" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Eric Dumazet
Tested-byRe: [PATCH] net: Unbreak compat_sys_{send,recv}msg
Quoted 15 lines · David Miller
From: Andy Lutomirski <[email protected]>
Date: Wed, 5 Jun 2013 22:38:26 -0700Quoted 12 lines
I broke them in this commit:
commit
1be374a0518a288147c6a7398792583200a67261
Author: Andy Lutomirski <[email protected]>
Date: Wed May 22 14:07:44 2013 -0700net: Block
MSG_CMSG_COMPATin send(m)msg and recv(m)msgThis patch adds
__sys_sendmsgand__sys_sendmsgas common helpers that acceptMSG_CMSG_COMPATand blocksMSG_CMSG_COMPATat the syscall entrypoints. It also reverts some unnecessary checks insys_socketcall.
Quoted 5 lines
Quoted 3 lines
Apparently I was suffering from underscore blindness the first time around.
Signed-off-by Andy Lutomirski <[email protected]>Eric, can you test this patch too?
Yes, this fixes the problem as well on x86_64
Thanks !
PS: I had following fuzz while applying on Linus tree :
patching file include/linux/socket.h Hunk #1 succeeded at 320 (offset -1 lines). patching file net/compat.c
patching file net/socket.c
Hunk #1 succeeded at 1956 (offset -22 lines). Hunk #2 succeeded at 2071 (offset -22 lines). Hunk #3 succeeded at 2125 (offset -22 lines). Hunk #4 succeeded at 2163 (offset -22 lines). Hunk #5 succeeded at 2255 (offset -22 lines). Hunk #6 succeeded at 2317 (offset -22 lines). Hunk #7 succeeded at 2515 (offset -20 lines).
David Miller
AppliedRe: [PATCH] net: Unbreak compat_sys_{send,recv}msg
From: Eric Dumazet <[email protected]>
Date: Thu, 06 Jun 2013 06:45:37 -0700
Quoted 18 lines · David Miller
Quoted 17 lines
From: Andy Lutomirski <[email protected]>
Date: Wed, 5 Jun 2013 22:38:26 -0700Quoted 14 lines
I broke them in this commit:
commit
1be374a0518a288147c6a7398792583200a67261
Author: Andy Lutomirski <[email protected]>
Date: Wed May 22 14:07:44 2013 -0700net: Block
MSG_CMSG_COMPATin send(m)msg and recv(m)msgThis patch adds
__sys_sendmsgand__sys_sendmsgas common helpers that acceptMSG_CMSG_COMPATand blocksMSG_CMSG_COMPATat the syscall entrypoints. It also reverts some unnecessary checks insys_socketcall.Apparently I was suffering from underscore blindness the first time around.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Andy Lutomirski <[email protected]>Eric, can you test this patch too?
Yes, this fixes the problem as well on
x86_64Tested-by Eric Dumazet <[email protected]>
Applied, thanks.
Re: [PATCH 5/5] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
In reply to Michael NeulingQuoted 6 lines · Michael Neuling
Quoted 4 lines · Andy Lutomirski
MSG_CMSG_COMPATis (AFAIK) not intended to be part of the API -- it's a hack that steals a bit to indicate to other networking code that a compat entry was used. So don't allow it from a non-compat syscall.
Quoted 8 lines
Dave & Linus
This is causing a regression on 64bit powerpc with 32bit usermode. When I hit userspace, udev is broken and I suspect all networking is broken as well.
Can we please revert
1be374a0518a288147c6a7398792583200a67261upstream?
It seems to also break x86_64, if using 32bit usermode.
David Miller
maintainerRe: [PATCH 5/5] net: Block MSG_CMSG_COMPAT in send(m)msg and recv(m)msg
From: Eric Dumazet <[email protected]>
Date: Wed, 05 Jun 2013 21:35:25 -0700
Quoted 9 lines · Michael Neuling
Quoted 8 lines · Andy Lutomirski
Quoted 4 lines
MSG_CMSG_COMPATis (AFAIK) not intended to be part of the API -- it's a hack that steals a bit to indicate to other networking code that a compat entry was used. So don't allow it from a non-compat syscall.Dave & Linus
Quoted 8 lines
Quoted 6 lines
This is causing a regression on 64bit powerpc with 32bit usermode. When I hit userspace, udev is broken and I suspect all networking is broken as well.
Can we please revert
1be374a0518a288147c6a7398792583200a67261upstream?It seems to also break
x86_64, if using 32bit usermode.
Sorry, I only merged this because Ingo Molnar and others kept beating me over the head about merging this fix.
Linus please revert, and I will not bow to such pressure in the future, I should know better.
David Miller
maintainerPlease don't mix networking and non-networking changes into a patch series.
Submit the networking patch to [email protected]
Thanks.
Quoted 2 lines · David Miller
Please don't mix networking and non-networking changes into a patch series.
As explained in 0/5 the networking fix justifies the first 4 patches. The networking fix is independent of the first 4 patches.
Quoted 1 line
Submit the networking patch to [email protected]
He Cc:-ed the patch to [email protected]. If you agree with it then the fix should probably be picked up ASAP.
Thanks,
Ingo
David Miller
maintainerFrom: Ingo Molnar <[email protected]>
Date: Tue, 28 May 2013 10:25:14 +0200
Quoted 6 lines · David Miller
Quoted 3 lines
Please don't mix networking and non-networking changes into a patch series.
Quoted 7 lines
As explained in 0/5 the networking fix justifies the first 4 patches. The networking fix is independent of the first 4 patches.
Quoted 1 line
Submit the networking patch to [email protected]
He Cc:-ed the patch to [email protected]. If you agree with it then the fix should probably be picked up ASAP.
He submitted it as part of a series which implies that it belongs together with the others.
If it does not, he should submit it seperately.
If they belong together, then the entire series should be CC:'d to netdev so that networking developers can see all of the context and participate in the patches that the networking "enables".
Quoted 12 lines · David Miller
From: Ingo Molnar <[email protected]>
Date: Tue, 28 May 2013 10:25:14 +0200Quoted 9 lines · David Miller
Quoted 3 lines
Please don't mix networking and non-networking changes into a patch series.
As explained in 0/5 the networking fix justifies the first 4 patches. The networking fix is independent of the first 4 patches.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Submit the networking patch to [email protected]
He Cc:-ed the patch to [email protected]. If you agree with it then the fix should probably be picked up ASAP.
He submitted it as part of a series which implies that it belongs together with the others.
And then described it in the 0/5 patch that it's not. It's also visible from the title, the changelog and the diff of the 5/5 patch that it's a separate fix.
So please consider it submitted.
Thanks,
Ingo
Quoted 14 lines · Andy Lutomirski
Currently,
__get_usercan't trigger an OOPS -- any exception will be caught and return-EFAULT. This means that, if anaccess_okcheck is missing somewhere, then an attacker can freely use it to probe for valid kernel mappings.This series annotates all of the exception fixups as "catch anything" or "catch valid uaccess faults", and skips the fixup (and hence oopses) if an instruction of the latter type faults for any reason other than a page fault to a user address.
I know of only one bug of this type; it's fixed in patch 5.
Perhaps surprisingly, this seems to survive Trinity fairly well.
Quoted 7 lines
Andy Lutomirski (5):
x86: Split "utter crap" pnpbios fixup out offixup_exception
x86: Clean up extable entry format (and free up a bit)
x86: Annotate_ASM_EXTABLEusers to distinguish uaccess from
everything else
x86: Don't fixup uaccess faults to kernel or non-canonical addresses
net: BlockMSG_CMSG_COMPATin send(m)msg and recv(m)msg
Patch 5 is (for better or for worse) in -linus now. What's the status of the other four? (They're certainly not 3.10 material.)
--Andy