[PATCH v4 1/2] malloc: Randomize the alignment of the thread heaps [BZ #22853]
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Mon Nov 9 19:29:31 GMT 2026
On 28/10/26 12:12, Hector Marco-Gisbert wrote:
> From: Hector Marco-Gisbert <hecmargi@upv.es>
>
> The heaps of the non-main arenas are HEAP_MAX_SIZE reservations aligned
> to HEAP_MAX_SIZE (64 MiB on 64-bit targets, 1 MiB on 32-bit targets), so
> that the heap_info of any chunk is found by masking the chunk address.
Thanks for picking this up again. The core of it looks right to me, but
there are some issue below.
> The alignment discards the low bits of the address that the kernel had
> randomized, so a thread heap keeps only the ASLR entropy above
> log2 (HEAP_MAX_SIZE): 14 of the 28 bits of mmap on x86_64, 4 of 18 on
> AArch64, and nothing at all on 32-bit targets (the heap is at one of two
> addresses on i386 and armhf with the default 8 bits of mmap entropy).
> This was reported in 2018 and the CVE-2019-1010025 was assigned to it.
>
> Instead of aligning the heaps to zero modulo HEAP_MAX_SIZE, align them
> to a secret, per-process residue arena_rnd: every heap of the process is
> placed at an address congruent to arena_rnd modulo HEAP_MAX_SIZE. The
> owner of a chunk is still found with a mask, now preceded and followed
> by the residue:
>
> heap = ((ptr - arena_rnd) & ~(HEAP_MAX_SIZE - 1)) | arena_rnd
>
> The allocation policy, the data structures and the layout inside a heap
> are unchanged; the cost is one static variable, a subtraction and an OR
> in heap_for_ptr, and two system calls when the library initializes.
>
> The residue is chosen in __ptmalloc_init, before any heap exists, from
> getrandom (with random_bits as fallback), so it is independent of the
> position of every other mapping of the process: a leaked thread heap
> address reveals of the libraries nothing that the original layout did
> not reveal, and vice versa. It is a multiple of the page size, or of
> the huge page size when the heaps are mapped with MAP_HUGETLB
> (glibc.malloc.hugetlb=2), which requires that alignment. With
> transparent huge pages (glibc.malloc.hugetlb=1) the heap base is only
> page aligned, so the accessible range of a heap is extended to the next
> huge page boundary (heap_prot_size) and the heaps keep being backed by
> huge pages. When address space randomization has been disabled for the
> process (ADDR_NO_RANDOMIZE personality, as set by setarch -R and by
> debuggers) the residue is zero and the layout is exactly the original
> one.
>
> With the change, the thread heaps have the same entropy as any other
> mmap of the process on every Linux target tested (x86_64, i386, AArch64,
> armhf, RISC-V 64, PPC64LE with 4 KiB and 64 KiB pages, and s390x), and a
> leak of a library address leaves log2 (HEAP_MAX_SIZE / pagesize) bits of
> a thread heap unknown: 14 on 64-bit targets with 4 KiB pages, 10 with
> 64 KiB pages, 8 on 32-bit targets.
>
> Compared with the v2 posted in March 2020
> <https://sourceware.org/pipermail/libc-alpha/2020-March/112127.html>:
> the residue comes from the kernel random source instead of from the
> address of a libc variable, which makes it independent of the ASLR of
> the architecture, as Adhemerval Zanella asked in the 2020 review
> <https://sourceware.org/pipermail/libc-alpha/2020-March/112074.html>;
> the layout with ASLR disabled is the original one; huge pages are
> supported; and the second copy of arena.c in libc_malloc_debug.so uses
> the public personality and getrandom interfaces.
>
> Signed-off-by: Hector Marco-Gisbert <hecmargi@upv.es>
> Signed-off-by: Ismael Ripoll <iripoll@disca.upv.es>
> ---
> NEWS | 6 ++
> malloc/arena.c | 153 +++++++++++++++++++++++++++++++++++++++++++------
> 2 files changed, 141 insertions(+), 18 deletions(-)
>
> diff --git a/NEWS b/NEWS
> index 1d6d0c8e02..2dd72d6e2a 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -9,6 +9,12 @@ Version 2.45
>
> Major new features:
>
> +* The heaps of the malloc thread arenas are no longer aligned to a fixed
> + boundary but to a secret per-process offset, so that they get the same
> + address space layout randomization as the other mappings of the process
> + (BZ #22853). Programs that disable address space randomization for the
> + process (setarch -R, debuggers) keep the previous layout.
The last sentence goes away if the personality check is removed (below).
> +
> * Additional optimized and correctly rounded mathematical functions have
> been imported from the CORE-MATH project, in particular cbrt.
>
> diff --git a/malloc/arena.c b/malloc/arena.c
> index d0f1003ad4..38b2f0439a 100644
> --- a/malloc/arena.c
> +++ b/malloc/arena.c
> @@ -18,6 +18,11 @@
>
> #include <stdbool.h>
> #include <setvmaname.h>
> +#include <not-cancel.h>
> +#include <random-bits.h>
> +#include <sysdep.h>
> +#include <sys/personality.h>
sys/personality.h only exists for Linux, so this breaks the Hurd build.
> +#include <sys/random.h>
>
> #define TUNABLE_NAMESPACE malloc
> #include <elf/dl-tunables.h>
> @@ -64,7 +69,8 @@ heap_max_size (void)
>
> /* A heap is a single contiguous memory region holding (coalesceable)
> malloc_chunks. It is allocated with mmap() and always starts at an
> - address aligned to HEAP_MAX_SIZE. */
> + address congruent to arena_rnd modulo HEAP_MAX_SIZE (randomized
> + arena alignment, see heap_for_ptr). */
>
> typedef struct _heap_info
> {
> @@ -138,13 +144,56 @@ __libc_lock_define_initialized (static, list_lock);
> ptr = arena_get2 ((size), NULL); \
> } while (0)
>
> +/* Randomized arena alignment. Heaps are not aligned to HEAP_MAX_SIZE
> + but placed at a secret, per-process offset arena_rnd from the
> + alignment boundary. All heaps share the offset, so the heap that
> + owns a pointer is still found with a mask: subtract the offset,
> + align down, add the offset back. The offset is taken from the
> + kernel random source (see __ptmalloc_init), so it restores to the
> + thread heaps the entropy that the alignment would discard, and it
> + is zero when ASLR has been disabled for the process
> + (ADDR_NO_RANDOMIZE), which keeps the original layout. [BZ #22853] */
> +static uintptr_t arena_rnd;
> +
> +/* True when address space randomization has been disabled for this
> + process, in which case the arenas keep their original, deterministic
> + layout. */
> +static bool
> +arena_aslr_disabled (void)
> +{
> +#if IS_IN (libc) && defined __NR_personality
> + int persona = INTERNAL_SYSCALL_CALL (personality, 0xffffffff);
> +#elif defined __linux__
> + /* libc_malloc_debug.so and other users outside libc.so go through
> + the public interfaces. */
> + int persona = personality (0xffffffff);
> +#else
> + int persona = -1;
> +#endif
> + return persona >= 0 && (persona & ADDR_NO_RANDOMIZE) != 0;
> +}
I think we should remove this, and always use the random residue. Nothing in
the scheme depends on the residue being zero when ASLR is off, also:
* It adds a system call to the start-up of every process, threaded
or not, before main.
* personality is not universally allowed by seccomp filters. In systemd,
personality is only in the @system-service group, not in @default or
@process.
* It is not consistent with what we do elsewhere. The stack protector
canary and the tcache key are random under ADDR_NO_RANDOMIZE as well,
and the patch does not honor kernel.randomize_va_space=0 either.
If someone needs reproducible thread heaps, a tunable can be added
later; it is cheaper than a system call in every process.
> +
> +/* Fill *RND with random bits from the kernel; false if it fails. */
> +static bool
> +arena_get_random (uintptr_t *rnd)
> +{
> +#if IS_IN (libc)
> + return (__getrandom_nocancel_nostatus_direct (rnd, sizeof (*rnd),
> + GRND_NONBLOCK)
> + == sizeof (*rnd));
> +#else
> + return getrandom (rnd, sizeof (*rnd), GRND_NONBLOCK) == sizeof (*rnd);
> +#endif
> +}
> +
The libc_malloc_debug.so never creates a thread heap, malloc_check only
works on main_arena, and new_heap is not even linked into it. The
residue is dead code there.
Also, there is no need for a second getrandom call. The
tcache_key_initialize already does one in __ptmalloc_init, so read
two words there and pass one to each. Also, do not derive the residue from
tcache_key itself, since the key is stored in every chunk in the
tcache. Something like:
#if IS_IN (libc)
/* One getrandom call provides the tcache key and the heap offset
(arena_rnd). They must not share bits, the tcache key is stored
in every chunk in the tcache. We need to use the _nostatus
version here, see BZ 29624. */
uintptr_t rnd[2];
if (__getrandom_nocancel_nostatus_direct (rnd, sizeof (rnd),
GRND_NONBLOCK)
!= sizeof (rnd))
{
rnd[0] = 0;
rnd[1] = random_bits ();
# if __WORDSIZE == 64
rnd[1] = (rnd[1] << 32) | random_bits ();
# endif
}
#endif
#if USE_TCACHE
tcache_key_initialize (rnd[0]);
#endif
with tcache_key_initialize taking the key as an argument (its existing
fallback loop already handles 0), and at the end:
#if IS_IN (libc)
size_t align = GLRO (dl_pagesize);
if (mp_.hp_pagesize > align && mp_.hp_pagesize <= heap_max_size ())
align = mp_.hp_pagesize;
arena_rnd = rnd[1] & (heap_max_size () - 1) & ~(align - 1);
#endif
> /* find the heap and corresponding arena for a given ptr */
>
> static __always_inline heap_info *
> heap_for_ptr (void *ptr)
> {
> size_t max_size = heap_max_size ();
> - return PTR_ALIGN_DOWN (ptr, max_size);
> + return (heap_info *) ((((uintptr_t) ptr - arena_rnd) & ~(max_size - 1))
> + | arena_rnd);
> }
>
> static __always_inline struct malloc_state *
> @@ -293,6 +342,37 @@ __ptmalloc_init (void)
> do_set_mmap_threshold (mp_.hp_pagesize);
> __always_fail_morecore = true;
> }
> +
> + /* Randomized arena alignment: choose the per-process heap offset. The
> + random bits come from the kernel (getrandom) and are therefore
> + independent of the position of every other mapping of the process;
> + a thread heap address reveals nothing about the libraries beyond
> + what the original layout revealed, and vice versa. When ASLR has
> + been disabled for the process (ADDR_NO_RANDOMIZE personality, as set
> + by setarch -R and by debuggers) the offset is zero and the layout is
> + exactly the original one. The offset is a multiple of the system
> + page size, or of the huge page size when heaps are mapped with
> + MAP_HUGETLB (glibc.malloc.hugetlb=2), which requires that alignment;
> + transparent huge pages (hugetlb=1) only need page alignment (madvise)
> + and do not reduce it. This runs before any heap is created, so no
> + synchronization is needed. */
"transparent huge pages only need page alignment and do not reduce it"
is true for growing the heap, but not for trimming it; see heap_trim
below.
> + {
> + size_t pagesize = GLRO (dl_pagesize);
> + uintptr_t rnd = 0;
> + if (!arena_aslr_disabled ())
> + {
> + if (!arena_get_random (&rnd))
> + {
> + rnd = random_bits ();
> +#if __WORDSIZE == 64
> + rnd = (rnd << 32) | random_bits ();
> +#endif
> + }
> + }
> + if (mp_.hp_pagesize > pagesize && mp_.hp_pagesize <= heap_max_size ())
> + pagesize = mp_.hp_pagesize;
> + arena_rnd = rnd & (heap_max_size () - 1) & ~(pagesize - 1);
> + }
> }
>
> /* Managing heaps and arenas (for concurrent threads) */
> @@ -333,14 +413,30 @@ dump_heap (heap_info *heap)
>
> /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) calls return decreasing
> addresses as opposed to increasing, new_heap would badly fragment the
> - address space. In that case remember the second HEAP_MAX_SIZE part
> - aligned to HEAP_MAX_SIZE from last mmap (0, HEAP_MAX_SIZE << 1, ...)
> - call (if it is already aligned) and try to reuse it next time. We need
> - no locking for it, as kernel ensures the atomicity for us - worst case
> - we'll call mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in
> - multiple threads, but only one will succeed. */
> + address space. In that case remember the second HEAP_MAX_SIZE part,
> + at offset arena_rnd from the HEAP_MAX_SIZE boundary, of the last
> + mmap (0, HEAP_MAX_SIZE << 1, ...) call (if it already has that offset)
> + and try to reuse it next time. We need no locking for it, as kernel
> + ensures the atomicity for us - worst case we'll call
> + mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in multiple
> + threads, but only one will succeed. */
> static char *aligned_heap_area;
>
> +/* Size of the range of a heap at BASE that must be accessible for the
> + first SIZE bytes to be usable: SIZE itself, or, when PAGESIZE is a
> + huge page size, SIZE extended to the next absolute PAGESIZE boundary
> + (bounded by MAX_SIZE), so that transparent huge pages can back a heap
> + whose base is only page aligned (randomized arena alignment). */
> +static __always_inline size_t
> +heap_prot_size (char *base, size_t size, size_t pagesize, size_t max_size)
> +{
This handles growth, but heap_trim still releases memory in units of
heap->pagesize relative to the heap base:
extra = ALIGN_DOWN(top_area - pad, heap->pagesize);
With an unaligned base, every MADV_DONTNEED of the trim starts and ends in
the middle of a huge page, so each released 2 MiB splits two huge pages.
This adds extra runtime cost because khugepaged collapses them back once
the process goes idle.
And this is not only about an opt-in tunable because now AArch64 defaults
to hugetlb=1 (MALLOC_DEFAULT_THP_PAGESIZE is 2 MiB in
sysdeps/unix/sysv/linux/aarch64/hugepages.h). So it would apply to
every multi-threaded program there.
The simplest fix would be to align the residue to thp_pagesize as you
do for hp_pagesize, but that costs entropy. On AArch64 with 4 KiB pages
it would keep only 5 of the 14 residue bits by default. I think the
better way is to keep the page granular residue and put the end
of the heap, not only the protected range, on absolute huge page
boundaries.
I also think there is a related problem with glibc.mem.decorate_maps=1,
where alloc_new_heap names only the first SIZE bytes. So with this
change the named VMA ends in the middle of a huge page.
> + if (pagesize <= GLRO (dl_pagesize))
> + return size;
> + size_t prot_size = ALIGN_UP ((uintptr_t) base + size, pagesize)
> + - (uintptr_t) base;
> + return prot_size > max_size ? max_size : prot_size;
> +}
> +
> /* Create a new heap. size is automatically rounded up to a multiple
> of the page size. */
>
> @@ -373,7 +469,8 @@ alloc_new_heap (size_t size, size_t top_pad, size_t pagesize,
> {
> p2 = (char *) MMAP (aligned_heap_area, max_size, PROT_NONE, mmap_flags);
> aligned_heap_area = NULL;
> - if (p2 != MAP_FAILED && ((unsigned long) p2 & (max_size - 1)))
> + if (p2 != MAP_FAILED
> + && ((uintptr_t) p2 & (max_size - 1)) != arena_rnd)
> {
> __munmap (p2, max_size);
> p2 = MAP_FAILED;
> @@ -386,6 +483,11 @@ alloc_new_heap (size_t size, size_t top_pad, size_t pagesize,
> {
> p2 = (char *) (((uintptr_t) p1 + (max_size - 1))
> & ~(max_size - 1));
> + /* Move to the secret residue; step back one heap if that
> + would leave the end of the heap outside the mapping. */
> + p2 = (char *) ((uintptr_t) p2 | arena_rnd);
> + if (p1 + max_size <= p2)
> + p2 -= max_size;
> ul = p2 - p1;
> if (ul)
> __munmap (p1, ul);
> @@ -396,19 +498,29 @@ alloc_new_heap (size_t size, size_t top_pad, size_t pagesize,
> else
> {
> /* Try to take the chance that an allocation of only max_size
> - is already aligned. */
> + is already aligned to the secret residue. */
> p2 = (char *) MMAP (NULL, max_size, PROT_NONE, mmap_flags);
> if (p2 == MAP_FAILED)
> return NULL;
>
> - if ((unsigned long) p2 & (max_size - 1))
> + if (((uintptr_t) p2 & (max_size - 1)) != arena_rnd)
> {
> __munmap (p2, max_size);
> return NULL;
> }
> }
> }
> - if (__mprotect (p2, size, extra_mmap_prot | PROT_READ | PROT_WRITE) != 0)
> + /* With randomized arena alignment the heap base is only page aligned
> + (unless MAP_HUGETLB is used). When the heap page size is a huge page
> + size (transparent huge pages), make accessible the whole range up to
> + the next absolute boundary of that size, within the reservation, so
> + that every huge page sized range the heap grows into is entirely
> + accessible before it is first touched; otherwise the kernel cannot
> + back it with a huge page. heap->size, which bounds the chunks, is
> + not changed; only the protected range is larger. */
> + size_t prot_size = heap_prot_size (p2, size, pagesize, max_size);
> + if (__mprotect (p2, prot_size, extra_mmap_prot | PROT_READ | PROT_WRITE)
> + != 0)
> {
> __munmap (p2, max_size);
> return NULL;
> @@ -417,11 +529,11 @@ alloc_new_heap (size_t size, size_t top_pad, size_t pagesize,
> /* Only consider the actual usable range. */
> __set_vma_name (p2, size, " glibc: malloc arena");
>
> - madvise_thp (p2, size);
> + madvise_thp (p2, prot_size);
>
> h = (heap_info *) p2;
> h->size = size;
> - h->mprotect_size = size;
> + h->mprotect_size = prot_size;
> h->pagesize = pagesize;
> LIBC_PROBE (memory_heap_new, 2, h, h->size);
> return h;
> @@ -460,15 +572,17 @@ grow_heap (heap_info *h, long diff)
>
> if ((unsigned long) new_size > h->mprotect_size)
> {
> + size_t prot_size = heap_prot_size ((char *) h, new_size, pagesize,
> + max_size);
> if (__mprotect ((char *) h + h->mprotect_size,
> - (unsigned long) new_size - h->mprotect_size,
> + prot_size - h->mprotect_size,
> extra_mmap_prot | PROT_READ | PROT_WRITE) != 0)
> return -2;
>
> - h->mprotect_size = new_size;
> + h->mprotect_size = prot_size;
> }
>
> - madvise_thp (h, new_size);
> + madvise_thp (h, h->mprotect_size);
>
> h->size = new_size;
> LIBC_PROBE (memory_heap_more, 2, h, h->size);
> @@ -546,7 +660,10 @@ heap_trim (heap_info *heap, size_t pad)
> p = prev_chunk (p);
> unlink_chunk (ar_ptr, p);
> }
> - assert (((unsigned long) ((char *) p + new_size) & (heap->pagesize - 1))
> + /* With randomized arena alignment the heap base is only page
> + aligned, so check the heap size, not the absolute address, in
> + units of the heap page size. */
> + assert ((((char *) p + new_size - (char *) heap) & (heap->pagesize - 1))
> == 0);
> assert (((char *) p + new_size) == ((char *) heap + heap->size));
> top (ar_ptr) = top_chunk = p;
With the change above, the end of a heap is on an absolute boundary again,
except for a heap that fills its reservation.
Finally, as this is contributed under DCO, please add "Copyright The
GNU Toolchain Authors." to the header of arena.c (see the Contribution
checklist).
More information about the Libc-alpha
mailing list