Skip to content

Commit 1366262

Browse files
committed
hwasan: Improve precision of checks using short granule tags.
A short granule is a granule of size between 1 and `TG-1` bytes. The size of a short granule is stored at the location in shadow memory where the granule's tag is normally stored, while the granule's actual tag is stored in the last byte of the granule. This means that in order to verify that a pointer tag matches a memory tag, HWASAN must check for two possibilities: * the pointer tag is equal to the memory tag in shadow memory, or * the shadow memory tag is actually a short granule size, the value being loaded is in bounds of the granule and the pointer tag is equal to the last byte of the granule. Pointer tags between 1 to `TG-1` are possible and are as likely as any other tag. This means that these tags in memory have two interpretations: the full tag interpretation (where the pointer tag is between 1 and `TG-1` and the last byte of the granule is ordinary data) and the short tag interpretation (where the pointer tag is stored in the granule). When HWASAN detects an error near a memory tag between 1 and `TG-1`, it will show both the memory tag and the last byte of the granule. Currently, it is up to the user to disambiguate the two possibilities. Because this functionality obsoletes the right aligned heap feature of the HWASAN memory allocator (and because we can no longer easily test it), the feature is removed. Also update the documentation to cover both short granule tags and outlined checks. Differential Revision: https://reviews.llvm.org/D63908 llvm-svn: 365551
1 parent a6548d0 commit 1366262

File tree

17 files changed

+457
-232
lines changed

17 files changed

+457
-232
lines changed

clang/docs/HardwareAssistedAddressSanitizerDesign.rst

+52-12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ Algorithm
3838

3939
For a more detailed discussion of this approach see https://arxiv.org/pdf/1802.09517.pdf
4040

41+
Short granules
42+
--------------
43+
44+
A short granule is a granule of size between 1 and `TG-1` bytes. The size
45+
of a short granule is stored at the location in shadow memory where the
46+
granule's tag is normally stored, while the granule's actual tag is stored
47+
in the last byte of the granule. This means that in order to verify that a
48+
pointer tag matches a memory tag, HWASAN must check for two possibilities:
49+
50+
* the pointer tag is equal to the memory tag in shadow memory, or
51+
* the shadow memory tag is actually a short granule size, the value being loaded
52+
is in bounds of the granule and the pointer tag is equal to the last byte of
53+
the granule.
54+
55+
Pointer tags between 1 to `TG-1` are possible and are as likely as any other
56+
tag. This means that these tags in memory have two interpretations: the full
57+
tag interpretation (where the pointer tag is between 1 and `TG-1` and the
58+
last byte of the granule is ordinary data) and the short tag interpretation
59+
(where the pointer tag is stored in the granule).
60+
61+
When HWASAN detects an error near a memory tag between 1 and `TG-1`, it
62+
will show both the memory tag and the last byte of the granule. Currently,
63+
it is up to the user to disambiguate the two possibilities.
64+
4165
Instrumentation
4266
===============
4367

@@ -46,24 +70,40 @@ Memory Accesses
4670
All memory accesses are prefixed with an inline instruction sequence that
4771
verifies the tags. Currently, the following sequence is used:
4872

49-
5073
.. code-block:: none
5174
5275
// int foo(int *a) { return *a; }
53-
// clang -O2 --target=aarch64-linux -fsanitize=hwaddress -c load.c
76+
// clang -O2 --target=aarch64-linux -fsanitize=hwaddress -fsanitize-recover=hwaddress -c load.c
5477
foo:
55-
0: 08 00 00 90 adrp x8, 0 <__hwasan_shadow>
56-
4: 08 01 40 f9 ldr x8, [x8] // shadow base (to be resolved by the loader)
57-
8: 09 dc 44 d3 ubfx x9, x0, #4, #52 // shadow offset
58-
c: 28 69 68 38 ldrb w8, [x9, x8] // load shadow tag
59-
10: 09 fc 78 d3 lsr x9, x0, #56 // extract address tag
60-
14: 3f 01 08 6b cmp w9, w8 // compare tags
61-
18: 61 00 00 54 b.ne 24 // jump on mismatch
62-
1c: 00 00 40 b9 ldr w0, [x0] // original load
63-
20: c0 03 5f d6 ret
64-
24: 40 20 21 d4 brk #0x902 // trap
78+
0: 90000008 adrp x8, 0 <__hwasan_shadow>
79+
4: f9400108 ldr x8, [x8] // shadow base (to be resolved by the loader)
80+
8: d344dc09 ubfx x9, x0, #4, #52 // shadow offset
81+
c: 38696909 ldrb w9, [x8, x9] // load shadow tag
82+
10: d378fc08 lsr x8, x0, #56 // extract address tag
83+
14: 6b09011f cmp w8, w9 // compare tags
84+
18: 54000061 b.ne 24 <foo+0x24> // jump to short tag handler on mismatch
85+
1c: b9400000 ldr w0, [x0] // original load
86+
20: d65f03c0 ret
87+
24: 7100413f cmp w9, #0x10 // is this a short tag?
88+
28: 54000142 b.cs 50 <foo+0x50> // if not, trap
89+
2c: 12000c0a and w10, w0, #0xf // find the address's position in the short granule
90+
30: 11000d4a add w10, w10, #0x3 // adjust to the position of the last byte loaded
91+
34: 6b09015f cmp w10, w9 // check that position is in bounds
92+
38: 540000c2 b.cs 50 <foo+0x50> // if not, trap
93+
3c: 9240dc09 and x9, x0, #0xffffffffffffff
94+
40: b2400d29 orr x9, x9, #0xf // compute address of last byte of granule
95+
44: 39400129 ldrb w9, [x9] // load tag from it
96+
48: 6b09011f cmp w8, w9 // compare with pointer tag
97+
4c: 54fffe80 b.eq 1c <foo+0x1c> // if so, continue
98+
50: d4212440 brk #0x922 // otherwise trap
99+
54: b9400000 ldr w0, [x0] // tail duplicated original load (to handle recovery)
100+
58: d65f03c0 ret
65101
66102
Alternatively, memory accesses are prefixed with a function call.
103+
On AArch64, a function call is used by default in trapping mode. The code size
104+
and performance overhead of the call is reduced by using a custom calling
105+
convention that preserves most registers and is specialized to the register
106+
containing the address and the type and size of the memory access.
67107

