Commit Graph

111 Commits

Author SHA1 Message Date
Ariel Abreu
127442f146
dtape: host_info: Fix how RAM size is reported
`basic_info->memory_size` is 32-bit; using it for `basic_info->max_mem`
truncates the actual value.
2022-03-21 00:19:24 -04:00
Ariel Abreu
878918aa18
dtape: Implement a few duct-taped functions for neofetch
`mach_vm_region`, `thread_info`, and `TASK_THREAD_TIMES_INFO`
2022-03-15 01:59:46 -04:00
Ariel Abreu
0c08310cc5
dtape: Fix mach_vm_remap
It was using the byte size as the page count, which resulted in mapping many more fixed pages than necessary, overwriting existing memory.
2022-03-10 10:50:49 -05:00
Ariel Abreu
10e4d99c93
Remove constexpr from Call::callNumberToString
Some older compilers can't detect that this is actually constexpr.
2022-03-09 18:41:59 -05:00
Ariel Abreu
2cf1226395
Turn build options into proper CMake options 2022-03-09 17:44:54 -05:00
Ariel Abreu
cb4353fcd2
dtape: Implement a few more duct-taped XNU calls
Also, make sure to terminate the IPC space when destroying a task;
this is what actually terminates ports and turns them into dead names.
2022-03-09 17:35:29 -05:00
Ariel Abreu
db67387fcd
Allow calls to be unmanaged
Unmanaged calls are those that can come from unmanaged processes,
i.e. processes that the server does not control. They can also come from
managed processes, but they don't have to.

This commit does not introduce any unmanaged calls, however.
During local development, I created one and later decided to discard it.
However, this does seem like a useful feature, so it's being added with
this commit.
2022-03-09 17:33:33 -05:00
Ariel Abreu
d33faaa3d8
Treat ECONNREFUSED like EPIPE when sending messages 2022-03-09 17:29:30 -05:00
Ariel Abreu
ddac152b11
Add some code useful for debugging
The main debugging code added is for keeping track of port names and
their associated IPC objects, as well as keeping track of the members of
port sets.

Additionally, when extended debugging is enabled, the server can now
wait for a debugger with the new env var `DSERVER_WAIT4DEBUGGER`.

