diff options
author | Dan Williams <dan.j.williams@intel.com> | 2018-01-29 20:02:28 -0500 |
---|---|---|
committer | Thomas Gleixner <tglx@linutronix.de> | 2018-01-30 15:54:29 -0500 |
commit | babdde2698d482b6c0de1eab4f697cf5856c5859 (patch) | |
tree | 54cd8df04b47db35a7c707638e1d7d3cad1f44e1 /arch/x86/include | |
parent | f3804203306e098dae9ca51540fcd5eb700d7f40 (diff) |
x86: Implement array_index_mask_nospec
array_index_nospec() uses a mask to sanitize user controllable array
indexes, i.e. generate a 0 mask if 'index' >= 'size', and a ~0 mask
otherwise. While the default array_index_mask_nospec() handles the
carry-bit from the (index - size) result in software.
The x86 array_index_mask_nospec() does the same, but the carry-bit is
handled in the processor CF flag without conditional instructions in the
control flow.
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-arch@vger.kernel.org
Cc: kernel-hardening@lists.openwall.com
Cc: gregkh@linuxfoundation.org
Cc: alan@linux.intel.com
Link: https://lkml.kernel.org/r/151727414808.33451.1873237130672785331.stgit@dwillia2-desk3.amr.corp.intel.com
Diffstat (limited to 'arch/x86/include')
-rw-r--r-- | arch/x86/include/asm/barrier.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h index 7fb336210e1b..173b38f5fe88 100644 --- a/arch/x86/include/asm/barrier.h +++ b/arch/x86/include/asm/barrier.h | |||
@@ -24,6 +24,30 @@ | |||
24 | #define wmb() asm volatile("sfence" ::: "memory") | 24 | #define wmb() asm volatile("sfence" ::: "memory") |
25 | #endif | 25 | #endif |
26 | 26 | ||
27 | /** | ||
28 | * array_index_mask_nospec() - generate a mask that is ~0UL when the | ||
29 | * bounds check succeeds and 0 otherwise | ||
30 | * @index: array element index | ||
31 | * @size: number of elements in array | ||
32 | * | ||
33 | * Returns: | ||
34 | * 0 - (index < size) | ||
35 | */ | ||
36 | static inline unsigned long array_index_mask_nospec(unsigned long index, | ||
37 | unsigned long size) | ||
38 | { | ||
39 | unsigned long mask; | ||
40 | |||
41 | asm ("cmp %1,%2; sbb %0,%0;" | ||
42 | :"=r" (mask) | ||
43 | :"r"(size),"r" (index) | ||
44 | :"cc"); | ||
45 | return mask; | ||
46 | } | ||
47 | |||
48 | /* Override the default implementation from linux/nospec.h. */ | ||
49 | #define array_index_mask_nospec array_index_mask_nospec | ||
50 | |||
27 | #ifdef CONFIG_X86_PPRO_FENCE | 51 | #ifdef CONFIG_X86_PPRO_FENCE |
28 | #define dma_rmb() rmb() | 52 | #define dma_rmb() rmb() |
29 | #else | 53 | #else |