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>,
	Simon Horman <horms@kernel.org>, Jun Yang <junvyyang@tencent.com>,
	linux-sctp@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [BUG] sctp: auth: NULL asoc->shkey after a failed SCTP_CMD_ASSOC_SHKEY makes close() oops
Date: Wed, 18 Nov 2026 19:04:25 -0800 (PST)	[thread overview]
Message-ID: <179505710266.3511678.14017674052424733262@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3714 bytes --]

Hi Marcelo, Xin,

While testing v7.4-rc3 with an LLM-guided test generator that we are
developing as a research prototype, we found that an SCTP association
whose shared key could not be set up still builds authenticated chunks,
with a NULL key. Closing such a socket with SO_LINGER {1, 0} then
oopses in sctp_auth_shkey_hold(); here as an unprivileged user:

  BUG: KASAN: null-ptr-deref in sctp_auth_shkey_hold (include/linux/refcount.h:366 net/sctp/auth.c:113)
  Write of size 4 at addr 0000000000000018 by task test/115
  CPU: 1 UID: 65534 PID: 115 Comm: test Not tainted 7.4.0-rc3 #1 PREEMPT(lazy)
  Call Trace:
   <TASK>
   sctp_auth_shkey_hold (include/linux/refcount.h:366 net/sctp/auth.c:113)
   sctp_control_set_owner_w (net/sctp/sm_make_chunk.c:112)
   sctp_make_abort_user (net/sctp/sm_make_chunk.c:1492 net/sctp/sm_make_chunk.c:953 net/sctp/sm_make_chunk.c:1016)
   sctp_close (net/sctp/socket.c:1564)
   inet_release (net/ipv4/af_inet.c:443)
   __sock_release (net/socket.c:735)
   sock_close (net/socket.c:1529)
   __fput (fs/file_table.c:512)
   fput_close_sync (fs/file_table.c:618)
   __x64_sys_close (fs/open.c:1574 fs/open.c:1597 fs/open.c:1584)
   do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
   </TASK>

When the INIT-ACK arrives, sctp_sf_do_5_1C_ack() queues
SCTP_CMD_PEER_INIT, which records that the peer supports AUTH and which
chunks it wants authenticated, and then SCTP_CMD_ASSOC_SHKEY:

	case SCTP_CMD_ASSOC_SHKEY:
		error = sctp_auth_asoc_init_active_key(asoc,
					GFP_ATOMIC);
		break;

sctp_auth_asoc_init_active_key() returns -ENOMEM before it sets
asoc->shkey when the secret cannot be allocated:

	secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
	if (!secret)
		return -ENOMEM;
	...
	asoc->shkey = ep_key;

sctp_cmd_interpreter() then drops the remaining commands, but the
association stays, with peer.auth_capable set and asoc->shkey NULL. If
the peer listed ABORT among the chunks to authenticate, the ABORT that
close() builds has chunk->auth set, and sctp_control_set_owner_w()
takes a reference on the NULL key:

	if (chunk->auth) {
		if (!chunk->shkey)
			chunk->shkey = asoc->shkey;
		sctp_auth_shkey_hold(chunk->shkey);
	}

The peer can be remote; the local side needs one GFP_ATOMIC allocation
to fail while it processes the INIT-ACK. The reproducer makes it fail
with fail-nth (CONFIG_FAULT_INJECTION) and runs as an unprivileged user
(uid 65534) in its own user and network namespace: a listener on
127.0.0.1 with AUTH enabled and ABORT in its SCTP_AUTH_CHUNK list, and
a client that connects with fail-nth set to 1..60 and closes each
socket with SO_LINGER {1, 0}. On an x86_64 QEMU guest with 2 vCPUs and
KASAN (gcc 13.3), it oopses every time.

The reference on the shared key comes from 1b1e0bc99474 ("sctp: add
refcnt support for sh_key"). ad1320865b18 ("sctp: hold shkey across
socket migration") and 52ffeaa358d1 ("sctp: re-point retained control
chunks on association migration"), both in v7.4-rc3, handle the
migration paths and not this one.

Checked against v7.4-rc3 (aebb72744764); this code is the same in net
(2a324a58a084).

A patch is attached (git format-patch against v7.4-rc3; it applies to
net (2a324a58a084)):

  0001-sctp-auth-don-t-authenticate-chunks-without-an-assoc.patch

It makes sctp_auth_send_cid() return false while asoc->shkey is NULL, so
chunks are not marked for authentication until the association has a
key. It was written with LLM assistance; we reviewed and tested it. With
it, the reproducer no longer triggers the report.

The reproducer (repro.c), the kernel config and the patch are attached.

Thanks,
Kyuwon Cho

[-- Attachment #2: repro.c --]
[-- Type: text/plain, Size: 2911 bytes --]

// 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 <arpa/inet.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

#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;
}

[-- Attachment #3: config.gz --]
[-- Type: application/gzip, Size: 38966 bytes --]

[-- Attachment #4: 0001-sctp-auth-don-t-authenticate-chunks-without-an-assoc.patch --]
[-- Type: text/plain, Size: 1705 bytes --]

>From 2e554096914c2c0823d4f2e396cdf9fcece2663e Mon Sep 17 00:00:00 2001
From: Kyuwon Cho <kyuwoncho18@gmail.com>
Date: Thu, 19 Nov 2026 11:36:52 +0900
Subject: [PATCH net] sctp: auth: don't authenticate chunks without an
 association key

When the INIT-ACK is processed, SCTP_CMD_PEER_INIT marks the peer as
AUTH capable and SCTP_CMD_ASSOC_SHKEY then sets up the association key.
If sctp_auth_asoc_init_active_key() fails to allocate the secret, it
returns -ENOMEM before setting asoc->shkey, but the association stays,
with peer.auth_capable set. A chunk that the peer wants authenticated,
such as the ABORT built by close() with SO_LINGER {1, 0}, then takes a
reference on the NULL key:

  BUG: KASAN: null-ptr-deref in sctp_auth_shkey_hold (net/sctp/auth.c:113)
  Write of size 4 at addr 0000000000000018
  Call Trace:
   sctp_auth_shkey_hold (net/sctp/auth.c:113)
   sctp_control_set_owner_w (net/sctp/sm_make_chunk.c:112)
   sctp_make_abort_user (net/sctp/sm_make_chunk.c:1016)
   sctp_close (net/sctp/socket.c:1564)

Don't mark chunks for authentication while the association has no key.

Fixes: 1b1e0bc99474 ("sctp: add refcnt support for sh_key")
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
 net/sctp/auth.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/sctp/auth.c b/net/sctp/auth.c
index cbeb8a87a17a..5f8e110929d4 100644
--- a/net/sctp/auth.c
+++ b/net/sctp/auth.c
@@ -660,7 +660,7 @@ int sctp_auth_send_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
 	if (!asoc)
 		return 0;
 
-	if (!asoc->peer.auth_capable)
+	if (!asoc->peer.auth_capable || !asoc->shkey)
 		return 0;
 
 	return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
-- 
2.43.0


                 reply	other threads:[~2026-11-19  3:04 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=179505710266.3511678.14017674052424733262@gmail.com \
    --to=kyuwoncho18@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=junvyyang@tencent.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sctp@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=marcelo.leitner@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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