As mentioned there, I'm opening this issue to discuss another finding I investigated following static code analysis of hardened_malloc.
If we can reach the following definition in get_large_size_class() with 0 < size < 5, we trigger either an undefined behaviour (call to __builtin_clzl(0)) or an underflow of spacing_shift:
size_t spacing_shift = 64 - __builtin_clzl(size - 1) - 3;
Then, in the underflow case and on the next line, spacing_shift would be greater than the width of the left operand, which is an undefined behaviour in C:
size_t spacing_class = 1ULL << spacing_shift;
I've been able to hit this via two different paths, with SLAB_CANARY set to false:
alloc_aligned(), for instance from a call to h_aligned_alloc(8192, 2);
h_free_sized(p, expected_size) called with 0 < expected_size < 5 and p pointing outside the slab region.
As mentioned there, I'm opening this issue to discuss another finding I investigated following static code analysis of hardened_malloc.
If we can reach the following definition in
get_large_size_class()with0 < size < 5, we trigger either an undefined behaviour (call to__builtin_clzl(0)) or an underflow ofspacing_shift:Then, in the underflow case and on the next line,
spacing_shiftwould be greater than the width of the left operand, which is an undefined behaviour in C:I've been able to hit this via two different paths, with
SLAB_CANARYset to false:alloc_aligned(), for instance from a call toh_aligned_alloc(8192, 2);h_free_sized(p, expected_size)called with0 < expected_size < 5andppointing outside the slab region.