From ff5e4c7ac76166bcc266a22eeca00b013a8b2c71 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Sun, 22 Nov 2026 13:37:44 -0500 Subject: [PATCH] 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. --- tools/linuxfs/preload/netif.c | 133 ++++++++++++++++++++++------ tools/linuxfs/preload/opens.c | 5 +- tools/tests/test_netlink_preload.py | 129 ++++++++++++++++++++++++++- 3 files changed, 235 insertions(+), 32 deletions(-) diff --git a/tools/linuxfs/preload/netif.c b/tools/linuxfs/preload/netif.c index 5be0dde22..04100065f 100644 --- a/tools/linuxfs/preload/netif.c +++ b/tools/linuxfs/preload/netif.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -520,6 +521,13 @@ static void write_table(FILE *out, const char *table, const struct link *link) { 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 if (strcmp(table, "tcp") == 0 || strcmp(table, "udp") == 0) { + fprintf(out, " sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode%s\n", + table[0] == 'u' ? " ref pointer drops" : ""); + } else if (strcmp(table, "tcp6") == 0 || strcmp(table, "udp6") == 0) { + fprintf(out, " sl local_address remote_address " + "st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode%s\n", + table[0] == 'u' ? " ref pointer drops" : ""); } else { fprintf(out, "1\n"); } @@ -527,7 +535,10 @@ static void write_table(FILE *out, const char *table, const struct link *link) { #define CARRIER_TABLE 5 -static const char *const net_tables[] = { "route", "ipv6_route", "if_inet6", "dev", "arp", "carrier" }; +#define SOCKET_TABLES 6 + +static const char *const net_tables[] = { "route", "ipv6_route", "if_inet6", "dev", "arp", "carrier", + "tcp", "tcp6", "udp", "udp6" }; static long long net_denied_at[sizeof(net_tables) / sizeof(net_tables[0])]; #define CARRIER_SLOTS 8 @@ -564,8 +575,8 @@ static void remember_denial(int table, const char *path) { static int denied_table(const char *path) { if (strncmp(path, "/proc/net/", 10) == 0) { - for (int i = 0; i < CARRIER_TABLE; i++) { - if (strcmp(path + 10, net_tables[i]) == 0) return i; + for (int i = 0; i < (int) (sizeof(net_tables) / sizeof(net_tables[0])); i++) { + if (i != CARRIER_TABLE && strcmp(path + 10, net_tables[i]) == 0) return i; } return -1; } @@ -576,9 +587,9 @@ static int denied_table(const char *path) { return -1; } -static FILE *synthesize(const char *table) { +static FILE *synthesize(int table) { struct link link; - if (!load_link(&link)) return NULL; + if (!load_link(&link) && table < SOCKET_TABLES) return NULL; int fd = memfd_create("droiddeck-net", MFD_CLOEXEC); if (fd < 0) return NULL; FILE *out = fdopen(fd, "w+"); @@ -586,7 +597,7 @@ static FILE *synthesize(const char *table) { close(fd); return NULL; } - write_table(out, table, &link); + write_table(out, net_tables[table], &link); if (fflush(out) != 0) { fclose(out); return NULL; @@ -610,11 +621,11 @@ static FILE *open_stream(fopen_fn real, const char *path, const char *mode, cons path = dd_oom_adj_redirect(path, mode && (mode[0] == 'w' || mode[0] == 'a' || strchr(mode, '+'))); int table = path && mode && mode[0] == 'r' ? denied_table(path) : -1; FILE *f; - if (table >= 0 && denied_recently(table, path) && (f = synthesize(net_tables[table])) != NULL) return f; + if (table >= 0 && denied_recently(table, path) && (f = synthesize(table)) != NULL) return f; f = real(path, mode); if (f || table < 0) return f; int saved = errno; - if ((f = synthesize(net_tables[table])) != NULL) { + if ((f = synthesize(table)) != NULL) { if (saved == EACCES) remember_denial(table, path); return f; } @@ -622,6 +633,30 @@ static FILE *open_stream(fopen_fn real, const char *path, const char *mode, cons return NULL; } +int dd_net_open(const char *path, int flags, int fd) __attribute__((visibility("hidden"))); + +int dd_net_open(const char *path, int flags, int fd) { + if (fd >= 0 || path == NULL || (flags & O_ACCMODE) != O_RDONLY || (flags & O_DIRECTORY)) return fd; + int table = denied_table(path); + if (table < 0) return fd; + int saved = errno; + FILE *f = synthesize(table); + if (f == NULL) { + errno = saved; + return fd; + } + char self[32]; + snprintf(self, sizeof(self), "/proc/self/fd/%d", fileno(f)); + int copy = open(self, O_RDONLY | (flags & O_CLOEXEC)); + fclose(f); + if (copy < 0) { + errno = saved; + return fd; + } + if (saved == EACCES) remember_denial(table, path); + return copy; +} + FILE *fopen(const char *path, const char *mode) { static fopen_fn real; if (real == NULL) real = (fopen_fn) dlsym(RTLD_NEXT, "fopen"); @@ -664,6 +699,7 @@ struct nl_socket { unsigned references; int type; int cap_ack; + uint64_t groups; struct nl_socket *next; }; @@ -685,13 +721,14 @@ struct nl_fd_page { struct nl_metadata { uint64_t nonce; + uint64_t groups; uint32_t port; uint32_t type; uint32_t cap_ack; char magic[12]; }; -_Static_assert(sizeof(struct nl_metadata) == 32, "netlink socket metadata ABI"); +_Static_assert(sizeof(struct nl_metadata) == 40, "netlink socket metadata ABI"); struct nl_packet { struct nl_packet *next; @@ -705,9 +742,9 @@ struct nl_reply { int overflow; }; -static const char nl_client_magic[12] = "dd-nl-c-v1"; -static const char nl_peer_magic[12] = "dd-nl-p-v1"; -static const char nl_reservation_magic[12] = "dd-nl-r-v1"; +static const char nl_client_magic[12] = "dd-nl-c-v2"; +static const char nl_peer_magic[12] = "dd-nl-p-v2"; +static const char nl_reservation_magic[12] = "dd-nl-r-v2"; static struct nl_socket *nl_sockets; static struct nl_fd *nl_fds; static struct nl_fd_page *nl_fd_pages[NL_FD_BUCKETS]; @@ -910,13 +947,13 @@ static int nl_save_packets(int fd, struct nl_packet **packets) { } } -static int nl_replace_peer(int fd, struct nl_socket *socket, unsigned port, int capped) { +static int nl_replace_peer(int fd, struct nl_socket *socket, unsigned port, int capped, uint64_t groups) { static int (*real_socket)(int, int, int); static int (*real_close)(int); if (real_socket == NULL) real_socket = (int (*)(int, int, int)) dlsym(RTLD_NEXT, "socket"); if (real_close == NULL) real_close = (int (*)(int)) dlsym(RTLD_NEXT, "close"); struct sockaddr_un client_addr, peer_addr; - struct nl_metadata client, peer = { .port = port, .type = socket->type, .cap_ack = capped }; + struct nl_metadata client, peer = { .groups = groups, .port = port, .type = socket->type, .cap_ack = capped }; if (!nl_address(fd, 0, &client_addr, &client, nl_client_magic)) { errno = EINVAL; return -1; @@ -955,6 +992,7 @@ static int nl_replace_peer(int fd, struct nl_socket *socket, unsigned port, int socket->peer_inode = status.st_ino; socket->port = port; socket->cap_ack = capped; + socket->groups = groups; errno = saved; return replayed; failed: { @@ -1025,7 +1063,7 @@ static struct nl_fd *nl_restore(int fd) { socket->device = status.st_dev; socket->inode = status.st_ino; socket->type = peer.type; - if (nl_replace_peer(fd, socket, peer.port, peer.cap_ack) != 0) { + if (nl_replace_peer(fd, socket, peer.port, peer.cap_ack, peer.groups) != 0) { free(socket); free(entry); return NULL; @@ -1159,7 +1197,7 @@ static int nl_peer_of(int fd) { if (nl_address(fd, 1, &addr, &metadata, nl_peer_magic)) { if ((metadata.nonce == socket->peer_inode && nl_identity(socket->peer, socket->device, socket->peer_inode)) - || nl_replace_peer(fd, socket, metadata.port, metadata.cap_ack) == 0) { + || nl_replace_peer(fd, socket, metadata.port, metadata.cap_ack, metadata.groups) == 0) { if (socket->port != 0 && (socket->reservation < 0 || !nl_identity(socket->reservation, socket->device, socket->reservation_inode))) nl_reserve_port(socket, socket->port); @@ -1317,7 +1355,7 @@ static int nl_bind_port(int fd, unsigned requested, int automatic) { } if (other == NULL) { if (nl_reserve_port(socket, port) == 0) { - result = nl_replace_peer(fd, socket, port, socket->cap_ack); + result = nl_replace_peer(fd, socket, port, socket->cap_ack, socket->groups); if (result != 0) nl_close_reservation(socket); break; } @@ -1342,6 +1380,23 @@ static int nl_autobind(int fd) { return nl_bind_port(fd, 0, 1); } +static int nl_set_groups(int fd, uint64_t keep, uint64_t add) { + int result = 0; + pthread_mutex_lock(&nl_sockets_lock); + struct nl_fd *entry = nl_find(fd); + if (entry) { + struct nl_socket *socket = entry->socket; + struct sockaddr_un addr; + struct nl_metadata metadata; + uint64_t current = nl_address(fd, 1, &addr, &metadata, nl_peer_magic) ? metadata.groups : socket->groups; + uint64_t groups = (current & keep) | add; + if (groups != current || groups != socket->groups) + result = nl_replace_peer(fd, socket, socket->port, socket->cap_ack, groups); + } + pthread_mutex_unlock(&nl_sockets_lock); + return result; +} + int dd_nl_bind(int fd, const struct sockaddr *addr, socklen_t len, int *rc) { const struct sockaddr_nl *nl; if (addr && len >= sizeof(sa_family_t) && addr->sa_family == AF_NETLINK) nl_discover(fd); @@ -1352,15 +1407,39 @@ int dd_nl_bind(int fd, const struct sockaddr *addr, socklen_t len, int *rc) { return 1; } nl = (const struct sockaddr_nl *) addr; - if (nl->nl_groups != 0) { - errno = EACCES; - *rc = -1; - return 1; - } *rc = nl_bind_port(fd, nl->nl_pid, 0); + if (*rc == 0) *rc = nl_set_groups(fd, ~(uint64_t) UINT32_MAX, nl->nl_groups); return 1; } +static unsigned nl_groups_of(int fd) { + struct sockaddr_un addr; + struct nl_metadata metadata; + if (nl_address(fd, 1, &addr, &metadata, nl_peer_magic)) return (unsigned) metadata.groups; + unsigned groups = 0; + pthread_mutex_lock(&nl_sockets_lock); + struct nl_fd *entry = nl_find(fd); + if (entry) groups = (unsigned) entry->socket->groups; + pthread_mutex_unlock(&nl_sockets_lock); + return groups; +} + +static int nl_membership(int fd, int option, const void *value, socklen_t len) { + unsigned group; + if (value == NULL || len < sizeof(group)) { + errno = value == NULL ? EFAULT : EINVAL; + return -1; + } + memcpy(&group, value, sizeof(group)); + if (group == 0 || group > RTNLGRP_MAX) { + errno = EINVAL; + return -1; + } + if (group > 64) return 0; + uint64_t bit = (uint64_t) 1 << (group - 1); + return nl_set_groups(fd, ~bit, option == NETLINK_ADD_MEMBERSHIP ? bit : 0); +} + int dd_nl_getsockname(int fd, struct sockaddr *addr, socklen_t *len, int *rc) { struct sockaddr_nl nl; socklen_t size; @@ -1373,6 +1452,7 @@ int dd_nl_getsockname(int fd, struct sockaddr *addr, socklen_t *len, int *rc) { memset(&nl, 0, sizeof(nl)); nl.nl_family = AF_NETLINK; nl.nl_pid = nl_port_of(fd); + nl.nl_groups = nl_groups_of(fd); size = *len < sizeof(nl) ? *len : (socklen_t) sizeof(nl); memcpy(addr, &nl, size); *len = sizeof(nl); @@ -1517,9 +1597,10 @@ int dd_nl_setsockopt(int fd, int level, int option, const void *value, socklen_t *rc = real(peer, level, option, value, len); return 1; } - if (option != NETLINK_CAP_ACK && option != NETLINK_EXT_ACK) { - errno = option == NETLINK_ADD_MEMBERSHIP || option == NETLINK_DROP_MEMBERSHIP - ? EACCES : ENOPROTOOPT; + if (option == NETLINK_ADD_MEMBERSHIP || option == NETLINK_DROP_MEMBERSHIP) { + *rc = nl_membership(fd, option, value, len); + } else if (option != NETLINK_CAP_ACK && option != NETLINK_EXT_ACK) { + errno = ENOPROTOOPT; *rc = -1; } else if (value == NULL || len < sizeof(int)) { errno = value == NULL ? EFAULT : EINVAL; @@ -1530,7 +1611,7 @@ int dd_nl_setsockopt(int fd, int level, int option, const void *value, socklen_t memcpy(&enabled, value, sizeof(enabled)); pthread_mutex_lock(&nl_sockets_lock); struct nl_fd *entry = nl_find(fd); - if (entry && nl_replace_peer(fd, entry->socket, entry->socket->port, enabled != 0) != 0) { + if (entry && nl_replace_peer(fd, entry->socket, entry->socket->port, enabled != 0, entry->socket->groups) != 0) { pthread_mutex_unlock(&nl_sockets_lock); *rc = -1; return 1; diff --git a/tools/linuxfs/preload/opens.c b/tools/linuxfs/preload/opens.c index c9df092b4..5ab362e93 100644 --- a/tools/linuxfs/preload/opens.c +++ b/tools/linuxfs/preload/opens.c @@ -17,6 +17,7 @@ int dd_ntsync_open(const char *path, int flags) __attribute__((visibility("hidde 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"))); @@ -62,7 +63,7 @@ static int open_from(const char *path, int flags, mode_t mode, const void *calle 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 fd; + return dd_net_open(path, flags, fd); } static int openat_from(int dirfd, const char *path, int flags, mode_t mode, const void *caller) { @@ -85,7 +86,7 @@ static int openat_from(int dirfd, const char *path, int flags, mode_t mode, cons 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 fd; + return dd_net_open(path, flags, fd); } int open(const char *path, int flags, ...) { diff --git a/tools/tests/test_netlink_preload.py b/tools/tests/test_netlink_preload.py index 7c47c971f..8a6dc7952 100644 --- a/tools/tests/test_netlink_preload.py +++ b/tools/tests/test_netlink_preload.py @@ -12,7 +12,10 @@ PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" SHIM = r""" #define _GNU_SOURCE #include +#include +#include #include +#include #include #include #include @@ -92,6 +95,33 @@ FILE *fopen(const char *path, const char *mode) { return real(path, mode); } +static int denied(const char *path) { + const char *prefix = getenv("NETLINK_TEST_DENIED"); + if (!prefix || !path || strncmp(path, prefix, strlen(prefix))) return 0; + errno = EACCES; + return 1; +} + +int open(const char *path, int flags, ...) { + static int (*real)(const char *, int, ...); + if (!real) real = dlsym(RTLD_NEXT, "open"); + va_list ap; + va_start(ap, flags); + mode_t mode = va_arg(ap, mode_t); + va_end(ap); + return denied(path) ? -1 : real(path, flags, mode); +} + +int openat(int dirfd, const char *path, int flags, ...) { + static int (*real)(int, const char *, int, ...); + if (!real) real = dlsym(RTLD_NEXT, "openat"); + va_list ap; + va_start(ap, flags); + mode_t mode = va_arg(ap, mode_t); + va_end(ap); + return denied(path) ? -1 : real(dirfd, path, flags, mode); +} + int close_range(unsigned int first, unsigned int last, int flags) { return syscall(SYS_close_range, (long)first, (long)last, (long)flags, 0L, 0L, 0L); } @@ -137,6 +167,35 @@ static int route_socket(void) { return descriptor; } +static void proc_net_test(int denied) { + static const char *const tables[] = {"/proc/net/tcp", "/proc/net/tcp6", "/proc/net/udp", "/proc/net/udp6"}; + char text[512], target[128], link[64]; + for (unsigned i = 0; i < sizeof(tables) / sizeof(tables[0]); i++) { + int descriptor = i % 2 ? openat(AT_FDCWD, tables[i], O_RDONLY | O_CLOEXEC) : open(tables[i], O_RDONLY | O_CLOEXEC); + require(descriptor >= 0, "socket table opens"); + require(fcntl(descriptor, F_GETFD) & FD_CLOEXEC, "socket table keeps close-on-exec"); + snprintf(link, sizeof(link), "/proc/self/fd/%d", descriptor); + ssize_t length = readlink(link, target, sizeof(target) - 1); + require(length > 0, "socket table target"); + target[length] = 0; + require((strncmp(target, "/memfd:droiddeck-net", 20) == 0) == denied, "synthesized only when denied"); + if (denied) { + require((fcntl(descriptor, F_GETFL) & O_ACCMODE) == O_RDONLY, "socket table is read-only"); + require(write(descriptor, "x", 1) < 0 && errno == EBADF, "socket table rejects writes"); + ssize_t total = read(descriptor, text, sizeof(text) - 1); + require(total > 0, "socket table read"); + text[total] = 0; + require(strstr(text, "local_address") && strchr(text, '\n') == text + total - 1, "header without sockets"); + } + close(descriptor); + } + if (denied) { + require(open("/proc/net/tcp", O_WRONLY) < 0 && errno == EACCES, "socket table writes stay denied"); + require(open("/proc/net/netstat", O_RDONLY) < 0 && errno == EACCES, "other tables stay denied"); + require(open("/proc/net/tcp", O_RDONLY | O_DIRECTORY) < 0 && errno == EACCES, "directory opens stay denied"); + } +} + static unsigned port_of(int descriptor) { struct sockaddr_nl address; socklen_t length = sizeof(address); @@ -609,6 +668,13 @@ static void batch_test(int oversized) { close(descriptor); } +static unsigned groups_of(int descriptor) { + struct sockaddr_nl address; + socklen_t length = sizeof(address); + require(getsockname(descriptor, (struct sockaddr *)&address, &length) == 0, "getsockname"); + return address.nl_groups; +} + static void inherited_test(int argc, char **argv) { require(argc == 6, "inherited arguments"); int descriptor = atoi(argv[2]), closed = atoi(argv[4]), datagram = atoi(argv[5]); @@ -616,6 +682,7 @@ static void inherited_test(int argc, char **argv) { require(fcntl(closed, F_GETFD) == -1 && errno == EBADF, "cloexec descriptor closed"); require(port_of(descriptor) == port && !(fcntl(descriptor, F_GETFD) & FD_CLOEXEC), "inherited bound port"); + require(groups_of(descriptor) == RTMGRP_LINK, "membership survives exec"); socket_info(descriptor, SOCK_RAW); socket_info(datagram, SOCK_DGRAM); require(fcntl(datagram, F_GETFL) & O_NONBLOCK, "inherited nonblocking flag"); @@ -640,6 +707,9 @@ static void exec_test(const char *program, int forked) { int enabled = 1; require(setsockopt(descriptor, SOL_NETLINK, NETLINK_CAP_ACK, &enabled, sizeof(enabled)) == 0, "inherited cap ack"); + int group = RTNLGRP_LINK; + require(setsockopt(descriptor, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) == 0, + "inherited membership"); unsigned port = port_of(descriptor); int alias = fcntl(descriptor, F_DUPFD, 100); int closed = fcntl(descriptor, F_DUPFD_CLOEXEC, 200); @@ -718,8 +788,8 @@ static void ack_test(const char *mode) { static void options_test(void) { int descriptor = route_socket(); int group = RTNLGRP_LINK; - require(setsockopt(descriptor, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) == -1 - && errno == EACCES, "membership denied"); + require(setsockopt(descriptor, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) == 0, + "membership accepted"); require(setsockopt(descriptor, SOL_NETLINK, NETLINK_PKTINFO, &group, sizeof(group)) == -1 && errno == ENOPROTOOPT, "unsupported option"); require(setsockopt(descriptor, SOL_NETLINK, NETLINK_EXT_ACK, &group, sizeof(group)) == 0, "ext ack"); @@ -735,6 +805,44 @@ static void options_test(void) { close(descriptor); } +static void subscribe_test(void) { + unsigned wanted = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR; + int descriptor = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); + struct sockaddr_nl address = {.nl_family = AF_NETLINK, .nl_groups = wanted}; + require(descriptor >= 0, "socket"); + require(bind(descriptor, (struct sockaddr *)&address, sizeof(address)) == 0, "bind with groups"); + require(groups_of(descriptor) == wanted, "bound groups"); + int group = RTNLGRP_IPV4_ROUTE; + require(setsockopt(descriptor, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) == 0, + "add membership"); + require(groups_of(descriptor) == (wanted | RTMGRP_IPV4_ROUTE), "added group"); + group = RTNLGRP_LINK; + require(setsockopt(descriptor, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, &group, sizeof(group)) == 0, + "drop membership"); + require(groups_of(descriptor) == ((wanted & ~RTMGRP_LINK) | RTMGRP_IPV4_ROUTE), "dropped group"); + pid_t child = fork(); + require(child >= 0, "membership fork"); + if (child == 0) { + group = RTNLGRP_IPV4_ROUTE; + _exit(setsockopt(descriptor, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, &group, sizeof(group)) == 0 + && groups_of(descriptor) == (wanted & ~RTMGRP_LINK) ? 0 : 1); + } + int status; + require(waitpid(child, &status, 0) == child && WIFEXITED(status) && WEXITSTATUS(status) == 0, + "membership child"); + require(groups_of(descriptor) == (wanted & ~RTMGRP_LINK), "membership shared after fork"); + group = 0; + require(setsockopt(descriptor, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) == -1 + && errno == EINVAL, "group zero refused"); + struct timeval timeout = {.tv_usec = 30000}; + require(setsockopt(descriptor, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == 0, "receive timeout"); + unsigned char buffer[4096]; + require(recv(descriptor, buffer, sizeof(buffer), 0) == -1 + && (errno == EAGAIN || errno == EWOULDBLOCK), "no unsolicited events"); + usable(descriptor); + close(descriptor); +} + static void dump_test(void) { int descriptor = route_socket(); send_link(descriptor, 0, NLM_F_DUMP | NLM_F_ACK); @@ -1040,6 +1148,7 @@ int main(int argc, char **argv) { if (!strcmp(argv[1], "ack") || !strcmp(argv[1], "error") || !strcmp(argv[1], "capped")) ack_test(argv[1]); else if (!strcmp(argv[1], "options")) options_test(); + else if (!strcmp(argv[1], "subscribe")) subscribe_test(); else if (!strcmp(argv[1], "dump")) dump_test(); else if (!strcmp(argv[1], "send_timeout")) send_timeout_test(0); else if (!strcmp(argv[1], "nonblocking")) send_timeout_test(1); @@ -1082,6 +1191,7 @@ int main(int argc, char **argv) { } else if (!strcmp(argv[1], "range") || !strcmp(argv[1], "cloexec") || !strcmp(argv[1], "closefrom")) close_range_test(argv[1]); + else if (!strcmp(argv[1], "proc_net")) proc_net_test(argc > 2 && !strcmp(argv[2], "denied")); else if (!strcmp(argv[1], "route")) { require(argc >= 3, "route destination"); route_test(argv[2]); @@ -1129,10 +1239,12 @@ class NetlinkPreloadTest(unittest.TestCase): def tearDownClass(cls): cls.temp.cleanup() - def run_program(self, mode, *arguments, snapshot=None): + def run_program(self, mode, *arguments, snapshot=None, denied=None): env = dict(os.environ, LD_PRELOAD=f"{self.preload}:{self.shim}", NETLINK_TEST_SNAPSHOT=str(snapshot or self.snapshot), DD_SYSV_DIR=str(self.directory / "sysv")) + if denied: + env["NETLINK_TEST_DENIED"] = denied result = subprocess.run([str(self.program), mode, *arguments], env=env, capture_output=True, text=True, timeout=5) self.assertEqual(0, result.returncode, result.stdout + result.stderr) @@ -1207,9 +1319,12 @@ class NetlinkPreloadTest(unittest.TestCase): def test_cloexec_sockets_release_their_ports_after_exec(self): self.run_program("all_cloexec") - def test_subscription_is_denied_and_receive_timeout_is_applied(self): + def test_membership_is_accepted_and_receive_timeout_is_applied(self): self.run_program("options") + def test_change_listeners_subscribe_and_still_query(self): + self.run_program("subscribe") + def test_dump_keeps_multipart_completion(self): self.run_program("dump") @@ -1249,6 +1364,12 @@ class NetlinkPreloadTest(unittest.TestCase): with self.subTest(mode=mode): self.run_program(mode) + def test_denied_socket_tables_open_as_empty_tables(self): + self.run_program("proc_net", "denied", denied="/proc/net/") + + def test_readable_socket_tables_stay_real(self): + self.run_program("proc_net") + def test_loopback_and_own_addresses_are_local_routes(self): for destination, source in (("127.0.0.2", "127.0.0.1"), ("::1", "::1"), ("192.168.1.10", "192.168.1.10"), ("2001:db8::10", "2001:db8::10")):