// KASAN: null-ptr-deref Write in sctp_auth_shkey_hold, from close() on an SCTP socket whose association // shared key could not be set up. // A listener on 127.0.0.1:9999 enables AUTH and lists ABORT among the chunks that must be authenticated, so its // INIT-ACK asks the client to authenticate ABORT. Each client connect() fails its Nth allocation (fail-nth, // CONFIG_FAULT_INJECTION); when the failed one is the association key secret of SCTP_CMD_ASSOC_SHKEY, the // association keeps peer.auth_capable with asoc->shkey == NULL, and close() with SO_LINGER {1, 0} builds an // authenticated ABORT that takes a reference on the NULL key. // It runs in a new user and network namespace, so it works as an unprivileged user too. #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #define SOL_SCTP 132 #define SCTP_AUTH_CHUNK 21 #define SCTP_AUTH_SUPPORTED 129 static void write_file(const char *path, const char *s) { int f = open(path, O_WRONLY); if (f >= 0 && write(f, s, strlen(s)) < 0) perror(path); close(f); } static void enter_netns_with_lo(void) { struct ifreq ifr = { .ifr_name = "lo", .ifr_flags = IFF_UP }; char map[32]; int s; snprintf(map, sizeof(map), "0 %d 1", getuid()); if (unshare(CLONE_NEWUSER | CLONE_NEWNET)) perror("unshare"); write_file("/proc/self/setgroups", "deny"); write_file("/proc/self/uid_map", map); snprintf(map, sizeof(map), "0 %d 1", getgid()); write_file("/proc/self/gid_map", map); s = socket(AF_INET, SOCK_DGRAM, 0); ioctl(s, SIOCSIFFLAGS, &ifr); close(s); } static int sctp_auth_socket(void) { struct { int32_t assoc_id; uint32_t value; } on = { 0, 1 }; int s = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); setsockopt(s, SOL_SCTP, SCTP_AUTH_SUPPORTED, &on, sizeof(on)); return s; } int main(void) { struct sockaddr_in sa = { .sin_family = AF_INET, .sin_port = htons(9999) }; struct linger abort_on_close = { 1, 0 }; uint8_t abort_chunk = 6; /* SCTP_CID_ABORT */ int lst, fail; enter_netns_with_lo(); lst = sctp_auth_socket(); fail = open("/proc/self/fail-nth", O_WRONLY); inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr); setsockopt(lst, SOL_SCTP, SCTP_AUTH_CHUNK, &abort_chunk, sizeof(abort_chunk)); if (bind(lst, (struct sockaddr *)&sa, sizeof(sa)) || listen(lst, 128) || fail < 0) { perror("setup"); return 1; } for (int n = 1; n <= 60; n++) { int c = sctp_auth_socket(); char nth[16]; fcntl(c, F_SETFL, O_NONBLOCK); pwrite(fail, nth, snprintf(nth, sizeof(nth), "%d", n), 0); connect(c, (struct sockaddr *)&sa, sizeof(sa)); usleep(20000); pwrite(fail, "0", 1, 0); setsockopt(c, SOL_SOCKET, SO_LINGER, &abort_on_close, sizeof(abort_on_close)); close(c); usleep(5000); } printf("done\n"); return 0; }