The AsyncWriter class was originally written for some additional
debugging code I wrote but later decided wouldn't really be useful.
I kept the AsyncWriter class, however, as it seems it might be useful
for future code (it's basically fire-and-forget asynchronous writing).
Note that it has not been tested at all.
2022-03-09 17:28:37 -05:00
Ariel Abreu
1be73cf3ae
Remove threads from ksyn queues upon death 2022-03-04 14:24:44 -05:00
Ariel Abreu
bce5f3049b
Re-work sigexc handling code
We now handle the sigexc calls as normal calls, with the exception that
it's okay for them to become active while another call was active.

We also set the thread's wait result to THREAD_INTERRUPTED and handle
syscall returns in interrupted continuations by jumping back to the
sigexc_enter code.
2022-03-04 12:51:57 -05:00
Ariel Abreu
99da1d77c9
dtape: Partially implement mach_vm_remap
It doesn't support memory sharing or copying to a map other than the
current task yet. However, the LKM didn't support the latter case either,
so the only thing we're really missing is the ability to create a shared
region from a previously private one.
2022-03-04 12:49:17 -05:00
Ariel Abreu
06f9612fa2
Fix race for call destruction when sleeping a microthread
When a microthread went to sleep with a continuation, we discarded its
call. This would lead to the call being disposed before we had a chance
to reply to it. Instead, now we keep a reference to it in the thread
until we send a reply for it.
2022-03-04 00:29:21 -05:00
Ariel Abreu
65cee94d6c
dtape: Initialize host statistics properly 2022-03-03 00:13:03 -05:00
Ariel Abreu
d2fa20cdd1
dtape: Safely stub HOST_VM_INFO in host_statistics 2022-03-03 00:12:31 -05:00
Ariel Abreu
2179e01ebf
dtape: Allow locks to be locked from non-microthread contexts 2022-03-03 00:12:02 -05:00
Ariel Abreu
fa7257ddfd
kqchan: Re-enable ipc_mqueue_override_send
This function works properly now.

Additionally, add some more debug code to Mach port kqchannels,
but remove some debug code from `misc.c` (it causes issues with ASAN).
2022-03-03 00:11:21 -05:00
Ariel Abreu
342245fa21
dtape getters: Return NULL if input is NULL
Found via UBSAN
2022-03-03 00:09:29 -05:00
Ariel Abreu
a7e054c57e
Properly handle task and thread death
It was mainly thread death that needed fixing; we need to remove
duct-taped threads from their task's thread list.
2022-03-03 00:08:38 -05:00
Ariel Abreu
ba7ef096a2
Fix for systems without the "index" option in overlayfs 2022-03-03 00:05:47 -05:00
Ariel Abreu
8c2fc2e364
Inform ASAN about our microthreading business
If we don't tell ASAN about what we're doing with microthreads, it gets
majorly confused about what's going on and generates lots of false
positives.

This is mainly because it poisons the stack, which we re-use when we
start a microthread from a new point (e.g. from a continuation or a new
call). The solution is simple: just tell ASAN that that memory for the
stack is OK when we do this.

Informing ASAN about our microthreads (which it calls "fibers") is just
a neat bonus to have it recognize our microthread stacks as actual stacks
and not just random memory.
2022-03-03 00:04:49 -05:00
Ariel Abreu
3c8765b284
Prevent threads from being doubly impersonated
This means that it should now be impossible for microthread to be running
on two different threads simultaneously, impersonated or not.
2022-03-02 23:56:00 -05:00
Ariel Abreu
1ac70a952a
Make sure no microthreading occurs during dtape_init 2022-03-02 23:52:54 -05:00
Ariel Abreu
c242e7237e
workers: Add option to make work queues single threaded 2022-03-02 23:50:00 -05:00
Ariel Abreu
97e36ba645
calls: De-duplicate generated internal code a bit 2022-03-02 23:46:32 -05:00
Ariel Abreu
02e1023db1
calls: Add console_open
This RPC call gives the caller a socket to which it can write to to log
to the server's log stream.

This is used to give userspace a place to put messages for "/dev/console".
Most notably, launchd tries to log to this device for important log
messages. This allows us to capture those messages.
2022-03-02 23:44:00 -05:00
Ariel Abreu
63c421a4cd
build: Add variables to turn ASAN and UBSAN on and off 2022-03-02 23:37:07 -05:00
Ariel Abreu
edfad52389
Attempt to handle signals while call is active
This is an early attempt at getting signal handling working when there is a call already active.
2022-02-26 10:38:45 -05:00
Ariel Abreu
50d60abf27
dtape: Initialize mk_timer zone 2022-02-25 22:59:43 -05:00
Ariel Abreu
466351c067
dtape: Implement HOST_PREFERRED_USER_ARCH 2022-02-25 22:49:56 -05:00
Ariel Abreu
fcdd0c28c5
dtape: Partially implement task_info 2022-02-25 22:41:01 -05:00
Ariel Abreu
4897515c34
dtape: Implement task{,_name}_for_pid and pid_for_task 2022-02-25 21:57:14 -05:00
Ariel Abreu
54756d6159
dtape: Move hook typedefs into their own header 2022-02-25 20:50:44 -05:00
Ariel Abreu
d49634723e
dtape_kqchan: Fix a couple of stupid mistakes and add some more logging
So, when I discovered the mistake in `knote_post`, at first,
I was baffled at why we were even getting events if we were checking for
them and returning if there *were* events.

As it turns out, because the event checks for single ports and portsets
were switched, `dtape_kqchan_mach_port_has_events` was always returning `false`.
I fixed the issue there before I found the issue in `knote_post`,
so when I did that, suddenly, `knote_post` wasn't getting generating any
events. "WTF," I wondered. I spent *hours* trying to figure out why the
hell `knote_post` wouldn't return any events. Turns out that I was
missing an exclamation mark.

In the end, I decided to remove that check altogether, since the worst
that would happen is we have a spurious event and notify userspace and
userspace asks for an event when we don't have one, which we just tell them.

And with this commit, we can now reliably and consistently enter a shell
using darlingserver! 🎉
2022-02-25 17:23:28 -05:00
Ariel Abreu
8294dcf96f
dtape: Some fixes for termination and proper lock usage
Thanks to the recently enabled assertions, it turns out that we actually need to disable IPC in tasks and threads before terminating it.

Additionally, let's lock threads when we should and not do so when we shouldn't. This should fix some thread synchronization issues I was running into.
2022-02-25 17:08:35 -05:00
Ariel Abreu
77a234c872
dtape: Add lock assertions + memory fences
The memory fences might be unnecessary, but they're there just in case.
2022-02-25 17:06:21 -05:00
Ariel Abreu
d1a38bb778
dtape: Initialize the host notify zone and lock 2022-02-25 17:05:27 -05:00
Ariel Abreu
560e5e3245
dtape: Enable asserts + a few fixes for some asserts
Enabling assertions should allow us to catch mistakes in our duct-tape that would otherwise go unchecked.
2022-02-25 17:05:18 -05:00
Ariel Abreu
f68d8c4bd9
dtape: Replace XNU printf calls with dtape_log calls 2022-02-25 16:58:18 -05:00
Ariel Abreu
2f4e4ef4f4
dtape_semaphore: Properly handle KERN_ABORTED
This indicates that the waiting was interrupted (with THREAD_INTERRUPTED) and we probably don't want to be panicking needlessly over it.
2022-02-25 16:57:05 -05:00
Ariel Abreu
35811342c4
kqchan: Check for events after sending messages
XNU's kevent filter code returns FILTER_ACTIVE when there are events still available after a filter call. This tells kevent to check for more events at its discretion. Thus, to mimic this behavior, whenever we send a message, we check for more events afterwards.

Note that we only do this once the message has been fully sent. For reasons that are still unclear, checking immediately after queueing the message and queueing a new notification message as a result of this check messes up the order of the messages that our peer sees.
2022-02-25 16:52:48 -05:00
Ariel Abreu
d95c18f39c
Logging improvements
Instances of classes that inherit from the new Loggable class can now be logged directly to a Log::Stream. This makes it easier to log objects that provide context for a message.

Additionally, the kqchan code has been updated to use this new functionality to log kqchannels along with messages and make it easier to identify them and their processes.

Finally, for every message, the thread and process logging it is now recorded along with the message.
2022-02-25 16:45:09 -05:00
Ariel Abreu
88282d5c81
Change thread execution deferral code a bit 2022-02-25 16:25:48 -05:00
Ariel Abreu
7274224d44
Implement psynch using pthread kext
This is an initial implementation that will probably need more tweaking and fixing down the line, but it seems to work for now.
2022-02-23 00:55:03 -05:00
Ariel Abreu
556f078514
Various minor fixes and cleanups
* Delete duct-tape/Makefile (it's a copy of the LKM's Makefile)
  * Add a slash between the prefix directory and the socket filename so the socket is actually *in* the prefix and not next to it.
  * Disable console logging by default and introduce a `DSERVER_LOG_STDERR` env var to optionally enable it
  * EPIPE means the peer of the first message in a message queue died, so just drop the message; the main event loop will soon notice (via pidfd) that the peer died.
2022-02-20 01:41:28 -05:00
Ariel Abreu
a0e95e0ee2
Fix Mach port set support in kqchannels
We use a kernel waiter thread to wait on the port set waitq. This is probably too much just for waiting on a port set; however, this is the best non-invasive solution. The other way to do this would be to modify the mqueue code to perform KNOTE on a port's port set (which is not easy to find; you'd have to walk through the port's waitq's waitq_sets looking for something that looks like a portset waitq_set).

Additionally, when a peer asks to read a Mach port kqchan and there are no events available, report it like we do for process kqchannels.
2022-02-20 01:36:26 -05:00
Ariel Abreu
9dc6253fa0
Implement task_is_64_bit RPC call 2022-02-20 01:30:11 -05:00
Ariel Abreu
6e3605aca5
Port handle_ux_exception over from the LKM 2022-02-20 01:29:51 -05:00
Ariel Abreu
03706a335c
Fix stupid mistakes with S2C and duct-taped memory manipulation
S2C calls were always failing because `_s2cPerform` was moving `_s2cReply` into a local variable (as it should) but then using `_s2cReply` (which is invalidated by the move) for error checking and returning that value instead of the moved local variable.

Also, copyinmap/copyout had the order of the arguments to memmove mixed up for the kernel_map case.
2022-02-20 01:28:26 -05:00
Ariel Abreu
fa20ec7673
Wait for thread to stop running before impersonating it
This commit also introduces the ability for thread execution to be deferred; this is used to prevent the thread from running while we're impersonating it.
2022-02-20 01:22:58 -05:00