Heap ni mahali ambapo programu inaweza kuhifadhi data wakati inapoomba data kwa kuita kazi kama malloc, calloc... Aidha, wakati kumbukumbu hii haitahitajika tena inapatikana kwa kuita kazi free.
Kama inavyoonyeshwa, iko tu baada ya ambapo binary inapo load kwenye kumbukumbu (angalia sehemu ya [heap]):
Basic Chunk Allocation
Wakati data fulani inapoombwa kuhifadhiwa kwenye heap, nafasi fulani ya heap inatengwa kwa ajili yake. Nafasi hii itakuwa ya bin na ni data iliyohitajika + nafasi ya vichwa vya bin + ofset ya ukubwa wa chini wa bin itahifadhiwa kwa chunk. Lengo ni kuhifadhi kumbukumbu kidogo iwezekanavyo bila kufanya iwe ngumu kupata kila chunk ilipo. Kwa hili, taarifa za metadata za chunk zinatumika kujua kila chunk iliyotumika/iliyopatikana ilipo.
Kuna njia tofauti za kutenga nafasi kulingana na bin inayotumika, lakini mbinu ya jumla ni kama ifuatavyo:
Programu inaanza kwa kuomba kiasi fulani cha kumbukumbu.
Ikiwa katika orodha ya chunks kuna mtu anapatikana mkubwa wa kutosha kutimiza ombi, itatumika
Hii inaweza hata kumaanisha kwamba sehemu ya chunk inayopatikana itatumika kwa ombi hili na iliyobaki itaongezwa kwenye orodha ya chunks
Ikiwa hakuna chunk inayopatikana kwenye orodha lakini bado kuna nafasi kwenye kumbukumbu ya heap iliyotengwa, meneja wa heap anaunda chunk mpya
Ikiwa hakuna nafasi ya kutosha ya heap kutenga chunk mpya, meneja wa heap anaomba kernel kuongeza kumbukumbu iliyotengwa kwa heap na kisha kutumia kumbukumbu hii kuunda chunk mpya
Ikiwa kila kitu kinashindwa, malloc inarudisha null.
Kumbuka kwamba ikiwa kumbukumbu iliyohitajika inapita kigezo fulani, mmap itatumika kubaini kumbukumbu iliyohitajika.
Arenas
Katika maombi ya multithreaded, meneja wa heap lazima kuzuia mashindano ambayo yanaweza kusababisha ajali. Awali, hii ilifanywa kwa kutumia mutex ya kimataifa kuhakikisha kwamba ni thread moja tu inaweza kufikia heap kwa wakati mmoja, lakini hii ilisababisha masuala ya utendaji kutokana na kuzuiliwa kwa mutex.
Ili kushughulikia hili, allocator wa heap ptmalloc2 ilianzisha "arenas," ambapo kila arena inafanya kazi kama heap tofauti yenye miundo yake ya data na mutex, ikiruhusu nyuzi nyingi kufanya operesheni za heap bila kuingiliana, mradi tu watumie arenas tofauti.
Arena ya "muhimu" inashughulikia operesheni za heap kwa maombi ya nyuzi moja. Wakati nyuzi mpya zinapoongezwa, meneja wa heap anawapa arenas za sekondari ili kupunguza ushindani. Kwanza inajaribu kuunganisha kila nyuzi mpya na arena isiyotumika, ikiumba mpya ikiwa inahitajika, hadi kikomo cha mara 2 ya idadi ya nyuzi za CPU kwa mifumo ya 32-bit na mara 8 kwa mifumo ya 64-bit. Mara kikomo kinapofikiwa, nyuzi lazima zishiriki arenas, na kusababisha ushindani wa uwezekano.
Tofauti na arena ya msingi, ambayo inapanuka kwa kutumia wito wa mfumo wa brk, arenas za sekondari zinaunda "subheaps" kwa kutumia mmap na mprotect ili kuiga tabia ya heap, ikiruhusu kubadilika katika usimamizi wa kumbukumbu kwa operesheni za multithreaded.
Subheaps
Subheaps hutumikia kama akiba ya kumbukumbu kwa arenas za sekondari katika maombi ya multithreaded, ikiruhusu kukua na kusimamia maeneo yao ya heap tofauti na heap kuu. Hapa kuna jinsi subheaps zinavyotofautiana na heap ya awali na jinsi zinavyofanya kazi:
Heap ya Awali vs. Subheaps:
Heap ya awali iko moja kwa moja baada ya binary ya programu kwenye kumbukumbu, na inapanuka kwa kutumia wito wa mfumo wa sbrk.
Subheaps, zinazotumiwa na arenas za sekondari, zinaundwa kupitia mmap, wito wa mfumo unaoelekeza eneo fulani la kumbukumbu.
Hifadhi ya Kumbukumbu kwa kutumia mmap:
Wakati meneja wa heap anaunda subheap, anahifadhi block kubwa ya kumbukumbu kupitia mmap. Hifadhi hii haitoi kumbukumbu mara moja; inateua tu eneo ambalo michakato mingine ya mfumo au hifadhi hazipaswi kutumia.
Kwa kawaida, ukubwa wa hifadhi kwa subheap ni 1 MB kwa michakato ya 32-bit na 64 MB kwa michakato ya 64-bit.
Upanuzi wa Polepole kwa kutumia mprotect:
Eneo la kumbukumbu lililotengwa awali linapewa alama kama PROT_NONE, ikionyesha kwamba kernel haitaji kutenga kumbukumbu halisi kwa nafasi hii bado.
Ili "kukua" subheap, meneja wa heap anatumia mprotect kubadilisha ruhusa za ukurasa kutoka PROT_NONE hadi PROT_READ | PROT_WRITE, ikimhimiza kernel kutenga kumbukumbu halisi kwa anwani zilizotengwa hapo awali. Njia hii ya hatua kwa hatua inaruhusu subheap kupanuka kadri inavyohitajika.
Mara subheap yote itakapokamilika, meneja wa heap anaunda subheap mpya ili kuendelea na hifadhi.
heap_info
Struktura hii inahifadhi taarifa muhimu za heap. Aidha, kumbukumbu ya heap inaweza kuwa si ya mfululizo baada ya hifadhi zaidi, struktura hii pia itahifadhi taarifa hiyo.
// From https://github.com/bminor/glibc/blob/a07e000e82cb71238259e674529c37c12dc7d423/malloc/arena.c#L837typedefstruct _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 mprotectedPROT_READ|PROT_WRITE. */size_t pagesize; /* Page size used when allocating the arena. *//* Make sure the following data is properly aligned, particularlythat sizeof (heap_info) + 2 * SIZE_SZ is a multiple ofMALLOC_ALIGNMENT. */char pad[-3* SIZE_SZ & MALLOC_ALIGN_MASK];} heap_info;
malloc_state
Kila heap (main arena au maeneo mengine ya nyuzi) ina malloc_state structure.
Ni muhimu kutambua kwamba main arena malloc_state structure ni kigezo cha kimataifa katika libc (hivyo iko katika nafasi ya kumbukumbu ya libc).
Katika kesi ya malloc_state structures za heaps za nyuzi, zinapatikana ndani ya "heap" ya nyuzi husika.
Kuna mambo kadhaa ya kuvutia ya kuzingatia kutoka kwa muundo huu (angalia msimbo wa C hapa chini):
__libc_lock_define (, mutex); Ipo ili kuhakikisha kwamba muundo huu kutoka kwa heap unafikiwa na nyuzi 1 kwa wakati mmoja
* `mchunkptr bins[NBINS * 2 - 2];` ina **viungo** kwa **chunks za kwanza na za mwisho** za **bins** ndogo, kubwa na zisizo na mpangilio (the -2 ni kwa sababu index 0 haitumiki)
* Kwa hivyo, **chunk ya kwanza** ya bins hizi itakuwa na **kiungo cha nyuma kwa muundo huu** na **chunk ya mwisho** ya bins hizi itakuwa na **kiungo cha mbele** kwa muundo huu. Ambayo kimsingi inamaanisha kwamba ikiwa unaweza **kuvuja anwani hizi katika main arena** utakuwa na kiungo kwa muundo katika **libc**.
* Struktura `struct malloc_state *next;` na `struct malloc_state *next_free;` ni orodha zilizounganishwa za arenas
* Chunk ya `top` ni "chunk" ya mwisho, ambayo kimsingi ni **nafasi yote iliyobaki ya heap**. Mara tu chunk ya juu inapokuwa "bila", heap inatumika kabisa na inahitaji kuomba nafasi zaidi.
* Chunk ya `last reminder` inatokana na hali ambapo chunk ya ukubwa sahihi haipatikani na kwa hivyo chunk kubwa inakatwa, sehemu ya kiungo iliyobaki inawekwa hapa.
```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
Muundo huu unawakilisha kipande maalum cha kumbukumbu. Nyanja mbalimbali zina maana tofauti kwa vipande vilivyotolewa na visivyotolewa.
// https://github.com/bminor/glibc/blob/master/malloc/malloc.cstruct 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;};typedefstruct malloc_chunk* mchunkptr;
Kama ilivyotajwa hapo awali, vipande hivi pia vina metadata, ambayo inawakilishwa vizuri katika picha hii:
Metadata kwa kawaida ni 0x08B ikionyesha ukubwa wa sasa wa kipande kwa kutumia bits 3 za mwisho kuonyesha:
A: Ikiwa 1 inatoka kwenye subheap, ikiwa 0 iko kwenye arena kuu
M: Ikiwa 1, kipande hiki ni sehemu ya nafasi iliyotengwa na mmap na si sehemu ya heap
P: Ikiwa 1, kipande cha awali kinatumika
Kisha, nafasi ya data ya mtumiaji, na hatimaye 0x08B kuonyesha ukubwa wa kipande cha awali wakati kipande kinapatikana (au kuhifadhi data ya mtumiaji wakati inatengwa).
Zaidi ya hayo, wakati inapatikana, data ya mtumiaji inatumika pia kubeba data fulani:
fd: Kielekezi kwa kipande kinachofuata
bk: Kielekezi kwa kipande cha awali
fd_nextsize: Kielekezi kwa kipande cha kwanza katika orodha ambacho ni kidogo kuliko yenyewe
bk_nextsize: Kielekezi kwa kipande cha kwanza katika orodha ambacho ni kikubwa kuliko yenyewe
Kumbuka jinsi kuunganisha orodha kwa njia hii kunazuia haja ya kuwa na array ambapo kila kipande kimoja kinarekodiwa.
Kielekezi za Kipande
Wakati malloc inatumika, kielekezi kwa maudhui ambayo yanaweza kuandikwa kinarejeshwa (karibu baada ya vichwa), hata hivyo, wakati wa kusimamia vipande, inahitajika kielekezi kwa mwanzo wa vichwa (metadata).
Kwa ajili ya mabadiliko haya, kazi hizi zinatumika:
// https://github.com/bminor/glibc/blob/master/malloc/malloc.c/* Convert a chunk address to a user mem pointer without correcting the tag. */#definechunk2mem(p) ((void*)((char*)(p) + CHUNK_HDR_SZ))/* Convert a user mem pointer to a chunk address and extract the right tag. */#definemem2chunk(mem) ((mchunkptr)tag_at (((char*)(mem) - CHUNK_HDR_SZ)))/* The smallest possible chunk */#defineMIN_CHUNK_SIZE (offsetof(struct malloc_chunk, fd_nextsize))/* The smallest size we can malloc is an aligned minimal chunk */#defineMINSIZE \(unsignedlong)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) &~MALLOC_ALIGN_MASK))
Mwelekeo & ukubwa wa chini
Pointer kwa kipande na 0x0f lazima iwe 0.
// From https://github.com/bminor/glibc/blob/a07e000e82cb71238259e674529c37c12dc7d423/sysdeps/generic/malloc-size.h#L61#defineMALLOC_ALIGN_MASK (MALLOC_ALIGNMENT -1)// https://github.com/bminor/glibc/blob/a07e000e82cb71238259e674529c37c12dc7d423/sysdeps/i386/malloc-alignment.h#defineMALLOC_ALIGNMENT16// https://github.com/bminor/glibc/blob/master/malloc/malloc.c/* Check if m has acceptable alignment */#definealigned_OK(m) (((unsignedlong)(m) & MALLOC_ALIGN_MASK) ==0)#definemisaligned_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 constantif passed a literal constant. */#definerequest2size(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 resultingvalue is less than PTRDIFF_T. Returns the requested size orMINSIZE in case the value is less than MINSIZE, or 0 if any of theprevious checks fail. */staticinlinesize_tchecked_request2size (size_t req) __nonnull (1){if (__glibc_unlikely (req > PTRDIFF_MAX))return0;/* When using tagged memory, we cannot share the end of the userblock with the header for the next chunk, so ensure that weallocate blocks that are rounded up to the granule size. Takecare not to overflow from close to MAX_SIZE_T to a smallnumber. Ideally, this would be part of request2size(), but thatmust be a macro that produces a compile time constant if passeda 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);}returnrequest2size (req);}
Kumbuka kwamba kwa kuhesabu jumla ya nafasi inayohitajika, SIZE_SZ inaongezwa mara 1 tu kwa sababu uwanja wa prev_size unaweza kutumika kuhifadhi data, hivyo basi kichwa cha awali pekee kinahitajika.
Pata data ya Chunk na badilisha metadata
Hizi kazi zinafanya kazi kwa kupokea kiashiria kwa chunk na ni muhimu kuangalia/set metadata:
Angalia bendera za 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 */#definePREV_INUSE0x1/* extract inuse bit of previous chunk */#defineprev_inuse(p) ((p)->mchunk_size & PREV_INUSE)/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */#defineIS_MMAPPED0x2/* check for mmap()'ed chunk */#definechunk_is_mmapped(p) ((p)->mchunk_size & IS_MMAPPED)/* size field is or'ed with NON_MAIN_ARENA if the chunk was obtainedfrom a non-main arena. This is only set immediately before handingthe chunk to the user, if necessary. */#defineNON_MAIN_ARENA0x4/* Check for chunk from main arena. */#definechunk_main_arena(p) (((p)->mchunk_size & NON_MAIN_ARENA) ==0)/* Mark a chunk as not being on the main arena. */#defineset_non_main_arena(p) ((p)->mchunk_size |= NON_MAIN_ARENA)
Sizes na viashiria vya vipande vingine
/*Bits to mask off when extracting sizeNote: IS_MMAPPED is intentionally not masked off from size field inmacros for which mmapped chunks should never be seen. This shouldcause helpful core dumps to occur if it is tried by accident bypeople extending or adapting this malloc.*/#defineSIZE_BITS (PREV_INUSE | IS_MMAPPED | NON_MAIN_ARENA)/* Get size, ignoring use bits */#definechunksize(p) (chunksize_nomask (p) &~(SIZE_BITS))/* Like chunksize, but do not mask SIZE_BITS. */#definechunksize_nomask(p) ((p)->mchunk_size)/* Ptr to next physical malloc_chunk. */#definenext_chunk(p) ((mchunkptr) (((char*) (p)) +chunksize (p)))/* Size of the chunk below P. Only valid if !prev_inuse (P). */#defineprev_size(p) ((p)->mchunk_prev_size)/* Set the size of the chunk below P. Only valid if !prev_inuse (P). */#defineset_prev_size(p, sz) ((p)->mchunk_prev_size = (sz))/* Ptr to previous physical malloc_chunk. Only valid if !prev_inuse (P). */#defineprev_chunk(p) ((mchunkptr) (((char*) (p)) -prev_size (p)))/* Treat space at ptr + offset as a chunk */#definechunk_at_offset(p, s) ((mchunkptr) (((char*) (p)) + (s)))
Weka kichwa na mguu (wakati nambari za kipande zinatumika)
/* Set size at head, without disturbing its use bit */#defineset_head_size(p, s) ((p)->mchunk_size = (((p)->mchunk_size & SIZE_BITS) | (s)))/* Set size/use field */#defineset_head(p, s) ((p)->mchunk_size = (s))/* Set size at footer (only when chunk is not in use) */#defineset_foot(p, s) (((mchunkptr) ((char*) (p) + (s)))->mchunk_prev_size = (s))
Pata ukubwa wa data halisi inayoweza kutumika ndani ya kipande
#pragmaGCCpoisonmchunk_size#pragmaGCCpoisonmchunk_prev_size/* This is the size of the real usable data in the chunk. Not valid fordumped heap chunks. */#definememsize(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 granulesize, 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;}
Set a breakpoint at the end of the main function and lets find out where the information was stored:
Ni rahisi kuona kwamba string panda iliwekwa kwenye 0xaaaaaaac12a0 (ambayo ilikuwa anwani iliyotolewa kama jibu na malloc ndani ya x0). Kuangalia 0x10 bytes kabla inawezekana kuona kwamba 0x0 inawakilisha kwamba kipande cha awali hakitumiki (urefu 0) na kwamba urefu wa kipande hiki ni 0x21.
Nafasi za ziada zilizohifadhiwa (0x21-0x10=0x11) zinatokana na vichwa vilivyoongezwa (0x10) na 0x1 haimaanishi kwamba ilihifadhiwa 0x21B bali bits 3 za mwisho za urefu wa kichwa cha sasa zina maana maalum. Kwa kuwa urefu daima umeunganishwa kwa byte 16 (katika mashine za 64bits), bits hizi kwa kweli hazitakuwa zitatumika na nambari ya urefu.
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
Mfano wa Multithreading
Multithread
```c #include #include #include #include #include
void* 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 2\n"); ret = pthread_create(&t1, NULL, threadFuncNoMalloc, NULL);
printf("Before exit\n"); getchar();
return 0; }
</details>
Kwa kuangalia mfano wa awali, inawezekana kuona jinsi mwanzoni kuna arena 1 tu:
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
Kisha, baada ya kuita thread ya kwanza, ile inayoitwa malloc, arena mpya inaundwa:
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
na ndani yake kuna chunks kadhaa zinazopatikana:
<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
## Bins & Allocations za Kumbukumbu/Kuondoa
Angalia ni zipi bins na jinsi zilivyoandaliwa na jinsi kumbukumbu inavyotolewa na kuondolewa katika:
<div data-gb-custom-block data-tag="content-ref" data-url='bins-and-memory-allocations.md'>
[bins-and-memory-allocations.md](bins-and-memory-allocations.md)
</div>
## Ukaguzi wa Usalama wa Kazi za Heap
Kazi zinazohusiana na heap zitafanya ukaguzi fulani kabla ya kutekeleza vitendo vyake ili kujaribu kuhakikisha kuwa heap haijaharibiwa:
<div data-gb-custom-block data-tag="content-ref" data-url='heap-memory-functions/heap-functions-security-checks.md'>
[heap-functions-security-checks.md](heap-memory-functions/heap-functions-security-checks.md)
</div>
## Marejeleo
* [https://azeria-labs.com/heap-exploitation-part-1-understanding-the-glibc-heap-implementation/](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/](https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/)