Pwnable/Tech

-Heap- ptmalloc2의 구조

Kon4 2022. 1. 5. 16:26

bin: free된 청크들이 저장되는 객체

의의: 메모리 낭비를 막음, 해제된 청크를 빠르게 탐색하여 재사용할 수 있게 함.

 

ptmalloc has 128 bin.

 

ALL 128 --

Small bin: 62

Large bin: 63

Unsortedbin: 1

not-used: 2

 

실제 구조

0. not-used

1. unsorted bin

2. small[0]

...

63. smal[61]

64. large[0]

..

126. large[62]

127. not-used

--------------------

small bin: 32byte ~ 1023 byte

small bin[0] : 32byte

..

small bin[61] : 1008byte

small bin structure: circular doubuly-linked list -> FIFO

--> unlink, consolidation

 

##fast bin

fast bin: 32byte~176byte

fastbin: 10 but using 7 / each size: 16byte

Result in linux, 32byte ~ 128byte(using 7 bins)

fast bin structure: singly linked list -> LIFO --> so fast but have unification.

no unlink, no consolidation

Not using BK

 

 

large bin[0]: 1024~1088 .. [32]: 3072 ~ 3583

largebin structure: doubly-linked list

--> unlink, consolidation , 내림차순 정렬

 

unsortedbin

unsortedbin structure: circular doubly-linked list

_. 정렬X

fastbin 외의 모든 청크가 해제되었을때 크기를 구분X --> unsortedbin에 보관.

 

small bin 할당요청 -> Search fastbin or smallbin -> if no detected, Search unstortedbin.

large bin 할당요청 -> Search unstortedbin ->  if no detected, Search fastbin or smallbin.. etc

 

arena : bin들의 정보를 모두 담고 있는 객체.

멀티 스레딩 환경 속 공유 자원 사용으로 인한 레이스 컨디션을 막기 위해 lock을 사용. --> 64개의 arena 가 모두 lock 되어 있으면 "병목 현상"이 생김. 그 결과 ver. gblic 2.26 or over.. 에서 tcache(thread local cache) 객체를 만듬.

*https://rninche01.tistory.com/entry/heap2-glibc-malloc1-feat-Arena 잘정리되어있음.

 

tcache

: arena의 단점을 해소시킬 목적으로나온 , 말그대로, 독립적, 할당 캐시 저장소이다.

 

ALL 64

tcache structure: singly linked list -> LIFO

max chunk: 7

tcache range: 32 ~ 1040 --> range of fast and small bin

**First Search object when chunk size is in tcache range.

*if tcache is fully, Chunk allocates bin of own's size.

 

참고로 tcache | arena(some bins..) 형태로 나눠져 있음.

tcache는 보안 검사가 많이 생략되어 있어서 공격자들에게 힙 익스플로잇의 좋은 도구로 활용