public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 0/3]  Add __builtin_load_no_speculate
@ 2018-01-04 13:58 Richard Earnshaw
  2018-01-04 13:58 ` [PATCH 1/3] [builtins] Generic support for __builtin_load_no_speculate() Richard Earnshaw
                   ` (7 more replies)
  0 siblings, 8 replies; 70+ messages in thread
From: Richard Earnshaw @ 2018-01-04 13:58 UTC (permalink / raw)
  To: gcc-patches; +Cc: Richard Earnshaw

[-- Attachment #1: Type: text/plain, Size: 4444 bytes --]


Recently, Google Project Zero disclosed several classes of attack
against speculative execution. One of these, known as variant-1
(CVE-2017-5753), allows explicit bounds checks to be bypassed under
speculation, providing an arbitrary read gadget. Further details can
be found on the GPZ blog [1] and the documentation that is included
with the first patch.

This patch set adds a new builtin function for GCC to provide a
mechanism for limiting speculation by a CPU after a bounds-checked
memory access.  I've tried to design this in such a way that it can be
used for any target where this might be necessary.  The patch set
provides a generic implementation of the builtin and then
target-specific support for Arm and AArch64.  Other architectures can
utilize the internal infrastructure as needed.

Most of the details of the builtin and the hooks that need to be
implemented to support it are described in the updates to the manual,
but a short summary is given below.

TYP __builtin_load_no_speculate
        (const volatile TYP *ptr,
         const volatile void *lower,
         const volatile void *upper,
         TYP failval,
         const volatile void *cmpptr)

Where TYP can be any integral type (signed or unsigned char, int,
short, long, etc) or any pointer type.

The builtin implements the following logical behaviour:

inline TYP __builtin_load_no_speculate
         (const volatile TYP *ptr,
          const volatile void *lower,
          const volatile void *upper,
          TYP failval,
          const volatile void *cmpptr)
{
  TYP result;

  if (cmpptr >= lower && cmpptr < upper)
    result = *ptr;
  else
    result = failval;
  return result;
}

in addition the specification of the builtin ensures that future
speculation using *ptr may only continue iff cmpptr lies within the
bounds specified.

Some optimizations are permitted to make the builtin easier to use.
The final two arguments can both be omitted (c++ style): failval will
default to 0 in this case and if cmpptr is omitted ptr will be used
for expansions of the range check.  In addition either lower or upper
(but not both) may be a literal NULL and the expansion will then
ignore that boundary condition when expanding.

The patch set is constructed as follows:
1 - generic modifications to GCC providing the builtin function for all
    architectures and expanding to an implementation that gives the
    logical behaviour of the builtin only.  A warning is generated if
    this expansion path is used that code will execute correctly but
    without providing protection against speculative use.
2 - AArch64 support
3 - AArch32 support (arm) for A32 and thumb2 states.

These patches can be used with the header file that Arm recently
published here: https://github.com/ARM-software/speculation-barrier.

Kernel patches are also being developed, eg:
https://lkml.org/lkml/2018/1/3/754.  The intent is that eventually
code like this will be able to use support directly from the compiler
in a portable manner.

Similar patches are also being developed for LLVM and will be posted
to their development lists shortly.

[1] More information on the topic can be found here:
https://googleprojectzero.blogspot.co.uk/2018/01/reading-privileged-memory-with-side.html
Arm specific information can be found here: https://www.arm.com/security-update



Richard Earnshaw (3):
  [builtins] Generic support for __builtin_load_no_speculate()
  [aarch64] Implement support for __builtin_load_no_speculate.
  [arm] Implement support for the de-speculation intrinsic

 gcc/builtin-types.def         |  16 +++++
 gcc/builtins.c                |  99 +++++++++++++++++++++++++
 gcc/builtins.def              |  22 ++++++
 gcc/c-family/c-common.c       | 164 ++++++++++++++++++++++++++++++++++++++++++
 gcc/c-family/c-cppbuiltin.c   |   5 +-
 gcc/config/aarch64/aarch64.c  |  92 ++++++++++++++++++++++++
 gcc/config/aarch64/aarch64.md |  28 ++++++++
 gcc/config/arm/arm.c          | 107 +++++++++++++++++++++++++++
 gcc/config/arm/arm.md         |  40 ++++++++++-
 gcc/config/arm/unspecs.md     |   1 +
 gcc/doc/cpp.texi              |   4 ++
 gcc/doc/extend.texi           |  53 ++++++++++++++
 gcc/doc/tm.texi               |   6 ++
 gcc/doc/tm.texi.in            |   2 +
 gcc/target.def                |  20 ++++++
 gcc/targhooks.c               |  69 ++++++++++++++++++
 gcc/targhooks.h               |   3 +
 17 files changed, 729 insertions(+), 2 deletions(-)

[-- Attachment #2: Type: text/plain, Size: 1 bytes --]



^ permalink raw reply	[flat|nested] 70+ messages in thread
* Re: [PATCH 0/3] Add __builtin_load_no_speculate
@ 2018-01-07 23:11 Bernd Edlinger
  0 siblings, 0 replies; 70+ messages in thread
From: Bernd Edlinger @ 2018-01-07 23:11 UTC (permalink / raw)
  To: Richard Earnshaw, gcc-patches

Hi Richard,

I wonder how to use this builtin correctly.
Frankly it sounds like way too complicated.

Do you expect we need to use this stuff on every
array access now, or is that just a theoretical
thing that can only happen with jave byte code
interpreters?

If I assume there is an array of int, then
the correct way to check the array bounds
would be something like:

int a[N];

int x = __builtin_load_no_speculate(ptr, a, a+N);

But what if ptr points to the last byte of a[N-1] ?
This would load 3 bytes past the array, right ?

Then you would have to write:

x = __builtin_load_no_speculate(ptr, a, (char*)(a+N) - 3);

which is just horrible, wouldn't it be better to fold that
into the condition:

I mean, instead of...

if (cmpptr >= lower && cmpptr < upper)

...use something like:

if (cmpptr >= lower && cmpptr <= upper - sizeof(TYP))


And what happens if the untrusted pointer is unaligned?
Wouldn't that also give quite surprising values to speculate
with?


Bernd.

^ permalink raw reply	[flat|nested] 70+ messages in thread
* Re: [PATCH 0/3] Add __builtin_load_no_speculate
@ 2018-01-08 15:47 Nick Clifton
  0 siblings, 0 replies; 70+ messages in thread
From: Nick Clifton @ 2018-01-08 15:47 UTC (permalink / raw)
  To: gcc-patches

Hi Guys,

  It seems to me that it might be worth taking a step back here,
  and consider adding a security framework to gcc.  Mitigations
  for CVEs in the past have resulted in individual patches being
  added to gcc, oftern in a target specific manner, and with no
  real framework to support them, document them, or indicate to
  an external tool that they have been applied.

  In addition security fixes often result in the generation of
  less optimal code, and so it might be useful to have a way to
  tell other parts of gcc that a given particular sequence should
  not be altered.

  Not that I am an expert in this area, but I do think that it is
  something that should be discussed...

Cheers
  Nick



^ permalink raw reply	[flat|nested] 70+ messages in thread
[parent not found: <9f3b02c6-09a7-273c-64b3-4d00a40f99bf@hotmail.de>]

end of thread, other threads:[~2018-01-18 14:50 UTC | newest]

Thread overview: 70+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-04 13:58 [PATCH 0/3] Add __builtin_load_no_speculate Richard Earnshaw
2018-01-04 13:58 ` [PATCH 1/3] [builtins] Generic support for __builtin_load_no_speculate() Richard Earnshaw
2018-01-08  2:20   ` Bill Schmidt
2018-01-08  6:55     ` Jeff Law
2018-01-08 11:52       ` Richard Biener
2018-01-08 14:23       ` Bill Schmidt
2018-01-08 15:38         ` Richard Earnshaw (lists)
2018-01-08 17:02           ` Bill Schmidt
2018-01-08 19:42         ` Jeff Law
2018-01-08 21:08           ` Bill Schmidt
2018-01-10 23:48             ` Jeff Law
2018-01-08 14:19     ` Richard Earnshaw (lists)
2018-01-08 16:07       ` Bill Schmidt
2018-01-09 10:26         ` Richard Earnshaw (lists)
2018-01-09 17:11           ` Bill Schmidt
2018-01-10 23:36             ` Jeff Law
2018-01-10 23:28         ` Jeff Law
2018-01-12 16:16           ` Richard Earnshaw (lists)
2018-01-12 22:48             ` Bill Schmidt
2018-01-04 13:59 ` [PATCH 3/3] [arm] Implement support for the de-speculation intrinsic Richard Earnshaw
2018-01-04 13:59 ` [PATCH 2/3] [aarch64] Implement support for __builtin_load_no_speculate Richard Earnshaw
2018-01-05  9:51   ` Richard Biener
2018-01-05 10:48     ` Richard Earnshaw (lists)
2018-01-05 16:31       ` Jeff Law
2018-01-04 18:20 ` [PATCH 0/3] Add __builtin_load_no_speculate Joseph Myers
2018-01-05 17:26   ` Jeff Law
2018-01-09 15:30     ` Richard Earnshaw (lists)
2018-01-11  0:13       ` Jeff Law
2018-01-05  9:44 ` Richard Biener
2018-01-05 10:40   ` Richard Earnshaw (lists)
2018-01-05 10:47     ` Richard Biener
2018-01-05 10:59       ` Richard Earnshaw (lists)
2018-01-05 11:35         ` Jakub Jelinek
2018-01-05 11:58           ` Richard Earnshaw (lists)
2018-01-05 16:44             ` Jeff Law
2018-01-05 16:50       ` Jeff Law
2018-01-05 16:41     ` Jeff Law
2018-01-05 16:37   ` Jeff Law
2018-01-05 11:37 ` Alexander Monakov
2018-01-05 11:53   ` Richard Earnshaw (lists)
2018-01-05 13:08     ` Alexander Monakov
2018-01-09 10:47       ` Richard Earnshaw (lists)
2018-01-09 13:41         ` Alexander Monakov
2018-01-09 13:50           ` Richard Earnshaw (lists)
2018-01-09 14:37             ` Alexander Monakov
2018-01-10 23:48         ` Jeff Law
2018-01-05 17:24 ` Jeff Law
2018-01-09 11:44   ` Richard Earnshaw (lists)
2018-01-17 14:56 ` [PATCH 0/3] [v2] Implement __builtin_speculation_safe_load Richard Earnshaw
2018-01-17 14:56   ` [PATCH 3/3] [arm] Implement support for the de-speculation intrinsic Richard Earnshaw
2018-01-17 14:56   ` [PATCH 2/3] [aarch64] Implement support for __builtin_speculation_safe_load Richard Earnshaw
2018-01-17 14:59   ` [PATCH 1/3] [builtins] Generic support for __builtin_speculation_safe_load() Richard Earnshaw
2018-01-17 17:22     ` Joseph Myers
2018-01-17 17:22       ` Jakub Jelinek
2018-01-18 12:48     ` Richard Biener
2018-01-18 12:52       ` Richard Biener
2018-01-18 14:17       ` Richard Earnshaw (lists)
2018-01-18 14:59         ` Richard Biener
2018-01-17 17:17   ` [PATCH 0/3] [v2] Implement __builtin_speculation_safe_load Joseph Myers
2018-01-07 23:11 [PATCH 0/3] Add __builtin_load_no_speculate Bernd Edlinger
2018-01-08 15:47 Nick Clifton
     [not found] <9f3b02c6-09a7-273c-64b3-4d00a40f99bf@hotmail.de>
2018-01-08 16:17 ` Bernd Edlinger
2018-01-08 17:52   ` Richard Earnshaw (lists)
2018-01-09 17:44     ` Bernd Edlinger
2018-01-09 17:53       ` Richard Earnshaw (lists)
2018-01-09 18:00         ` Bernd Edlinger
2018-01-09 18:01           ` Richard Earnshaw (lists)
2018-01-09 18:21             ` Richard Earnshaw (lists)
2018-01-09 18:22               ` Bernd Edlinger
2018-01-09 22:54         ` Bernd Edlinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).