Commit graph

76 commits

Author SHA1 Message Date
Ben Wiederhake
5574d45eda DevTools: Mark compilation-unit-only functions as static
This enables a nice warning in case a function becomes dead code. Also, in case
of signal_trampoline_dummy, marking it external (non-static) prevents it from
being 'optimized away', which would lead to surprising and weird linker errors.
2020-08-12 20:40:59 +02:00
Brian Gianforcaro
0f42463eab Kernel: Use Userspace<T> for the execve syscall 2020-08-10 12:52:15 +02:00
Andreas Kling
fae9c9f81f UserspaceEmulator: Add the dup2 syscall 2020-08-07 18:46:56 +02:00
Andreas Kling
5dce5fa7c2 UserspaceEmulator: Add the chdir syscall 2020-08-07 18:44:51 +02:00
Andreas Kling
ee5e8081da UserspaceEmulator: Pass full path to new UE instance in virt$execve()
Don't just pass argv[0] to the new UE, pass the full program path.
2020-08-07 17:28:00 +02:00
Andreas Kling
5a5b687014 UserspaceEmulator: Add the getpgid() and waitid() syscalls
With this, you can now kinda sorta run the shell in UserspaceEmulator!
2020-08-07 16:51:08 +02:00
Andreas Kling
93b1e54237 UserspaceEmulator: Add the setpgid syscall 2020-08-07 16:34:50 +02:00
Andreas Kling
2b3b83801b UserspaceEmulator: Make the "unimplemented syscall" output look nicer 2020-08-07 16:34:50 +02:00
Andreas Kling
2f1d596dd3 UserspaceEmulator: Fix bad rc check in ttyname and getcwd syscalls
Errors here are (rc < 0), not (rc < 1).
2020-08-06 11:45:52 +02:00
Andreas Kling
d608d714b9 UserspaceEmulator: Support ioctl(TCGETS) and ioctl(TCSETS) 2020-08-05 22:34:50 +02:00
Andreas Kling
e0e3e5b9b1 UserspaceEmulator: Add the access syscall 2020-08-05 22:34:50 +02:00
Andreas Kling
c497603177 UserspaceEmulator: Add the getcwd syscall 2020-08-05 22:34:50 +02:00
Andreas Kling
b187a42e53 UserspaceEmulator: Add the ttyname syscall 2020-08-05 22:34:50 +02:00
Andreas Kling
9d93e208ac UserspaceEmulator: Support ioctl(TIOCSPGRP) 2020-08-05 22:34:50 +02:00
Andreas Kling
3717a00290 UserspaceEmulator: Add the getpgrp syscall 2020-08-05 22:34:50 +02:00
Andreas Kling
8dea25d974 UserspaceEmulator: Add support for UNIX signals :^)
The emulator will now register signal handlers for all possible signals
and act as a translation layer between the kernel and the emulated
process.

To get an accurate simulation of signal handling, we duplicate the same
trampoline mechanism used by the kernel's signal delivery system, and
also use the "sigreturn" syscall to return from a signal handler.

Signal masking is not fully implemented yet, but this is pretty cool!
2020-08-05 22:34:50 +02:00
Andreas Kling
dff8305138 UserspaceEmulator: Don't print backtrace offsets if we have file/line
File/line is way more interesting than offsets, so let's only do the
offsets if we don't have file/line information.
2020-08-01 09:49:03 +02:00
Andreas Kling
5de7bae383 UserspaceEmulator: Forward errors from fork() correctly 2020-08-01 09:11:18 +02:00
Andreas Kling
c7e4c0734b UserspaceEmulator: Use a report() function instead of dbgprintf()
Ultimately we'll want to make it a bit easier to add more reporting.
This at least makes it easier to redirect the logging.
2020-07-31 20:56:48 +02:00
Andreas Kling
b8d3dbcf2d UserspaceEmulator: Add syscalls: stat(), realpath(), gethostname()
This is enough to run /bin/ls :^)
2020-07-28 00:03:25 +02:00
Andreas Kling
9def88e08d UserspaceEmulator: Don't just return "EMULATED" in get_process_name()
Now that emulated processes have their real name (with a "(UE)" prefix)
we can actually let them know their name.
2020-07-28 00:03:25 +02:00
Andreas Kling
2ac5c2278d UserspaceEmulator: Support ioctl(TIOCGWINSZ)
This is very commonly used by terminal programs, and easy to support.
2020-07-28 00:03:25 +02:00
Andreas Kling
0b287c18b9 UserspaceEmulator: Implement the execve() syscall :^)
This virtual syscall works by exec'ing the UserspaceEmulator itself,
with the emulated program's provided arguments as the arguments to the
new UserspaceEmulator instance.

