Skip to content

Commit 9e2a22d

Browse files
Andy Heffernantbarbette
authored andcommitted
HashAllocator: poison freed blocks
When the Click build is configured with the option --enable-hash-allocator-poisoning, this change will cause the HashAllocator to write a "poison" byte value to the block being returned to a HashAllocator pool. This ensures that when a stale reference to a freed block is followed, the code will be much less likely to interpret the block as a valid object or struct. In particular, pointer values will be non-NULL but bad, leading to immediate failure with a clear signature indicating the presence of a stale reference bug. Signed-off-by: Andy Heffernan <[email protected]>
1 parent 5c77c0b commit 9e2a22d

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

config-linuxmodule.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@
178178
/* Define to 1 if Linux defines the type 'uintptr_t'. */
179179
#undef HAVE_UINTPTR_T_LINUXMODULE
180180

181+
/* Define to 1 to enable poisoning of freed HashAllocator blocks. */
182+
#undef HAVE_HASH_ALLOCATOR_POISONING
183+
181184
/* The size of a `click_jiffies_t', as computed by sizeof. */
182185
#define SIZEOF_CLICK_JIFFIES_T SIZEOF_LONG
183186

config-userlevel.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@
316316
/* Define if you have the <valgrind/memcheck.h> header file. */
317317
#undef HAVE_VALGRIND_MEMCHECK_H
318318

319+
/* Define to 1 to enable poisoning of freed HashAllocator blocks. */
320+
#undef HAVE_HASH_ALLOCATOR_POISONING
321+
319322
/* Define if you have the vsnprintf function. */
320323
#undef HAVE_VSNPRINTF
321324

configure.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,11 @@ if test "$value" != 0; then
14821482
AC_DEFINE_UNQUOTED([CLICK_DEBUG_SCHEDULING], [$value], [Define to enable debugging support for Click scheduling.])
14831483
fi
14841484

1485+
AC_ARG_ENABLE(hash-allocator-poisoning, [ --enable-hash-allocator-poisoning enable HashAllocator block poisoning], :, enable_hash_allocator_poisoning=no)
1486+
if test $enable_hash_allocator_poisoning = yes; then
1487+
AC_DEFINE(HAVE_HASH_ALLOCATOR_POISONING)
1488+
fi
1489+
14851490

14861491
dnl Compile for the native architecture
14871492
AC_ARG_ENABLE(portable-binary, [AS_HELP_STRING([--enable-portable-binary], [disable compiler optimizations that would produce unportable binaries])],

include/click/hashallocator.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class HashAllocator { public:
2323

2424
private:
2525

26+
#if HAVE_HASH_ALLOCATOR_POISONING
27+
// Freed blocks are poisoned with this byte value.
28+
static const uint8_t poison_byte = 0x0d;
29+
#endif
30+
2631
struct link {
2732
link *next;
2833
};
@@ -91,6 +96,9 @@ inline void *HashAllocator::allocate()
9196
inline void HashAllocator::deallocate(void *p)
9297
{
9398
if (p) {
99+
#if HAVE_HASH_ALLOCATOR_POISONING
100+
memset(p, poison_byte, _size);
101+
#endif
94102
reinterpret_cast<link *>(p)->next = _free;
95103
_free = reinterpret_cast<link *>(p);
96104
#ifdef VALGRIND_MEMPOOL_FREE

0 commit comments

Comments
 (0)