NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

kern/61080: Kernel panic (NULL dereference) in sctp_send_cookie_echo on IPv6 INIT-ACK



>Number:         61080
>Category:       kern
>Synopsis:       Kernel panic (NULL dereference) in sctp_send_cookie_echo on IPv6 INIT-ACK
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 26 14:15:00 +0000 2026
>Originator:     Thomas Dreibholz
>Release:        11.1
>Organization:
Simula Metropolitan Centre for Digital Engineering
>Environment:
NetBSD localhost 11.1 NetBSD 11.1 (CUSTOM_SCTP) #0: Wed Nov 25 14:55:32 UTC 2026  root@:/usr/src/sys/arch/amd64/compile/CUSTOM_SCTP amd64
>Description:
On a freshly installed NetBSD 11.1, with SCTP-enabled customly built kernel (i.e. built with "options SCTP"), I made a build of the latest NetPerfMeter (https://www.nntb.no/~dreibh/netperfmeter/) and ran its CTest test suite. This led to a reproducible kernel crash and reboot, triggered by an unprivileged user without any special permissions.

Basically, I ran this:
sudo pkgin -y install bash cmake figlet gcc15 getopt gettext git gmake joe  ghostscript libiberty qpdf p5-PDF-API2
mkdir -p ~/src
cd ~/src
git clone https://github.com/dreibh/netperfmeter
cd netperfmeter
autogen.sh
ctest .

After reboot, I performed these steps to provide diagnosis information:

cd /var/crash
sudo -v
sudo gunzip -c /var/crash/netbsd.0.gz > /tmp/netbsd.0
sudo gunzip -c /var/crash/netbsd.0.core.gz > /tmp/netbsd.0.core

sudo dmesg -M /tmp/netbsd.0.core -N /tmp/netbsd.0 | tail -n 25
------ OUTPUT ------
[    14,370542] wsdisplay0: screen 4 added (default, vt100 emulation)
[    14,560516] /altroot/altboot: replaying log to disk
[   730,833926] fatal page fault in supervisor mode
[   730,833926] trap type 6 code 0x2 rip 0xffffffff80aa38a4 cs 0x8 rflags 0x10202 cr2 0x178 ilevel 0x4 rsp 0xffffb9026783c450
[   730,833926] curlwp 0xffffab0e1365b400 pid 0.3 lowest kstack 0xffffb902678382c0
[   730,833926] panic: trap
[   730,833926] cpu0: Begin traceback...
[   730,843912] vpanic() at netbsd:vpanic+0x171
[   730,843912] panic() at netbsd:panic+0x3c
[   730,853892] trap() at netbsd:trap+0xb32
[   730,853892] --- trap (number 6) ---
[   730,853892] sctp_send_cookie_echo() at netbsd:sctp_send_cookie_echo+0x134
[   730,853892] sctp_process_control.constprop.0() at netbsd:sctp_process_control.constprop.0+0x744
[   730,853892] sctp_common_input_processing() at netbsd:sctp_common_input_processing+0xa8
[   730,853892] sctp6_input() at netbsd:sctp6_input+0x18f
[   730,863881] ip6intr() at netbsd:ip6intr+0x7d1
[   730,863881] softint_dispatch() at netbsd:softint_dispatch+0x95
[   730,863881] DDB lost frame for netbsd:Xsoftintr+0x4c, trying 0xffffb9026783d0f0
[   730,863881] Xsoftintr() at netbsd:Xsoftintr+0x4c
[   730,863881] --- interrupt ---
[   730,863881] 0:
[   730,863881] cpu0: End traceback...

[   730,863881] dumping to dev 168,4 (offset=8, size=2092258):
[   730,863881] dump 
------

sudo gdb -batch \
      -ex "target kvm /tmp/netbsd.0.core" \
      -ex "bt" \
      -ex "frame 6" \
      -ex "info registers" \
      -ex "x/15i sctp_send_cookie_echo+0x114" \
      /tmp/netbsd.0 2>&1

------ OUTPUT ------
warning: BFD: warning: /tmp/netbsd.0 has a section extending past end of file
0xffffffff802398c5 in cpu_reboot ()
#0  0xffffffff802398c5 in cpu_reboot ()
#1  0xffffffff80d2b971 in kern_reboot ()
#2  0xffffffff80d6109d in vpanic ()
#3  0xffffffff80d61172 in panic ()
#4  0xffffffff8023c2ca in trap ()
#5  0xffffffff80234a7b in alltraps ()
#6  0xffffffff80aa38a4 in sctp_send_cookie_echo ()
#7  0xffffffff80a9be83 in sctp_process_control.constprop ()
#8  0xffffffff80a9da79 in sctp_common_input_processing ()
#9  0xffffffff80ae0e0c in sctp6_input ()
#10 0xffffffff80accc61 in ip6intr ()
#11 0xffffffff80d36252 in softint_dispatch ()
#12 0xffffffff802347bc in Xsoftintr ()
#6  0xffffffff80aa38a4 in sctp_send_cookie_echo ()
rax            0xffffab0d32173038  -93401813405640
rbx            0xffffab0d047c1200  -93402578546176
rcx            0xffffffffffffff    72057594037927935
rdx            0x0                 0
rsi            0xffffffff          4294967295
rdi            0x4                 4
rbp            0xffffb9026783c480  0xffffb9026783c480
rsp            0xffffb9026783c450  0xffffb9026783c450
r8             0x400000000000000   288230376151711744
r9             0x400000000000000   288230376151711744
r10            0x14170f44f228c0ae  1447642594017853614
r11            0x0                 0
r12            0xffffab0d390c1038  -93401696694216
r13            0x9c                156
r14            0x14c               332
r15            0x34                52
rip            0xffffffff80aa38a4  0xffffffff80aa38a4 <sctp_send_cookie_echo+308>
eflags         0x10202             [ IF RF ]
cs             0x8                 8
ss             0x10                16
ds             0x0                 0
es             0x80d5cc14          -2133472236
fs             0x6783c440          1736688704
gs             0x2                 2
fs_base        <unavailable>
gs_base        <unavailable>
   0xffffffff80aa3884 <sctp_send_cookie_echo+276>:      movl   $0x0,0x38(%rax)
   0xffffffff80aa388b <sctp_send_cookie_echo+283>:      lea    0x40(%r12),%rdx
   0xffffffff80aa3890 <sctp_send_cookie_echo+288>:      mov    %rdx,0x48(%rax)
   0xffffffff80aa3894 <sctp_send_cookie_echo+292>:      mov    %rbx,0x60(%rax)
   0xffffffff80aa3898 <sctp_send_cookie_echo+296>:      mov    0x478(%r12),%rdx
   0xffffffff80aa38a0 <sctp_send_cookie_echo+304>:      mov    %rdx,0x68(%rax)
=> 0xffffffff80aa38a4 <sctp_send_cookie_echo+308>:      addl   $0x1,0x178(%rdx)
   0xffffffff80aa38ab <sctp_send_cookie_echo+315>:      mov    0x48(%rax),%rcx
   0xffffffff80aa38af <sctp_send_cookie_echo+319>:      mov    0x3a0(%rcx),%rdx
   0xffffffff80aa38b6 <sctp_send_cookie_echo+326>:      mov    %rdx,0x70(%rax)
   0xffffffff80aa38ba <sctp_send_cookie_echo+330>:      lea    0x70(%rax),%rsi
   0xffffffff80aa38be <sctp_send_cookie_echo+334>:      test   %rdx,%rdx
   0xffffffff80aa38c1 <sctp_send_cookie_echo+337>:      je     0xffffffff80aa38f7 <sctp_send_cookie_echo+391>
   0xffffffff80aa38c3 <sctp_send_cookie_echo+339>:      mov    %rsi,0x78(%rdx)
   0xffffffff80aa38c7 <sctp_send_cookie_echo+343>:      mov    0x48(%rax),%rdx
You can't do that without a process to debug.
------

awk '/^sctp_send_cookie_echo\(/,/^}/ { printf "%5d: %s\n", NR, $0 }'       
------ OUTPUT ------
/usr/src/sys/netinet/sctp_output.c
 6027: sctp_send_cookie_echo(struct mbuf *m,
 6028:                int offset,
 6029:                struct sctp_tcb *stcb,
 6030:                struct sctp_nets *net)
 6031: {
 6032:  /*
 6033:   * pull out the cookie and put it at the front of the control
 6034:   * chunk queue.
 6035:   */
 6036:  int at;
 6037:  struct mbuf *cookie, *mat;
 6038:  struct sctp_paramhdr parm, *phdr;
 6039:  struct sctp_chunkhdr *hdr;
 6040:  struct sctp_tmit_chunk *chk;
 6041:  uint16_t ptype, plen;
 6042:  /* First find the cookie in the param area */
 6043:  cookie = NULL;
 6044:  at = offset + sizeof(struct sctp_init_chunk);
 6045: 
 6046:  do {
 6047:          phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
 6048:          if (phdr == NULL) {
 6049:                  return (-3);
 6050:          }
 6051:          ptype = ntohs(phdr->param_type);
 6052:          plen = ntohs(phdr->param_length);
 6053:          if (ptype == SCTP_STATE_COOKIE) {
 6054:                  int pad;
 6055:                  /* found the cookie */
 6056:                  if ((pad = (plen % 4))) {
 6057:                          plen += 4 - pad;
 6058:                  }
 6059:                  cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
 6060:                  if (cookie == NULL) {
 6061:                          /* No memory */
 6062:                          return (-2);
 6063:                  }
 6064:                  break;
 6065:          }
 6066:          at += SCTP_SIZE32(plen);
 6067:  } while (phdr);
 6068:  if (cookie == NULL) {
 6069:          /* Did not find the cookie */
 6070:          return (-3);
 6071:  }
 6072:  /* ok, we got the cookie lets change it into a cookie echo chunk */
 6073: 
 6074:  /* first the change from param to cookie */
 6075:  hdr = mtod(cookie, struct sctp_chunkhdr *);
 6076:  hdr->chunk_type = SCTP_COOKIE_ECHO;
 6077:  hdr->chunk_flags = 0;
 6078:  /* now we MUST have a PKTHDR on it */
 6079:  if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
 6080:          /* we hope this happens rarely */
 6081:          MGETHDR(mat, M_DONTWAIT, MT_HEADER);
 6082:          if (mat == NULL) {
 6083:                  sctp_m_freem(cookie);
 6084:                  return (-4);
 6085:          }
 6086:          mat->m_len = 0;
 6087:          m_reset_rcvif(mat);
 6088:          mat->m_next = cookie;
 6089:          cookie = mat;
 6090:  }
 6091:  cookie->m_pkthdr.len = plen;
 6092:  /* get the chunk stuff now and place it in the FRONT of the queue */
 6093:  chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
 6094:  if (chk == NULL) {
 6095:          /* no memory */
 6096:          sctp_m_freem(cookie);
 6097:          return (-5);
 6098:  }
 6099:  sctppcbinfo.ipi_count_chunk++;
 6100:  sctppcbinfo.ipi_gencnt_chunk++;
 6101:  chk->send_size = cookie->m_pkthdr.len;
 6102:  chk->rec.chunk_id = SCTP_COOKIE_ECHO;
 6103:  chk->sent = SCTP_DATAGRAM_UNSENT;
 6104:  chk->snd_count = 0;
 6105:  chk->flags = 0;
 6106:  chk->asoc = &stcb->asoc;
 6107:  chk->data = cookie;
 6108:  chk->whoTo = chk->asoc->primary_destination;
 6109:  chk->whoTo->ref_count++;
 6110:  TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
 6111:  chk->asoc->ctrl_queue_cnt++;
 6112:  return (0);
 6113: }
