mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: Konstantin Serebryany <konstantin.s.serebryany@gmail.com>,
	Rich Felker <dalias@libc.org>,
	musl@lists.openwall.com
Subject: Re: buffer overflow in regcomp and a way to find more of those
Date: Sat, 28 Mar 2015 23:00:19 +0100	[thread overview]
Message-ID: <20150328220018.GG3071@port70.net> (raw)
In-Reply-To: <20150323123540.GP16260@port70.net>

* Szabolcs Nagy <nsz@port70.net> [2015-03-23 13:35:40 +0100]:
> * Konstantin Serebryany <konstantin.s.serebryany@gmail.com> [2015-03-22 21:55:26 -0700]:
> > On Sat, Mar 21, 2015 at 6:28 AM, Szabolcs Nagy <nsz@port70.net> wrote:
> > > i assume for that we still need to change the libc startup code, malloc
> > > functions and may be some things around thread stacks
> > 
> > Try to compile a simple file with asan:
> > 
> > int main(int argc, char **argv) {
> >   int a[10];
> >   a[argc * 10] = 0;
> >   return 0;
> > }
> > 
> > 
> > % clang -fsanitize=address  a.c -c
> > 
> > % nm a.o | grep U
> >                  U __asan_init_v5
> >                  U __asan_option_detect_stack_use_after_return
> >                  U __asan_report_store4
> >                  U __asan_stack_malloc_1
> > 
> > __asan_report_store4 should print an error message saying that
> > "bad write of 4 bytes" happened in <current stack trace> on address <param>.
> > Also make  other __asan_report_{store,load}{1,2,4,8,16}
> > 
> > __asan_init_v5 will be called by the module initializer.
> > When called for the first time, it should mmap the shadow memory.
> > https://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
> > 

it seems asan intrumented code with memory access cannot run
before __asan_init_v5 does the shadow mapping (otherwise the
compiler generated shadow access would crash)

this is problematic for dynamic linking because the loader
calls various libc functions so those cannot be instrumented
unless shadow memory is already in place

i managed to make a minimal asan runtime work with static linking
(and then stack corruption is indeed detected).
(i called __asan_init_v5 in the begining of musl's __libc_start_main)

> > __asan_option_detect_stack_use_after_return is a global, define it to 0.
> > __asan_stack_malloc_1 -- just make it an empty function.
> > 
> > Now, you can build a code with asan and detect stack buffer overflows.
> > (The reports won't be very detailed, but they will be correct).
> > If you add poisoned redzones to malloc -- you get heap buffer overflows.
> > If you delay the reuse of free-d memory -- you get use-after-free.
> > 
> > If you then implement __asan_register_globals (it is called on module
> > initialization and poisons redzones for globals)
> > you get global buffer overflows.
> > 

i havent tried to do the heap/global poisoning

it's not clear to me what's the best way to manage the shadow
memory: mmap with PROT_NONE the entire 0x7fff8000 .. 0x10007fff8000
range and then mmap with rw the subranges that shadow mmaped memory
in the application?

then a modified mmap is needed to manage the shadow maps

so i think for a asan+cov instrumented libc:

- [S]crt1.s should do the initial shadow mmap before any c code gets run
- mmap should be replaced to do shadow management
- malloc etc should be replaced to handle shadow poisoning
- the minimal asan and cov runtimes should be added to libc
(so their symbols are available early in the loader)

and then we can use such a libc for testing and fuzzing
to catch heap/stack corruptions

i guess it is possible to have a /lib/ld-muslasan-x86_64.so.1
and Scrt1asan.o on a system and the compiler/linker could
use those when compiling some code with asan+cov instrumentation
(but this can get ugly if there will be more instrumentations
that need runtime support in the future)


  parent reply	other threads:[~2015-03-28 22:00 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-20 20:17 Konstantin Serebryany
2015-03-20 20:40 ` Rich Felker
2015-03-20 21:28 ` Szabolcs Nagy
2015-03-20 23:48   ` Szabolcs Nagy
2015-03-20 22:32 ` Rich Felker
2015-03-20 23:52 ` Szabolcs Nagy
2015-03-21  0:06   ` Konstantin Serebryany
2015-03-21  0:26     ` Szabolcs Nagy
2015-03-21  0:46       ` Rich Felker
2015-03-21  0:54         ` Konstantin Serebryany
2015-03-21  1:00           ` Rich Felker
2015-03-21  1:05             ` Konstantin Serebryany
2015-03-21  1:10               ` Konstantin Serebryany
2015-03-21  1:23                 ` Szabolcs Nagy
2015-03-21  1:30                   ` Rich Felker
2015-03-21  2:10                     ` Szabolcs Nagy
2015-03-21  2:17                       ` Rich Felker
2015-03-21  1:32               ` Rich Felker
2015-03-21  1:37                 ` Konstantin Serebryany
2015-03-21  1:56                   ` Rich Felker
2015-03-21  2:14                     ` Konstantin Serebryany
2015-03-21  2:20                       ` Rich Felker
2015-03-21  6:05                         ` Konstantin Serebryany
2015-03-21 13:28                           ` Szabolcs Nagy
2015-03-21 21:03                             ` Szabolcs Nagy
2015-03-21 21:38                               ` Szabolcs Nagy
2015-03-21 22:13                                 ` Szabolcs Nagy
2015-03-22  6:36                                   ` Justin Cormack
2015-03-23  5:02                               ` Konstantin Serebryany
2015-03-23 12:25                                 ` Szabolcs Nagy
2015-03-23 15:56                                   ` Konstantin Serebryany
2015-03-23  4:55                             ` Konstantin Serebryany
2015-03-23 12:35                               ` Szabolcs Nagy
2015-03-23 14:40                                 ` stephen Turner
2015-03-23 14:53                                   ` Szabolcs Nagy
2015-03-23 15:46                                     ` stephen Turner
2015-03-23 16:28                                       ` Rich Felker
2015-03-23 17:21                                         ` Nathan McSween
2015-03-28 22:00                                 ` Szabolcs Nagy [this message]
2015-03-28 22:32                                   ` Konstantin Serebryany
2015-03-28 22:38                                     ` Rich Felker
2015-03-28 23:15                                       ` Szabolcs Nagy

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=20150328220018.GG3071@port70.net \
    --to=nsz@port70.net \
    --cc=dalias@libc.org \
    --cc=konstantin.s.serebryany@gmail.com \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).