Skip to main content

Overview

Safe-Link Double Protect is a blind bypass technique for the safe-linking mitigation that requires no heap leaks. By protecting a pointer twice with the same key, the XOR operations cancel out, effectively reverting to the original unprotected pointer.
Glibc Compatibility: Works on glibc 2.32+ where safe-linking was introduced.

What It Achieves

This technique allows you to:
  • Bypass safe-linking without heap leaks: No need to know actual addresses
  • Link arbitrary pointers into tcache: Achieve arbitrary allocation primitive
  • Exploit with limited primitives: Only needs tcache metadata control
  • Require only 4-bit bruteforce: If using write primitive (no bruteforce with increment)
Prerequisites: This technique requires control over the tcache metadata structure, making it often paired with techniques like House of Water.

The Core Concept

Safe-linking protects pointers using:
The key insight is that protecting twice with the same key cancels out:
Mathematical Proof:

Full Source Code

Step-by-Step Walkthrough

1

Set Up Multiple Tcache Bins

Allocate chunks in different size classes to have control over multiple tcache bins:
2

Write Target Address

Store your target address (aligned) in a heap chunk:
3

Populate Tcache Bins

Free the chunks to create tcache freelists:
Now both freelists have safe-linked pointers.
4

Corrupt Tcache Metadata (First Protection)

Modify the 0x40 tcache entry to point to our value chunk:
Now the 0x40 tcache contains our target address (protected once by safe-linking).
5

Allocate to Get Protected Pointer

Take one chunk from 0x40 tcache to make our target the head:
6

Apply Second Protection (Double Protect)

Point the 0x20 tcache bin to the 0x40 tcache bin entry:
When we free into 0x20, safe-linking will protect our already-protected pointer again, canceling out the protection!
7

Trigger Arbitrary Allocation

Allocate from the 0x20 tcache twice:
The second allocation returns our arbitrary target address.

Why This Works

The technique exploits how safe-linking is applied:
  1. First Protection (when freeing chunk A):
  2. Second Protection (when reading from tcache):
  3. Cancellation:
The XOR operations with the same key cancel each other out.

Bruteforce Considerations

4-bit Bruteforce: If using a write primitive, the lowest 4 bits contain randomness from ASLR. However:
  • No bruteforce needed if you can increment integers
  • 1/16 success rate if you must write exact values
  • Can be avoided by aligning your target address

Prerequisites

This technique requires:
  1. Tcache Metadata Control: Ability to overwrite tcache_perthread_struct
  2. Multiple Tcache Bins: Need at least two different size classes
  3. Heap Chunk with Target Address: A place to store your target pointer
  4. Allocation Primitive: Ability to call malloc to trigger the exploit

Combining with Other Techniques

House of Water

House of Water provides the tcache metadata control needed:

House of Water

Learn how to gain leakless control of tcache metadata

Tcache Relative Write

Can be used to corrupt tcache metadata:

Tcache Relative Write

Achieve out-of-bounds tcache metadata writes

Why This Is Advanced

Safe-Link Double Protect is considered advanced because:
  1. Requires deep understanding of safe-linking: Must understand XOR properties at bit level
  2. Needs precise tcache metadata control: Difficult to achieve in most programs
  3. Complex multi-step setup: Requires orchestrating multiple tcache bins
  4. Blind technique: Works without leaks, but needs precise execution
  5. Fragile exploitation: Small mistakes can crash the program

Advantages Over Other Bypasses

Common Pitfalls

Avoid these mistakes:
  1. Unaligned targets: Ensure target addresses are 16-byte aligned
  2. Wrong tcache bins: Must use two different size classes
  3. Incorrect metadata offset: Tcache metadata layout is version-specific
  4. Premature allocation: Don’t malloc before setting up both protections

Real-World Applications

This technique has been used in:
  • 37c3 Potluck CTF - Tamagoyaki: Original demonstration of the technique
  • Modern CTF challenges on glibc 2.32+ without heap leaks
  • Exploits where information disclosure is heavily restricted

Implementation Tips

Debugging the Exploit

Use GDB to verify each step:
  • Safe-Linking Overview - Understanding the mitigation
  • Decrypt Safe-Linking - Alternative bypass
  • [House of Water/techniques/house/house-of-water) - Tcache metadata control
  • [Tcache Poisoning/techniques/tcache/tcache-poisoning) - Classic technique this bypasses

References