Netdev List
 help / color / mirror / Atom feed
From: Kyuwon Cho <kyuwoncho18@gmail.com>
To: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>,
	Xin Long <lucien.xin@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Neil Horman <nhorman@tuxdriver.com>,
	Mao Wenan <maowenan@huawei.com>,
	linux-sctp@vger.kernel.org, netdev@vger.kernel.org
Subject: [PATCH net] sctp: keep the port of a bound socket when adding an address fails
Date: Wed, 25 Nov 2026 22:49:00 +0900	[thread overview]
Message-ID: <179560159232.2963157.3418291084812904092@gmail.com> (raw)

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 <kyuwoncho18@gmail.com>
---
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 <arpa/inet.h>
#include <fcntl.h>
#include <net/if.h>
#include <sched.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

#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


                 reply	other threads:[~2026-11-25 13:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179560159232.2963157.3418291084812904092@gmail.com \
    --to=kyuwoncho18@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-sctp@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=maowenan@huawei.com \
    --cc=marcelo.leitner@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=nhorman@tuxdriver.com \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox