2008-10-23 13:26:29 +08:00
|
|
|
#ifndef _ASM_X86_ASM_H
|
|
|
|
#define _ASM_X86_ASM_H
|
2008-01-30 20:30:06 +08:00
|
|
|
|
2008-06-17 07:08:17 +08:00
|
|
|
#ifdef __ASSEMBLY__
|
|
|
|
# define __ASM_FORM(x) x
|
2013-08-30 03:13:04 +08:00
|
|
|
# define __ASM_FORM_RAW(x) x
|
2011-07-19 20:00:45 +08:00
|
|
|
# define __ASM_FORM_COMMA(x) x,
|
2008-06-17 07:08:17 +08:00
|
|
|
#else
|
|
|
|
# define __ASM_FORM(x) " " #x " "
|
2013-08-30 03:13:04 +08:00
|
|
|
# define __ASM_FORM_RAW(x) #x
|
2011-07-19 20:00:45 +08:00
|
|
|
# define __ASM_FORM_COMMA(x) " " #x ","
|
2008-06-17 07:08:17 +08:00
|
|
|
#endif
|
2008-01-30 20:31:26 +08:00
|
|
|
|
2008-06-17 07:08:17 +08:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
# define __ASM_SEL(a,b) __ASM_FORM(a)
|
2013-08-30 03:13:04 +08:00
|
|
|
# define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(a)
|
2008-01-30 20:30:06 +08:00
|
|
|
#else
|
2008-06-17 07:08:17 +08:00
|
|
|
# define __ASM_SEL(a,b) __ASM_FORM(b)
|
2013-08-30 03:13:04 +08:00
|
|
|
# define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(b)
|
2008-06-17 07:08:17 +08:00
|
|
|
#endif
|
2008-01-30 20:30:06 +08:00
|
|
|
|
2011-07-19 19:59:51 +08:00
|
|
|
#define __ASM_SIZE(inst, ...) __ASM_SEL(inst##l##__VA_ARGS__, \
|
|
|
|
inst##q##__VA_ARGS__)
|
2013-08-30 03:13:04 +08:00
|
|
|
#define __ASM_REG(reg) __ASM_SEL_RAW(e##reg, r##reg)
|
2008-01-30 20:30:06 +08:00
|
|
|
|
2008-06-17 07:08:17 +08:00
|
|
|
#define _ASM_PTR __ASM_SEL(.long, .quad)
|
|
|
|
#define _ASM_ALIGN __ASM_SEL(.balign 4, .balign 8)
|
2008-01-30 20:31:26 +08:00
|
|
|
|
2008-08-19 09:13:33 +08:00
|
|
|
#define _ASM_MOV __ASM_SIZE(mov)
|
2008-06-17 07:08:17 +08:00
|
|
|
#define _ASM_INC __ASM_SIZE(inc)
|
|
|
|
#define _ASM_DEC __ASM_SIZE(dec)
|
|
|
|
#define _ASM_ADD __ASM_SIZE(add)
|
|
|
|
#define _ASM_SUB __ASM_SIZE(sub)
|
|
|
|
#define _ASM_XADD __ASM_SIZE(xadd)
|
2017-05-02 06:47:41 +08:00
|
|
|
#define _ASM_MUL __ASM_SIZE(mul)
|
2008-08-19 09:13:33 +08:00
|
|
|
|
2008-06-24 22:37:57 +08:00
|
|
|
#define _ASM_AX __ASM_REG(ax)
|
2008-06-25 04:36:31 +08:00
|
|
|
#define _ASM_BX __ASM_REG(bx)
|
|
|
|
#define _ASM_CX __ASM_REG(cx)
|
2008-06-24 22:37:57 +08:00
|
|
|
#define _ASM_DX __ASM_REG(dx)
|
2008-08-19 09:13:33 +08:00
|
|
|
#define _ASM_SP __ASM_REG(sp)
|
|
|
|
#define _ASM_BP __ASM_REG(bp)
|
|
|
|
#define _ASM_SI __ASM_REG(si)
|
|
|
|
#define _ASM_DI __ASM_REG(di)
|
2008-01-30 20:30:06 +08:00
|
|
|
|
2016-06-09 03:38:40 +08:00
|
|
|
/*
|
|
|
|
* Macros to generate condition code outputs from inline assembly,
|
|
|
|
* The output operand must be type "bool".
|
|
|
|
*/
|
|
|
|
#ifdef __GCC_ASM_FLAG_OUTPUTS__
|
|
|
|
# define CC_SET(c) "\n\t/* output condition code " #c "*/\n"
|
|
|
|
# define CC_OUT(c) "=@cc" #c
|
|
|
|
#else
|
|
|
|
# define CC_SET(c) "\n\tset" #c " %[_cc_" #c "]\n"
|
|
|
|
# define CC_OUT(c) [_cc_ ## c] "=qm"
|
|
|
|
#endif
|
|
|
|
|
2008-02-04 23:47:56 +08:00
|
|
|
/* Exception table entry */
|
2009-09-01 02:57:20 +08:00
|
|
|
#ifdef __ASSEMBLY__
|
2016-02-18 02:20:12 +08:00
|
|
|
# define _ASM_EXTABLE_HANDLE(from, to, handler) \
|
2012-04-21 07:57:35 +08:00
|
|
|
.pushsection "__ex_table","a" ; \
|
2016-02-18 02:20:12 +08:00
|
|
|
.balign 4 ; \
|
2012-04-21 08:12:48 +08:00
|
|
|
.long (from) - . ; \
|
|
|
|
.long (to) - . ; \
|
2016-02-18 02:20:12 +08:00
|
|
|
.long (handler) - . ; \
|
2012-04-21 07:57:35 +08:00
|
|
|
.popsection
|
|
|
|
|
2016-02-18 02:20:12 +08:00
|
|
|
# define _ASM_EXTABLE(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_default)
|
|
|
|
|
|
|
|
# define _ASM_EXTABLE_FAULT(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_fault)
|
|
|
|
|
|
|
|
# define _ASM_EXTABLE_EX(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_ext)
|
2014-04-17 16:17:05 +08:00
|
|
|
|
locking/refcounts, x86/asm: Implement fast refcount overflow protection
This implements refcount_t overflow protection on x86 without a noticeable
performance impact, though without the fuller checking of REFCOUNT_FULL.
This is done by duplicating the existing atomic_t refcount implementation
but with normally a single instruction added to detect if the refcount
has gone negative (e.g. wrapped past INT_MAX or below zero). When detected,
the handler saturates the refcount_t to INT_MIN / 2. With this overflow
protection, the erroneous reference release that would follow a wrap back
to zero is blocked from happening, avoiding the class of refcount-overflow
use-after-free vulnerabilities entirely.
Only the overflow case of refcounting can be perfectly protected, since
it can be detected and stopped before the reference is freed and left to
be abused by an attacker. There isn't a way to block early decrements,
and while REFCOUNT_FULL stops increment-from-zero cases (which would
be the state _after_ an early decrement and stops potential double-free
conditions), this fast implementation does not, since it would require
the more expensive cmpxchg loops. Since the overflow case is much more
common (e.g. missing a "put" during an error path), this protection
provides real-world protection. For example, the two public refcount
overflow use-after-free exploits published in 2016 would have been
rendered unexploitable:
http://perception-point.io/2016/01/14/analysis-and-exploitation-of-a-linux-kernel-vulnerability-cve-2016-0728/
http://cyseclabs.com/page?n=02012016
This implementation does, however, notice an unchecked decrement to zero
(i.e. caller used refcount_dec() instead of refcount_dec_and_test() and it
resulted in a zero). Decrements under zero are noticed (since they will
have resulted in a negative value), though this only indicates that a
use-after-free may have already happened. Such notifications are likely
avoidable by an attacker that has already exploited a use-after-free
vulnerability, but it's better to have them reported than allow such
conditions to remain universally silent.
On first overflow detection, the refcount value is reset to INT_MIN / 2
(which serves as a saturation value) and a report and stack trace are
produced. When operations detect only negative value results (such as
changing an already saturated value), saturation still happens but no
notification is performed (since the value was already saturated).
On the matter of races, since the entire range beyond INT_MAX but before
0 is negative, every operation at INT_MIN / 2 will trap, leaving no
overflow-only race condition.
As for performance, this implementation adds a single "js" instruction
to the regular execution flow of a copy of the standard atomic_t refcount
operations. (The non-"and_test" refcount_dec() function, which is uncommon
in regular refcount design patterns, has an additional "jz" instruction
to detect reaching exactly zero.) Since this is a forward jump, it is by
default the non-predicted path, which will be reinforced by dynamic branch
prediction. The result is this protection having virtually no measurable
change in performance over standard atomic_t operations. The error path,
located in .text.unlikely, saves the refcount location and then uses UD0
to fire a refcount exception handler, which resets the refcount, handles
reporting, and returns to regular execution. This keeps the changes to
.text size minimal, avoiding return jumps and open-coded calls to the
error reporting routine.
Example assembly comparison:
refcount_inc() before:
.text:
ffffffff81546149: f0 ff 45 f4 lock incl -0xc(%rbp)
refcount_inc() after:
.text:
ffffffff81546149: f0 ff 45 f4 lock incl -0xc(%rbp)
ffffffff8154614d: 0f 88 80 d5 17 00 js ffffffff816c36d3
...
.text.unlikely:
ffffffff816c36d3: 48 8d 4d f4 lea -0xc(%rbp),%rcx
ffffffff816c36d7: 0f ff (bad)
These are the cycle counts comparing a loop of refcount_inc() from 1
to INT_MAX and back down to 0 (via refcount_dec_and_test()), between
unprotected refcount_t (atomic_t), fully protected REFCOUNT_FULL
(refcount_t-full), and this overflow-protected refcount (refcount_t-fast):
2147483646 refcount_inc()s and 2147483647 refcount_dec_and_test()s:
cycles protections
atomic_t 82249267387 none
refcount_t-fast 82211446892 overflow, untested dec-to-zero
refcount_t-full 144814735193 overflow, untested dec-to-zero, inc-from-zero
This code is a modified version of the x86 PAX_REFCOUNT atomic_t
overflow defense from the last public patch of PaX/grsecurity, based
on my understanding of the code. Changes or omissions from the original
code are mine and don't reflect the original grsecurity/PaX code. Thanks
to PaX Team for various suggestions for improvement for repurposing this
code to be a refcount-only protection.
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: David S. Miller <davem@davemloft.net>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Elena Reshetova <elena.reshetova@intel.com>
Cc: Eric Biggers <ebiggers3@gmail.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Hans Liljestrand <ishkamiel@gmail.com>
Cc: James Bottomley <James.Bottomley@hansenpartnership.com>
Cc: Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rik van Riel <riel@redhat.com>
Cc: Serge E. Hallyn <serge@hallyn.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: arozansk@redhat.com
Cc: axboe@kernel.dk
Cc: kernel-hardening@lists.openwall.com
Cc: linux-arch <linux-arch@vger.kernel.org>
Link: http://lkml.kernel.org/r/20170815161924.GA133115@beast
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-08-16 00:19:24 +08:00
|
|
|
# define _ASM_EXTABLE_REFCOUNT(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_refcount)
|
|
|
|
|
2014-04-17 16:17:05 +08:00
|
|
|
# define _ASM_NOKPROBE(entry) \
|
|
|
|
.pushsection "_kprobe_blacklist","aw" ; \
|
|
|
|
_ASM_ALIGN ; \
|
|
|
|
_ASM_PTR (entry); \
|
|
|
|
.popsection
|
2015-05-14 01:42:23 +08:00
|
|
|
|
|
|
|
.macro ALIGN_DESTINATION
|
|
|
|
/* check for bad alignment of destination */
|
|
|
|
movl %edi,%ecx
|
|
|
|
andl $7,%ecx
|
|
|
|
jz 102f /* already aligned */
|
|
|
|
subl $8,%ecx
|
|
|
|
negl %ecx
|
|
|
|
subl %ecx,%edx
|
|
|
|
100: movb (%rsi),%al
|
|
|
|
101: movb %al,(%rdi)
|
|
|
|
incq %rsi
|
|
|
|
incq %rdi
|
|
|
|
decl %ecx
|
|
|
|
jnz 100b
|
|
|
|
102:
|
|
|
|
.section .fixup,"ax"
|
|
|
|
103: addl %ecx,%edx /* ecx is zerorest also */
|
|
|
|
jmp copy_user_handle_tail
|
|
|
|
.previous
|
|
|
|
|
|
|
|
_ASM_EXTABLE(100b,103b)
|
|
|
|
_ASM_EXTABLE(101b,103b)
|
|
|
|
.endm
|
|
|
|
|
2009-09-01 02:57:20 +08:00
|
|
|
#else
|
2016-02-18 02:20:12 +08:00
|
|
|
# define _EXPAND_EXTABLE_HANDLE(x) #x
|
|
|
|
# define _ASM_EXTABLE_HANDLE(from, to, handler) \
|
2012-04-21 07:57:35 +08:00
|
|
|
" .pushsection \"__ex_table\",\"a\"\n" \
|
2016-02-18 02:20:12 +08:00
|
|
|
" .balign 4\n" \
|
2012-04-21 08:12:48 +08:00
|
|
|
" .long (" #from ") - .\n" \
|
|
|
|
" .long (" #to ") - .\n" \
|
2016-02-18 02:20:12 +08:00
|
|
|
" .long (" _EXPAND_EXTABLE_HANDLE(handler) ") - .\n" \
|
2012-04-21 07:57:35 +08:00
|
|
|
" .popsection\n"
|
|
|
|
|
2016-02-18 02:20:12 +08:00
|
|
|
# define _ASM_EXTABLE(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_default)
|
|
|
|
|
|
|
|
# define _ASM_EXTABLE_FAULT(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_fault)
|
|
|
|
|
|
|
|
# define _ASM_EXTABLE_EX(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_ext)
|
|
|
|
|
locking/refcounts, x86/asm: Implement fast refcount overflow protection
This implements refcount_t overflow protection on x86 without a noticeable
performance impact, though without the fuller checking of REFCOUNT_FULL.
This is done by duplicating the existing atomic_t refcount implementation
but with normally a single instruction added to detect if the refcount
has gone negative (e.g. wrapped past INT_MAX or below zero). When detected,
the handler saturates the refcount_t to INT_MIN / 2. With this overflow
protection, the erroneous reference release that would follow a wrap back
to zero is blocked from happening, avoiding the class of refcount-overflow
use-after-free vulnerabilities entirely.
Only the overflow case of refcounting can be perfectly protected, since
it can be detected and stopped before the reference is freed and left to
be abused by an attacker. There isn't a way to block early decrements,
and while REFCOUNT_FULL stops increment-from-zero cases (which would
be the state _after_ an early decrement and stops potential double-free
conditions), this fast implementation does not, since it would require
the more expensive cmpxchg loops. Since the overflow case is much more
common (e.g. missing a "put" during an error path), this protection
provides real-world protection. For example, the two public refcount
overflow use-after-free exploits published in 2016 would have been
rendered unexploitable:
http://perception-point.io/2016/01/14/analysis-and-exploitation-of-a-linux-kernel-vulnerability-cve-2016-0728/
http://cyseclabs.com/page?n=02012016
This implementation does, however, notice an unchecked decrement to zero
(i.e. caller used refcount_dec() instead of refcount_dec_and_test() and it
resulted in a zero). Decrements under zero are noticed (since they will
have resulted in a negative value), though this only indicates that a
use-after-free may have already happened. Such notifications are likely
avoidable by an attacker that has already exploited a use-after-free
vulnerability, but it's better to have them reported than allow such
conditions to remain universally silent.
On first overflow detection, the refcount value is reset to INT_MIN / 2
(which serves as a saturation value) and a report and stack trace are
produced. When operations detect only negative value results (such as
changing an already saturated value), saturation still happens but no
notification is performed (since the value was already saturated).
On the matter of races, since the entire range beyond INT_MAX but before
0 is negative, every operation at INT_MIN / 2 will trap, leaving no
overflow-only race condition.
As for performance, this implementation adds a single "js" instruction
to the regular execution flow of a copy of the standard atomic_t refcount
operations. (The non-"and_test" refcount_dec() function, which is uncommon
in regular refcount design patterns, has an additional "jz" instruction
to detect reaching exactly zero.) Since this is a forward jump, it is by
default the non-predicted path, which will be reinforced by dynamic branch
prediction. The result is this protection having virtually no measurable
change in performance over standard atomic_t operations. The error path,
located in .text.unlikely, saves the refcount location and then uses UD0
to fire a refcount exception handler, which resets the refcount, handles
reporting, and returns to regular execution. This keeps the changes to
.text size minimal, avoiding return jumps and open-coded calls to the
error reporting routine.
Example assembly comparison:
refcount_inc() before:
.text:
ffffffff81546149: f0 ff 45 f4 lock incl -0xc(%rbp)
refcount_inc() after:
.text:
ffffffff81546149: f0 ff 45 f4 lock incl -0xc(%rbp)
ffffffff8154614d: 0f 88 80 d5 17 00 js ffffffff816c36d3
...
.text.unlikely:
ffffffff816c36d3: 48 8d 4d f4 lea -0xc(%rbp),%rcx
ffffffff816c36d7: 0f ff (bad)
These are the cycle counts comparing a loop of refcount_inc() from 1
to INT_MAX and back down to 0 (via refcount_dec_and_test()), between
unprotected refcount_t (atomic_t), fully protected REFCOUNT_FULL
(refcount_t-full), and this overflow-protected refcount (refcount_t-fast):
2147483646 refcount_inc()s and 2147483647 refcount_dec_and_test()s:
cycles protections
atomic_t 82249267387 none
refcount_t-fast 82211446892 overflow, untested dec-to-zero
refcount_t-full 144814735193 overflow, untested dec-to-zero, inc-from-zero
This code is a modified version of the x86 PAX_REFCOUNT atomic_t
overflow defense from the last public patch of PaX/grsecurity, based
on my understanding of the code. Changes or omissions from the original
code are mine and don't reflect the original grsecurity/PaX code. Thanks
to PaX Team for various suggestions for improvement for repurposing this
code to be a refcount-only protection.
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: David S. Miller <davem@davemloft.net>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Elena Reshetova <elena.reshetova@intel.com>
Cc: Eric Biggers <ebiggers3@gmail.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Hans Liljestrand <ishkamiel@gmail.com>
Cc: James Bottomley <James.Bottomley@hansenpartnership.com>
Cc: Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rik van Riel <riel@redhat.com>
Cc: Serge E. Hallyn <serge@hallyn.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: arozansk@redhat.com
Cc: axboe@kernel.dk
Cc: kernel-hardening@lists.openwall.com
Cc: linux-arch <linux-arch@vger.kernel.org>
Link: http://lkml.kernel.org/r/20170815161924.GA133115@beast
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-08-16 00:19:24 +08:00
|
|
|
# define _ASM_EXTABLE_REFCOUNT(from, to) \
|
|
|
|
_ASM_EXTABLE_HANDLE(from, to, ex_handler_refcount)
|
|
|
|
|
2014-04-17 16:17:05 +08:00
|
|
|
/* For C file, we already have NOKPROBE_SYMBOL macro */
|
2009-09-01 02:57:20 +08:00
|
|
|
#endif
|
2008-02-04 23:47:56 +08:00
|
|
|
|
2008-10-23 13:26:29 +08:00
|
|
|
#endif /* _ASM_X86_ASM_H */
|