Skip to contentSkip to conversation

iptunnel: detect protocol mismatch on tunnel change

PATCH Applied Stephen Hemminger 4 participants 5 messages Apr 10, 2023 – Apr 14, 2023

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

PATCH 1/1 iptunnel: detect protocol mismatch on tunnel change

If attempt is made to change an IPv6 tunnel by using IPv4 parameters, a stack overflow would happen and garbage request would be passed to kernel.

Example:
ip tunnel add gre1 mode ip6gre local 2001:db8::1 remote 2001:db8::2 ttl 255 ip tunnel change gre1 mode gre local 192.168.0.0 remote 192.168.0.1 ttl 255

The second command should fail because it attempting set IPv4 addresses on a GRE tunnel that is IPv6.

Do best effort detection of this mismatch by giving a bigger buffer to get tunnel request, and checking that the IP header is IPv4. It is still possible but unlikely that byte would match in IPv6 tunnel paramater, but good enough to catch the obvious cases.

Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1032642

---
 ip/iptunnel.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
ip/iptunnel.c +12 -2
index 02c3670b469d..b6da145913d6 100644
@@ -17,6 +17,7 @@
 #include <net/if_arp.h>
 #include <linux/ip.h>
 #include <linux/if_tunnel.h>
+#include <linux/ip6_tunnel.h>
 
 #include "rt_names.h"
 #include "utils.h"
@@ -172,11 +173,20 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
 			if (get_ifname(p->name, *argv))
 				invarg("\"name\" not a valid ifname", *argv);
 			if (cmd == SIOCCHGTUNNEL && count == 0) {
-				struct ip_tunnel_parm old_p = {};
+				union {
+					struct ip_tunnel_parm ip_tnl;
+					struct ip6_tnl_parm2 ip6_tnl;
+				} old_p = {};
 
 				if (tnl_get_ioctl(*argv, &old_p))
 					return -1;
-				*p = old_p;
+
+				if (old_p.ip_tnl.iph.version != 4 ||
+				    old_p.ip_tnl.iph.ihl != 5)
+					invarg("\"name\" is not an ip tunnel",
+					       *argv);
+
+				*p = old_p.ip_tnl;
 			}
 		}
 		count++;
Signature · 1 line
2.39.2
Tested-by on v1 1/1
#1 lore raw
Quoted 49 lines · Stephen Hemminger

If attempt is made to change an IPv6 tunnel by using IPv4 parameters, a stack overflow would happen and garbage request would be passed to kernel.

Example:
ip tunnel add gre1 mode ip6gre local 2001:db8::1 remote 2001:db8::2 ttl 255 ip tunnel change gre1 mode gre local 192.168.0.0 remote 192.168.0.1 ttl 255

The second command should fail because it attempting set IPv4 addresses on a GRE tunnel that is IPv6.

Do best effort detection of this mismatch by giving a bigger buffer to get tunnel request, and checking that the IP header is IPv4. It is still possible but unlikely that byte would match in IPv6 tunnel paramater, but good enough to catch the obvious cases.

Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1032642

---
 ip/iptunnel.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
ip/iptunnel.c +5 -2
index 02c3670b469d..b6da145913d6 100644
@@ -17,6 +17,7 @@
 #include <net/if_arp.h>
 #include <linux/ip.h>
 #include <linux/if_tunnel.h>
+#include <linux/ip6_tunnel.h>
 
 #include "rt_names.h"
 #include "utils.h"
@@ -172,11 +173,20 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
                        if (get_ifname(p->name, *argv))
                                invarg("\"name\" not a valid ifname", *argv);
                        if (cmd == SIOCCHGTUNNEL && count == 0) {
-                               struct ip_tunnel_parm old_p = {};
+                               union {
+                                       struct ip_tunnel_parm ip_tnl;
+                                       struct ip6_tnl_parm2 ip6_tnl;
+                               } old_p = {};
 
                                if (tnl_get_ioctl(*argv, &old_p))
                                        return -1;
-                               *p = old_p;

ip/iptunnel.c static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) · line 192

