// https://syzkaller.appspot.com/bug?id=1c59ebeffd6a9db2fd0b731eea830cf87c4eea21 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef NLA_ALIGNTO #define NLA_ALIGNTO 4 #endif #ifndef NLA_ALIGN #define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1)) #endif #ifndef NLA_HDRLEN #define NLA_HDRLEN ((int)NLA_ALIGN(sizeof(struct nlattr))) #endif #define NUM_CFS_HOGS 10 #define NUM_CHILD_NETNS 8 #define MAIN_DPS_COUNT 400 #define MAIN_FLOWS_PER_DP 200 #define CHILD_DPS_COUNT 10 #define CHILD_FLOWS_PER_DP 10 #define MAX_CHILDREN 64 static pid_t g_parent_pid; static pid_t g_children[MAX_CHILDREN]; static int g_num_children = 0; static void cleanup_children(void) { if (getpid() != g_parent_pid) return; for (int i = 0; i < g_num_children; i++) { if (g_children[i] > 0) { kill(g_children[i], SIGKILL); waitpid(g_children[i], NULL, 0); g_children[i] = 0; } } } static void on_alarm(int sig) { (void)sig; printf("[*] Alarm triggered, cleaning up children and exiting...\n"); cleanup_children(); _exit(0); } static inline void *nla_put(void *buf, uint16_t type, size_t len, const void *data) { struct nlattr *nla = (struct nlattr *)buf; nla->nla_type = type; nla->nla_len = NLA_HDRLEN + len; if (len && data) memcpy((char *)nla + NLA_HDRLEN, data, len); return (char *)buf + NLA_ALIGN(nla->nla_len); } static int write_file(const char *path, const char *data) { int fd = open(path, O_WRONLY); if (fd < 0) { printf("[-] Failed to open %s: %s\n", path, strerror(errno)); return -1; } size_t len = strlen(data); ssize_t n = write(fd, data, len); if (n < 0 || (size_t)n != len) { printf("[-] Failed to write to %s: %s\n", path, strerror(errno)); close(fd); return -1; } int res = close(fd); if (res < 0) { printf("[-] Failed to close %s: %s\n", path, strerror(errno)); return -1; } return 0; } static int setup_netns(void) { uid_t uid = getuid(); gid_t gid = getgid(); if (unshare(CLONE_NEWNET) == 0) { printf("[+] unshare(CLONE_NEWNET) successful.\n"); return 0; } printf("[*] unshare(CLONE_NEWNET) failed (%s), trying CLONE_NEWUSER | CLONE_NEWNET...\n", strerror(errno)); if (unshare(CLONE_NEWUSER | CLONE_NEWNET) != 0) { printf("[-] unshare(CLONE_NEWUSER | CLONE_NEWNET) failed: %s\n", strerror(errno)); return -1; } if (write_file("/proc/self/setgroups", "deny") < 0) return -1; char map_buf[64]; snprintf(map_buf, sizeof(map_buf), "0 %u 1\n", (unsigned int)uid); if (write_file("/proc/self/uid_map", map_buf) < 0) return -1; snprintf(map_buf, sizeof(map_buf), "0 %u 1\n", (unsigned int)gid); if (write_file("/proc/self/gid_map", map_buf) < 0) return -1; printf("[+] unshare(CLONE_NEWUSER | CLONE_NEWNET) with ID mappings successful.\n"); return 0; } static int pin_to_cpu(int cpu) { cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(cpu, &cpuset); int res = sched_setaffinity(0, sizeof(cpuset), &cpuset); if (res < 0) { printf("[-] Failed to pin to CPU %d: %s\n", cpu, strerror(errno)); return -1; } printf("[+] Pinned to CPU %d successfully.\n", cpu); return 0; } static int get_family_id(int nl_fd, const char *name) { struct { struct nlmsghdr n; struct genlmsghdr g; char buf[256]; } req; memset(&req, 0, sizeof(req)); req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); req.n.nlmsg_type = GENL_ID_CTRL; req.n.nlmsg_flags = NLM_F_REQUEST; req.n.nlmsg_seq = 1; req.g.cmd = CTRL_CMD_GETFAMILY; req.g.version = 1; size_t name_len = strlen(name) + 1; struct nlattr *attr = (struct nlattr *)((char *)&req + req.n.nlmsg_len); attr->nla_type = CTRL_ATTR_FAMILY_NAME; attr->nla_len = NLA_HDRLEN + name_len; memcpy((char *)attr + NLA_HDRLEN, name, name_len); req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len + attr->nla_len); ssize_t s = send(nl_fd, &req, req.n.nlmsg_len, 0); if (s < 0) { printf("[-] Failed to send CTRL_CMD_GETFAMILY for %s: %s\n", name, strerror(errno)); return -1; } char buf[4096]; ssize_t r = recv(nl_fd, buf, sizeof(buf), 0); if (r < 0) { printf("[-] Failed to receive reply for CTRL_CMD_GETFAMILY: %s\n", strerror(errno)); return -1; } struct nlmsghdr *nlh = (struct nlmsghdr *)buf; if (!NLMSG_OK(nlh, (size_t)r)) { printf("[-] Invalid Netlink message received for family lookup.\n"); return -1; } if (nlh->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(nlh); printf("[-] Netlink error in family lookup for %s: %d (%s)\n", name, err->error, strerror(-err->error)); return -1; } struct genlmsghdr *gh = (struct genlmsghdr *)NLMSG_DATA(nlh); struct nlattr *nla = (struct nlattr *)((char *)gh + GENL_HDRLEN); int remaining = nlh->nlmsg_len - NLMSG_LENGTH(GENL_HDRLEN); while (remaining >= (int)sizeof(struct nlattr)) { if (nla->nla_len < sizeof(struct nlattr) || nla->nla_len > remaining) break; if (nla->nla_type == CTRL_ATTR_FAMILY_ID) { uint16_t fid = *(uint16_t *)((char *)nla + NLA_HDRLEN); printf("[+] Discovered family '%s' ID: %u\n", name, (unsigned int)fid); return (int)fid; } int aligned = NLA_ALIGN(nla->nla_len); remaining -= aligned; nla = (struct nlattr *)((char *)nla + aligned); } printf("[-] Family ID attribute for %s not found in reply.\n", name); return -1; } static int create_datapath(int nl_fd, int family_id, const char *dp_name) { static uint32_t seq = 2; struct { struct nlmsghdr n; struct genlmsghdr g; struct ovs_header o; char buf[256]; } req; memset(&req, 0, sizeof(req)); req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN + sizeof(struct ovs_header)); req.n.nlmsg_type = (uint16_t)family_id; req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ECHO; req.n.nlmsg_seq = ++seq; req.g.cmd = OVS_DP_CMD_NEW; req.g.version = OVS_DATAPATH_VERSION; req.o.dp_ifindex = 0; size_t name_len = strlen(dp_name) + 1; struct nlattr *attr = (struct nlattr *)((char *)&req + req.n.nlmsg_len); attr->nla_type = OVS_DP_ATTR_NAME; attr->nla_len = NLA_HDRLEN + name_len; memcpy((char *)attr + NLA_HDRLEN, dp_name, name_len); req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len + attr->nla_len); attr = (struct nlattr *)((char *)&req + req.n.nlmsg_len); attr->nla_type = OVS_DP_ATTR_UPCALL_PID; attr->nla_len = NLA_HDRLEN + sizeof(uint32_t); *(uint32_t *)((char *)attr + NLA_HDRLEN) = 0; req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len + attr->nla_len); ssize_t s = send(nl_fd, &req, req.n.nlmsg_len, 0); if (s < 0) { printf("[-] Failed to send OVS_DP_CMD_NEW for %s: %s\n", dp_name, strerror(errno)); return -1; } char buf[4096]; ssize_t ret = recv(nl_fd, buf, sizeof(buf), 0); if (ret < 0) { printf("[-] Failed to recv reply for OVS_DP_CMD_NEW (%s): %s\n", dp_name, strerror(errno)); return -1; } struct nlmsghdr *nlh = (struct nlmsghdr *)buf; if (!NLMSG_OK(nlh, (size_t)ret)) { printf("[-] Invalid Netlink reply received for OVS_DP_CMD_NEW (%s)\n", dp_name); return -1; } if (nlh->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(nlh); if (err->error < 0) { printf("[-] OVS_DP_CMD_NEW (%s) Netlink error: %d (%s)\n", dp_name, err->error, strerror(-err->error)); return -1; } return 0; } if (nlh->nlmsg_type == (uint16_t)family_id) { struct genlmsghdr *gh = (struct genlmsghdr *)NLMSG_DATA(nlh); struct ovs_header *oh = (struct ovs_header *)((char *)gh + GENL_HDRLEN); return oh->dp_ifindex; } printf("[-] OVS_DP_CMD_NEW (%s) unexpected msg type: %u\n", dp_name, nlh->nlmsg_type); return -1; } /* Fast pre-built flow template for high-speed insertion */ static char g_flow_buf[1024]; static size_t g_flow_len = 0; static struct ovs_header *g_flow_hdr = NULL; static struct ovs_key_ipv4 *g_key_ipv4 = NULL; static struct ovs_key_ipv4 *g_mask_ipv4 = NULL; static void init_flow_template(int flow_family_id) { memset(g_flow_buf, 0, sizeof(g_flow_buf)); struct nlmsghdr *n = (struct nlmsghdr *)g_flow_buf; n->nlmsg_type = (uint16_t)flow_family_id; n->nlmsg_flags = NLM_F_REQUEST; n->nlmsg_seq = 10000; struct genlmsghdr *g = (struct genlmsghdr *)NLMSG_DATA(n); g->cmd = OVS_FLOW_CMD_NEW; g->version = OVS_FLOW_VERSION; g_flow_hdr = (struct ovs_header *)((char *)g + GENL_HDRLEN); g_flow_hdr->dp_ifindex = 0; char *ptr = (char *)g_flow_hdr + sizeof(struct ovs_header); /* OVS_FLOW_ATTR_KEY (nested) */ char *key_start = ptr; struct nlattr *nla_key = (struct nlattr *)key_start; nla_key->nla_type = OVS_FLOW_ATTR_KEY; ptr += NLA_HDRLEN; /* OVS_KEY_ATTR_ETHERNET inside key */ struct ovs_key_ethernet eth_key; memset(ð_key, 0, sizeof(eth_key)); eth_key.eth_dst[0] = 0x00; eth_key.eth_dst[1] = 0x11; eth_key.eth_dst[2] = 0x22; eth_key.eth_dst[3] = 0x33; eth_key.eth_dst[4] = 0x44; eth_key.eth_dst[5] = 0x66; eth_key.eth_src[0] = 0x00; eth_key.eth_src[1] = 0x11; eth_key.eth_src[2] = 0x22; eth_key.eth_src[3] = 0x33; eth_key.eth_src[4] = 0x44; eth_key.eth_src[5] = 0x55; ptr = (char *)nla_put(ptr, OVS_KEY_ATTR_ETHERNET, sizeof(eth_key), ð_key); /* OVS_KEY_ATTR_ETHERTYPE inside key */ uint16_t eth_type = htons(ETH_P_IP); ptr = (char *)nla_put(ptr, OVS_KEY_ATTR_ETHERTYPE, sizeof(eth_type), ð_type); /* OVS_KEY_ATTR_IPV4 inside key */ struct ovs_key_ipv4 ip_key; memset(&ip_key, 0, sizeof(ip_key)); ip_key.ipv4_src = htonl(0x0a000000); ip_key.ipv4_dst = htonl(0x0a010000); ip_key.ipv4_proto = 0; ip_key.ipv4_ttl = 64; g_key_ipv4 = (struct ovs_key_ipv4 *)(ptr + NLA_HDRLEN); ptr = (char *)nla_put(ptr, OVS_KEY_ATTR_IPV4, sizeof(ip_key), &ip_key); nla_key->nla_len = (uint16_t)(ptr - key_start); ptr = key_start + NLA_ALIGN(nla_key->nla_len); /* OVS_FLOW_ATTR_MASK (nested) */ char *mask_start = ptr; struct nlattr *nla_mask = (struct nlattr *)mask_start; nla_mask->nla_type = OVS_FLOW_ATTR_MASK; ptr += NLA_HDRLEN; /* OVS_KEY_ATTR_ETHERNET inside mask */ struct ovs_key_ethernet eth_mask; memset(ð_mask, 0xff, sizeof(eth_mask)); ptr = (char *)nla_put(ptr, OVS_KEY_ATTR_ETHERNET, sizeof(eth_mask), ð_mask); /* OVS_KEY_ATTR_ETHERTYPE inside mask */ uint16_t eth_type_mask = 0xffff; ptr = (char *)nla_put(ptr, OVS_KEY_ATTR_ETHERTYPE, sizeof(eth_type_mask), ð_type_mask); /* OVS_KEY_ATTR_IPV4 inside mask */ struct ovs_key_ipv4 ip_mask; memset(&ip_mask, 0, sizeof(ip_mask)); ip_mask.ipv4_src = htonl(0xffffffff); ip_mask.ipv4_dst = htonl(0xffffffff); ip_mask.ipv4_proto = 0; ip_mask.ipv4_ttl = 0; g_mask_ipv4 = (struct ovs_key_ipv4 *)(ptr + NLA_HDRLEN); ptr = (char *)nla_put(ptr, OVS_KEY_ATTR_IPV4, sizeof(ip_mask), &ip_mask); nla_mask->nla_len = (uint16_t)(ptr - mask_start); ptr = mask_start + NLA_ALIGN(nla_mask->nla_len); /* OVS_FLOW_ATTR_ACTIONS (empty nested attr) */ struct nlattr *nla_act = (struct nlattr *)ptr; nla_act->nla_type = OVS_FLOW_ATTR_ACTIONS; nla_act->nla_len = NLA_HDRLEN; ptr += NLA_HDRLEN; n->nlmsg_len = NLMSG_ALIGN(ptr - g_flow_buf); g_flow_len = n->nlmsg_len; } static int fast_create_flow(int nl_fd, int dp_ifindex, int k) { g_flow_hdr->dp_ifindex = dp_ifindex; g_key_ipv4->ipv4_src = htonl(0x0a000000 & (0xffffffff ^ k)); g_key_ipv4->ipv4_dst = htonl(0x0a010000 + k); g_mask_ipv4->ipv4_src = htonl(0xffffffff ^ k); ssize_t s = send(nl_fd, g_flow_buf, g_flow_len, 0); if (s < 0) { printf("[-] Failed to send flow %d on dp ifindex %d: %s\n", k, dp_ifindex, strerror(errno)); return -1; } return 0; } static int test_first_flow(int nl_fd, int dp_ifindex) { struct nlmsghdr *n = (struct nlmsghdr *)g_flow_buf; n->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; g_flow_hdr->dp_ifindex = dp_ifindex; g_key_ipv4->ipv4_src = htonl(0x0a000000); g_key_ipv4->ipv4_dst = htonl(0x0a010000); g_mask_ipv4->ipv4_src = htonl(0xffffffff); ssize_t s = send(nl_fd, g_flow_buf, g_flow_len, 0); if (s < 0) { printf("[-] Failed to send test flow: %s\n", strerror(errno)); return -1; } char reply_buf[4096]; ssize_t r = recv(nl_fd, reply_buf, sizeof(reply_buf), 0); if (r < 0) { printf("[-] Failed to recv ACK for test flow: %s\n", strerror(errno)); return -1; } struct nlmsghdr *nlh = (struct nlmsghdr *)reply_buf; if (!NLMSG_OK(nlh, (size_t)r)) { printf("[-] Invalid Netlink reply for test flow ACK\n"); return -1; } if (nlh->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(nlh); if (err->error != 0) { printf("[-] Test flow Netlink error: %d (%s)\n", err->error, strerror(-err->error)); return -1; } } n->nlmsg_flags = NLM_F_REQUEST; printf("[+] Verified flow insertion with kernel ACK successfully.\n"); return 0; } static int bring_interface_up(const char *name) { int s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { printf("[-] socket(AF_INET, SOCK_DGRAM) failed: %s\n", strerror(errno)); return -1; } struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, name, IFNAMSIZ - 1); int res = ioctl(s, SIOCGIFFLAGS, &ifr); if (res < 0) { printf("[-] ioctl(SIOCGIFFLAGS) for %s failed: %s\n", name, strerror(errno)); close(s); return -1; } ifr.ifr_flags |= IFF_UP | IFF_RUNNING; res = ioctl(s, SIOCSIFFLAGS, &ifr); if (res < 0) { printf("[-] ioctl(SIOCSIFFLAGS) for %s failed: %s\n", name, strerror(errno)); close(s); return -1; } res = close(s); if (res < 0) { printf("[-] close socket failed: %s\n", strerror(errno)); return -1; } return 0; } static void build_packet(unsigned char *frame, int k) { memset(frame, 0, 34); /* Ethernet header: dst 00:11:22:33:44:66, src 00:11:22:33:44:55, ethertype 0x0800 */ uint8_t dst_mac[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x66}; uint8_t src_mac[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; memcpy(&frame[0], dst_mac, 6); memcpy(&frame[6], src_mac, 6); frame[12] = 0x08; frame[13] = 0x00; /* IPv4 header (20 bytes) */ frame[14] = 0x45; /* Version 4, IHL 5 */ frame[15] = 0x00; /* TOS */ frame[16] = 0x00; frame[17] = 0x14; /* Total length = 20 bytes */ frame[18] = 0x00; frame[19] = 0x01; /* ID 1 */ frame[20] = 0x00; frame[21] = 0x00; /* Flags & Frag offset */ frame[22] = 64; /* TTL 64 */ frame[23] = 0; /* Protocol = 0 */ frame[24] = 0x00; frame[25] = 0x00; /* Checksum placeholder */ uint32_t s_ip = htonl(0x0a000000); uint32_t d_ip = htonl(0x0a010000 + k); memcpy(&frame[26], &s_ip, 4); memcpy(&frame[30], &d_ip, 4); uint32_t csum = 0; for (int i = 14; i < 34; i += 2) { csum += ((uint32_t)frame[i] << 8) + frame[i + 1]; } while (csum >> 16) { csum = (csum & 0xffff) + (csum >> 16); } uint16_t ip_csum = htons(~csum); memcpy(&frame[24], &ip_csum, 2); } static void spawn_cfs_hogs(int target_cpu, int count, int sync_pipe_read) { for (int i = 0; i < count; i++) { pid_t pid = fork(); if (pid < 0) { printf("[-] Failed to fork CFS CPU hog %d: %s\n", i, strerror(errno)); exit(1); } if (pid == 0) { prctl(PR_SET_PDEATHSIG, SIGKILL); if (getppid() != g_parent_pid) _exit(0); if (pin_to_cpu(target_cpu) < 0) _exit(1); int prio_res = setpriority(PRIO_PROCESS, 0, -20); if (prio_res < 0) { printf("[-] setpriority(-20) failed for CFS hog %d: %s\n", i, strerror(errno)); } char c = 0; ssize_t rd = read(sync_pipe_read, &c, 1); (void)rd; close(sync_pipe_read); volatile unsigned long x = 0; while (1) { x++; } _exit(0); } g_children[g_num_children++] = pid; } printf("[+] Spawned %d CFS CPU contention hogs (nice -20) on CPU %d.\n", count, target_cpu); } static void spawn_rt_spinner(int target_cpu, int sync_pipe_read) { pid_t pid = fork(); if (pid < 0) { printf("[-] Failed to fork RT spinner: %s\n", strerror(errno)); exit(1); } if (pid == 0) { prctl(PR_SET_PDEATHSIG, SIGKILL); if (getppid() != g_parent_pid) _exit(0); if (pin_to_cpu(target_cpu) < 0) _exit(1); struct sched_param sp; memset(&sp, 0, sizeof(sp)); sp.sched_priority = 50; int res = sched_setscheduler(0, SCHED_FIFO, &sp); if (res < 0) { printf("[-] sched_setscheduler(SCHED_FIFO, 50) failed: %s\n", strerror(errno)); _exit(1); } char c = 0; ssize_t rd = read(sync_pipe_read, &c, 1); (void)rd; close(sync_pipe_read); /* Pure zero-sleep busy loop: 100% CPU monopolization */ volatile unsigned long x = 0; while (1) { x++; } _exit(0); } g_children[g_num_children++] = pid; printf("[+] Spawned zero-sleep SCHED_FIFO spinner on CPU %d.\n", target_cpu); } static void spawn_child_netns(int target_cpu, int ns_idx) { pid_t pid = fork(); if (pid < 0) { printf("[-] Failed to fork child netns %d: %s\n", ns_idx, strerror(errno)); exit(1); } if (pid == 0) { prctl(PR_SET_PDEATHSIG, SIGKILL); if (getppid() != g_parent_pid) _exit(0); if (pin_to_cpu(target_cpu) < 0) _exit(1); if (setup_netns() < 0) _exit(1); int child_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); if (child_fd < 0) { printf("[-] Child netns %d socket failed: %s\n", ns_idx, strerror(errno)); _exit(1); } struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; setsockopt(child_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); int child_dp_fid = get_family_id(child_fd, OVS_DATAPATH_FAMILY); if (child_dp_fid < 0) _exit(1); int child_flow_fid = get_family_id(child_fd, OVS_FLOW_FAMILY); if (child_flow_fid < 0) _exit(1); init_flow_template(child_flow_fid); for (int i = 0; i < CHILD_DPS_COUNT; i++) { char name[32]; snprintf(name, sizeof(name), "cdp%d_%d", ns_idx, i); int ifindex = create_datapath(child_fd, child_dp_fid, name); if (ifindex <= 0) continue; for (int k = 0; k < CHILD_FLOWS_PER_DP; k++) { fast_create_flow(child_fd, ifindex, k); } bring_interface_up(name); } close(child_fd); /* Keep child netns alive forever so delayed work fires into CPU 1 worker pool */ while (1) { sleep(10); } _exit(0); } g_children[g_num_children++] = pid; } static void spawn_packet_churn(const int *ifindexes, int num_dps, int churn_pipe_read) { pid_t pid = fork(); if (pid < 0) { printf("[-] Failed to fork packet churn thread: %s\n", strerror(errno)); exit(1); } if (pid == 0) { prctl(PR_SET_PDEATHSIG, SIGKILL); if (getppid() != g_parent_pid) _exit(0); /* Pin packet churner to CPU 0 so it runs unimpeded by CPU 1 RT load */ if (pin_to_cpu(0) < 0) _exit(1); char c = 0; ssize_t rd = read(churn_pipe_read, &c, 1); (void)rd; close(churn_pipe_read); int pkt_sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (pkt_sock < 0) { printf("[-] Failed to create AF_PACKET socket in churn: %s\n", strerror(errno)); _exit(1); } unsigned char frame[34]; build_packet(frame, MAIN_FLOWS_PER_DP - 1); while (1) { for (int d = 0; d < num_dps; d++) { int ifidx = ifindexes[d]; if (ifidx <= 0) continue; struct sockaddr_ll sll; memset(&sll, 0, sizeof(sll)); sll.sll_family = AF_PACKET; sll.sll_ifindex = ifidx; sll.sll_protocol = htons(ETH_P_IP); sll.sll_halen = ETH_ALEN; memcpy(sll.sll_addr, "\x00\x11\x22\x33\x44\x66", 6); sendto(pkt_sock, frame, sizeof(frame), 0, (struct sockaddr *)&sll, sizeof(sll)); } usleep(100000); /* 100ms burst */ } close(pkt_sock); _exit(0); } g_children[g_num_children++] = pid; printf("[+] Spawned tail-end packet churn process on CPU 0.\n"); } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); g_parent_pid = getpid(); atexit(cleanup_children); signal(SIGALRM, on_alarm); alarm(85); printf("[*] Starting workqueue lockup in ovs_dp_masks_rebalance reproducer...\n"); int num_cpus = sysconf(_SC_NPROCESSORS_ONLN); if (num_cpus < 0) { printf("[-] sysconf(_SC_NPROCESSORS_ONLN) failed: %s\n", strerror(errno)); exit(1); } printf("[+] Online CPU count: %d\n", num_cpus); int target_cpu = (num_cpus > 1) ? 1 : 0; int supervisor_cpu = 0; printf("[+] Target CPU for workqueue and load: %d, Supervisor CPU: %d\n", target_cpu, supervisor_cpu); /* Pin calling process to target_cpu initially so initial timers attach to CPU 1 */ int res = pin_to_cpu(target_cpu); if (res < 0) exit(1); /* Step 1: Set workqueue watchdog threshold to 10s */ int wd_fd = open("/sys/module/workqueue/parameters/watchdog_thresh", O_WRONLY); if (wd_fd >= 0) { ssize_t w = write(wd_fd, "10\n", 3); if (w > 0) { printf("[+] Set workqueue watchdog threshold to 10s.\n"); } else { printf("[-] Failed to write to watchdog_thresh: %s\n", strerror(errno)); } close(wd_fd); } else { printf("[*] Note: /sys/module/workqueue/parameters/watchdog_thresh not accessible (%s)\n", strerror(errno)); } /* Disable real-time task throttling so SCHED_FIFO can monopolize CPU 1 */ int rt_fd = open("/proc/sys/kernel/sched_rt_runtime_us", O_WRONLY); if (rt_fd >= 0) { ssize_t w = write(rt_fd, "-1\n", 3); if (w > 0) { printf("[+] Disabled RT throttling (sched_rt_runtime_us = -1).\n"); } else { printf("[-] Failed to write to sched_rt_runtime_us: %s\n", strerror(errno)); } close(rt_fd); } else { printf("[*] Note: /proc/sys/kernel/sched_rt_runtime_us not accessible (%s)\n", strerror(errno)); } /* Best-effort: Restrict fair_server bandwidth via debugfs if available */ mount("none", "/sys/kernel/debug", "debugfs", 0, NULL); int fs_fd = open("/sys/kernel/debug/sched/fair_server/cpu1/runtime", O_WRONLY); if (fs_fd >= 0) { ssize_t w = write(fs_fd, "5000000\n", 8); if (w > 0) { printf("[+] Restricted fair_server runtime to 5ms on CPU 1.\n"); } close(fs_fd); } /* Create synchronization pipes */ int sync_pipe[2]; if (pipe(sync_pipe) < 0) { printf("[-] pipe(sync_pipe) failed: %s\n", strerror(errno)); exit(1); } int churn_pipe[2]; if (pipe(churn_pipe) < 0) { printf("[-] pipe(churn_pipe) failed: %s\n", strerror(errno)); exit(1); } /* Spawn 10 CFS CPU contention hogs (nice -20) on CPU 1 (waiting on sync_pipe) */ spawn_cfs_hogs(target_cpu, NUM_CFS_HOGS, sync_pipe[0]); /* Spawn zero-sleep SCHED_FIFO spinner on CPU 1 (waiting on sync_pipe) */ spawn_rt_spinner(target_cpu, sync_pipe[0]); /* Set up main network namespace */ if (setup_netns() < 0) { printf("[-] Main setup_netns() failed.\n"); exit(1); } int nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); if (nl_fd < 0) { printf("[-] Failed to create NETLINK_GENERIC socket: %s\n", strerror(errno)); exit(1); } printf("[+] socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC) successful.\n"); struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; res = setsockopt(nl_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); if (res < 0) { printf("[-] Failed to set SO_RCVTIMEO on socket: %s\n", strerror(errno)); close(nl_fd); exit(1); } int dp_family_id = get_family_id(nl_fd, OVS_DATAPATH_FAMILY); if (dp_family_id < 0) { printf("[-] Failed to resolve Open vSwitch datapath family.\n"); close(nl_fd); exit(1); } printf("[+] Family '%s' resolved to ID %d.\n", OVS_DATAPATH_FAMILY, dp_family_id); int flow_family_id = get_family_id(nl_fd, OVS_FLOW_FAMILY); if (flow_family_id < 0) { printf("[-] Failed to resolve Open vSwitch flow family.\n"); close(nl_fd); exit(1); } printf("[+] Family '%s' resolved to ID %d.\n", OVS_FLOW_FAMILY, flow_family_id); /* Initialize fast flow template */ init_flow_template(flow_family_id); /* Create 400 datapaths with 200 distinct flow wildcard masks in main netns */ printf("[*] Populating %d datapaths with %d distinct flow masks each on CPU %d...\n", MAIN_DPS_COUNT, MAIN_FLOWS_PER_DP, target_cpu); int ifindexes[MAIN_DPS_COUNT]; memset(ifindexes, 0, sizeof(ifindexes)); for (int i = 0; i < MAIN_DPS_COUNT; i++) { char dp_name[32]; snprintf(dp_name, sizeof(dp_name), "dp%d", i); int dp_ifindex = create_datapath(nl_fd, dp_family_id, dp_name); if (dp_ifindex <= 0) { printf("[-] Failed to create datapath %s\n", dp_name); close(nl_fd); exit(1); } ifindexes[i] = dp_ifindex; if (i == 0) { res = test_first_flow(nl_fd, dp_ifindex); if (res < 0) { printf("[-] First flow verification failed.\n"); close(nl_fd); exit(1); } } for (int k = 0; k < MAIN_FLOWS_PER_DP; k++) { res = fast_create_flow(nl_fd, dp_ifindex, k); if (res < 0) { printf("[-] Failed to create flow %d on datapath %s\n", k, dp_name); close(nl_fd); exit(1); } } res = bring_interface_up(dp_name); if (res < 0) { printf("[-] Failed to bring up interface %s\n", dp_name); close(nl_fd); exit(1); } if ((i + 1) % 100 == 0 || (i + 1) == MAIN_DPS_COUNT) { printf("[+] Populated %d/%d datapaths with %d distinct flow masks each.\n", i + 1, MAIN_DPS_COUNT, MAIN_FLOWS_PER_DP); } } printf("[+] All %d datapaths and %d flows populated in main netns.\n", MAIN_DPS_COUNT, MAIN_DPS_COUNT * MAIN_FLOWS_PER_DP); /* Spawn background packet churn process on CPU 0 in main netns */ spawn_packet_churn(ifindexes, MAIN_DPS_COUNT, churn_pipe[0]); /* Wake up packet churner */ if (write(churn_pipe[1], "C", 1) <= 0) { printf("[-] Failed to signal churn_pipe: %s\n", strerror(errno)); } close(churn_pipe[1]); /* Spawn 8 child network namespaces on CPU 1, staggered by 400ms */ printf("[*] Spawning %d child network namespaces on CPU %d...\n", NUM_CHILD_NETNS, target_cpu); for (int ns = 0; ns < NUM_CHILD_NETNS; ns++) { usleep(400000); /* 400ms */ spawn_child_netns(target_cpu, ns); } printf("[+] All %d child network namespaces spawned and staggered.\n", NUM_CHILD_NETNS); /* Pin supervisor process to CPU 0 so it avoids starvation */ res = pin_to_cpu(supervisor_cpu); if (res < 0) { printf("[-] Failed to pin supervisor thread to CPU %d: %s\n", supervisor_cpu, strerror(errno)); exit(1); } printf("[+] Supervisor thread pinned to CPU %d.\n", supervisor_cpu); /* Unleash RT spinner and CFS hogs on CPU 1 */ if (write(sync_pipe[1], "XXXXXXXXXXX", 11) <= 0) { printf("[-] Failed to signal sync_pipe: %s\n", strerror(errno)); } close(sync_pipe[1]); printf("[+] Unleashed zero-sleep SCHED_FIFO spinner and %d CFS hogs on CPU %d!\n", NUM_CFS_HOGS, target_cpu); /* Close netlink socket in parent */ close(nl_fd); /* Observe workqueue watchdog lockup */ printf("[*] Entering watchdog observation window (waiting 65 seconds for lockup detection)...\n"); for (int sec = 1; sec <= 65; sec++) { sleep(1); if (sec % 5 == 0) { printf("[*] %d/65 seconds elapsed (workqueue lockup in progress)...\n", sec); } } printf("[+] Watchdog observation window finished.\n"); cleanup_children(); printf("[+] Program completed cleanly.\n"); return 0; }