pwn

advanced understanding

about heap

Posted by pic4xiu on September 23, 2019

Studyed by 走位,orz

main thread (don’t have heap_info just one heap)heap use brk (adjoin with data)

use program break location to adjust the main arena(global variable)

thread heap use mmap(can contain many heaps)

  • arena -> heap -> chunk
struct malloc_state
{
  /* Serialize access.  */
  mutex_t mutex;
  /* Flags (formerly in max_fast).  */
  int flags;
  /* 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.  */
  struct malloc_state *next_free;
  /* Memory allocated from the system in this arena.  */
  INTERNAL_SIZE_T system_mem;
  INTERNAL_SIZE_T max_system_mem;
};
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.  */
  /* Make sure the following data is properly aligned, particularly
     that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
     MALLOC_ALIGNMENT. */
  char pad[-6 * SIZE_SZ & MALLOC_ALIGN_MASK];
} heap_info;
struct malloc_chunk {
  /* #define INTERNAL_SIZE_T size_t */
  INTERNAL_SIZE_T      prev_size;  /* Size of previous chunk (if free).  */
  INTERNAL_SIZE_T      size;       /* Size in bytes, including overhead. */
  struct malloc_chunk* fd;         /* double links -- used only if free. 这两个指针只在free chunk中存在*/
  struct malloc_chunk* bk;
  /* Only used for large blocks: pointer to next larger size.  */
  struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
  struct malloc_chunk* bk_nextsize;
};

two picture

last remainder chunk

when I malloc(small chunk) but my small bin and unsorted bin can not satisfy,I will ergodic binmaps to find the most fit chunk .if this chunk has rest,the rest will be put into unsorted bin and we call it last remainer chunk

fast bin

first malloc(fast bin) , system will execute _int_malloc function , it will find that fast bin is null , and small bin is also null , so will execute malloc_consolidate to initialize malloc_state

malloc_consolidate

  • initialize malloc_state
  • initialize bins

malloc_state

  • bins pointe to themselves

unsorted bin

circulating double linked list

No restrictions for size

small bin

smaller than 512 bytes

  • FIFO
  • also circulating double linked list
  • need to be merged
  • 16 + 61*8 = 508

when you free(small bin) , system will check the bin adjoin the other free bin or not , if adjoin ,system will merge them and fall off from small bin , and put them into unsorted bin

large bin

larged tham 512 bytes

Sort from big to small in one bin , the largest will be put into front end and the smallest will be rear end