// https://syzkaller.appspot.com/bug?id=2ae09b46c8ae78c1122c851fe837ec9be684d7d9 #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 #include #include #include #include #include #ifndef NLA_F_NESTED #define NLA_F_NESTED (1 << 15) #endif /* RFC 7748 Curve25519 test vectors */ static const uint8_t alice_priv[32] = { 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a }; static const uint8_t alice_pub[32] = { 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b, 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d, 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, 0x4e, 0x6a }; static const uint8_t bob_priv[32] = { 0x5d, 0xab, 0x08, 0x7e, 0x62, 0x4a, 0x8a, 0x4b, 0x79, 0xe1, 0x7f, 0x8b, 0x83, 0x80, 0x0e, 0xe6, 0x6f, 0x3b, 0xb1, 0x29, 0x26, 0x18, 0xb6, 0xfd, 0x1c, 0x2f, 0x8b, 0x27, 0xff, 0x88, 0xe0, 0xeb }; static const uint8_t bob_pub[32] = { 0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4, 0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37, 0x3f, 0x83, 0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d, 0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f }; static const uint8_t charlie_pub[32] = { 0xc3, 0xda, 0x55, 0x37, 0x9d, 0xe9, 0xc6, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }; struct wg_peer_cfg { const uint8_t *public_key; uint16_t endpoint_port; const char *endpoint_ip; const char *allowed_ip; uint8_t cidr; }; static void add_nlattr(struct nlmsghdr *nlh, size_t maxlen, uint16_t type, const void *data, size_t datalen) { size_t len = RTA_LENGTH(datalen); if (NLMSG_ALIGN(nlh->nlmsg_len) + RTA_ALIGN(len) > maxlen) { printf("[-] add_nlattr: buffer overflow\n"); exit(1); } struct rtattr *rta = (struct rtattr *)((char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len)); rta->rta_type = type; rta->rta_len = len; if (datalen > 0 && data != NULL) memcpy(RTA_DATA(rta), data, datalen); nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + RTA_ALIGN(len); } static struct rtattr *start_nested_nlattr(struct nlmsghdr *nlh, size_t maxlen, uint16_t type) { struct rtattr *nest = (struct rtattr *)((char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len)); add_nlattr(nlh, maxlen, type, NULL, 0); return nest; } static void end_nested_nlattr(struct nlmsghdr *nlh, struct rtattr *nest) { nest->rta_len = (char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len) - (char *)nest; } static int netlink_wait_ack(int fd, uint32_t seq) { while (1) { char buf[4096]; struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) }; struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 }; ssize_t len = recvmsg(fd, &msg, 0); if (len < 0) { printf("[-] recvmsg failed: %s\n", strerror(errno)); return -1; } struct nlmsghdr *nlh = (struct nlmsghdr *)buf; for (; NLMSG_OK(nlh, (size_t)len); nlh = NLMSG_NEXT(nlh, len)) { if (nlh->nlmsg_seq != seq) continue; if (nlh->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(nlh); if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { printf("[-] Truncated NLMSG_ERROR\n"); return -1; } if (err->error != 0) { errno = -err->error; printf("[-] Netlink error: %d (%s)\n", err->error, strerror(-err->error)); return -1; } return 0; } } } } static void setup_namespaces(void) { uid_t uid = getuid(); gid_t gid = getgid(); if (uid == 0) { int res = unshare(CLONE_NEWNET); if (res < 0) { printf("[-] Failed to unshare(CLONE_NEWNET): %s\n", strerror(errno)); exit(1); } printf("[+] Successfully unshared CLONE_NEWNET as root\n"); } else { int res = unshare(CLONE_NEWUSER | CLONE_NEWNET); if (res < 0) { printf("[-] Failed to unshare(CLONE_NEWUSER | CLONE_NEWNET): %s\n", strerror(errno)); exit(1); } printf("[+] Successfully unshared CLONE_NEWUSER | CLONE_NEWNET\n"); FILE *f_deny = fopen("/proc/self/setgroups", "w"); if (f_deny) { if (fprintf(f_deny, "deny") < 0) { printf("[-] Failed to write to /proc/self/setgroups: %s\n", strerror(errno)); fclose(f_deny); exit(1); } fclose(f_deny); } FILE *f_uid = fopen("/proc/self/uid_map", "w"); if (!f_uid) { printf("[-] Failed to open /proc/self/uid_map: %s\n", strerror(errno)); exit(1); } if (fprintf(f_uid, "0 %d 1\n", uid) < 0) { printf("[-] Failed to write uid_map: %s\n", strerror(errno)); fclose(f_uid); exit(1); } fclose(f_uid); FILE *f_gid = fopen("/proc/self/gid_map", "w"); if (!f_gid) { printf("[-] Failed to open /proc/self/gid_map: %s\n", strerror(errno)); exit(1); } if (fprintf(f_gid, "0 %d 1\n", gid) < 0) { printf("[-] Failed to write gid_map: %s\n", strerror(errno)); fclose(f_gid); exit(1); } fclose(f_gid); printf("[+] Successfully configured uid_map and gid_map\n"); } } static int set_link_up(int rtnl_fd, const char *name) { static uint32_t seq = 100; seq++; int ifindex = if_nametoindex(name); if (ifindex <= 0) { printf("[-] Interface %s not found: %s\n", name, strerror(errno)); return -1; } char buf[1024]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; nlh->nlmsg_type = RTM_NEWLINK; nlh->nlmsg_seq = seq; struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nlh); ifi->ifi_family = AF_UNSPEC; ifi->ifi_index = ifindex; ifi->ifi_flags = IFF_UP; ifi->ifi_change = IFF_UP; if (send(rtnl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send RTM_NEWLINK (UP) for %s: %s\n", name, strerror(errno)); return -1; } return netlink_wait_ack(rtnl_fd, seq); } static int resolve_genl_family(int genl_fd, const char *family_name) { char buf[1024]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); nlh->nlmsg_flags = NLM_F_REQUEST; nlh->nlmsg_type = GENL_ID_CTRL; nlh->nlmsg_seq = 201; struct genlmsghdr *ghdr = (struct genlmsghdr *)NLMSG_DATA(nlh); ghdr->cmd = CTRL_CMD_GETFAMILY; ghdr->version = 1; add_nlattr(nlh, sizeof(buf), CTRL_ATTR_FAMILY_NAME, family_name, strlen(family_name) + 1); if (send(genl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send CTRL_CMD_GETFAMILY: %s\n", strerror(errno)); return -1; } char rcvbuf[4096]; struct iovec iov = { .iov_base = rcvbuf, .iov_len = sizeof(rcvbuf) }; struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 }; ssize_t len = recvmsg(genl_fd, &msg, 0); if (len < 0) { printf("[-] recvmsg from genl_fd failed: %s\n", strerror(errno)); return -1; } struct nlmsghdr *resp = (struct nlmsghdr *)rcvbuf; for (; NLMSG_OK(resp, (size_t)len); resp = NLMSG_NEXT(resp, len)) { if (resp->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(resp); printf("[-] Genl error resolving %s: %d (%s)\n", family_name, err->error, strerror(-err->error)); return -1; } if (resp->nlmsg_type == GENL_ID_CTRL) { struct genlmsghdr *rghdr = (struct genlmsghdr *)NLMSG_DATA(resp); struct rtattr *rta = (struct rtattr *)((char *)rghdr + GENL_HDRLEN); int rta_len = resp->nlmsg_len - NLMSG_LENGTH(GENL_HDRLEN); for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) { if (rta->rta_type == CTRL_ATTR_FAMILY_ID) { uint16_t id = *(uint16_t *)RTA_DATA(rta); return id; } } } } printf("[-] CTRL_ATTR_FAMILY_ID not found in genl response\n"); return -1; } static int create_wireguard_interface(int rtnl_fd, const char *name) { static uint32_t seq = 300; seq++; char buf[1024]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL; nlh->nlmsg_type = RTM_NEWLINK; nlh->nlmsg_seq = seq; struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nlh); ifi->ifi_family = AF_UNSPEC; add_nlattr(nlh, sizeof(buf), IFLA_IFNAME, name, strlen(name) + 1); struct rtattr *linkinfo = start_nested_nlattr(nlh, sizeof(buf), IFLA_LINKINFO); add_nlattr(nlh, sizeof(buf), IFLA_INFO_KIND, "wireguard", strlen("wireguard")); end_nested_nlattr(nlh, linkinfo); if (send(rtnl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send RTM_NEWLINK for %s: %s\n", name, strerror(errno)); return -1; } return netlink_wait_ack(rtnl_fd, seq); } static int create_veth_pair(int rtnl_fd, const char *name0, const char *name1) { static uint32_t seq = 400; seq++; char buf[1024]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL; nlh->nlmsg_type = RTM_NEWLINK; nlh->nlmsg_seq = seq; struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nlh); ifi->ifi_family = AF_UNSPEC; add_nlattr(nlh, sizeof(buf), IFLA_IFNAME, name0, strlen(name0) + 1); struct rtattr *linkinfo = start_nested_nlattr(nlh, sizeof(buf), IFLA_LINKINFO); add_nlattr(nlh, sizeof(buf), IFLA_INFO_KIND, "veth", strlen("veth")); struct rtattr *infodata = start_nested_nlattr(nlh, sizeof(buf), IFLA_INFO_DATA); struct rtattr *peer = start_nested_nlattr(nlh, sizeof(buf), VETH_INFO_PEER); size_t ifi_len = sizeof(struct ifinfomsg); if (NLMSG_ALIGN(nlh->nlmsg_len) + ifi_len > sizeof(buf)) { printf("[-] Buffer overflow adding peer ifinfomsg\n"); return -1; } struct ifinfomsg *peer_ifi = (struct ifinfomsg *)((char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len)); memset(peer_ifi, 0, ifi_len); peer_ifi->ifi_family = AF_UNSPEC; nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + ifi_len; add_nlattr(nlh, sizeof(buf), IFLA_IFNAME, name1, strlen(name1) + 1); end_nested_nlattr(nlh, peer); end_nested_nlattr(nlh, infodata); end_nested_nlattr(nlh, linkinfo); if (send(rtnl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send RTM_NEWLINK for veth: %s\n", strerror(errno)); return -1; } return netlink_wait_ack(rtnl_fd, seq); } static int create_macvtap_interface(int rtnl_fd, const char *name, int link_index) { static uint32_t seq = 500; seq++; char buf[1024]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL; nlh->nlmsg_type = RTM_NEWLINK; nlh->nlmsg_seq = seq; struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nlh); ifi->ifi_family = AF_UNSPEC; add_nlattr(nlh, sizeof(buf), IFLA_IFNAME, name, strlen(name) + 1); add_nlattr(nlh, sizeof(buf), IFLA_LINK, &link_index, sizeof(int)); struct rtattr *linkinfo = start_nested_nlattr(nlh, sizeof(buf), IFLA_LINKINFO); add_nlattr(nlh, sizeof(buf), IFLA_INFO_KIND, "macvtap", strlen("macvtap")); end_nested_nlattr(nlh, linkinfo); if (send(rtnl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send RTM_NEWLINK for macvtap: %s\n", strerror(errno)); return -1; } return netlink_wait_ack(rtnl_fd, seq); } static int add_ip_addr(int rtnl_fd, const char *ifname, const char *ip_str, int cidr) { static uint32_t seq = 600; seq++; int ifindex = if_nametoindex(ifname); if (ifindex <= 0) { printf("[-] Failed to find ifindex for %s: %s\n", ifname, strerror(errno)); return -1; } char buf[1024]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL; nlh->nlmsg_type = RTM_NEWADDR; nlh->nlmsg_seq = seq; struct ifaddrmsg *ifa = (struct ifaddrmsg *)NLMSG_DATA(nlh); ifa->ifa_family = AF_INET; ifa->ifa_prefixlen = cidr; ifa->ifa_flags = IFA_F_PERMANENT; ifa->ifa_scope = RT_SCOPE_UNIVERSE; ifa->ifa_index = ifindex; struct in_addr addr; if (inet_pton(AF_INET, ip_str, &addr) != 1) { printf("[-] Invalid IP address: %s\n", ip_str); return -1; } add_nlattr(nlh, sizeof(buf), IFA_LOCAL, &addr, sizeof(addr)); add_nlattr(nlh, sizeof(buf), IFA_ADDRESS, &addr, sizeof(addr)); if (send(rtnl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send RTM_NEWADDR for %s: %s\n", ifname, strerror(errno)); return -1; } return netlink_wait_ack(rtnl_fd, seq); } static int add_clsact_qdisc(int rtnl_fd, const char *ifname) { static uint32_t seq = 700; seq++; int ifindex = if_nametoindex(ifname); if (ifindex <= 0) { printf("[-] Failed to find ifindex for %s: %s\n", ifname, strerror(errno)); return -1; } char buf[1024]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL; nlh->nlmsg_type = RTM_NEWQDISC; nlh->nlmsg_seq = seq; struct tcmsg *tcm = (struct tcmsg *)NLMSG_DATA(nlh); tcm->tcm_family = AF_UNSPEC; tcm->tcm_ifindex = ifindex; tcm->tcm_parent = TC_H_CLSACT; tcm->tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0); add_nlattr(nlh, sizeof(buf), TCA_KIND, "clsact", strlen("clsact") + 1); if (send(rtnl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send RTM_NEWQDISC for %s: %s\n", ifname, strerror(errno)); return -1; } return netlink_wait_ack(rtnl_fd, seq); } static int add_redirect_filter(int rtnl_fd, const char *src_ifname, const char *dst_ifname) { static uint32_t seq = 800; seq++; int src_ifindex = if_nametoindex(src_ifname); int dst_ifindex = if_nametoindex(dst_ifname); if (src_ifindex <= 0 || dst_ifindex <= 0) { printf("[-] Failed to get ifindex: %s (%d), %s (%d)\n", src_ifname, src_ifindex, dst_ifname, dst_ifindex); return -1; } char buf[2048]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL; nlh->nlmsg_type = RTM_NEWTFILTER; nlh->nlmsg_seq = seq; struct tcmsg *tcm = (struct tcmsg *)NLMSG_DATA(nlh); tcm->tcm_family = AF_UNSPEC; tcm->tcm_ifindex = src_ifindex; tcm->tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS); tcm->tcm_info = TC_H_MAKE(1 << 16, htons(ETH_P_ALL)); add_nlattr(nlh, sizeof(buf), TCA_KIND, "matchall", strlen("matchall") + 1); struct rtattr *opts = start_nested_nlattr(nlh, sizeof(buf), TCA_OPTIONS); struct rtattr *act = start_nested_nlattr(nlh, sizeof(buf), TCA_MATCHALL_ACT); struct rtattr *act_1 = start_nested_nlattr(nlh, sizeof(buf), 1); add_nlattr(nlh, sizeof(buf), TCA_ACT_KIND, "mirred", strlen("mirred") + 1); struct rtattr *act_opts = start_nested_nlattr(nlh, sizeof(buf), TCA_ACT_OPTIONS); struct tc_mirred p; memset(&p, 0, sizeof(p)); p.action = TC_ACT_STOLEN; p.eaction = TCA_EGRESS_REDIR; p.ifindex = dst_ifindex; add_nlattr(nlh, sizeof(buf), TCA_MIRRED_PARMS, &p, sizeof(p)); end_nested_nlattr(nlh, act_opts); end_nested_nlattr(nlh, act_1); end_nested_nlattr(nlh, act); end_nested_nlattr(nlh, opts); if (send(rtnl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send RTM_NEWTFILTER: %s\n", strerror(errno)); return -1; } return netlink_wait_ack(rtnl_fd, seq); } static int wg_configure_device(int genl_fd, int wg_family, const char *ifname, const uint8_t private_key[32], uint16_t listen_port, const struct wg_peer_cfg *peers, size_t num_peers) { static uint32_t seq = 900; seq++; char buf[4096]; memset(buf, 0, sizeof(buf)); struct nlmsghdr *nlh = (struct nlmsghdr *)buf; nlh->nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; nlh->nlmsg_type = wg_family; nlh->nlmsg_seq = seq; struct genlmsghdr *ghdr = (struct genlmsghdr *)NLMSG_DATA(nlh); ghdr->cmd = WG_CMD_SET_DEVICE; ghdr->version = 1; add_nlattr(nlh, sizeof(buf), WGDEVICE_A_IFNAME, ifname, strlen(ifname) + 1); if (private_key) add_nlattr(nlh, sizeof(buf), WGDEVICE_A_PRIVATE_KEY, private_key, 32); if (listen_port) add_nlattr(nlh, sizeof(buf), WGDEVICE_A_LISTEN_PORT, &listen_port, sizeof(uint16_t)); if (num_peers > 0) { /* NLA_F_NESTED flag is required on all nested attribute types in WireGuard Netlink */ struct rtattr *peers_nest = start_nested_nlattr(nlh, sizeof(buf), WGDEVICE_A_PEERS | NLA_F_NESTED); for (size_t i = 0; i < num_peers; i++) { struct rtattr *peer_nest = start_nested_nlattr(nlh, sizeof(buf), (i + 1) | NLA_F_NESTED); add_nlattr(nlh, sizeof(buf), WGPEER_A_PUBLIC_KEY, peers[i].public_key, 32); if (peers[i].endpoint_ip && peers[i].endpoint_port) { struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(peers[i].endpoint_port); inet_pton(AF_INET, peers[i].endpoint_ip, &sin.sin_addr); add_nlattr(nlh, sizeof(buf), WGPEER_A_ENDPOINT, &sin, sizeof(sin)); } if (peers[i].allowed_ip) { struct rtattr *allowed_nest = start_nested_nlattr(nlh, sizeof(buf), WGPEER_A_ALLOWEDIPS | NLA_F_NESTED); struct rtattr *ip_nest = start_nested_nlattr(nlh, sizeof(buf), 1 | NLA_F_NESTED); uint16_t fam = AF_INET; add_nlattr(nlh, sizeof(buf), WGALLOWEDIP_A_FAMILY, &fam, sizeof(fam)); struct in_addr ip_addr; inet_pton(AF_INET, peers[i].allowed_ip, &ip_addr); add_nlattr(nlh, sizeof(buf), WGALLOWEDIP_A_IPADDR, &ip_addr, sizeof(ip_addr)); uint8_t cidr = peers[i].cidr; add_nlattr(nlh, sizeof(buf), WGALLOWEDIP_A_CIDR_MASK, &cidr, sizeof(cidr)); end_nested_nlattr(nlh, ip_nest); end_nested_nlattr(nlh, allowed_nest); } end_nested_nlattr(nlh, peer_nest); } end_nested_nlattr(nlh, peers_nest); } if (send(genl_fd, nlh, nlh->nlmsg_len, 0) < 0) { printf("[-] Failed to send WG_CMD_SET_DEVICE for %s: %s\n", ifname, strerror(errno)); return -1; } return netlink_wait_ack(genl_fd, seq); } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[+] Starting reproducer for lockdep warning in wg_socket_clear_peer_endpoint_src...\n"); setup_namespaces(); int rtnl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (rtnl_fd < 0) { printf("[-] Failed to open NETLINK_ROUTE socket: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully opened NETLINK_ROUTE socket\n"); int genl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); if (genl_fd < 0) { printf("[-] Failed to open NETLINK_GENERIC socket: %s\n", strerror(errno)); close(rtnl_fd); exit(1); } printf("[+] Successfully opened NETLINK_GENERIC socket\n"); int res = set_link_up(rtnl_fd, "lo"); if (res < 0) { printf("[-] Failed to bring up loopback 'lo': %s\n", strerror(errno)); exit(1); } printf("[+] Successfully brought up loopback 'lo'\n"); int wg_family = resolve_genl_family(genl_fd, WG_GENL_NAME); if (wg_family < 0) { printf("[-] WireGuard Generic Netlink family not available\n"); exit(1); } printf("[+] WireGuard Generic Netlink family resolved (family ID = %d)\n", wg_family); res = create_wireguard_interface(rtnl_fd, "wg0"); if (res < 0) { printf("[-] Failed to create wireguard interface 'wg0': %s\n", strerror(errno)); exit(1); } printf("[+] Successfully created wireguard interface 'wg0'\n"); res = create_wireguard_interface(rtnl_fd, "wg1"); if (res < 0) { printf("[-] Failed to create wireguard interface 'wg1': %s\n", strerror(errno)); exit(1); } printf("[+] Successfully created wireguard interface 'wg1'\n"); res = create_veth_pair(rtnl_fd, "veth0", "veth1"); if (res < 0) { printf("[-] Failed to create veth pair: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully created veth pair 'veth0' <-> 'veth1'\n"); int veth0_idx = if_nametoindex("veth0"); if (veth0_idx <= 0) { printf("[-] Failed to get ifindex for veth0: %s\n", strerror(errno)); exit(1); } res = create_macvtap_interface(rtnl_fd, "macvtap0", veth0_idx); if (res < 0) { printf("[-] Failed to create macvtap interface 'macvtap0': %s\n", strerror(errno)); exit(1); } printf("[+] Successfully created macvtap interface 'macvtap0'\n"); int tap_ifindex = if_nametoindex("macvtap0"); if (tap_ifindex <= 0) { printf("[-] Failed to get ifindex for macvtap0: %s\n", strerror(errno)); exit(1); } printf("[+] macvtap0 ifindex is %d\n", tap_ifindex); res = set_link_up(rtnl_fd, "veth0"); if (res < 0) { printf("[-] Failed to bring up veth0: %s\n", strerror(errno)); exit(1); } res = set_link_up(rtnl_fd, "veth1"); if (res < 0) { printf("[-] Failed to bring up veth1: %s\n", strerror(errno)); exit(1); } res = set_link_up(rtnl_fd, "macvtap0"); if (res < 0) { printf("[-] Failed to bring up macvtap0: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully brought up veth0, veth1, and macvtap0\n"); res = add_ip_addr(rtnl_fd, "wg0", "10.0.0.1", 24); if (res < 0) { printf("[-] Failed to assign 10.0.0.1/24 to wg0: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully assigned 10.0.0.1/24 to wg0\n"); res = set_link_up(rtnl_fd, "wg0"); if (res < 0) { printf("[-] Failed to bring up wg0: %s\n", strerror(errno)); exit(1); } res = set_link_up(rtnl_fd, "wg1"); if (res < 0) { printf("[-] Failed to bring up wg1: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully brought up wg0 and wg1\n"); res = add_clsact_qdisc(rtnl_fd, "veth1"); if (res < 0) { printf("[-] Failed to add clsact qdisc to veth1: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully attached clsact qdisc to veth1\n"); res = add_redirect_filter(rtnl_fd, "veth1", "wg0"); if (res < 0) { printf("[-] Failed to add matchall redirect filter to veth1: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully installed TC matchall mirred redirect filter: veth1 -> wg0\n"); struct wg_peer_cfg wg0_peers[2] = { { .public_key = bob_pub, .endpoint_ip = "127.0.0.1", .endpoint_port = 12346, .allowed_ip = "10.0.0.2", .cidr = 32 }, { .public_key = charlie_pub, .endpoint_ip = "127.0.0.1", .endpoint_port = 12347, .allowed_ip = "10.0.0.3", .cidr = 32 } }; res = wg_configure_device(genl_fd, wg_family, "wg0", alice_priv, 12345, wg0_peers, 2); if (res < 0) { printf("[-] Failed to configure wg0: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully configured wg0 (Alice, listen 12345, peers: Bob, Charlie)\n"); struct wg_peer_cfg wg1_peers[1] = { { .public_key = alice_pub, .endpoint_ip = "127.0.0.1", .endpoint_port = 12345, .allowed_ip = "10.0.0.1", .cidr = 32 } }; res = wg_configure_device(genl_fd, wg_family, "wg1", bob_priv, 12346, wg1_peers, 1); if (res < 0) { printf("[-] Failed to configure wg1: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully configured wg1 (Bob, listen 12346, peer: Alice)\n"); int dummy_fd = socket(AF_INET, SOCK_DGRAM, 0); if (dummy_fd < 0) { printf("[-] Failed to create dummy UDP socket: %s\n", strerror(errno)); exit(1); } struct sockaddr_in charlie_addr; memset(&charlie_addr, 0, sizeof(charlie_addr)); charlie_addr.sin_family = AF_INET; charlie_addr.sin_port = htons(12347); charlie_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (bind(dummy_fd, (struct sockaddr *)&charlie_addr, sizeof(charlie_addr)) < 0) { printf("[-] Failed to bind dummy UDP listener to 127.0.0.1:12347: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully bound dummy UDP listener to 127.0.0.1:12347\n"); int ping_sock = socket(AF_INET, SOCK_DGRAM, 0); if (ping_sock < 0) { printf("[-] Failed to create ping UDP socket: %s\n", strerror(errno)); exit(1); } struct sockaddr_in ping_src; memset(&ping_src, 0, sizeof(ping_src)); ping_src.sin_family = AF_INET; ping_src.sin_addr.s_addr = inet_addr("10.0.0.1"); ping_src.sin_port = htons(54321); if (bind(ping_sock, (struct sockaddr *)&ping_src, sizeof(ping_src)) < 0) { printf("[-] Failed to bind ping socket to 10.0.0.1:54321: %s\n", strerror(errno)); exit(1); } struct sockaddr_in bob_dst; memset(&bob_dst, 0, sizeof(bob_dst)); bob_dst.sin_family = AF_INET; bob_dst.sin_addr.s_addr = inet_addr("10.0.0.2"); bob_dst.sin_port = htons(1234); if (sendto(ping_sock, "handshake_bob", 13, 0, (struct sockaddr *)&bob_dst, sizeof(bob_dst)) < 0) { printf("[-] Failed to send UDP ping to 10.0.0.2: %s\n", strerror(errno)); exit(1); } printf("[+] Sent handshake trigger to Bob (10.0.0.2)\n"); usleep(200000); // 200 ms for WireGuard handshake between wg0 and wg1 struct sockaddr_in charlie_dst; memset(&charlie_dst, 0, sizeof(charlie_dst)); charlie_dst.sin_family = AF_INET; charlie_dst.sin_addr.s_addr = inet_addr("10.0.0.3"); charlie_dst.sin_port = htons(1234); if (sendto(ping_sock, "handshake_charlie", 17, 0, (struct sockaddr *)&charlie_dst, sizeof(charlie_dst)) < 0) { printf("[-] Failed to send UDP packet to Charlie (10.0.0.3): %s\n", strerror(errno)); exit(1); } printf("[+] Sent handshake trigger to Charlie (10.0.0.3)\n"); printf("[*] Sleeping 6 seconds for Charlie handshake timer to expire in softirq...\n"); sleep(6); printf("[+] Handshake timer expired, &peer->endpoint_lock is now marked SOFTIRQ-safe\n"); char dev_path[128]; snprintf(dev_path, sizeof(dev_path), "/dev/tap%d", tap_ifindex); char mknod_path[128] = {0}; int tap_fd_check = -1; for (int retry = 0; retry < 5; retry++) { tap_fd_check = open(dev_path, O_RDWR); if (tap_fd_check >= 0) break; usleep(50000); } if (tap_fd_check < 0) { printf("[*] %s not directly present, querying sysfs for major:minor\n", dev_path); char sysfs_path[256]; FILE *f = NULL; snprintf(sysfs_path, sizeof(sysfs_path), "/sys/class/net/macvtap0/tap%d/dev", tap_ifindex); f = fopen(sysfs_path, "r"); if (!f) { snprintf(sysfs_path, sizeof(sysfs_path), "/sys/class/macvtap/tap%d/dev", tap_ifindex); f = fopen(sysfs_path, "r"); } if (!f) { snprintf(sysfs_path, sizeof(sysfs_path), "/sys/devices/virtual/net/macvtap0/tap%d/dev", tap_ifindex); f = fopen(sysfs_path, "r"); } if (!f) { printf("[-] Failed to open sysfs dev file for tap%d: %s\n", tap_ifindex, strerror(errno)); exit(1); } int major = 0, minor = 0; if (fscanf(f, "%d:%d", &major, &minor) != 2) { printf("[-] Failed to parse major:minor from %s\n", sysfs_path); fclose(f); exit(1); } fclose(f); printf("[+] Found macvtap device numbers %d:%d\n", major, minor); snprintf(mknod_path, sizeof(mknod_path), "/tmp/tap%d", tap_ifindex); unlink(mknod_path); int mres = mknod(mknod_path, S_IFCHR | 0666, makedev(major, minor)); if (mres < 0) { printf("[-] Failed to mknod %s: %s\n", mknod_path, strerror(errno)); exit(1); } printf("[+] Successfully created device node %s\n", mknod_path); snprintf(dev_path, sizeof(dev_path), "%s", mknod_path); } else { close(tap_fd_check); } printf("[+] Using macvtap device node %s\n", dev_path); struct { struct virtio_net_hdr vhdr; struct ethhdr eth; struct iphdr ip; struct udphdr udp; char payload[16]; } __attribute__((packed)) pkt; memset(&pkt, 0, sizeof(pkt)); pkt.eth.h_proto = htons(ETH_P_IP); memset(pkt.eth.h_dest, 0xff, 6); pkt.eth.h_source[0] = 0x02; pkt.eth.h_source[1] = 0xaa; pkt.eth.h_source[2] = 0xbb; pkt.eth.h_source[3] = 0xcc; pkt.eth.h_source[4] = 0xdd; pkt.eth.h_source[5] = 0xee; pkt.ip.version = 4; pkt.ip.ihl = 5; pkt.ip.tos = 0; pkt.ip.tot_len = htons(sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(pkt.payload)); pkt.ip.id = htons(0x1234); pkt.ip.frag_off = 0; pkt.ip.ttl = 64; pkt.ip.protocol = IPPROTO_UDP; pkt.ip.saddr = inet_addr("10.0.0.1"); pkt.ip.daddr = inet_addr("10.0.0.2"); uint32_t csum = 0; uint16_t *ip16 = (uint16_t *)&pkt.ip; for (size_t i = 0; i < sizeof(struct iphdr) / 2; i++) { csum += ip16[i]; } while (csum >> 16) { csum = (csum & 0xffff) + (csum >> 16); } pkt.ip.check = (uint16_t)~csum; pkt.udp.source = htons(1234); pkt.udp.dest = htons(1234); pkt.udp.len = htons(sizeof(struct udphdr) + sizeof(pkt.payload)); pkt.udp.check = 0; memset(pkt.payload, 'X', sizeof(pkt.payload)); printf("[*] Starting reproduction loop to trigger lock inversion deadlock...\n"); for (int iter = 0; iter < 100; iter++) { int tap_fd = open(dev_path, O_RDWR); if (tap_fd < 0) { printf("[-] Failed to open %s in iteration %d: %s\n", dev_path, iter, strerror(errno)); break; } struct pollfd pfd = { .fd = tap_fd, .events = POLLIN }; int pres = poll(&pfd, 1, 0); if (pres < 0) { printf("[-] poll failed in iteration %d: %s\n", iter, strerror(errno)); close(tap_fd); break; } for (int p = 0; p < 25; p++) { ssize_t w = write(tap_fd, &pkt, sizeof(pkt)); (void)w; } int cres = close(tap_fd); if (cres < 0) { printf("[-] close failed in iteration %d: %s\n", iter, strerror(errno)); break; } usleep(10000); // 10ms } printf("[+] Completed reproduction loop.\n"); sleep(3); if (mknod_path[0]) unlink(mknod_path); close(ping_sock); close(dummy_fd); close(rtnl_fd); close(genl_fd); printf("[+] Reproducer finished execution successfully.\n"); return 0; }