This means that we "follow" exec'ed programs and emulate them as well.
In the future we might want to make this an opt-in (or opt-out, idk)
behavior, but for now it's what we do.

This is really quite cool, I think! :^)
2020-07-27 19:10:18 +02:00
Andreas Kling
b9b74e355a UserspaceEmulator: Implement the fork() syscall :^) 2020-07-27 19:10:18 +02:00
Andreas Kling
d9f933df7b UserspaceEmulator: Implement the setuid() and setgid() syscalls
Note that running a setuid program (e.g /bin/ping) in UE does not
actually run uid=0. You'll have to run UE itself as uid=0 if you want
to test programs that do setuid/setgid.
2020-07-27 16:32:30 +02:00
Andreas Kling
368cea4094 UserspaceEmulator: Implement the accept() and setsockopt() syscalls
It's now possible to run LookupServer in UE (by setting up SystemServer
to run the service inside UE.) No bugs found, but very cool! :^)
2020-07-27 16:28:40 +02:00
Andreas Kling
f097ed6ada UserspaceEmulator: Transfer the environment to the emulated process 2020-07-27 15:57:12 +02:00
Andreas Kling
9c155c8f35 UserspaceEmulator: Tweak some output strings 2020-07-21 23:35:09 +02:00
Andreas Kling
abebec0e04 UserspaceEmulator: Use the base address of instructions in backtraces
Instead of using SoftCPU::eip() which points at the *next* instruction
most of the time, stash away a "base EIP" so we can use it when making
backtraces. This makes the correct line number show up! :^)
2020-07-21 19:08:01 +02:00
Andreas Kling
0f91dfa139 UserspaceEmulator: Show file and line numbers in backtraces :^)
This was super easy thanks to the awesome LibDebug work by @itamar8910!
2020-07-21 19:08:01 +02:00
Andreas Kling
d1dd5013ea UserspaceEmulator: Remove unnecessary local getpid() caches
Now that LibC caches this for us, we can stop worrying.
2020-07-21 19:08:01 +02:00
Andreas Kling
15753e9633 UserspaceEmulator: Don't hardcode the amount of thread-local data
This made it impossible to add more thread-local things to LibC. :^)
2020-07-21 19:08:01 +02:00
Andreas Kling
903c5b0833 UserspaceEmulator: Mark the full initial TCB as initialized memory 2020-07-21 16:27:54 +02:00
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
a49c794725 UserspaceEmulator: Add the get_dir_entries() syscall + an ioctl() stub 2020-07-18 17:57:40 +02:00
Andreas Kling
75500b449c UserspaceEmulator: Fix every line in backtraces showing EIP
Oops, we're supposed to show the return address for each frame, not the
current EIP every time. :^)
2020-07-18 00:25:02 +02:00
Andreas Kling
b17d175379 UserspaceEmulator: Add the usleep() syscall 2020-07-16 21:38:01 +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
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
7e13244238 UserspaceEmulator: Add ways to check if a Region is stack/mmap 2020-07-16 19:21:45 +02:00
Andreas Kling
67cdbe1925 UserspaceEmulator: Cache the location and size of "malloc" and "free"
This allows us to quickly skip some auditing checks while we're inside
malloc/free themselves.
2020-07-16 00:24:11 +02:00
Andreas Kling
33e3e8d63d UserspaceEmulator: Add the getrandom() syscall 2020-07-15 23:44:51 +02:00
Andreas Kling
1873b8f3e4 UserspaceEmulator: Fix bogus use of "errno" in shbuf related syscalls
When we're making direct syscalls, there's no "errno" involved.
Thanks to Sergey for spotting these.
2020-07-15 23:43:38 +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
Andreas Kling
c8b496162d UserspaceEmulator: Add some more syscalls :^)
Here's set_process_icon(), gettimeofday() and clock_gettime().
2020-07-15 18:47:45 +02:00
Andreas Kling
2da44dba44 UserspaceEmulator: Add support for shared buffers (shbuf)
We track these separately from regular mmap() regions, as they have
slightly different behaviors.
2020-07-15 18:47:45 +02:00
Andreas Kling
339f12e8a2 UserspaceEmulator: Implement an assortment of system calls
Here goes mkdir(), unlink(), socket(), getsockopt(), fchmod()
bind(), connect(), listen(), select() and recvfrom().

They're not perfect but they seem to work. :^)
2020-07-15 18:47:45 +02:00
Sergey Bugaev
e12b591509 UserspaceEmulator: Implement virt$pipe() 2020-07-15 13:41:46 +02:00
Andreas Kling
a27473cbc2 UserspaceEmulator+LibX86: Turn on -O3 optimization for emulation code
Since this code is performance-sensitive, let's have the compiler do
whatever it can to help us with the most important files.

This yields a ~8% speedup.
2020-07-13 20:23:00 +02:00