Libc Heap
Tip
Μάθετε & εξασκηθείτε στο AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Μάθετε & εξασκηθείτε στο GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Μάθετε & εξασκηθείτε στο Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Υποστηρίξτε το HackTricks
- Ελέγξτε τα σχέδια συνδρομής!
- Εγγραφείτε στην 💬 ομάδα Discord ή στην ομάδα telegram ή ακολουθήστε μας στο Twitter 🐦 @hacktricks_live.
- Μοιραστείτε κόλπα hacking υποβάλλοντας PRs στα HackTricks και HackTricks Cloud github repos.
Βασικά για το Heap
The heap is basically the place where a program is going to be able to store data when it requests data calling functions like malloc, calloc… Moreover, when this memory is no longer needed it’s made available calling the function free.
As it’s shown, its just after where the binary is being loaded in memory (check the [heap] section):
.png)
Βασική Κατανομή Chunk
When some data is requested to be stored in the heap, some space of the heap is allocated to it. This space will belong to a bin and only the requested data + the space of the bin headers + minimum bin size offset will be reserved for the chunk. The goal is to just reserve as minimum memory as possible without making it complicated to find where each chunk is. For this, the metadata chunk information is used to know where each used/free chunk is.
There are different ways to reserver the space mainly depending on the used bin, but a general methodology is the following:
- The program starts by requesting certain amount of memory.
- If in the list of chunks there someone available big enough to fulfil the request, it’ll be used
- This might even mean that part of the available chunk will be used for this request and the rest will be added to the chunks list
- If there isn’t any available chunk in the list but there is still space in allocated heap memory, the heap manager creates a new chunk
- If there is not enough heap space to allocate the new chunk, the heap manager asks the kernel to expand the memory allocated to the heap and then use this memory to generate the new chunk
- If everything fails,
mallocreturns null.
Note that if the requested memory passes a threshold, mmap will be used to map the requested memory.
Arenas
Σε multithreaded εφαρμογές, ο heap manager πρέπει να αποτρέψει race conditions που θα μπορούσαν να οδηγήσουν σε crashes. Αρχικά, αυτό γινόταν χρησιμοποιώντας έναν global mutex για να εξασφαλιστεί ότι μόνο ένα νήμα μπορούσε να έχει πρόσβαση στο heap κάθε φορά, αλλά αυτό προκάλεσε performance issues λόγω του bottleneck που δημιουργεί ο mutex.
Για να αντιμετωπιστεί αυτό, ο ptmalloc2 heap allocator εισήγαγε τις “arenas”, όπου κάθε arena λειτουργεί ως ξεχωριστό heap με τις δικές του data structures και mutex, επιτρέποντας σε πολλαπλά νήματα να εκτελούν λειτουργίες στο heap χωρίς να παρεμβαίνουν το ένα στο άλλο, εφόσον χρησιμοποιούν διαφορετικές arenas.
The default “main” arena handles heap operations for single-threaded applications. When new threads are added, the heap manager assigns them secondary arenas to reduce contention. It first attempts to attach each new thread to an unused arena, creating new ones if needed, up to a limit of 2 times the number of CPU cores for 32-bit systems and 8 times for 64-bit systems. Once the limit is reached, threads must share arenas, leading to potential contention.
Unlike the main arena, which expands using the brk system call, secondary arenas create “subheaps” using mmap and mprotect to simulate the heap behaviour, allowing flexibility in managing memory for multithreaded operations.
Subheaps
Subheaps serve as memory reserves for secondary arenas in multithreaded applications, allowing them to grow and manage their own heap regions separately from the main heap. Here’s how subheaps differ from the initial heap and how they operate:
- Initial Heap vs. Subheaps:
- The initial heap is located directly after the program’s binary in memory, and it expands using the
sbrksystem call. - Subheaps, used by secondary arenas, are created through
mmap, a system call that maps a specified memory region.
- Memory Reservation with
mmap:
- When the heap manager creates a subheap, it reserves a large block of memory through
mmap. This reservation doesn’t allocate memory immediately; it simply designates a region that other system processes or allocations shouldn’t use. - By default, the reserved size for a subheap is 1 MB for 32-bit processes and 64 MB for 64-bit processes.
- Gradual Expansion with
mprotect:
- The reserved memory region is initially marked as
PROT_NONE, indicating that the kernel doesn’t need to allocate physical memory to this space yet. - To “grow” the subheap, the heap manager uses
mprotectto change page permissions fromPROT_NONEtoPROT_READ | PROT_WRITE, prompting the kernel to allocate physical memory to the previously reserved addresses. This step-by-step approach allows the subheap to expand as needed. - Once the entire subheap is exhausted, the heap manager creates a new subheap to continue allocation.
heap_info
This struct allocates relevant information of the heap. Moreover, heap memory might not be continuous after more allocations, this struct will also store that info.
// From https://github.com/bminor/glibc/blob/a07e000e82cb71238259e674529c37c12dc7d423/malloc/arena.c#L837
typedef struct _heap_info
{
mstate ar_ptr; /* Arena for this heap. */
struct _heap_info *prev; /* Previous heap. */
size_t size; /* Current size in bytes. */
size_t mprotect_size; /* Size in bytes that has been mprotected
PROT_READ|PROT_WRITE. */
size_t pagesize; /* Page size used when allocating the arena. */
/* Make sure the following data is properly aligned, particularly
that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
MALLOC_ALIGNMENT. */
char pad[-3 * SIZE_SZ & MALLOC_ALIGN_MASK];
} heap_info;
malloc_state
Κάθε heap (main arena ή other threads arenas) έχει μια malloc_state δομή.
Είναι σημαντικό να σημειωθεί ότι η main arena malloc_state δομή είναι μια global variable στο libc (επομένως βρίσκεται στον χώρο μνήμης του libc).
Στην περίπτωση των malloc_state δομών των heap των threads, αυτές βρίσκονται μέσα στο αντίστοιχο thread “heap”.
Υπάρχουν μερικά ενδιαφέροντα σημεία να σημειωθούν από αυτή τη δομή (βλέπε C κώδικα παρακάτω):
-
__libc_lock_define (, mutex);Υπάρχει για να διασφαλίσει ότι αυτή η δομή από το heap προσπελαύνεται από 1 thread κάθε φορά -
Flags:
-
#define NONCONTIGUOUS_BIT (2U)
#define contiguous(M) (((M)->flags & NONCONTIGUOUS_BIT) == 0) #define noncontiguous(M) (((M)->flags & NONCONTIGUOUS_BIT) != 0) #define set_noncontiguous(M) ((M)->flags |= NONCONTIGUOUS_BIT) #define set_contiguous(M) ((M)->flags &= ~NONCONTIGUOUS_BIT)
- Η `mchunkptr bins[NBINS * 2 - 2];` περιέχει **pointers** προς τα **first και last chunks** των small, large και unsorted **bins** (το -2 είναι επειδή ο δείκτης 0 δεν χρησιμοποιείται)
- Επομένως, το **first chunk** αυτών των bins θα έχει έναν **backwards pointer προς αυτή τη δομή** και το **last chunk** αυτών των bins θα έχει έναν **forward pointer** προς αυτή τη δομή. Αυτό, βασικά, σημαίνει ότι αν μπορείτε να l**eak these addresses in the main arena** θα έχετε έναν pointer προς τη δομή στο **libc**.
- Οι structs `struct malloc_state *next;` και `struct malloc_state *next_free;` είναι linked lists των arenas
- Το `top` chunk είναι το τελευταίο "chunk", που ουσιαστικά είναι **όλος ο υπόλοιπος χώρος του heap**. Μόλις το top chunk γίνει "empty", το heap έχει εξαντληθεί και πρέπει να ζητήσει περισσότερο χώρο.
- Το `last reminder` chunk προκύπτει σε περιπτώσεις όπου ένα chunk ακριβούς μεγέθους δεν είναι διαθέσιμο και επομένως ένα μεγαλύτερο chunk διαχωρίζεται, και ένα pointer του απομένουσας μέρους τοποθετείται εδώ.
```c
// From https://github.com/bminor/glibc/blob/a07e000e82cb71238259e674529c37c12dc7d423/malloc/malloc.c#L1812
struct malloc_state
{
/* Serialize access. */
__libc_lock_define (, mutex);
/* Flags (formerly in max_fast). */
int flags;
/* Set if the fastbin chunks contain recently inserted free blocks. */
/* Note this is a bool but not all targets support atomics on booleans. */
int have_fastchunks;
/* Fastbins */
mfastbinptr fastbinsY[NFASTBINS];
/* Base of the topmost chunk -- not otherwise kept in a bin */
mchunkptr top;
/* The remainder from the most recent split of a small request */
mchunkptr last_remainder;
/* Normal bins packed as described above */
mchunkptr bins[NBINS * 2 - 2];
/* Bitmap of bins */
unsigned int binmap[BINMAPSIZE];
/* Linked list */
struct malloc_state *next;
/* Linked list for free arenas. Access to this field is serialized
by free_list_lock in arena.c. */
struct malloc_state *next_free;
/* Number of threads attached to this arena. 0 if the arena is on
the free list. Access to this field is serialized by
free_list_lock in arena.c. */
INTERNAL_SIZE_T attached_threads;
/* Memory allocated from the system in this arena. */
INTERNAL_SIZE_T system_mem;
INTERNAL_SIZE_T max_system_mem;
};
malloc_chunk
Αυτή η δομή αντιπροσωπεύει ένα συγκεκριμένο chunk μνήμης. Τα διάφορα πεδία έχουν διαφορετική σημασία για allocated και unallocated chunks.
// https://github.com/bminor/glibc/blob/master/malloc/malloc.c
struct malloc_chunk {
INTERNAL_SIZE_T mchunk_prev_size; /* Size of previous chunk, if it is free. */
INTERNAL_SIZE_T mchunk_size; /* Size in bytes, including overhead. */
struct malloc_chunk* fd; /* double links -- used only if this chunk is free. */
struct malloc_chunk* bk;
/* Only used for large blocks: pointer to next larger size. */
struct malloc_chunk* fd_nextsize; /* double links -- used only if this chunk is free. */
struct malloc_chunk* bk_nextsize;
};
typedef struct malloc_chunk* mchunkptr;
Όπως αναφέρθηκε προηγουμένως, αυτά τα chunks έχουν επίσης κάποια μεταδεδομένα, πολύ καλά απεικονιζόμενα σε αυτήν την εικόνα:
.png)
https://azeria-labs.com/wp-content/uploads/2019/03/chunk-allocated-CS.png
Τα μεταδεδομένα είναι συνήθως 0x08B, που δείχνει το τρέχον μέγεθος του chunk χρησιμοποιώντας τα τελευταία 3 bits για να υποδείξει:
A: Αν είναι 1 προέρχεται από subheap, αν είναι 0 βρίσκεται στο main arenaM: Αν είναι 1, αυτό το chunk είναι μέρος ενός χώρου που έχει δεσμευτεί με mmap και δεν αποτελεί μέρος heapP: Αν είναι 1, το προηγούμενο chunk είναι σε χρήση
Στη συνέχεια, ο χώρος για τα user data, και τέλος 0x08B για να υποδείξει το προηγούμενο chunk size όταν το chunk είναι διαθέσιμο (ή για να αποθηκεύσει user data όταν είναι allocated).
Επιπλέον, όταν είναι διαθέσιμο, τα user data χρησιμοποιούνται επίσης για να περιέχουν κάποια δεδομένα:
fd: Δείκτης προς το επόμενο chunkbk: Δείκτης προς το προηγούμενο chunkfd_nextsize: Δείκτης στο πρώτο chunk στη λίστα που είναι μικρότερο από τον ίδιοbk_nextsize: Δείκτης στο πρώτο chunk στη λίστα που είναι μεγαλύτερο από τον ίδιο
.png)
https://azeria-labs.com/wp-content/uploads/2019/03/chunk-allocated-CS.png
Tip
Σημειώστε πώς η σύνδεση της λίστας με αυτόν τον τρόπο αποτρέπει την ανάγκη ύπαρξης ενός πίνακα όπου κάθε chunk καταχωρείται.
Δείκτες Chunk
Όταν χρησιμοποιείται το malloc, επιστρέφεται ένας δείκτης στο περιεχόμενο στο οποίο μπορεί να γραφτεί (αμέσως μετά τα headers), όμως, όταν διαχειριζόμαστε chunks, χρειάζεται ένας δείκτης στην αρχή των headers (των μεταδεδομένων).
Για αυτές τις μετατροπές χρησιμοποιούνται οι παρακάτω συναρτήσεις:
// https://github.com/bminor/glibc/blob/master/malloc/malloc.c
/* Convert a chunk address to a user mem pointer without correcting the tag. */
#define chunk2mem(p) ((void*)((char*)(p) + CHUNK_HDR_SZ))
/* Convert a user mem pointer to a chunk address and extract the right tag. */
#define mem2chunk(mem) ((mchunkptr)tag_at (((char*)(mem) - CHUNK_HDR_SZ)))
/* The smallest possible chunk */
#define MIN_CHUNK_SIZE (offsetof(struct malloc_chunk, fd_nextsize))
/* The smallest size we can malloc is an aligned minimal chunk */
#define MINSIZE \
(unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
Στοίχιση & ελάχιστο μέγεθος
Ο pointer προς το chunk και 0x0f πρέπει να είναι 0.
// From https://github.com/bminor/glibc/blob/a07e000e82cb71238259e674529c37c12dc7d423/sysdeps/generic/malloc-size.h#L61
#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
// https://github.com/bminor/glibc/blob/a07e000e82cb71238259e674529c37c12dc7d423/sysdeps/i386/malloc-alignment.h
#define MALLOC_ALIGNMENT 16
// https://github.com/bminor/glibc/blob/master/malloc/malloc.c
/* Check if m has acceptable alignment */
#define aligned_OK(m) (((unsigned long)(m) & MALLOC_ALIGN_MASK) == 0)
#define misaligned_chunk(p) \
((uintptr_t)(MALLOC_ALIGNMENT == CHUNK_HDR_SZ ? (p) : chunk2mem (p)) \
& MALLOC_ALIGN_MASK)
/* pad request bytes into a usable size -- internal version */
/* Note: This must be a macro that evaluates to a compile time constant
if passed a literal constant. */
#define request2size(req) \
(((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? \
MINSIZE : \
((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
/* Check if REQ overflows when padded and aligned and if the resulting
value is less than PTRDIFF_T. Returns the requested size or
MINSIZE in case the value is less than MINSIZE, or 0 if any of the
previous checks fail. */
static inline size_t
checked_request2size (size_t req) __nonnull (1)
{
if (__glibc_unlikely (req > PTRDIFF_MAX))
return 0;
/* When using tagged memory, we cannot share the end of the user
block with the header for the next chunk, so ensure that we
allocate blocks that are rounded up to the granule size. Take
care not to overflow from close to MAX_SIZE_T to a small
number. Ideally, this would be part of request2size(), but that
must be a macro that produces a compile time constant if passed
a constant literal. */
if (__glibc_unlikely (mtag_enabled))
{
/* Ensure this is not evaluated if !mtag_enabled, see gcc PR 99551. */
asm ("");
req = (req + (__MTAG_GRANULE_SIZE - 1)) &
~(size_t)(__MTAG_GRANULE_SIZE - 1);
}
return request2size (req);
}
Σημειώστε ότι για τον υπολογισμό του συνολικού απαιτούμενου χώρου προστίθεται το SIZE_SZ μόνο 1 φορά επειδή το πεδίο prev_size μπορεί να χρησιμοποιηθεί για την αποθήκευση δεδομένων, επομένως χρειάζεται μόνο η αρχική κεφαλίδα.
Λήψη δεδομένων Chunk και τροποποίηση μεταδεδομένων
Αυτές οι συναρτήσεις λειτουργούν λαμβάνοντας έναν pointer σε ένα chunk και είναι χρήσιμες για τον έλεγχο/ρύθμιση μεταδεδομένων:
- Έλεγχος flags του chunk
// From https://github.com/bminor/glibc/blob/master/malloc/malloc.c
/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
#define PREV_INUSE 0x1
/* extract inuse bit of previous chunk */
#define prev_inuse(p) ((p)->mchunk_size & PREV_INUSE)
/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
#define IS_MMAPPED 0x2
/* check for mmap()'ed chunk */
#define chunk_is_mmapped(p) ((p)->mchunk_size & IS_MMAPPED)
/* size field is or'ed with NON_MAIN_ARENA if the chunk was obtained
from a non-main arena. This is only set immediately before handing
the chunk to the user, if necessary. */
#define NON_MAIN_ARENA 0x4
/* Check for chunk from main arena. */
#define chunk_main_arena(p) (((p)->mchunk_size & NON_MAIN_ARENA) == 0)
/* Mark a chunk as not being on the main arena. */
#define set_non_main_arena(p) ((p)->mchunk_size |= NON_MAIN_ARENA)
- Μεγέθη και δείκτες σε άλλα chunks
/*
Bits to mask off when extracting size
Note: IS_MMAPPED is intentionally not masked off from size field in
macros for which mmapped chunks should never be seen. This should
cause helpful core dumps to occur if it is tried by accident by
people extending or adapting this malloc.
*/
#define SIZE_BITS (PREV_INUSE | IS_MMAPPED | NON_MAIN_ARENA)
/* Get size, ignoring use bits */
#define chunksize(p) (chunksize_nomask (p) & ~(SIZE_BITS))
/* Like chunksize, but do not mask SIZE_BITS. */
#define chunksize_nomask(p) ((p)->mchunk_size)
/* Ptr to next physical malloc_chunk. */
#define next_chunk(p) ((mchunkptr) (((char *) (p)) + chunksize (p)))
/* Size of the chunk below P. Only valid if !prev_inuse (P). */
#define prev_size(p) ((p)->mchunk_prev_size)
/* Set the size of the chunk below P. Only valid if !prev_inuse (P). */
#define set_prev_size(p, sz) ((p)->mchunk_prev_size = (sz))
/* Ptr to previous physical malloc_chunk. Only valid if !prev_inuse (P). */
#define prev_chunk(p) ((mchunkptr) (((char *) (p)) - prev_size (p)))
/* Treat space at ptr + offset as a chunk */
#define chunk_at_offset(p, s) ((mchunkptr) (((char *) (p)) + (s)))
- bit ζητήματος
/* extract p's inuse bit */
#define inuse(p) \
((((mchunkptr) (((char *) (p)) + chunksize (p)))->mchunk_size) & PREV_INUSE)
/* set/clear chunk as being inuse without otherwise disturbing */
#define set_inuse(p) \
((mchunkptr) (((char *) (p)) + chunksize (p)))->mchunk_size |= PREV_INUSE
#define clear_inuse(p) \
((mchunkptr) (((char *) (p)) + chunksize (p)))->mchunk_size &= ~(PREV_INUSE)
/* check/set/clear inuse bits in known places */
#define inuse_bit_at_offset(p, s) \
(((mchunkptr) (((char *) (p)) + (s)))->mchunk_size & PREV_INUSE)
#define set_inuse_bit_at_offset(p, s) \
(((mchunkptr) (((char *) (p)) + (s)))->mchunk_size |= PREV_INUSE)
#define clear_inuse_bit_at_offset(p, s) \
(((mchunkptr) (((char *) (p)) + (s)))->mchunk_size &= ~(PREV_INUSE))
- Ορίστε την επικεφαλίδα και το υποσέλιδο (όταν chunk nos χρησιμοποιούνται
/* Set size at head, without disturbing its use bit */
#define set_head_size(p, s) ((p)->mchunk_size = (((p)->mchunk_size & SIZE_BITS) | (s)))
/* Set size/use field */
#define set_head(p, s) ((p)->mchunk_size = (s))
/* Set size at footer (only when chunk is not in use) */
#define set_foot(p, s) (((mchunkptr) ((char *) (p) + (s)))->mchunk_prev_size = (s))
- Προσδιορίστε το μέγεθος των πραγματικά χρησιμοποιήσιμων δεδομένων μέσα στο chunk
#pragma GCC poison mchunk_size
#pragma GCC poison mchunk_prev_size
/* This is the size of the real usable data in the chunk. Not valid for
dumped heap chunks. */
#define memsize(p) \
(__MTAG_GRANULE_SIZE > SIZE_SZ && __glibc_unlikely (mtag_enabled) ? \
chunksize (p) - CHUNK_HDR_SZ : \
chunksize (p) - CHUNK_HDR_SZ + (chunk_is_mmapped (p) ? 0 : SIZE_SZ))
/* If memory tagging is enabled the layout changes to accommodate the granule
size, this is wasteful for small allocations so not done by default.
Both the chunk header and user data has to be granule aligned. */
_Static_assert (__MTAG_GRANULE_SIZE <= CHUNK_HDR_SZ,
"memory tagging is not supported with large granule.");
static __always_inline void *
tag_new_usable (void *ptr)
{
if (__glibc_unlikely (mtag_enabled) && ptr)
{
mchunkptr cp = mem2chunk(ptr);
ptr = __libc_mtag_tag_region (__libc_mtag_new_tag (ptr), memsize (cp));
}
return ptr;
}
Παραδείγματα
Γρήγορο παράδειγμα heap
Γρήγορο παράδειγμα heap από https://guyinatuxedo.github.io/25-heap/index.html αλλά σε arm64:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
char *ptr;
ptr = malloc(0x10);
strcpy(ptr, "panda");
}
Βάλε ένα breakpoint στο τέλος της main function και ας δούμε πού αποθηκεύτηκε η πληροφορία:
.png)
Μπορούμε να δούμε ότι η συμβολοσειρά panda αποθηκεύτηκε στη διεύθυνση 0xaaaaaaac12a0 (η οποία ήταν η διεύθυνση που επέστρεψε το malloc στο x0). Ελέγχοντας 0x10 bytes πριν, μπορούμε να δούμε ότι το 0x0 υποδηλώνει ότι το προηγούμενο chunk δεν χρησιμοποιείται (μήκος 0) και ότι το μήκος αυτού του chunk είναι 0x21.
Οι επιπλέον χώροι που δεσμεύτηκαν (0x21-0x10=0x11) προέρχονται από τα πρόσθετα headers (0x10) και το 0x1 δεν σημαίνει ότι δεσμεύτηκαν 0x21B αλλά ότι τα τελευταία 3 bit του μήκους του τρέχοντος header έχουν κάποιες ειδικές σημασίες. Εφόσον το μήκος είναι πάντα ευθυγραμμισμένο σε 16-byte (σε μηχανές 64-bit), αυτά τα bits στην ουσία δεν χρησιμοποιούνται από τον αριθμό μήκους.
0x1: Previous in Use - Specifies that the chunk before it in memory is in use
0x2: Is MMAPPED - Specifies that the chunk was obtained with mmap()
0x4: Non Main Arena - Specifies that the chunk was obtained from outside of the main arena
Παράδειγμα πολυνηματισμού
Πολυνηματισμός
```c #includevoid* threadFuncMalloc(void* arg) { printf(“Hello from thread 1\n”); char* addr = (char*) malloc(1000); printf(“After malloc and before free in thread 1\n”); free(addr); printf(“After free in thread 1\n”); }
void* threadFuncNoMalloc(void* arg) { printf(“Hello from thread 2\n”); }
int main() { pthread_t t1; void* s; int ret; char* addr;
printf(“Before creating thread 1\n”); getchar(); ret = pthread_create(&t1, NULL, threadFuncMalloc, NULL); getchar();
printf(“Before creating thread 2\n”); ret = pthread_create(&t1, NULL, threadFuncNoMalloc, NULL);
printf(“Before exit\n”); getchar();
return 0; }
</details>
Debugging του προηγούμενου παραδείγματος είναι δυνατό να δει κανείς πως στην αρχή υπάρχει μόνο 1 arena:
<figure><img src="../../images/image (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
Στη συνέχεια, μετά την κλήση του πρώτου thread, αυτού που καλεί malloc, δημιουργείται ένα νέο arena:
<figure><img src="../../images/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
και μέσα σε αυτό μπορούν να βρεθούν μερικά chunks:
<figure><img src="../../images/image (2) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
## Bins & Διανομές/Απελευθερώσεις μνήμης
Δείτε ποια είναι τα bins, πώς οργανώνονται και πώς η μνήμη δεσμεύεται και απελευθερώνεται στο:
<a class="content_ref" href="bins-and-memory-allocations.md"><span class="content_ref_label">Bins & Memory Allocations</span></a>
## Έλεγχοι ασφαλείας των Heap Functions
Οι συναρτήσεις που εμπλέκονται με το heap θα εκτελέσουν ορισμένους ελέγχους πριν εκτελέσουν τις ενέργειές τους για να προσπαθήσουν να διασφαλίσουν ότι το heap δεν έχει υποστεί αλλοίωση:
<a class="content_ref" href="heap-memory-functions/heap-functions-security-checks.md"><span class="content_ref_label">Heap Functions Security Checks</span></a>
## musl mallocng exploitation notes (Alpine)
- **Slab group/slot grooming for huge linear copies:** mallocng sizeclasses χρησιμοποιούν mmap()'d groups των οποίων τα slots γίνονται πλήρως `munmap()`'d όταν είναι κενά. Για μεγάλες γραμμικές αντιγραφές (~0x15555555 bytes), διατηρήστε το span mapped (αποφύγετε holes από released groups) και τοποθετήστε το victim allocation δίπλα στο source slot.
- **Cycling offset mitigation:** Στο slot reuse, mallocng μπορεί να προωθήσει την αρχή των user-data κατά πολλαπλάσια του `UNIT` (0x10) όταν το slack χωράει επιπλέον 4-byte header. Αυτό μετατοπίζει τα overwrite offsets (π.χ., LSB pointer hits) εκτός αν ελέγχετε τους reuse counts ή τηρείτε strides χωρίς slack (π.χ., τα Lua `Table` objects με stride 0x50 δείχνουν offset 0). Εξετάστε offsets με το muslheap’s `mchunkinfo`:
```gdb
pwndbg> mchunkinfo 0x7ffff7a94e40
... stride: 0x140
... cycling offset : 0x1 (userdata --> 0x7ffff7a94e40)
- Προτιμήστε τη διαφθορά αντικειμένων κατά το runtime αντί για allocator metadata: mallocng mixes cookies/guarded out-of-band metadata, οπότε στοχεύστε αντικείμενα υψηλότερου επιπέδου. Στο Redis’s Lua 5.1,
Table->arrayδείχνει σε ένα array απόTValuetagged values· η υπεργραφή του LSB ενός pointer στοTValue->value(π.χ. με το JSON terminator byte0x22) μπορεί να αναπροσανατολίσει τις αναφορές χωρίς να αγγίξει malloc metadata. - Debugging stripped/static Lua on Alpine: Build a matching Lua, list symbols with
readelf -Ws, strip function symbols viaobjcopy --strip-symbolto expose struct layouts in GDB, then use Lua-aware pretty-printers (GdbLuaExtension for Lua 5.1) plus muslheap to check stride/reserved/cycling-offset values before triggering the overflow.
Μελέτες Περίπτωσης
Μελετήστε allocator-specific primitives που προκύπτουν από πραγματικά σφάλματα:
Virtualbox Slirp Nat Packet Heap Exploitation
Gnu Obstack Function Pointer Hijack
Αναφορές
- https://azeria-labs.com/heap-exploitation-part-1-understanding-the-glibc-heap-implementation/
- https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/
- Pumping Iron on the Musl Heap – Real World CVE-2022-24834 Exploitation on an Alpine mallocng Heap
- musl mallocng enframe (v1.2.4)
- muslheap GDB plugin
- GdbLuaExtension (Lua 5.1 support)
Tip
Μάθετε & εξασκηθείτε στο AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Μάθετε & εξασκηθείτε στο GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Μάθετε & εξασκηθείτε στο Azure Hacking:
HackTricks Training Azure Red Team Expert (AzRTE)
Υποστηρίξτε το HackTricks
- Ελέγξτε τα σχέδια συνδρομής!
- Εγγραφείτε στην 💬 ομάδα Discord ή στην ομάδα telegram ή ακολουθήστε μας στο Twitter 🐦 @hacktricks_live.
- Μοιραστείτε κόλπα hacking υποβάλλοντας PRs στα HackTricks και HackTricks Cloud github repos.


