Commit Graph

131 Commits

Author SHA1 Message Date
Pedro Tôrres
e9e0a9f9a4
bump cmake minimum required version to 3.13 2022-10-27 16:17:30 -03:00
Ariel Abreu
1c7134a900
Merge pull request #5 from trungnt2910/dev/trungnt2910/wsl1-support
fix: Some darlingserver workarounds for WSL1
2022-09-01 10:12:33 -04:00
Trung Nguyen
b01df17317
fix: Workaround for WSL1 tmpfs bug
Do not mount /dev/shm with MS_NOEXEC flag on WSL1. A bug on WSL1
(https://github.com/microsoft/WSL/issues/8777) prevents files from
being mapped using mmap if the underlying filesystem is mounted
with MS_NOEXEC.
2022-08-29 22:02:01 +07:00
Trung Nguyen
12aa182f51
fix: Workaround for systems without overlayfs
Darling now be used without overlayfs by enabling
the environment "DARLING_NOOVERLAYFS". Darling also
disables overlayfs when it detects itself running in a WSL1
environment.

Without overlayfs, Darling will have to recursively copy all files
and folders from LIBEXEC_PATH to DPREFIX.
2022-08-25 08:54:24 +07:00
Ariel Abreu
94c375886b
Merge pull request #4 from trungnt2910/dev/trungnt2910/wsl1-support
feat: Initial WSL1 support
2022-08-22 02:17:45 -04:00
Ariel Abreu
d8595a1d92
Merge pull request #3 from sherpya/stub-loadinfo
Use dtape_stub_safe() for HOST_CPU_LOAD_INFO
2022-08-21 13:12:39 -04:00
Trung Nguyen
2ad420c633
feat: Initial WSL1 support
- Implemented an alternative to pidfd_open for kernels older than 5.3.
mldr should send a "lifetime pipe" to darlingserver during process start.
When the process dies, darlingserver should receive a POLLHUP event.
- Set increased_limit.rlim_cur to default_limit.rlim_max on systems without
/proc/sys/fs/nr_open. On WSL1, this greatly increases the number of open file
descriptors available.
- For systems without NSpid in /proc/self/status, implemented a way to manage
thread IDs in darlingserver during checkin. darlingserver should receive a hint
address on the thread's stack, and then compare it with a stack pointer retrieved using
PTRACE_GETREGS
- Avoided sending socket messages when msg_hdr.msg_name->sun_path is an empty string.
A null msg_name is used instead, otherwise, on some systems, this would fail with EINVAL.
2022-08-21 20:59:24 +07:00
Gianluigi Tiesi
f4837bf507 Use dtape_stub_safe() for HOST_CPU_LOAD_INFO
Avoid darlingserver crash when calling host_statistics()
asking for HOST_CPU_LOAD_INFO

`top` makes this call, at least cash top and not the
server
2022-08-19 15:05:14 +02:00
Ariel Abreu
2101f7a5a5
Implement some processor info duct-tape code 2022-07-12 19:03:17 -04:00
Ariel Abreu
13695c79b2
Report correct thread states 2022-07-05 16:42:40 -04:00
Ariel Abreu
d3a51300ea
Significantly reduce default logging output
Debug logging produces *lots* of output *very* quickly, so that's
disabled by default now. The log level can be controlled with the new
`DSERVER_LOG_LEVEL` env var. Just set it to the minimum level
you want to see in the output. It defaults to "error" so that only
error messages are logged.
2022-07-04 18:02:53 -04:00
Ariel Abreu
a6bfe117f7
Use a stack pool to reduce memory usage 2022-06-30 22:50:02 -04:00
Ariel Abreu
26ef1e040f
Use SIGUSR1 to perform leak checking when ASAN is enabled 2022-06-30 22:49:45 -04:00
Ariel Abreu
e7c383758a
Fix some leaks in duct-taped code
One significant change made here is that lck_mtx structures now directly
contain the internals of dtape_mutex structures. This was changed
because the old way of storing in a malloc'ed object led to memory leaks.
The problem is that there's a lot of XNU code that uses simple locks and
does not destroy them (because it doesn't need to in the XNU
implementation). Since the only structure that really cares about the
lock size is the waitq structure, we just patch that up. Besides, we
had modified the waitq structure in the LKM before and nothing blew up,
so this should be fine.
2022-06-30 22:49:17 -04:00
Ariel Abreu
cfd5cb9999
Add additional log info for process kqchannels 2022-06-30 22:44:35 -04:00
Ariel Abreu
4f001e2a34
Add reply-push synchronization
This is used to avoid the server reading incorrect/corrupted reply
contents for pushed replies. This was happening because clients were
sending the push-reply call with the pointer to the message contents,
but they were immediately returning after sending it. This led to a race
condition in which the server would sometimes read the data after the
client had already overwritten/discarded said data.
2022-06-24 01:39:04 -04:00
Ariel Abreu
700d89812c
Don't allowed saved replies to be overwritten 2022-06-24 01:36:56 -04:00
Ariel Abreu
89a7a10d41
Minor fixes for the Message class 2022-06-24 01:36:28 -04:00
Ariel Abreu
d49022a865
Workaround for thread resuming from syscall return 2022-06-24 01:36:06 -04:00
Ariel Abreu
2d5e25be35
Ignore calls from non-existent threads
The thread might have died after sending the message, so
it might not exist by the time the server gets the message.
In that case, just ignore/drop the message.
2022-06-24 01:35:16 -04:00
Ariel Abreu
5bd9a87821
Merge pull request #2 from rdrpenguin04/main
Add mach_vm_msync
2022-06-18 23:01:21 -04:00
Ray Redondo
58b0391a2c
update duct-tape/src/memory.c
Because I don't understand this and someone else does

Co-authored-by: Ariel Abreu <facekapow@outlook.com>
2022-06-18 20:44:33 -05:00
Ariel Abreu
a0115601ee
Call dtape_timer_fired within a microthread
This function calls other duct-taped functions that might need to wait
for a bit, so it needs to run in a microthread so it can do so.
2022-06-18 17:22:48 -04:00
Ariel Abreu
f2ad6c2cc2
Fix duct-tape timer scheduling
We were previously always updating the timer deadline. This meant that,
when a later deadline than the current one came along, we would update
the deadline to the later one. In effect, we were scheduling a timer for
the latest deadline available rather than the earliest.

The fix involves keeping track of the current deadline and not updating
it if the new deadline is later than the current one. There is an option
to override this behavior, however, because sometimes the timer_call code
changes the deadline on us to a later time and we *do* want to update it
when it tells us to do so explicitly. For example, the deadline returned
by timer_queue_expire is definitive: that's definitely the next deadline
we want. The deadline passed to timer_queue_assign, on the other hand,
is merely is a suggestion.
2022-06-18 16:51:59 -04:00
Ray Redondo
b37026af03 add mach_vm_msync 2022-06-17 23:18:21 -05:00
Ariel Abreu
814434f139
GetExecutablePath: Write the path out to the correct process
We were writing out the path to the target process (i.e. the one we're
looking up), but we should instead write it out to the process who made
the call.
2022-05-19 15:21:32 -04:00
Ariel Abreu
2d64a44c85
Schedule interrupts after pending calls if necessary
This resolves a race condition where we receive a call and then
immediately receive an interrupt while that call is still pending.
The new behavior is to go ahead and process the pending call, but we
trigger interrupt processing as soon as the call suspends.
2022-05-19 15:20:23 -04:00
Ariel Abreu
4f05ef945b
Defer kqchannel notifications until after read replies are sent
See DarlingServer::Kqchan::MachPort::_read() for why this is necessary.

This fixes crashes in libkqueue due to out-of-order kqchannel messages,
mainly visible in aslmanager.
2022-05-14 17:06:21 -04:00
Ariel Abreu
a4afa0cd29
Increase core limits on server startup
For some reason, the core limit is always set to 0 when the server
starts up, regardless of what the parent shell/program has it set to.
2022-05-14 16:57:42 -04:00
Ariel Abreu
2ba5a41c4e
Properly implement knote_vanish
This fixes some crashes with syslogd because the mqueue was vanishing
and calling knote_vanish, indicating its klist was going to be emptied.
However, since we weren't storing this flag in the knote,
filt_machportdetach thought the knote was still attached and tried to
detach it, causing a NULL pointer access.
2022-05-14 01:38:22 -04:00
Ariel Abreu
51f08430c9
Remove capabilities and libcap dependency
Together with the corresponding changes in mldr, darlingserver no longer
requires capabilities while running! The next step towards making
Darling completely unprivileged would be to remove SUID from the main
Darling binary, but that's a task for some other time.

I originally started doing this to see if some issues I was seeing with
LLDB were related to the capabilities in mldr, but it seems they're
unrelated.
2022-05-10 21:08:07 -04:00
Ariel Abreu
fdc3420249
Make sure processes stay alive until kqchannels die 2022-05-10 21:02:07 -04:00
Ariel Abreu
a46079444c
Report correct arch values in host_info
We're actually supposed to report plain x86, even on x86_64 processors.
2022-05-10 10:15:57 -04:00
Ariel Abreu
9123bad915
Handle thread and process death asynchronously
What this means is that we no longer release and destroy Thread and
Process instances when the threads and processes they manage die.
Instead, we keep them alive to perform some cleanup (like finishing
active calls).

This should fix the duct-tape panic where threads and tasks are still
referenced at death.

Best of all, there don't seem to be any leaks with this approach: for
each `process dying` or `thread dying` message in the log, there's a
`process being destroyed` or `thread being destroyed` message later
on. This means we're not leaking any processes or threads.
2022-04-08 10:38:36 -04:00
Ariel Abreu
7d6f0ed991
dtape: Forgot to unlock shared entry lock in special case for LLDB 2022-04-08 10:31:52 -04:00
Ariel Abreu
82892d3049
Move interrupter_enter processing to Thread class
This call needs to access lots of private thread members, so it's better
to provide a single private helper that handles the call in the Thread
class rather than have it all in a Call.
2022-04-08 10:27:53 -04:00
Ariel Abreu
80ab579ca2
Implement kernel runner queue scaling
This allows kernel runner threads to be created as necessary to process
the work that comes in through `kernelAsync` and `kernelSync`.

There's currently a hardcoded max of 10 permanent kernel runners.
However, if the workload is too much, temporary runners can be spawned;
each temporary worker processes a single work item and then exits. There
is no limit on the number of temporary workers that can be spawned.
2022-04-08 10:24:45 -04:00
Ariel Abreu
e17bdbb1e7
Merge pull request #1 from trungnt2910/main
feat: Manage executable path in darlingserver
2022-04-05 12:52:50 -04:00
Trung Nguyen
226610318c
chore: Address comments 2022-04-05 12:43:27 +07:00
Trung Nguyen
c5a32023f3
feat: Manage executable path in darlingserver 2022-04-05 09:25:23 +07:00
Ariel Abreu
761a292f11
Fix (rare) deadlock with process kqchannels 2022-04-04 17:39:40 -04:00
Ariel Abreu
7e1dd74cd2
Implement memory sharing (in mach_vm_remap)
This commit allows Darling processes to convert private memory in other
Darling processes into shared memory that they can access. This is
necessary, e.g. for LLDB.
2022-04-01 02:21:32 -04:00
Ariel Abreu
be203fd569
Allow threads to perform S2C calls at any time
This is possible now because we can signal threads with a real-time
signal that libsystem_kernel handles.
2022-04-01 02:11:17 -04:00
Ariel Abreu
b4fccbd6dd
Thread: Add mapFile call 2022-04-01 02:04:55 -04:00
Ariel Abreu
fe6df7af56
Add mprotect S2C call 2022-04-01 01:56:35 -04:00
Ariel Abreu
5b0bf0ff13
dtape: Safely stub thread_abort_safely 2022-04-01 01:51:31 -04:00
Ariel Abreu
e46c682d50
Add some dyld info debug messages and fix a typo in TASK_DYLD_INFO
I had forgotten to add `return KERN_SUCCESS`, so it was continuing into
TASK_VM_INFO and presumably failing with KERN_INVALID_ARGUMENT.
2022-03-28 14:19:14 -04:00
Ariel Abreu
f099fe3390
Fix mach_vm_region{,_recurse} to use the target task
They were using the current task, but that's not always the case.
LLDB, for example, calls mach_vm_region_recurse with the map of the task
it's debugging.
2022-03-28 14:17:48 -04:00
Ariel Abreu
8a28cb0dde
Fix Process::memoryRegionInfo and change how it returns info
std::stoul is base 10 by default, so we were trying to process hex
values as decimal values(producing incorrect values, as expected).

Also, memoryRegionInfo now returns a structure with the info rather than
having everything passed in as a reference, just like memoryInfo was
recently changed to do as well. This should make easier to add more info
fields later.
2022-03-28 14:16:00 -04:00
Ariel Abreu
71a247e51e
Add support for nested interrupts 2022-03-27 14:50:23 -04:00