Quoted 10 lines
ip/iptunnel.c +7 -0
+
+                               if (old_p.ip_tnl.iph.version != 4 ||
+                                   old_p.ip_tnl.iph.ihl != 5)
+                                       invarg("\"name\" is not an ip tunnel",
+                                              *argv);
+
+                               *p = old_p.ip_tnl;
                        }
                }
                count++;

Can confirm it doesn't crash anymore, thanks.

#2 lore raw
In reply to Stephen Hemminger
Quoted 13 lines · Stephen Hemminger
ip/iptunnel.c +1 -0
index 02c3670b469d..b6da145913d6 100644
@@ -17,6 +17,7 @@
 #include <net/if_arp.h>
 #include <linux/ip.h>
 #include <linux/if_tunnel.h>
+#include <linux/ip6_tunnel.h>
 
 #include "rt_names.h"
 #include "utils.h"

ip/iptunnel.c static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) · line 179

Quoted 8 lines
ip/iptunnel.c +4 -1
@@ -172,11 +173,20 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
 			if (get_ifname(p->name, *argv))
 				invarg("\"name\" not a valid ifname", *argv);
 			if (cmd == SIOCCHGTUNNEL && count == 0) {
-				struct ip_tunnel_parm old_p = {};
+				union {
+					struct ip_tunnel_parm ip_tnl;
+					struct ip6_tnl_parm2 ip6_tnl;
+				} old_p = {};'

That addresses the stack smashing, but ....

ip/iptunnel.c static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) · line 185

Quoted 7 lines
ip/iptunnel.c +3 -1
 				if (tnl_get_ioctl(*argv, &old_p))
 					return -1;
-				*p = old_p;
+
+				if (old_p.ip_tnl.iph.version != 4 ||
+				    old_p.ip_tnl.iph.ihl != 5)

this field overlays laddr in ip6_tnl_parm2 which means there is a collision in valid addresses.

Quoted 7 lines
ip/iptunnel.c +4 -0
+					invarg("\"name\" is not an ip tunnel",
+					       *argv);
+
+				*p = old_p.ip_tnl;
 			}
 		}
 		count++;
#3 lore raw
Quoted 12 lines · On Tue, 11 Apr 2023 09:55:25 -0600 David Ahern <[email protected]> wrote:

That addresses the stack smashing, but ....

ip/iptunnel.c static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) · line 185

Quoted 7 lines
ip/iptunnel.c +3 -1
 				if (tnl_get_ioctl(*argv, &old_p))
 					return -1;
-				*p = old_p;
+
+				if (old_p.ip_tnl.iph.version != 4 ||
+				    old_p.ip_tnl.iph.ihl != 5)  

this field overlays laddr in ip6_tnl_parm2 which means there is a collision in valid addresses.

That is why the commit message said this is best effort. What happens when there is a collision with a valid address is that the call will fail later as a bogus change to an IP6 tunnel.

 # ip tunnel add gre1 mode ip6gre local 4545:db8::1 remote 2001:db8::2 ttl 255
 # ip tunnel change gre1 mode gre local 192.168.0.0 remote 192.168.0.1 ttl 255
ttl != 0 and nopmtudisc are incompatible

This is off in the non-standard IPv6 address range so not a likely to bother anyone.

#4 lore raw
In reply to Stephen Hemminger
This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Hello:

This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Quoted 9 lines · On Mon, 10 Apr 2023 16:35:09 -0700 you wrote:

If attempt is made to change an IPv6 tunnel by using IPv4 parameters, a stack overflow would happen and garbage request would be passed to kernel.

Example:
ip tunnel add gre1 mode ip6gre local 2001:db8::1 remote 2001:db8::2 ttl 255 ip tunnel change gre1 mode gre local 192.168.0.0 remote 192.168.0.1 ttl 255

[...]

Here is the summary with links:
- [iproute2] iptunnel: detect protocol mismatch on tunnel change

    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=8cc2eac60d5f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html