57 Commits

Author SHA1 Message Date
Vitaly Buka
43aa6e6bf3
[hwasan] Fixing false invalid-free with disabled tagging (#67169)
This problem was accidentally discovered by the internal symbolizer, but
it's relevant for external one as well, see the test.

If we just disable tagging, there may still be tagged allocations that
have already been freed. After disabling tagging, these tagged
allocations can be released to the user as-is, which would later break
the "invalid-free" check.

We cannot just disable the "invalid-free" check with disabled tagging,
because if we re-enable tagging, the issue still applies to allocations
created when it was disabled.

The fix is to continue tagging with zero even if tagging is disabled.

This makes the "disabled" mode less efficient, but this is not the
primary use case.
2023-09-22 13:35:35 -07:00
Vitaly Buka
b55f05a3f2 [NFC][hwasan] Don't tag secondary allocations on free
OnUnmap will tag it to zero anyway.

Reviewed By: fmayer

Differential Revision: https://reviews.llvm.org/D156282
2023-07-26 13:36:10 -07:00
Jin Xin Ng
3ab36712da
[hwasan] RunMallocHooks with orig_size
This matches behaviour of asan. sanitizer_common/TestCases/malloc_hook.cpp
should've caught this- but hwasan was on XFAIL.

Differential Revision: https://reviews.llvm.org/D151580
2023-05-31 18:19:30 +00:00
Jin Xin Ng
7639265af4
[sanitizer] Implement __sanitizer_get_allocated_size_fast
The primary motivation for this change is to allow FreeHooks to obtain
the allocated size of the pointer being freed in a fast, efficient manner.

Differential Revision: https://reviews.llvm.org/D151360
2023-05-26 00:19:47 +00:00
Vitaly Buka
d9b574c312 [hwasan] Fix allocator_interface implementation
__sanitizer_get_current_allocated_bytes had as body, but allocator
caches were not registered to collect stats. It's done by
SizeClassAllocator64LocalCache::Init().

Reviewed By: thurston

Differential Revision: https://reviews.llvm.org/D151389
2023-05-24 18:05:42 -07:00
Vitaly Buka
fa58f32754 [NFC][HWASAN] Rename AllocatorSwallowThreadLocalCache 2023-05-24 13:27:47 -07:00
Jin Xin Ng
c1903ba5ea
[hwasan] Move RunFreeHooks call
Ensures a subsequent call (via an external caller) to
__sanitizer_get_allocated_size via hooks will return a valid size.

This allows a faster version of __sanitizer_get_allocated_size
to be implemented, which can skip checks.

Test to ensure RunFreeHooks' call order will come with
__sanitizer_get_allocated_size_fast

Differential Revision: https://reviews.llvm.org/D151151
2023-05-23 17:02:51 +00:00
Vitaly Buka
82b1e3b412 [HWASAN] Use InTaggableRegion in basic tagging functions
For primary use-case when !HWASAN_ALIASING_MODE the function is constant
true and should be eliminated by optimizations.

In case HWASAN_ALIASING_MODE all new calls to the functions were missing
in the first place. We just not use this mode for anything but tests, so
we didn't noticed.

Addressing @thurston comment on D149293

Reviewed By: thurston

Differential Revision: https://reviews.llvm.org/D149305
2023-04-26 23:42:04 -07:00
Vitaly Buka
fd85a6df1d [NFC][HWASAN] Move InTaggableRegion
Prepare to use it in all tag related functions.

Reviewed By: thurston

Differential Revision: https://reviews.llvm.org/D149304
2023-04-26 19:52:21 -07:00
Vitaly Buka
7ac72cea0e [hwasan] Implement __sanitizer_purge_allocator
Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D149241
2023-04-26 19:16:40 -07:00
Vitaly Buka
66f162a667 [HWASAN] Fix __sanitizer_get_allocated_{begin,size}
HWASAN_ALIASING_MODE needs to untag only
primary allocator pointers.

Reviewed By: kstoimenov, thurston

Differential Revision: https://reviews.llvm.org/D149238
2023-04-26 18:55:44 -07:00
Vitaly Buka
4c83674679 [HWASAN] Fix HwasanReallocate in aliasing mode
HWASAN_ALIASING_MODE needs to untag only
primary allocator pointers.

Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D149293
2023-04-26 18:48:30 -07:00
Leonard Chan
82e5994c8d [hwasan] Do not memset allocation if it comes from the secondary allocator
The secondary allocator calls mmap which should return zero-inited pages, so we
don't need to explicitly memset it with zeros. This is similar to what asan's
calloc does.

Differential Revision: https://reviews.llvm.org/D149285
2023-04-26 21:49:42 +00:00
Fangrui Song
8c63dc6f6d Internalize AllocationBegin functions after D147005
Reviewed By: thurston

Differential Revision: https://reviews.llvm.org/D148195
2023-04-13 10:28:58 -07:00
Vitaly Buka
39c0602414 [lsan] Rename IgnoreObjectLocked into IgnoreObject
We don't lock allocator for this call.
2023-04-05 16:17:44 -07:00
Vitaly Buka
7d32866869 [hwasan] Use GetBlockBegin instead of GetBlockBeginFastLocked
Asan and lsan use non-locked version as well.
IgnoreObjectLocked name is missleading.
2023-04-05 16:17:44 -07:00
Enna1
dc7498eb02 [hwasan] Record allocation thread id in HeapAllocationRecord
Extend HeapAllocationRecord to record allocation thread id, print thread id in memory allocation stack trace.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D147215
2023-04-04 11:25:18 +08:00
Thurston Dang
d644ab022a Update __sanitizer_get_allocated_begin to return const void*
D147005 introduced __sanitizer_get_allocated_begin, with a return
value of void*. This involved a few naughty casts that dropped the
const. This patch adds back the const qualifier.

Differential Revision: https://reviews.llvm.org/D147489
2023-04-04 00:43:36 +00:00
Thurston Dang
415b1cfd57 Add __sanitizer_get_allocated_begin API and implementations
This function will return the start of the allocation, if given a pointer that lies within an allocation. Otherwise, it returns NULL.

It will be useful for detecting dynamic TLS allocations in glibc >=2.25, which
uses malloc (see https://github.com/google/sanitizers/issues/1409#issuecomment-1214244142).

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D147005
2023-04-03 21:27:51 +00:00
Kirill Stoimenov
d87468e56c [HWASAN] Add support for max_allocation_size_mb option
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143667
2023-02-09 20:24:47 +00:00
Kirill Stoimenov
9de144eee5 [HWASAN] Remove GetRequestedSize check from Metadata::IsAllocated
After D143438 there is no point in this check as the size will never 0. Also this keeps it consistent with ASAN where there is not size check in LsanMetadata::allocated.

Reviewed By: MaskRay, vitalybuka

Differential Revision: https://reviews.llvm.org/D143442
2023-02-07 15:51:56 +00:00
Kirill Stoimenov
e403fd8705 [HWASAN] Add unlikely to if in HwasanAllocate. 2023-02-07 00:39:17 +00:00
Kirill Stoimenov
c6ea5b0cd1 [HWASAN] Modify HwasanAllocate to set the size to 1 if requested size is 0
This should keep it consistent with LSAN and ASAN,

Reviewed By: vitalybuka, MaskRay

Differential Revision: https://reviews.llvm.org/D143438
2023-02-07 00:01:20 +00:00
Kirill Stoimenov
914f86949a [HWASAN] Fix Metadata::IsAllocatedMetadata::IsAllocated to return true even if the requested size is 0.
Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D143438
2023-02-06 23:47:23 +00:00
Kirill Stoimenov
eb3be66028 [LSAN] Add GetUserAddr function which returns the user visible address of an internal pointer
For HWASAN this would be the tagged address. It is the same pointer when pointer tagging is not used. Coincidently this also fixes some test which rely on comparing pointers.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143121
2023-02-02 00:39:00 +00:00
Kirill Stoimenov
2b0322ed56 [HWASAN] Fix PointsIntoChunk to untag pointers. Also added some checks where we know that the pointer should be untagged.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D142389
2023-01-24 00:35:17 +00:00
Kirill Stoimenov
9545580447 [HWASAN] Remove FindHeapChunkByAddressFastLocked
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D142042
2023-01-18 23:33:11 +00:00
Vitaly Buka
0e08a85429 [HWASA] Restore LsanMetadata from D141642 Diff5
Unlike asan, we can't use pointer arithmetics to get from user ptr to
metadata. Asan does not use CombinedAllocator::GetMetadata and store
metadata next to the user data.
2023-01-17 23:45:51 -08:00
Vitaly Buka
c804775cd3 [hwasan] Fix LsanMetadata::LsanMetadata 2023-01-17 19:23:42 -08:00
Kirill Stoimenov
e022ca8b6e [HWASAN] Implemented LSAN SetLsanTag and IgnoreObjectLocked
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D141642
2023-01-17 17:59:17 -08:00
Kirill Stoimenov
a3587ea6d1 [HWASAN][NFC] Added implementation for the most of the allocator related functions from the LSAN interface.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D139964
2022-12-13 21:38:13 +00:00
Kirill Stoimenov
1b0d63c506 [HWASAN] Implemented LsanMetadata in HWASAN.
Please note that that this code is not used. It will be connected at some point when the rest of the code is ready.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D139464
2022-12-13 17:05:15 +00:00
Kirill Stoimenov
ee56d88b02 [HWASAN][NFC] Renamed [g|s]et_requested_size to [G|S]etRequestedSize.
Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D139727
2022-12-12 19:23:19 +00:00
Kirill Stoimenov
ab658a4847 [HWASAN] Removed right_aligned from HWASAN allocator Metadata.
This came up during review of D139464. Looks like this filed is always set to false to it is basically unused.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D139654
2022-12-08 19:55:39 +00:00
Vitaly Buka
12e137ab24 [NFC][sanitizer] Consolidate malloc hook invocations 2022-04-12 20:08:29 -07:00
Vitaly Buka
b84673b3f4 [NFC][sanitizer] Remove unnececary HOOK macros 2022-04-12 19:47:44 -07:00
Vitaly Buka
6318001209 [sanitizer] Support IsRssLimitExceeded in all sanitizers
Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D115000
2021-12-03 12:45:44 -08:00
Florian Mayer
023f18bbaf [hwasan] do not check if freed pointer belonged to allocator.
In that case it is very likely that there will be a tag mismatch anyway.

We handle the case that the pointer belongs to neither of the allocators
by getting a nullptr from allocator.GetBlockBegin.

Reviewed By: hctim, eugenis

Differential Revision: https://reviews.llvm.org/D108383
2021-08-25 09:31:01 +01:00
Mitch Phillips
433b2eaf91 [hwasan] Always untag short granule in shadow.
Fixes a regression when the allocator is disabled, and a dirty
allocation is re-used. This only occurs when the allocator is disabled,
so a test-only fix, but still necessary.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D108650
2021-08-24 14:10:04 -07:00
Florian Mayer
de916a7b12 Revert "[hwasan] do not check if freed pointer belonged to allocator."
This reverts commit 119146f8ae25c31ea630a15761a6fba6b7eb909c.
2021-08-20 12:21:00 +01:00
Florian Mayer
119146f8ae [hwasan] do not check if freed pointer belonged to allocator.
In that case it is very likely that there will be a tag mismatch anyway.

We handle the case that the pointer belongs to neither of the allocators
by getting a nullptr from allocator.GetBlockBegin.

Reviewed By: hctim, eugenis

Differential Revision: https://reviews.llvm.org/D108383
2021-08-20 10:12:47 +01:00
Mitch Phillips
fd51ab6341 [hwasan] Don't report short-granule shadow as overwritten.
The shadow for a short granule is stored in the last byte of the
granule. Currently, if there's a tail-overwrite report (a
buffer-overflow-write in uninstrumented code), we report the shadow byte
as a mismatch against the magic.

Fix this bug by slapping the shadow into the expected value. This also
makes sure that if the uninstrumented WRITE does clobber the shadow
byte, it reports the shadow was actually clobbered as well.

Reviewed By: eugenis, fmayer

Differential Revision: https://reviews.llvm.org/D107938
2021-08-18 11:25:57 -07:00
Evgenii Stepanov
8a570a873b [hwasan] Support malloc in atfork.
Before this change we were locking the StackDepot in the fork()
interceptor. This results in a deadlock when allocator functions are
used in a pthread_atfork() callback.

Instead, set up a pthread_atfork() callback at init that locks/unlocks
both StackDepot and the allocator. Since our callback is set up very
early, the pre-fork callback is executed late, and both post-fork ones
are executed early, which works perfect for us.

Differential Revision: https://reviews.llvm.org/D108063
2021-08-17 15:29:49 -07:00
Florian Mayer
bae9527c20 [hwasan] Add report for wild frees.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D107577
2021-08-13 09:04:48 +01:00
Leonard Chan
189c552518 [compiler-rt][hwasan] Refactor kAliasRegionStart usage
This moves logic for setting kAliasRegionStart into hwasan_allocator.cpp
so other platforms that do not support aliasing mode will not need to define
kAliasRegionStart.

Differential Revision: https://reviews.llvm.org/D105725
2021-07-12 16:33:05 -07:00
Florian Mayer
ad8494c021 [hwasan] Make sure we retag with a new tag on free.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D105021
2021-06-30 11:13:38 +01:00
Florian Mayer
18070723ef [hwasan] Do not use short granule tags as poison tags.
Short granule tags as poison cause a UaF to read the referenced
memory to retrieve the tag, and means we do not detect the UaF
if the last granule's tag is still around.

This only increases the change of not catching a UaF from
0.39 % (1 / 256) to 0.42 % (1 / (256 - 17)).

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D104304
2021-06-17 11:59:37 +01:00
Matt Morehouse
96a4167b4c [HWASan] Use page aliasing on x86_64.
Userspace page aliasing allows us to use middle pointer bits for tags
without untagging them before syscalls or accesses.  This should enable
easier experimentation with HWASan on x86_64 platforms.

Currently stack, global, and secondary heap tagging are unsupported.
Only primary heap allocations get tagged.

Note that aliasing mode will not work properly in the presence of
fork(), since heap memory will be shared between the parent and child
processes.  This mode is non-ideal; we expect Intel LAM to enable full
HWASan support on x86_64 in the future.

Reviewed By: vitalybuka, eugenis

Differential Revision: https://reviews.llvm.org/D98875
2021-03-25 07:04:14 -07:00
Matt Morehouse
c8ef98e5de Revert "[HWASan] Use page aliasing on x86_64."
This reverts commit 63f73c3eb9716256ab8dbb868e16d08a88636cba due to
breakage on aarch64 without TBI.
2021-03-24 16:18:29 -07:00
Matt Morehouse
63f73c3eb9 [HWASan] Use page aliasing on x86_64.
Userspace page aliasing allows us to use middle pointer bits for tags
without untagging them before syscalls or accesses.  This should enable
easier experimentation with HWASan on x86_64 platforms.

Currently stack, global, and secondary heap tagging are unsupported.
Only primary heap allocations get tagged.

Note that aliasing mode will not work properly in the presence of
fork(), since heap memory will be shared between the parent and child
processes.  This mode is non-ideal; we expect Intel LAM to enable full
HWASan support on x86_64 in the future.

Reviewed By: vitalybuka, eugenis

Differential Revision: https://reviews.llvm.org/D98875
2021-03-24 11:43:41 -07:00