ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 0x07.How2heap - fastbin_dup_consolidate
    0x.Heap Tutorial 2018. 4. 9. 19:15

    How2heap - fastbin_dup_consolidate



    최근에 how2heap에 추가된 취약점인데, 조금 신기하기도 한 취약점이다.

    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>

    int main() {
    void* p1 = malloc(0x40);
    void* p2 = malloc(0x40);
    fprintf(stderr, "Allocated two fastbins: p1=%p p2=%p\n", p1, p2);
    fprintf(stderr, "Now free p1!\n");
    free(p1);

    void* p3 = malloc(0x400);
    fprintf(stderr, "Allocated large bin to trigger malloc_consolidate(): p3=%p\n", p3);
    fprintf(stderr, "In malloc_consolidate(), p1 is moved to the unsorted bin.\n");
    free(p1);
    fprintf(stderr, "Trigger the double free vulnerability!\n");
    fprintf(stderr, "We can pass the check in malloc() since p1 is not fast top.\n");
    fprintf(stderr, "Now p1 is in unsorted bin and fast bin. So we'will get it twice: %p %p\n", malloc(0x40), malloc(0x40));
    }                      


    이번에도 예전처럼 순서대로 분석을 하도록 하겠다.


    void* p1 = malloc(0x40);
    void* p2 = malloc(0x40);


    p1, p2로 0x40의 크기로 malloc을 하였고, 그 주소값은 아래와 같다.


    Allocated two fastbins: p1=0x801010 p2=0x801060


    이제 p1을 해제를 하면


    free(p1);


    fastbin에 추가가 될 것이다.


    arena를 확인해보니




    fastbin안에 0x602000가 들어가있다.

    그런데 여기에서 재밌는 일이 발생된다.


    void* p3 = malloc(0x400);

    만약 p3와 같이 largebin이 생성이되면, malloc_consolidate()가 호출되는데, 이 놈으로 인하여 해제된 p1이 unsorted bin으로 들어가게 된다.


    p3=0x8010b0





    0x602000의 값이 변경되었고, smallbin 영역에 0x60200이 들어가있다.

    p3가 largebin으로 할당이 된 뒤에는, p1이 다시한번 free가 된다!


    free(p1);


    힙 영역을 보니




    fastbin과 smallbin에 같은 영역인 0x602000이 들어가있다.

    p1이 fastbin과 unsorted bin에 이중으로 걸치게 되면서 아래와 같은 경우가 발생한다.


    %p %p\n", malloc(0x40), malloc(0x40));


    결과를 보면


    0x801010 0x801010


    같은 값이다. 힙 영역으로 보면



    첫 malloc에서 fastbin으로 할당이 되고,




    두 번째 malloc에서 smallbin 영역으로 할당이 된다.

    왜 이런일이 발생하는걸까?

    Fastbin Consolidate는 large bin 이상의 크기로 동적할당 요청이 들어오면 다음 청크가 Top Chunk인지를 확인한 다음, Top Chunk가 아닐 경우에 fastbin을 검사하여 smallbin으로 병합해버리기 때문이다. 그리하여 p1이라는 놈은 fastbin이면서도 병합을 거친 unsortedbin 이기도 한 것이다. 그리하여 double free가 트리거되는 취약점이 발생하게 되는 것이다.

    '0x.Heap Tutorial' 카테고리의 다른 글

    0x09.How2heap - house_of_einherjar  (0) 2018.11.22
    0x08.How2heap - unsafe_unlink  (0) 2018.04.10
    0x06.Malloc hook  (0) 2018.03.19
    0x05.How2heap - fastbin_dup_into_stack  (0) 2018.03.12
    0x04.How2heap - fastbin_dup  (0) 2018.03.08

    댓글

Designed by Tistory.