mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 15:56:01 +00:00
* fix(flatpak): file dialogs and forwarded descriptors work in Flatpak apps Flatpak apps opened from the desktop could not open files: the file chooser's result went through a document store that cannot mount on Android, and image loaders started with flatpak-spawn --forward-fd lost their D-Bus descriptor because the app relaunches sandboxes and only stdio survived. - A session document store hands picked files and folders back in place instead of failing without a mount. - The bwrap shim offers its inherited descriptors once over a private socket, and the native hook puts them back on their original numbers before the program starts. - The native hook resolves /proc/<pid>/root paths to the other process's sandbox, so xdg-desktop-portal reads .flatpak-info and identifies the calling app. * fix(flatpak): check documents against the app's view and harden fd forwarding The document store hands a picked file back in place only when the calling app's own sandbox can open it with the requested access, so paths the app cannot see are refused instead of handed out broken. /proc/<pid>/root lookups follow links inside that process's view, duplicated root descriptors keep their mapping, and close_range drops stale records. Forwarded descriptors survive targets at the fd limit, truncated offers are ignored, and the shim refuses more descriptors than one message can carry. * fix(flatpak): match picked documents by file identity and keep O_NOFOLLOW on process roots The document store accepts an in-place path only when the app's view holds the same file (device and inode) as the picked descriptor, and reads each process root once. A bare /proc/<pid>/root opened with O_NOFOLLOW is no longer mapped, so it fails like the kernel's link. * Keep a trailing-slash process root translated and track only directory roots
822 lines
35 KiB
C
822 lines
35 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).
|
|
//
|
|
// Opening, creating and inspecting files: open, fopen, temporary files, directories, the stat family.
|
|
|
|
#ifndef DDNATV_HOST_TEST
|
|
|
|
#ifndef O_TMPFILE
|
|
#define O_TMPFILE (020000000 | 00200000)
|
|
#endif
|
|
|
|
static inline int open_needs_mode(int flags) {
|
|
#ifdef O_TMPFILE
|
|
return ((flags & O_CREAT) != 0) || ((flags & O_TMPFILE) == O_TMPFILE);
|
|
#else
|
|
return (flags & O_CREAT) != 0;
|
|
#endif
|
|
}
|
|
|
|
// The app is root for the programs it runs, and root puts names in every directory and takes them out
|
|
// whatever the mode of the directory; the app does not. A directory that is the app's own can have lost
|
|
// its owner's rights: an earlier version left the directories dpkg makes (with mode 0) at mode 0 when the
|
|
// chmod that follows did not take effect, and a package can ship a read-only one (#44). When a call that
|
|
// makes or removes a name is refused in such a directory, the directory is given its owner's rights and the
|
|
// call is made once more. Returns 1 when the directory was repaired (the call can be made again).
|
|
static int give_dir_owner_rights(int dirfd, const char *name) {
|
|
static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL;
|
|
if (!name || !*name) return 0;
|
|
char parent[PATH_MAX];
|
|
size_t len = strlen(name);
|
|
if (len >= sizeof(parent)) return 0;
|
|
memcpy(parent, name, len + 1);
|
|
while (len > 1 && parent[len - 1] == '/') parent[--len] = '\0';
|
|
char *slash = strrchr(parent, '/');
|
|
if (!slash) {
|
|
strcpy(parent, ".");
|
|
} else if (slash == parent) {
|
|
parent[1] = '\0';
|
|
} else {
|
|
*slash = '\0';
|
|
}
|
|
struct stat st;
|
|
if (real_fstatat(dirfd, parent, &st, 0) != 0 || !S_ISDIR(st.st_mode)) return 0;
|
|
if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids();
|
|
if (st.st_uid != __atomic_load_n(&g_app_uid, __ATOMIC_RELAXED)) return 0;
|
|
if ((st.st_mode & 0700) == 0700) return 0;
|
|
if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat");
|
|
return orig_fchmodat && orig_fchmodat(dirfd, parent, (st.st_mode | 0700) & 07777, 0) == 0;
|
|
}
|
|
|
|
// For the calls that were refused (EACCES): whether the directory of `name` was repaired, so that the call
|
|
// can be made again. errno stays what the call left when it was not.
|
|
static int refused_in_repaired_dir(int dirfd, const char *name) {
|
|
if (errno != EACCES) return 0;
|
|
int saved = errno;
|
|
int fixed = give_dir_owner_rights(dirfd, name);
|
|
if (!fixed) errno = saved;
|
|
return fixed;
|
|
}
|
|
|
|
static int dlopen_preload_deps(const char *path, void *(*orig_dlopen)(const char *, int), int depth);
|
|
|
|
// Hook dlopen and dlmopen
|
|
void *dlopen(const char *filename, int flags) {
|
|
static void *(*orig_dlopen)(const char *, int) = NULL;
|
|
if (!orig_dlopen) orig_dlopen = (void *(*)(const char *, int))dlsym(RTLD_NEXT, "dlopen");
|
|
if (!filename) return orig_dlopen ? orig_dlopen(NULL, flags) : NULL;
|
|
if (!orig_dlopen) return NULL;
|
|
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path_follow(filename, buf, sizeof(buf));
|
|
void *handle = orig_dlopen(target, flags);
|
|
if (!handle && target[0] == '/' && !(flags & RTLD_NOLOAD)) {
|
|
// Its dependencies may be in its RUNPATH, which ld.so looked for outside the rootfs
|
|
int saved = errno;
|
|
if (dlopen_preload_deps(target, orig_dlopen, 0) > 0) handle = orig_dlopen(target, flags);
|
|
if (!handle) errno = saved;
|
|
}
|
|
return handle;
|
|
}
|
|
|
|
void *dlmopen(Lmid_t lmid, const char *filename, int flags) {
|
|
static void *(*orig_dlmopen)(Lmid_t, const char *, int) = NULL;
|
|
if (!orig_dlmopen) orig_dlmopen = (void *(*)(Lmid_t, const char *, int))dlsym(RTLD_NEXT, "dlmopen");
|
|
if (!filename) return orig_dlmopen ? orig_dlmopen(lmid, NULL, flags) : NULL;
|
|
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path(filename, buf, sizeof(buf));
|
|
return orig_dlmopen ? orig_dlmopen(lmid, target, flags) : NULL;
|
|
}
|
|
|
|
// /proc/<pid>/status (and a thread's) give the app's own ids, which every other call presents as
|
|
// root: process monitors comparing them with getuid() showed none of the session's processes. Only
|
|
// such tools are given root's there: Chromium (Steam's web helper) checks the ids of the processes it
|
|
// sandboxes in those files, and with them all 0 every helper exited and Steam never drew a frame.
|
|
static int reader_compares_process_ids(void) {
|
|
static const char *const k_readers[] = {
|
|
"ps", "top", "htop", "btop", "atop", "pgrep", "pkill", "pidof", "pstree", "w", "glances",
|
|
"ksystemstats", "plasma-systemmonitor", "ksysguard", "ksysguardd", "kinfocenter", "gnome-system-monitor",
|
|
};
|
|
const char *me = program_invocation_short_name;
|
|
if (!me) return 0;
|
|
for (size_t i = 0; i < sizeof(k_readers) / sizeof(k_readers[0]); i++) {
|
|
if (strcmp(me, k_readers[i]) == 0) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int is_proc_status_path(const char *p) {
|
|
if (!p || strncmp(p, "/proc/", 6) != 0) return 0;
|
|
p += 6;
|
|
if (strncmp(p, "self/", 5) == 0) p += 5;
|
|
else if (strncmp(p, "thread-self/", 12) == 0) p += 12;
|
|
else {
|
|
if (*p < '0' || *p > '9') return 0;
|
|
while (*p >= '0' && *p <= '9') p++;
|
|
if (*p++ != '/') return 0;
|
|
if (strncmp(p, "task/", 5) == 0) {
|
|
p += 5;
|
|
if (*p < '0' || *p > '9') return 0;
|
|
while (*p >= '0' && *p <= '9') p++;
|
|
if (*p++ != '/') return 0;
|
|
}
|
|
}
|
|
return strcmp(p, "status") == 0;
|
|
}
|
|
|
|
static void proc_status_root_ids(char *text, size_t len, const char *field, unsigned long id) {
|
|
size_t flen = strlen(field);
|
|
for (size_t at = 0; at + flen <= len; at++) {
|
|
if ((at != 0 && text[at - 1] != '\n') || memcmp(text + at, field, flen) != 0) continue;
|
|
char *p = text + at + flen;
|
|
char *end = memchr(p, '\n', len - (size_t)(p - text));
|
|
if (!end) end = text + len;
|
|
char out[128];
|
|
size_t used = 0;
|
|
while (p < end && used + 24 < sizeof(out)) {
|
|
while (p < end && (*p == '\t' || *p == ' ')) out[used++] = *p++;
|
|
char *num = p;
|
|
while (p < end && *p >= '0' && *p <= '9') p++;
|
|
if (p == num) break;
|
|
unsigned long v = strtoul(num, NULL, 10);
|
|
used += (size_t)snprintf(out + used, sizeof(out) - used, "%lu", v == id ? 0UL : v);
|
|
}
|
|
if (p != end) return;
|
|
// the line shrinks or stays (an id becomes 0), so it is rewritten in place
|
|
size_t old_len = (size_t)(end - (text + at + flen));
|
|
if (used > old_len) return;
|
|
memcpy(text + at + flen, out, used);
|
|
memmove(text + at + flen + used, end, len - (size_t)(end - text) + 1);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// The status file with the app's ids as root's, in a memory file; -2 when `path` is no such file
|
|
// or is opened for writing.
|
|
static int open_proc_status(const char *path, int flags) {
|
|
if ((flags & O_ACCMODE) != O_RDONLY || !is_proc_status_path(path) || !reader_compares_process_ids()) return -2;
|
|
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
|
if (fd < 0) return -1;
|
|
char text[16384];
|
|
size_t len = 0;
|
|
for (;;) {
|
|
ssize_t n = (ssize_t)syscall(SYS_read, fd, text + len, sizeof(text) - 1 - len);
|
|
if (n <= 0) break;
|
|
len += (size_t)n;
|
|
if (len >= sizeof(text) - 1) break;
|
|
}
|
|
syscall(SYS_close, fd);
|
|
text[len] = '\0';
|
|
if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids();
|
|
proc_status_root_ids(text, len, "Uid:", (unsigned long)__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED));
|
|
len = strlen(text);
|
|
proc_status_root_ids(text, len, "Gid:", (unsigned long)__atomic_load_n(&g_app_gid, __ATOMIC_RELAXED));
|
|
len = strlen(text);
|
|
int mfd = (int)syscall(SYS_memfd_create, "status", (flags & O_CLOEXEC) ? MFD_CLOEXEC : 0);
|
|
if (mfd < 0) return (int)syscall(SYS_openat, AT_FDCWD, path, flags, 0);
|
|
size_t written = 0;
|
|
while (written < len) {
|
|
ssize_t n = (ssize_t)syscall(SYS_write, mfd, text + written, len - written);
|
|
if (n <= 0) break;
|
|
written += (size_t)n;
|
|
}
|
|
syscall(SYS_lseek, mfd, 0, SEEK_SET);
|
|
return mfd;
|
|
}
|
|
|
|
// openat(dirfd, "status") with dirfd on /proc/<pid> or a thread of it
|
|
static int open_proc_status_at(int dirfd, const char *name, int flags) {
|
|
if (dirfd < 0 || !name || strcmp(name, "status") != 0 || (flags & O_ACCMODE) != O_RDONLY) return -2;
|
|
char link[48], dir[PATH_MAX];
|
|
snprintf(link, sizeof(link), "/proc/self/fd/%d", dirfd);
|
|
long n = syscall(SYS_readlinkat, AT_FDCWD, link, dir, sizeof(dir) - 8);
|
|
if (n <= 0) return -2;
|
|
dir[n] = '\0';
|
|
memcpy(dir + n, "/status", 8);
|
|
return open_proc_status(dir, flags);
|
|
}
|
|
|
|
// Hook open
|
|
int open(const char *pathname, int flags, ...) {
|
|
static int (*orig_open)(const char *, int, ...) = NULL;
|
|
if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
|
init_ddnatv_hook();
|
|
char exe_buf[PATH_MAX];
|
|
if (is_proc_self_exe(pathname)) {
|
|
if (g_real_exe[0] != '\0') {
|
|
pathname = g_real_exe;
|
|
} else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) {
|
|
pathname = exe_buf;
|
|
}
|
|
}
|
|
int status_fd = open_proc_status(pathname, flags);
|
|
if (status_fd != -2) return status_fd;
|
|
char buf[PATH_MAX];
|
|
const char *target = proc_root_host(pathname, buf, sizeof(buf), !(flags & O_NOFOLLOW), !(flags & O_NOFOLLOW));
|
|
if (!target) target = ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf));
|
|
if (open_needs_mode(flags)) {
|
|
va_list args;
|
|
va_start(args, flags);
|
|
mode_t mode = va_arg(args, mode_t);
|
|
va_end(args);
|
|
int fd = orig_open(target, flags, mode);
|
|
if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(AT_FDCWD, target)) fd = orig_open(target, flags, mode);
|
|
forget_proc_root_fd(fd);
|
|
return fd;
|
|
}
|
|
int fd = orig_open(target, flags);
|
|
remember_proc_root_fd(fd, pathname);
|
|
return fd;
|
|
}
|
|
|
|
// Hook openat
|
|
int openat(int dirfd, const char *pathname, int flags, ...) {
|
|
static int (*orig_openat)(int, const char *, int, ...) = NULL;
|
|
if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat");
|
|
init_ddnatv_hook();
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
char exe_buf[PATH_MAX];
|
|
if (is_proc_self_exe(pathname)) {
|
|
if (g_real_exe[0] != '\0') {
|
|
pathname = g_real_exe;
|
|
} else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) {
|
|
pathname = exe_buf;
|
|
}
|
|
}
|
|
int status_fd = (pathname && pathname[0] == '/') ? open_proc_status(pathname, flags) : open_proc_status_at(dirfd, pathname, flags);
|
|
if (status_fd != -2) return status_fd;
|
|
char buf[PATH_MAX];
|
|
const char *target = proc_root_host(pathname, buf, sizeof(buf), !(flags & O_NOFOLLOW), !(flags & O_NOFOLLOW));
|
|
if (!target) target = (pathname && pathname[0] == '/') ? ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
|
if (open_needs_mode(flags)) {
|
|
va_list args;
|
|
va_start(args, flags);
|
|
mode_t mode = va_arg(args, mode_t);
|
|
va_end(args);
|
|
int fd = orig_openat(dirfd, target, flags, mode);
|
|
if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(dirfd, target)) fd = orig_openat(dirfd, target, flags, mode);
|
|
forget_proc_root_fd(fd);
|
|
return fd;
|
|
}
|
|
int fd = orig_openat(dirfd, target, flags);
|
|
remember_proc_root_fd(fd, pathname);
|
|
return fd;
|
|
}
|
|
|
|
// Hook creat
|
|
int creat(const char *pathname, mode_t mode) {
|
|
return open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
|
|
}
|
|
|
|
// Fortified open variants used by GNU tar and other coreutils compiled with _FORTIFY_SOURCE=2/3
|
|
int __open_2(const char *pathname, int flags) {
|
|
return open(pathname, flags);
|
|
}
|
|
|
|
int __openat_2(int dirfd, const char *pathname, int flags) {
|
|
return openat(dirfd, pathname, flags);
|
|
}
|
|
|
|
// openat2/open_how local definition: avoids depending on <linux/openat2.h>,
|
|
// which old NDK/cross headers may lack. Layout matches UAPI ver0
|
|
// (flags, mode, resolve = 3 x __u64); extra fields are never read here.
|
|
struct ddnatv_open_how {
|
|
uint64_t flags;
|
|
uint64_t mode;
|
|
uint64_t resolve;
|
|
};
|
|
#ifndef RESOLVE_BENEATH
|
|
#define RESOLVE_BENEATH 0x08
|
|
#endif
|
|
#ifndef __NR_openat2
|
|
#define __NR_openat2 437
|
|
#endif
|
|
#ifndef __NR_fchmodat2
|
|
#define __NR_fchmodat2 452
|
|
#endif
|
|
|
|
static long call_orig_syscall(long number, unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5, unsigned long a6) {
|
|
static long (*orig_syscall)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long) = NULL;
|
|
if (!orig_syscall) {
|
|
orig_syscall = (long (*)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "syscall");
|
|
}
|
|
return orig_syscall ? orig_syscall(number, a1, a2, a3, a4, a5, a6) : syscall(number, a1, a2, a3, a4, a5, a6);
|
|
}
|
|
|
|
// Hook openat2 (used by GNU tar 1.35+/gnulib for RESOLVE_BENEATH traversal).
|
|
// Rewrites absolute paths like openat, then delegates; on ENOSYS (kernels
|
|
// without openat2, e.g. Android < 5.6) emulates with plain openat instead of
|
|
// trusting every caller to implement the fallback itself.
|
|
int openat2(int dirfd, const char *pathname, struct ddnatv_open_how *how, size_t usize) {
|
|
static int (*orig_openat2)(int, const char *, struct ddnatv_open_how *, size_t) = NULL;
|
|
if (!orig_openat2) orig_openat2 = (int (*)(int, const char *, struct ddnatv_open_how *, size_t))dlsym(RTLD_NEXT, "openat2");
|
|
if (!pathname) {
|
|
errno = EFAULT;
|
|
return -1;
|
|
}
|
|
const char *given = pathname;
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
// a remapped path is absolute, which RESOLVE_BENEATH and the like refuse: the caller walks it itself
|
|
if (pathname != given && how && how->resolve != 0) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
char exe_buf[PATH_MAX];
|
|
if (is_proc_self_exe(pathname)) {
|
|
if (g_real_exe[0] != '\0') {
|
|
pathname = g_real_exe;
|
|
} else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) {
|
|
pathname = exe_buf;
|
|
}
|
|
}
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
|
if (orig_openat2) {
|
|
int ret = orig_openat2(dirfd, target, how, usize);
|
|
if (ret >= 0 || errno != ENOSYS) return ret;
|
|
}
|
|
#ifdef __NR_openat2
|
|
long sret = call_orig_syscall(__NR_openat2, (unsigned long)dirfd, (unsigned long)target, (unsigned long)how, (unsigned long)usize, 0, 0);
|
|
if (sret >= 0 || errno != ENOSYS) {
|
|
return (int)sret;
|
|
}
|
|
#endif
|
|
if (!how || usize < 16) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
int flags = (int)(how->flags & 0xffffffffu);
|
|
mode_t mode = (mode_t)(how->mode & 07777u);
|
|
|
|
// RESOLVE_BENEATH, RESOLVE_NO_SYMLINKS and the other resolve flags restrict every component of
|
|
// the path, which a plain openat() cannot do (a symlink in the middle would still be followed).
|
|
// Say so: callers such as gnulib's openat2 (GNU tar) then walk the path safely themselves.
|
|
if (how->resolve != 0) {
|
|
errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
if (open_needs_mode(flags)) {
|
|
return openat(dirfd, target, flags, mode);
|
|
}
|
|
return openat(dirfd, target, flags);
|
|
}
|
|
|
|
// Hook fchmodat2 (used by GNU tar for lchmod-style symlink chmod).
|
|
// Delegates when the libc symbol exists, otherwise emulates: symlinks cannot
|
|
// be chmod'ed without kernel support, so skip them (return 0) like gnulib,
|
|
// and route everything else through fchmodat.
|
|
int fchmodat2(int dirfd, const char *pathname, mode_t mode, int flags) {
|
|
static int (*orig_fchmodat2)(int, const char *, mode_t, int) = NULL;
|
|
if (!orig_fchmodat2) orig_fchmodat2 = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat2");
|
|
if (!pathname) {
|
|
errno = EFAULT;
|
|
return -1;
|
|
}
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
|
if (orig_fchmodat2) {
|
|
int ret = orig_fchmodat2(dirfd, target, mode, flags);
|
|
if (ret == 0 || errno != ENOSYS) return ret;
|
|
}
|
|
if ((flags & AT_SYMLINK_NOFOLLOW) != 0) {
|
|
struct stat st;
|
|
if (fstatat(dirfd, target, &st, AT_SYMLINK_NOFOLLOW) == 0 && S_ISLNK(st.st_mode)) {
|
|
return 0;
|
|
}
|
|
}
|
|
return fchmodat(dirfd, target, mode, 0);
|
|
}
|
|
|
|
|
|
// Hook fopen
|
|
FILE *fopen(const char *pathname, const char *mode) {
|
|
static FILE *(*orig_fopen)(const char *, const char *) = NULL;
|
|
if (!orig_fopen) orig_fopen = (FILE *(*)(const char *, const char *))dlsym(RTLD_NEXT, "fopen");
|
|
if (mode && mode[0] == 'r' && !strchr(mode, '+')) {
|
|
int status_fd = open_proc_status(pathname, O_RDONLY | (strchr(mode, 'e') ? O_CLOEXEC : 0));
|
|
if (status_fd >= 0) return fdopen(status_fd, mode);
|
|
if (status_fd == -1) return NULL;
|
|
}
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
|
|
return orig_fopen(target, mode);
|
|
}
|
|
|
|
// Hook freopen
|
|
FILE *freopen(const char *pathname, const char *mode, FILE *stream) {
|
|
static FILE *(*orig_freopen)(const char *, const char *, FILE *) = NULL;
|
|
if (!orig_freopen) orig_freopen = (FILE *(*)(const char *, const char *, FILE *))dlsym(RTLD_NEXT, "freopen");
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
|
|
return orig_freopen(target, mode, stream);
|
|
}
|
|
|
|
// Hook write
|
|
ssize_t write(int fd, const void *buf, size_t count) {
|
|
static ssize_t (*orig_write)(int, const void *, size_t) = NULL;
|
|
if (!orig_write) orig_write = (ssize_t (*)(int, const void *, size_t))dlsym(RTLD_NEXT, "write");
|
|
return orig_write ? orig_write(fd, buf, count) : -1;
|
|
}
|
|
|
|
// Hook mkstemp
|
|
int mkstemp(char *template) {
|
|
static int (*orig_mkstemp)(char *) = NULL;
|
|
if (!orig_mkstemp) orig_mkstemp = (int (*)(char *))dlsym(RTLD_NEXT, "mkstemp");
|
|
if (!template) { errno = EINVAL; return -1; }
|
|
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path(template, buf, sizeof(buf));
|
|
if (target == template) {
|
|
return orig_mkstemp ? orig_mkstemp(template) : -1;
|
|
}
|
|
|
|
char tmp_buf[PATH_MAX];
|
|
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
|
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
|
|
|
int fd = orig_mkstemp ? orig_mkstemp(tmp_buf) : -1;
|
|
if (fd >= 0) {
|
|
size_t orig_len = strlen(template);
|
|
size_t tmp_len = strlen(tmp_buf);
|
|
if (orig_len >= 6 && tmp_len >= 6) {
|
|
memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6);
|
|
}
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
// Hook mkostemp
|
|
int mkostemp(char *template, int flags) {
|
|
static int (*orig_mkostemp)(char *, int) = NULL;
|
|
if (!orig_mkostemp) orig_mkostemp = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkostemp");
|
|
if (!template) { errno = EINVAL; return -1; }
|
|
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path(template, buf, sizeof(buf));
|
|
if (target == template) {
|
|
return orig_mkostemp ? orig_mkostemp(template, flags) : -1;
|
|
}
|
|
|
|
char tmp_buf[PATH_MAX];
|
|
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
|
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
|
|
|
int fd = orig_mkostemp ? orig_mkostemp(tmp_buf, flags) : -1;
|
|
if (fd >= 0) {
|
|
size_t orig_len = strlen(template);
|
|
size_t tmp_len = strlen(tmp_buf);
|
|
if (orig_len >= 6 && tmp_len >= 6) {
|
|
memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6);
|
|
}
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
// Hook mkstemps
|
|
int mkstemps(char *template, int suffixlen) {
|
|
static int (*orig_mkstemps)(char *, int) = NULL;
|
|
if (!orig_mkstemps) orig_mkstemps = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkstemps");
|
|
if (!template) { errno = EINVAL; return -1; }
|
|
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path(template, buf, sizeof(buf));
|
|
if (target == template) {
|
|
return orig_mkstemps ? orig_mkstemps(template, suffixlen) : -1;
|
|
}
|
|
|
|
char tmp_buf[PATH_MAX];
|
|
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
|
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
|
|
|
int fd = orig_mkstemps ? orig_mkstemps(tmp_buf, suffixlen) : -1;
|
|
if (fd >= 0) {
|
|
size_t orig_len = strlen(template);
|
|
size_t tmp_len = strlen(tmp_buf);
|
|
if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) {
|
|
memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6);
|
|
}
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
// Hook mkostemps
|
|
int mkostemps(char *template, int suffixlen, int flags) {
|
|
static int (*orig_mkostemps)(char *, int, int) = NULL;
|
|
if (!orig_mkostemps) orig_mkostemps = (int (*)(char *, int, int))dlsym(RTLD_NEXT, "mkostemps");
|
|
if (!template) { errno = EINVAL; return -1; }
|
|
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path(template, buf, sizeof(buf));
|
|
if (target == template) {
|
|
return orig_mkostemps ? orig_mkostemps(template, suffixlen, flags) : -1;
|
|
}
|
|
|
|
char tmp_buf[PATH_MAX];
|
|
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
|
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
|
|
|
int fd = orig_mkostemps ? orig_mkostemps(tmp_buf, suffixlen, flags) : -1;
|
|
if (fd >= 0) {
|
|
size_t orig_len = strlen(template);
|
|
size_t tmp_len = strlen(tmp_buf);
|
|
if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) {
|
|
memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6);
|
|
}
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
// Hook mkdtemp
|
|
char *mkdtemp(char *template) {
|
|
static char *(*orig_mkdtemp)(char *) = NULL;
|
|
if (!orig_mkdtemp) orig_mkdtemp = (char *(*)(char *))dlsym(RTLD_NEXT, "mkdtemp");
|
|
if (!template) { errno = EINVAL; return NULL; }
|
|
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path(template, buf, sizeof(buf));
|
|
if (target == template) {
|
|
return orig_mkdtemp ? orig_mkdtemp(template) : NULL;
|
|
}
|
|
|
|
char tmp_buf[PATH_MAX];
|
|
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
|
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
|
|
|
char *res = orig_mkdtemp ? orig_mkdtemp(tmp_buf) : NULL;
|
|
if (res) {
|
|
size_t orig_len = strlen(template);
|
|
size_t tmp_len = strlen(tmp_buf);
|
|
if (orig_len >= 6 && tmp_len >= 6) {
|
|
memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6);
|
|
}
|
|
return template;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
// Hook tmpfile
|
|
FILE *tmpfile(void) {
|
|
init_ddnatv_hook();
|
|
char template_buf[PATH_MAX];
|
|
if (g_ddnatv_root[0] != '\0') {
|
|
snprintf(template_buf, sizeof(template_buf), "%s/tmp/tmpfile.XXXXXX", g_ddnatv_root);
|
|
} else {
|
|
snprintf(template_buf, sizeof(template_buf), "/tmp/tmpfile.XXXXXX");
|
|
}
|
|
int fd = mkstemp(template_buf);
|
|
if (fd < 0) return NULL;
|
|
unlink(template_buf);
|
|
FILE *f = fdopen(fd, "w+b");
|
|
if (!f) {
|
|
int saved_errno = errno;
|
|
close(fd);
|
|
errno = saved_errno;
|
|
}
|
|
return f;
|
|
}
|
|
|
|
#ifndef __USE_FILE_OFFSET64
|
|
// Programs built with _LARGEFILE64_SOURCE call this name. With _FILE_OFFSET_BITS=64 (the 32-bit ARM
|
|
// build) the headers already export the definition above as tmpfile64.
|
|
FILE *tmpfile64(void) {
|
|
return tmpfile();
|
|
}
|
|
#endif
|
|
|
|
// Hook opendir
|
|
DIR *opendir(const char *name) {
|
|
static DIR *(*orig_opendir)(const char *) = NULL;
|
|
if (!orig_opendir) orig_opendir = (DIR *(*)(const char *))dlsym(RTLD_NEXT, "opendir");
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path_follow(name, buf, sizeof(buf));
|
|
return orig_opendir(target);
|
|
}
|
|
|
|
// Hook stat
|
|
int stat(const char *pathname, struct stat *statbuf) {
|
|
static int (*orig_stat)(const char *, struct stat *) = NULL;
|
|
if (!orig_stat) orig_stat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "stat");
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
|
|
int ret = orig_stat ? orig_stat(target, statbuf) : -1;
|
|
if (ret != 0 && errno == ENOENT) {
|
|
char alt[PATH_MAX];
|
|
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
|
if (retry) ret = orig_stat(retry, statbuf);
|
|
}
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
// Hook lstat
|
|
int lstat(const char *pathname, struct stat *statbuf) {
|
|
static int (*orig_lstat)(const char *, struct stat *) = NULL;
|
|
if (!orig_lstat) orig_lstat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "lstat");
|
|
char buf[PATH_MAX];
|
|
const char *target = rewrite_path(pathname, buf, sizeof(buf));
|
|
int ret = orig_lstat ? orig_lstat(target, statbuf) : -1;
|
|
char alt[PATH_MAX];
|
|
if (ret != 0 && errno == ENOENT) {
|
|
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
|
if (retry) {
|
|
ret = orig_lstat(retry, statbuf);
|
|
if (ret == 0) target = retry;
|
|
}
|
|
}
|
|
if (ret == 0 && statbuf && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target &&
|
|
readlink_result_may_change(pathname, (long long)statbuf->st_size)) {
|
|
char link_buf[PATH_MAX];
|
|
static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL;
|
|
if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
|
|
ssize_t llen = orig_readlink ? orig_readlink(target, link_buf, sizeof(link_buf) - 1) : -1;
|
|
if (llen > 0) {
|
|
ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf));
|
|
if (post_len > 0) {
|
|
statbuf->st_size = post_len;
|
|
}
|
|
}
|
|
}
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
// Hook fstatat
|
|
int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) {
|
|
static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL;
|
|
if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat");
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
|
int ret = orig_fstatat ? orig_fstatat(dirfd, target, statbuf, flags) : -1;
|
|
char alt[PATH_MAX];
|
|
if (ret != 0 && errno == ENOENT && pathname && pathname[0] == '/') {
|
|
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
|
if (retry) {
|
|
ret = orig_fstatat(dirfd, retry, statbuf, flags);
|
|
if (ret == 0) target = retry;
|
|
}
|
|
}
|
|
if (ret == 0 && statbuf && (flags & AT_SYMLINK_NOFOLLOW) && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target &&
|
|
readlink_result_may_change(pathname, (long long)statbuf->st_size)) {
|
|
char link_buf[PATH_MAX];
|
|
static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL;
|
|
if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat");
|
|
ssize_t llen = orig_readlinkat ? orig_readlinkat(dirfd, target, link_buf, sizeof(link_buf) - 1) : -1;
|
|
if (llen > 0) {
|
|
ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf));
|
|
if (post_len > 0) {
|
|
statbuf->st_size = post_len;
|
|
}
|
|
}
|
|
}
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
// Hook fstat
|
|
int fstat(int fd, struct stat *statbuf) {
|
|
static int (*orig_fstat)(int, struct stat *) = NULL;
|
|
if (!orig_fstat) orig_fstat = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat");
|
|
int ret = orig_fstat ? orig_fstat(fd, statbuf) : -1;
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
// Glibc __xstat compatibility hooks, what programs built against glibc before 2.33 call (Firefox):
|
|
// they present owners like stat() does, or Firefox saw $HOME owned by the app's uid while geteuid()
|
|
// said 0 and refused to start ("as root in a regular user's session").
|
|
int __xstat(int ver, const char *pathname, struct stat *statbuf) {
|
|
static int (*orig___xstat)(int, const char *, struct stat *) = NULL;
|
|
if (!orig___xstat) orig___xstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__xstat");
|
|
if (!orig___xstat) return stat(pathname, statbuf);
|
|
char buf[PATH_MAX];
|
|
int ret = orig___xstat(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf);
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
int __xstat64(int ver, const char *pathname, struct stat64 *statbuf) {
|
|
static int (*orig___xstat64)(int, const char *, struct stat64 *) = NULL;
|
|
if (!orig___xstat64) orig___xstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__xstat64");
|
|
if (!orig___xstat64) return stat(pathname, (struct stat *)statbuf);
|
|
char buf[PATH_MAX];
|
|
int ret = orig___xstat64(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf);
|
|
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
|
return ret;
|
|
}
|
|
|
|
int __lxstat(int ver, const char *pathname, struct stat *statbuf) {
|
|
static int (*orig___lxstat)(int, const char *, struct stat *) = NULL;
|
|
if (!orig___lxstat) orig___lxstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__lxstat");
|
|
if (!orig___lxstat) return lstat(pathname, statbuf);
|
|
char buf[PATH_MAX];
|
|
int ret = orig___lxstat(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf);
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
int __lxstat64(int ver, const char *pathname, struct stat64 *statbuf) {
|
|
static int (*orig___lxstat64)(int, const char *, struct stat64 *) = NULL;
|
|
if (!orig___lxstat64) orig___lxstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__lxstat64");
|
|
if (!orig___lxstat64) return lstat(pathname, (struct stat *)statbuf);
|
|
char buf[PATH_MAX];
|
|
int ret = orig___lxstat64(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf);
|
|
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
|
return ret;
|
|
}
|
|
|
|
int __fxstat(int ver, int fd, struct stat *statbuf) {
|
|
static int (*orig___fxstat)(int, int, struct stat *) = NULL;
|
|
if (!orig___fxstat) orig___fxstat = (int (*)(int, int, struct stat *))dlsym(RTLD_NEXT, "__fxstat");
|
|
if (!orig___fxstat) return fstat(fd, statbuf);
|
|
int ret = orig___fxstat(ver, fd, statbuf);
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
int __fxstat64(int ver, int fd, struct stat64 *statbuf) {
|
|
static int (*orig___fxstat64)(int, int, struct stat64 *) = NULL;
|
|
if (!orig___fxstat64) orig___fxstat64 = (int (*)(int, int, struct stat64 *))dlsym(RTLD_NEXT, "__fxstat64");
|
|
if (!orig___fxstat64) return fstat(fd, (struct stat *)statbuf);
|
|
int ret = orig___fxstat64(ver, fd, statbuf);
|
|
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
|
return ret;
|
|
}
|
|
|
|
int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *statbuf, int flags) {
|
|
static int (*orig___fxstatat)(int, int, const char *, struct stat *, int) = NULL;
|
|
if (!orig___fxstatat) orig___fxstatat = (int (*)(int, int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "__fxstatat");
|
|
if (!orig___fxstatat) return fstatat(dirfd, pathname, statbuf, flags);
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
|
int ret = orig___fxstatat(ver, dirfd, target, statbuf, flags);
|
|
if (ret == 0) apply_stat_emulation(statbuf);
|
|
return ret;
|
|
}
|
|
|
|
int __fxstatat64(int ver, int dirfd, const char *pathname, struct stat64 *statbuf, int flags) {
|
|
static int (*orig___fxstatat64)(int, int, const char *, struct stat64 *, int) = NULL;
|
|
if (!orig___fxstatat64) orig___fxstatat64 = (int (*)(int, int, const char *, struct stat64 *, int))dlsym(RTLD_NEXT, "__fxstatat64");
|
|
if (!orig___fxstatat64) return fstatat(dirfd, pathname, (struct stat *)statbuf, flags);
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
|
int ret = orig___fxstatat64(ver, dirfd, target, statbuf, flags);
|
|
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
|
return ret;
|
|
}
|
|
|
|
// Hook statx
|
|
int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf) {
|
|
// Rust's standard library asks whether statx exists by calling it with a NULL path and buffer
|
|
// (EFAULT means it does). glibc's header declares both as nonnull, so the compiler would drop
|
|
// the NULL check below and read the path: every Rust program (rg, rustc, cargo...) crashed.
|
|
__asm__ volatile("" : "+r"(pathname), "+r"(statxbuf));
|
|
static int (*orig_statx)(int, const char *, int, unsigned int, struct statx *) = NULL;
|
|
if (!orig_statx) orig_statx = (int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(RTLD_NEXT, "statx");
|
|
DDNATV_AT_REMAP(dirfd, pathname);
|
|
char buf[PATH_MAX];
|
|
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
|
int ret = orig_statx ? orig_statx(dirfd, target, flags, mask, statxbuf) : -1;
|
|
if (ret == 0 && statxbuf && hardlinks_maybe_any()) {
|
|
const unsigned int needed = STATX_INO | STATX_NLINK | STATX_MTIME;
|
|
struct statx full;
|
|
const struct statx *sx = statxbuf;
|
|
if ((statxbuf->stx_mask & needed) != needed) {
|
|
// The caller asked for less ("stat -c %h" for the link count only): what an emulated
|
|
// link is recognized by
|
|
int saved_errno = errno;
|
|
sx = orig_statx(dirfd, target, flags, needed, &full) == 0 && (full.stx_mask & needed) == needed ? &full : NULL;
|
|
errno = saved_errno;
|
|
}
|
|
if (sx) {
|
|
struct stat st;
|
|
memset(&st, 0, sizeof(st));
|
|
st.st_dev = makedev(sx->stx_dev_major, sx->stx_dev_minor);
|
|
st.st_ino = sx->stx_ino;
|
|
st.st_nlink = sx->stx_nlink;
|
|
st.st_mtim.tv_sec = sx->stx_mtime.tv_sec;
|
|
st.st_mtim.tv_nsec = sx->stx_mtime.tv_nsec;
|
|
apply_emulated_hardlink(&st);
|
|
if (st.st_ino != sx->stx_ino || st.st_nlink != sx->stx_nlink) {
|
|
statxbuf->stx_ino = st.st_ino;
|
|
statxbuf->stx_nlink = st.st_nlink;
|
|
statxbuf->stx_mask |= STATX_INO | STATX_NLINK;
|
|
}
|
|
}
|
|
}
|
|
if (ret == 0 && statxbuf) {
|
|
uid_t uid = (uid_t)statxbuf->stx_uid;
|
|
gid_t gid = (gid_t)statxbuf->stx_gid;
|
|
present_owner_as_root((statxbuf->stx_mask & STATX_UID) ? &uid : NULL, (statxbuf->stx_mask & STATX_GID) ? &gid : NULL);
|
|
statxbuf->stx_uid = (uint32_t)uid;
|
|
statxbuf->stx_gid = (uint32_t)gid;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
#endif /* !DDNATV_HOST_TEST */
|