From: Peter Zijlstra <peterz@infradead.org>
To: Dan Williams <dan.j.williams@intel.com>
Cc: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org,
kernel-hardening@lists.openwall.com,
Catalin Marinas <catalin.marinas@arm.com>,
x86@kernel.org, Will Deacon <will.deacon@arm.com>,
Alexei Starovoitov <ast@kernel.org>,
Russell King <linux@armlinux.org.uk>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
tglx@linutronix.de, torvalds@linux-foundation.org,
akpm@linux-foundation.org, alan@linux.intel.com
Subject: Re: [PATCH v2 06/19] asm-generic/barrier: mask speculative execution flows
Date: Fri, 12 Jan 2018 10:12:46 +0100 [thread overview]
Message-ID: <20180112091246.GP32035@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <151571801681.27429.15417813964230837664.stgit@dwillia2-desk3.amr.corp.intel.com>
On Thu, Jan 11, 2018 at 04:46:56PM -0800, Dan Williams wrote:
> diff --git a/include/linux/nospec.h b/include/linux/nospec.h
> new file mode 100644
> index 000000000000..5c66fc30f919
> --- /dev/null
> +++ b/include/linux/nospec.h
> @@ -0,0 +1,71 @@
> +// SPDX-License-Identifier: GPL-2.0
> +// Copyright(c) 2018 Intel Corporation. All rights reserved.
> +
> +#ifndef __NOSPEC_H__
> +#define __NOSPEC_H__
> +
> +#include <linux/jump_label.h>
> +#include <asm/barrier.h>
> +
> +#ifndef array_ptr_mask
> +#define array_ptr_mask(idx, sz) \
> +({ \
> + unsigned long mask; \
> + unsigned long _i = (idx); \
> + unsigned long _s = (sz); \
> + \
> + mask = ~(long)(_i | (_s - 1 - _i)) >> (BITS_PER_LONG - 1); \
> + mask; \
> +})
> +#endif
> +
> +/**
> + * __array_ptr - Generate a pointer to an array element, ensuring
> + * the pointer is bounded under speculation to NULL.
> + *
> + * @base: the base of the array
> + * @idx: the index of the element, must be less than LONG_MAX
> + * @sz: the number of elements in the array, must be less than LONG_MAX
> + *
> + * If @idx falls in the interval [0, @sz), returns the pointer to
> + * @arr[@idx], otherwise returns NULL.
> + */
> +#define __array_ptr(base, idx, sz) \
> +({ \
> + union { typeof(*(base)) *_ptr; unsigned long _bit; } __u; \
> + typeof(*(base)) *_arr = (base); \
> + unsigned long _i = (idx); \
> + unsigned long _mask = array_ptr_mask(_i, (sz)); \
> + \
> + __u._ptr = _arr + (_i & _mask); \
> + __u._bit &= _mask; \
> + __u._ptr; \
> +})
> +
> +#ifdef CONFIG_SPECTRE1_IFENCE
> +DECLARE_STATIC_KEY_TRUE(nospec_key);
> +#else
> +DECLARE_STATIC_KEY_FALSE(nospec_key);
> +#endif
> +
> +#ifdef ifence_array_ptr
> +/*
> + * The expectation is that no compiler or cpu will mishandle __array_ptr
> + * leading to problematic speculative execution. Bypass the ifence
> + * based implementation by default.
> + */
> +#define array_ptr(base, idx, sz) \
> +({ \
> + typeof(*(base)) *__ret; \
> + \
> + if (static_branch_unlikely(&nospec_key)) \
> + __ret = ifence_array_ptr(base, idx, sz); \
> + else \
> + __ret = __array_ptr(base, idx, sz); \
> + __ret; \
> +})
So I think this wants:
#ifndef HAVE_JUMP_LABEL
#error Compiler lacks asm-goto, can generate unsafe code
#endif
Suppose the generic array_ptr_mask() is unsafe on some arch and they
only implement ifence_array_ptr() and they compile without asm-goto,
then the above reverts to a dynamic condition, which can be speculated.
If we then speculate into the 'bad' __array_ptr we're screwed.
> +#else
> +#define array_ptr __array_ptr
> +#endif
> +
> +#endif /* __NOSPEC_H__ */
In general I think I would write all this in a form like:
#define __array_ptr(base, idx, sz) \
({ \
union { typeof(*(base)) *_ptr; unsigned long _bit; } __u; \
typeof(*(base)) *_arr = (base); \
unsigned long _i = (idx); \
unsigned long _mask = array_ptr_mask(_i, (sz)); \
\
__u._ptr = _arr + (_i & _mask); \
__u._bit &= _mask; \
__u._ptr; \
})
#if defined(array_ptr_mask) && defined(ifence_array_ptr)
#ifndef HAVE_JUMP_LABEL
#error Compiler lacks asm-goto, can generate unsafe code
#endif
#define array_ptr(base, idx, sz) \
({ \
typeof(*(base)) *__ret; \
\
if (static_branch_unlikely(&nospec_key)) \
__ret = ifence_array_ptr(base, idx, sz); \
else \
__ret = __array_ptr(base, idx, sz); \
__ret; \
})
#elif defined(array_ptr_mask)
#define array_ptr(base, idx, sz) __array_ptr(base, idx, sz)
#elif defined(ifence_array_ptr)
#define array_ptr(base, idx, sz) ifence_array_ptr(base, idx, sz)
#else
/* XXX we want a suitable warning here ? */
#define array_ptr(base, idx, sz) (idx < sz ? base + idx : NULL)
#endif
and stick the generic array_ptr_mask into asm-generic/nospec.h or
something.
Then the static key stuff is limited to architectures that define _both_
array_ptr_mask and ifence_array_ptr.
next prev parent reply other threads:[~2018-01-12 9:13 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-12 0:46 [PATCH v2 00/19] prevent bounds-check bypass via speculative execution Dan Williams
2018-01-12 0:46 ` [PATCH v2 01/19] Documentation: document array_ptr Dan Williams
2018-01-12 10:38 ` Geert Uytterhoeven
2018-01-16 21:01 ` Kees Cook
2018-01-12 0:46 ` [PATCH v2 02/19] arm64: implement ifence_array_ptr() Dan Williams
2018-01-12 0:46 ` [PATCH v2 03/19] arm: " Dan Williams
2018-01-12 0:46 ` [PATCH v2 04/19] x86: implement ifence() Dan Williams
2018-01-12 2:27 ` Eric W. Biederman
2018-01-12 3:39 ` Dan Williams
2018-01-12 0:46 ` [PATCH v2 05/19] x86: implement ifence_array_ptr() and array_ptr_mask() Dan Williams
2018-01-12 0:46 ` [PATCH v2 06/19] asm-generic/barrier: mask speculative execution flows Dan Williams
2018-01-12 2:42 ` Eric W. Biederman
2018-01-12 9:12 ` Peter Zijlstra [this message]
2018-01-13 0:41 ` Dan Williams
2018-01-15 8:46 ` Peter Zijlstra
2018-01-12 0:47 ` [PATCH v2 07/19] x86: introduce __uaccess_begin_nospec and ASM_IFENCE Dan Williams
2018-01-12 17:51 ` Josh Poimboeuf
2018-01-12 18:21 ` Dan Williams
2018-01-12 18:58 ` Josh Poimboeuf
2018-01-12 19:26 ` Dan Williams
2018-01-12 20:01 ` Linus Torvalds
2018-01-12 20:41 ` Josh Poimboeuf
2018-01-12 0:47 ` [PATCH v2 08/19] x86: use __uaccess_begin_nospec and ASM_IFENCE in get_user paths Dan Williams
2018-01-12 1:11 ` Linus Torvalds
2018-01-12 1:14 ` Dan Williams
2018-01-12 0:47 ` [PATCH v2 09/19] ipv6: prevent bounds-check bypass via speculative execution Dan Williams
2018-01-12 0:47 ` [PATCH v2 10/19] ipv4: " Dan Williams
2018-01-12 7:59 ` Greg KH
2018-01-12 18:47 ` Dan Williams
2018-01-13 8:56 ` Greg KH
2018-01-12 0:47 ` [PATCH v2 11/19] vfs, fdtable: " Dan Williams
2018-01-12 0:47 ` [PATCH v2 12/19] userns: " Dan Williams
2018-01-12 0:47 ` [PATCH v2 13/19] udf: " Dan Williams
2018-01-15 10:32 ` Jan Kara
2018-01-15 17:49 ` Dan Williams
2018-01-12 0:47 ` [PATCH v2 14/19] [media] uvcvideo: " Dan Williams
2018-08-06 21:40 ` Laurent Pinchart
2018-01-12 0:47 ` [PATCH v2 15/19] carl9170: " Dan Williams
2018-01-12 14:42 ` Christian Lamparter
2018-01-12 18:39 ` Dan Williams
2018-01-12 20:01 ` Christian Lamparter
2018-01-12 23:05 ` Dan Williams
2018-01-12 0:47 ` [PATCH v2 16/19] p54: " Dan Williams
2018-01-12 0:47 ` [PATCH v2 17/19] qla2xxx: " Dan Williams
2018-01-12 1:19 ` James Bottomley
2018-01-12 5:38 ` Dan Williams
2018-01-12 6:05 ` James Bottomley
2018-01-12 0:48 ` [PATCH v2 18/19] cw1200: " Dan Williams
2018-01-12 0:48 ` [PATCH v2 19/19] net: mpls: " Dan Williams
2018-01-12 1:19 ` [PATCH v2 00/19] " Linus Torvalds
2018-01-12 1:41 ` Dan Williams
2018-01-18 13:18 ` Will Deacon
2018-01-18 16:58 ` Dan Williams
2018-01-18 17:05 ` Will Deacon
2018-01-18 21:41 ` Laurent Pinchart
2018-01-13 0:15 ` Tony Luck
2018-01-13 18:51 ` Linus Torvalds
2018-01-16 19:21 ` Tony Luck
2018-01-12 10:02 ` Russell King - ARM Linux
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180112091246.GP32035@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=alan@linux.intel.com \
--cc=ast@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=dan.j.williams@intel.com \
--cc=hpa@zytor.com \
--cc=kernel-hardening@lists.openwall.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=will.deacon@arm.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox