diff options
author | Dan Williams <dan.j.williams@intel.com> | 2018-01-29 17:02:28 -0800 |
---|---|---|
committer | Ben Hutchings <ben@decadent.org.uk> | 2018-03-19 18:58:36 +0000 |
commit | b75b708ffa363a3e5500fdd3f377500a2be7fd31 (patch) | |
tree | 382fef8572facc5379f55b9ee1b3a2d571090eb7 | |
parent | 9c6bfff3c4e1599b4f8ee495a30d270163a8e1d4 (diff) | |
download | linux-stable-b75b708ffa363a3e5500fdd3f377500a2be7fd31.tar.gz linux-stable-b75b708ffa363a3e5500fdd3f377500a2be7fd31.tar.bz2 linux-stable-b75b708ffa363a3e5500fdd3f377500a2be7fd31.zip |
x86: Implement array_index_mask_nospec
commit babdde2698d482b6c0de1eab4f697cf5856c5859 upstream.
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
[bwh: Backported to 3.2: adjust filename, context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
-rw-r--r-- | arch/x86/include/asm/system.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/arch/x86/include/asm/system.h b/arch/x86/include/asm/system.h index 51738f69a45c..41fda4d8710c 100644 --- a/arch/x86/include/asm/system.h +++ b/arch/x86/include/asm/system.h @@ -455,6 +455,30 @@ void stop_this_cpu(void *dummy); #endif /** + * array_index_mask_nospec() - generate a mask that is ~0UL when the + * bounds check succeeds and 0 otherwise + * @index: array element index + * @size: number of elements in array + * + * Returns: + * 0 - (index < size) + */ +static inline unsigned long array_index_mask_nospec(unsigned long index, + unsigned long size) +{ + unsigned long mask; + + asm ("cmp %1,%2; sbb %0,%0;" + :"=r" (mask) + :"r"(size),"r" (index) + :"cc"); + return mask; +} + +/* Override the default implementation from linux/nospec.h. */ +#define array_index_mask_nospec array_index_mask_nospec + +/** * read_barrier_depends - Flush all pending reads that subsequents reads * depend on. * |