Netdev List
 help / color / mirror / Atom feed
* [PATCH net] sctp: keep the port of a bound socket when adding an address fails
@ 2026-11-25 13:49 Kyuwon Cho
  2026-11-25 18:52 ` Xin Long
  2026-12-02  2:40 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Kyuwon Cho @ 2026-11-25 13:49 UTC (permalink / raw)
  To: Marcelo Ricardo Leitner, Xin Long
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Neil Horman, Mao Wenan,
	linux-sctp, netdev

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net] sctp: keep the port of a bound socket when adding an address fails
  2026-11-25 13:49 [PATCH net] sctp: keep the port of a bound socket when adding an address fails Kyuwon Cho
@ 2026-11-25 18:52 ` Xin Long
  2026-12-02  2:40 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Xin Long @ 2026-11-25 18:52 UTC (permalink / raw)
  To: Kyuwon Cho
  Cc: Marcelo Ricardo Leitner, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
	Neil Horman, Mao Wenan, linux-sctp, netdev

On Wed, Nov 25, 2026 at 8:49 AM Kyuwon Cho <kyuwoncho18@gmail.com> wrote:
>
> 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
>
Acked-by: Xin Long <lucien.xin@gmail.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] sctp: keep the port of a bound socket when adding an address fails
  2026-11-25 13:49 [PATCH net] sctp: keep the port of a bound socket when adding an address fails Kyuwon Cho
  2026-11-25 18:52 ` Xin Long
@ 2026-12-02  2:40 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-12-02  2:40 UTC (permalink / raw)
  To: Kyuwon Cho
  Cc: marcelo.leitner, lucien.xin, davem, edumazet, kuba, pabeni,
	willemb, horms, nhorman, maowenan, linux-sctp, netdev

Hello:

This patch was applied to netdev/net.git (main)
by Willem de Bruijn <willemb@google.com>:

On Wed, 25 Nov 2026 22:49:00 +0900 you wrote:
> 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():
> 
> [...]

Here is the summary with links:
  - [net] sctp: keep the port of a bound socket when adding an address fails
    https://git.kernel.org/netdev/net/c/a0c304e6db0f

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02  2:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 13:49 [PATCH net] sctp: keep the port of a bound socket when adding an address fails Kyuwon Cho
2026-11-25 18:52 ` Xin Long
2026-12-02  2:40 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox