본문 바로가기
Systemhacking/Base info

malloc consolidate with Fastbin_Dup Consolidate

by reindeer002 2023. 6. 4.
728x90

"Malloc consolidate는 개발자 입장에선 external fragmentation을 막을 수 있어서 좋고,
공격자 입장에서는 fastbin을 병합함으로써 small bin(libc) 주소를 leak하거나,
double free(fastbin dup conslidate)할 때 사용한다." ( from brwook )

아래 코드는 https://github.com/andigena/glibc-2.23-0ubuntu3/blob/master/malloc/malloc.c의 코드 중
fastbin에서 unsorted bin으로 free chunk가 이동하는 malloc consolidate에 해당하는 코드이다.

    if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) {
      if (have_fastchunks(av))
	malloc_consolidate(av);

 

  else
    {
      idx = largebin_index (nb);
      if (have_fastchunks (av))
        malloc_consolidate (av);
    }

 

즉, 아래 2가지 상황에서 malloc_consolidate가 발생하게 된다.

  1. large bin 이상의 크기를 할당했을 때
  2. free 했을 때 최종적으로 0x10000 이상의 크기를 지닌 chunk가 만들어 질 때

 예를 들어 아래와 같은 상황으로 heap chunk를 할당했다고 가정해보자.

 

 그럼 첫 번째, 두 번째 free를 거쳐 아래와 같은 bins의 모습을 가질 것이다.
그 이유는 0xa0 size의 chunk가 Top Chunk와 병합되는 과정에서 0x10000을 넘어서는 chunk가 생성되기 때문이다.

0x10000을 넘어서는 chunk가 할당되어 fastbin에서 unsorted bin으로 chunk가 이동했음

 

이제 이 상황에서 free 0x603420을 한번 더 입력하면 해당 chunk가 fastbin에 한번 더 등록된다.
 이미 unsorted bin에 등록되어있는데도 말이다!

0x603410 위치의 chunk가 2번 free 되어 bins에도 2번 등록된 모습

이를 활용하여 Fastbin Dup Consolidate 및 Double Free 취약점을 활용할 수 있을 것이다.

728x90

'Systemhacking > Base info' 카테고리의 다른 글

File Stream Oriented Programming  (0) 2023.07.19
IO_buf_end attack  (0) 2023.07.19
small bin attack  (0) 2023.06.04
unsorted bin attack  (0) 2023.06.02
unsafe Unlink in heap  (0) 2022.11.07

댓글