Skip to content

Commit

Permalink
[scudo] Allow for weak hooks, gated by a define
Browse files Browse the repository at this point in the history
Summary:
Hooks in the allocation & deallocation paths can be a security risk (see for an
example https://scarybeastsecurity.blogspot.com/2016/11/0day-exploit-advancing-exploitation.html
which used the glibc's __free_hook to complete exploitation).

But some users have expressed a need for them, even if only for tests and
memory benchmarks. So allow for `__sanitizer_malloc_hook` &
`__sanitizer_free_hook` to be called if defined, and gate them behind a global
define `SCUDO_CAN_USE_HOOKS` defaulting to 0.

Reviewers: alekseyshl

Reviewed By: alekseyshl

Subscribers: #sanitizers, llvm-commits

Differential Revision: https://reviews.llvm.org/D42430

llvm-svn: 323278
  • Loading branch information
Kostya Kortchinsky committed Jan 23, 2018
1 parent cbce2f0 commit 1ebebde
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler-rt/lib/scudo/scudo_allocator.cpp
Expand Up @@ -430,7 +430,8 @@ struct ScudoAllocator {
}
void *Ptr = reinterpret_cast<void *>(UserPtr);
Chunk::storeHeader(Ptr, &Header);
// if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(Ptr, Size);
if (SCUDO_CAN_USE_HOOKS && &__sanitizer_malloc_hook)
__sanitizer_malloc_hook(Ptr, Size);
return Ptr;
}

Expand Down Expand Up @@ -480,7 +481,8 @@ struct ScudoAllocator {
// the TLS destructors, ending up in initialized thread specific data never
// being destroyed properly. Any other heap operation will do a full init.
initThreadMaybe(/*MinimalInit=*/true);
// if (&__sanitizer_free_hook) __sanitizer_free_hook(Ptr);
if (SCUDO_CAN_USE_HOOKS && &__sanitizer_free_hook)
__sanitizer_free_hook(Ptr);
if (UNLIKELY(!Ptr))
return;
if (UNLIKELY(!Chunk::isAligned(Ptr))) {
Expand Down
6 changes: 6 additions & 0 deletions compiler-rt/lib/scudo/scudo_platform.h
Expand Up @@ -55,6 +55,12 @@
# define SCUDO_CAN_USE_PUBLIC_INTERFACE 1
#endif

// Hooks in the allocation & deallocation paths can become a security concern if
// implemented improperly, or if overwritten by an attacker. Use with caution.
#ifndef SCUDO_CAN_USE_HOOKS
# define SCUDO_CAN_USE_HOOKS 0
#endif

namespace __scudo {

#if SANITIZER_CAN_USE_ALLOCATOR64
Expand Down

0 comments on commit 1ebebde

Please sign in to comment.