Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Kyuwon Cho
Wed, 25 Nov 2026 13:49
[PATCH net] sctp: keep the port of a bound socket when adding an address fails
Author: Kyuwon Cho
Date: Wed, 25 Nov 2026 13:49
Date: Wed, 25 Nov 2026 13:49
111 lines
14539 bytes
14539 bytes
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
Re: [PATCH net] sctp: keep the port of a bound socket when adding an address fails
Author: Xin Long
Date: Wed, 25 Nov 2026 18:52
Date: Wed, 25 Nov 2026 18:52
122 lines
16790 bytes
16790 bytes
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>
Re: [PATCH net] sctp: keep the port of a bound socket when adding an address fails
Author: patchwork-bot+netdevbpf
Date: Wed, 02 Dec 2026 02:40
Date: Wed, 02 Dec 2026 02:40
27 lines
9016 bytes
9016 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads