sctp: reject forged cookie peer_addr with unknown address family
Subsystems (3)
PATCH v2 1/1 sctp: reject forged cookie peer_addr with unknown address family
When cookie authentication is disabled, COOKIE-ECHO peer_addr is attacker-controlled. An invalid sa_family made sctp_get_af_specific() return NULL and crash in sctp_transport_init() on af_specific->sockaddr_len. Validate it with af->addr_valid() in sctp_unpack_cookie(), which also rejects a mismatched family, e.g. an AF_INET6 peer_addr on an IPv4 socket that would later crash in sctp_v6_get_dst() on inet6_sk(sk)->opt, and bail in sctp_transport_new() if af_specific is missing. Validate a copy of peer_addr, since addr_valid() rewrites a v4-mapped v6 address in place and the cookie still lives in the received skb, which is not guaranteed to be unshared.
For that, sctp_v6_addr_valid() has to return 0 when it is called with a socket that is not PF_INET6, so that IPv6 addresses are never used on IPv4 sockets. As addr_valid() now also checks sk_family and ipv6_only_sock, simplify the address handling in sctp_process_param() with it, as it additionally filters out non-unicast addresses. As a side effect, a v4-mapped address in an IPv6 address parameter is now normalised to AF_INET before the transport is created, matching what the socket API paths already do.
Also add the missing !af check in sctp_process_init(), as sctp_get_af_specific(AF_INET6) returns NULL on CONFIG_IPV6=n builds.
BUG: KASAN: null-ptr-deref in sctp_transport_new+0xa7/0x350 Read of size 4 at addr 00000000000000b4 by task poc/682 Call Trace: <IRQ> sctp_transport_new+0xa7/0x350 sctp_assoc_add_peer+0x153/0x850 sctp_process_init+0xf9/0x1180 sctp_sf_do_5_1D_ce+0x464/0xbc0 sctp_do_sm+0x114/0x2990 sctp_endpoint_bh_rcv+0x280/0x430 sctp_inq_push+0xdd/0x100 sctp_rcv+0x17f5/0x1ae0 sctp4_rcv+0x2b/0x40 ip_protocol_deliver_rcu+0x25b/0x270 ip_local_deliver+0xd1/0xe0 </IRQ> Kernel panic - not syncing: Fatal exception in interrupt
---
Changes since v1 [1]:
- validate the forged cookie peer_addr with af->addr_valid() instead of a family whitelist, and make sctp_v6_addr_valid() reject non-PF_INET6 sockets, so a forged AF_INET6 cookie can no longer crash an IPv4 socket in sctp_v6_get_dst() (found by Sashiko)
- simplify sctp_process_param() address handling with addr_valid(), as addr_valid() now also checks sk_family and ipv6_only_sock
- add the missing !af check in sctp_process_init() for CONFIG_IPV6=n builds (suggested by Xin Long)
[1] https://lore.kernel.org/netdev/[email protected]/
net/sctp/ipv6.c | 3 +++ net/sctp/sm_make_chunk.c | 25 +++++++++++++++++-------- net/sctp/transport.c | 3 +++ 3 files changed, 23 insertions(+), 8 deletions(-)
net/sctp/ipv6.c +3 -0
@@ -734,6 +734,9 @@ static int sctp_v6_addr_valid(union sctp_addr *addr, {int ret =ipv6_addr_type (&addr->v6.sin6_addr); +if (sp &&sctp_opt2sk (sp)->sk_family !=PF_INET6 ) +return 0 ; +/* Support v4-mapped-v6 address. */ if (ret ==IPV6_ADDR_MAPPED ) {/* Note: This routine is used in input, so v4-mapped-v6
net/sctp/sm_make_chunk.c +17 -8
@@ -1732,7 +1732,9 @@ struct sctp_association *sctp_unpack_cookie(struct sctp_cookie *bear_cookie;struct sctp_chunkhdr *ch;unsigned int len, chlen; +union sctp_addr paddr;enum sctp_scope scope; +struct sctp_af *af;ktime_t kt;/* Header size is static data prior to the actual cookie, including
@@ -1841,6 +1843,15 @@ struct sctp_association *sctp_unpack_cookie( goto fail; } +/* peer_addr is peer-controlled when cookie authentication is +* disabled. Validate a copy, as addr_valid() may rewrite a +* v4-mapped address in place. +*/ + paddr = bear_cookie->peer_addr; + af =sctp_get_af_specific (paddr.sa.sa_family); +if (!af || !af->addr_valid (&paddr,sctp_sk (ep->base.sk),NULL )) + goto malformed; +/* Make a new base association. */ scope =sctp_scope (sctp_source (chunk)); retval =sctp_association_new (ep, ep->base.sk, scope, gfp);
@@ -2381,6 +2392,8 @@ int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk, (param.p->type == SCTP_PARAM_IPV4_ADDRESS || param.p->type == SCTP_PARAM_IPV6_ADDRESS)) { af =sctp_get_af_specific (param_type2af (param.p->type)); +if (!af) +continue ;if (!af->from_addr_param (&addr, param.addr, chunk->sctp_hdr->source,0 ))continue ;
@@ -2554,18 +2567,14 @@ static int sctp_process_param(struct sctp_association *asoc,*/ switch (param.p->type) {case SCTP_PARAM_IPV6_ADDRESS : -if (PF_INET6 != asoc->base.sk->sk_family) -break ; - goto do_addr_param; -case SCTP_PARAM_IPV4_ADDRESS : -/* v4 addresses are not allowed on v6-only socket */ -if (ipv6_only_sock (asoc->base.sk)) -break ; -do_addr_param: af =sctp_get_af_specific (param_type2af (param.p->type)); +if (!af) +break ;if (!af->from_addr_param (&addr, param.addr,htons (asoc->peer.port),0 ))break ; +if (!af->addr_valid (&addr,sctp_sk (asoc->base.sk),NULL )) +break ; scope =sctp_scope (peer_addr);if (sctp_in_scope (net, &addr, scope))if (!sctp_assoc_add_peer (asoc, &addr, gfp,SCTP_UNCONFIRMED ))
net/sctp/transport.c +3 -0
@@ -92,6 +92,9 @@ struct sctp_transport *sctp_transport_new(struct net *net, {struct sctp_transport *transport; +if (!sctp_get_af_specific (addr->sa.sa_family)) +return NULL ; + transport =kzalloc_obj (*transport, gfp);if (!transport)return NULL ;
Signature · 1 line
2.43.0
Xin Long
maintainerQuoted 94 lines · Xingyuan Mo
When cookie authentication is disabled, COOKIE-ECHO
peer_addris attacker-controlled. An invalidsa_familymadesctp_get_af_specific()return NULL and crash insctp_transport_init()onaf_specific->sockaddr_len. Validate it withaf->addr_valid()insctp_unpack_cookie(), which also rejects a mismatched family, e.g. anAF_INET6peer_addron an IPv4 socket that would later crash insctp_v6_get_dst()on inet6_sk(sk)->opt, and bail insctp_transport_new()ifaf_specificis missing. Validate a copy ofpeer_addr, sinceaddr_valid()rewrites a v4-mapped v6 address in place and the cookie still lives in the received skb, which is not guaranteed to be unshared.For that,
sctp_v6_addr_valid()has to return 0 when it is called with a socket that is notPF_INET6, so that IPv6 addresses are never used on IPv4 sockets. Asaddr_valid()now also checkssk_familyandipv6_only_sock, simplify the address handling insctp_process_param()with it, as it additionally filters out non-unicast addresses. As a side effect, a v4-mapped address in an IPv6 address parameter is now normalised toAF_INETbefore the transport is created, matching what the socket API paths already do.Also add the missing !af check in
sctp_process_init(), assctp_get_af_specific(AF_INET6)returns NULL onCONFIG_IPV6=nbuilds.BUG: KASAN: null-ptr-deref in sctp_transport_new+0xa7/0x350 Read of size 4 at addr 00000000000000b4 by task poc/682 Call Trace: <IRQ> sctp_transport_new+0xa7/0x350 sctp_assoc_add_peer+0x153/0x850 sctp_process_init+0xf9/0x1180 sctp_sf_do_5_1D_ce+0x464/0xbc0 sctp_do_sm+0x114/0x2990 sctp_endpoint_bh_rcv+0x280/0x430 sctp_inq_push+0xdd/0x100 sctp_rcv+0x17f5/0x1ae0 sctp4_rcv+0x2b/0x40 ip_protocol_deliver_rcu+0x25b/0x270 ip_local_deliver+0xd1/0xe0 </IRQ> Kernel panic - not syncing: Fatal exception in interruptFixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Assisted-by opencode:deepseek-v4Signed-off-by Xingyuan Mo <[email protected]>---
Changes since v1 [1]:
- validate the forged cookiepeer_addrwithaf->addr_valid()instead of a family whitelist, and makesctp_v6_addr_valid()reject non-PF_INET6 sockets, so a forgedAF_INET6cookie can no longer crash an IPv4 socket insctp_v6_get_dst()(found by Sashiko)
- simplifysctp_process_param()address handling withaddr_valid(), asaddr_valid()now also checkssk_familyandipv6_only_sock
- add the missing !af check insctp_process_init()forCONFIG_IPV6=nbuilds (suggested by Xin Long)[1] https://lore.kernel.org/netdev/[email protected]/
net/sctp/ipv6.c | 3 +++ net/sctp/sm_make_chunk.c | 25 +++++++++++++++++-------- net/sctp/transport.c | 3 +++ 3 files changed, 23 insertions(+), 8 deletions(-)net/sctp/ipv6.c +3
-0@@ -734,6 +734,9 @@ static int sctp_v6_addr_valid(union sctp_addr *addr, {int ret =ipv6_addr_type (&addr->v6.sin6_addr); +if (sp &&sctp_opt2sk (sp)->sk_family !=PF_INET6 ) +return 0 ; +/* Support v4-mapped-v6 address. */ if (ret ==IPV6_ADDR_MAPPED ) {/* Note: This routine is used in input, so v4-mapped-v6 net/sctp/sm_make_chunk.c +2
-0@@ -1732,7 +1732,9 @@ struct sctp_association *sctp_unpack_cookie(struct sctp_cookie *bear_cookie;struct sctp_chunkhdr *ch;unsigned int len, chlen; +union sctp_addr paddr;enum sctp_scope scope; +struct sctp_af *af;ktime_t kt;/* Header size is static data prior to the actual cookie, including
net/sctp/sm_make_chunk.c struct sctp_association *sctp_unpack_cookie( · line 1849
Quoted 7 lines
net/sctp/sm_make_chunk.c +4
-0@@ -1841,6 +1843,15 @@ struct sctp_association *sctp_unpack_cookie( goto fail; } +/* peer_addr is peer-controlled when cookie authentication is +* disabled. Validate a copy, as addr_valid() may rewrite a +* v4-mapped address in place. +*/
af->addr_valid() is more like ensuring the address is a unicast address and the socket supports the address family. You can either improve the inline comment above or just remove it.
Quoted 9 lines
net/sctp/sm_make_chunk.c +5
-0+ paddr = bear_cookie->peer_addr; + af =sctp_get_af_specific (paddr.sa.sa_family); +if (!af || !af->addr_valid (&paddr,sctp_sk (ep->base.sk),NULL )) + goto malformed; +/* Make a new base association. */ scope =sctp_scope (sctp_source (chunk)); retval =sctp_association_new (ep, ep->base.sk, scope, gfp);
net/sctp/sm_make_chunk.c int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk, · line 2399
Quoted 8 lines
net/sctp/sm_make_chunk.c +2
-0@@ -2381,6 +2392,8 @@ int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk, (param.p->type == SCTP_PARAM_IPV4_ADDRESS || param.p->type == SCTP_PARAM_IPV6_ADDRESS)) { af =sctp_get_af_specific (param_type2af (param.p->type)); +if (!af) +continue ;if (!af->from_addr_param (&addr, param.addr, chunk->sctp_hdr->source,0 ))continue ;
You can merge these 2 checks with (!af || !af->from_addr_param()).
Quoted 8 lines
net/sctp/sm_make_chunk.c +0
-4@@ -2554,18 +2567,14 @@ static int sctp_process_param(struct sctp_association *asoc,*/ switch (param.p->type) {case SCTP_PARAM_IPV6_ADDRESS : -if (PF_INET6 != asoc->base.sk->sk_family) -break ; - goto do_addr_param; -
net/sctp/sm_make_chunk.c static int sctp_process_param(struct sctp_association *asoc, · line 2575
Quoted 10 lines
net/sctp/sm_make_chunk.c +2
-4case SCTP_PARAM_IPV4_ADDRESS : -/* v4 addresses are not allowed on v6-only socket */ -if (ipv6_only_sock (asoc->base.sk)) -break ; -do_addr_param: af =sctp_get_af_specific (param_type2af (param.p->type)); +if (!af) +break ;if (!af->from_addr_param (&addr, param.addr,htons (asoc->peer.port),0 ))break ;
Same here.
Quoted 10 lines
net/sctp/sm_make_chunk.c +2
-0+if (!af->addr_valid (&addr,sctp_sk (asoc->base.sk),NULL )) +break ; scope =sctp_scope (peer_addr);if (sctp_in_scope (net, &addr, scope))if (!sctp_assoc_add_peer (asoc, &addr, gfp,SCTP_UNCONFIRMED ))net/sctp/transport.c +0
-0
net/sctp/transport.c struct sctp_transport *sctp_transport_new(struct net *net, · line 97
Quoted 6 lines
net/sctp/transport.c +3
-0@@ -92,6 +92,9 @@ struct sctp_transport *sctp_transport_new(struct net *net, {struct sctp_transport *transport; +if (!sctp_get_af_specific (addr->sa.sa_family)) +return NULL ; +
Any reason why we need this check?
Thanks.
Quoted 6 lines
net/sctp/transport.c +0
-1transport =kzalloc_obj (*transport, gfp);if (!transport)return NULL ; --2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 2 · Low: 1
- [Medium] sctp_unpack_cookie() validates a *copy* (paddr) of the attacker-controlled bear_cookie->peer_addr and then discards it.
- [Medium] The new if (sp && sctp_opt2sk(sp)->sk_family != PF_INET6) return 0; is placed *before* the IPV6_ADDR_MAPPED handling in…
- [Low] A minimal crash fix (validate the attacker-controlled cookie peer_addr, guard the missing af_specific) is bundled with a semantic…
--
sctp: reject forged cookie peer_addr with unknown address family
This validates the attacker-controlled COOKIE-ECHO peer_addr with af->addr_valid() in sctp_unpack_cookie(), makes sctp_v6_addr_valid() return 0 for non-PF_INET6 sockets, reuses addr_valid() in sctp_process_param(), and adds missing sctp_get_af_specific() NULL checks in sctp_process_init() and sctp_transport_new().
Previous posting: https://lore.kernel.org/netdev/[email protected]/
Quoted 5 lines
net/sctp/ipv6.c +0
-0
net/sctp/ipv6.c static int sctp_v6_addr_valid(union sctp_addr *addr, · line 742
Quoted 9 lines
net/sctp/ipv6.c +3
-0@@ -734,6 +734,9 @@ static int sctp_v6_addr_valid(union sctp_addr *addr, {int ret =ipv6_addr_type (&addr->v6.sin6_addr); +if (sp &&sctp_opt2sk (sp)->sk_family !=PF_INET6 ) +return 0 ; +/* Support v4-mapped-v6 address. */ if (ret ==IPV6_ADDR_MAPPED ) {/* Note: This routine is used in input, so v4-mapped-v6
[Severity: Medium]
The new test runs before the IPV6_ADDR_MAPPED branch, so it also rejects v4-mapped addresses that used to be normalised to AF_INET and accepted. Can this change the return value of two existing socket option paths that take the address family straight from user data?
sctp_setsockopt_bindx() only checks sctp_get_af_specific() on the buffer walk (there is no sctp_sockaddr_af()/af_supported() gate on the remove path), and then sctp_bindx_rem() does:
af = sctp_get_af_specific(sa_addr->sa.sa_family);
if (!af) {
retval = -EINVAL;
goto err_bindx_rem;
}
if (!af->addr_valid(sa_addr, sp, NULL)) {
retval = -EADDRNOTAVAIL;sctp_setsockopt_primary_addr() has the same shape:
af = sctp_get_af_specific(prim->sspp_addr.ss_family); if (!af) return -EINVAL; if (!af->addr_valid((union sctp_addr *)&prim->sspp_addr, sp, NULL)) return -EADDRNOTAVAIL;
On a PF_INET socket with a v4-mapped AF_INET6 sockaddr, the old code took the IPV6_ADDR_MAPPED branch, rewrote the kernel copy with sctp_v6_map_v4() and returned sctp_v4_addr_valid() == 1, so sctp_del_bind_addr() and sctp_assoc_lookup_laddr() matched the real AF_INET address. With the new test firing first (sk_family == PF_INET), do SCTP_BINDX_REM_ADDR and SCTP_SET_PEER_PRIMARY_ADDR now return -EADDRNOTAVAIL where they used to succeed?
Note that simply moving the new test below the IPV6_ADDR_MAPPED block does not look sufficient on its own, given the cookie path validates a throw-away copy (see below): a v4-mapped AF_INET6 cookie peer_addr on an IPv4 socket would then be accepted un-normalised and reach sctp_v6_get_dst(), where inet6_sk() returns NULL for an AF_INET socket. Would restricting the socket-family test to the new caller, or propagating the normalised address to the consumer, avoid touching the setsockopt paths?
Quoted 4 lines
net/sctp/sm_make_chunk.c +0
-0
[ ... ]
net/sctp/sm_make_chunk.c struct sctp_association *sctp_unpack_cookie( · line 1854
Quoted 13 lines
net/sctp/sm_make_chunk.c +9
-0@@ -1841,6 +1843,15 @@ struct sctp_association *sctp_unpack_cookie( goto fail; } +/* peer_addr is peer-controlled when cookie authentication is +* disabled. Validate a copy, as addr_valid() may rewrite a +* v4-mapped address in place. +*/ + paddr = bear_cookie->peer_addr; + af =sctp_get_af_specific (paddr.sa.sa_family); +if (!af || !af->addr_valid (&paddr,sctp_sk (ep->base.sk),NULL )) + goto malformed; +
[Severity: Medium]
Is the value that gets validated here the value that is actually used?
paddr is a local copy and is dropped after the check, so only the accept or reject decision survives. For an AF_INET6 peer_addr holding a v4-mapped address on a dual-stack (PF_INET6, non v6only) listener, sctp_v6_addr_valid() accepts it only because it rewrites the copy:
net/sctp/ipv6.c:sctp_v6_addr_valid() {
if (ret == IPV6_ADDR_MAPPED) {
if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
return 0;
sctp_v6_map_v4(addr);
return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
}
/* Is this a non-unicast address */
if (!(ret & IPV6_ADDR_UNICAST))
return 0;Without that rewrite the same address would be rejected by the !(ret & IPV6_ADDR_UNICAST) test.
The AF_INET6 form is what the rest of the path consumes:
memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
and sctp_sf_do_5_1D_ce() feeds the raw in-skb cookie address to sctp_process_init():
if (!sctp_process_init(new_asoc, chunk, &chunk->subh.cookie_hdr->c.peer_addr, peer_init, GFP_ATOMIC))
which reaches sctp_assoc_add_peer() -> sctp_transport_new(). Does the resulting transport therefore end up with af_specific = sctp_af_v6_specific and the peer-supplied sin6_flowinfo as its flowlabel, from a representation that was never validated as-is?
if (addr->sa.sa_family == AF_INET6) {
__be32 info = addr->v6.sin6_flowinfo;
if (info) {
peer->flowlabel = ntohl(info & IPV6_FLOWLABEL_MASK);The new sctp_get_af_specific() guard in sctp_transport_new() only rejects unknown families, so it does not catch this case either.
The sibling path fixed in the same patch (sctp_process_param(), below) keeps the normalised AF_INET address it hands to sctp_assoc_add_peer(). Can the two peer-controlled feeders of sctp_assoc_add_peer() then disagree on the stored representation, so sctp_cmp_addr_exact() and sctp_assoc_lookup_paddr() produce two transports for one peer address, and asoc->c.peer_addr keeps a family that does not match the wire?
Would propagating paddr into retval->c.peer_addr (with sctp_sf_do_5_1D_ce() using the association copy), or rejecting v4-mapped cookie addresses outright, be a way to keep the validated and the used value identical?
[ ... ]
Quoted 13 lines
net/sctp/sm_make_chunk.c +0
-8@@ -2554,18 +2567,14 @@ static int sctp_process_param(struct sctp_association *asoc,*/ switch (param.p->type) {case SCTP_PARAM_IPV6_ADDRESS : -if (PF_INET6 != asoc->base.sk->sk_family) -break ; - goto do_addr_param; -case SCTP_PARAM_IPV4_ADDRESS : -/* v4 addresses are not allowed on v6-only socket */ -if (ipv6_only_sock (asoc->base.sk)) -break ; -do_addr_param:
net/sctp/sm_make_chunk.c static int sctp_process_param(struct sctp_association *asoc, · line 2580
Quoted 10 lines
net/sctp/sm_make_chunk.c +4
-0af =sctp_get_af_specific (param_type2af (param.p->type)); +if (!af) +break ;if (!af->from_addr_param (&addr, param.addr,htons (asoc->peer.port),0 ))break ; +if (!af->addr_valid (&addr,sctp_sk (asoc->base.sk),NULL )) +break ; scope =sctp_scope (peer_addr);if (sctp_in_scope (net, &addr, scope))if (!sctp_assoc_add_peer (asoc, &addr, gfp,SCTP_UNCONFIRMED ))
[Severity: Low]
This isn't a bug, but could the crash fix and this rework be split into separate patches?
The crash fix itself is the peer_addr validation in sctp_unpack_cookie() plus the missing af_specific checks. This hunk additionally changes what happens on the wire for INIT and INIT-ACK address parameters:
- a v4-mapped value in an SCTP_PARAM_IPV6_ADDRESS is now rewritten in place to AF_INET by sctp_v6_map_v4() before sctp_assoc_add_peer(), so the created transport's family, its af_specific and route selection, and getpaddrs() output change
- a v4-mapped value on an ipv6-only socket and IPv6 non-unicast parameters are now dropped instead of creating a transport, where before sctp_v6_scope() could return SCTP_SCOPE_LINK for a multicast parameter and sctp_in_scope() could accept it
The change to sctp_v6_addr_valid() also alters the meaning of the generic sctp_af::addr_valid() callback for all existing callers: its comment says "Return 0 - If the address is a non-unicast or an illegal address", and it is now also a socket-policy predicate.
With Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") the whole series will be picked up by every stable tree, including the normalisation change and the setsockopt behaviour change noted above. Would keeping the fix minimal and sending the addr_valid() reuse as a follow-up on top make the stable backport safer?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260915212917.3775248-1-hdthky0%40gmail.com