Double Free Bug & Use After Free
2026. 3. 24. 01:16

원데이 익스에 앞서, DFB/UAF를 간단히 정리해보려고한다. 

Double Free Bug?

같은 포인터를 두 번 free를 하면, free list / fastbin / tcache안에 같은 크기의 청크가 중복 등록될 수 있다.

즉, 할당자 입장에서 `free list = [p] -> [p] -> ...` 처럼 되게된다. (정상적이라면, 같은 청크는 한번만 존재해야함.)

그 결과, malloc을 여러번 호출하면 같은 주소가 반복해서 바환되거나 freelist 연결구조를 조작할 발판이 생기게된다.

 

p = malloc(0x40);
free(p);
free(p);

a = malloc(0x40); //  a == p
b = malloc(0x40); //  b == p

 

서로 다른 두 포인터가 같은 물리적 chunk를 받는 overlap이 발생하게된다!

 

DFB만으로는 보통 익스플로잇까지는 힘들 수 있다. 따라서 그다음에 나오는 primitive가 더 중요할 수 있다.

// 정상 free list
free list : p -> next
// double free 후
free list : p -> p -> next
// malloc 1:
free list : p -> next
// malloc 2:
free list : next
-> a == b == p 가 되는 것!! 이때 a에 뭔가를 쓰게되면 b객체 또한 덮히게된다.

즉 double free는 overlap을 만드는 원인으로 작동한다.

 

UAF는 바로 exploit primitive이고, double free는 그 primitive를 만들기 위한 취약점이다.

  • UAF : 바로 exploit 가능 
    • primitive: dangling pointer read/write/call
  • DFB
    • -> allocator corruption -> overlap / arbitrary write -> exploit
    • primitive: overlap / arbitrary allocation / poisoning
  • DFB -> UAF-like
    • 같은 청크 재할당 -> stale pointer 유지 -> UAF-like 상태 -> exploit
    • primitive: overlap + stale pointer access

Use After Free

free 후 사용하는 것으로 프로그램 로직이 직접 잘못된 경우이다.

free(obj);
obj->fn();

 

DFB + UAF-like

UAF-like란, double free / allocator corruption / overlap 가 원인이고 그 결과 stale pointer로 재사용된 메모리를 만지게되는 것으로 UAF랑은 root cause가 다르다.

DFB로 힙 상태를 꼬아놓고 그다음 stale pointer를 사용해서 UAF처럼 행동하는 것.

notes[0] = malloc(0x40);
alias = notes[0];

free(notes[0]);
free(alias);   // double free

// + 같은 청크가 다시 할당? -> 거의 UAF와 동일해짐.

a = malloc(size); // returns X
b = malloc(size); // returns X again

 

-> 원래 취약점은 double free지만, exploit단게에서 공격자가 쓰는 것은 stale pointer access, reused chunk overwrite 즉, UAF같은 사용 패턴을 사용한다.

 

// 객체 생성

free list: X -> next // allocator 내부
notes[0] -> chunk X // 객체 상태

// free 두 번 했을때 
free list: X -> X -> next
notes[0] -> X

// 두번 재할당 : 3개의 포인터가 같은 청크를 가리키게됨.
note[0] -> x                a -> x                b ->x

이렇게 꼬여버린 힙에서 하나의 포인터를 이용해서 stale pointer 역할을 수행하면, 결과적으로 해제/재사용된 메모리를 계속 만질 수 있게 된다.

 

보통 실전 문제들에선 `DFB -> tcache dup -> same chunk returned twice -> overlap -> overwrite target object -> hijack` 으로 exploit을 작성하긴함.

 

CVE-2010-0249 (heap spray + UAF)

tistraw0454
tistraw0454
tistraw0454 님의 블로그 입니다.