Files
DroidDeck/tools/linuxfs/preload/opens.c
T
MaxsTechReview ff5e4c7ac7 fix(preload): accept NETLINK_ROUTE multicast subscriptions (#3015)
* fix(preload): accept NETLINK_ROUTE multicast subscriptions

Programs that watch for network changes bind their route socket with
multicast groups or join them with NETLINK_ADD_MEMBERSHIP. The emulated
socket refused both with EACCES, and .NET's NetworkChange treats that as
fatal: CurseForge's Mods Agent failed to initialise with "Permission
denied" from NetworkChange.CreateSocket(), so the app never loaded its
library.

Real Linux lets unprivileged sockets join rtnetlink groups. Accept the
groups at bind and through ADD/DROP_MEMBERSHIP, report them from
getsockname(), and keep queries on the same socket working. No change
events are generated yet; queries still return the current snapshot.

* fix(preload): keep netlink group membership in the shared socket state

Memberships now live in the peer metadata the kernel holds for the socket, next to the port and cap ack, so they survive exec and a change made by one process after fork is seen by every process holding the socket.

* fix(session): answer denied /proc/net socket tables

Android's SELinux policy denies app processes every file under /proc/net.
.NET lists TCP listeners from /proc/net/tcp and tcp6 before it binds a
server port, so the CurseForge Mods Agent failed on every port with
"An error was encountered while querying information from the operating
system" on stock firmware. Denied tcp, tcp6, udp and udp6 reads now get a
header-only table, through open and openat as well as fopen, the same way
the route and interface tables already are.

* fix(session): keep synthesized socket tables read-only and file-only

Reopen the populated table read-only instead of duplicating its writable
descriptor, and leave O_DIRECTORY opens with their original failure.
2026-11-22 13:37:44 -05:00

136 lines
4.7 KiB
C

/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
int dd_status_without_tracer(const char *path, int flags, const void *caller) __attribute__((visibility("hidden")));
int dd_writable_retry(int dirfd, const char *path, int flags) __attribute__((visibility("hidden")));
int dd_ntsync_open(const char *path, int flags) __attribute__((visibility("hidden")));
const char *dd_oom_adj_redirect(const char *path, int writing) __attribute__((visibility("hidden")));
int dd_venc_open(const char *path, int flags) __attribute__((visibility("hidden")));
int dd_vdec_open(const char *path, int flags) __attribute__((visibility("hidden")));
int dd_net_open(const char *path, int flags, int fd) __attribute__((visibility("hidden")));
int dd_dircache_create(int dirfd, const char *path, int flags, mode_t mode,
int (*real)(int, const char *, int, mode_t)) __attribute__((visibility("hidden")));
static int (*real_open)(const char *, int, ...);
static int (*real_openat)(int, const char *, int, ...);
static int via_open(int dirfd, const char *path, int flags, mode_t mode) {
(void)dirfd;
return real_open(path, flags, mode);
}
static int via_openat(int dirfd, const char *path, int flags, mode_t mode) {
return real_openat(dirfd, path, flags, mode);
}
static int retry_without_noatime(int fd, int flags) {
return fd < 0 && errno == EPERM && (flags & O_NOATIME);
}
static int is_trace_marker(const char *path) {
return path != NULL
&& (strcmp(path, "/sys/kernel/tracing/trace_marker") == 0
|| strcmp(path, "/sys/kernel/debug/tracing/trace_marker") == 0);
}
static int open_from(const char *path, int flags, mode_t mode, const void *caller) {
int fd;
if (is_trace_marker(path)) {
errno = EACCES;
return -1;
}
path = dd_oom_adj_redirect(path, (flags & O_ACCMODE) != O_RDONLY);
if ((fd = dd_status_without_tracer(path, flags, caller)) >= 0) return fd;
if ((fd = dd_ntsync_open(path, flags)) != -2) return fd;
if ((fd = dd_venc_open(path, flags)) != -2) return fd;
if ((fd = dd_vdec_open(path, flags)) != -2) return fd;
if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open");
if (real_open == NULL) {
errno = ENOSYS;
return -1;
}
fd = dd_dircache_create(AT_FDCWD, path, flags, mode, via_open);
if (retry_without_noatime(fd, flags)) fd = real_open(path, flags & ~O_NOATIME, mode);
if (fd < 0 && dd_writable_retry(AT_FDCWD, path, flags)) fd = real_open(path, flags & ~O_NOATIME, mode);
return dd_net_open(path, flags, fd);
}
static int openat_from(int dirfd, const char *path, int flags, mode_t mode, const void *caller) {
int fd;
if (is_trace_marker(path)) {
errno = EACCES;
return -1;
}
path = dd_oom_adj_redirect(path, (flags & O_ACCMODE) != O_RDONLY);
if ((fd = dd_status_without_tracer(path, flags, caller)) >= 0) return fd;
if ((fd = dd_ntsync_open(path, flags)) != -2) return fd;
if ((fd = dd_venc_open(path, flags)) != -2) return fd;
if ((fd = dd_vdec_open(path, flags)) != -2) return fd;
if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat");
if (real_openat == NULL) {
errno = ENOSYS;
return -1;
}
fd = dd_dircache_create(dirfd, path, flags, mode, via_openat);
if (retry_without_noatime(fd, flags)) fd = real_openat(dirfd, path, flags & ~O_NOATIME, mode);
if (fd < 0 && dd_writable_retry(dirfd, path, flags)) fd = real_openat(dirfd, path, flags & ~O_NOATIME, mode);
return dd_net_open(path, flags, fd);
}
int open(const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return open_from(path, flags, mode, __builtin_return_address(0));
}
int openat(int dirfd, const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return openat_from(dirfd, path, flags, mode, __builtin_return_address(0));
}
#ifdef __OFF_T_MATCHES_OFF64_T
int open64(const char *path, int flags, ...) __attribute__((alias("open")));
int openat64(int dirfd, const char *path, int flags, ...) __attribute__((alias("openat")));
#else
int open64(const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return open_from(path, flags | O_LARGEFILE, mode, __builtin_return_address(0));
}
int openat64(int dirfd, const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return openat_from(dirfd, path, flags | O_LARGEFILE, mode, __builtin_return_address(0));
}
#endif