68108
Heap
69109
----

compiler-rt/lib/hwasan/hwasan_allocator.cpp

+27-52
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "sanitizer_common/sanitizer_stackdepot.h"
1717
#include "hwasan.h"
1818
#include "hwasan_allocator.h"
19+
#include "hwasan_checks.h"
1920
#include "hwasan_mapping.h"
2021
#include "hwasan_malloc_bisect.h"
2122
#include "hwasan_thread.h"
@@ -42,13 +43,8 @@ enum RightAlignMode {
4243
kRightAlignAlways
4344
};
4445

45-
// These two variables are initialized from flags()->malloc_align_right
46-
// in HwasanAllocatorInit and are never changed afterwards.
47-
static RightAlignMode right_align_mode = kRightAlignNever;
48-
static bool right_align_8 = false;
49-
5046
// Initialized in HwasanAllocatorInit, an never changed.
51-
static ALIGNED(16) u8 tail_magic[kShadowAlignment];
47+
static ALIGNED(16) u8 tail_magic[kShadowAlignment - 1];
5248

5349
bool HwasanChunkView::IsAllocated() const {
5450
return metadata_ && metadata_->alloc_context_id && metadata_->requested_size;
@@ -58,8 +54,6 @@ bool HwasanChunkView::IsAllocated() const {
5854
static uptr AlignRight(uptr addr, uptr requested_size) {
5955
uptr tail_size = requested_size % kShadowAlignment;
6056
if (!tail_size) return addr;
61-
if (right_align_8)
62-
return tail_size > 8 ? addr : addr + 8;
6357
return addr + kShadowAlignment - tail_size;
6458
}
6559

@@ -95,30 +89,7 @@ void HwasanAllocatorInit() {
9589
!flags()->disable_allocator_tagging);
9690
SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
9791
allocator.Init(common_flags()->allocator_release_to_os_interval_ms);
98-
switch (flags()->malloc_align_right) {
99-
case 0: break;
100-
case 1:
101-
right_align_mode = kRightAlignSometimes;
102-
right_align_8 = false;
103-
break;
104-
case 2:
105-
right_align_mode = kRightAlignAlways;
106-
right_align_8 = false;
107-
break;
108-
case 8:
109-
right_align_mode = kRightAlignSometimes;
110-
right_align_8 = true;
111-
break;
112-
case 9:
113-
right_align_mode = kRightAlignAlways;
114-
right_align_8 = true;
115-
break;
116-
default:
117-
Report("ERROR: unsupported value of malloc_align_right flag: %d\n",
118-
flags()->malloc_align_right);
119-
Die();
120-
}
121-
for (uptr i = 0; i < kShadowAlignment; i++)
92+
for (uptr i = 0; i < sizeof(tail_magic); i++)
12293
tail_magic[i] = GetCurrentThread()->GenerateRandomTag();
12394
}
12495

@@ -172,29 +143,32 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
172143
uptr fill_size = Min(size, (uptr)flags()->max_malloc_fill_size);
173144
internal_memset(allocated, flags()->malloc_fill_byte, fill_size);
174145
}
175-
if (!right_align_mode)
146+
if (size != orig_size) {
176147
internal_memcpy(reinterpret_cast<u8 *>(allocated) + orig_size, tail_magic,
177-
size - orig_size);
148+
size - orig_size - 1);
149+
}
178150

