Skip to contentSkip to conversation

Fix more "existing route may get deleted" cases

PATCH Superseded [email protected]... 2 participants 5 messages Jan 16, 2023 – Jan 24, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix more "existing route may get deleted" cases

From: Selva Nair <[email protected]...>

- Set RL_DID_LOCAL only if the corresponding route addition succeeds. This is needed to preserve when the direct route to the vpn server preexists.

- Ensure net_route_v4/v6_add/del() functions using iproute2 return error when route addition fails. Return value follows the same logic as corresponding functions using netlink though all failure reasons get the same error code of -1.

NOTE: delete_route_connected_v6_net() is called even if the corresponding addition fails. This looks harder to fix, but also less critical.

---

This anticipates two other cases of wrongly deleted routes to be fixed by
https://patchwork.openvpn.net/project/openvpn2/patch/[email protected].../

 src/openvpn/networking_iproute2.c | 32 +++++++++++++++++++++++--------
 src/openvpn/route.c               | 26 ++++++++++++++-----------
 src/openvpn/route.h               |  8 ++++----
 3 files changed, 43 insertions(+), 23 deletions(-)
src/openvpn/networking_iproute2.c +24 -8
index f93756d6..0efeed0f 100644
@@ -267,6 +267,7 @@ net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
 {
     struct argv argv = argv_new();
     const char *dst_str = print_in_addr_t(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s route add %s/%d", iproute_path, dst_str, prefixlen);
 
@@ -288,11 +289,14 @@ net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route add command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route add command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
@@ -302,6 +306,7 @@ net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
 {
     struct argv argv = argv_new();
     char *dst_str = (char *)print_in6_addr(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s -6 route add %s/%d dev %s", iproute_path, dst_str,
                 prefixlen, iface);
@@ -319,11 +324,14 @@ net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 add command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 add command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
@@ -333,6 +341,7 @@ net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
 {
     struct argv argv = argv_new();
     const char *dst_str = print_in_addr_t(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s route del %s/%d", iproute_path, dst_str, prefixlen);
 
@@ -342,11 +351,14 @@ net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route delete command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route delete command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
@@ -356,6 +368,7 @@ net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
 {
     struct argv argv = argv_new();
     char *dst_str = (char *)print_in6_addr(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s -6 route del %s/%d dev %s", iproute_path, dst_str,
                 prefixlen, iface);
@@ -373,11 +386,14 @@ net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 del command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 del command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
src/openvpn/route.c +15 -11
index 3a978cb4..20dacb5e 100644
@@ -912,7 +912,7 @@ init_route_ipv6_list(struct route_ipv6_list *rl6,
     return ret;
 }
 
-static bool
+static int
 add_route3(in_addr_t network,
            in_addr_t netmask,
            in_addr_t gateway,
@@ -1050,10 +1050,14 @@ redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt,
                 if ((rl->spec.flags & RTSA_REMOTE_HOST)
                     && rl->spec.remote_host != IPV4_INVALID_ADDR)
                 {
-                    ret = add_route3(rl->spec.remote_host, IPV4_NETMASK_HOST,
-                                     rl->rgi.gateway.addr, tt, flags | ROUTE_REF_GW,
-                                     &rl->rgi, es, ctx);
-                    rl->iflags |= RL_DID_LOCAL;
+                    int status = add_route3(rl->spec.remote_host, IPV4_NETMASK_HOST,
+                                            rl->rgi.gateway.addr, tt, flags | ROUTE_REF_GW,
+                                            &rl->rgi, es, ctx);
+                    if (status == RTA_SUCCESS)
+                    {
+                        rl->iflags |= RL_DID_LOCAL;
+                    }
+                    ret = (status != RTA_ERROR);
                 }
                 else
                 {
@@ -1551,7 +1555,7 @@ is_on_link(const int is_local_route, const unsigned int flags, const struct rout
     return rgi && (is_local_route == LR_MATCH || ((flags & ROUTE_REF_GW) && (rgi->flags & RGI_ON_LINK)));
 }
 
-bool
+int
 add_route(struct route_ipv4 *r,
           const struct tuntap *tt,
           unsigned int flags,
@@ -1564,7 +1568,7 @@ add_route(struct route_ipv4 *r,
 
     if (!(r->flags & RT_DEFINED))
     {
-        return true; /* no error */
+        return RTA_SUCCESS; /* no error */
     }
 
     struct argv argv = argv_new();
@@ -1858,7 +1862,7 @@ done:
     /* release resources potentially allocated during route setup */
     net_ctx_reset(ctx);
 
-    return (status != RTA_ERROR);
+    return status;
 }
 
 
@@ -1885,7 +1889,7 @@ route_ipv6_clear_host_bits( struct route_ipv6 *r6 )
     }
 }
 
-bool
+int
 add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
                unsigned int flags, const struct env_set *es,
                openvpn_net_ctx_t *ctx)
@@ -1895,7 +1899,7 @@ add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
 
     if (!(r6->flags & RT_DEFINED) )
     {
-        return true; /* no error */
+        return RTA_SUCCESS; /* no error */
     }
 
     struct argv argv = argv_new();
@@ -2131,7 +2135,7 @@ done:
     /* release resources potentially allocated during route setup */
     net_ctx_reset(ctx);
 
-    return (status != RTA_ERROR);
+    return status;
 }
 
 static void
src/openvpn/route.h +4 -4
index 71b4cf4e..1e110911 100644
@@ -259,13 +259,13 @@ void copy_route_ipv6_option_list(struct route_ipv6_option_list *dest,
 
 void route_ipv6_clear_host_bits( struct route_ipv6 *r6 );
 
-bool add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
+int add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
 
 void delete_route_ipv6(const struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx);
 
-bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
-               const struct route_gateway_info *rgi, const struct env_set *es,
-               openvpn_net_ctx_t *ctx);
+int add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
+              const struct route_gateway_info *rgi, const struct env_set *es,
+              openvpn_net_ctx_t *ctx);
 
 void add_route_to_option_list(struct route_option_list *l,
                               const char *network,
Signature · 1 line
2.34.1
#1 lore raw

Hi,

Quoted 5 lines · On Mon, Jan 16, 2023 at 02:48:09PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- Set RL_DID_LOCAL only if the corresponding route addition succeeds. This is needed to preserve when the direct route to the vpn server preexists.

While I think this is a good thing, the existing implementation does not look good - changing add_route() to return RTA_* sounds logical, and makes "return something meaningful from add_route3()" very easy, but the "check windows route installation success?" code paths relies on code like this...

            ret = add_route(r, tt, flags, &rl->rgi, es, ctx) && ret;

this still *works* with an "int" return, because RTA_ERROR is 0, and the "not flagged as error" codes are 1 and 2 - but I'm not sure if that was intentional or luck, and it makes reading the code harder.

So maybe postpone *this* part of the patch to 2.7 to give us a bit more time to think about how the return code/logic should look like?

Quoted 4 lines

- Ensure net_route_v4/v6_add/del() functions using iproute2 return error when route addition fails. Return value follows the same logic as corresponding functions using netlink though all failure reasons get the same error code of -1.

This one is very straightforward, easily testable in itself, and could go in right away.

I tested this as part of "testing this whole patch" on a linux/iproute2 build, triggering errors with invalid and double routes.

Without patch (2 duplicates, 1 invalid):

2023-01-21 16:50:05 /bin/ip route del 10.194.0.0/16
2023-01-21 16:50:05 /bin/ip route del 10.195.0.0/24
RTNETLINK answers: No such process
2023-01-21 16:50:05 ERROR: Linux route delete command failed: external program exited with error status: 2
2023-01-21 16:50:05 /bin/ip route del 10.194.0.0/16
RTNETLINK answers: No such process
2023-01-21 16:50:05 ERROR: Linux route delete command failed: external program exited with error status: 2
2023-01-21 16:50:05 /bin/ip route del 10.194.2.1/32
2023-01-21 16:50:05 delete_route_ipv6(fd00:abcd:194::/48)
2023-01-21 16:50:05 /bin/ip -6 route del fd00:abcd:194::/48 dev tun2
2023-01-21 16:50:05 delete_route_ipv6(fd00:abcd:194::/48)
2023-01-21 16:50:05 /bin/ip -6 route del fd00:abcd:194::/48 dev tun2
RTNETLINK answers: No such process
2023-01-21 16:50:05 ERROR: Linux route -6 del command failed: external program exited with error status: 2

With patch, same call == same set of "input routes"

^C2023-01-21 16:51:24 event_wait : Interrupted system call (fd=-1,code=4)

2023-01-21 16:51:24 /bin/ip route del 10.194.0.0/16
2023-01-21 16:51:24 /bin/ip route del 10.194.2.1/32
2023-01-21 16:51:24 delete_route_ipv6(fd00:abcd:194::/48)
2023-01-21 16:51:24 /bin/ip -6 route del fd00:abcd:194::/48 dev tun2
2023-01-21 16:51:24 Closing TUN/TAP interface

-> confirmed!

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
author
#2 lore raw

Hi

Quoted 15 lines · Gert Doering

Hi,

Quoted 5 lines · On Mon, Jan 16, 2023 at 02:48:09PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- Set RL_DID_LOCAL only if the corresponding route addition succeeds. This is needed to preserve when the direct route to the vpn server preexists.

While I think this is a good thing, the existing implementation does not look good - changing add_route() to return RTA_* sounds logical, and makes "return something meaningful from add_route3()" very easy, but the "check windows route installation success?" code paths relies on code like this...

Quoted 6 lines
            ret = add_route(r, tt, flags, &rl->rgi, es, ctx) && ret;

this still *works* with an "int" return, because RTA_ERROR is 0, and the "not flagged as error" codes are 1 and 2 - but I'm not sure if that was intentional or luck, and it makes reading the code harder.

Yes, pure luck --- I overlooked it..

Quoted 4 lines

So maybe postpone *this* part of the patch to 2.7 to give us a bit more time to think about how the return code/logic should look like?

agreed.

Quoted 10 lines
Quoted 6 lines

- Ensure net_route_v4/v6_add/del() functions using iproute2 return error when route addition fails. Return value follows the same logic as corresponding functions using netlink though all failure reasons get the same error code of -1.

This one is very straightforward, easily testable in itself, and could go in right away.

Will do a v2 with only this part.

Selva

#3 lore raw

PATCH v2 1/1 Fix one more "existing route may get deleted" case

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- Ensure net_route_v4/v6_add/del() functions using iproute2 return error when route addition fails. Return value follows the same logic as corresponding functions using netlink though all failure reasons get the same error code of -1.

TODO: Preserve any preexisting direct route to VPN and optionally the IPv6 connected net route.

v2: Following review, removed the poorly coded RL_DID_LOCAL-related chunks. That part needs a better fix.

---
 src/openvpn/networking_iproute2.c | 32 +++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)
src/openvpn/networking_iproute2.c +24 -8
index f93756d6..0efeed0f 100644
@@ -267,6 +267,7 @@ net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
 {
     struct argv argv = argv_new();
     const char *dst_str = print_in_addr_t(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s route add %s/%d", iproute_path, dst_str, prefixlen);
 
@@ -288,11 +289,14 @@ net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route add command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route add command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
@@ -302,6 +306,7 @@ net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
 {
     struct argv argv = argv_new();
     char *dst_str = (char *)print_in6_addr(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s -6 route add %s/%d dev %s", iproute_path, dst_str,
                 prefixlen, iface);
@@ -319,11 +324,14 @@ net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 add command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 add command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
@@ -333,6 +341,7 @@ net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
 {
     struct argv argv = argv_new();
     const char *dst_str = print_in_addr_t(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s route del %s/%d", iproute_path, dst_str, prefixlen);
 
@@ -342,11 +351,14 @@ net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route delete command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route delete command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
@@ -356,6 +368,7 @@ net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
 {
     struct argv argv = argv_new();
     char *dst_str = (char *)print_in6_addr(*dst, 0, &ctx->gc);
+    int ret = 0;
 
     argv_printf(&argv, "%s -6 route del %s/%d dev %s", iproute_path, dst_str,
                 prefixlen, iface);
@@ -373,11 +386,14 @@ net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
     }
 
     argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 del command failed");
+    if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 del command failed"))
+    {
+        ret = -1;
+    }
 
     argv_free(&argv);
 
-    return 0;
+    return ret;
 }
 
 int
Signature · 1 line
2.34.1
Acked-by on v2 1/1
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix one more 'existing route may get deleted' case

This is identical to the "I already tested these" bits of v1, so didn't do more than "compare diffs" and "basic linux compile test" (with --enable-iproute2) plus "run binary once to see if errors are detected" (they are) today.

Your patch has been applied to the master and release/2.6 branch.

commit 00fac39c58a7d5c8fa6d8f40232e9ab79f3fa9e0 (master) commit f30b1c90c06af73aef4ce93a62119393727032d6 (release/2.6)

Author: Selva Nair
Date:   Sat Jan 21 14:42:26 2023 -0500
     Fix one more 'existing route may get deleted' case

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26067.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering