public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: up201407890@alunos.dcc.fc.up.pt
To: libc-alpha@sourceware.org
Subject: use-after-free / double-free exploit mitigation
Date: Wed, 06 Sep 2017 12:47:00 -0000	[thread overview]
Message-ID: <20170906144653.14363oywmmoc9ug4@webmail.alunos.dcc.fc.up.pt> (raw)

Hello list,

What are your thoughts on adding a SAFE_FREE() macro to glibc:

#define SAFE_FREE(x) do { if((x) != 0x0) { free(x); (x) = (void *)0x1;  
} } while(0)

After free(x), we set x to an address that will crash when  
dereferenced (use-after-free), and will also crash when it's an  
argument to free(). Note that NULL isn't used, because free(NULL) does  
nothing, which might hide potential double-free bugs.

The pointer passed to free() isn't always an lvalue we can assign to,  
e.g free(func()), but when a var is used it will detect use-after-free  
and double-free bugs by having the program crash instead of allowing  
various heap grooms and further exploitation.


Here's a trivial example,

Crashes when use-after-free:

$ cat test.c
#include <unistd.h>
#include <stdlib.h>

#define SAFE_FREE(x) do { if((x) != 0x0) { free(x); (x)=(void *)0x1; }  
} while(0)

struct unicorn_counter { int num; };

int main() {
     struct unicorn_counter* p_unicorn_counter;
     int* run_calc = malloc(sizeof(int));
     *run_calc = 0;
     SAFE_FREE(run_calc);
     //SAFE_FREE(run_calc);
     p_unicorn_counter = malloc(sizeof(struct unicorn_counter));
     p_unicorn_counter->num = 42;
     if (*run_calc) // use-after-free
        execlp("/usr/bin/id", "id", 0);
}

$ gcc test.c
$ ./a.out
Segmentation fault
$ gdb ./a.out
gdb$ r

...

0x4005ed <main+87>:	mov    eax,DWORD PTR [rax]

Stopped reason: SIGSEGV
0x00000000004005ed in main ()
gdb$ print /x $rax
$1 = 0x1



Crashes when double-free, by uncommenting 2nd SAFE_FREE(run_calc):

$ cat test.c
#include <unistd.h>
#include <stdlib.h>

#define SAFE_FREE(x) do { if((x) != 0x0) { free(x); (x)=(void *)0x1; }  
} while(0)

struct unicorn_counter { int num; };

int main() {
     struct unicorn_counter* p_unicorn_counter;
     int* run_calc = malloc(sizeof(int));
     *run_calc = 0;
     SAFE_FREE(run_calc);
     SAFE_FREE(run_calc); // double-free
     p_unicorn_counter = malloc(sizeof(struct unicorn_counter));
     p_unicorn_counter->num = 42;
     if (*run_calc) execlp("/usr/bin/id", "id", 0);
}

$ gcc test.c
$ gdb ./a.out
gdb$ r

...

0x7ffff7aad614 <__GI___libc_free+20>:	mov    rax,QWORD PTR [rdi-0x8]

Stopped reason: SIGSEGV
__GI___libc_free (mem=0x1) at malloc.c:2929
warning: Source file is more recent than executable.
$ print /x $rdi
$1 = 0x1

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

             reply	other threads:[~2017-09-06 12:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-06 12:47 up201407890 [this message]
2017-09-07 16:00 ` Florian Weimer
2017-09-08 12:45   ` Martin Sebor
2017-09-09 17:59 Federico Manuel Bento
2017-09-10 15:41 ` Martin Sebor
2017-09-11 15:36   ` Federico Manuel Bento

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170906144653.14363oywmmoc9ug4@webmail.alunos.dcc.fc.up.pt \
    --to=up201407890@alunos.dcc.fc.up.pt \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).