179151
void *user_ptr = allocated;
180152
// Tagging can only be skipped when both tag_in_malloc and tag_in_free are
181153
// false. When tag_in_malloc = false and tag_in_free = true malloc needs to
182154
// retag to 0.
183155
if ((flags()->tag_in_malloc || flags()->tag_in_free) &&
184156
atomic_load_relaxed(&hwasan_allocator_tagging_enabled)) {
185-
tag_t tag = flags()->tag_in_malloc && malloc_bisect(stack, orig_size)
186-
? (t ? t->GenerateRandomTag() : kFallbackAllocTag)
187-
: 0;
188-
user_ptr = (void *)TagMemoryAligned((uptr)user_ptr, size, tag);
189-
}
190-
191-
if ((orig_size % kShadowAlignment) && (alignment <= kShadowAlignment) &&
192-
right_align_mode) {
193-
uptr as_uptr = reinterpret_cast<uptr>(user_ptr);
194-
if (right_align_mode == kRightAlignAlways ||
195-
GetTagFromPointer(as_uptr) & 1) { // use a tag bit as a random bit.
196-
user_ptr = reinterpret_cast<void *>(AlignRight(as_uptr, orig_size));
197-
meta->right_aligned = 1;
157+
if (flags()->tag_in_malloc && malloc_bisect(stack, orig_size)) {
158+
tag_t tag = t ? t->GenerateRandomTag() : kFallbackAllocTag;
159+
uptr tag_size = orig_size ? orig_size : 1;
160+
uptr full_granule_size = RoundDownTo(tag_size, kShadowAlignment);
161+
user_ptr =
162+
(void *)TagMemoryAligned((uptr)user_ptr, full_granule_size, tag);
163+
if (full_granule_size != tag_size) {
164+
u8 *short_granule =
165+
reinterpret_cast<u8 *>(allocated) + full_granule_size;
166+
TagMemoryAligned((uptr)short_granule, kShadowAlignment,
167+
tag_size % kShadowAlignment);
168+
short_granule[kShadowAlignment - 1] = tag;
169+
}
170+
} else {
171+
user_ptr = (void *)TagMemoryAligned((uptr)user_ptr, size, 0);
198172
}
199173
}
200174

@@ -204,10 +178,10 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
204178

205179
static bool PointerAndMemoryTagsMatch(void *tagged_ptr) {
206180
CHECK(tagged_ptr);
207-
tag_t ptr_tag = GetTagFromPointer(reinterpret_cast<uptr>(tagged_ptr));
181+
uptr tagged_uptr = reinterpret_cast<uptr>(tagged_ptr);
208182
tag_t mem_tag = *reinterpret_cast<tag_t *>(
209183
MemToShadow(reinterpret_cast<uptr>(UntagPtr(tagged_ptr))));
210-
return ptr_tag == mem_tag;
184+
return PossiblyShortTagMatches(mem_tag, tagged_uptr, 1);
211185
}
212186

213187
static void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) {
@@ -228,14 +202,15 @@ static void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) {
228202

229203
// Check tail magic.
230204
uptr tagged_size = TaggedSize(orig_size);
231-
if (flags()->free_checks_tail_magic && !right_align_mode && orig_size) {
232-
uptr tail_size = tagged_size - orig_size;
205+
if (flags()->free_checks_tail_magic && orig_size &&
206+
tagged_size != orig_size) {
207+
uptr tail_size = tagged_size - orig_size - 1;
233208
CHECK_LT(tail_size, kShadowAlignment);
234209
void *tail_beg = reinterpret_cast<void *>(
235210
reinterpret_cast<uptr>(aligned_ptr) + orig_size);
236211
if (tail_size && internal_memcmp(tail_beg, tail_magic, tail_size))
237212
ReportTailOverwritten(stack, reinterpret_cast<uptr>(tagged_ptr),
238-
orig_size, tail_size, tail_magic);
213+
orig_size, tail_magic);
239214
}
240215

241216
meta->requested_size = 0;

compiler-rt/lib/hwasan/hwasan_checks.h

+29-4
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,29 @@ __attribute__((always_inline)) static void SigTrap(uptr p, uptr size) {
6161
// __builtin_unreachable();
6262
}
6363

64+
__attribute__((always_inline, nodebug)) static bool PossiblyShortTagMatches(
65+
tag_t mem_tag, uptr ptr, uptr sz) {
66+
tag_t ptr_tag = GetTagFromPointer(ptr);
67+
if (ptr_tag == mem_tag)
68+
return true;
69+
if (mem_tag >= kShadowAlignment)
70+
return false;
71+
if ((ptr & (kShadowAlignment - 1)) + sz > mem_tag)
72+
return false;
73+
#ifndef __aarch64__
74+
ptr = UntagAddr(ptr);
75+
#endif
76+
return *(u8 *)(ptr | (kShadowAlignment - 1)) == ptr_tag;
77+
}
78+
6479
enum class ErrorAction { Abort, Recover };
6580
enum class AccessType { Load, Store };
6681

6782
template <ErrorAction EA, AccessType AT, unsigned LogSize>
6883
__attribute__((always_inline, nodebug)) static void CheckAddress(uptr p) {
69-
tag_t ptr_tag = GetTagFromPointer(p);
7084
uptr ptr_raw = p & ~kAddressTagMask;
7185
tag_t mem_tag = *(tag_t *)MemToShadow(ptr_raw);
72-
if (UNLIKELY(ptr_tag != mem_tag)) {
86+
if (UNLIKELY(!PossiblyShortTagMatches(mem_tag, p, 1 << LogSize))) {
7387
SigTrap<0x20 * (EA == ErrorAction::Recover) +
7488
0x10 * (AT == AccessType::Store) + LogSize>(p);
7589
if (EA == ErrorAction::Abort)
@@ -85,15 +99,26 @@ __attribute__((always_inline, nodebug)) static void CheckAddressSized(uptr p,
8599
tag_t ptr_tag = GetTagFromPointer(p);
86100
uptr ptr_raw = p & ~kAddressTagMask;
87101
tag_t *shadow_first = (tag_t *)MemToShadow(ptr_raw);
88-
tag_t *shadow_last = (tag_t *)MemToShadow(ptr_raw + sz - 1);
89-
for (tag_t *t = shadow_first; t <= shadow_last; ++t)
102+
tag_t *shadow_last = (tag_t *)MemToShadow(ptr_raw + sz);
103+
for (tag_t *t = shadow_first; t < shadow_last; ++t)
90104
if (UNLIKELY(ptr_tag != *t)) {
91105
SigTrap<0x20 * (EA == ErrorAction::Recover) +
92106
0x10 * (AT == AccessType::Store) + 0xf>(p, sz);
93107
if (EA == ErrorAction::Abort)
94108
__builtin_unreachable();
95109
}
110+
uptr end = p + sz;
111+
uptr tail_sz = end & 0xf;
112+
if (UNLIKELY(tail_sz != 0 &&
113+
!PossiblyShortTagMatches(
114+
*shadow_last, end & ~(kShadowAlignment - 1), tail_sz))) {
115+
SigTrap<0x20 * (EA == ErrorAction::Recover) +
116+
0x10 * (AT == AccessType::Store) + 0xf>(p, sz);
117+
if (EA == ErrorAction::Abort)
118+
__builtin_unreachable();
119+
}
96120
}
121+
97122
} // end namespace __hwasan
98123

99124
#endif // HWASAN_CHECKS_H

compiler-rt/lib/hwasan/hwasan_flags.inc

-26
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,6 @@ HWASAN_FLAG(
3737
"HWASan allocator flag. max_malloc_fill_size is the maximal amount of "
3838
"bytes that will be filled with malloc_fill_byte on malloc.")
3939

40-
// Rules for malloc alignment on aarch64:
41-
// * If the size is 16-aligned, then malloc should return 16-aligned memory.
42-
// * Otherwise, malloc should return 8-alignment memory.
43-
// So,
44-
// * If the size is 16-aligned, we don't need to do anything.
45-
// * Otherwise we don't have to obey 16-alignment, just the 8-alignment.
46-
// * We may want to break the 8-alignment rule to catch more buffer overflows
47-
// but this will break valid code in some rare cases, like this:
48-
// struct Foo {
49-
// // accessed via atomic instructions that require 8-alignment.
50-
// std::atomic<int64_t> atomic_stuff;
51-
// ...
52-
// char vla[1]; // the actual size of vla could be anything.
53-
// }
54-
// Which means that the safe values for malloc_align_right are 0, 8, 9,
55-
// and the values 1 and 2 may require changes in otherwise valid code.
56-
57-
HWASAN_FLAG(
58-
int, malloc_align_right, 0, // off by default
59-
"HWASan allocator flag. "
60-
"0 (default): allocations are always aligned left to 16-byte boundary; "
61-
"1: allocations are sometimes aligned right to 1-byte boundary (risky); "
62-
"2: allocations are always aligned right to 1-byte boundary (risky); "
63-
"8: allocations are sometimes aligned right to 8-byte boundary; "
64-
"9: allocations are always aligned right to 8-byte boundary."
65-
)
6640
HWASAN_FLAG(bool, free_checks_tail_magic, 1,
6741
"If set, free() will check the magic values "
6842
"to the right of the allocated object "

0 commit comments

Comments
 (0)