Bad behaviour when unintentionally mixing ipv4 and ipv6 addresses
Subsystems (2)
Hi,
By chance Reinhard Max spotted an interesting flaw in Linux bind(2)...
If you create a IPv4 socket and then incorrectly bind(2) to a IPv6 address (which you got from getaddrinfo(3) or similar), the socket will be bound to INADDR_ANY.
The reason is that the kernel just takes the sockaddr_in6 struct and evaluates it as a sockaddr_in struct, with the port being OK, but the IPv4 sin_addr overlaying the IPv6 sin6_flowinfo field.
As the sin6_flowinfo field is usually 0, your service can end up listening to the world.
A testprogram that you can strace is attached, run netstat -apn |grep 12345 afterwards to see it binds 0.0.0.0:12345.
Perhaps add a check like the one below? (untested)
Or use if (addr->sin_family == AF_INET6) to just catch the IPv6 case?
Ciao, Marcus
Subject: [PATCH] net/ipv4: Check for mistakenly passed in non-IPv4 address
Hi,
Check against mistakenly passing in IPv6 addresses (which would result in an INADDR_ANY bind) or similar incompatible sockaddrs.
Ciao, Marcus
--- net/ipv4/af_inet.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
net/ipv4/af_inet.c +3 -0
@@ -465,6 +465,9 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)if (addr_len <sizeof (struct sockaddr_in )) goto out; +if (addr->sin_family !=AF_INET ) + goto out; + chk_addr_ret =inet_addr_type (sock_net (sk), addr->sin_addr.s_addr);/* Not specified by any standard per-se, however it breaks too
Signature · 1 line
1.7.4.1
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv) {
int fd,fd2;
struct sockaddr_in6 saddrin,saddrin2;
size_t len;
fd = socket(AF_INET,SOCK_STREAM,0);
if (fd == -1) {
perror("socket");
exit(1);
}
memset(&saddrin,0,sizeof(saddrin));
/*memcpy(&saddrin.sin6_addr, &in6addr_loopback, sizeof(saddrin.sin6_addr));*/
memset(&saddrin.sin6_addr, 0x42, sizeof(saddrin.sin6_addr));
saddrin.sin6_family = AF_INET6;
saddrin.sin6_port = htons(12345);
if (-1 == bind(fd, (struct sockaddr*)&saddrin, sizeof(saddrin))) {
perror("bind");
exit(1);
}
if (-1 == listen(fd, 5)) {
perror("listen");
exit(1);
}
len = sizeof(saddrin2);
fd2 = accept (fd, (struct sockaddr*)&saddrin2, &len);
if (fd2 == -1) perror("accept");
close(fd);
return 0;
}David Miller
maintainerFrom: Marcus Meissner <[email protected]>
Date: Wed, 25 May 2011 17:59:18 +0200
net/ipv4/af_inet.c int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) · line 471
Quoted 8 lines
net/ipv4/af_inet.c +3
-0@@ -465,6 +465,9 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)if (addr_len <sizeof (struct sockaddr_in )) goto out; +if (addr->sin_family !=AF_INET ) + goto out; + chk_addr_ret =inet_addr_type (sock_net (sk), addr->sin_addr.s_addr);
Since we haven't been validating the sin_family field for 18+ years, the chance to break some applications is very real.
But I think it's more important to fix this (and force any broken apps to set sin_family correctly). So I will apply this, thanks.
Quoted 3 lines · David Miller
Since we haven't been validating the
sin_familyfield for 18+ years, the chance to break some applications is very real.But I think it's more important to fix this (and force any broken apps to set
sin_familycorrectly). So I will apply this, thanks.
I think a corresponding check should also go into inet6_bind() in net/ipv6/af_inet6.c .
cu
Reinhard
PATCH 1/1 net/ipv6: check for mistakenly passed in non-AF_INET6 sockaddrs
Quoted 11 lines · Reinhard Max
Quoted 5 lines · David Miller
Since we haven't been validating the
sin_familyfield for 18+ years, the chance to break some applications is very real.But I think it's more important to fix this (and force any broken apps to set
sin_familycorrectly). So I will apply this, thanks.I think a corresponding check should also go into
inet6_bind()innet/ipv6/af_inet6.c.
Good idea,
Same check as for IPv4, also do for IPv6.
(If you passed in a IPv4 sockaddr_in here, the sizeof check
in the line before would have triggered already though.)
Ciao, Marcus
--- net/ipv6/af_inet6.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
net/ipv6/af_inet6.c +4 -0
@@ -272,6 +272,10 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)if (addr_len <SIN6_LEN_RFC2133 )return -EINVAL ; + +if (addr->sin6_family !=AF_INET6 ) +return -EINVAL ; + addr_type =ipv6_addr_type (&addr->sin6_addr);if ((addr_type &IPV6_ADDR_MULTICAST ) && sock->type ==SOCK_STREAM )return -EINVAL ;
Signature · 1 line
1.7.4.1
Re: [PATCH] net/ipv6: check for mistakenly passed in non-AF_INET6 sockaddrs
From: Marcus Meissner <[email protected]>
Date: Mon, 6 Jun 2011 18:00:07 +0200
Quoted 15 lines · Reinhard Max
Quoted 11 lines · David Miller
Quoted 6 lines
Since we haven't been validating the
sin_familyfield for 18+ years, the chance to break some applications is very real.But I think it's more important to fix this (and force any broken apps to set
sin_familycorrectly). So I will apply this, thanks.I think a corresponding check should also go into
inet6_bind()innet/ipv6/af_inet6.c.Good idea,
Quoted 7 lines
Same check as for IPv4, also do for IPv6.
(If you passed in a IPv4
sockaddr_inhere, the sizeof check
in the line before would have triggered already though.)Signed-off-by Marcus Meissner <[email protected]>Cc Reinhard Max <[email protected]>
Applied, thanks.
Re: [PATCH] net/ipv6: check for mistakenly passed in non-AF_INET6 sockaddrs
In reply to Marcus Meissnernet/ipv6/af_inet6.c int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) · line 278
Quoted 6 lines · Marcus Meissner
Same check as for IPv4, also do for IPv6.
[...]net/ipv6/af_inet6.c +4
-0+ +if (addr->sin6_family !=AF_INET6 ) +return -EINVAL ; +
According to the POSIX manpage for bind(), the error code should be EAFNOSUPPORT ("The specified address is not a valid address for the address family of the specified socket"). This would also match the error code of connect() in the same situation.
And I think the family should be checked before the length for both, bind() and connect(), to get more descriptive error messages when passing an address of the wrong family.
cu
Reinhard