mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
* FEX: emulate x86-64 ptrace, seccomp and /proc for anti-cheat launchers * Guest hook: route raw execve/execveat through the exec wrapper and pass self-sent SIGSYS to the program A program that execs with syscall(SYS_execveat, ...) bypassed the environment the guest needs, so re-executing /proc/self/exe died in the loader. /proc/self/exe now maps to the real program on exec as it already does on open. A SIGSYS the program queues itself, or one from a filter for another architecture, went to the Android-filter emulation instead of the program's handler. * FEX: list only guest mappings in /proc/<pid>/maps on hosts below 47 address bits Credit the anti-cheat emulation source in the patch header. * Preloads: leave poll, ppoll and select buffers untouched when no emulated device is open A bad user pointer now reaches the kernel and returns EFAULT instead of crashing the caller inside the wrapper. * Emulate only host-architecture seccomp traps in the guest hook; report the calling thread's seccomp mode in FEX's /proc/self/status A SIGSYS carrying another architecture's seccomp siginfo now takes its default action when the program has no handler, so a process whose emulated filter traps a syscall while SIGSYS is blocked or ignored dies as on Linux. * FEX: read the host status file through the raw openat syscall * README: credit StonkGuy's FEX anti-cheat emulation * FEX: open the host status file with a direct system call so preloads cannot rewrite it * FEX: hand a traced program's ptrace state across exec * Credit StonkGuy in the FEX anti-cheat patches and README Co-authored-by: StonkGuy <68372320+StonkGuy@users.noreply.github.com> * FEX: open guest paths with the raw openat system call so host preloads cannot replace the file * FEX: serve emulated proc files by the requested path when the host returns a memfd * FEX: map fixed guest memory above a small host address space Co-authored-by: StonkGuy <StonkGuy@users.noreply.github.com> * FEX: serve decoder reads from fixed guest memory above the host limit * Run games with anti-cheat files on an x86-64 DroidDeck-Proton under FEX The Proton launchers look for Easy Anti-Cheat or BattlEye bridge files in the game folder and, when an x86-64 DroidDeck-Proton and the matching Proton runtime are installed, hand the launch to that Proton under FEX with the game's own prefix. Picking the x86-64 tool directly also runs it under FEX. DROIDDECK_ANTICHEAT=off keeps a game on ARM64. * Keep x86-64 DroidDeck-Proton builds out of the ARM64 tool list * droiddeck-fex: take the runtime from an adopted tool's original manifest * FEX: show the guest command line when the argument area cannot be remapped * FEX: describe the container's procfs in emulated mount tables * FEX: order the anti-cheat series after the guest stack patch from main * Test the container procfs view of FEX's emulated mount tables * Docs: describe the anti-cheat signal mask fix and the container procfs view * README: match the StonkGuy credit to the renumbered FEX patches * Keep anti-cheat games on a tool the user chose instead of moving them to x86-64 * test(game-env): build the adopted-tool wrapper through extra_wrapper_sh * Check anti-cheat runtime bridges, apply component choices under FEX and honour MAP_FIXED_NOREPLACE above the host limit * Check the driver query is answered before each wrapper's call sites * Keep x86-64 Proton builds out of the automatic default tool * Keep high guest mappings intact when a fixed map or unmap fails Check room in the region table, splits included, before a fixed map or unmap removes anything, return the host munmap error without touching the table entry, and keep write-protecting low shared aliases of executable ranges that sit above the host address limit. * Keep a requested x86-64 default and accept linked anti-cheat runtimes A request for the current automatic x86-64 default now marks it as the app's choice, so the next settle no longer moves it back to ARM64. Anti-cheat runtime discovery follows a symlinked runtime directory. * Route anti-cheat games to x86-64 Proton only on a capable FEX The FEX build now lists its patches in share/fex-emu/.droiddeck-patches. Anti-cheat routing needs the fixed guest memory patch in that list and otherwise keeps the launch on ARM64, as an older FEX cannot map the anti-cheat helper buffer above the host address limit. Installing a FEX build without the list removes a list left by an earlier build. * Update FEX before deciding anti-cheat routing A device on an older FEX build never went through droiddeck-fex on the ARM64 path, so the catalog FEX was never installed and the gate kept anti-cheat games on ARM64 forever. dd_anticheat now runs the new droiddeck-fex update action first, which installs the catalog build under the same lock and retry window as other FEX launches. * Leave MAP_FIXED_NOREPLACE above the host limit to the host Wine finds the top of the address space with MAP_FIXED_NOREPLACE probes. Claiming those probes above a 39-bit host limit made Wine place its stacks in relocated memory, and every wineserver request then failed with EFAULT because the iovec buffers it passes to writev are nested pointers that are not translated. Only MAP_FIXED requests are relocated again, so the probes see the real host limit while fixed mappings above it keep working. * Translate only pointer syscall arguments into high guest memory Pointer arguments are now picked from a per-syscall layout table, so scalar values such as a truncate length are never rewritten. Sized buffers must map to one contiguous host range or the call fails with EFAULT, and new host backings are placed next to their guest neighbours so adjacent mappings stay contiguous. readv, writev and the preadv/pwritev family, sendmsg and recvmsg get a host-side copy of their iovec and msghdr with translated bases, leaving the guest copies untouched. Code invalidation for kernel writes maps the host buffer back to its guest address, and an unmap checks the table before it changes any host mapping. * Claim high guest memory only with LSE atomics and invalidate every code alias High guest mappings are claimed only when the JIT emits LSE atomics, and exclusive load/store instructions are never single-stepped, so an atomic on high memory cannot spin. A store into high code invalidates every mirror of the written page through the tracked mapping. madvise and mprotect on high memory apply to each region at its own host address. * Reject wrapping lengths and drop stale code on discarded high memory A nonzero length that wraps when page-aligned now fails madvise with EINVAL and mprotect with ENOMEM, as on Linux. Discarding advice on high memory invalidates translated code for the range whenever advice reached any region. * Add an end-to-end probe for guest memory above the host address limit * Emulate exclusive pairs on high guest memory and release failed host unmaps later * Size counted guest buffers by element and keep exclusive access within guest protection poll, ppoll, epoll waits, mmsg calls and select bitmaps now translate the whole array their count describes, so a buffer that crosses into an unrelated host range fails with EFAULT and oversized counts cannot wrap. High regions remember their protection through mprotect, splitting at the range edges, and exclusive stores to read-only pages or loads from inaccessible pages reach the guest as a fault instead of being emulated. * Refuse a high unmap when failed host unmaps could not all be retained UnmapLocked counts the overlapping regions before it changes anything and returns ENOMEM when the retired list could not hold all of them, so a host range is never dropped from tracking while still mapped. * Serve memory trim requests while syscall user dispatch blocks A thread that runs with syscall user dispatch on and its selector set to block (as x86-64 Wine does while in Windows code) took the trim signal with the selector still blocking. Every raw syscall the handler made was then dispatched to the program's SIGSYS handler, which either deadlocked the thread or killed the process when SIGSYS was blocked, so the session saw it as a deaf process and the game stalled during loading. Track the selector each thread registers through prctl, allow syscalls for the length of the handler and restore the selector before returning. * Allow syscalls for a forwarded prctl while dispatch blocks The prctl wrapper issues its syscall from the preload's own text, so a thread whose selector blocks got SIGSYS where the plain libc call would have been dispatched the same way by the caller's own rules. Allow syscalls for the forwarded call and restore the selector afterwards. The dispatch test now waits until the selector blocks before it sends the trim request, and checks a prctl call made while it blocks. * Merge high regions after protection changes and translate message batches Protect now merges neighbouring high regions whose guest and host ranges touch and whose protection and code state match, so toggling page protections no longer fills the region table. Exclusive loads on high memory accept write-only pages and exclusive stores need only PROT_WRITE, matching x86-64 where writable pages are readable. sendmmsg and recvmmsg translate the name, control and io vectors of every message in the batch, return msg_len for each processed message and the received header fields for recvmmsg. Counted arrays passed to getgroups, setgroups, semop, semtimedop, io_submit, io_getevents, io_pgetevents, move_pages, futex_waitv and listmount are now checked to stay within one host range. The unused ToHost helper is removed. * Translate fixed-size, string and in-array guest pointers in high memory Give every syscall pointer without a length a fixed x86-64 size, a length read through another argument, or a terminator scan for strings, so spans crossing separately backed regions are refused before the kernel sees them. Size message queue buffers as the type word plus msgsz. Translate io_submit control blocks and their buffers, including vectored reads and writes, through bounded host copies, restore the guest words afterwards, and report guest iocb addresses back from io_getevents. Translate move_pages page lists for the calling process and futex_waitv addresses. Merge new mappings with matching neighbours. * Size high-memory syscall spans by kernel widths and embedded sizes Read counts with the width the kernel uses for each argument, so int and unsigned int counts ignore the upper register bits and negative counts reach the handler. Size capability data by its header version, perf and scheduler attributes, file handles and mount requests by their own size fields, and landlock rules by rule type. Keep the translated prefix of a message batch, read headers that span separate backings one by one, write results back through the current guest mapping, and stop counting messages whose length cannot be stored. Revisit an entry moved into an earlier slot while merging regions. Give newer syscalls fallback numbers in the test harness. * Send translated message batches one message at a time Submit sendmmsg batches that need scratch headers one message per call, storing each length before the next message goes out, so a length that cannot be stored stops the batch where Linux would. Skip the name length when no name was given and store flags before the control length. Check the whole fixed file-handle header even when its size is invalid. Hand capget and capset a private header so the checked data size matches the version the kernel reads, and return a rewritten version to the guest. Patch and restore AIO control blocks under the mapping lock and restore only the block that was patched. * Harden high-memory aio, capability and message batch write-backs Restore patched aio buffers through /proc/self/mem when the control block became read-only and keep the scratch vectors alive if that fails too, fault capability calls whose header write-back fails, stop send batches after a short message, and pin the batch socket with a private dup. * Pin batch sockets, fall back to whole batches and reconcile pending aio restores Follow-up message sends and the first dispatch share one duplicated socket. When it cannot be duplicated, the whole translated batch goes to the kernel in one call and lengths are written back up to the first one that cannot be stored. Short sends compare against the iovec lengths given to the kernel. Unrestorable aio_buf edits, including unreadable control blocks, keep their storage in a bounded list that later submissions reconcile. * Stop message batches at unreadable vectors, send singly without a spare descriptor and reserve aio restore slots A message whose iovec snapshot cannot be taken ends the prepared batch instead of being sent with an unknown length; when it is the first message the kernel handles the original array natively. When the batch socket cannot be duplicated, messages still go one at a time on the original descriptor with each length written before the next send. Pending aio restore slots are reserved under the pending mutex before the first guest edit and released or handed to the pending record, so concurrent submissions cannot exceed the bound. Tests use a real socket pair, injected descriptor exhaustion and concurrent reservations. * Send only the first message of a batch when the socket cannot be pinned * Replay high-memory faults over the whole access and reconcile aio restores on unmap * Keep high-memory traps ordered, fault-exact and locked against unmap Protect, Map and low mmap/munmap/mremap/mprotect reconcile pending aio restores at once, and io_submit reconciles them even when no high region is mapped. Ordered loads and stores split across backings are emulated between full fences instead of being byte-copied through the trampoline; atomics and exclusives are still refused. Gather and scatter use fault-safe copies, and a failing piece is re-faulted at the guest instruction through a probe load, with SIGBUS redelivered the same way. The VMA lock is held from planning until the scatter finishes, so unmap and protect cannot race a resumed access. Sign-extending loads are no longer classified as writes. * Defer user signals while guest high memory mappings are held Only the trampoline breakpoint completes a replay, user-sent SIGSEGV, SIGBUS and SIGTRAP raised while the mapping lock is held are requeued after it is released, and stranded control block words are restored when their page turns readable again. The harness now runs under a timeout. * Rebase the FEX patch series onto FEX-2612 * FEX: restore stranded aio words before readable protection, report failures Restore every pending aio word in the range before a readable guest protection is applied, on both the high and low paths, and fail the guest mprotect with the old protection kept when a word cannot be restored or its window cannot be closed. Ignore user-sent SIGSEGV/SIGBUS after deferral, read the cleared syscall info when resuming with guest GPRs and on restore, and check exclusive acquisition from a separate thread in the harness. * FEX: submit aio control blocks through private host copies Stop editing guest IOCBs in place when aio_buf needs translation. io_submit now hands the kernel a host copy of each such control block, maps io_event.obj back to the guest pointer when the completion is reaped, redirects io_cancel by guest pointer and context to the copy, and frees copies on reap, on io_destroy, or when the kernel did not accept them. The guest control block is never patched and its protection is never touched, so the pending restore records, the protection windows and the restore hooks in mmap, munmap, mremap and mprotect are removed with their tests. * fex: keep aio keys, cancels and reaped events native for copied control blocks Guest aio_key is no longer cleared before dispatch: the slot is probed with a same-value write and only the first n accepted copies get key 0 after the host io_submit returns n. io_cancel redirects to a copy only when the guest block is readable and its key is 0, so unreadable blocks fault in the kernel as before. io_getevents and io_pgetevents reap into emulator storage, translate and release copies there, then copy the events out, returning -EFAULT if that fails. A successful io_setup clears the ring header magic so userspace ring reapers fall back to io_getevents, where copies are translated and released. * fex: leave aio rejections, keys and unwritable event buffers to the kernel A copied control block whose key slot is read-only is now passed to the kernel as the guest's own block at the end of the list, so the kernel reports its native fd, eventfd or key error. After io_submit, guest keys follow the private copies through the first rejected position, and they are stored through a fresh translation of the guest address, skipped when it no longer maps. io_getevents and io_pgetevents probe the guest event buffer and reap at most its writable prefix, handing an unwritable buffer to the kernel so completions are not consumed. * Kept AIO completions and key stores stable against sibling remaps Wrapped event buffers go to the kernel unchanged. Direct reaps report guest control blocks and release copies only after the store lands. Guest stores and reads hold the mapping lock across translation and copy. Completions whose copy-out fails are stashed per context and delivered first on the next reap, and dropped on io_destroy. * fex: stage every AIO reap and keep stashed completions recoverable Hold the guest mapping for every high address, restore the caller's signal mask after thread-context holds, replace the fixed event stash with a per-context table that reserves space before dispatch, stage all reaps so control block identities are translated before copy-out, keep collecting ready completions once the stash meets min_nr, and restash events on every error except an interrupted delivery or a destroyed context. * fex: own AIO recovery storage before dispatch and bind it to the context lifetime Allocate the recovery node with the event buffer before the kernel consumes completions and hand the buffer itself to the stash when copy-out fails, so settling never allocates. Track each context by a generation that io_setup advances, so a destroy that finishes after a replacement context reuses the ring address only releases the old context's copies and stash. Drop the reservation cap so blocking reaps keep their min_nr. * fex: publish AIO contexts from private storage, keep their generation, wake blocked reapers for stashed completions * fex: wait for AIO events in bounded slices instead of wake markers, roll back setup by its own generation, fault unreadable io_setup output Blocked reapers wait in the kernel for at most 20 ms at a time and take stashed completions between waits until the guest minimum or timeout is met, so no eventfd or io_submit notification is needed. Deferred signals end the wait like EINTR. io_setup rollback uses the generation BeginContext installed, and an unreadable context output returns -EFAULT before dispatch. --------- Co-authored-by: StonkGuy <68372320+StonkGuy@users.noreply.github.com> Co-authored-by: StonkGuy <StonkGuy@users.noreply.github.com>
978 lines
33 KiB
C
978 lines
33 KiB
C
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
|
// parts share their static functions and are built with the same command as before).
|
|
//
|
|
// Calls an app may not make (chown, mknod, capabilities, prctl, landlock, seccomp, namespaces,
|
|
// close_range) and the generic syscall() entry point.
|
|
|
|
#ifndef DDNATV_HOST_TEST
|
|
|
|
// ndk_translation's file is "/memfd:child"; any memory file counts in a process whose executable is
|
|
// a translator's runner (houdini). ndk_translation shows a program its own path as /proc/self/exe,
|
|
// so the name is what tells there.
|
|
//
|
|
// This runs at the start of every program, so it costs three system calls on a phone that runs ARM
|
|
// code itself: the descriptors that are open (/proc/self/fd, read at once) are all that are asked
|
|
// about, where asking about each of the numbers from 3 to 63 was 61 readlink() calls in every
|
|
// process that was started.
|
|
// Set once this process knows that it has none (it looked, or was told by DDNATV_NT=0 in its
|
|
// environment, which prepare_ddnatv_env hands on to the programs it starts)
|
|
static int g_no_translator = 0;
|
|
|
|
__attribute__((constructor(102))) static void find_translator_files(void) {
|
|
// The first program of a session looks; the ones it starts are told (a translator is there for
|
|
// all the programs of a machine or for none, and where it is not known they look as before)
|
|
const char *known = getenv("DDNATV_NT");
|
|
if (known && known[0] == '0' && known[1] == '\0') {
|
|
g_no_translator = 1;
|
|
return;
|
|
}
|
|
ssize_t (*real_readlink)(const char *, char *, size_t) = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
|
|
int (*real_open)(const char *, int, ...) = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
|
int (*real_close)(int) = (int (*)(int))dlsym(RTLD_NEXT, "close");
|
|
ssize_t (*real_getdents64)(int, void *, size_t) = (ssize_t (*)(int, void *, size_t))dlsym(RTLD_NEXT, "getdents64");
|
|
if (!real_readlink || !real_open || !real_close) return;
|
|
int dir = real_getdents64 ? real_open("/proc/self/fd", O_RDONLY | O_DIRECTORY | O_CLOEXEC) : -1;
|
|
if (dir < 0) return;
|
|
// The numbers of the descriptors open now, those from 3 to 63 (the directory's own left out)
|
|
char entries[2048];
|
|
unsigned char open_fds[64];
|
|
int nopen = 0;
|
|
for (;;) {
|
|
long n = real_getdents64(dir, entries, sizeof(entries));
|
|
if (n <= 0) break;
|
|
for (long off = 0; off < n;) {
|
|
// struct linux_dirent64: d_ino (8), d_off (8), d_reclen (2), d_type (1), then d_name
|
|
unsigned short reclen;
|
|
memcpy(&reclen, entries + off + 16, sizeof(reclen));
|
|
const char *name = entries + off + 19;
|
|
off += reclen ? reclen : n; // a record without a length ends the buffer
|
|
if (name[0] < '0' || name[0] > '9') continue; // "." and ".."
|
|
int fd = atoi(name);
|
|
if (fd >= 3 && fd < 64 && fd != dir) open_fds[nopen++] = (unsigned char)fd;
|
|
}
|
|
// A short answer is the whole directory; asking again would only be told "no more"
|
|
if ((size_t)n + 64 <= sizeof(entries)) break;
|
|
}
|
|
real_close(dir);
|
|
if (nopen == 0) {
|
|
g_no_translator = 1;
|
|
return;
|
|
}
|
|
|
|
char path[PATH_MAX];
|
|
int runner = 0;
|
|
ssize_t n = real_readlink("/proc/self/exe", path, sizeof(path) - 1);
|
|
if (n > 0) {
|
|
path[n] = '\0';
|
|
const char *base = strrchr(path, '/');
|
|
base = base ? base + 1 : path;
|
|
runner = strncmp(base, "ndk_translation", 15) == 0 || strncmp(base, "houdini", 7) == 0;
|
|
}
|
|
// In ascending order, as the list is kept
|
|
for (int i = 0; i < nopen - 1; i++) {
|
|
for (int j = i + 1; j < nopen; j++) {
|
|
if (open_fds[j] < open_fds[i]) {
|
|
unsigned char t = open_fds[i];
|
|
open_fds[i] = open_fds[j];
|
|
open_fds[j] = t;
|
|
}
|
|
}
|
|
}
|
|
for (int i = 0; i < nopen && g_kept_fd_count < DDNATV_MAX_KEPT_FDS; i++) {
|
|
char link[32], target[64];
|
|
snprintf(link, sizeof(link), "/proc/self/fd/%d", open_fds[i]);
|
|
ssize_t m = real_readlink(link, target, sizeof(target) - 1);
|
|
if (m <= 0) continue;
|
|
target[m] = '\0';
|
|
if (strncmp(target, "/memfd:child", 12) == 0 || (runner && strncmp(target, "/memfd:", 7) == 0)) {
|
|
g_kept_fds[g_kept_fd_count++] = open_fds[i];
|
|
}
|
|
}
|
|
if (g_kept_fd_count == 0) g_no_translator = 1;
|
|
}
|
|
|
|
int chown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; }
|
|
int fchown(int fd, uid_t owner, gid_t group) { (void)fd; (void)owner; (void)group; return 0; }
|
|
int lchown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; }
|
|
int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) {
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
(void)dirfd; (void)pathname; (void)owner; (void)group; (void)flags; return 0;
|
|
}
|
|
int chroot(const char *path) { (void)path; return 0; }
|
|
|
|
// Hook mkfifo / mkfifoat / mknod / mknodat
|
|
int mkfifo(const char *pathname, mode_t mode);
|
|
int mkfifoat(int dirfd, const char *pathname, mode_t mode);
|
|
|
|
int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev) {
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
(void)dev;
|
|
if (!pathname) {
|
|
errno = EFAULT;
|
|
return -1;
|
|
}
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
|
// A file type of 0 means a regular file, as in mknod(2), which also fails with EEXIST instead
|
|
// of touching an existing file.
|
|
if (S_ISREG(mode) || (mode & S_IFMT) == 0) {
|
|
// mknod(2) returns 0 on success, never a file descriptor: close the
|
|
// transient fd instead of leaking one per call (tar/dpkg extract
|
|
// thousands of files; leaked fds exhaust the table -> EBADF noise).
|
|
int fd = openat(dirfd, target, O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, mode & 07777);
|
|
if (fd >= 0) {
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
if (S_ISFIFO(mode)) {
|
|
return mkfifoat(dirfd, target, mode);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int mknod(const char *pathname, mode_t mode, dev_t dev) {
|
|
return mknodat(AT_FDCWD, pathname, mode, dev);
|
|
}
|
|
|
|
int mkfifoat(int dirfd, const char *pathname, mode_t mode) {
|
|
static int (*orig_mkfifoat)(int, const char *, mode_t) = NULL;
|
|
if (!orig_mkfifoat) orig_mkfifoat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkfifoat");
|
|
if (!pathname) {
|
|
errno = EFAULT;
|
|
return -1;
|
|
}
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
|
if (orig_mkfifoat) {
|
|
return orig_mkfifoat(dirfd, target, mode);
|
|
}
|
|
static int (*orig_mkfifo)(const char *, mode_t) = NULL;
|
|
if (!orig_mkfifo) orig_mkfifo = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "mkfifo");
|
|
if (orig_mkfifo && dirfd == AT_FDCWD) {
|
|
return orig_mkfifo(target, mode);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int mkfifo(const char *pathname, mode_t mode) {
|
|
return mkfifoat(AT_FDCWD, pathname, mode);
|
|
}
|
|
|
|
// Hook sync / syncfs to prevent Android flash storage stalls during dpkg operations
|
|
void sync(void) {
|
|
// No-op
|
|
}
|
|
|
|
int syncfs(int fd) {
|
|
(void)fd;
|
|
return 0;
|
|
}
|
|
|
|
int capget(void *hdrp, void *datap) {
|
|
static int (*orig_capget)(void *, void *) = NULL;
|
|
if (!orig_capget) orig_capget = (int (*)(void *, void *))dlsym(RTLD_NEXT, "capget");
|
|
if (orig_capget && orig_capget(hdrp, datap) == 0) {
|
|
return 0;
|
|
}
|
|
if (datap) {
|
|
uint32_t ver = hdrp ? *(const uint32_t *)hdrp : 0;
|
|
size_t nbytes = (ver == 0x20071026U || ver == 0x20080522U) ? 24 : 12;
|
|
memset(datap, 0, nbytes);
|
|
}
|
|
errno = 0;
|
|
return 0;
|
|
}
|
|
int capset(void *hdrp, const void *datap) { (void)hdrp; (void)datap; return 0; }
|
|
|
|
int prctl(int option, ...) {
|
|
va_list ap;
|
|
va_start(ap, option);
|
|
unsigned long arg2 = va_arg(ap, unsigned long);
|
|
unsigned long arg3 = va_arg(ap, unsigned long);
|
|
unsigned long arg4 = va_arg(ap, unsigned long);
|
|
unsigned long arg5 = va_arg(ap, unsigned long);
|
|
va_end(ap);
|
|
|
|
// PR_SET_NO_NEW_PRIVS = 38, PR_GET_NO_NEW_PRIVS = 39
|
|
#ifdef PR_SET_NO_NEW_PRIVS
|
|
if (option == PR_SET_NO_NEW_PRIVS) return 0;
|
|
#endif
|
|
if (option == 38) return 0;
|
|
#ifdef PR_GET_NO_NEW_PRIVS
|
|
if (option == PR_GET_NO_NEW_PRIVS) return 1;
|
|
#endif
|
|
if (option == 39) return 1;
|
|
|
|
// PR_SET_SECCOMP = 22, PR_GET_SECCOMP = 21
|
|
#ifdef PR_SET_SECCOMP
|
|
if (option == PR_SET_SECCOMP) { errno = EINVAL; return -1; }
|
|
#endif
|
|
if (option == 22) { errno = EINVAL; return -1; }
|
|
#ifdef PR_GET_SECCOMP
|
|
if (option == PR_GET_SECCOMP) return 0;
|
|
#endif
|
|
if (option == 21) return 0;
|
|
|
|
// PR_SET_SPECULATION_CTRL = 47
|
|
if (option == 47) return 0;
|
|
|
|
static int (*orig_prctl)(int, unsigned long, unsigned long, unsigned long, unsigned long) = NULL;
|
|
if (!orig_prctl) orig_prctl = (int (*)(int, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "prctl");
|
|
return orig_prctl ? orig_prctl(option, arg2, arg3, arg4, arg5) : 0;
|
|
}
|
|
|
|
// Limits that leave a process no new descriptors (RLIMIT_NOFILE below 16) or no file writes
|
|
// (RLIMIT_FSIZE 0) are not applied. Sandboxes give them to their unprivileged part (sshd's before
|
|
// login sets 1 and 0, with the seccomp filter above), and Android's ARM translator (emulators, x86
|
|
// devices) needs new descriptors and memory files in the process: sshd's died in it ("CHECK failed"
|
|
// in its mmap) and the client's connection was reset. Other limits are set as asked.
|
|
static int sandbox_only_limit(int resource, const struct rlimit *rlim) {
|
|
if (!rlim) return 0;
|
|
if (resource == RLIMIT_NOFILE) return rlim->rlim_cur < 16;
|
|
if (resource == RLIMIT_FSIZE) return rlim->rlim_cur == 0;
|
|
return 0;
|
|
}
|
|
|
|
// The kernel checks RLIMIT_RTTIME on a thread that is realtime or has a CPU-time timer armed, and a
|
|
// limit of 0 is reached at once: SIGXCPU at the soft limit, SIGKILL at the hard one. SDL sets the
|
|
// RTTimeUSecMax it reads from org.freedesktop.portal.Realtime before it asks for a realtime thread,
|
|
// and xdg-desktop-portal reports 0 when it reaches no RealtimeKit, as in a session with no system bus.
|
|
static int fatal_limit(int resource, const struct rlimit *rlim) {
|
|
return rlim && resource == RLIMIT_RTTIME && rlim->rlim_cur == 0;
|
|
}
|
|
|
|
int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlim) {
|
|
if (sandbox_only_limit((int)resource, rlim) || fatal_limit((int)resource, rlim)) return 0;
|
|
static int (*orig_setrlimit)(__rlimit_resource_t, const struct rlimit *) = NULL;
|
|
#if defined(__USE_FILE_OFFSET64) && !defined(__LP64__)
|
|
// With 64-bit file offsets on a 32-bit system this function is setrlimit64
|
|
if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit64");
|
|
#else
|
|
if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit");
|
|
#endif
|
|
if (!orig_setrlimit) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
return orig_setrlimit(resource, rlim);
|
|
}
|
|
#if defined(__LP64__)
|
|
__asm__(".globl setrlimit64\n" ".set setrlimit64, setrlimit\n");
|
|
#endif
|
|
|
|
#ifndef LANDLOCK_CREATE_RULESET_VERSION
|
|
#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0)
|
|
#endif
|
|
|
|
#ifndef __NR_landlock_create_ruleset
|
|
#define __NR_landlock_create_ruleset 444
|
|
#endif
|
|
#ifndef __NR_landlock_add_rule
|
|
#define __NR_landlock_add_rule 445
|
|
#endif
|
|
#ifndef __NR_landlock_restrict_self
|
|
#define __NR_landlock_restrict_self 446
|
|
#endif
|
|
|
|
#ifndef __NR_seccomp
|
|
#if defined(__aarch64__)
|
|
#define __NR_seccomp 277
|
|
#elif defined(__x86_64__)
|
|
#define __NR_seccomp 317
|
|
#elif defined(__arm__)
|
|
#define __NR_seccomp 383
|
|
#else
|
|
#define __NR_seccomp 277
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef __NR_unshare
|
|
#if defined(__aarch64__)
|
|
#define __NR_unshare 97
|
|
#elif defined(__x86_64__)
|
|
#define __NR_unshare 272
|
|
#elif defined(__arm__)
|
|
#define __NR_unshare 337
|
|
#else
|
|
#define __NR_unshare 97
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef __NR_setns
|
|
#if defined(__aarch64__)
|
|
#define __NR_setns 268
|
|
#elif defined(__x86_64__)
|
|
#define __NR_setns 308
|
|
#elif defined(__arm__)
|
|
#define __NR_setns 375
|
|
#else
|
|
#define __NR_setns 268
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef __NR_clone3
|
|
#define __NR_clone3 435
|
|
#endif
|
|
#ifndef __NR_pidfd_open
|
|
#define __NR_pidfd_open 434
|
|
#endif
|
|
#ifndef __NR_pidfd_send_signal
|
|
#define __NR_pidfd_send_signal 424
|
|
#endif
|
|
#ifndef __NR_pidfd_getfd
|
|
#define __NR_pidfd_getfd 438
|
|
#endif
|
|
#ifndef __NR_close_range
|
|
#define __NR_close_range 436
|
|
#endif
|
|
#ifndef __NR_faccessat
|
|
#if defined(__aarch64__)
|
|
#define __NR_faccessat 48
|
|
#elif defined(__arm__)
|
|
#define __NR_faccessat 334
|
|
#elif defined(__x86_64__)
|
|
#define __NR_faccessat 269
|
|
#endif
|
|
#endif
|
|
#ifndef __NR_faccessat2
|
|
#define __NR_faccessat2 439
|
|
#endif
|
|
#ifndef __NR_statx
|
|
#if defined(__aarch64__) || defined(__arm__)
|
|
#define __NR_statx 291
|
|
#elif defined(__x86_64__)
|
|
#define __NR_statx 332
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef CLONE_PIDFD
|
|
#define CLONE_PIDFD 0x00001000
|
|
#endif
|
|
#ifndef P_PIDFD
|
|
#define P_PIDFD 3
|
|
#endif
|
|
|
|
#ifndef SECCOMP_SET_MODE_STRICT
|
|
#define SECCOMP_SET_MODE_STRICT 0
|
|
#endif
|
|
#ifndef SECCOMP_SET_MODE_FILTER
|
|
#define SECCOMP_SET_MODE_FILTER 1
|
|
#endif
|
|
#ifndef SECCOMP_GET_ACTION_AVAIL
|
|
#define SECCOMP_GET_ACTION_AVAIL 2
|
|
#endif
|
|
|
|
int landlock_create_ruleset(const void *attr, size_t size, uint32_t flags) {
|
|
(void)attr;
|
|
(void)size;
|
|
(void)flags;
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
int landlock_add_rule(int ruleset_fd, int rule_type, const void *rule_attr, uint32_t flags) {
|
|
(void)ruleset_fd;
|
|
(void)rule_type;
|
|
(void)rule_attr;
|
|
(void)flags;
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
int landlock_restrict_self(int ruleset_fd, uint32_t flags) {
|
|
(void)ruleset_fd;
|
|
(void)flags;
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
int seccomp(unsigned int operation, unsigned int flags, void *args) {
|
|
(void)flags;
|
|
(void)args;
|
|
if (operation != SECCOMP_SET_MODE_STRICT &&
|
|
operation != SECCOMP_SET_MODE_FILTER &&
|
|
operation != SECCOMP_GET_ACTION_AVAIL &&
|
|
operation != 3 /* SECCOMP_GET_NOTIF_SIZES */) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
int unshare(int flags) {
|
|
(void)flags;
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
int setns(int fd, int nstype) {
|
|
(void)fd;
|
|
(void)nstype;
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
#ifndef CLOSE_RANGE_CLOEXEC
|
|
#define CLOSE_RANGE_CLOEXEC (1U << 2)
|
|
#endif
|
|
#ifndef CLOSE_RANGE_UNSHARE
|
|
#define CLOSE_RANGE_UNSHARE (1U << 1)
|
|
#endif
|
|
|
|
static int close_range_all(unsigned int first, unsigned int last, int flags);
|
|
|
|
// Tests only ("IN OUT TID"): before each part it closes, once it has looked at the kept fds, thread
|
|
// TID's close_range() writes a byte to OUT and waits for one on IN, as a thread descheduled there
|
|
// would.
|
|
static void close_range_test_gate(void) {
|
|
const char *gate = getenv("DDNATV_TEST_CLOSE_RANGE_GATE");
|
|
int in, out, tid;
|
|
if (!gate || sscanf(gate, "%d %d %d", &in, &out, &tid) != 3 || tid != (int)syscall(SYS_gettid)) return;
|
|
int saved = errno;
|
|
char c = 1;
|
|
syscall(SYS_write, out, &c, 1);
|
|
while (syscall(SYS_read, in, &c, 1) < 0 && errno == EINTR) {}
|
|
errno = saved;
|
|
}
|
|
|
|
static int close_range_part(unsigned int first, unsigned int last, void *flags) {
|
|
close_range_test_gate();
|
|
return close_range_all(first, last, *(int *)flags);
|
|
}
|
|
|
|
// The system call itself on the one number no fd has (close_range_all() would emulate a missing call
|
|
// by closing fds)
|
|
static int close_range_unshare(void) {
|
|
static long (*raw_syscall)(long, ...) = NULL;
|
|
if (!raw_syscall) raw_syscall = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall");
|
|
if (!raw_syscall) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
return raw_syscall(__NR_close_range, ~0U, ~0U, CLOSE_RANGE_UNSHARE) == 0 ? 0 : -1;
|
|
}
|
|
|
|
static int close_range_kept(unsigned int first, unsigned int last, int flags) {
|
|
// The table is unshared first, once, even when every fd in the range is kept, and this thread
|
|
// goes on with its own copy of the kept list (kept_split()). Without the system call, it is
|
|
// emulated where nothing is kept, and refused otherwise.
|
|
if (flags & CLOSE_RANGE_UNSHARE) {
|
|
if (kept_split(close_range_unshare) == 0) flags &= ~CLOSE_RANGE_UNSHARE;
|
|
else if (errno != ENOSYS || kept_any() > 0) return -1;
|
|
}
|
|
// Close-on-exec only (CLOSE_RANGE_CLOEXEC) leaves the translator's files open
|
|
int snapshot[DDNATV_MAX_KEPT_FDS];
|
|
const int *kept = (flags & CLOSE_RANGE_CLOEXEC) ? g_cloexec_kept_fds : snapshot;
|
|
int kept_count = (flags & CLOSE_RANGE_CLOEXEC) ? g_cloexec_kept_count : kept_snapshot(snapshot);
|
|
if (kept_count == 0) return close_range_part(first, last, &flags);
|
|
return for_each_range_without(first, last, kept, kept_count, close_range_part, &flags);
|
|
}
|
|
|
|
int close_range(unsigned int first, unsigned int last, int flags) {
|
|
if (!(flags & CLOSE_RANGE_CLOEXEC)) forget_proc_root_range(first, last);
|
|
uint64_t ticket = kept_close_begin();
|
|
int r = close_range_kept(first, last, flags);
|
|
int saved = errno;
|
|
kept_close_end(ticket);
|
|
errno = saved;
|
|
return r;
|
|
}
|
|
|
|
// glibc's closefrom() makes the close_range system call itself, past the function above
|
|
void closefrom(int lowfd) {
|
|
close_range(lowfd < 0 ? 0U : (unsigned int)lowfd, ~0U, 0);
|
|
}
|
|
|
|
static int close_range_all(unsigned int first, unsigned int last, int flags) {
|
|
static int (*orig_close_range)(unsigned int, unsigned int, int) = NULL;
|
|
if (!orig_close_range) {
|
|
orig_close_range = (int (*)(unsigned int, unsigned int, int))dlsym(RTLD_NEXT, "close_range");
|
|
}
|
|
#ifdef __NR_close_range
|
|
static long (*orig_raw_syscall)(long, ...) = NULL;
|
|
if (!orig_raw_syscall) orig_raw_syscall = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall");
|
|
if (orig_raw_syscall) {
|
|
long sret = orig_raw_syscall(__NR_close_range, first, last, flags);
|
|
if (sret == 0) return 0;
|
|
if (sret < 0 && errno != ENOSYS) return -1;
|
|
}
|
|
#endif
|
|
if (orig_close_range) {
|
|
int cret = orig_close_range(first, last, flags);
|
|
if (cret == 0) return 0;
|
|
if (cret < 0 && errno != ENOSYS) return -1;
|
|
}
|
|
|
|
int set_cloexec = (flags & CLOSE_RANGE_CLOEXEC) != 0;
|
|
|
|
DIR *d = opendir("/proc/self/fd");
|
|
if (d) {
|
|
int dfd = dirfd(d);
|
|
struct dirent *de;
|
|
while ((de = readdir(d)) != NULL) {
|
|
if (de->d_name[0] == '.') continue;
|
|
int fd = atoi(de->d_name);
|
|
if (fd >= (int)first && (last == ~0U || (unsigned int)fd <= last) && fd != dfd) {
|
|
if (set_cloexec) {
|
|
int fl = fcntl(fd, F_GETFD);
|
|
if (fl >= 0) {
|
|
fcntl(fd, F_SETFD, fl | FD_CLOEXEC);
|
|
}
|
|
} else {
|
|
close(fd);
|
|
}
|
|
}
|
|
}
|
|
closedir(d);
|
|
return 0;
|
|
}
|
|
int max_fd = (int)sysconf(_SC_OPEN_MAX);
|
|
if (max_fd <= 0 || max_fd > 1024) max_fd = 1024;
|
|
int end = (last < (unsigned int)max_fd) ? (int)last : max_fd;
|
|
for (int fd = (int)first; fd <= end; fd++) {
|
|
if (set_cloexec) {
|
|
int fl = fcntl(fd, F_GETFD);
|
|
if (fl >= 0) {
|
|
fcntl(fd, F_SETFD, fl | FD_CLOEXEC);
|
|
}
|
|
} else {
|
|
close(fd);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static __thread int t_syscall_redirect = 0;
|
|
|
|
static long nested_path_syscall(long number, unsigned long dirfd, unsigned long pathname, unsigned long arg3, unsigned long arg4) {
|
|
char nested_buf[PATH_MAX];
|
|
const char *path = (const char *)pathname;
|
|
if (path && path[0] == '/') path = rewrite_path(path, nested_buf, sizeof(nested_buf));
|
|
return call_orig_syscall(number, dirfd, (unsigned long)path, arg3, arg4, 0, 0);
|
|
}
|
|
|
|
static long fake_root_ids(long number, unsigned int *real, unsigned int *effective, unsigned int *saved) {
|
|
long rc = call_orig_syscall(number, (unsigned long)real, (unsigned long)effective, (unsigned long)saved, 0, 0, 0);
|
|
if (rc == 0 && !app_user_mode()) *real = *effective = *saved = 0;
|
|
return rc;
|
|
}
|
|
|
|
long syscall(long number, ...) {
|
|
va_list ap;
|
|
va_start(ap, number);
|
|
unsigned long arg1 = va_arg(ap, unsigned long);
|
|
unsigned long arg2 = va_arg(ap, unsigned long);
|
|
unsigned long arg3 = va_arg(ap, unsigned long);
|
|
unsigned long arg4 = va_arg(ap, unsigned long);
|
|
unsigned long arg5 = va_arg(ap, unsigned long);
|
|
unsigned long arg6 = va_arg(ap, unsigned long);
|
|
va_end(ap);
|
|
|
|
#if defined(__NR_readlinkat)
|
|
if (number == __NR_readlinkat) {
|
|
return (long)readlinkat((int)arg1, (const char *)arg2, (char *)arg3, (size_t)arg4);
|
|
}
|
|
#endif
|
|
#if defined(__NR_readlink)
|
|
if (number == __NR_readlink) {
|
|
return (long)readlink((const char *)arg1, (char *)arg2, (size_t)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_linkat)
|
|
if (number == __NR_linkat) {
|
|
return (long)linkat((int)arg1, (const char *)arg2, (int)arg3, (const char *)arg4, (int)arg5);
|
|
}
|
|
#endif
|
|
#if defined(__NR_link)
|
|
if (number == __NR_link) {
|
|
return (long)link((const char *)arg1, (const char *)arg2);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_landlock_create_ruleset
|
|
if (number == __NR_landlock_create_ruleset) {
|
|
return (long)landlock_create_ruleset((const void *)arg1, (size_t)arg2, (uint32_t)arg3);
|
|
}
|
|
if (number == __NR_landlock_add_rule) {
|
|
return (long)landlock_add_rule((int)arg1, (int)arg2, (const void *)arg3, (uint32_t)arg4);
|
|
}
|
|
if (number == __NR_landlock_restrict_self) {
|
|
return (long)landlock_restrict_self((int)arg1, (uint32_t)arg2);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_rt_sigprocmask
|
|
if (number == __NR_rt_sigprocmask && arg2 && (arg1 == SIG_BLOCK || arg1 == SIG_SETMASK) && arg4 == sizeof(uint64_t)) {
|
|
uint64_t mask;
|
|
memcpy(&mask, (const void *)arg2, sizeof(mask));
|
|
mask &= ~(1ULL << (SIGSYS - 1));
|
|
return call_orig_syscall(number, arg1, (unsigned long)&mask, arg3, arg4, 0, 0);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_rt_sigaction
|
|
if (number == __NR_rt_sigaction && arg1 == SIGSYS && arg4 == sizeof(uint64_t)) {
|
|
struct { void *handler; unsigned long flags; void *restorer; uint64_t mask; } kact, kold;
|
|
struct sigaction act, old;
|
|
memset(&act, 0, sizeof(act));
|
|
if (arg2) {
|
|
memcpy(&kact, (const void *)arg2, sizeof(kact));
|
|
act.sa_sigaction = (void (*)(int, siginfo_t *, void *))kact.handler;
|
|
act.sa_flags = (int)kact.flags;
|
|
memcpy(&act.sa_mask, &kact.mask, sizeof(kact.mask));
|
|
}
|
|
remember_app_sigsys(arg2 ? &act : NULL, arg3 ? &old : NULL);
|
|
if (arg3) {
|
|
memset(&kold, 0, sizeof(kold));
|
|
kold.handler = (void *)old.sa_sigaction;
|
|
kold.flags = (unsigned long)(unsigned int)old.sa_flags;
|
|
memcpy(&kold.mask, &old.sa_mask, sizeof(kold.mask));
|
|
memcpy((void *)arg3, &kold, sizeof(kold));
|
|
}
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_seccomp
|
|
if (number == __NR_seccomp) {
|
|
return (long)seccomp((unsigned int)arg1, (unsigned int)arg2, (void *)arg3);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_unshare
|
|
if (number == __NR_unshare) {
|
|
return (long)unshare((int)arg1);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_setns
|
|
if (number == __NR_setns) {
|
|
return (long)setns((int)arg1, (int)arg2);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_clone
|
|
if (number == __NR_clone && (arg1 & CLONE_PIDFD)) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_waitid
|
|
if (number == __NR_waitid && (int)arg1 == P_PIDFD) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_execve
|
|
if (number == __NR_execve) {
|
|
return (long)execve((const char *)arg1, (char *const *)arg2, (char *const *)arg3);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_execveat
|
|
if (number == __NR_execveat && arg2 && ((const char *)arg2)[0] != '\0' &&
|
|
(((const char *)arg2)[0] == '/' || (int)arg1 == AT_FDCWD) && (arg5 & ~(unsigned long)AT_EMPTY_PATH) == 0) {
|
|
return (long)execve((const char *)arg2, (char *const *)arg3, (char *const *)arg4);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_clone3
|
|
if (number == __NR_clone3) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_pidfd_open
|
|
if (number == __NR_pidfd_open) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
#endif
|
|
#ifdef __NR_pidfd_send_signal
|
|
if (number == __NR_pidfd_send_signal) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
#endif
|
|
#ifdef __NR_pidfd_getfd
|
|
if (number == __NR_pidfd_getfd) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_close_range
|
|
if (number == __NR_close_range) {
|
|
// libddsession's close_range() calls syscall() again: the nested call goes straight to the kernel
|
|
if (t_syscall_redirect) return call_orig_syscall(number, arg1, arg2, arg3, 0, 0, 0);
|
|
t_syscall_redirect++;
|
|
long ret = (long)close_range((unsigned int)arg1, (unsigned int)arg2, (int)arg3);
|
|
t_syscall_redirect--;
|
|
return ret;
|
|
}
|
|
#endif
|
|
|
|
if (g_initialized) {
|
|
#ifdef __NR_openat
|
|
if (number == __NR_openat) {
|
|
if (t_syscall_redirect) return nested_path_syscall(number, arg1, arg2, arg3, arg4);
|
|
t_syscall_redirect++;
|
|
long ret = (long)openat((int)arg1, (const char *)arg2, (int)arg3, (mode_t)arg4);
|
|
t_syscall_redirect--;
|
|
return ret;
|
|
}
|
|
#endif
|
|
#if defined(__NR_mkdirat)
|
|
if (number == __NR_mkdirat) {
|
|
return (long)mkdirat((int)arg1, (const char *)arg2, (mode_t)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_unlinkat)
|
|
if (number == __NR_unlinkat) {
|
|
return (long)unlinkat((int)arg1, (const char *)arg2, (int)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_renameat)
|
|
if (number == __NR_renameat) {
|
|
return (long)renameat((int)arg1, (const char *)arg2, (int)arg3, (const char *)arg4);
|
|
}
|
|
#endif
|
|
#if defined(__NR_renameat2)
|
|
if (number == __NR_renameat2) {
|
|
return (long)renameat2((int)arg1, (const char *)arg2, (int)arg3, (const char *)arg4, (unsigned int)arg5);
|
|
}
|
|
#endif
|
|
#if defined(__NR_symlinkat)
|
|
if (number == __NR_symlinkat) {
|
|
return (long)symlinkat((const char *)arg1, (int)arg2, (const char *)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_fchmodat)
|
|
if (number == __NR_fchmodat) {
|
|
return (long)fchmodat((int)arg1, (const char *)arg2, (mode_t)arg3, 0);
|
|
}
|
|
#endif
|
|
#if defined(__NR_fchownat)
|
|
if (number == __NR_fchownat) {
|
|
return (long)fchownat((int)arg1, (const char *)arg2, (uid_t)arg3, (gid_t)arg4, (int)arg5);
|
|
}
|
|
#endif
|
|
#if defined(__NR_utimensat)
|
|
if (number == __NR_utimensat) {
|
|
if (!arg2) return call_orig_syscall(number, arg1, arg2, arg3, arg4, 0, 0);
|
|
return (long)utimensat((int)arg1, (const char *)arg2, (const struct timespec *)arg3, (int)arg4);
|
|
}
|
|
#endif
|
|
#if defined(__NR_connect)
|
|
if (number == __NR_connect) {
|
|
return (long)connect((int)arg1, (const struct sockaddr *)arg2, (socklen_t)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_bind)
|
|
if (number == __NR_bind) {
|
|
return (long)bind((int)arg1, (const struct sockaddr *)arg2, (socklen_t)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_sendmsg)
|
|
if (number == __NR_sendmsg) {
|
|
return (long)sendmsg((int)arg1, (const struct msghdr *)arg2, (int)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_recvmsg)
|
|
if (number == __NR_recvmsg) {
|
|
return (long)recvmsg((int)arg1, (struct msghdr *)arg2, (int)arg3);
|
|
}
|
|
#endif
|
|
#if defined(__NR_getsockopt)
|
|
if (number == __NR_getsockopt) {
|
|
return (long)getsockopt((int)arg1, (int)arg2, (int)arg3, (void *)arg4, (socklen_t *)arg5);
|
|
}
|
|
#endif
|
|
#if defined(__NR_truncate)
|
|
if (number == __NR_truncate) {
|
|
return (long)truncate((const char *)arg1, (off_t)arg2);
|
|
}
|
|
#endif
|
|
#if defined(__NR_chdir)
|
|
if (number == __NR_chdir) {
|
|
return (long)chdir((const char *)arg1);
|
|
}
|
|
#endif
|
|
#if defined(__NR_chroot)
|
|
if (number == __NR_chroot) {
|
|
return (long)chroot((const char *)arg1);
|
|
}
|
|
#endif
|
|
#if defined(__NR_inotify_add_watch)
|
|
if (number == __NR_inotify_add_watch) {
|
|
return (long)inotify_add_watch((int)arg1, (const char *)arg2, (uint32_t)arg3);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#ifdef __NR_openat2
|
|
if (number == __NR_openat2) {
|
|
// libddsession's openat2() calls syscall() again (an endless loop until the stack ran out): the nested
|
|
// call maps the path and goes straight to the kernel
|
|
if (t_syscall_redirect) return nested_path_syscall(number, arg1, arg2, arg3, arg4);
|
|
t_syscall_redirect++;
|
|
long ret = (long)openat2((int)arg1, (const char *)arg2, (struct ddnatv_open_how *)arg3, (size_t)arg4);
|
|
t_syscall_redirect--;
|
|
return ret;
|
|
}
|
|
#endif
|
|
|
|
#ifdef __NR_fchmodat2
|
|
if (number == __NR_fchmodat2) {
|
|
return (long)fchmodat2((int)arg1, (const char *)arg2, (mode_t)arg3, (int)arg4);
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NR_statx)
|
|
if (number == __NR_statx) {
|
|
return (long)statx((int)arg1, (const char *)arg2, (int)arg3, (unsigned int)arg4, (struct statx *)arg5);
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NR_faccessat)
|
|
if (number == __NR_faccessat) {
|
|
if (t_syscall_redirect) return nested_path_syscall(number, arg1, arg2, arg3, 0);
|
|
t_syscall_redirect++;
|
|
long ret = (long)faccessat((int)arg1, (const char *)arg2, (int)arg3, 0);
|
|
t_syscall_redirect--;
|
|
return ret;
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NR_faccessat2)
|
|
if (number == __NR_faccessat2) {
|
|
return (long)faccessat2((int)arg1, (const char *)arg2, (int)arg3, (int)arg4);
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NR_mknodat)
|
|
if (number == __NR_mknodat) {
|
|
return (long)mknodat((int)arg1, (const char *)arg2, (mode_t)arg3, (dev_t)arg4);
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NR_mknod)
|
|
if (number == __NR_mknod) {
|
|
return (long)mknod((const char *)arg1, (mode_t)arg2, (dev_t)arg3);
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NR_mkfifoat)
|
|
if (number == __NR_mkfifoat) {
|
|
return (long)mkfifoat((int)arg1, (const char *)arg2, (mode_t)arg3);
|
|
}
|
|
#endif
|
|
|
|
#if defined(__NR_setresuid)
|
|
if (number == __NR_setresuid) return (long)setresuid((uid_t)arg1, (uid_t)arg2, (uid_t)arg3);
|
|
#endif
|
|
#if defined(__NR_setresuid32)
|
|
if (number == __NR_setresuid32) return (long)setresuid((uid_t)arg1, (uid_t)arg2, (uid_t)arg3);
|
|
#endif
|
|
#if defined(__NR_setresgid)
|
|
if (number == __NR_setresgid) return (long)setresgid((gid_t)arg1, (gid_t)arg2, (gid_t)arg3);
|
|
#endif
|
|
#if defined(__NR_setresgid32)
|
|
if (number == __NR_setresgid32) return (long)setresgid((gid_t)arg1, (gid_t)arg2, (gid_t)arg3);
|
|
#endif
|
|
#if defined(__NR_setuid)
|
|
if (number == __NR_setuid) return (long)setuid((uid_t)arg1);
|
|
#endif
|
|
#if defined(__NR_setuid32)
|
|
if (number == __NR_setuid32) return (long)setuid((uid_t)arg1);
|
|
#endif
|
|
#if defined(__NR_setgid)
|
|
if (number == __NR_setgid) return (long)setgid((gid_t)arg1);
|
|
#endif
|
|
#if defined(__NR_setgid32)
|
|
if (number == __NR_setgid32) return (long)setgid((gid_t)arg1);
|
|
#endif
|
|
#if defined(__NR_setreuid)
|
|
if (number == __NR_setreuid) return (long)setreuid((uid_t)arg1, (uid_t)arg2);
|
|
#endif
|
|
#if defined(__NR_setreuid32)
|
|
if (number == __NR_setreuid32) return (long)setreuid((uid_t)arg1, (uid_t)arg2);
|
|
#endif
|
|
#if defined(__NR_setregid)
|
|
if (number == __NR_setregid) return (long)setregid((gid_t)arg1, (gid_t)arg2);
|
|
#endif
|
|
#if defined(__NR_setregid32)
|
|
if (number == __NR_setregid32) return (long)setregid((gid_t)arg1, (gid_t)arg2);
|
|
#endif
|
|
#if defined(__NR_setgroups)
|
|
if (number == __NR_setgroups) return (long)setgroups((size_t)arg1, (const gid_t *)arg2);
|
|
#endif
|
|
#if defined(__NR_setgroups32)
|
|
if (number == __NR_setgroups32) return (long)setgroups((size_t)arg1, (const gid_t *)arg2);
|
|
#endif
|
|
#if defined(__NR_setfsuid)
|
|
if (number == __NR_setfsuid) return (long)presented_uid();
|
|
#endif
|
|
#if defined(__NR_setfsuid32)
|
|
if (number == __NR_setfsuid32) return (long)presented_uid();
|
|
#endif
|
|
#if defined(__NR_setfsgid)
|
|
if (number == __NR_setfsgid) return (long)presented_gid();
|
|
#endif
|
|
#if defined(__NR_setfsgid32)
|
|
if (number == __NR_setfsgid32) return (long)presented_gid();
|
|
#endif
|
|
#if defined(__NR_getuid)
|
|
if (number == __NR_getuid) return (long)presented_uid();
|
|
#endif
|
|
#if defined(__NR_getuid32)
|
|
if (number == __NR_getuid32) return (long)presented_uid();
|
|
#endif
|
|
#if defined(__NR_geteuid)
|
|
if (number == __NR_geteuid) return (long)presented_uid();
|
|
#endif
|
|
#if defined(__NR_geteuid32)
|
|
if (number == __NR_geteuid32) return (long)presented_uid();
|
|
#endif
|
|
#if defined(__NR_getgid)
|
|
if (number == __NR_getgid) return (long)presented_gid();
|
|
#endif
|
|
#if defined(__NR_getgid32)
|
|
if (number == __NR_getgid32) return (long)presented_gid();
|
|
#endif
|
|
#if defined(__NR_getegid)
|
|
if (number == __NR_getegid) return (long)presented_gid();
|
|
#endif
|
|
#if defined(__NR_getegid32)
|
|
if (number == __NR_getegid32) return (long)presented_gid();
|
|
#endif
|
|
#if defined(__NR_getresuid)
|
|
if (number == __NR_getresuid) return fake_root_ids(number, (unsigned int *)arg1, (unsigned int *)arg2, (unsigned int *)arg3);
|
|
#endif
|
|
#if defined(__NR_getresuid32)
|
|
if (number == __NR_getresuid32) return fake_root_ids(number, (unsigned int *)arg1, (unsigned int *)arg2, (unsigned int *)arg3);
|
|
#endif
|
|
#if defined(__NR_getresgid)
|
|
if (number == __NR_getresgid) return fake_root_ids(number, (unsigned int *)arg1, (unsigned int *)arg2, (unsigned int *)arg3);
|
|
#endif
|
|
#if defined(__NR_getresgid32)
|
|
if (number == __NR_getresgid32) return fake_root_ids(number, (unsigned int *)arg1, (unsigned int *)arg2, (unsigned int *)arg3);
|
|
#endif
|
|
#if defined(__NR_getgroups)
|
|
if (number == __NR_getgroups) return (long)getgroups((int)arg1, (gid_t *)arg2);
|
|
#endif
|
|
#if defined(__NR_fchown)
|
|
if (number == __NR_fchown) return (long)fchown((int)arg1, (uid_t)arg2, (gid_t)arg3);
|
|
#endif
|
|
|
|
return call_orig_syscall(number, arg1, arg2, arg3, arg4, arg5, arg6);
|
|
}
|
|
|
|
#endif /* !DDNATV_HOST_TEST */
|