Overview
The House of Einherjar is an elegant heap exploitation technique that leverages an off-by-one overflow with a null byte to create overlapping chunks. Named after the warriors of Norse mythology, this technique uses a single null byte to achieve powerful control over heap allocations.This technique is particularly powerful because it only requires a single null byte overflow, one of the most common vulnerability types in C programs.
Glibc Version Compatibility
Ret2 Wargames Practice: Try this technique hands-on at House of Einherjar Interactive Challenge
What This Technique Achieves
The House of Einherjar enables:- Chunk overlapping: Create a large chunk that overlaps existing allocations
- Use-after-free creation: Get malloc to return already-allocated memory
- Tcache poisoning setup: Use the overlap to corrupt tcache for arbitrary allocation
- Control flow hijacking: Overwrite function pointers or return addresses
Prerequisites and Constraints
How It Works
1
Create fake chunk
Allocate a chunk and set up fake chunk metadata with self-referential fd/bk pointers.
2
Allocate victim chunks
Allocate chunks ‘b’ and ‘c’ where ‘b’ will overflow into ‘c’.
3
Overflow with null byte
Write a null byte past the end of chunk ‘b’ to clear c’s PREV_INUSE bit.
4
Set fake prev_size
Write a fake prev_size in chunk ‘b’ to point back to fake chunk ‘a’.
5
Fill tcache and free
Fill tcache for size 0xf8, then free chunk ‘c’ to trigger consolidation.
6
Exploit the overlap
Allocate a large chunk covering the overlapping region, then use tcache poisoning.
Complete Source Code
Technical Deep Dive
The Off-By-One Null Byte
The null byte overflow clears the PREV_INUSE bit:Why Size Must End in 0x00
The chunk size must have LSB = 0x01 before overflow:- 0x101 -> null byte -> 0x100 ✓
- 0x111 -> null byte -> 0x110 (changes size, likely crashes) ✗
- 0x101 (request 0xf8)
- 0x201 (request 0x1f8)
- 0x301 (request 0x2f8)
The Unlink Check
When consolidating, unlink() validates the fake chunk:Tcache Poisoning with Safe Linking
Glibc 2.32+ uses safe linking for tcache:- Heap leak (to know address)
- Calculate:
target ^ (current_addr >> 12) - Overwrite fd pointer via overlapping chunk
CTF Challenge
Seccon 2016 - tinypad
Seccon 2016 - tinypad
Challenge: Note-taking application with off-by-one overflowVulnerability:
- strcpy() caused off-by-one null byte overflow
- Could overflow from one note into next chunk’s metadata
- Created fake chunk in early allocation
- Used off-by-one to clear PREV_INUSE of target chunk
- Set fake prev_size to point back to fake chunk
- Freed target chunk, consolidating with fake chunk
- Got overlapping chunk allocation
- Overwrote function pointer for shell
Common Pitfalls
Exploitation Strategy
Phase 1: Setup and Overlap
- Leak heap address
- Create fake chunk with self-referential pointers
- Allocate overflow and victim chunks
- Off-by-one to clear PREV_INUSE
- Set fake prev_size
- Fill tcache and free victim
- Allocate large overlapping chunk
Phase 2: Tcache Poisoning
- Free the overlapped chunk (now covered by large chunk)
- Use large chunk to overwrite freed chunk’s fd pointer
- Set fd to:
target ^ (fd_address >> 12) - Drain tcache with one allocation
- Next allocation returns target
Mitigations
This technique remains unpatched but faces several obstacles:- Safe linking (glibc 2.32+): Requires heap leak for tcache poisoning
- Unlink validation: Fake chunk must have valid fd/bk pointers
- Bounds checking: Some implementations detect off-by-one overflows
- Tcache key (glibc 2.34+): Tcache entries have additional validation
Related Techniques
Poison Null Byte
Alternative null byte exploitation technique
Tcache Poisoning
Core technique used in modern House of Einherjar
Unsafe Unlink
Related technique exploiting chunk consolidation
See Also
- [Off-By-One Vulnerabilities/techniques/advanced/poison-null-byte)
- Chunk Consolidation
- Safe Linking
- Tcache Management
