1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-25 15:55:07 +00:00
Commit graph

14 commits

Author SHA1 Message Date
Andreas Kling
be5f42adea UserspaceEmulator+LibX86: Start tracking uninitialized memory :^)
This patch introduces the concept of shadow bits. For every byte of
memory there is a corresponding shadow byte that contains metadata
about that memory.

Initially, the only metadata is whether the byte has been initialized
or not. That's represented by the least significant shadow bit.

Shadow bits travel together with regular values throughout the entire
CPU and MMU emulation. There are two main helper classes to facilitate
this: ValueWithShadow and ValueAndShadowReference.

ValueWithShadow<T> is basically a struct { T value; T shadow; } whereas
ValueAndShadowReference<T> is struct { T& value; T& shadow; }.

The latter is used as a wrapper around general-purpose registers, since
they can't use the plain ValueWithShadow memory as we need to be able
to address individual 8-bit and 16-bit subregisters (EAX, AX, AL, AH.)

Whenever a computation is made using uninitialized inputs, the result
is tainted and becomes uninitialized as well. This allows us to track
this state as it propagates throughout memory and registers.

This patch doesn't yet keep track of tainted flags, that will be an
important upcoming improvement to this.

I'm sure I've messed up some things here and there, but it seems to
basically work, so we have a place to start! :^)
2020-07-21 02:37:29 +02:00
Andreas Kling
df58ea808e UserspaceEmulator: Skip freed mallocations in reachability scan
Something being reachable from a freed mallocation doesn't make it
actually reachable.

Thanks to Jonas Bengtsson for spotting this! :^)
2020-07-17 00:24:23 +02:00
Andreas Kling
27aa2e5841 UserspaceEmulator: Reset malloc backtrace on mallocation reuse
If a previously-freed malloc chunk is reused, forget any old backtraces
and save a new malloc backtrace.
2020-07-16 20:55:41 +02:00
Andreas Kling
e50874621a UserspaceEmulator: Don't scan text segment for malloc leaks
There will be no (true positive) malloc addresses in the text segment.
2020-07-16 19:27:03 +02:00
Andreas Kling
3dc1c80958 UserspaceEmulator: Print the number of bytes leaked on exit :^) 2020-07-16 19:21:45 +02:00
Andreas Kling
c13da77e85 UserspaceEmulator: Add TLS regions to reachability checking 2020-07-16 19:21:45 +02:00
Andreas Kling
1dcc21d32e UserspaceEmulator: Include malloc/free backtraces in UAF logs :^)
When catching a use-after-free, we now also print out the backtraces
for where the memory was allocated, and for where it was freed.

This will be extremely helpful for debugging.
2020-07-16 19:21:45 +02:00
Andreas Kling
dd68370efc UserspaceEmulator: Put the memory reachability logging behind a macro 2020-07-16 19:21:45 +02:00
Andreas Kling
441918be7e UserspaceEmulator: Capture backtraces of malloc/free events
This lets us show backtraces for each leaked mallocation in the leak
report at the end. :^)
2020-07-16 19:21:45 +02:00
Andreas Kling
f6584bfc36 UserspaceEmulator: Implement very basic leak checking :^)
Upon exit, the emulator will now print a leak report of any malloc
allocations that are still live and don't have pointers to their base
address anywhere in either another live mallocation, or in one of the
non-malloc-block memory regions.

Note that the malloc-block memory region check is not fully functional
and this will work even better once we get that fixed.

This is pretty cool. :^)
2020-07-16 19:21:45 +02:00
Andreas Kling
323ec4c2ca UserspaceEmulator: Let's say "Use-after-free" instead of "UAF"
I don't know why I went with the compact format here.
2020-07-16 00:50:55 +02:00
Andreas Kling
c7cf87c9f8 UserspaceEmulator: Don't complain about free(nullptr) 2020-07-15 23:53:58 +02:00
Andreas Kling
092f643119 UserspaceEmulator: Log invalid and double free() calls :^)
We can easily catch free() on never-malloced addresses, as well as
double calls to free() on the same address, so let's do it!
2020-07-15 23:25:20 +02:00
Andreas Kling
c314292319 UserspaceEmulator: Catch use-after-frees by tracking malloc/free :^)
This patch introduces a "MallocTracer" to the UserspaceEmulator.
If this object is present on the Emulator, it can be notified whenever
the emulated program does a malloc() or free().

The notifications come in via a magic instruction sequence that we
embed in the LibC malloc() and free() functions. The sequence is:

    "salc x2, push reg32 x2, pop reg32 x3"

The data about the malloc/free operation is in the three pushes.
We make sure the sequence is harmless when running natively.

Memory accesses on MmapRegion are then audited to see if they fall
inside a known-to-be-freed malloc chunk. If so, we complain loud
and red in the debugger output. :^)

This is very, very cool! :^)

It's also a whole lot slower than before, since now we're auditing
memory accesses against a new set of metadata. This will need to be
optimized (and running in this mode should be opt-in, perhaps even
a separate program, etc.)
2020-07-15 23:25:20 +02:00