------

With some analysis, it seems the issue is around line 6108: chk->asoc->primary_destination is NULL => chk->whoTo = NULL. Then, in the next line, the NULL-pointer gets dereferenced.
>How-To-Repeat:
I tracked down the problem to the minimal script below. This script only uses Perl to reproduce the issue, no further dependencies needed.


#!/bin/sh
# Minimal reproducer for NetBSD 11.1 IPv6 SCTP kernel panic
# (NULL dereference in sctp_send_cookie_echo+0x134 on INIT-ACK)
#
# WARNING: Running this on an affected NetBSD 11.1 kernel will trigger
# a kernel panic (fatal page fault in supervisor mode).

set -eu

PORT="${1:-13950}"
SRV_PID=""

cleanup() {
    if [ -n "${SRV_PID}" ] && kill -0 "${SRV_PID}" 2>/dev/null; then
        kill "${SRV_PID}" 2>/dev/null || true
        wait "${SRV_PID}" 2>/dev/null || true
    fi
}
trap cleanup EXIT INT TERM

# 1. Start dual-stack (IPV6_V6ONLY=0) SCTP IPv6 listener on [::]:$PORT
perl -e '
    use strict;
    use warnings;
    use Socket qw(AF_INET6 SOCK_STREAM SOL_SOCKET SO_REUSEADDR IN6ADDR_ANY sockaddr_in6);

    my $port         = $ARGV[0];
    my $IPPROTO_IPV6 = eval { Socket::IPPROTO_IPV6() } // 41;
    my $IPV6_V6ONLY  = eval { Socket::IPV6_V6ONLY() }  // 27;
    my $IPPROTO_SCTP = 132;

    socket(my $srv, AF_INET6, SOCK_STREAM, $IPPROTO_SCTP)
        or die "server socket(SCTP): $!\n";
    setsockopt($srv, SOL_SOCKET, SO_REUSEADDR, pack("i", 1))
        or die "server setsockopt(SO_REUSEADDR): $!\n";
    setsockopt($srv, $IPPROTO_IPV6, $IPV6_V6ONLY, pack("i", 0))
        or die "server setsockopt(IPV6_V6ONLY=0): $!\n";
    bind($srv, sockaddr_in6($port, IN6ADDR_ANY))
        or die "server bind([::]:$port): $!\n";
    listen($srv, 10)
        or die "server listen: $!\n";

    while (accept(my $conn, $srv)) {
        close($conn);
    }
' "${PORT}" &
SRV_PID=$!

sleep 1

# 2. Connect with default (IPV6_V6ONLY=1), unbound AF_INET6 SCTP socket to [::1]:$PORT
perl -e '
    use strict;
    use warnings;
    use Socket qw(AF_INET6 SOCK_STREAM sockaddr_in6 inet_pton);

    my $port         = $ARGV[0];
    my $IPPROTO_SCTP = 132;

    socket(my $cli, AF_INET6, SOCK_STREAM, $IPPROTO_SCTP)
        or die "client socket(SCTP): $!\n";
    connect($cli, sockaddr_in6($port, inet_pton(AF_INET6, "::1")))
        or die "client connect([::1]:$port): $!\n";
    close($cli);
' "${PORT}"

echo "Completed without panic."

>Fix:
Some idea:
For some reason, there is no primary path when trying to send the COOKIE_ECHO. On the server side, IPV6_V6ONLY=0. The client does not set IPV6_V6ONLY, so it should be at its default (IPV6_V6ONLY=1). If the server advertises IPv4 addresses to the client, the client must delete them. May be something goes wrong here, and no suitable primary path is chosen.




Home | Main Index | Thread Index | Old Index