Since commit 29b99f54a8e6 ("sctp: destroy bucket if failed to bind addr"), sctp_do_bind() releases the port when sctp_add_bind_addr() fails. sctp_bindx_add() also calls it for each extra address of a socket that is already bound, and possibly listening. There sctp_get_port_local() takes no new reference, but the failure path still calls sctp_put_port(), which unhashes the port and clears bind_hash and inet_num while the socket keeps its other addresses and stays listening. listen(0) then writes through the NULL bind_hash, and accept() of a pending association oopses in sctp_clone_sock(): KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:sctp_inet_listen (net/sctp/socket.c:8723) Call Trace: __sys_listen (net/socket.c:2016) __x64_sys_listen (net/socket.c:2021) do_syscall_64 (arch/x86/entry/syscall_64.c:61) Release the port only when this call took it, that is, when the socket had no bind_hash before. Fixes: 29b99f54a8e6 ("sctp: destroy bucket if failed to bind addr") Signed-off-by: Kyuwon Cho --- The issue was found by an LLM-guided test generator that we are developing as a research prototype. A reproducer, which runs as any user with CONFIG_FAILSLAB: #define _GNU_SOURCE #include #include #include #include #include #include #include #include #define SCTP_SOCKOPT_BINDX_ADD 100 /* needs CONFIG_FAULT_INJECTION and CONFIG_FAILSLAB; runs as any user */ int main(void) { struct ifreq lo = { .ifr_name = "lo", .ifr_flags = IFF_UP }; struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(30114), .sin_addr.s_addr = htonl(INADDR_LOOPBACK), }; int s, one = 1, nth = open("/proc/thread-self/fail-nth", O_WRONLY); if (unshare(CLONE_NEWUSER | CLONE_NEWNET)) return perror("unshare"), 1; s = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); ioctl(s, SIOCSIFFLAGS, &lo); bind(s, (void *)&a, sizeof(a)); listen(s, 5); setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); /* fail the allocation in sctp_add_bind_addr() */ a.sin_addr.s_addr = inet_addr("127.0.0.2"); if (write(nth, "1", 1) != 1) return perror("fail-nth"), 1; printf("bindx add: %d\n", setsockopt(s, IPPROTO_SCTP, SCTP_SOCKOPT_BINDX_ADD, &a, sizeof(a))); printf("listen(0): %d\n", listen(s, 0)); return 0; } Without the patch, listen(0) oopses as above; with it, it returns 0, and in a variant that accepts a pending association, accept() returns the new socket. The patch was written with LLM assistance; we reviewed and tested it. net/sctp/socket.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index bc68af88e271..519063833aac 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -420,6 +420,7 @@ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) struct sctp_bind_addr *bp = &ep->base.bind_addr; struct sctp_af *af; unsigned short snum; + bool bound; int ret = 0; /* Common sockaddr verification. */ @@ -467,6 +468,7 @@ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) * The function sctp_get_port_local() does duplicate address * detection. */ + bound = sp->bind_hash; addr->v4.sin_port = htons(snum); if (sctp_get_port_local(sk, addr)) return -EADDRINUSE; @@ -484,7 +486,8 @@ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) SCTP_ADDR_SRC, GFP_ATOMIC); if (ret) { - sctp_put_port(sk); + if (!bound) + sctp_put_port(sk); return ret; } /* Copy back into socket for getsockname() use. */ -- 2.43.0