Files
DroidDeck/tools/linuxfs/preload/lock.c
T
Kurt Himebauchanduubooster-team 6a5722c6d1 fix(preload): answer NETLINK_ROUTE queries inside a session (#2141)
* fix(preload): answer NETLINK_ROUTE queries inside a session

ip(8) could not run in a session: the app's SELinux domain has no
netlink_route_socket bind permission, so iproute2 failed at bind and never saw
the link that getifaddrs() and /proc/net already expose.

Serve NETLINK_ROUTE from the same etc/droiddeck-net snapshot: an AF_UNIX
socketpair backs the netlink fd, link/addr/route dumps and route get are
synthesised, writes return EPERM, and multicast subscriptions keep failing
with EACCES so consumers that wait for link events keep their fallback.

Replies carry the port id reported by getsockname() and recvmsg() rewrites
the source address to AF_NETLINK/pid 0. iproute2's dump loop requires both
before it accepts a message or NLMSG_DONE; when either is missing every
message is skipped silently and the process blocks in recvmsg().

* fix(preload): keep socket() and send() semantics for emulated netlink

* fix(preload): deliver netlink replies with the caller's blocking mode

* fix(preload): preserve NETLINK_ROUTE protocol and fd semantics

- Send ACKs for successful queries and honor NETLINK_CAP_ACK
- Reject unsupported subscriptions and apply socket timeouts
- Track socket identity and shared references across dup and close
- Support kernel connect and rewrite recvfrom source addresses
- Resolve local and connected routes before default gateways
- Add netlink and iproute2 regression tests

* fix(preload): handle netlink request limits and socket lifecycle

- Collect large sendmsg requests without falling back to UNIX sockets
- Reject oversized replies explicitly instead of returning partial results
- Allocate unique port IDs and reject duplicate bindings
- Honor socket flags and restore inherited sockets across exec
- Validate socket types and report netlink socket identity
- Replace the fixed socket table with a dynamic registry
- Add regression tests for batching, port conflicts and exec inheritance

* fix(preload): optimize netlink tracking and support link name queries

- Add lock-free fd tracking to bypass netlink locks on ordinary sockets
- Move descriptor restoration to initialization and explicit discovery paths
- Support RTM_GETLINK lookups via IFLA_IFNAME with correct error codes
- Add regression coverage for hot-path overhead and name-based queries

* fix(preload): accept unterminated netlink interface names

- Copy IFLA_IFNAME into a bounded, NUL-terminated buffer before lookup
- Match kernel string length validation and ERANGE errors
- Add regression coverage for unterminated names and boundary cases

* fix(preload): pin peers for netlink replies

* fix(preload): make autobind idempotent

* fix(preload): isolate forked netlink cleanup

* fix(preload): avoid interposed fd scans

* perf(preload): skip PID checks for untracked fds

* fix(preload): verify peer before replay

---------

Co-authored-by: uubooster-team <uu.booster@service.netease.com>
2026-11-05 18:27:08 -05:00

92 lines
2.1 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 <sys/file.h>
#include <unistd.h>
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;
}
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 bl_nl_before_dup(int fd, void **state) __attribute__((visibility("hidden")));
void bl_nl_after_dup(void *state, int fd) __attribute__((visibility("hidden")));
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;
}
void *nl_state = NULL;
int duplicate = cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC;
if (duplicate && bl_nl_before_dup(fd, &nl_state) != 0) return -1;
result = real_fcntl(fd, cmd, arg);
if (duplicate) bl_nl_after_dup(nl_state, result);
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)
int fcntl64(int fd, int cmd, ...) __attribute__((alias("fcntl")));
#endif