Search K
Appearance
Appearance
Other ways to support HackTricks:
Malloc allocates memory in 8-byte (32-bit) or 16-byte (64-bit) groupings. This means the end of chunks in 32-bit systems should align with 0x8, and in 64-bit systems with 0x0. The security feature checks that each chunk aligns correctly at these specific locations before using a pointer from a bin.
The enforcement of chunk alignment in 64-bit systems significantly enhances Malloc's security by limiting the placement of fake chunks to only 1 out of every 16 addresses. This complicates exploitation efforts, especially in scenarios where the user has limited control over input values, making attacks more complex and harder to execute successfully.
The new alignment rules in Malloc also thwart a classic attack involving the __malloc_hook
. Previously, attackers could manipulate chunk sizes to overwrite this function pointer and gain code execution. Now, the strict alignment requirement ensures that such manipulations are no longer viable, closing a common exploitation route and enhancing overall security.
Pointer Mangling is a security enhancement used to protect fastbin and tcache Fd pointers in memory management operations. This technique helps prevent certain types of memory exploit tactics, specifically those that do not require leaked memory information or that manipulate memory locations directly relative to known positions (relative overwrites).
The core of this technique is an obfuscation formula:
New_Ptr = (L >> 12) XOR P
The reason for the bitwise shift of the storage location (L) by 12 bits to the right before the XOR operation is critical. This manipulation addresses a vulnerability inherent in the deterministic nature of the least significant 12 bits of memory addresses, which are typically predictable due to system architecture constraints. By shifting the bits, the predictable portion is moved out of the equation, enhancing the randomness of the new, mangled pointer and thereby safeguarding against exploits that rely on the predictability of these bits.
This mangled pointer leverages the existing randomness provided by Address Space Layout Randomization (ASLR), which randomizes addresses used by programs to make it difficult for attackers to predict the memory layout of a process.
Demangling the pointer to retrieve the original address involves using the same XOR operation. Here, the mangled pointer is treated as P in the formula, and when XORed with the unchanged storage location (L), it results in the original pointer being revealed. This symmetry in mangling and demangling ensures that the system can efficiently encode and decode pointers without significant overhead, while substantially increasing security against attacks that manipulate memory pointers.
Pointer mangling aims to prevent partial and full pointer overwrites in heap management, a significant enhancement in security. This feature impacts exploit techniques in several ways:
__malloc_hook
) by manipulating fastbin or tcache entries are hindered. For example, an attack might involve leaking a LibC address, freeing a chunk into the tcache bin, and then overwriting the Fd pointer to redirect it to __malloc_hook
for arbitrary code execution. With pointer mangling, these pointers must be correctly mangled, necessitating a heap leak for accurate manipulation, thereby elevating the exploitation barrier.โ
For a better explanation of the process check the original post from here.
The formula used for mangling and demangling pointers is:
New_Ptr = (L >> 12) XOR P
Where L is the storage location and P is the Fd pointer. When L is shifted right by 12 bits, it exposes the most significant bits of P, due to the nature of XOR, which outputs 0 when bits are XORed with themselves.
Key Steps in the Algorithm:
You can find an implementation of this algorithm here: https://github.com/mdulin2/mangle
Pointer guard is an exploit mitigation technique used in glibc to protect stored function pointers, particularly those registered by library calls such as atexit()
. This protection involves scrambling the pointers by XORing them with a secret stored in the thread data (fs:0x30
) and applying a bitwise rotation. This mechanism aims to prevent attackers from hijacking control flow by overwriting function pointers.
PTR_MANGLE
macro which XORs the pointer with a 64-bit secret and then performs a left rotation of 0x11 bits. The reverse operation for recovering the original pointer is handled by PTR_DEMANGLE
.__libc_pthread_functions
), an attacker can find predictable function pointers.__pthread_attr_destroy
and its mangled version from the function pointer table, the secret can be calculated by reverse rotating (right rotation) the mangled pointer and then XORing it with the address of the function.Other ways to support HackTricks: