Skip to contentSkip to conversation

IPV6 loopback bound socket succeeds connecting to remote host

PATCH Albert Pretorius 3 participants 9 messages Nov 29, 2010 – Dec 29, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi
I found a problem with ipv6 when a UDP socket is bound to loopback (::1) and connecting to a remote address. The same applies to TCP. Any data sent ends up on the remote host with a source address of loopback. The expected result of the connect should be EINVAL just like it is for ipv4. Here is a possible patch that fixes this problem below. I tested it on 2.6.37-rc3 using a tool I put on http://www.gitorious.org/bindconnect

----8<----

net/ipv6/ip6_output.c +27 -0
index 99157b4..a0de66c 100644
@@ -927,6 +927,7 @@ static int ip6_dst_lookup_tail(struct sock *sk,
 {
    int err;
    struct net *net = sock_net(sk);
+   struct net_device *dev_out;
 
    if (*dst == NULL)
        *dst = ip6_route_output(net, sk, fl);
@@ -934,6 +935,32 @@ static int ip6_dst_lookup_tail(struct sock *sk,
    if ((err = (*dst)->error))
        goto out_err_release;
 
+   dev_out = ip6_dst_idev(*dst)->dev;
+   if (dev_out && ipv6_addr_loopback(&fl->fl6_src) &&
+           !(dev_out->flags & IFF_LOOPBACK)) {
+       /*
+        * If socket is bound to loopback address but interface
+        * is not then it is invalid...
+        */
+       struct inet6_ifaddr *ifa;
+       struct inet6_dev *idev = in6_dev_get(dev_out);
+       err = -EINVAL;
+       if (idev) {
+           list_for_each_entry(ifa, &idev->addr_list, if_list)
+           {
+               /*
+                * ... unless the interface has the same address
+                */
+               if (ipv6_addr_equal(&ifa->addr, &fl->fl6_dst))
+                   err = 0;
+           }
+           in6_dev_put(idev);
+       }
+
+       if (err)
+           goto out_err_release;
+   }
+
    if (ipv6_addr_any(&fl->fl6_src)) {
        err = ipv6_dev_get_saddr(net, ip6_dst_idev(*dst)->dev,
                     &fl->fl6_dst,

---->8----

The bindconnect tool is build with just make and run like:

./obj/bindconnect -l ::1 9999 -r nnnn::nnn:nnnn:nnnn:nnnn%eth0 4444 -u
ERROR: Error occurred during connect() : Invalid argument

without the patch a UDP datagram is sent and TCP sends a SYN.

Please CC me personally as I am not subscribed to the mailing list.

thanks,
Albert Pretorius

#1 lore raw

Albert Pretorius wrote, at 11/29/2010 06:55 PM:

Quoted 4 lines

Hi
I found a problem with ipv6 when a UDP socket is bound to loopback (::1) and connecting to a remote address. The same applies to TCP. Any data sent ends up on the remote host with a source address of loopback. The expected result of the connect should be EINVAL just like it is for ipv4. Here is a possible patch that fixes this problem below. I tested it on 2.6.37-rc3 using a tool I put on http://www.gitorious.org/bindconnect

Indeed, there is an guide in RFC4291 for us to use IPv6 loopback address.

See RFC4291 2.5.3 section:
The loopback address must not be used as the source address in IPv6 packets that are sent outside of a single node. An IPv6 packet with a destination address of loopback must never be sent outside of a single node and must never be forwarded by an IPv6 router. A packet received on an interface with a destination address of loopback must be dropped.

I think it make nonsense to translate data between loopback device and other e.g. eth0 device in same machine. With your patch, also can't establish a tcp connection from loopback device to other device in same host.

=====
[PATCH] ipv6: forbid to send ipv6 packet from loopback to other device in same host

According to 2.5.3 section of RFC4291, commit f630e43 dropped the received packet with loopback as destination address. Now forbid to send packet from loopback to other device. Original patch is provided by Albert Pretorius.

---
 net/ipv6/ip6_output.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
net/ipv6/ip6_output.c +8 -0
index 99157b4..6b6cf84 100644
@@ -927,6 +927,7 @@ static int ip6_dst_lookup_tail(struct sock *sk,
 {
 	int err;
 	struct net *net = sock_net(sk);
+	struct net_device *dev_out;
 
 	if (*dst == NULL)
 		*dst = ip6_route_output(net, sk, fl);
@@ -934,6 +935,13 @@ static int ip6_dst_lookup_tail(struct sock *sk,
 	if ((err = (*dst)->error))
 		goto out_err_release;
 
+	dev_out = ip6_dst_idev(*dst)->dev;
+	if (dev_out && ipv6_addr_loopback(&fl->fl6_src) &&
+	    !(dev_out->flags & IFF_LOOPBACK)) {
+		err = -EINVAL;
+		goto out_err_release;
+	}
+
 	if (ipv6_addr_any(&fl->fl6_src)) {
 		err = ipv6_dev_get_saddr(net, ip6_dst_idev(*dst)->dev,
 					 &fl->fl6_dst,
Signature · 1 line
1.6.3.3
#2 lore raw

Hi

Quoted 2 lines · Shan Wei

I think it make nonsense to translate data between loopback device and other e.g. eth0 device in same machine.

I agree, RFC4291 makes it clear for IPV6 that no interface should accept traffic from loopback, I should not have tried to make it behave like IPV4. I can not find an equivalent statement for IPV4 though, all I could find is this from RFC3330:

127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher level protocol to an address anywhere within this block should loop back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback, but no addresses within this block should ever appear on any network anywhere [RFC1700, page 5].

Do you perhaps know?
thank you,
Albert Pretorius

#3 lore raw

Albert Pretorius wrote, at 12/02/2010 03:45 PM:

Quoted 9 lines

Hi

Quoted 2 lines · Shan Wei

I think it make nonsense to translate data between loopback device and other e.g. eth0 device in same machine.

I agree, RFC4291 makes it clear for IPV6 that no interface should accept traffic from loopback, I should not have tried to make it behave like IPV4. I can not find an equivalent statement for IPV4 though, all I could find is this from RFC3330:

Quoted 8 lines

127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher level protocol to an address anywhere within this block should loop back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback, but no addresses within this block should ever appear on any network anywhere [RFC1700, page 5].

Do you perhaps know?

There are no same statement for IPv4 loopback address. I have checked RFC1122, RFC1700 and RFC5753 .

Signature · 3 lines
Best Regards
-----
Shan Wei
Quoted 6 lines

thank you,
Albert Pretorius

maintainer
#4 lore raw

From: Shan Wei <[email protected]>
Date: Thu, 02 Dec 2010 16:41:39 +0800

Quoted 17 lines

Albert Pretorius wrote, at 12/02/2010 03:45 PM:

Quoted 16 lines

Hi

Quoted 2 lines · Shan Wei

I think it make nonsense to translate data between loopback device and other e.g. eth0 device in same machine.

I agree, RFC4291 makes it clear for IPV6 that no interface should accept traffic from loopback, I should not have tried to make it behave like IPV4. I can not find an equivalent statement for IPV4 though, all I could find is this from RFC3330:

127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher level protocol to an address anywhere within this block should loop back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback, but no addresses within this block should ever appear on any network anywhere [RFC1700, page 5].

Quoted 3 lines
Quoted 1 line

Do you perhaps know?

There are no same statement for IPv4 loopback address. I have checked RFC1122, RFC1700 and RFC5753 .

Shan, you really need to handle this in the ipv6 routing code.

Your approach will only modify socket based route handling, it will not handle the ipv6 forwarding case which as per the quoted RFC sections must be handled too.

#5 lore raw

David Miller wrote, at 12/17/2010 04:18 AM:

Quoted 3 lines

Your approach will only modify socket based route handling, it will not handle the ipv6 forwarding case which as per the quoted RFC sections must be handled too.

For the ipv6 forwarding case, we have done the check in ip6_forward().

493 int addrtype = ipv6_addr_type(&hdr->saddr);
494
495 /* This check is security critical. */
496 if (addrtype == IPV6_ADDR_ANY ||
497 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
498 goto error;

Signature · 3 lines
Best Regards
-----
Shan Wei
maintainer
#6 lore raw

From: Shan Wei <[email protected]>
Date: Mon, 20 Dec 2010 14:31:28 +0800

Quoted 5 lines

David Miller wrote, at 12/17/2010 04:18 AM:

Quoted 3 lines

Your approach will only modify socket based route handling, it will not handle the ipv6 forwarding case which as per the quoted RFC sections must be handled too.

Quoted 8 lines

For the ipv6 forwarding case, we have done the check in ip6_forward().

493 int addrtype = ipv6_addr_type(&hdr->saddr);
494
495 /* This check is security critical. */
496 if (addrtype == IPV6_ADDR_ANY ||
497 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
498 goto error;

Indeed, thanks for pointing this out.

#7 lore raw

David Miller wrote, at 12/20/2010 02:43 PM:

Quoted 10 lines

From: Shan Wei <[email protected]>
Date: Mon, 20 Dec 2010 14:31:28 +0800

Quoted 7 lines

David Miller wrote, at 12/17/2010 04:18 AM:

Quoted 3 lines

Your approach will only modify socket based route handling, it will not handle the ipv6 forwarding case which as per the quoted RFC sections must be handled too.

For the ipv6 forwarding case, we have done the check in ip6_forward().

Quoted 8 lines
Quoted 6 lines

493 int addrtype = ipv6_addr_type(&hdr->saddr);
494
495 /* This check is security critical. */
496 if (addrtype == IPV6_ADDR_ANY ||
497 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
498 goto error;

Indeed, thanks for pointing this out.

Notice that the state in patchwork is “Changes Requested”, what should i do now? I have no idead which part of this patch should be changed.

Signature · 3 lines
Best Regards
-----
Shan Wei
#8 lore raw

Hi
I tested this on 2.6.37-rc8 but unfortunately that does not address the problem. I suspect the ip_forward.c change only addresses the routing of data when /proc/sys/net/ipv6/conf/all/forwarding is enabled.

The original patch is required to prevent the kernel from sending a packet on the network with a source address of loopback. The kernel should not do that regardless of whether the packet will be dropped by the network (not routed).

Also from an application point of view I believe the behaviour should be the same for IPV4 and IPV6 when binding to loopback and connecting to remote address (i.e. EINVAL).

A further change is required to avoid the kernel acting on the reception of a packet with source address of loopback. Currently it will generate an ICMP6 dest unreachable, or pass the packet to an application listening on that port.

This patch (which includes the previous) seems to addresses the above problems:

---8<---

net/ipv6/ip6_input.c +1 -1
index a83e920..a374100 100644
@@ -108,7 +108,7 @@ int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt
     * of loopback must be dropped.
     */
    if (!(dev->flags & IFF_LOOPBACK) &&
-       ipv6_addr_loopback(&hdr->daddr))
+       (ipv6_addr_loopback(&hdr->daddr) | ipv6_addr_loopback(&hdr->saddr)))
        goto err;
 
    skb->transport_header = skb->network_header + sizeof(*hdr);
net/ipv6/ip6_output.c +8 -0
index 94b5bf1..0257998 100644
@@ -919,6 +919,7 @@ static int ip6_dst_lookup_tail(struct sock *sk,
 {
    int err;
    struct net *net = sock_net(sk);
+   struct net_device *dev_out;
 
    if (*dst == NULL)
        *dst = ip6_route_output(net, sk, fl);
@@ -926,6 +927,13 @@ static int ip6_dst_lookup_tail(struct sock *sk,
    if ((err = (*dst)->error))
        goto out_err_release;
 
+   dev_out = ip6_dst_idev(*dst)->dev;
+   if (dev_out && ipv6_addr_loopback(&fl->fl6_src) &&
+       !(dev_out->flags & IFF_LOOPBACK)) {
+       err = -EINVAL;
+       goto out_err_release;
+   }
+
    if (ipv6_addr_any(&fl->fl6_src)) {
        err = ipv6_dev_get_saddr(net, ip6_dst_idev(*dst)->dev,
                     &fl->fl6_dst,

--->8---

best regards,
Albert Pretorius

Quoted 48 lines · Shan Wei

From: Shan Wei <[email protected]>
Subject: Re: IPV6 loopback bound socket succeeds connecting to remote host
To: "David Miller" <[email protected]>
Cc: [email protected], [email protected], [email protected], [email protected], [email protected]
Date: Wednesday, 22 December, 2010, 7:06
David Miller wrote, at 12/20/2010
02:43 PM:

Quoted 5 lines

From: Shan Wei <[email protected]>
Date: Mon, 20 Dec 2010 14:31:28 +0800

Quoted 2 lines

David Miller wrote, at 12/17/2010 04:18 AM:

Quoted 1 line

Your approach will only modify socket based

route handling, it will

Quoted 1 line
Quoted 1 line
Quoted 1 line

not handle the ipv6 forwarding case which as

per the quoted RFC

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

sections must be handled too.

For the ipv6 forwarding case, we have done the

check in ip6_forward().

Quoted 2 lines
Quoted 2 lines

  493

     int addrtype =
ipv6_addr_type(&hdr->saddr);

Quoted 2 lines
Quoted 2 lines

  494
  495

     /* This check is security critical.
*/

Quoted 1 line
Quoted 1 line

  496

     if (addrtype == IPV6_ADDR_ANY ||

Quoted 1 line
Quoted 1 line

  497

         addrtype &
(IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))

Quoted 1 line
Quoted 1 line

  498

             goto
error;

Quoted 2 lines

Indeed, thanks for pointing this out.

Notice that the state in patchwork is “Changes Requested”, what should i do
now?  I have no idead which part of this patch should be changed.

Signature · 3 lines
Best Regards
-----
Shan Wei