Libc Heap
Tip
AWS हैकिंग सीखें और अभ्यास करें:
HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें:HackTricks Training GCP Red Team Expert (GRTE)
Azure हैकिंग सीखें और अभ्यास करें:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।
Heap मूल बातें
Heap मूल रूप से वह जगह है जहाँ एक प्रोग्राम डेटा स्टोर कर सकता है जब वह malloc, calloc आदि जैसे फ़ंक्शन्स को कॉल करके मेमोरी माँगता है। और जब यह मेमोरी अब आवश्यक नहीं रहती है तो इसे फ़ंक्शन free को कॉल करके उपलब्ध करवा दिया जाता है।
जैसा कि दिखाया गया है, यह बाइनरी के मेमोरी में लोड होने के ठीक बाद आता है (देखें [heap] सेक्शन):
.png)
बुनियादी चंक आवंटन
जब heap में कुछ डेटा स्टोर करने के लिए अनुरोध किया जाता है, तो heap का कुछ हिस्सा उस अनुरोध के लिए रिज़र्व किया जाता है। यह स्थान एक bin से संबंधित होगा और केवल अनुरोधित डेटा + bin हेडर्स का स्पेस + न्यूनतम bin साइज ऑफ़सेट चंक के लिए आरक्षित किया जाएगा। लक्ष्य यह है कि संभवतः कम से कम मेमोरी आरक्षित की जाए बिना यह जटिल बनाए कि प्रत्येक चंक कहाँ है। इसके लिए, metadata चंक जानकारी का उपयोग यह जानने के लिए किया जाता है कि कौन से चंक उपयोग में हैं और कौन से free हैं।
विभिन्न बिन्स के उपयोग पर निर्भर कर के जगह आरक्षित करने के अलग तरीके हैं, लेकिन सामान्य प्रक्रिया कुछ इस प्रकार है:
- प्रोग्राम शुरुआत में एक निश्चित मात्रा में मेमोरी अनुरोध करता है।
- यदि चंक्स की सूची में कोई उपलब्ध चंक अनुरोध को पूरा करने के लिए पर्याप्त बड़ा है, तो वही उपयोग किया जाएगा।
- इसका मतलब यह भी हो सकता है कि उपलब्ध चंक के हिस्से को इस अनुरोध के लिए उपयोग किया जाएगा और शेष भाग चंक्स सूची में जोड़ दिया जाएगा।
- यदि सूची में कोई उपलब्ध चंक नहीं है लेकिन allocated heap मेमोरी में अभी भी जगह है, तो heap manager एक नया चंक बनाता है।
- यदि नया चंक अलोकेट करने के लिए पर्याप्त heap स्पेस नहीं है, तो heap manager kernel से heap को विस्तारित करने के लिए कहता है और फिर इस मेमोरी का उपयोग करके नया चंक बनाता है।
- यदि सब कुछ असफल रहता है, तो
mallocnull लौटाता है।
ध्यान दें कि यदि अनुरोधित मेमोरी किसी थ्रेशहोल्ड से अधिक होती है, तो mmap का उपयोग करके अनुरोधित मेमोरी को मैप किया जाएगा।
Arenas
Multithreaded applications में, heap manager को race conditions से बचाना होता है जो crashes का कारण बन सकती हैं। शुरुआत में यह एक global mutex का उपयोग करके किया जाता था ताकि एक समय में केवल एक थ्रेड ही heap तक पहुंच सके, लेकिन इससे mutex से होने वाला bottleneck performance समस्याएँ पैदा करता था।
इसी समस्या के समाधान के लिए ptmalloc2 heap allocator ने “arenas” पेश किए, जहाँ प्रत्येक arena एक अलग heap की तरह कार्य करता है जिसमें अपना डेटा structures और mutex होता है, जिससे कई थ्रेड्स बिना एक-दूसरे को प्रभावित किए heap ऑपरेशन्स कर सकते हैं, बशर्ते वे अलग arenas का उपयोग करें।
डिफ़ॉल्ट “main” arena single-threaded एप्लिकेशन्स के लिए heap ऑपरेशन्स संभालती है। जब नए थ्रेड्स जोड़े जाते हैं, तो heap manager उन्हें contention कम करने के लिए secondary arenas असाइन करता है। यह पहले प्रत्येक नए थ्रेड को किसी unused arena से जोड़ने की कोशिश करता है, आवश्यकता पड़ने पर नए arenas बनाता है, 32-bit सिस्टम्स के लिए CPU cores की संख्या का 2x और 64-bit के लिए 8x तक सीमित। जब यह सीमा पहुँच जाती है, तो थ्रेड्स को arenas साझा करना पड़ता है, जिससे संभावित contention हो सकती है।
Main arena के विपरीत, जो brk सिस्टम कॉल का उपयोग करके बढ़ती है, secondary arenas “subheaps” बनाते हैं जो mmap और mprotect का उपयोग करके heap व्यवहार का अनुकरण करते हैं, जिससे multithreaded ऑपरेशन्स के लिए मेमोरी प्रबंधन में लचीलापन आता है।
Subheaps
Subheaps multithreaded applications में secondary arenas के लिए मेमोरी रिज़र्व के रूप में कार्य करते हैं, जिससे वे अलग-अलग अपने heap क्षेत्रों को बढ़ा और प्रबंधित कर सकें। यहाँ बताया गया है कि subheaps प्रारंभिक heap से कैसे अलग होते हैं और कैसे काम करते हैं:
- Initial Heap बनाम Subheaps:
- Initial heap बाइनरी के ठीक बाद मेमोरी में स्थित होता है, और यह
sbrkसिस्टम कॉल का उपयोग करके फैलता है। - Subheaps, जो secondary arenas द्वारा उपयोग किए जाते हैं,
mmapके माध्यम से बनाए जाते हैं, एक सिस्टम कॉल जो निर्दिष्ट मेमोरी क्षेत्र को मैप करता है।
mmapके साथ मेमोरी रिज़र्वेशन:
- जब heap manager एक subheap बनाता है, तो यह
mmapके ज़रिये एक बड़ा ब्लॉक मेमोरी रिज़र्व करता है। यह रिज़र्वेशन तुरंत मेमोरी अलोकेट नहीं करता; यह केवल एक क्षेत्र बता देता है जिसे अन्य सिस्टम प्रक्रियाएँ या अलोकेशन्स उपयोग नहीं करनी चाहिए। - डिफ़ॉल्ट रूप से, subheap के लिए रिज़र्व्ड साइज 32-bit प्रोसेस के लिए 1 MB और 64-bit प्रोसेस के लिए 64 MB है।
mprotectके साथ क्रमिक विस्तार:
- रिज़र्व किया गया मेमोरी रीज़न शुरू में
PROT_NONEके रूप में चिन्हित होता है, जिससे kernel को इस स्पेस के लिए फिजिकल मेमोरी तत्काल अलोकेट करने की ज़रूरत नहीं होती। - Subheap को “grow” करने के लिए heap manager
mprotectका उपयोग करता है ताकि पेज परमिशन्सPROT_NONEसेPROT_READ | PROT_WRITEमें बदले जाएँ, जिससे kernel पहले रिज़र्व किए गए एड्रेसेस के लिए फिजिकल मेमोरी अलोकेट करता है। यह कदम-दर-कदम तरीका subheap को आवश्यकतानुसार बढ़ने की अनुमति देता है। - एक बार पूरा subheap खत्म हो जाने पर, heap manager आगे allocation जारी रखने के लिए नया subheap बनाता है।
heap_info
यह struct heap की संबंधित जानकारी को आरक्षित करता है। साथ ही, अधिक allocations के बाद heap मेमोरी लगातार नहीं भी हो सकती है, यह struct उस जानकारी को भी स्टोर करेगा।
// 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 or other threads arenas) में एक malloc_state structure** होता है।\ यह ध्यान देने योग्य है कि **main arena malloc_state** structure libc में एक **global variable** है (इसलिए libc memory space में स्थित)।\ threads के heaps के **malloc_state` structures के मामले में, वे अपने thread के “heap” के अंदर स्थित होते हैं।
इस structure से कुछ दिलचस्प बातें ध्यान देने योग्य हैं (नीचे C code देखें):
-
__libc_lock_define (, mutex);यह सुनिश्चित करने के लिए है कि heap का यह structure एक समय में केवल एक ही thread द्वारा access हो -
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)
- The `mchunkptr bins[NBINS * 2 - 2];` contains **pointers** to the **first and last chunks** of the small, large and unsorted **bins** (the -2 is because the index 0 is not used)
- इसलिए, इन bins का **first chunk** इस structure की ओर एक **backwards pointer** रखेगा और इन bins का **last chunk** इस structure की ओर एक **forward pointer** रखेगा। जिसका मतलब यह है कि अगर आप main arena में इन addresses को leak कर पाते हैं तो आपके पास इस structure का एक pointer libc में होगा।
- The structs `struct malloc_state *next;` और `struct malloc_state *next_free;` arenas की linked lists हैं
- The `top` chunk आखिरी "chunk" है, जो मूल रूप से **पूरा heap का शेष स्थान** होता है। जब top chunk "empty" हो जाता है, तो heap पूरी तरह से उपयोग हो चुका होता है और इसे और space request करने की जरूरत पड़ती है।
- The `last reminder` chunk उन मामलों से आता है जहां एक exact size chunk उपलब्ध नहीं होता और इसलिए एक बड़े chunk को splitter किया जाता है; बचे हुए हिस्से का 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 का प्रतिनिधित्व करती है। विभिन्न fields का अर्थ 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 कुछ metadata भी रखते हैं, जो इस चित्र में बहुत अच्छी तरह दर्शाए गए हैं:
.png)
https://azeria-labs.com/wp-content/uploads/2019/03/chunk-allocated-CS.png
Metadata आमतौर पर 0x08B होता है जो वर्तमान chunk size को सूचित करता है और अंतिम 3 बिट्स यह दर्शाने के लिए उपयोग होते हैं:
A: अगर 1 है तो यह subheap से आता है, अगर 0 है तो यह main arena में हैM: अगर 1 है, तो यह chunk mmap के साथ allocated space का हिस्सा है और heap का हिस्सा नहीं हैP: अगर 1 है, तो previous chunk उपयोग में है
इसके बाद user data के लिए स्थान होता है, और अंत में 0x08B होता है जो previous chunk size को संकेत करता है जब chunk उपलब्ध हो (या जब यह allocated हो तो user data को स्टोर करने के लिए)।
इसके अलावा, जब available होता है, तो user data में कुछ और जानकारी भी रखी जाती है:
fd: अगले chunk के लिए pointerbk: पिछले chunk के लिए pointerfd_nextsize: उस सूची में पहले ऐसे chunk का pointer जो खुद से छोटा हैbk_nextsize: उस सूची में पहले ऐसे chunk का pointer जो खुद से बड़ा है
.png)
https://azeria-labs.com/wp-content/uploads/2019/03/chunk-allocated-CS.png
Tip
ध्यान दें कि इस तरह सूची को लिंक करने से हर एक chunk को रजिस्टर करने के लिए एक array रखने की आवश्यकता नहीं पड़ती।
Chunk पॉइंटर्स
जब malloc का उपयोग किया जाता है तो एक pointer उस कंटेंट के लिए लौटता है जिसे लिखा जा सकता है (headers के ठीक बाद), हालांकि chunks को manage करते समय headers (metadata) की शुरुआत के लिए एक pointer चाहिए होता है.
इन परिवर्तनों के लिए ये functions उपयोग किए जाते हैं:
// 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))
संरेखण और न्यूनतम आकार
chunk के pointer और 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);
}
Note that for calculating the total space needed it’s only added SIZE_SZ 1 time because the prev_size field can be used to store data, therefore only the initial header is needed.
Chunk डेटा प्राप्त करें और metadata बदलें
ये फ़ंक्शन किसी chunk के pointer को प्राप्त करके काम करते हैं और metadata को check/set करने के लिए उपयोगी होते हैं:
- Check chunk flags
// 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)
- आकार और pointers अन्य 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)))
- इन्स्यू बिट
/* 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;
}
उदाहरण
Quick Heap उदाहरण
यह Quick 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");
}
main function के अंत में एक breakpoint सेट करें और चलिए पता लगाते हैं कि जानकारी कहाँ स्टोर की गई थी:
.png)
यह देखा जा सकता है कि string panda 0xaaaaaaac12a0 पर स्टोर था (जो कि x0 के अंदर malloc द्वारा लौटाया गया address था)। 0x10 bytes पहले जांचने पर देखा जा सकता है कि 0x0 दर्शाता है कि previous chunk is not used (लंबाई 0) और इस chunk की लंबाई 0x21 है।
अतिरिक्त आरक्षित स्पेस (0x21-0x10=0x11) added headers (0x10) से आता है और 0x1 इसका मतलब यह नहीं कि 0x21B आरक्षित किया गया था, बल्कि यह current header की लंबाई के आखिरी 3 बिट्स कुछ विशेष अर्थ रखते हैं। चूँकि लंबाई हमेशा 16-byte aligned होती है (in 64bits machines), ये बिट्स वास्तव में length संख्या द्वारा कभी उपयोग नहीं किए जाते।
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>
पिछले उदाहरण को डिबग करने पर देखा जा सकता है कि शुरुआत में केवल 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 & Memory Allocations/Frees
जाँचें कि Bins क्या हैं और वे कैसे व्यवस्थित हैं और Memory कैसे allocate और free होता है:
<a class="content_ref" href="bins-and-memory-allocations.md"><span class="content_ref_label">Bins & Memory Allocations</span></a>
## Heap Functions सुरक्षा जांच
Heap से संबंधित functions अपने actions करने से पहले कुछ checks करेंगे ताकि heap corrupt न हुआ हो, यह सुनिश्चित करने की कोशिश की जा सके:
<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 किये जाते हैं। लंबे linear copies (~0x15555555 bytes) के लिए span को mapped रखें (released groups से बनने वाले holes से बचें) और victim allocation को source slot के सन्निकट रखें।
- **Cycling offset mitigation:** On slot reuse mallocng user-data की start को `UNIT` (0x10) के multiples से आगे बढ़ा सकता है जब slack extra 4-byte header के लिए फिट बैठता है। इससे overwrite offsets शिफ्ट हो जाते हैं (उदा., LSB pointer hits) जब तक आप reuse counts को कंट्रोल न करें या slack बिना strides पर टिके रहें (उदा., Lua `Table` objects at stride 0x50 में offset 0 दिखता है)। offsets की जांच muslheap’s `mchunkinfo` से करें:
```gdb
pwndbg> mchunkinfo 0x7ffff7a94e40
... stride: 0x140
... cycling offset : 0x1 (userdata --> 0x7ffff7a94e40)
-
अलोकेटर मेटाडेटा की तुलना में रनटाइम ऑब्जेक्ट भ्रष्टाचार को प्राथमिकता दें: mallocng cookies/guarded out-of-band metadata को मिलाता है, इसलिए उच्च-स्तर के ऑब्जेक्ट्स को लक्षित करें। Redis’s Lua 5.1 में,
Table->arrayएकTValuetagged values की array की ओर इशारा करता है;TValue->valueमें एक pointer के LSB को overwrite करना (उदा., JSON terminator byte0x22के साथ) रेफरेंस को pivot कर सकता है बिना malloc मेटाडेटा को छुए। -
Alpine पर stripped/static Lua को debug करना: मिलती-जुलती Lua बनाएं,
readelf -Wsसे symbols सूचीबद्ध करें, struct layouts को GDB में उजागर करने के लिएobjcopy --strip-symbolद्वारा function symbols को strip करें, फिर overflow ट्रिगर करने से पहले Lua-aware pretty-printers (GdbLuaExtension for Lua 5.1) और muslheap का उपयोग करके stride/reserved/cycling-offset मानों की जांच करें।
केस स्टडीज़
रियल-वर्ल्ड बग्स से निकाले गए allocator-विशिष्ट primitives का अध्ययन करें:
Virtualbox Slirp Nat Packet Heap Exploitation
Gnu Obstack Function Pointer Hijack
References
- 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 हैकिंग सीखें और अभ्यास करें:
HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें:HackTricks Training GCP Red Team Expert (GRTE)
Azure हैकिंग सीखें और अभ्यास करें:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।


