// A TCP socket in a sockmap with a stream verdict program (SK_PASS) receives data from a writer thread. The test // removes the socket from the sockmap (element delete, element overwrite, or closing the map) and immediately // installs the TLS ULP and TLS RX keys on it. 460e6486617c only refuses TLS while sk_psock(sk) is set, so this // succeeds once the psock is gone; a verdict sk_data_ready already in flight can then dequeue skbs from the // socket's receive queue that TLS now owns. // Runs as root (sockmap needs CAP_NET_ADMIN/CAP_BPF). Expect a NULL dereference in tls_strp_check_rcv(), or a // WARNING in tls_strp_load_anchor_with_queue() or tcp_recvmsg_locked(), depending on timing. #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef SOL_TLS #define SOL_TLS 282 #endif static int bpf(int cmd, union bpf_attr *a) { return syscall(__NR_bpf, cmd, a, sizeof(*a)); } static int sockmap_with_verdict(int prog) { union bpf_attr a = { .map_type = BPF_MAP_TYPE_SOCKMAP, .key_size = 4, .value_size = 4, .max_entries = 4 }; int map = bpf(BPF_MAP_CREATE, &a); memset(&a, 0, sizeof(a)); a.target_fd = map; a.attach_bpf_fd = prog; a.attach_type = BPF_SK_SKB_STREAM_VERDICT; bpf(BPF_PROG_ATTACH, &a); return map; } static int map_set(int map, uint32_t key, int sock, int cmd) { uint32_t val = sock; union bpf_attr a = { .map_fd = map, .key = (uintptr_t)&key, .value = (uintptr_t)&val }; return bpf(cmd, &a); } static void tcp_pair(int *c, int *s) { struct sockaddr_in a = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) }; socklen_t al = sizeof(a); int l = socket(AF_INET, SOCK_STREAM, 0); bind(l, (void *)&a, sizeof(a)); getsockname(l, (void *)&a, &al); listen(l, 1); *c = socket(AF_INET, SOCK_STREAM, 0); connect(*c, (void *)&a, sizeof(a)); *s = accept(l, NULL, NULL); close(l); } static void *writer(void *p) { /* A TLS 1.2 application-data header with a long length keeps a partial record queued. */ char buf[64] = { 0x17, 3, 3, 0, 0x40 }; for (int i = 0; i < 200; i++) if (send(*(int *)p, buf, sizeof(buf), MSG_NOSIGNAL) < 0) break; return NULL; } int main(void) { struct bpf_insn insns[] = { { .code = 0xb7, .imm = 1 }, { .code = 0x95 } }; /* r0 = SK_PASS; exit */ union bpf_attr a = { .prog_type = BPF_PROG_TYPE_SK_SKB, .insn_cnt = 2, .insns = (uintptr_t)insns, .license = (uintptr_t)"GPL" }; struct ifreq ifr = { .ifr_name = "lo", .ifr_flags = IFF_UP }; struct tls12_crypto_info_aes_gcm_128 ci = { .info = { TLS_1_2_VERSION, TLS_CIPHER_AES_GCM_128 } }; int s0 = socket(AF_INET, SOCK_DGRAM, 0), prog = bpf(BPF_PROG_LOAD, &a); unsigned seed = 1; time_t t0 = time(NULL); ioctl(s0, SIOCSIFFLAGS, &ifr); for (int i = 0; time(NULL) - t0 < 100; i++) { int map = sockmap_with_verdict(prog), c, s, c2 = -1, s2 = -1; pthread_t t; char drain[4096]; tcp_pair(&c, &s); map_set(map, 0, s, BPF_MAP_UPDATE_ELEM); pthread_create(&t, NULL, writer, &c); for (volatile unsigned k = rand_r(&seed) % 20000; k; k--) ; if (i % 3 == 0) map_set(map, 0, s, BPF_MAP_DELETE_ELEM); if (i % 3 == 1) { tcp_pair(&c2, &s2); map_set(map, 0, s2, BPF_MAP_UPDATE_ELEM); } if (i % 3 == 2) { close(map); map = -1; } if (!setsockopt(s, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"))) setsockopt(s, SOL_TLS, TLS_RX, &ci, sizeof(ci)); pthread_join(t, NULL); for (int k = 0; k < 128 && recv(s, drain, sizeof(drain), MSG_DONTWAIT) > 0; k++) ; close(s2); close(c2); close(map); close(s); close(c); } printf("done\n"); return 0; }