Memory Tagging and Hardened Allocators
The mitigations so far protect control data: return addresses, function pointers, the GOT. Most memory bugs, though, are heap overflows and use-after-frees whose first victim is ordinary data in a neighbouring or reused object. Two families of defence go after the bugs themselves, at run time, in production. Memory tagging gives every allocation a small colour and every pointer the same colour, and the hardware faults when they disagree: a probabilistic AddressSanitizer in silicon. Hardened allocators organise the heap so that corruption is less useful and more often detected: metadata away from data, randomised placement, delayed reuse, checked frees. Both catch bugs that were missed in testing, in the field, at a cost low enough to leave on.
Memory tagging, the idea
Arm's Memory Tagging Extension (MTE, Armv8.5-A) works on 16-byte granules. Each granule of memory has a 4-bit tag, stored by the hardware outside normal memory. A pointer carries a 4-bit tag in its top byte, which the CPU ignores when computing the address (Arm's "top byte ignore"). The allocator picks a random tag for each new block and colours both the block's granules and the pointer it returns; on free it re-colours the block. Every load and store compares the pointer's tag with the memory's tag:
- Overflow into the next block: the next block has (very probably) a different tag, so the first out-of-bounds access faults.
- Use-after-free: the block was re-tagged at
free(and again when reused), so the stale pointer's old tag doesn't match.
With 16 possible tags, a random mismatch is missed with probability 1/16; allocators improve on that by making adjacent blocks always differ. MTE runs in synchronous mode (the faulting instruction is reported exactly, slower) or asynchronous mode (the fault is reported a little later, cheap enough for production). Android supports MTE on devices with Arm cores that implement it, starting with the Google Pixel 8 (as a developer option, 2023); Linux supports it for user space since 5.10.
A model of tagging
The model tags a small heap of 16-byte granules exactly as described: allocation picks a tag (from a fixed sequence here; real allocators pick randomly), pointers carry the tag, and every access compares. It runs the overflow and the use-after-free of earlier lessons and reports what MTE would do. It runs in your browser:
GRANULE = 16
mem_tags = [0] * 8 # 8 granules = 128 bytes of heap, tag 0 = unallocated
next_tags = iter([3, 9, 5, 12, 7]) # fixed for the lesson; real allocators draw unpredictable tags
free_list = []
def malloc(n):
g = (n + GRANULE - 1) // GRANULE
start = next(i for i in range(len(mem_tags) - g + 1) if all(t == 0 for t in mem_tags[i:i + g]))
tag = next(next_tags)
for i in range(start, start + g):
mem_tags[i] = tag
return (tag, start * GRANULE, n) # a "pointer": (tag, address, size)
def free(p):
tag, addr, n = p
for i in range(addr // GRANULE, (addr + n + GRANULE - 1) // GRANULE):
mem_tags[i] = 0 # re-tagged on free
def access(p, offset, what):
tag, addr, _ = p
g = (addr + offset) // GRANULE
ok = mem_tags[g] == tag
print(f"{what:34} ptr tag {tag:2}, memory tag {mem_tags[g]:2} -> {'ok' if ok else 'TAG CHECK FAULT'}")
a = malloc(32) # granules 0-1
b = malloc(16) # granule 2
access(a, 0, "a[0]")
access(a, 31, "a[31] (last byte)")
access(a, 32, "a[32] (overflow into b)")
free(b)
c = malloc(16) # reuses granule 2 with a new tag
access(b, 0, "b[0] after free (use-after-free)")
access(c, 0, "c[0] (the new owner)")
print("granule tags:", mem_tags)a[0] ptr tag 3, memory tag 3 -> ok
a[31] (last byte) ptr tag 3, memory tag 3 -> ok
a[32] (overflow into b) ptr tag 3, memory tag 9 -> TAG CHECK FAULT
b[0] after free (use-after-free) ptr tag 9, memory tag 5 -> TAG CHECK FAULT
c[0] (the new owner) ptr tag 5, memory tag 5 -> ok
granule tags: [3, 3, 5, 0, 0, 0, 0, 0]
The in-bounds accesses pass, the first byte past a lands in b's granule with b's tag and faults, and the stale pointer b faults after its granule was given to c with a new tag. A bug that ASan would report in testing is stopped in production, at the faulting access, with no source changes. Two limits remain visible in the model: overflows within a 16-byte granule (a 13-byte block has 3 slack bytes that carry its own tag) aren't detected, and with random tags a mismatch is missed 1 time in 16.
The compiler can tag stack objects too. Cross-compiling for an MTE-capable Android target with -fsanitize=memtag-stack shows the instructions it inserts: irg (insert a random tag into a pointer) and st2g (store that tag for two granules), then re-tagging before return:
cat > tag.c <<'SRC'
void use(char *p);
void f(void) { char buf[32]; use(buf); }
SRC
clang --target=aarch64-linux-android34 -march=armv8.5-a+memtag -O2 -fsanitize=memtag-stack -S -o - tag.c \
| grep -vE '^\s*(\.|//)|^\.L'f: // @f
sub sp, sp, #48
stp x29, x30, [sp, #32] // 16-byte Folded Spill
add x29, sp, #32
irg x0, sp
st2g x0, [x0]
bl use
st2g sp, [sp], #32
ldp x29, x30, [sp], #16 // 16-byte Folded Reload
ret
buf (32 bytes = 2 granules) gets a fresh random tag in x0 before use sees it; on return the granules are re-tagged with the stack pointer's tag, so a pointer to buf that escapes and is used later faults. The x86 world has no shipped equivalent yet; software versions (HWASan, which keeps tags in shadow memory and uses the top byte on Arm) are used for testing.
Hardened allocators
Allocators decide where objects go and when memory is reused, which is exactly what heap exploitation needs to control. Hardened allocators make different choices than glibc's speed-first design:
| Technique | Allocators that use it | Effect |
|---|---|---|
| Out-of-line or checksummed metadata | Scudo (chunk header checksums), hardened_malloc, PartitionAlloc | an overflow can't silently rewrite the allocator's records |
| Size-class (and type) segregation | Scudo, PartitionAlloc, Apple's kalloc_type, Windows' segment heap | an overflow from a buffer reaches only objects of the same size or type |
| Randomised placement | Scudo, hardened_malloc | the attacker can't predict which object follows the buffer |
| Quarantine / delayed reuse | Scudo (optional), MiraclePtr/BackupRefPtr in Chrome, ASan | a freed block isn't immediately handed to the next allocation |
| Checked frees | all of them, glibc included | double and invalid frees are detected |
| Sampled guard pages | GWP-ASan (Chrome, Android, Firefox's PHC) | a random few allocations get a guard page: low cost, catches bugs in the field |
Scudo is Android's default allocator since Android 11 and ships with LLVM. The session runs the use-after-free and double free toys with glibc and with Scudo preloaded, then with Scudo's quarantine on:
SCUDO=/usr/lib/llvm-18/lib/clang/18/lib/linux/libclang_rt.scudo_standalone-x86_64.so
cat > uaf.c <<'SRC'
#include <stdio.h>
#include <stdlib.h>
struct session { char user[16]; int is_admin; };
int main(int argc, char **argv) {
setvbuf(stdout, NULL, _IONBF, 0);
struct session *s = malloc(sizeof *s);
s->is_admin = 0;
free(s);
char *note = malloc(sizeof(struct session));
printf("note reuses the freed session: %s\n", note == (char *)s ? "yes" : "no");
if (argc > 1) free(s); /* double free on request */
free(note);
return 0;
}
SRC
gcc -g -O0 uaf.c -o uaf
echo "== glibc"; ./uaf
echo "== Scudo (defaults)"; LD_PRELOAD=$SCUDO ./uaf; LD_PRELOAD=$SCUDO ./uaf double 2>&1 | grep 'Scudo ERROR' | sed 's/0x[0-9a-f]*/<addr>/'
echo "== Scudo with a quarantine"
SCUDO_OPTIONS=quarantine_size_kb=256:thread_local_quarantine_size_kb=64:quarantine_max_chunk_size=2048 LD_PRELOAD=$SCUDO ./uaf== glibc
note reuses the freed session: yes
== Scudo (defaults)
note reuses the freed session: yes
Scudo ERROR: invalid chunk state when deallocating address <addr>
== Scudo with a quarantine
note reuses the freed session: no
With defaults, Scudo also reuses the freed block at once (it optimises for speed too), but it rejects the double free with invalid chunk state when deallocating: every chunk header records its state, protected by a checksum. With a quarantine configured, freed blocks wait before reuse and the dangling pointer no longer aliases the next object, which breaks the simplest UAF exploitation pattern. The quarantine costs memory, which is why it is off by default.
How these are deployed
- Android: Scudo by default; MTE opt-in per app (
android:memtagMode) on capable devices; GWP-ASan sampling in system processes. - Chrome: PartitionAlloc with MiraclePtr (BackupRefPtr) quarantining memory that
raw_ptr<T>fields still reference, and GWP-ASan. - Linux distributions: glibc by default;
hardened_malloc(GrapheneOS), Scudo or mimalloc's secure mode can be preloaded for exposed services; glibc itself has no hardened mode; its tunables help testing more than production (GLIBC_TUNABLES=glibc.malloc.perturb=…fills allocated and freed blocks with a byte pattern, so stale or uninitialised data stands out).
Pitfalls
- Probabilistic is not absolute. MTE misses a tag collision 1 time in 16, and in-granule overflows always; a determined attacker with many tries may get through.
- Asynchronous MTE reports late: the crash points after the bad access. Reproduce in synchronous mode or under ASan to find the line.
- Allocator swaps change timing and memory use. Benchmark before enabling quarantines or guard-page sampling on busy services.
Practice
1. In the model, why did a[31] pass and a[32] fault?
a is 32 bytes = granules 0 and 1, both tagged 3. Offset 32 is in granule 2, which belongs to b and has tag 9.
2. Would MTE detect a one-byte overflow of a 13-byte block?
No: the block occupies one 16-byte granule, so bytes 13–15 have the block's own tag. ASan (which tracks exact sizes with partial shadow bytes) would.
3. Why does size-class segregation help against heap overflows?
Objects of very different sizes (say a 24-byte string buffer and a 200-byte object with function pointers) live in different regions, so overflowing the small buffer can only reach other small objects of the same class.
Test yourself
What does MTE compare on every load and store?
- The canary
- The pointer's 4-bit tag with the tag of the 16-byte memory granule
- The return address
- The allocation size
Why did the stale pointer b fault in the model after c was allocated?
- b was NULL
- The granule was re-tagged when it was freed and given a new tag for c
- c overflowed
- The heap was full
What did Scudo report for the double free?
- Nothing
- invalid chunk state when deallocating
- stack smashing detected
- heap-use-after-free
What did turning on Scudo's quarantine change?
- Nothing
- The freed session was no longer reused by the next allocation
- The double free was allowed
- The program ran faster