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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Heap Basics
Heap์ ํ๋ก๊ทธ๋จ์ด malloc, calloc ๋ฑ๊ณผ ๊ฐ์ ํจ์๋ฅผ ํธ์ถํด ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ณต๊ฐ์ด๋ค. ๋ํ ์ด ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ ์ด์ ํ์ํ์ง ์์ผ๋ฉด free ํจ์๋ฅผ ํธ์ถํด ๋ฐํ๋๋ค.
As itโs shown, its just after where the binary is being loaded in memory (check the [heap] section):
.png)
Basic Chunk Allocation
๋ฐ์ดํฐ๋ฅผ heap์ ์ ์ฅํ๋๋ก ์์ฒญํ๋ฉด heap์ ์ผ๋ถ ๊ณต๊ฐ์ด ํ ๋น๋๋ค. ์ด ๊ณต๊ฐ์ bin์ ์ํ๋ฉฐ ์์ฒญํ ๋ฐ์ดํฐ + bin headers์ ๊ณต๊ฐ + ์ต์ bin ํฌ๊ธฐ ์คํ์ ๋ง chunk๋ก ์์ฝ๋๋ค. ๋ชฉํ๋ ๊ฐ chunk์ ์์น๋ฅผ ์ฐพ๊ธฐ ๋ณต์กํ๊ฒ ๋ง๋ค์ง ์์ผ๋ฉด์ ๊ฐ๋ฅํ ์ต์ ๋ฉ๋ชจ๋ฆฌ๋ง ์์ฝํ๋ ๊ฒ์ด๋ค. ์ด๋ฅผ ์ํด metadata chunk ์ ๋ณด๊ฐ ์ฌ์ฉ๋์ด ๊ฐ ์ฌ์ฉ/ํด์ ๋ chunk์ ์์น๋ฅผ ํ์ ํ๋ค.
๊ณต๊ฐ์ ์์ฝํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉ๋๋ bin์ ๋ฐ๋ผ ๋ค๋ฅด์ง๋ง, ์ผ๋ฐ์ ์ธ ์ ์ฐจ๋ ๋ค์๊ณผ ๊ฐ๋ค:
- ํ๋ก๊ทธ๋จ์ด ์ผ์ ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ฒญํ๋ค.
- chunk ๋ชฉ๋ก์์ ์์ฒญ์ ์ถฉ์กฑํ ์ ์๋ ์ถฉ๋ถํ ํฐ ๊ฐ์ฉ chunk๊ฐ ์์ผ๋ฉด ๊ทธ๊ฒ์ ์ฌ์ฉํ๋ค.
- ์ด๋ ๊ฐ์ฉ chunk์ ์ผ๋ถ๋ง ์ด ์์ฒญ์ ์ฌ์ฉ๋๊ณ ๋จ์ ๋ถ๋ถ์ chunk ๋ชฉ๋ก์ ๋ค์ ์ถ๊ฐ๋ ์ ์์์ ์๋ฏธํ๋ค.
- ๋ชฉ๋ก์ ์ฌ์ฉ ๊ฐ๋ฅํ chunk๊ฐ ์์ง๋ง ์์ง ํ ๋น๋ heap ๋ฉ๋ชจ๋ฆฌ์ ๊ณต๊ฐ์ด ๋จ์์๋ค๋ฉด, heap ๊ด๋ฆฌ์๋ ์ chunk๋ฅผ ์์ฑํ๋ค.
- ์ chunk๋ฅผ ํ ๋นํ ๋งํ heap ๊ณต๊ฐ์ด ๋ถ์กฑํ๋ฉด, heap ๊ด๋ฆฌ์๋ ์ปค๋์ heap์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ์ฅํ๋๋ก ์์ฒญํ ๋ค ์ด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํด ์ chunk๋ฅผ ๋ง๋ ๋ค.
- ๋ชจ๋ ๊ฒ์ด ์คํจํ๋ฉด
malloc์ null์ ๋ฐํํ๋ค.
์์ฒญํ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์๊ณ๊ฐ์ ๋์ผ๋ฉด, **mmap**์ด ์ฌ์ฉ๋์ด ์์ฒญํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋งคํํ๋ค.
Arenas
๋ฉํฐ์ค๋ ๋ ์ ํ๋ฆฌ์ผ์ด์ ์์ heap ๊ด๋ฆฌ์๋ ์ถฉ๋๋ก ์ธํ ํฌ๋์๋ฅผ ๋ง๊ธฐ ์ํด race conditions๋ฅผ ๋ฐฉ์งํด์ผ ํ๋ค. ์ด๊ธฐ์๋ ์ ์ญ mutex๋ฅผ ์ฌ์ฉํด ํ ๋ฒ์ ํ๋์ ์ค๋ ๋๋ง heap์ ์ ๊ทผํ๋๋ก ํ์ง๋ง, ์ด๋ mutex๋ก ์ธํ ๋ณ๋ชฉ ํ์ ๋๋ฌธ์ ์ฑ๋ฅ ๋ฌธ์ ๋ฅผ ์ผ์ผ์ผฐ๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ptmalloc2 heap allocator๋ โarenasโ๋ฅผ ๋์ ํ๋ค. ๊ฐ arena๋ ์์ฒด ๋ฐ์ดํฐ ๊ตฌ์กฐ์ mutex๋ฅผ ๊ฐ์ง ๋ณ๋์ heap์ฒ๋ผ ๋์ํ์ฌ, ์๋ก ๋ค๋ฅธ arena๋ฅผ ์ฌ์ฉํ๋ ํ ์ฌ๋ฌ ์ค๋ ๋๊ฐ ์๋ก ๊ฐ์ญํ์ง ์๊ณ heap ์์ ์ ์ํํ ์ ์๋ค.
๊ธฐ๋ณธ โmainโ arena๋ ๋จ์ผ ์ค๋ ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ heap ์์ ์ ์ฒ๋ฆฌํ๋ค. ์๋ก์ด ์ค๋ ๋๊ฐ ์ถ๊ฐ๋๋ฉด heap ๊ด๋ฆฌ์๋ ๊ฒฝ์์ ์ค์ด๊ธฐ ์ํด ์ด๋ค์๊ฒ secondary arenas๋ฅผ ํ ๋นํ๋ค. ๊ด๋ฆฌ์๋ ๋จผ์ ์ฌ์ฉ๋์ง ์๋ arena์ ์๋ก์ด ์ค๋ ๋๋ฅผ ๋ถ์ด๋ ค ์๋ํ๊ณ , ํ์ํ๋ฉด ์ arena๋ฅผ ์์ฑํ๋ฉฐ, 32๋นํธ ์์คํ ์์๋ CPU ์ฝ์ด ์์ 2๋ฐฐ, 64๋นํธ ์์คํ ์์๋ 8๋ฐฐ๊น์ง ์์ฑํ๋ค. ํ๋์ ๋๋ฌํ๋ฉด ์ค๋ ๋๋ค์ด arena๋ฅผ ๊ณต์ ํด์ผ ํ๋ฏ๋ก ๊ฒฝ์์ด ๋ฐ์ํ ์ ์๋ค.
main arena๋ brk ์์คํ
์ฝ์ ์ฌ์ฉํด ํ์ฅ๋๋ ๋ฐ๋ฉด, secondary arenas๋ mmap๊ณผ mprotect๋ฅผ ์ฌ์ฉํด subheaps๋ฅผ ์์ฑํ์ฌ heap ๋์์ ์๋ฎฌ๋ ์ด์
ํ๋ฏ๋ก ๋ฉํฐ์ค๋ ๋ ์์
์ ๋ํ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์ ์ ์ฐ์ฑ์ ์ ๊ณตํ๋ค.
Subheaps
Subheaps๋ ๋ฉํฐ์ค๋ ๋ ์ ํ๋ฆฌ์ผ์ด์ ์์ secondary arenas๊ฐ ๋ณ๋๋ก ์ฑ์ฅํ๊ณ ์์ ๋ง์ heap ์์ญ์ ๊ด๋ฆฌํ ์ ์๋๋ก ํ๋ ๋ฉ๋ชจ๋ฆฌ ์์ฝ ๊ณต๊ฐ์ด๋ค. Subheaps๊ฐ ์ด๊ธฐ heap๊ณผ ์ด๋ป๊ฒ ๋ค๋ฅธ์ง ๋ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง๋ ๋ค์๊ณผ ๊ฐ๋ค:
- Initial Heap vs. Subheaps:
- ์ด๊ธฐ heap์ ๋ฐ์ด๋๋ฆฌ ๋ฐ๋ก ๋ค์ ์์นํ๋ฉฐ
sbrk์์คํ ์ฝ์ ์ฌ์ฉํด ํ์ฅ๋๋ค. - secondary arenas์์ ์ฌ์ฉํ๋ subheaps๋ ์ง์ ๋ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ ๋งคํํ๋ ์์คํ
์ฝ์ธ
mmap์ ํตํด ์์ฑ๋๋ค.
- Memory Reservation with
mmap:
- heap ๊ด๋ฆฌ์๊ฐ subheap์ ์์ฑํ ๋,
mmap์ ํตํด ํฐ ๋ธ๋ก์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ฝํ๋ค. ์ด ์์ฝ์ ์ฆ์ ๋ฌผ๋ฆฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ์ง ์์ผ๋ฉฐ, ๋จ์ง ๋ค๋ฅธ ์์คํ ํ๋ก์ธ์ค๋ ํ ๋น์ด ํด๋น ์์ญ์ ์ฌ์ฉํ์ง ๋ชปํ๋๋ก ์์ญ์ ์ง์ ํ๋ ๊ฒ์ด๋ค. - ๊ธฐ๋ณธ์ ์ผ๋ก subheap์ ์์ฝ ํฌ๊ธฐ๋ 32๋นํธ ํ๋ก์ธ์ค์์ 1 MB, 64๋นํธ ํ๋ก์ธ์ค์์ 64 MB์ด๋ค.
- Gradual Expansion with
mprotect:
- ์์ฝ๋ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ ์ฒ์์
PROT_NONE๋ก ํ์๋์ด ์ปค๋์ด ์์ง ๋ฌผ๋ฆฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ ํ์๊ฐ ์์์ ๋ํ๋ธ๋ค. - subheap์ โ์ฑ์ฅโ์ํค๊ธฐ ์ํด heap ๊ด๋ฆฌ์๋
mprotect๋ฅผ ์ฌ์ฉํ์ฌ ํ์ด์ง ๊ถํ์PROT_NONE์์PROT_READ | PROT_WRITE๋ก ๋ณ๊ฒฝํ๊ณ , ์ด๋ก ์ธํด ์ปค๋์ ์ด์ ์ ์์ฝ๋ ์ฃผ์์ ๋ฌผ๋ฆฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๋ค. ์ด ๋จ๊ณ๋ณ ์ ๊ทผ ๋ฐฉ์์ subheap์ด ํ์์ ๋ฐ๋ผ ํ์ฅ๋๋๋ก ํ์ฉํ๋ค. - ์ ์ฒด subheap์ด ์์ง๋๋ฉด heap ๊ด๋ฆฌ์๋ ์๋ก์ด subheap์ ์์ฑํ์ฌ ํ ๋น์ ๊ณ์ํ๋ค.
heap_info
์ด struct๋ heap์ ๋ํ ๊ด๋ จ ์ ๋ณด๋ฅผ ํ ๋นํ๋ค. ๋ํ ์ถ๊ฐ ํ ๋น ์ดํ์๋ 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 ๋๋ ๋ค๋ฅธ ์ค๋ ๋์ arenas)์๋ malloc_state ๊ตฌ์กฐ์ฒด๊ฐ ์์ต๋๋ค.
ํนํ main arena malloc_state ๊ตฌ์กฐ์ฒด๋ libc ์์ ์ ์ญ ๋ณ์(๋ฐ๋ผ์ libc ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์์น)๋ผ๋ ์ ์ด ์ค์ํฉ๋๋ค.
์ค๋ ๋๋ค์ heap์ ์๋ malloc_state ๊ตฌ์กฐ์ฒด๋ค์ ๊ฐ ์ค๋ ๋์ โheapโ ๋ด๋ถ์ ์์นํฉ๋๋ค.
๋ค์์ ์ด ๊ตฌ์กฐ์ฒด์์ ์ฃผ๋ชฉํ ๋งํ ๋ช ๊ฐ์ง ์ฌํญ์ ๋๋ค(์๋ C ์ฝ๋ ์ฐธ๊ณ ):
-
__libc_lock_define (, mutex);๋ heap์ ์ด ๊ตฌ์กฐ์ฒด์ ํ ๋ฒ์ ํ ์ค๋ ๋๋ง ์ ๊ทผํ๋๋ก ๋ณด์ฅํ๊ธฐ ์ํด ์กด์ฌํฉ๋๋ค. -
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];`๋ small, large ๋ฐ unsorted bins์ first์ last chunks์ ๋ํ pointers๋ฅผ ํฌํจํฉ๋๋ค( -2๋ index 0์ด ์ฌ์ฉ๋์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค).
- ๋ฐ๋ผ์ ์ด bins๋ค์ **first chunk**๋ ์ด ๊ตฌ์กฐ์ฒด๋ก ํฅํ๋ **backwards pointer**๋ฅผ ๊ฐ์ง๋ฉฐ, **last chunk**๋ ์ด ๊ตฌ์กฐ์ฒด๋ก ํฅํ๋ **forward pointer**๋ฅผ ๊ฐ์ง๋๋ค. ์ฆ, main arena์์ ์ด๋ฌํ ์ฃผ์๋ฅผ leakํ ์ ์๋ค๋ฉด libc ์์ ๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ์ป๊ฒ ๋ฉ๋๋ค.
- `struct malloc_state *next;` ๋ฐ `struct malloc_state *next_free;` ๊ตฌ์กฐ์ฒด๋ค์ arenas์ linked list์
๋๋ค.
- `top` chunk์ ๋ง์ง๋ง "chunk"๋ก, ๊ธฐ๋ณธ์ ์ผ๋ก ๋จ์์๋ ๋ชจ๋ heap ๊ณต๊ฐ์
๋๋ค. `top` chunk๊ฐ "๋น์์ง๋ฉด", heap์ด ์์ ํ ์ฌ์ฉ๋ ๊ฒ์ด๋ฉฐ ๋ ๋ง์ ๊ณต๊ฐ์ ์์ฒญํด์ผ ํฉ๋๋ค.
- `last reminder` chunk๋ ์ ํํ ํฌ๊ธฐ์ chunk๊ฐ ์์ ๋ ๋ ํฐ chunk๋ฅผ ๋ถํ ํ์ฌ ๋จ๋ ๋ถ๋ถ์ ํฌ์ธํฐ๊ฐ ์ฌ๊ธฐ์ ๋์ด๋ ๊ฒฝ์ฐ์์ ์ต๋๋ค.
```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 chunk์ ๋ํด ์๋ก ๋ค๋ฅธ ์๋ฏธ๋ฅผ ๊ฐ์ง๋๋ค.
// 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;
As commented previously, these chunks also have some metadata, very good represented in this image:
.png)
https://azeria-labs.com/wp-content/uploads/2019/03/chunk-allocated-CS.png
The metadata is usually 0x08B indicating the current chunk size using the last 3 bits to indicate:
A: If 1 it comes from a subheap, if 0 itโs in the main arenaM: If 1, this chunk is part of a space allocated with mmap and not part of a heapP: If 1, the previous chunk is in use
Then, the space for the user data, and finally 0x08B to indicate the previous chunk size when the chunk is available (or to store user data when itโs allocated).
Moreover, when available, the user data is used to contain also some data:
fd: Pointer to the next chunkbk: Pointer to the previous chunkfd_nextsize: Pointer to the first chunk in the list is smaller than itselfbk_nextsize: Pointer to the first chunk the list that is larger than itself
.png)
https://azeria-labs.com/wp-content/uploads/2019/03/chunk-allocated-CS.png
Tip
Note how liking the list this way prevents the need to having an array where every single chunk is being registered.
Chunk Pointers
When malloc is used a pointer to the content that can be written is returned (just after the headers), however, when managing chunks, itโs needed a pointer to the begining of the headers (metadata).
For these conversions these functions are used:
// 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๋ฅผ ํ์ธ/์ค์ ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค:
- 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 to other 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;
}
์์
๋น ๋ฅธ Heap ์์
https://guyinatuxedo.github.io/25-heap/index.html์ ๋น ๋ฅธ heap ์์ ์ด์ง๋ง arm64์ฉ:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
char *ptr;
ptr = malloc(0x10);
strcpy(ptr, "panda");
}
main ํจ์์ ๋์ breakpoint๋ฅผ ์ค์ ํ๊ณ ์ ๋ณด๊ฐ ์ด๋์ ์ ์ฅ๋์๋์ง ์์๋ณด์:
.png)
๋ฌธ์์ด panda๊ฐ 0xaaaaaaac12a0์ ์ ์ฅ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค(์ด ์ฃผ์๋ x0 ์์์ malloc์ด ๋ฐํํ ์ฃผ์๋ค). 0x10 ๋ฐ์ดํธ ์์ ํ์ธํด๋ณด๋ฉด 0x0์ด previous chunk is not used(๊ธธ์ด 0)์ ๋ํ๋ด๋ฉฐ, ์ด ์ฒญํฌ์ ๊ธธ์ด๋ 0x21์ด๋ค.
์ถ๊ฐ๋ก ์์ฝ๋ ๊ณต๊ฐ(0x21-0x10=0x11)์ added headers(0x10)์์ ์จ ๊ฒ์ด๋ฉฐ, 0x1์ 0x21B๊ฐ ์์ฝ๋์๋ค๋ ์๋ฏธ๊ฐ ์๋๋ผ ํ์ฌ ํค๋ ๊ธธ์ด์ ๋ง์ง๋ง 3๋นํธ๊ฐ ํน๋ณํ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ค๋ ๋ป์ด๋ค. ๊ธธ์ด๋ ํญ์ 16-byte aligned(64bits machines)์ด๋ฏ๋ก, ์ด ๋นํธ๋ค์ ์ค์ ๊ธธ์ด ์ซ์์์๋ ์ฌ์ฉ๋์ง ์๋๋ค.
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>
์ด์ ์์ ๋ฅผ ๋๋ฒ๊น
ํ๋ฉด ์์ํ ๋ arena๊ฐ 1๊ฐ๋ง ์์์ ํ์ธํ ์ ์๋ค:
<figure><img src="../../images/image (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
๊ทธ๋ฐ ๋ค์ ์ฒซ ๋ฒ์งธ thread, ์ฆ malloc์ ํธ์ถํ๋ thread๋ฅผ ์คํํ๋ฉด ์๋ก์ด 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๊ฐ ๋ฌด์์ธ์ง, ์ด๋ป๊ฒ ์กฐ์ง๋๋์ง, ๊ทธ๋ฆฌ๊ณ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ด๋ป๊ฒ ํ ๋น๋๊ณ ํด์ ๋๋์ง ํ์ธํ๋ผ:
<a class="content_ref" href="bins-and-memory-allocations.md"><span class="content_ref_label">Bins & Memory Allocations</span></a>
## Heap ํจ์ ๋ณด์ ๊ฒ์ฌ
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๋ ์ฌ๋กฏ์ด ๋น์ด ์์ ๋ ์์ ํ `munmap()`๋๋ mmap()'d ๊ทธ๋ฃน์ ์ฌ์ฉํ๋ค. ๊ธด ์ ํ ๋ณต์ฌ(~0x15555555 bytes)์ ๊ฒฝ์ฐ, span์ ๋งคํ๋ ์ํ๋ก ์ ์ง(ํด์ ๋ ๊ทธ๋ฃน์ผ๋ก ์ธํ ๊ตฌ๋ฉ์ ํผํจ)ํ๊ณ victim allocation์ source slot์ ์ธ์ ํ๊ฒ ๋ฐฐ์นํ๋ผ.
- **Cycling offset mitigation:** slot ์ฌ์ฌ์ฉ ์ mallocng์ slack์ด ์ถ๊ฐ 4-byte header๋ฅผ ์์ฉํ ๋ user-data ์์์ `UNIT` (0x10) ๋ฐฐ์๋งํผ ์๋น๊ธธ ์ ์๋ค. ์ด๋ก ์ธํด overwrite offsets(์: LSB pointer hits)๊ฐ ์ด๋ํ๋ค. reuse counts๋ฅผ ์ ์ดํ๊ฑฐ๋ slack์ด ์๋ strides(์: stride 0x50์ Lua `Table` ๊ฐ์ฒด๋ offset 0์ ๋ณด์)๋ฅผ ๊ณ ์ํ์ง ์๋ ํ ๊ทธ๋ ๋ค. offsets๋ muslheapโs `mchunkinfo`๋ก ๊ฒ์ฌํ๋ผ:
```gdb
pwndbg> mchunkinfo 0x7ffff7a94e40
... stride: 0x140
... cycling offset : 0x1 (userdata --> 0x7ffff7a94e40)
- Prefer runtime-object corruption over allocator metadata: mallocng mixes cookies/guarded out-of-band metadata, so target higher-level objects. In Redisโs Lua 5.1,
Table->arraypoints to an array ofTValuetagged values; overwriting the LSB of a pointer inTValue->value(e.g., with the JSON terminator byte0x22) can pivot references without touching malloc metadata. - Debugging stripped/static Lua on Alpine: ์ผ์นํ๋ Lua๋ฅผ ๋น๋ํ๊ณ ,
readelf -Ws๋ก ์ฌ๋ณผ์ ํ์ธํ ๋ค์objcopy --strip-symbol๋ก ํจ์ ์ฌ๋ณผ์ ์ ๊ฑฐํด GDB์์ ๊ตฌ์กฐ์ฒด ๋ ์ด์์์ ๋ ธ์ถ์ํค์ธ์. ๊ทธ๋ฐ ๋ค์ Lua-aware pretty-printers (GdbLuaExtension for Lua 5.1)์ muslheap์ ์ฌ์ฉํด ์ค๋ฒํ๋ก๋ฅผ ํธ๋ฆฌ๊ฑฐํ๊ธฐ ์ ์ stride/reserved/cycling-offset ๊ฐ๋ค์ ํ์ธํ์ธ์.
Case Studies
์ค์ ๋ฒ๊ทธ์์ ํ์๋ allocator-specific 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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


