Overview
Safe-Linking is a security mitigation introduced in glibc 2.32 that protects single-linked lists used in the heap allocator. It uses ASLR randomness to obfuscate pointers and enforces alignment checks to prevent pointer hijacking in tcache and fastbin freelists.Background
Before glibc 2.32, tcache and fastbin freelists stored forward pointers in plaintext, making them trivial to hijack:The Safe-Linking Mechanism
Commit: a1a486d70e introduced safe-linking in glibc 2.32.
1. Pointer Obfuscation (XOR Mangling)
Pointers are XOR’d with the address where they’re stored, shifted right by 12 bits:2. Alignment Check
The protection also verifies that revealed pointers are properly aligned:Example: Safe-Linking in Action
Security Properties
What Safe-Linking Protects Against
What Safe-Linking Protects Against
- Direct Pointer Overwrites: Attackers can’t simply write an arbitrary address into a freelist
- Information Leaks: Forward pointers no longer directly reveal heap addresses
- Basic Tcache Poisoning: The classic tcache_poisoning.c technique requires adaptation
- Fastbin Corruption: Similar protections apply to fastbin freelists
What Safe-Linking Does NOT Protect Against
What Safe-Linking Does NOT Protect Against
- Double-Free: Still exploitable with additional steps
- Heap Layout Attacks: Techniques like House of Botcake still work
- Metadata Corruption: Direct tcache_perthread_struct corruption bypasses this
- Known-Address Linking: If you know both the source and target addresses, you can compute the mangled value
Bypass Techniques
Several techniques have been developed to bypass or work around safe-linking:1. Decrypt Safe-Linking
Exploit the mathematical properties of the XOR obfuscation to recover the original pointer.Decrypt Safe-Linking
Learn how to recover original pointers from obfuscated values using the 12-bit sliding property.
2. Safe-Link Double Protect
Protect a pointer twice with the same key to effectively cancel out the obfuscation.Safe-Link Double Protect
Bypass safe-linking by applying the PROTECT_PTR operation twice to revert to the original value.
3. Tcache Metadata Manipulation
Directly corrupt the tcache_perthread_struct to bypass the freelist entirely.House of Water
Gain leakless control of tcache metadata to bypass safe-linking completely.
Impact on Classic Techniques
Tcache Poisoning
Before Safe-Linking:Fastbin Dup
Before Safe-Linking:Implementation Details
Affected Structures
Safe-linking protects:- Tcache bins (sizes 0x20 to 0x410)
- Fastbins (sizes 0x20 to 0xb0)
- Unsorted bin (uses double-linked list)
- Small bins (uses double-linked list)
- Large bins (uses double-linked list)
Performance Impact
The XOR operation and alignment check add minimal overhead:- ~2-3 CPU cycles per malloc/free
- No memory overhead
- No impact on allocation patterns
Code Reference
The safe-linking implementation in glibc:Practical Exploitation
To exploit heap vulnerabilities on glibc 2.32+, you typically need:- Heap Address Leak: To compute mangled pointers
- Known Chunk Layout: To predict pointer positions
- Bypass Strategy: One of the techniques listed above
Obtain Heap Leak
Use information disclosure bugs to leak a heap address. This can come from:
- Uninitialized memory
- Use-after-free reads
- Format string vulnerabilities
Comparison with Other Mitigations
| Mitigation | Glibc Version | Protects | Bypassable |
|---|---|---|---|
| Safe-Linking | 2.32+ | Single-linked lists | Yes (with heap leak) |
| Tcache Double-Free Check | 2.26-2.28 | Tcache double-free | Yes (House of Botcake) |
| Tcache Key Check | 2.29+ | Tcache double-free | Yes (overwrite key) |
| Safe-Unlinking | 2.3.4+ | Unlink attacks | Difficult |
Historical Context
Safe-linking was added in response to widespread exploitation of tcache poisoning:- 2017: Tcache introduced in glibc 2.26 with no protection
- 2018-2019: Tcache poisoning becomes the most common heap technique
- 2020: Safe-linking added in glibc 2.32
- 2020+: New bypass techniques developed (decrypt, double-protect, etc.)
Further Reading
- Decrypt Safe-Linking - Recover original pointers
- Safe-Link Double Protect - Blind bypass technique
- [Tcache Poisoning/techniques/tcache/tcache-poisoning) - The original technique
