SteamDeck: a standalone launcher for the native ARM64 Linux Steam client

Lifts Bannerlator's Linux/gamescope runtime out into an app of its own: the
Wayland compositor, the proot session, the runtime installer, the fake-evdev
controller path and the PulseAudio server, with none of the Wine, container or
store machinery around them.

One activity installs the runtime from the same winlator-contents catalog and a
second one runs the session: compositor on a SurfaceView, gamescope and Valve's
native arm64 client under proot, a physical pad republished into the fake-input
rings, and the session's milestones on screen until the first frame arrives.
This commit is contained in:
The412Banner committed 2026-09-19 13:39:17 -04:00
commit 4022351e22
210 files changed
+73587

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
/*
* GEM handle emulation for the KGSL device presented as a DRM render node.
*
* Compositors and Mesa validate or convert dma-bufs with PRIME ioctls on the render node; KGSL
* has no GEM. Handles only serve as tokens to these callers, so a table of duplicated dma-buf
* descriptors stands in for the kernel's, and every other device is left to libdrm.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_HANDLES 4096
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int handle_fds[MAX_HANDLES];
static dev_t kgsl_dev;
static int kgsl_state; /* 0 unknown, 1 known, -1 absent */
static int is_kgsl(int fd) {
struct stat st;
pthread_mutex_lock(&lock);
if (kgsl_state == 0) {
kgsl_state = stat("/dev/kgsl-3d0", &st) == 0 ? 1 : -1;
kgsl_dev = st.st_rdev;
}
pthread_mutex_unlock(&lock);
return kgsl_state > 0 && fstat(fd, &st) == 0 && S_ISCHR(st.st_mode) && st.st_rdev == kgsl_dev;
}
static void *real(const char *name) {
void *fn = dlsym(RTLD_NEXT, name);
if (!fn) {
errno = ENOSYS;
}
return fn;
}
int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) {
if (!is_kgsl(fd)) {
int (*fn)(int, int, uint32_t *) = (int (*)(int, int, uint32_t *)) real("drmPrimeFDToHandle");
return fn ? fn(fd, prime_fd, handle) : -ENOSYS;
}
int dup_fd = fcntl(prime_fd, F_DUPFD_CLOEXEC, 0);
if (dup_fd < 0) {
return -errno;
}
int ret = -ENOMEM;
pthread_mutex_lock(&lock);
for (uint32_t i = 1; i < MAX_HANDLES; i++) {
if (handle_fds[i] == 0) {
handle_fds[i] = dup_fd;
*handle = i;
ret = 0;
break;
}
}
pthread_mutex_unlock(&lock);
if (ret) {
close(dup_fd);
}
return ret;
}
int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd) {
if (!is_kgsl(fd)) {
int (*fn)(int, uint32_t, uint32_t, int *) = (int (*)(int, uint32_t, uint32_t, int *)) real("drmPrimeHandleToFD");
return fn ? fn(fd, handle, flags, prime_fd) : -ENOSYS;
}
int ret = -EINVAL;
pthread_mutex_lock(&lock);
if (handle > 0 && handle < MAX_HANDLES && handle_fds[handle] > 0) {
*prime_fd = fcntl(handle_fds[handle], F_DUPFD_CLOEXEC, 0);
ret = *prime_fd < 0 ? -errno : 0;
}
pthread_mutex_unlock(&lock);
return ret;
}
int drmCloseBufferHandle(int fd, uint32_t handle) {
if (!is_kgsl(fd)) {
int (*fn)(int, uint32_t) = (int (*)(int, uint32_t)) real("drmCloseBufferHandle");
return fn ? fn(fd, handle) : -ENOSYS;
}
int ret = -EINVAL;
pthread_mutex_lock(&lock);
if (handle > 0 && handle < MAX_HANDLES && handle_fds[handle] > 0) {
close(handle_fds[handle]);
handle_fds[handle] = 0;
ret = 0;
}
pthread_mutex_unlock(&lock);
return ret;
}
/*
* fork() carries over only the calling thread, so a lock another thread was holding at that
* instant stays held in the child by a thread that is not there to release it. Taking it before
* the fork makes the copy consistent; the parent then unlocks it and the child, whose one thread
* never locked it, gets a fresh one. (WinNative 79aa7f68.)
*/
static void drm_lock_before_fork(void) { pthread_mutex_lock(&lock); }
static void drm_unlock_after_fork(void) { pthread_mutex_unlock(&lock); }
static void drm_reset_after_fork(void) { pthread_mutex_init(&lock, NULL); }
__attribute__((constructor)) static void install_drm_fork_handlers(void) {
pthread_atfork(drm_lock_before_fork, drm_unlock_after_fork, drm_reset_after_fork);
}
+293
View File
@@ -0,0 +1,293 @@
/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
* changed to this project's. The reasoning below is the original author's.
*
* The session's controllers, described to libudev's callers.
*
* The pads are evdev nodes the app backs with its own input rings, bound over /dev/input. Wine's
* HID bus finds them there, but it identifies a node by resolving /sys/class/input/<name> and
* asking libudev about the result: on Android that resolves to whatever real input device the
* host numbered the same, whose device node libudev cannot read, so every pad is discarded before
* its identity is ever looked at. The fallback is Wine's SDL bus, and SDL enumerates through udev
* too, over a netlink socket the app sandbox refuses. A game was left with no controller at all
* while the Steam client, which reads the nodes itself, had one.
*
* So the pads are described here instead: the resolved path is answered for the session's own
* nodes, and libudev's accessors answer for that path from what the app already knows. They carry
* the identity of the pad Steam Input would present if it could - it has no /dev/uinput here to
* make one - which is the one identity Wine's own bus keeps rather than handing on to SDL.
*
* Only Wine's device host is answered, and only for the session's own nodes: the Steam client
* enumerates the same nodes for itself and would take a pad wearing Steam Input's identity for one
* of its own and ignore it. Every other caller, path and device object goes straight through.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
/* The app opens one ring per configured controller; four is what its launcher offers. */
#define PAD_MAX 4
#define EVENT_MINOR_BASE 64
/* BUS_VIRTUAL, and the identity Steam Input gives the gamepad it presents to a game. */
#define PAD_UEVENT_FORMAT \
"PRODUCT=6/28de/11ff/110\n" \
"NAME=\"Generic HID Gamepad %d\"\n" \
"PHYS=\"usb-fakeinput/input%d\"\n"
struct pad {
char syspath[64];
char devnode[32];
char sysname[16];
char uevent[128];
dev_t devnum;
};
static struct pad pads[PAD_MAX];
static int pad_count;
/*
* The slots the app published rings for, named in FAKE_EVDEV_MEMFD_PATHS as "<slot>=<path>"
* separated by semicolons. Without it the process has no pads of the session's and every call
* here is a passthrough.
*/
static void discover_pads(void) {
const char *spec = getenv("FAKE_EVDEV_MEMFD_PATHS");
const char *entry;
if (spec == NULL) return;
for (entry = spec; *entry != '\0';) {
char *end;
long slot = strtol(entry, &end, 10);
if (end != entry && *end == '=' && slot >= 0 && slot < PAD_MAX) {
struct pad *pad = &pads[slot];
if (pad->devnode[0] == '\0') {
snprintf(pad->sysname, sizeof(pad->sysname), "event%ld", slot);
snprintf(pad->syspath, sizeof(pad->syspath), "/sys/devices/virtual/input/bannerlator%ld",
slot);
snprintf(pad->devnode, sizeof(pad->devnode), "/dev/input/event%ld", slot);
snprintf(pad->uevent, sizeof(pad->uevent), PAD_UEVENT_FORMAT, (int)slot, (int)slot);
pad->devnum = makedev(13, EVENT_MINOR_BASE + (unsigned int)slot);
if (slot >= pad_count) pad_count = (int)slot + 1;
}
}
entry = strchr(entry, ';');
if (entry == NULL) break;
entry++;
}
}
static void ensure_pads(void) {
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, discover_pads);
}
/* The pad a synthetic syspath names, or NULL when the path is not one of ours. */
static struct pad *pad_for_syspath(const char *syspath) {
int i;
if (syspath == NULL) return NULL;
ensure_pads();
for (i = 0; i < pad_count; i++) {
if (pads[i].devnode[0] != '\0' && strcmp(pads[i].syspath, syspath) == 0) return &pads[i];
}
return NULL;
}
/* A device object is ours exactly when it is one of the pads. */
static struct pad *pad_for_device(void *device) {
int i;
if (device == NULL) return NULL;
ensure_pads();
for (i = 0; i < pad_count; i++) {
if (&pads[i] == device) return pads[i].devnode[0] != '\0' ? &pads[i] : NULL;
}
return NULL;
}
/* The pad /sys/class/input/<name> stands for, or NULL when the path names something else. */
static struct pad *pad_for_syslink(const char *path) {
static const char prefix[] = "/sys/class/input/";
const char *name;
int i;
if (path == NULL || strncmp(path, prefix, sizeof(prefix) - 1) != 0) return NULL;
name = path + sizeof(prefix) - 1;
ensure_pads();
for (i = 0; i < pad_count; i++) {
if (pads[i].devnode[0] != '\0' && strcmp(pads[i].sysname, name) == 0) return &pads[i];
}
return NULL;
}
static void *next_symbol(const char *name) { return dlsym(RTLD_NEXT, name); }
/* Wine reaches libudev from its HID bus driver, which is the only caller these pads are for. */
static int caller_is_winebus(void *caller) {
Dl_info info;
if (dladdr(caller, &info) == 0 || info.dli_fname == NULL) return 0;
return strstr(info.dli_fname, "winebus.so") != NULL;
}
char *realpath(const char *path, char *resolved) {
static char *(*real_realpath)(const char *, char *);
void *caller = __builtin_return_address(0);
struct pad *pad = caller_is_winebus(caller) ? pad_for_syslink(path) : NULL;
if (pad != NULL) {
if (resolved == NULL) return strdup(pad->syspath);
/* PATH_MAX is what the caller promises when it passes a buffer. */
memcpy(resolved, pad->syspath, strlen(pad->syspath) + 1);
return resolved;
}
if (real_realpath == NULL) real_realpath = next_symbol("realpath");
if (real_realpath == NULL) return NULL;
return real_realpath(path, resolved);
}
void *udev_device_new_from_syspath(void *udev, const char *syspath) {
static void *(*real_new)(void *, const char *);
void *caller = __builtin_return_address(0);
struct pad *pad = caller_is_winebus(caller) ? pad_for_syspath(syspath) : NULL;
if (pad != NULL) return pad;
if (real_new == NULL) real_new = next_symbol("udev_device_new_from_syspath");
if (real_new == NULL) return NULL;
return real_new(udev, syspath);
}
void *udev_device_ref(void *device) {
static void *(*real_ref)(void *);
if (pad_for_device(device) != NULL) return device;
if (real_ref == NULL) real_ref = next_symbol("udev_device_ref");
if (real_ref == NULL) return NULL;
return real_ref(device);
}
void *udev_device_unref(void *device) {
static void *(*real_unref)(void *);
if (pad_for_device(device) != NULL) return NULL;
if (real_unref == NULL) real_unref = next_symbol("udev_device_unref");
if (real_unref == NULL) return NULL;
return real_unref(device);
}
const char *udev_device_get_devnode(void *device) {
static const char *(*real_get)(void *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return pad->devnode;
if (real_get == NULL) real_get = next_symbol("udev_device_get_devnode");
if (real_get == NULL) return NULL;
return real_get(device);
}
const char *udev_device_get_syspath(void *device) {
static const char *(*real_get)(void *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return pad->syspath;
if (real_get == NULL) real_get = next_symbol("udev_device_get_syspath");
if (real_get == NULL) return NULL;
return real_get(device);
}
const char *udev_device_get_sysname(void *device) {
static const char *(*real_get)(void *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return pad->sysname;
if (real_get == NULL) real_get = next_symbol("udev_device_get_sysname");
if (real_get == NULL) return NULL;
return real_get(device);
}
const char *udev_device_get_subsystem(void *device) {
static const char *(*real_get)(void *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return "input";
if (real_get == NULL) real_get = next_symbol("udev_device_get_subsystem");
if (real_get == NULL) return NULL;
return real_get(device);
}
const char *udev_device_get_devtype(void *device) {
static const char *(*real_get)(void *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return NULL;
if (real_get == NULL) real_get = next_symbol("udev_device_get_devtype");
if (real_get == NULL) return NULL;
return real_get(device);
}
const char *udev_device_get_action(void *device) {
static const char *(*real_get)(void *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return NULL;
if (real_get == NULL) real_get = next_symbol("udev_device_get_action");
if (real_get == NULL) return NULL;
return real_get(device);
}
dev_t udev_device_get_devnum(void *device) {
static dev_t (*real_get)(void *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return pad->devnum;
if (real_get == NULL) real_get = next_symbol("udev_device_get_devnum");
if (real_get == NULL) return makedev(0, 0);
return real_get(device);
}
/*
* A pad has no bus behind it, so it stands in for its own input device and has no other parent.
* The reference is a borrowed one either way.
*/
void *udev_device_get_parent_with_subsystem_devtype(void *device, const char *subsystem,
const char *devtype) {
static void *(*real_get)(void *, const char *, const char *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) {
if (subsystem != NULL && strcmp(subsystem, "input") == 0 && devtype == NULL) return pad;
return NULL;
}
if (real_get == NULL) real_get = next_symbol("udev_device_get_parent_with_subsystem_devtype");
if (real_get == NULL) return NULL;
return real_get(device, subsystem, devtype);
}
const char *udev_device_get_sysattr_value(void *device, const char *attr) {
static const char *(*real_get)(void *, const char *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return attr != NULL && strcmp(attr, "uevent") == 0 ? pad->uevent : NULL;
if (real_get == NULL) real_get = next_symbol("udev_device_get_sysattr_value");
if (real_get == NULL) return NULL;
return real_get(device, attr);
}
const char *udev_device_get_property_value(void *device, const char *key) {
static const char *(*real_get)(void *, const char *);
struct pad *pad = pad_for_device(device);
if (pad != NULL) return key != NULL && strcmp(key, "ID_INPUT_JOYSTICK") == 0 ? "1" : NULL;
if (real_get == NULL) real_get = next_symbol("udev_device_get_property_value");
if (real_get == NULL) return NULL;
return real_get(device, key);
}
+109
View File
@@ -0,0 +1,109 @@
/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
* changed to this project's. The reasoning below is the original author's.
*
* File locking for the game library on external storage.
*
* The user's games are bound in from /storage/emulated, which Android serves over FUSE, and that
* filesystem implements no locking at all: flock() and fcntl()'s lock commands answer ENOSYS for
* every file under it. The Steam client takes a shared lock on each content file as it validates
* or updates a title and treats the refusal as a failure, so a download walks its progress back
* and forth instead of finishing; a game reading its own data hits the same wall.
*
* Where the kernel can lock, it does: the real call runs first and its answer stands, so files on
* the app's own f2fs storage keep genuine cross-process exclusion. Only the filesystem that cannot
* lock at all is answered here, and granting is the honest reading of it - a filesystem with no
* lock state has nothing to contend over, and the one other party that could contend is inside
* this same session. Reporting failure instead buys no safety and costs the download.
*
* F_GETLK is the exception: it asks who holds a lock, and on a filesystem holding none the true
* answer is "nobody", so it is reported unlocked rather than granted.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <sys/file.h>
#include <unistd.h>
/* The filesystem refuses the whole operation, rather than declining this particular lock. */
static int unsupported(int error) {
return error == ENOSYS || error == ENOTSUP || error == EOPNOTSUPP;
}
int flock(int fd, int operation) {
static int (*real_flock)(int, int);
int result;
if (real_flock == NULL) real_flock = dlsym(RTLD_NEXT, "flock");
if (real_flock == NULL) return 0;
result = real_flock(fd, operation);
if (result < 0 && unsupported(errno)) return 0;
return result;
}
/*
* fcntl()'s third argument is an int, a pointer, or absent depending on the command; only the
* lock commands are of interest here and all of those take a struct flock *. Anything else is
* passed straight through as a void * - wide enough for both an int and a pointer on every ABI
* the runtime is built for.
*/
static int is_lock_command(int cmd) {
switch (cmd) {
case F_SETLK:
case F_SETLKW:
#ifdef F_OFD_SETLK
case F_OFD_SETLK:
#endif
#ifdef F_OFD_SETLKW
case F_OFD_SETLKW:
#endif
return 1;
default:
return 0;
}
}
static int is_lock_query(int cmd) {
switch (cmd) {
case F_GETLK:
#ifdef F_OFD_GETLK
case F_OFD_GETLK:
#endif
return 1;
default:
return 0;
}
}
int fcntl(int fd, int cmd, ...) {
static int (*real_fcntl)(int, int, ...);
va_list ap;
void *arg;
int result;
va_start(ap, cmd);
arg = va_arg(ap, void *);
va_end(ap);
if (real_fcntl == NULL) real_fcntl = dlsym(RTLD_NEXT, "fcntl");
if (real_fcntl == NULL) {
errno = ENOSYS;
return -1;
}
result = real_fcntl(fd, cmd, arg);
if (result >= 0 || !unsupported(errno)) return result;
if (is_lock_command(cmd)) return 0;
if (is_lock_query(cmd) && arg != NULL) {
((struct flock *)arg)->l_type = F_UNLCK;
return 0;
}
return result;
}
#if defined(__GLIBC__) && defined(F_SETLK64)
/* glibc's large-file build redirects both names; the runtime is 64-bit, so they are the same call. */
int fcntl64(int fd, int cmd, ...) __attribute__((alias("fcntl")));
#endif
+247
View File
@@ -0,0 +1,247 @@
/*
* Loopback socket ownership for Steam's IPC peer check.
*
* Steam validates a websocket peer by running lsof to find which process owns a 127.0.0.1 port.
* The Android sandbox denies both /proc/net/tcp and sock_diag, so no tool can answer. Every Steam
* process is preloaded with this library, so each records the ports it binds and connects, and the
* lsof Steam spawns is answered from that shared registry instead of being run.
*/
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <spawn.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <linux/netlink.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
static const char *net_dir(void) {
static char dir[256];
if (!dir[0]) {
const char *base = getenv("BL_SYSV_DIR");
snprintf(dir, sizeof(dir), "%s/net", base && *base ? base : "/dev/shm/wnsysv");
char parent[256];
snprintf(parent, sizeof(parent), "%s", dir);
char *slash = strrchr(parent, '/');
if (slash) {
*slash = '\0';
mkdir(parent, 0777);
}
mkdir(dir, 0777);
}
return dir;
}
/* Record that this process owns a loopback socket on its local port; peer is the far end's port,
* 0 for a listening socket. */
static void record_port(int fd, unsigned peer) {
struct sockaddr_storage ss;
socklen_t len = sizeof(ss);
if (getsockname(fd, (struct sockaddr *)&ss, &len) != 0) {
return;
}
unsigned port;
if (ss.ss_family == AF_INET) {
struct sockaddr_in *in = (struct sockaddr_in *)&ss;
if (in->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
return;
}
port = ntohs(in->sin_port);
} else if (ss.ss_family == AF_INET6) {
port = ntohs(((struct sockaddr_in6 *)&ss)->sin6_port);
} else {
return;
}
if (port == 0) {
return;
}
char path[300];
snprintf(path, sizeof(path), "%s/p%u", net_dir(), port);
char line[64];
int n = snprintf(line, sizeof(line), "%d %d %d %u\n", (int)getpid(), (int)getppid(), (int)getuid(), peer);
int out = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (out >= 0) {
if (write(out, line, n) != n) { /* best effort */ }
close(out);
}
}
static unsigned addr_port(const struct sockaddr *addr, socklen_t len) {
if (addr && addr->sa_family == AF_INET && len >= sizeof(struct sockaddr_in)) {
return ntohs(((const struct sockaddr_in *)addr)->sin_port);
}
if (addr && addr->sa_family == AF_INET6 && len >= sizeof(struct sockaddr_in6)) {
return ntohs(((const struct sockaddr_in6 *)addr)->sin6_port);
}
return 0;
}
static unsigned peer_port(int fd) {
struct sockaddr_storage ps;
socklen_t plen = sizeof(ps);
if (getpeername(fd, (struct sockaddr *)&ps, &plen) != 0) {
return 0;
}
return addr_port((struct sockaddr *)&ps, plen);
}
typedef int (*bind_fn)(int, const struct sockaddr *, socklen_t);
typedef int (*listen_fn)(int, int);
typedef int (*connect_fn)(int, const struct sockaddr *, socklen_t);
typedef int (*accept4_fn)(int, struct sockaddr *, socklen_t *, int);
/* udevmon.c: a stand-in for the netlink socket the sandbox refuses, bound here without the kernel. */
__attribute__((visibility("hidden"))) int bl_udevmon_stand_in(int fd);
int bind(int fd, const struct sockaddr *addr, socklen_t len) {
static bind_fn real;
if (!real) real = (bind_fn)dlsym(RTLD_NEXT, "bind");
if (bl_udevmon_stand_in(fd)) {
if (addr == NULL || len < sizeof(struct sockaddr_nl) || addr->sa_family != AF_NETLINK) {
errno = EINVAL;
return -1;
}
return 0;
}
int ret = real(fd, addr, len);
if (ret == 0) record_port(fd, 0);
return ret;
}
int listen(int fd, int backlog) {
static listen_fn real;
if (!real) real = (listen_fn)dlsym(RTLD_NEXT, "listen");
int ret = real(fd, backlog);
if (ret == 0) record_port(fd, 0);
return ret;
}
int connect(int fd, const struct sockaddr *addr, socklen_t len) {
static connect_fn real;
if (!real) real = (connect_fn)dlsym(RTLD_NEXT, "connect");
int ret = real(fd, addr, len);
if (ret == 0 || errno == EINPROGRESS) {
/* The caller goes on to test for EINPROGRESS, which the bookkeeping must not disturb. */
int saved = errno;
record_port(fd, addr_port(addr, len));
errno = saved;
}
return ret;
}
int accept4(int fd, struct sockaddr *addr, socklen_t *len, int flags) {
static accept4_fn real;
if (!real) real = (accept4_fn)dlsym(RTLD_NEXT, "accept4");
int ret = real(fd, addr, len, flags);
if (ret >= 0) record_port(ret, peer_port(ret));
return ret;
}
int accept(int fd, struct sockaddr *addr, socklen_t *len) {
return accept4(fd, addr, len, 0);
}
/* lsof would read the denied /proc/net/tcp; answer its -i TCP@host:port query from the registry
* instead of executing it. Recognised only when invoked as lsof with that query. */
static int maybe_answer_lsof(const char *path, char *const argv[]) {
const char *base = strrchr(path, '/');
base = base ? base + 1 : path;
if (strcmp(base, "lsof") != 0) {
return 0;
}
const char *port_str = NULL;
for (int i = 0; argv[i]; i++) {
const char *at = strstr(argv[i], "TCP@");
if (at) {
const char *colon = strrchr(at, ':');
if (colon) port_str = colon + 1;
}
}
if (!port_str) {
return 0;
}
char path_buf[300];
snprintf(path_buf, sizeof(path_buf), "%s/p%u", net_dir(), (unsigned)atoi(port_str));
int in = open(path_buf, O_RDONLY);
if (in < 0) {
_exit(1); /* lsof exit code for "nothing found" */
}
char buf[64];
int n = read(in, buf, sizeof(buf) - 1);
close(in);
if (n <= 0) {
_exit(1);
}
buf[n] = '\0';
int pid = 0, ppid = 0, uid = 0;
unsigned peer = 0;
sscanf(buf, "%d %d %d %u", &pid, &ppid, &uid, &peer);
/* Steam splits the address on "->" and wants exactly two halves, taking the port it asked
* about from the left one; only a listening socket has no far end. */
char out[160];
int m = peer ? snprintf(out, sizeof(out), "p%d\nR%d\nu%d\nn127.0.0.1:%u->127.0.0.1:%u\n",
pid, ppid, uid, (unsigned)atoi(port_str), peer)
: snprintf(out, sizeof(out), "p%d\nR%d\nu%d\nn127.0.0.1:%u\n",
pid, ppid, uid, (unsigned)atoi(port_str));
if (write(1, out, m) != m) { /* best effort */ }
_exit(0);
}
int execve(const char *path, char *const argv[], char *const envp[]) {
static int (*real)(const char *, char *const[], char *const[]);
if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execve");
maybe_answer_lsof(path, argv);
return real(path, argv, envp);
}
/* popen(3) and posix_spawn(3) reach lsof without going through the execve symbol above. The
* check is cheap, so a spawned lsof is answered here too by handling it in a short-lived child. */
static int answer_lsof_via_spawn(const char *path, char *const argv[], pid_t *pid) {
pid_t child = fork();
if (child < 0) {
return errno;
}
if (child == 0) {
maybe_answer_lsof(path, argv);
_exit(127);
}
if (pid) {
*pid = child;
}
return 0;
}
int execvp(const char *file, char *const argv[]) {
static int (*real)(const char *, char *const[]);
if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execvp");
maybe_answer_lsof(file, argv);
return real(file, argv);
}
int execv(const char *path, char *const argv[]) {
static int (*real)(const char *, char *const[]);
if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execv");
maybe_answer_lsof(path, argv);
return real(path, argv);
}
int posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *fa,
const posix_spawnattr_t *attr, char *const argv[], char *const envp[]) {
const char *base = strrchr(path, '/');
base = base ? base + 1 : path;
if (strcmp(base, "lsof") == 0 && !fa) {
return answer_lsof_via_spawn(path, argv, pid);
}
static int (*real)(pid_t *, const char *, const posix_spawn_file_actions_t *,
const posix_spawnattr_t *, char *const[], char *const[]);
if (!real) real = (int (*)(pid_t *, const char *, const posix_spawn_file_actions_t *,
const posix_spawnattr_t *, char *const[], char *const[]))dlsym(RTLD_NEXT, "posix_spawn");
return real(pid, path, fa, attr, argv, envp);
}
+506
View File
@@ -0,0 +1,506 @@
/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
* changed to this project's. The reasoning below is the original author's.
*
* Network interfaces for a sandbox that hides them.
*
* Android denies an app the rtnetlink dump behind getifaddrs() and if_nameindex(), the hardware
* address ioctl, and every table under /proc/net. Wine builds its adapter, address, route and
* neighbour tables from exactly those, so a game saw a machine with no adapter, no address, no
* gateway and no MAC, and one that checks its adapters before going online waited for good.
* The app knows the real link from ConnectivityManager and writes it to /etc/bannerlator-net;
* whatever the kernel refuses is answered from that file. Anything the kernel does answer wins.
*/
#define _GNU_SOURCE 1
#include <arpa/inet.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netpacket/packet.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>
#define LINK_FILE "/etc/bannerlator-net"
#define LO_NAME "lo"
#define LO_INDEX 1
#define LO_MTU 65536
#define MAX_ADDRS 16
struct link_addr {
int family;
unsigned char bytes[16];
unsigned prefix;
};
struct link {
char name[IFNAMSIZ];
unsigned index;
int mtu;
unsigned char mac[6];
struct link_addr addrs[MAX_ADDRS];
int addr_count;
struct link_addr gateways[2];
int gateway_count;
};
static int parse_addr(const char *text, struct link_addr *out) {
memset(out, 0, sizeof(*out));
if (inet_pton(AF_INET, text, out->bytes) == 1) out->family = AF_INET;
else if (inet_pton(AF_INET6, text, out->bytes) == 1) out->family = AF_INET6;
else return 0;
return 1;
}
/* Read on every call: the app rewrites the file when the device changes network. */
static int load_link(struct link *link) {
typedef FILE *(*fopen_fn)(const char *, const char *);
fopen_fn real_fopen = (fopen_fn) dlsym(RTLD_NEXT, "fopen");
FILE *f = real_fopen ? real_fopen(LINK_FILE, "re") : NULL;
if (f == NULL) return 0;
memset(link, 0, sizeof(*link));
char line[160], text[64];
unsigned m[6], prefix;
while (fgets(line, sizeof(line), f)) {
if (sscanf(line, "if %15s %u %d", link->name, &link->index, &link->mtu) == 3) continue;
if (sscanf(line, "mac %2x:%2x:%2x:%2x:%2x:%2x", m, m + 1, m + 2, m + 3, m + 4, m + 5) == 6) {
for (int i = 0; i < 6; i++) link->mac[i] = (unsigned char) m[i];
} else if (sscanf(line, "addr %63s %u", text, &prefix) == 2) {
struct link_addr addr;
if (link->addr_count < MAX_ADDRS && parse_addr(text, &addr)
&& prefix <= (addr.family == AF_INET ? 32u : 128u)) {
addr.prefix = prefix;
link->addrs[link->addr_count++] = addr;
}
} else if (sscanf(line, "gw %63s", text) == 1) {
struct link_addr addr;
if (link->gateway_count < 2 && parse_addr(text, &addr)) {
link->gateways[link->gateway_count++] = addr;
}
}
}
fclose(f);
return link->name[0] != '\0' && link->index > LO_INDEX && link->addr_count > 0;
}
static void prefix_mask(int family, unsigned prefix, unsigned char *out) {
int len = family == AF_INET ? 4 : 16;
for (int i = 0; i < len; i++, prefix -= prefix < 8 ? prefix : 8) {
out[i] = prefix >= 8 ? 0xff : (unsigned char) (0xff00 >> prefix);
}
}
static int is_link_local(const struct link_addr *addr) {
return addr->family == AF_INET6 && addr->bytes[0] == 0xfe && (addr->bytes[1] & 0xc0) == 0x80;
}
/* One allocation per list. ifa_data of the head points at the head itself, which glibc never
* does, so freeifaddrs() can tell the two kinds of list apart without keeping any state. */
struct entry {
struct ifaddrs ifa;
struct sockaddr_storage addr, mask, extra;
char name[IFNAMSIZ];
};
static struct entry *fill_entry(struct entry *e, const char *name, unsigned flags) {
snprintf(e->name, sizeof(e->name), "%s", name);
e->ifa.ifa_name = e->name;
e->ifa.ifa_flags = flags;
e->ifa.ifa_addr = (struct sockaddr *) &e->addr;
e[-1].ifa.ifa_next = &e->ifa;
return e;
}
static void fill_packet(struct entry *e, unsigned index, const unsigned char *mac) {
struct sockaddr_ll *ll = (struct sockaddr_ll *) &e->addr;
ll->sll_family = AF_PACKET;
ll->sll_ifindex = (int) index;
ll->sll_hatype = mac ? ARPHRD_ETHER : ARPHRD_LOOPBACK;
ll->sll_halen = 6;
if (mac) memcpy(ll->sll_addr, mac, 6);
}
static void fill_inet(struct entry *e, const struct link_addr *addr, unsigned index) {
unsigned char mask[16];
prefix_mask(addr->family, addr->prefix, mask);
e->ifa.ifa_netmask = (struct sockaddr *) &e->mask;
if (addr->family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *) &e->addr;
struct sockaddr_in *smask = (struct sockaddr_in *) &e->mask;
sin->sin_family = smask->sin_family = AF_INET;
memcpy(&sin->sin_addr, addr->bytes, 4);
memcpy(&smask->sin_addr, mask, 4);
if (e->ifa.ifa_flags & IFF_BROADCAST) {
struct sockaddr_in *bcast = (struct sockaddr_in *) &e->extra;
bcast->sin_family = AF_INET;
bcast->sin_addr.s_addr = sin->sin_addr.s_addr | ~smask->sin_addr.s_addr;
e->ifa.ifa_broadaddr = (struct sockaddr *) bcast;
}
} else {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &e->addr;
struct sockaddr_in6 *smask = (struct sockaddr_in6 *) &e->mask;
sin6->sin6_family = smask->sin6_family = AF_INET6;
memcpy(&sin6->sin6_addr, addr->bytes, 16);
memcpy(&smask->sin6_addr, mask, 16);
if (is_link_local(addr)) sin6->sin6_scope_id = index;
}
}
static const struct link_addr LO_V4 = { AF_INET, { 127, 0, 0, 1 }, 8 };
static const struct link_addr LO_V6 = { AF_INET6, { [15] = 1 }, 128 };
static int real_list_usable(const struct ifaddrs *list) {
for (const struct ifaddrs *e = list; e; e = e->ifa_next) {
if (e->ifa_addr && e->ifa_addr->sa_family == AF_INET && !(e->ifa_flags & IFF_LOOPBACK)) {
return 1;
}
}
return 0;
}
int getifaddrs(struct ifaddrs **out) {
typedef int (*getifaddrs_fn)(struct ifaddrs **);
typedef void (*freeifaddrs_fn)(struct ifaddrs *);
getifaddrs_fn real = (getifaddrs_fn) dlsym(RTLD_NEXT, "getifaddrs");
struct ifaddrs *list = NULL;
int rc = real(&list);
int saved = errno;
if (rc == 0 && real_list_usable(list)) {
*out = list;
return 0;
}
struct link link;
if (!load_link(&link)) {
if (rc == 0) *out = list;
errno = saved;
return rc;
}
if (rc == 0 && list) ((freeifaddrs_fn) dlsym(RTLD_NEXT, "freeifaddrs"))(list);
/* A leading slot keeps fill_entry()'s back link in bounds; the list starts after it. */
struct entry *block = calloc((size_t) link.addr_count + 5, sizeof(*block));
if (block == NULL) {
errno = ENOMEM;
return -1;
}
unsigned lo_flags = IFF_UP | IFF_RUNNING | IFF_LOOPBACK;
unsigned up_flags = IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST;
struct entry *e = block;
fill_packet(fill_entry(++e, LO_NAME, lo_flags), LO_INDEX, NULL);
fill_inet(fill_entry(++e, LO_NAME, lo_flags), &LO_V4, LO_INDEX);
fill_inet(fill_entry(++e, LO_NAME, lo_flags), &LO_V6, LO_INDEX);
fill_packet(fill_entry(++e, link.name, up_flags), link.index, link.mac);
for (int i = 0; i < link.addr_count; i++) {
fill_inet(fill_entry(++e, link.name, up_flags), &link.addrs[i], link.index);
}
block[1].ifa.ifa_data = &block[1].ifa;
*out = &block[1].ifa;
return 0;
}
void freeifaddrs(struct ifaddrs *list) {
typedef void (*freeifaddrs_fn)(struct ifaddrs *);
if (list == NULL) return;
if (list->ifa_data == list) {
free((struct entry *) list - 1);
return;
}
((freeifaddrs_fn) dlsym(RTLD_NEXT, "freeifaddrs"))(list);
}
/* Names live in the same allocation as the array, right behind it; glibc strdup()s each one. */
struct name_block {
struct if_nameindex index[3];
char names[2][IFNAMSIZ];
};
struct if_nameindex *if_nameindex(void) {
typedef struct if_nameindex *(*if_nameindex_fn)(void);
struct if_nameindex *list = ((if_nameindex_fn) dlsym(RTLD_NEXT, "if_nameindex"))();
int saved = errno;
if (list && list[0].if_index != 0) return list;
struct link link;
struct name_block *block;
if (!load_link(&link) || (block = calloc(1, sizeof(*block))) == NULL) {
errno = saved;
return list;
}
if (list) if_freenameindex(list);
snprintf(block->names[0], IFNAMSIZ, "%s", LO_NAME);
snprintf(block->names[1], IFNAMSIZ, "%s", link.name);
block->index[0] = (struct if_nameindex) { LO_INDEX, block->names[0] };
block->index[1] = (struct if_nameindex) { link.index, block->names[1] };
return block->index;
}
void if_freenameindex(struct if_nameindex *list) {
typedef void (*if_freenameindex_fn)(struct if_nameindex *);
if (list == NULL) return;
if (list[0].if_name == ((struct name_block *) list)->names[0]) {
free(list);
return;
}
((if_freenameindex_fn) dlsym(RTLD_NEXT, "if_freenameindex"))(list);
}
unsigned int if_nametoindex(const char *name) {
typedef unsigned int (*if_nametoindex_fn)(const char *);
unsigned int index = ((if_nametoindex_fn) dlsym(RTLD_NEXT, "if_nametoindex"))(name);
if (index != 0 || name == NULL) return index;
int saved = errno;
struct link link;
if (strcmp(name, LO_NAME) == 0) return LO_INDEX;
if (load_link(&link) && strcmp(name, link.name) == 0) return link.index;
errno = saved;
return 0;
}
char *if_indextoname(unsigned int index, char name[IF_NAMESIZE]) {
typedef char *(*if_indextoname_fn)(unsigned int, char *);
char *found = ((if_indextoname_fn) dlsym(RTLD_NEXT, "if_indextoname"))(index, name);
if (found || name == NULL) return found;
int saved = errno;
struct link link;
if (index == LO_INDEX) return strcpy(name, LO_NAME);
if (load_link(&link) && index == link.index) return strcpy(name, link.name);
errno = saved;
return NULL;
}
static int mac_unusable(const struct ifreq *ifr) {
const unsigned char *a = (const unsigned char *) ifr->ifr_hwaddr.sa_data;
if (ifr->ifr_hwaddr.sa_family != ARPHRD_ETHER) return 1;
/* Android hands out 02:00:00:00:00:00 where it hides the real address. */
int rest = a[1] | a[2] | a[3] | a[4] | a[5];
return (a[0] & 0x03) || (rest == 0 && (a[0] == 0 || a[0] == 0x02));
}
static const struct link_addr *first_v4(const struct link *link) {
for (int i = 0; i < link->addr_count; i++) {
if (link->addrs[i].family == AF_INET) return &link->addrs[i];
}
return NULL;
}
static int answer_ifreq(unsigned long request, struct ifreq *ifr, int real_rc) {
char name[IFNAMSIZ + 1] = { 0 };
memcpy(name, ifr->ifr_name, IFNAMSIZ);
int loopback = strcmp(name, LO_NAME) == 0;
struct link link;
if (!load_link(&link) || (!loopback && strcmp(name, link.name) != 0)) return 0;
if (request == SIOCGIFHWADDR) {
if (real_rc == 0 && (loopback || !mac_unusable(ifr))) return 0;
memset(&ifr->ifr_hwaddr, 0, sizeof(ifr->ifr_hwaddr));
ifr->ifr_hwaddr.sa_family = loopback ? ARPHRD_LOOPBACK : ARPHRD_ETHER;
if (!loopback) memcpy(ifr->ifr_hwaddr.sa_data, link.mac, 6);
return 1;
}
if (real_rc == 0) return 0;
const struct link_addr *v4 = loopback ? &LO_V4 : first_v4(&link);
struct sockaddr_in *sin = (struct sockaddr_in *) &ifr->ifr_addr;
unsigned char mask[16];
switch (request) {
case SIOCGIFFLAGS:
ifr->ifr_flags = loopback ? IFF_UP | IFF_RUNNING | IFF_LOOPBACK
: IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST;
return 1;
case SIOCGIFMTU:
ifr->ifr_mtu = loopback ? LO_MTU : link.mtu;
return 1;
case SIOCGIFINDEX:
ifr->ifr_ifindex = loopback ? LO_INDEX : (int) link.index;
return 1;
case SIOCGIFADDR:
case SIOCGIFNETMASK:
case SIOCGIFBRDADDR:
if (v4 == NULL) return 0;
prefix_mask(AF_INET, v4->prefix, mask);
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
for (int i = 0; i < 4; i++) {
unsigned char *out = (unsigned char *) &sin->sin_addr + i;
*out = request == SIOCGIFNETMASK ? mask[i]
: request == SIOCGIFADDR ? v4->bytes[i]
: (unsigned char) (v4->bytes[i] | ~mask[i]);
}
return 1;
default:
return 0;
}
}
static int is_link_query(unsigned long request) {
return request == SIOCGIFHWADDR || request == SIOCGIFFLAGS || request == SIOCGIFMTU
|| request == SIOCGIFINDEX || request == SIOCGIFADDR || request == SIOCGIFNETMASK
|| request == SIOCGIFBRDADDR;
}
/* Every DRM and evdev call comes through here, so the lookup is done once. Two threads racing
* to store it store the same pointer. */
int ioctl(int fd, unsigned long request, ...) {
typedef int (*ioctl_fn)(int, unsigned long, void *);
static ioctl_fn real;
va_list ap;
va_start(ap, request);
void *arg = va_arg(ap, void *);
va_end(ap);
if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl");
int rc = real(fd, request, arg);
if (arg == NULL || !is_link_query(request)) return rc;
int saved = errno;
if (answer_ifreq(request, (struct ifreq *) arg, rc)) return 0;
errno = saved;
return rc;
}
static void print_v6(FILE *out, const unsigned char *bytes) {
for (int i = 0; i < 16; i++) fprintf(out, "%02x", bytes[i]);
}
static unsigned v6_scope(const struct link_addr *addr) {
if (memcmp(addr, &LO_V6, sizeof(*addr)) == 0) return 0x10;
return is_link_local(addr) ? 0x20 : 0x00;
}
static void write_table(FILE *out, const char *table, const struct link *link) {
static const unsigned char zero[16];
unsigned char mask[16];
if (strcmp(table, "route") == 0) {
fprintf(out, "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT\n");
for (int i = 0; i < link->gateway_count; i++) {
if (link->gateways[i].family != AF_INET) continue;
unsigned gw;
memcpy(&gw, link->gateways[i].bytes, 4);
fprintf(out, "%s\t00000000\t%08X\t0003\t0\t0\t0\t00000000\t0\t0\t0\n", link->name, gw);
}
for (int i = 0; i < link->addr_count; i++) {
if (link->addrs[i].family != AF_INET) continue;
unsigned addr, net;
prefix_mask(AF_INET, link->addrs[i].prefix, mask);
memcpy(&addr, link->addrs[i].bytes, 4);
memcpy(&net, mask, 4);
fprintf(out, "%s\t%08X\t00000000\t0001\t0\t0\t0\t%08X\t0\t0\t0\n",
link->name, addr & net, net);
}
} else if (strcmp(table, "ipv6_route") == 0) {
for (int i = 0; i < link->gateway_count; i++) {
if (link->gateways[i].family != AF_INET6) continue;
print_v6(out, zero);
fprintf(out, " 00 ");
print_v6(out, zero);
fprintf(out, " 00 ");
print_v6(out, link->gateways[i].bytes);
fprintf(out, " 00000400 00000001 00000000 00000003 %8s\n", link->name);
}
for (int i = 0; i < link->addr_count; i++) {
if (link->addrs[i].family != AF_INET6) continue;
unsigned char net[16];
prefix_mask(AF_INET6, link->addrs[i].prefix, mask);
for (int b = 0; b < 16; b++) net[b] = link->addrs[i].bytes[b] & mask[b];
print_v6(out, net);
fprintf(out, " %02x ", link->addrs[i].prefix);
print_v6(out, zero);
fprintf(out, " 00 ");
print_v6(out, zero);
fprintf(out, " 00000100 00000001 00000000 00000001 %8s\n", link->name);
}
} else if (strcmp(table, "if_inet6") == 0) {
print_v6(out, LO_V6.bytes);
fprintf(out, " %02x %02x %02x 80 %8s\n", LO_INDEX, LO_V6.prefix, v6_scope(&LO_V6), LO_NAME);
for (int i = 0; i < link->addr_count; i++) {
if (link->addrs[i].family != AF_INET6) continue;
print_v6(out, link->addrs[i].bytes);
fprintf(out, " %02x %02x %02x 80 %8s\n", link->index, link->addrs[i].prefix,
v6_scope(&link->addrs[i]), link->name);
}
} else if (strcmp(table, "dev") == 0) {
fprintf(out, "Inter-| Receive | Transmit\n"
" face |bytes packets errs drop fifo frame compressed multicast|"
"bytes packets errs drop fifo colls carrier compressed\n");
fprintf(out, "%6s: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", LO_NAME);
fprintf(out, "%6s: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", link->name);
} else if (strcmp(table, "arp") == 0) {
fprintf(out, "IP address HW type Flags HW address Mask Device\n");
} else {
fprintf(out, "1\n");
}
}
static const char *denied_table(const char *path) {
static const char *const tables[] = { "route", "ipv6_route", "if_inet6", "dev", "arp" };
if (strncmp(path, "/proc/net/", 10) == 0) {
for (size_t i = 0; i < sizeof(tables) / sizeof(tables[0]); i++) {
if (strcmp(path + 10, tables[i]) == 0) return tables[i];
}
return NULL;
}
size_t len = strlen(path);
if (strncmp(path, "/sys/class/net/", 15) == 0 && len > 8 && strcmp(path + len - 8, "/carrier") == 0) {
return "carrier";
}
return NULL;
}
/* A memfd rather than fmemopen(): the stream owns its contents, so fclose() frees everything. */
static FILE *synthesize(const char *table) {
struct link link;
if (!load_link(&link)) return NULL;
int fd = memfd_create("bannerlator-net", MFD_CLOEXEC);
if (fd < 0) return NULL;
FILE *out = fdopen(fd, "w+");
if (out == NULL) {
close(fd);
return NULL;
}
write_table(out, table, &link);
if (fflush(out) != 0) {
fclose(out);
return NULL;
}
rewind(out);
return out;
}
/* tracer.c */
int bl_status_without_tracer(const char *path, int flags) __attribute__((visibility("hidden")));
static FILE *open_stream(const char *symbol, const char *path, const char *mode) {
typedef FILE *(*fopen_fn)(const char *, const char *);
int status = mode && mode[0] == 'r' && !strchr(mode, '+')
? bl_status_without_tracer(path, O_RDONLY | O_CLOEXEC) : -1;
if (status >= 0) {
FILE *copy = fdopen(status, "r");
if (copy == NULL) close(status);
return copy;
}
FILE *f = ((fopen_fn) dlsym(RTLD_NEXT, symbol))(path, mode);
if (f || path == NULL || mode == NULL || mode[0] != 'r') return f;
int saved = errno;
const char *table = denied_table(path);
if (table && (f = synthesize(table)) != NULL) return f;
errno = saved;
return NULL;
}
FILE *fopen(const char *path, const char *mode) {
return open_stream("fopen", path, mode);
}
FILE *fopen64(const char *path, const char *mode) {
return open_stream("fopen64", path, mode);
}
+99
View File
@@ -0,0 +1,99 @@
/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
* changed to this project's. The reasoning below is the original author's.
*
* The adjustments a session's opens need.
*
* O_NOATIME on the game library: the user's games are bound in from shared storage, which Android
* serves over FUSE with every file owned by its media provider rather than by the app. O_NOATIME
* is reserved for a file's owner, so the kernel refuses such an open outright with EPERM. The
* Steam client opens every content file it reads that way, and treats the refusal as the file
* being unreadable or missing: a validation reports corrupt or missing files and an update is
* cancelled. The flag only asks the filesystem to skip an access-time update; the open is retried
* without it. Every other refusal is passed through untouched, and a file the process may open
* with the flag still is.
*
* The kernel's trace marker: tracefs is world-writable here, so the client's tier0 opens
* /sys/kernel/tracing/trace_marker at startup - three times over, once per module that profiles -
* and writes a "[%s] tgid=%d begin_ctx=%lld" line for every scope it enters. Nothing in the
* session reads those markers, and they are one more thing that can land in the wrong file when
* a descriptor number changes hands (tracer.c has the cause). They have been found appended to an
* app manifest, which the client then cannot parse, to a download's state file, and to content
* chunks, which it quarantines as corrupt. Refused the descriptor, tier0 writes nothing at all and
* has nothing to lose.
*
* A process status is served without the container's tracer in it; see tracer.c.
*/
#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 bl_status_without_tracer(const char *path, int flags) __attribute__((visibility("hidden")));
static int retry_without_noatime(int fd, int flags) {
return fd < 0 && errno == EPERM && (flags & O_NOATIME);
}
/* Both spellings tier0 tries, the debugfs one first. */
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);
}
int open(const char *path, int flags, ...) {
static int (*real_open)(const char *, int, ...);
va_list ap;
mode_t mode;
int fd;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
if (is_trace_marker(path)) {
errno = EACCES;
return -1;
}
if ((fd = bl_status_without_tracer(path, flags)) >= 0) return fd;
if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open");
if (real_open == NULL) {
errno = ENOSYS;
return -1;
}
fd = real_open(path, flags, mode);
if (retry_without_noatime(fd, flags)) fd = real_open(path, flags & ~O_NOATIME, mode);
return fd;
}
int openat(int dirfd, const char *path, int flags, ...) {
static int (*real_openat)(int, const char *, int, ...);
va_list ap;
mode_t mode;
int fd;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
if (is_trace_marker(path)) {
errno = EACCES;
return -1;
}
if ((fd = bl_status_without_tracer(path, flags)) >= 0) return fd;
if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat");
if (real_openat == NULL) {
errno = ENOSYS;
return -1;
}
fd = real_openat(dirfd, path, flags, mode);
if (retry_without_noatime(fd, flags)) fd = real_openat(dirfd, path, flags & ~O_NOATIME, mode);
return fd;
}
/* The runtime is 64-bit, so the large-file names are the same calls. */
int open64(const char *path, int flags, ...) __attribute__((alias("open")));
int openat64(int dirfd, const char *path, int flags, ...) __attribute__((alias("openat")));
+79
View File
@@ -0,0 +1,79 @@
/*
* get_robust_list(2) for the session's own threads.
*
* Android's app policy refuses the call, and Steam's cross-process mutex insists on seeing the
* head glibc registered. The kernel's list protocol gives it away: a robust mutex this thread
* holds is linked directly behind the registered head, so locking a private one and following
* the link back finds the head without depending on libc's layout.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <linux/futex.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <sys/uio.h>
#include <unistd.h>
static long (*real_syscall)(long, ...);
static struct robust_list_head *thread_robust_head(void) {
pthread_mutexattr_t attr;
pthread_mutex_t mutex;
struct robust_list_head *head = NULL;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
pthread_mutex_init(&mutex, &attr);
pthread_mutexattr_destroy(&attr);
if (pthread_mutex_lock(&mutex) != 0) {
return NULL;
}
/* The mutex holds tids and lock words beside the link, so a word is only followed
* through a read that fails instead of faulting on a non-pointer. */
uintptr_t lo = (uintptr_t)&mutex, hi = lo + sizeof(mutex);
for (uintptr_t p = lo; p + sizeof(void *) <= hi && !head; p += sizeof(void *)) {
struct robust_list_head *candidate = *(struct robust_list_head **)p;
struct robust_list_head copy;
struct iovec local = {&copy, sizeof(copy)}, remote = {candidate, sizeof(copy)};
if ((uintptr_t)candidate & (sizeof(void *) - 1)) {
continue;
}
if (process_vm_readv(getpid(), &local, 1, &remote, 1, 0) != (ssize_t)sizeof(copy)) {
continue;
}
uintptr_t entry = (uintptr_t)copy.list.next;
if (entry >= lo && entry < hi && copy.futex_offset < 0 && copy.futex_offset > -256) {
head = candidate;
}
}
pthread_mutex_unlock(&mutex);
pthread_mutex_destroy(&mutex);
return head;
}
long syscall(long number, ...) {
va_list ap;
long args[6];
va_start(ap, number);
for (int i = 0; i < 6; i++) {
args[i] = va_arg(ap, long);
}
va_end(ap);
if (number == SYS_get_robust_list && (args[0] == 0 || args[0] == gettid())) {
struct robust_list_head *head = thread_robust_head();
if (!head) {
errno = ENOSYS;
return -1;
}
*(struct robust_list_head **)args[1] = head;
*(size_t *)args[2] = sizeof(*head);
return 0;
}
if (!real_syscall) {
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
}
return real_syscall(number, args[0], args[1], args[2], args[3], args[4], args[5]);
}
+411
View File
@@ -0,0 +1,411 @@
/* System V semaphores and shared memory for programs in the Linux runtime.
*
* Android kernels are built without System V IPC, so semget/shmget fail with ENOSYS. Steam's tier0
* creates its client-instance semaphore with semget and gives up when that fails. This library is
* LD_PRELOADed into the session and keeps each object in a file under /dev/shm (a bound directory
* the whole session shares): a semaphore set is an array of counters waited on with futexes, a
* segment is a plain file that shmat maps. Objects are found by the inode of their file, which is
* what the id is. semop applies its operations one by one (no all-or-nothing across a set) and
* SEM_UNDO is accepted but not performed - enough for the client, which uses single-counter sets.
* Message queues stay unsupported. */
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/futex.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#define DIR_DEFAULT "/dev/shm/wnsysv"
#define SEM_MAGIC 0x574e5345u /* "WNSE" */
#define MAX_OBJECTS 64
struct sem_header {
uint32_t magic;
uint32_t nsems;
uint32_t key;
uint32_t mode;
int32_t creator_pid;
int32_t pad[11];
};
/* The counters follow the header, one 32-bit word each (a futex word). */
struct sem_map { int id; struct sem_header *hdr; size_t len; };
struct shm_map { void *addr; size_t len; int id; };
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
static struct sem_map g_sems[MAX_OBJECTS];
static struct shm_map g_shms[MAX_OBJECTS];
static char g_dir[PATH_MAX - NAME_MAX - 2];
static const char *object_dir(void) {
if (!g_dir[0]) {
const char *d = getenv("BL_SYSV_DIR");
snprintf(g_dir, sizeof(g_dir), "%s", d && *d ? d : DIR_DEFAULT);
mkdir(g_dir, 0700);
}
return g_dir;
}
static int object_id(int fd) {
struct stat st;
if (fstat(fd, &st) != 0) return -1;
return (int)(st.st_ino & 0x7fffffff);
}
/* Open the object file of kind ("sem"/"shm") for key, creating it as flags ask. */
static int open_object(const char *kind, key_t key, int flags, int *created) {
char path[PATH_MAX];
int oflags = O_RDWR | O_CLOEXEC;
*created = 0;
if (key == IPC_PRIVATE) {
static atomic_uint serial;
snprintf(path, sizeof(path), "%s/%s.p%d.%u", object_dir(), kind, (int)getpid(),
atomic_fetch_add(&serial, 1));
oflags |= O_CREAT | O_EXCL;
} else {
snprintf(path, sizeof(path), "%s/%s.k%08x", object_dir(), kind, (unsigned)key);
if (flags & IPC_CREAT) oflags |= O_CREAT;
if ((flags & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) oflags |= O_EXCL;
}
int fd = open(path, oflags, (flags & 0777) ? (flags & 0777) : 0600);
if (fd < 0) return -1;
struct stat st;
if (fstat(fd, &st) == 0 && st.st_size == 0) *created = 1;
return fd;
}
/* The object file for an id, found by inode. */
static int open_by_id(const char *kind, int id) {
DIR *d = opendir(object_dir());
if (!d) return -1;
struct dirent *e;
int fd = -1;
size_t klen = strlen(kind);
while ((e = readdir(d))) {
if (strncmp(e->d_name, kind, klen) || e->d_name[klen] != '.') continue;
if ((int)(e->d_ino & 0x7fffffff) != id) continue;
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", g_dir, e->d_name);
fd = open(path, O_RDWR | O_CLOEXEC);
break;
}
closedir(d);
if (fd < 0) errno = EINVAL;
return fd;
}
static int unlink_by_id(const char *kind, int id) {
DIR *d = opendir(object_dir());
if (!d) return -1;
struct dirent *e;
int rc = -1;
size_t klen = strlen(kind);
while ((e = readdir(d))) {
if (strncmp(e->d_name, kind, klen) || e->d_name[klen] != '.') continue;
if ((int)(e->d_ino & 0x7fffffff) != id) continue;
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", g_dir, e->d_name);
rc = unlink(path);
break;
}
closedir(d);
if (rc < 0) errno = EINVAL;
return rc;
}
static long futex(void *addr, int op, int val, const struct timespec *timeout) {
return syscall(SYS_futex, addr, op, val, timeout, NULL, 0);
}
/* ------------------------------------------------------------------ semaphores */
static struct sem_map *sem_lookup(int id) {
for (int i = 0; i < MAX_OBJECTS; i++)
if (g_sems[i].hdr && g_sems[i].id == id) return &g_sems[i];
int fd = open_by_id("sem", id);
if (fd < 0) return NULL;
struct stat st;
if (fstat(fd, &st) != 0 || st.st_size < (off_t)sizeof(struct sem_header)) { close(fd); errno = EINVAL; return NULL; }
void *m = mmap(NULL, (size_t)st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (m == MAP_FAILED) return NULL;
struct sem_header *h = m;
if (h->magic != SEM_MAGIC) { munmap(m, (size_t)st.st_size); errno = EINVAL; return NULL; }
for (int i = 0; i < MAX_OBJECTS; i++) {
if (g_sems[i].hdr) continue;
g_sems[i].id = id; g_sems[i].hdr = h; g_sems[i].len = (size_t)st.st_size;
return &g_sems[i];
}
munmap(m, (size_t)st.st_size);
errno = ENOSPC;
return NULL;
}
static void sem_forget(int id) {
for (int i = 0; i < MAX_OBJECTS; i++)
if (g_sems[i].hdr && g_sems[i].id == id) { munmap(g_sems[i].hdr, g_sems[i].len); g_sems[i].hdr = NULL; }
}
static _Atomic int32_t *sem_words(struct sem_header *h) { return (_Atomic int32_t *)(h + 1); }
int semget(key_t key, int nsems, int semflg) {
if (nsems < 0 || nsems > 250) { errno = EINVAL; return -1; }
int created;
int fd = open_object("sem", key, semflg, &created);
if (fd < 0) return -1;
if (created) {
if (nsems == 0) { close(fd); errno = EINVAL; return -1; }
size_t len = sizeof(struct sem_header) + (size_t)nsems * sizeof(int32_t);
if (ftruncate(fd, (off_t)len) != 0) { close(fd); return -1; }
struct sem_header h = {.magic = SEM_MAGIC, .nsems = (uint32_t)nsems, .key = (uint32_t)key,
.mode = (uint32_t)(semflg & 0777), .creator_pid = (int32_t)getpid()};
if (pwrite(fd, &h, sizeof(h), 0) != (ssize_t)sizeof(h)) { close(fd); return -1; }
} else {
struct sem_header h;
if (pread(fd, &h, sizeof(h), 0) != (ssize_t)sizeof(h) || h.magic != SEM_MAGIC) { close(fd); errno = EINVAL; return -1; }
if (nsems > 0 && (uint32_t)nsems > h.nsems) { close(fd); errno = EINVAL; return -1; }
}
int id = object_id(fd);
close(fd);
return id;
}
static int sem_wait_op(_Atomic int32_t *word, short op, short flg, const struct timespec *timeout) {
for (;;) {
int32_t v = atomic_load(word);
if (op == 0) {
if (v == 0) return 0;
} else if (v >= -op) {
if (atomic_compare_exchange_weak(word, &v, v + op)) return 0;
continue;
}
if (flg & IPC_NOWAIT) { errno = EAGAIN; return -1; }
long r = futex(word, FUTEX_WAIT, v, timeout);
if (r != 0 && errno == ETIMEDOUT) return -1;
if (r != 0 && errno == EINTR) { errno = EINTR; return -1; }
}
}
int semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timespec *timeout) {
pthread_mutex_lock(&g_lock);
struct sem_map *m = sem_lookup(semid);
pthread_mutex_unlock(&g_lock);
if (!m) return -1;
struct sem_header *h = m->hdr;
_Atomic int32_t *words = sem_words(h);
for (size_t i = 0; i < nsops; i++) {
if (sops[i].sem_num >= h->nsems) { errno = EFBIG; return -1; }
_Atomic int32_t *w = &words[sops[i].sem_num];
if (sops[i].sem_op > 0) {
atomic_fetch_add(w, sops[i].sem_op);
futex(w, FUTEX_WAKE, INT_MAX, NULL);
} else if (sem_wait_op(w, sops[i].sem_op, sops[i].sem_flg, timeout) != 0) {
return -1;
}
if (sops[i].sem_op < 0) futex(w, FUTEX_WAKE, INT_MAX, NULL); /* zero-waiters see the change */
}
return 0;
}
int semop(int semid, struct sembuf *sops, size_t nsops) { return semtimedop(semid, sops, nsops, NULL); }
int semctl(int semid, int semnum, int cmd, ...) {
union { int val; struct semid_ds *buf; unsigned short *array; } arg = {0};
if (cmd == SETVAL || cmd == IPC_STAT || cmd == IPC_SET || cmd == GETALL || cmd == SETALL) {
va_list ap;
va_start(ap, cmd);
arg.buf = va_arg(ap, struct semid_ds *);
va_end(ap);
}
if (cmd == IPC_RMID) {
pthread_mutex_lock(&g_lock);
sem_forget(semid);
int rc = unlink_by_id("sem", semid);
pthread_mutex_unlock(&g_lock);
return rc;
}
pthread_mutex_lock(&g_lock);
struct sem_map *m = sem_lookup(semid);
pthread_mutex_unlock(&g_lock);
if (!m) return -1;
struct sem_header *h = m->hdr;
_Atomic int32_t *words = sem_words(h);
switch (cmd) {
case GETVAL:
if (semnum < 0 || (uint32_t)semnum >= h->nsems) { errno = EINVAL; return -1; }
return atomic_load(&words[semnum]);
case SETVAL:
if (semnum < 0 || (uint32_t)semnum >= h->nsems) { errno = EINVAL; return -1; }
atomic_store(&words[semnum], (int32_t)(intptr_t)arg.buf);
futex(&words[semnum], FUTEX_WAKE, INT_MAX, NULL);
return 0;
case GETALL:
for (uint32_t i = 0; i < h->nsems; i++) arg.array[i] = (unsigned short)atomic_load(&words[i]);
return 0;
case SETALL:
for (uint32_t i = 0; i < h->nsems; i++) {
atomic_store(&words[i], arg.array[i]);
futex(&words[i], FUTEX_WAKE, INT_MAX, NULL);
}
return 0;
case GETPID: return h->creator_pid;
case GETNCNT: case GETZCNT: return 0;
case IPC_STAT:
memset(arg.buf, 0, sizeof(*arg.buf));
arg.buf->sem_perm.__key = (key_t)h->key;
arg.buf->sem_perm.uid = arg.buf->sem_perm.cuid = getuid();
arg.buf->sem_perm.gid = arg.buf->sem_perm.cgid = getgid();
arg.buf->sem_perm.mode = (unsigned short)h->mode;
arg.buf->sem_nsems = h->nsems;
return 0;
case IPC_SET: return 0;
default: errno = EINVAL; return -1;
}
}
/* ------------------------------------------------------------------ shared memory */
/* Linux keeps a removed segment attachable until the last detach, which X clients rely on: they
* remove the segment right after asking the server to attach it. A removed segment is renamed out
* of key lookup instead, and removed for good once it has been out of use for a while. */
static int remove_shm(int id) {
DIR *d = opendir(object_dir());
if (!d) return -1;
struct dirent *e;
int rc = -1;
while ((e = readdir(d))) {
if (strncmp(e->d_name, "shm.", 4) || !strncmp(e->d_name, "shm.d.", 6)) continue;
if ((int)(e->d_ino & 0x7fffffff) != id) continue;
char from[PATH_MAX], to[PATH_MAX];
snprintf(from, sizeof(from), "%s/%s", g_dir, e->d_name);
snprintf(to, sizeof(to), "%s/shm.d.%s", g_dir, e->d_name + 4);
rc = rename(from, to);
break;
}
closedir(d);
if (rc < 0) errno = EINVAL;
return rc;
}
static void expire_removed_shm(void) {
DIR *d = opendir(object_dir());
if (!d) return;
struct dirent *e;
time_t now = time(NULL);
while ((e = readdir(d))) {
if (strncmp(e->d_name, "shm.d.", 6)) continue;
char path[PATH_MAX];
struct stat st;
snprintf(path, sizeof(path), "%s/%s", g_dir, e->d_name);
if (stat(path, &st) == 0 && now - st.st_mtime > 30) unlink(path);
}
closedir(d);
}
int shmget(key_t key, size_t size, int shmflg) {
int created;
expire_removed_shm();
int fd = open_object("shm", key, shmflg, &created);
if (fd < 0) return -1;
if (created) {
if (size == 0) { close(fd); errno = EINVAL; return -1; }
if (ftruncate(fd, (off_t)size) != 0) { close(fd); return -1; }
} else {
struct stat st;
if (fstat(fd, &st) != 0 || (size_t)st.st_size < size) { close(fd); errno = EINVAL; return -1; }
}
int id = object_id(fd);
close(fd);
return id;
}
void *shmat(int shmid, const void *shmaddr, int shmflg) {
int fd = open_by_id("shm", shmid);
if (fd < 0) return (void *)-1;
struct stat st;
if (fstat(fd, &st) != 0) { close(fd); return (void *)-1; }
int prot = PROT_READ | ((shmflg & SHM_RDONLY) ? 0 : PROT_WRITE);
void *m = mmap((void *)shmaddr, (size_t)st.st_size, prot, MAP_SHARED, fd, 0);
close(fd);
if (m == MAP_FAILED) return (void *)-1;
pthread_mutex_lock(&g_lock);
for (int i = 0; i < MAX_OBJECTS; i++) {
if (g_shms[i].addr) continue;
g_shms[i].addr = m; g_shms[i].len = (size_t)st.st_size; g_shms[i].id = shmid;
break;
}
pthread_mutex_unlock(&g_lock);
return m;
}
int shmdt(const void *shmaddr) {
pthread_mutex_lock(&g_lock);
for (int i = 0; i < MAX_OBJECTS; i++) {
if (g_shms[i].addr != shmaddr) continue;
munmap(g_shms[i].addr, g_shms[i].len);
g_shms[i].addr = NULL;
pthread_mutex_unlock(&g_lock);
return 0;
}
pthread_mutex_unlock(&g_lock);
errno = EINVAL;
return -1;
}
int shmctl(int shmid, int cmd, struct shmid_ds *buf) {
switch (cmd) {
case IPC_RMID: {
pthread_mutex_lock(&g_lock);
int rc = remove_shm(shmid);
pthread_mutex_unlock(&g_lock);
return rc;
}
case IPC_STAT: {
int fd = open_by_id("shm", shmid);
if (fd < 0) return -1;
struct stat st;
int rc = fstat(fd, &st);
close(fd);
if (rc != 0) return -1;
memset(buf, 0, sizeof(*buf));
buf->shm_perm.uid = buf->shm_perm.cuid = getuid();
buf->shm_perm.gid = buf->shm_perm.cgid = getgid();
buf->shm_perm.mode = 0600;
buf->shm_segsz = (size_t)st.st_size;
buf->shm_nattch = 1;
buf->shm_cpid = (pid_t)getpid();
return 0;
}
case IPC_SET: case SHM_LOCK: case SHM_UNLOCK: return 0;
default: errno = EINVAL; return -1;
}
}
/*
* fork() carries over only the calling thread, so a lock another thread was holding at that
* instant stays held in the child by a thread that is not there to release it. Taking it before
* the fork makes the copy consistent; the parent then unlocks it and the child, whose one thread
* never locked it, gets a fresh one. (WinNative 79aa7f68.)
*/
static void sysv_lock_before_fork(void) { pthread_mutex_lock(&g_lock); }
static void sysv_unlock_after_fork(void) { pthread_mutex_unlock(&g_lock); }
static void sysv_reset_after_fork(void) { pthread_mutex_init(&g_lock, NULL); }
__attribute__((constructor)) static void install_sysv_fork_handlers(void) {
pthread_atfork(sysv_lock_before_fork, sysv_unlock_after_fork, sysv_reset_after_fork);
}
+103
View File
@@ -0,0 +1,103 @@
/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
* changed to this project's. The reasoning below is the original author's.
*
* A process status without the container's tracer in it.
*
* proot runs the session under ptrace, so every process here reads a TracerPid in its
* /proc/<pid>/status. The Steam client takes that to mean a debugger is attached, and its arm64
* build breaks into the supposed debugger with a bare "svc #0" after every failed assertion. That
* is no breakpoint: the registers still hold the call Plat_IsInDebugSession() just made, close(),
* with its own result, 1, as the argument - so each assertion closes descriptor 1. The number then
* goes to whatever the client opens next, and its standard output is written into that: app
* manifests and content chunks, its own connections, and the socket a game's Steam API talks
* over, which is how a game that stayed silent while loading found its pipe dead.
*
* The tracer is part of the container, not something a program should react to, so the status
* files are served with TracerPid 0.
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#define STATUS_MAX 16384
#define TRACER_FIELD "\nTracerPid:\t"
static const char *skip_digits(const char *s) {
const char *end = s;
while (isdigit((unsigned char)*end)) end++;
return end == s ? NULL : end;
}
/* /proc/{self,thread-self,<pid>}[/task/<tid>]/status */
static int is_status_path(const char *path) {
const char *rest;
if (path == NULL || strncmp(path, "/proc/", 6) != 0) return 0;
path += 6;
if (strncmp(path, "self/", 5) == 0) rest = path + 4;
else if (strncmp(path, "thread-self/", 12) == 0) rest = path + 11;
else if ((rest = skip_digits(path)) == NULL) return 0;
if (strncmp(rest, "/task/", 6) == 0 && (rest = skip_digits(rest + 6)) == NULL) return 0;
return strcmp(rest, "/status") == 0;
}
/* Chromium stands its hang watchdogs down when it sees a tracer, and the client's web helper does
* not come up within their patience here: it is left reading the truth. */
static int is_web_helper(void) {
return strcmp(program_invocation_short_name, "steamwebhelper") == 0;
}
static int write_all(int fd, const char *data, size_t len) {
while (len > 0) {
ssize_t n = write(fd, data, len);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) return -1;
data += n;
len -= (size_t)n;
}
return 0;
}
/* The descriptor to hand back for a read-only open of a status file, or -1 with errno untouched
* when the path is something else or the copy cannot be made, and the real open should run. */
__attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *path, int flags) {
char text[STATUS_MAX];
size_t len = 0;
int saved = errno;
int out = -1;
if ((flags & O_ACCMODE) != O_RDONLY || !is_status_path(path) || is_web_helper()) return -1;
int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
if (in < 0) goto done;
for (;;) {
ssize_t n = read(in, text + len, sizeof(text) - 1 - len);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) break;
len += (size_t)n;
}
close(in);
text[len] = '\0';
char *field = strstr(text, TRACER_FIELD);
if (field == NULL) goto done;
char *value = field + strlen(TRACER_FIELD);
char *line_end = strchr(value, '\n');
if (line_end == NULL) goto done;
out = memfd_create("status", (flags & O_CLOEXEC) ? MFD_CLOEXEC : 0);
if (out < 0) goto done;
if (write_all(out, text, (size_t)(value - text)) != 0 || write_all(out, "0", 1) != 0
|| write_all(out, line_end, len - (size_t)(line_end - text)) != 0
|| lseek(out, 0, SEEK_SET) != 0) {
close(out);
out = -1;
}
done:
errno = saved;
return out;
}
+191
View File
@@ -0,0 +1,191 @@
/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with the
* symbol prefix changed to match this library. The reasoning below is the original author's.
*
* udev's netlink monitor, stood in for where the sandbox refuses the socket it is built on.
*
* The app sandbox denies a NETLINK_KOBJECT_UEVENT socket outright. libudev cannot create a
* monitor without one, and SDL's HID layer cannot start without a monitor: every SDL_hid_init()
* fails, the Steam client's controller code retries it hundreds of times a second, its main loop
* stalls, and the client asserts out from under the running game. Refusing SDL the library
* altogether (the earlier answer here) is what produced that failure.
*
* So the socket is answered instead: one end of a datagram socketpair stands in for the netlink
* socket, its peer held open so it never reads as hung up, and the few calls libudev makes on
* the socket that only a netlink one could answer are answered here. Nothing is ever written to
* it, so the monitor is live and silent, which is the most the kernel would deliver in a
* container anyway. The real socket is tried first, so this stays out of the way wherever a
* monitor could genuinely be created.
*
* Wine is left without one. Its HID bus reads the session's pads itself only when its SDL bus
* cannot start, and that is the path inputudev.c describes them on. Given a monitor the SDL bus
* comes up and takes the pads over from it.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/netlink.h>
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
struct stand_in {
int fd; /* the descriptor handed out, -1 when the slot is free */
int peer; /* the other end, kept open */
ino_t inode; /* identifies the socket after fd is closed and its number reused */
};
static struct stand_in stand_ins[16];
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static void lock_before_fork(void) { pthread_mutex_lock(&lock); }
static void unlock_after_fork(void) { pthread_mutex_unlock(&lock); }
static void reset_after_fork(void) { pthread_mutex_init(&lock, NULL); }
__attribute__((constructor)) static void init_udevmon(void) {
for (size_t i = 0; i < sizeof(stand_ins) / sizeof(stand_ins[0]); i++)
stand_ins[i].fd = -1;
pthread_atfork(lock_before_fork, unlock_after_fork, reset_after_fork);
}
static ino_t socket_inode(int fd) {
struct stat st;
return fstat(fd, &st) == 0 ? st.st_ino : 0;
}
/* Whether fd is one of the stand-in sockets. A slot whose descriptor was closed and reused by
* something else is retired on sight, so a later socket with the same number is not mistaken. */
static int is_stand_in(int fd) {
int found = 0;
if (fd < 0)
return 0;
pthread_mutex_lock(&lock);
for (size_t i = 0; i < sizeof(stand_ins) / sizeof(stand_ins[0]); i++) {
if (stand_ins[i].fd != fd)
continue;
if (stand_ins[i].inode == socket_inode(fd)) {
found = 1;
} else {
close(stand_ins[i].peer);
stand_ins[i].fd = -1;
}
break;
}
pthread_mutex_unlock(&lock);
return found;
}
static int is_wine_process(void) {
static int known;
if (known == 0) {
char exe[PATH_MAX];
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
const char *name = exe;
if (n > 0) {
exe[n] = '\0';
name = strrchr(exe, '/');
name = name != NULL ? name + 1 : exe;
}
known = n > 0 && strncmp(name, "wine", 4) == 0 ? 1 : -1;
}
return known == 1;
}
static int make_stand_in(int type) {
int sv[2];
int saved = errno;
int flags = SOCK_DGRAM | (type & (SOCK_CLOEXEC | SOCK_NONBLOCK));
if (socketpair(AF_UNIX, flags, 0, sv) != 0) {
errno = saved;
return -1;
}
/* The peer is ours alone: it must not follow the socket into a child. */
fcntl(sv[1], F_SETFD, FD_CLOEXEC);
pthread_mutex_lock(&lock);
for (size_t i = 0; i < sizeof(stand_ins) / sizeof(stand_ins[0]); i++) {
if (stand_ins[i].fd == -1) {
stand_ins[i].fd = sv[0];
stand_ins[i].peer = sv[1];
stand_ins[i].inode = socket_inode(sv[0]);
pthread_mutex_unlock(&lock);
return sv[0];
}
}
pthread_mutex_unlock(&lock);
close(sv[0]);
close(sv[1]);
errno = saved;
return -1;
}
int socket(int domain, int type, int protocol) {
static int (*real_socket)(int, int, int);
int fd;
if (real_socket == NULL) real_socket = dlsym(RTLD_NEXT, "socket");
if (real_socket == NULL) {
errno = ENOSYS;
return -1;
}
fd = real_socket(domain, type, protocol);
if (fd >= 0 || domain != AF_NETLINK || protocol != NETLINK_KOBJECT_UEVENT)
return fd;
int refused = errno;
if (is_wine_process()) {
errno = refused;
return -1;
}
return make_stand_in(type);
}
/* For net.c's bind(): whether fd is a stand-in, which accepts any netlink address as bound. */
__attribute__((visibility("hidden"))) int bl_udevmon_stand_in(int fd) {
return is_stand_in(fd);
}
int getsockname(int fd, struct sockaddr *addr, socklen_t *len) {
static int (*real_getsockname)(int, struct sockaddr *, socklen_t *);
struct sockaddr_nl nl;
if (is_stand_in(fd)) {
if (addr == NULL || len == NULL) {
errno = EFAULT;
return -1;
}
memset(&nl, 0, sizeof(nl));
nl.nl_family = AF_NETLINK;
nl.nl_pid = (unsigned)getpid();
memcpy(addr, &nl, *len < sizeof(nl) ? *len : sizeof(nl));
*len = sizeof(nl);
return 0;
}
if (real_getsockname == NULL) real_getsockname = dlsym(RTLD_NEXT, "getsockname");
if (real_getsockname == NULL) {
errno = ENOSYS;
return -1;
}
return real_getsockname(fd, addr, len);
}
int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) {
static int (*real_setsockopt)(int, int, int, const void *, socklen_t);
if (is_stand_in(fd)) {
/* Group membership and packet filters only mean something on a socket that receives; this
* one never does, so they are accepted as set. The rest is genuine on a unix socket. */
if (level == SOL_NETLINK)
return 0;
if (level == SOL_SOCKET && (optname == SO_ATTACH_FILTER || optname == SO_DETACH_FILTER))
return 0;
}
if (real_setsockopt == NULL) real_setsockopt = dlsym(RTLD_NEXT, "setsockopt");
if (real_setsockopt == NULL) {
errno = ENOSYS;
return -1;
}
return real_setsockopt(fd, level, optname, optval, optlen);
}