2017-12-08 03:21:30 +08:00
|
|
|
=======================================================
|
|
|
|
Hardware-assisted AddressSanitizer Design Documentation
|
|
|
|
=======================================================
|
2017-12-05 04:01:38 +08:00
|
|
|
|
|
|
|
This page is a design document for
|
2017-12-08 03:21:30 +08:00
|
|
|
**hardware-assisted AddressSanitizer** (or **HWASAN**)
|
2017-12-05 04:01:38 +08:00
|
|
|
a tool similar to :doc:`AddressSanitizer`,
|
|
|
|
but based on partial hardware assistance.
|
|
|
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
============
|
|
|
|
|
|
|
|
:doc:`AddressSanitizer`
|
|
|
|
tags every 8 bytes of the application memory with a 1 byte tag (using *shadow memory*),
|
|
|
|
uses *redzones* to find buffer-overflows and
|
|
|
|
*quarantine* to find use-after-free.
|
|
|
|
The redzones, the quarantine, and, to a less extent, the shadow, are the
|
|
|
|
sources of AddressSanitizer's memory overhead.
|
|
|
|
See the `AddressSanitizer paper`_ for details.
|
|
|
|
|
2017-12-19 05:40:07 +08:00
|
|
|
AArch64 has the `Address Tagging`_ (or top-byte-ignore, TBI), a hardware feature that allows
|
2017-12-05 04:01:38 +08:00
|
|
|
software to use 8 most significant bits of a 64-bit pointer as
|
2017-12-08 03:21:30 +08:00
|
|
|
a tag. HWASAN uses `Address Tagging`_
|
2017-12-05 04:01:38 +08:00
|
|
|
to implement a memory safety tool, similar to :doc:`AddressSanitizer`,
|
|
|
|
but with smaller memory overhead and slightly different (mostly better)
|
|
|
|
accuracy guarantees.
|
|
|
|
|
|
|
|
Algorithm
|
|
|
|
=========
|
2018-03-14 09:55:49 +08:00
|
|
|
* Every heap/stack/global memory object is forcibly aligned by `TG` bytes
|
|
|
|
(`TG` is e.g. 16 or 64). We call `TG` the **tagging granularity**.
|
|
|
|
* For every such object a random `TS`-bit tag `T` is chosen (`TS`, or tag size, is e.g. 4 or 8)
|
2017-12-05 04:01:38 +08:00
|
|
|
* The pointer to the object is tagged with `T`.
|
2018-03-14 09:55:49 +08:00
|
|
|
* The memory for the object is also tagged with `T` (using a `TG=>1` shadow memory)
|
2017-12-05 04:01:38 +08:00
|
|
|
* Every load and store is instrumented to read the memory tag and compare it
|
|
|
|
with the pointer tag, exception is raised on tag mismatch.
|
|
|
|
|
2018-03-14 09:55:49 +08:00
|
|
|
For a more detailed discussion of this approach see https://arxiv.org/pdf/1802.09517.pdf
|
|
|
|
|
2017-12-05 04:01:38 +08:00
|
|
|
Instrumentation
|
|
|
|
===============
|
|
|
|
|
|
|
|
Memory Accesses
|
|
|
|
---------------
|
2017-12-19 05:40:07 +08:00
|
|
|
All memory accesses are prefixed with an inline instruction sequence that
|
|
|
|
verifies the tags. Currently, the following sequence is used:
|
2017-12-05 04:01:38 +08:00
|
|
|
|
2017-12-19 05:40:07 +08:00
|
|
|
|
2018-08-06 09:28:42 +08:00
|
|
|
.. code-block:: none
|
2017-12-19 05:40:07 +08:00
|
|
|
|
|
|
|
// int foo(int *a) { return *a; }
|
|
|
|
// clang -O2 --target=aarch64-linux -fsanitize=hwaddress -c load.c
|
|
|
|
foo:
|
2018-04-25 01:41:48 +08:00
|
|
|
0: 08 00 00 90 adrp x8, 0 <__hwasan_shadow>
|
2018-08-06 09:28:42 +08:00
|
|
|
4: 08 01 40 f9 ldr x8, [x8] // shadow base (to be resolved by the loader)
|
|
|
|
8: 09 dc 44 d3 ubfx x9, x0, #4, #52 // shadow offset
|
|
|
|
c: 28 69 68 38 ldrb w8, [x9, x8] // load shadow tag
|
|
|
|
10: 09 fc 78 d3 lsr x9, x0, #56 // extract address tag
|
|
|
|
14: 3f 01 08 6b cmp w9, w8 // compare tags
|
|
|
|
18: 61 00 00 54 b.ne 24 // jump on mismatch
|
|
|
|
1c: 00 00 40 b9 ldr w0, [x0] // original load
|
2018-04-25 01:41:48 +08:00
|
|
|
20: c0 03 5f d6 ret
|
2018-08-06 09:28:42 +08:00
|
|
|
24: 40 20 21 d4 brk #0x902 // trap
|
2017-12-19 05:40:07 +08:00
|
|
|
|
|
|
|
Alternatively, memory accesses are prefixed with a function call.
|
2017-12-05 04:01:38 +08:00
|
|
|
|
|
|
|
Heap
|
|
|
|
----
|
|
|
|
|
|
|
|
Tagging the heap memory/pointers is done by `malloc`.
|
2018-03-14 09:55:49 +08:00
|
|
|
This can be based on any malloc that forces all objects to be TG-aligned.
|
2017-12-19 05:40:07 +08:00
|
|
|
`free` tags the memory with a different tag.
|
2017-12-05 04:01:38 +08:00
|
|
|
|
|
|
|
Stack
|
|
|
|
-----
|
|
|
|
|
2018-02-03 09:06:21 +08:00
|
|
|
Stack frames are instrumented by aligning all non-promotable allocas
|
2018-03-14 09:55:49 +08:00
|
|
|
by `TG` and tagging stack memory in function prologue and epilogue.
|
2018-02-03 09:06:21 +08:00
|
|
|
|
|
|
|
Tags for different allocas in one function are **not** generated
|
|
|
|
independently; doing that in a function with `M` allocas would require
|
|
|
|
maintaining `M` live stack pointers, significantly increasing register
|
|
|
|
pressure. Instead we generate a single base tag value in the prologue,
|
|
|
|
and build the tag for alloca number `M` as `ReTag(BaseTag, M)`, where
|
|
|
|
ReTag can be as simple as exclusive-or with constant `M`.
|
|
|
|
|
2017-12-05 04:01:38 +08:00
|
|
|
Stack instrumentation is expected to be a major source of overhead,
|
|
|
|
but could be optional.
|
|
|
|
|
|
|
|
Globals
|
|
|
|
-------
|
|
|
|
|
|
|
|
TODO: details.
|
|
|
|
|
|
|
|
Error reporting
|
|
|
|
---------------
|
|
|
|
|
2017-12-19 05:40:07 +08:00
|
|
|
Errors are generated by the `HLT` instruction and are handled by a signal handler.
|
2017-12-05 04:01:38 +08:00
|
|
|
|
2017-12-08 03:21:30 +08:00
|
|
|
Attribute
|
|
|
|
---------
|
|
|
|
|
2017-12-09 02:14:03 +08:00
|
|
|
HWASAN uses its own LLVM IR Attribute `sanitize_hwaddress` and a matching
|
2017-12-08 03:21:30 +08:00
|
|
|
C function attribute. An alternative would be to re-use ASAN's attribute
|
|
|
|
`sanitize_address`. The reasons to use a separate attribute are:
|
|
|
|
|
|
|
|
* Users may need to disable ASAN but not HWASAN, or vise versa,
|
|
|
|
because the tools have different trade-offs and compatibility issues.
|
|
|
|
* LLVM (ideally) does not use flags to decide which pass is being used,
|
|
|
|
ASAN or HWASAN are being applied, based on the function attributes.
|
|
|
|
|
|
|
|
This does mean that users of HWASAN may need to add the new attribute
|
|
|
|
to the code that already uses the old attribute.
|
|
|
|
|
2017-12-05 04:01:38 +08:00
|
|
|
|
|
|
|
Comparison with AddressSanitizer
|
|
|
|
================================
|
|
|
|
|
2017-12-08 03:21:30 +08:00
|
|
|
HWASAN:
|
2017-12-05 04:01:38 +08:00
|
|
|
* Is less portable than :doc:`AddressSanitizer`
|
|
|
|
as it relies on hardware `Address Tagging`_ (AArch64).
|
|
|
|
Address Tagging can be emulated with compiler instrumentation,
|
|
|
|
but it will require the instrumentation to remove the tags before
|
|
|
|
any load or store, which is infeasible in any realistic environment
|
|
|
|
that contains non-instrumented code.
|
|
|
|
* May have compatibility problems if the target code uses higher
|
|
|
|
pointer bits for other purposes.
|
|
|
|
* May require changes in the OS kernels (e.g. Linux seems to dislike
|
2017-12-08 03:21:30 +08:00
|
|
|
tagged pointers passed from address space:
|
|
|
|
https://www.kernel.org/doc/Documentation/arm64/tagged-pointers.txt).
|
2017-12-05 04:01:38 +08:00
|
|
|
* **Does not require redzones to detect buffer overflows**,
|
|
|
|
but the buffer overflow detection is probabilistic, with roughly
|
2018-03-14 09:55:49 +08:00
|
|
|
`(2**TS-1)/(2**TS)` probability of catching a bug.
|
2017-12-05 04:01:38 +08:00
|
|
|
* **Does not require quarantine to detect heap-use-after-free,
|
|
|
|
or stack-use-after-return**.
|
|
|
|
The detection is similarly probabilistic.
|
|
|
|
|
2017-12-08 03:21:30 +08:00
|
|
|
The memory overhead of HWASAN is expected to be much smaller
|
2017-12-05 04:01:38 +08:00
|
|
|
than that of AddressSanitizer:
|
2018-03-14 09:55:49 +08:00
|
|
|
`1/TG` extra memory for the shadow
|
|
|
|
and some overhead due to `TG`-aligning all objects.
|
|
|
|
|
|
|
|
Supported architectures
|
|
|
|
=======================
|
|
|
|
HWASAN relies on `Address Tagging`_ which is only available on AArch64.
|
|
|
|
For other 64-bit architectures it is possible to remove the address tags
|
|
|
|
before every load and store by compiler instrumentation, but this variant
|
|
|
|
will have limited deployability since not all of the code is
|
|
|
|
typically instrumented.
|
|
|
|
|
|
|
|
The HWASAN's approach is not applicable to 32-bit architectures.
|
2017-12-05 04:01:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
Related Work
|
|
|
|
============
|
|
|
|
* `SPARC ADI`_ implements a similar tool mostly in hardware.
|
|
|
|
* `Effective and Efficient Memory Protection Using Dynamic Tainting`_ discusses
|
|
|
|
similar approaches ("lock & key").
|
|
|
|
* `Watchdog`_ discussed a heavier, but still somewhat similar
|
|
|
|
"lock & key" approach.
|
|
|
|
* *TODO: add more "related work" links. Suggestions are welcome.*
|
|
|
|
|
|
|
|
|
|
|
|
.. _Watchdog: http://www.cis.upenn.edu/acg/papers/isca12_watchdog.pdf
|
|
|
|
.. _Effective and Efficient Memory Protection Using Dynamic Tainting: https://www.cc.gatech.edu/~orso/papers/clause.doudalis.orso.prvulovic.pdf
|
|
|
|
.. _SPARC ADI: https://lazytyped.blogspot.com/2017/09/getting-started-with-adi.html
|
|
|
|
.. _AddressSanitizer paper: https://www.usenix.org/system/files/conference/atc12/atc12-final39.pdf
|
|
|
|
.. _Address Tagging: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0024a/ch12s05s01.html
|
|
|
|
|