sctp: Fix mangled IPv4 addresses on a IPv6 listening socket
Subsystems (3)
PATCH 1/1 sctp: Fix mangled IPv4 addresses on a IPv6 listening socket
sctp_v4_map_v6 was subtly writing and reading from members of a union in a way the clobbered data it needed to read before it read it.
Zeroing the v6 flowinfo overwrites the v4 sin_addr with 0, meaning that every place that calls sctp_v4_map_v6 gets ::ffff:0.0.0.0 as the result.
Reorder things to guarantee correct behaviour no matter what the union layout is.
This impacts user space clients that open an IPv6 SCTP socket and receive IPv4 connections. Prior to 299ee user space would see a sockaddr with AF_INET and a correct address, after 299ee the sockaddr is AF_INET6, but the address is wrong.
--- include/net/sctp/sctp.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
This bugfix should be a candidate for -stable
include/net/sctp/sctp.h +5 -2
@@ -571,11 +571,14 @@ static inline void sctp_v6_map_v4(union sctp_addr *addr)/* Map v4 address to v4-mapped v6 address */ static inline void sctp_v4_map_v6 (union sctp_addr *addr) { +__be16 port; + + port = addr->v4.sin_port; + addr->v6.sin6_addr.s6_addr32[3 ] = addr->v4.sin_addr.s_addr; + addr->v6.sin6_port = port; addr->v6.sin6_family =AF_INET6 ; addr->v6.sin6_flowinfo =0 ; addr->v6.sin6_scope_id =0 ; - addr->v6.sin6_port = addr->v4.sin_port; - addr->v6.sin6_addr.s6_addr32[3 ] = addr->v4.sin_addr.s_addr; addr->v6.sin6_addr.s6_addr32[0 ] =0 ; addr->v6.sin6_addr.s6_addr32[1 ] =0 ; addr->v6.sin6_addr.s6_addr32[2 ] =htonl (0x0000ffff );
Signature · 1 line
2.1.4
Quoted 2 lines · Jason Gunthorpe
sctp_v4_map_v6was subtly writing and reading from members of a union in a way the clobbered data it needed to read before
s/the/that/
Quoted 30 lines
it read it.
Zeroing the v6 flowinfo overwrites the v4
sin_addrwith 0, meaning that every place that callssctp_v4_map_v6gets ::ffff:0.0.0.0 as the result.Reorder things to guarantee correct behaviour no matter what the union layout is.
This impacts user space clients that open an IPv6 SCTP socket and receive IPv4 connections. Prior to 299ee user space would see a sockaddr with
AF_INETand a correct address, after 299ee the sockaddr isAF_INET6, but the address is wrong.Fixes 299ee123e198 (sctp: Fixup v4mapped behaviour to comply with Sock API)Signed-off-by Jason Gunthorpe <[email protected]>--- include/net/sctp/sctp.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)This bugfix should be a candidate for -stable
include/net/sctp/sctp.h +0
-0@@ -571,11 +571,14 @@ static inline void sctp_v6_map_v4(union sctp_addr *addr)/* Map v4 address to v4-mapped v6 address */ static inline void sctp_v4_map_v6 (union sctp_addr *addr) {
include/net/sctp/sctp.h static inline void sctp_v6_map_v4(union sctp_addr *addr) · line 578
Quoted 10 lines
include/net/sctp/sctp.h +5
-2+__be16 port; + + port = addr->v4.sin_port; + addr->v6.sin6_addr.s6_addr32[3 ] = addr->v4.sin_addr.s_addr; + addr->v6.sin6_port = port; addr->v6.sin6_family =AF_INET6 ; addr->v6.sin6_flowinfo =0 ; addr->v6.sin6_scope_id =0 ; - addr->v6.sin6_port = addr->v4.sin_port; - addr->v6.sin6_addr.s6_addr32[3 ] = addr->v4.sin_addr.s_addr;
Change looks good to me. You actually would only need to address the issue where the v4.sin_addr is copied before you overwrite/zero the flowinfo as only these two overlap in the union. Given that sockaddr_in and sockaddr_in6 are exported to uapi, they are immutable, but I see the point that union sctp_addr is kernel private - bad things happen if v4/v6 don't overlap the way anymore as they'd do now. ;)
Anyways:
Quoted 4 lines
include/net/sctp/sctp.h +0
-0addr->v6.sin6_addr.s6_addr32[0 ] =0 ; addr->v6.sin6_addr.s6_addr32[1 ] =0 ; addr->v6.sin6_addr.s6_addr32[2 ] =htonl (0x0000ffff );
From: Jason Gunthorpe
Quoted 18 lines
Sent: 27 May 2015 00:30
sctp_v4_map_v6was subtly writing and reading from members of a union in a way the clobbered data it needed to read before it read it.Zeroing the v6 flowinfo overwrites the v4
sin_addrwith 0, meaning that every place that callssctp_v4_map_v6gets ::ffff:0.0.0.0 as the result.Reorder things to guarantee correct behaviour no matter what the union layout is.
This impacts user space clients that open an IPv6 SCTP socket and receive IPv4 connections. Prior to 299ee user space would see a sockaddr with
AF_INETand a correct address, after 299ee the sockaddr isAF_INET6, but the address is wrong.Fixes 299ee123e198 (sctp: Fixup v4mapped behaviour to comply with Sock API)
...
Quoted 1 line
This bugfix should be a candidate for -stable
Anyone know off-hand which kernel releases are affected?
I'm going to have to note this in the release notes for one of our products.
David
Quoted 1 line · David Laight
From: Jason Gunthorpe
...
Quoted 6 lines
Quoted 1 line
Fixes 299ee123e198 (sctp: Fixup v4mapped behaviour to comply with Sock API)...
Quoted 1 line
This bugfix should be a candidate for -stable
Anyone know off-hand which kernel releases are affected?
I'm going to have to note this in the release notes for one of our products.
Ever heard of git-describe(1) ? ;)
git describe 299ee123e19889d511092347f5fc14db0f10e3a6 v3.16-rc7-1525-g299ee12
From: Daniel Borkmann
Quoted 1 line
Sent: 27 May 2015 10:34
...
Quoted 11 lines
Quoted 6 lines
Quoted 1 line
Fixes 299ee123e198 (sctp: Fixup v4mapped behaviour to comply with Sock API)...
Quoted 1 line
This bugfix should be a candidate for -stable
Anyone know off-hand which kernel releases are affected?
I'm going to have to note this in the release notes for one of our products.Ever heard of git-describe(1) ? ;)
git describe
299ee123e19889d511092347f5fc14db0f10e3a6v3.16-rc7-1525-g299ee12
Probably last time I asked the same question :-)
Since I don't spend all day fighting git, I don't know all the sub-commands.
In any case it looks like I can escape by turning off SCTP_I_WANT_MAPPED_V4_ADDR for kernels 3.17 through 4.0.
David
Quoted 2 lines · David Laight
In any case it looks like I can escape by turning off
SCTP_I_WANT_MAPPED_V4_ADDRfor kernels 3.17 through 4.0.
Just be aware that option is unusable on kernels without 299ee.
I fixed everything wrong I saw, but that doesn't mean it works 100%. Honestly, I don't think anyone has ever used it.
Jason
From: Jason Gunthorpe
Quoted 10 lines
Sent: 27 May 2015 16:32
Quoted 2 lines · David Laight
In any case it looks like I can escape by turning off
SCTP_I_WANT_MAPPED_V4_ADDRfor kernels 3.17 through 4.0.Just be aware that option is unusable on kernels without 299ee.
I fixed everything wrong I saw, but that doesn't mean it works 100%. Honestly, I don't think anyone has ever used it.
I'm now confused.
I've just done a test using a 4.0.0-rc1 kernel.
I'm binding an IPv6 listening socket and then connecting to it from 127.0.0.1.
I don't know it I'm being given an IPv4 format address or a v6mapped one (I shorten the latter before tracing it) - but it contains 127.0.0.1 (not 0.0.0.0).
(That is without changing any socket options.)
The kernel code I have seems to default 'v4mapped = 1' which means it should (if I read the code correctly) hit sctp_v4_map_v6(). But I'm not seeing the corruption.
Does 127.0.0.1 go through a different path that means the address is already in IPv6 format?
Testing without using the loopback address is rather harder (for automated test scripts).
(If anyone suggests that network namespaces might help, they can then explain how a single kernel entity is going to choose between different namespaces on an individual connection basis.
Think of something that is receiving data on one connection, decoding the requests, re-encapsulation the data in a different protocol and then sending the data onwards.)
David
Quoted 14 lines · David Laight
From: Jason Gunthorpe
Quoted 10 lines
Sent: 27 May 2015 16:32
Quoted 2 lines · David Laight
In any case it looks like I can escape by turning off
SCTP_I_WANT_MAPPED_V4_ADDRfor kernels 3.17 through 4.0.Just be aware that option is unusable on kernels without 299ee.
I fixed everything wrong I saw, but that doesn't mean it works 100%. Honestly, I don't think anyone has ever used it.
I'm now confused.
Quoted 7 lines
I've just done a test using a 4.0.0-rc1 kernel.
I'm binding an IPv6 listening socket and then connecting to it from 127.0.0.1.
I don't know it I'm being given an IPv4 format address or a v6mapped one (I shorten the latter before tracing it) - but it contains 127.0.0.1 (not 0.0.0.0).
(That is without changing any socket options.)
I don't know what your test does, but I used the same basic idea with loopback to find this issue. You should confirm the kernel is returning a AF_INET6 socket type, if it is AF_INET then there is a path I missed in 299ee and I should fix it..
Specifically, the corruption I confirmed was from a recvmsg call with MSG_NOTIFICATION set indicating a new connection has happened on a many to many socket.
strace sayth:
socket(PF_INET6, SOCK_SEQPACKET|SOCK_CLOEXEC, IPPROTO_SCTP) = 7
recvmsg(7, {msg_name(28)={sa_family¯_INET6, sin6_port=htons(9090), inet_pton(AF_INET6, "::ffff:0.0.0.0", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, msg_iov(1)=[{"\1\200\0\0\24\0\0\0\4\0\0\0\0\0\0\0\17%\0\0", 1024}], msg_controllen=0, msg_flags=MSG_EOR|MSG_MORE}, MSG_DONTWAIT) = 20
Jason
From: Jason Gunthorpe
Quoted 24 lines
Sent: 27 May 2015 17:32
Quoted 14 lines · David Laight
From: Jason Gunthorpe
Quoted 10 lines
Sent: 27 May 2015 16:32
Quoted 2 lines · David Laight
In any case it looks like I can escape by turning off
SCTP_I_WANT_MAPPED_V4_ADDRfor kernels 3.17 through 4.0.Just be aware that option is unusable on kernels without 299ee.
I fixed everything wrong I saw, but that doesn't mean it works 100%. Honestly, I don't think anyone has ever used it.
I'm now confused.
Quoted 7 lines
I've just done a test using a 4.0.0-rc1 kernel.
I'm binding an IPv6 listening socket and then connecting to it from 127.0.0.1.
I don't know it I'm being given an IPv4 format address or a v6mapped one (I shorten the latter before tracing it) - but it contains 127.0.0.1 (not 0.0.0.0).
(That is without changing any socket options.)
Quoted 4 lines
I don't know what your test does, but I used the same basic idea with loopback to find this issue. You should confirm the kernel is returning a
AF_INET6socket type, if it isAF_INETthen there is a path I missed in 299ee and I should fix it..
The code will be sleeping in kernel_accept() and later calls kernel_getpeername().
The code is used for both TCP and SCTP and this part is common (using the TCP semantics).
I'll look at the actual raw address format tomorrow.
I suspect it is already AF_INET6 before getting to the code that would badly translate it.
David
Quoted 4 lines · David Laight
The code will be sleeping in
kernel_accept()and later callskernel_getpeername().
The code is used for both TCP and SCTP and this part is common (using the TCP semantics).
getpeername uses a different flow, it calls into inet6_getname which will always return the AF_INET6 version.
The call to sctp_v6_addr_to_user after is to support the v6->v4 coversion when SCTP_I_WANT_MAPPED_V4_ADDR=0, it will never do the broken v4->v6 conversion.
Jason
From: Jason Gunthorpe
Quoted 10 lines
Sent: 27 May 2015 18:05
Quoted 4 lines · David Laight
The code will be sleeping in
kernel_accept()and later callskernel_getpeername().
The code is used for both TCP and SCTP and this part is common (using the TCP semantics).getpeername uses a different flow, it calls into
inet6_getnamewhich will always return theAF_INET6version.
Ok, that explains why I hadn't seen the problem.
It also means I don't have to worry about it.
David
Quoted 53 lines · Jason Gunthorpe
sctp_v4_map_v6was subtly writing and reading from members of a union in a way the clobbered data it needed to read before it read it.Zeroing the v6 flowinfo overwrites the v4
sin_addrwith 0, meaning that every place that callssctp_v4_map_v6gets ::ffff:0.0.0.0 as the result.Reorder things to guarantee correct behaviour no matter what the union layout is.
This impacts user space clients that open an IPv6 SCTP socket and receive IPv4 connections. Prior to 299ee user space would see a sockaddr with
AF_INETand a correct address, after 299ee the sockaddr isAF_INET6, but the address is wrong.Fixes 299ee123e198 (sctp: Fixup v4mapped behaviour to comply with Sock API)Signed-off-by Jason Gunthorpe <[email protected]>--- include/net/sctp/sctp.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)This bugfix should be a candidate for -stable
include/net/sctp/sctp.h +5
-2@@ -571,11 +571,14 @@ static inline void sctp_v6_map_v4(union sctp_addr *addr)/* Map v4 address to v4-mapped v6 address */ static inline void sctp_v4_map_v6 (union sctp_addr *addr) { +__be16 port; + + port = addr->v4.sin_port; + addr->v6.sin6_addr.s6_addr32[3 ] = addr->v4.sin_addr.s_addr; + addr->v6.sin6_port = port; addr->v6.sin6_family =AF_INET6 ; addr->v6.sin6_flowinfo =0 ; addr->v6.sin6_scope_id =0 ; - addr->v6.sin6_port = addr->v4.sin_port; - addr->v6.sin6_addr.s6_addr32[3 ] = addr->v4.sin_addr.s_addr; addr->v6.sin6_addr.s6_addr32[0 ] =0 ; addr->v6.sin6_addr.s6_addr32[1 ] =0 ; addr->v6.sin6_addr.s6_addr32[2 ] =htonl (0x0000ffff );Signature · 6 lines
2.1.4 -- To unsubscribe from this list: send the line "unsubscribe linux-sctp" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Nice catch
David Miller
maintainerFrom: Jason Gunthorpe <[email protected]>
Date: Tue, 26 May 2015 17:30:17 -0600
Quoted 11 lines
sctp_v4_map_v6was subtly writing and reading from members of a union in a way the clobbered data it needed to read before it read it.Zeroing the v6 flowinfo overwrites the v4
sin_addrwith 0, meaning that every place that callssctp_v4_map_v6gets ::ffff:0.0.0.0 as the result.Reorder things to guarantee correct behaviour no matter what the union layout is.
Quoted 7 lines
This impacts user space clients that open an IPv6 SCTP socket and receive IPv4 connections. Prior to 299ee user space would see a sockaddr with
AF_INETand a correct address, after 299ee the sockaddr isAF_INET6, but the address is wrong.Fixes 299ee123e198 (sctp: Fixup v4mapped behaviour to comply with Sock API)Signed-off-by Jason Gunthorpe <[email protected]>
Applied and queued up for -stable, thakns.