Skip to contentSkip to conversation

Replace custom min macro and use more C99 style in man_remote_entry_get

PATCH In review Arne Schwabe 2 participants 8 messages Dec 27, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Replace custom min macro and use more C99 style in man_remote_entry_get

---
 src/openvpn/manage.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
src/openvpn/manage.c +4 -8
index be9a38952..7e326702f 100644
@@ -865,8 +865,6 @@ man_remote_entry_count(struct management *man)
     }
 }
 
-#define min(a, b) ((a) < (b) ? (a) : (b))
-
 static void
 man_remote_entry_get(struct management *man, const char *p1, const char *p2)
 {
@@ -875,12 +873,10 @@ man_remote_entry_get(struct management *man, const char *p1, const char *p2)
     if (man->persist.callback.remote_entry_get
         && man->persist.callback.remote_entry_count)
     {
-        bool res;
-        unsigned int from, to;
         unsigned int count = (*man->persist.callback.remote_entry_count)(man->persist.callback.arg);
 
-        from = (unsigned int) atoi(p1);
-        to = p2 ? (unsigned int) atoi(p2) : from + 1;
+        unsigned int from = (unsigned int) atoi(p1);
+        unsigned int to = p2 ? (unsigned int) atoi(p2) : from + 1;
 
         if (!strcmp(p1, "all"))
         {
@@ -888,10 +884,10 @@ man_remote_entry_get(struct management *man, const char *p1, const char *p2)
             to = count;
         }
 
-        for (unsigned int i = from; i < min(to, count); i++)
+        for (unsigned int i = from; i < min_uint(to, count); i++)
         {
             char *remote = NULL;
-            res = (*man->persist.callback.remote_entry_get)(man->persist.callback.arg, i, &remote);
+            bool res = (*man->persist.callback.remote_entry_get)(man->persist.callback.arg, i, &remote);
             if (res && remote)
             {
                 msg(M_CLIENT, "%u,%s", i, remote);
Signature · 1 line
2.37.1 (Apple Git-137.1)
#1 lore raw

PATCH 2/2 Replace realloc with new gc_realloc function

The realloc logic has the problem that it relies on the memory being deallocated by uninit_options rather than by freeing the gc. This does not always happen in all code path. Especially the crypto selftest run by make check will not call uninit_options.

This introduces a gc_realloc function that ensures that the pointer is instead freed when gc_free is called.

---
 src/openvpn/buffer.c                   | 33 ++++++++++++++++++++++++++
 src/openvpn/buffer.h                   | 13 ++++++++++
 src/openvpn/options.c                  |  6 ++---
 tests/unit_tests/openvpn/test_buffer.c | 32 +++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 4 deletions(-)
src/openvpn/buffer.c +33 -0
index 575d45a1f..e3d5ba241 100644
@@ -412,6 +412,39 @@ gc_malloc(size_t size, bool clear, struct gc_arena *a)
     return ret;
 }
 
+void *
+gc_realloc(void *ptr, size_t size, struct gc_arena *a)
+{
+    void *ret = realloc(ptr, size);
+    check_malloc_return(ret);
+    if (a)
+    {
+        if (ptr && ptr != ret)
+        {
+            /* find the old entry and modify it if realloc changed
+             * the pointer */
+            struct gc_entry_special *e = NULL;
+            for (e = a->list_special; e != NULL; e = e->next)
+            {
+                if (e->addr == ptr)
+                {
+                    break;
+                }
+            }
+            ASSERT(e);
+            ASSERT(e->addr == ptr);
+            e->addr = ret;
+        }
+        else if (!ptr)
+        {
+            /* sets e->addr to newptr */
+            gc_addspecial(ret, free, a);
+        }
+    }
+
+    return ret;
+}
+
 void
 x_gc_free(struct gc_arena *a)
 {
src/openvpn/buffer.h +13 -0
index fece6336d..185226f7c 100644
@@ -187,6 +187,19 @@ struct buffer string_alloc_buf(const char *str, struct gc_arena *gc);
 
 void gc_addspecial(void *addr, void (*free_function)(void *), struct gc_arena *a);
 
+/**
+ * allows to realloc a pointer previously allocated by gc_malloc or gc_realloc
+ *
+ * @note only use this function on pointers returned by gc_malloc or re_alloc
+ *       with the same gc_arena
+ *
+ * @param ptr   Pointer of the previously allocated memory
+ * @param size  New size
+ * @param a     gc_arena to use
+ * @return      new pointer
+ */
+void *
+gc_realloc(void *ptr, size_t size, struct gc_arena *a);
 
 #ifdef BUF_INIT_TRACKING
 #define buf_init(buf, offset) buf_init_debug(buf, offset, __FILE__, __LINE__)
src/openvpn/options.c +2 -4
index 4e018fb84..e454b2ac0 100644
@@ -918,12 +918,10 @@ uninit_options(struct options *o)
 {
     if (o->connection_list)
     {
-        free(o->connection_list->array);
         CLEAR(*o->connection_list);
     }
     if (o->remote_list)
     {
-        free(o->remote_list->array);
         CLEAR(*o->remote_list);
     }
     if (o->gc_owned)
@@ -2173,7 +2171,7 @@ alloc_connection_entry(struct options *options, const int msglevel)
     if (l->len == l->capacity)
     {
         int capacity = l->capacity + CONNECTION_LIST_SIZE;
-        struct connection_entry **ce = realloc(l->array, capacity*sizeof(struct connection_entry *));
+        struct connection_entry **ce = gc_realloc(l->array, capacity*sizeof(struct connection_entry *), &options->gc);
         if (ce == NULL)
         {
             msg(msglevel, "Unable to process more connection options: out of memory. Number of entries = %d", l->len);
@@ -2206,7 +2204,7 @@ alloc_remote_entry(struct options *options, const int msglevel)
     if (l->len == l->capacity)
     {
         int capacity = l->capacity + CONNECTION_LIST_SIZE;
-        struct remote_entry **re = realloc(l->array, capacity*sizeof(struct remote_entry *));
+        struct remote_entry **re = gc_realloc(l->array, capacity*sizeof(struct remote_entry *), &options->gc);
         if (re == NULL)
         {
             msg(msglevel, "Unable to process more remote options: out of memory. Number of entries = %d", l->len);
tests/unit_tests/openvpn/test_buffer.c +32 -0
index ac701669f..9e3b1d2e9 100644
@@ -231,6 +231,37 @@ test_buffer_free_gc_two(void **state)
     gc_free(&gc);
 }
 
+
+static void
+test_buffer_gc_realloc(void **state)
+{
+    struct gc_arena gc = gc_new();
+
+    void *p1 = gc_realloc(NULL, 512, &gc);
+    void *p2 = gc_realloc(NULL, 512, &gc);
+
+    assert_ptr_not_equal(p1, p2);
+
+    memset(p1, '1', 512);
+    memset(p2, '2', 512);
+
+    p1 = gc_realloc(p1, 512, &gc);
+
+    /* allocate 512kB to ensure the pointer needs to change */
+    void *p1new = gc_realloc(p1, 512ul * 1024, &gc);
+    assert_ptr_not_equal(p1, p1new);
+
+    void *p2new = gc_realloc(p2, 512ul * 1024, &gc);
+    assert_ptr_not_equal(p2, p2new);
+
+    void *p3 = gc_realloc(NULL, 512, &gc);
+    memset(p3, '3', 512);
+
+
+    gc_free(&gc);
+}
+
+
 int
 main(void)
 {
@@ -259,6 +290,7 @@ main(void)
                                         test_buffer_list_teardown),
         cmocka_unit_test(test_buffer_free_gc_one),
         cmocka_unit_test(test_buffer_free_gc_two),
+        cmocka_unit_test(test_buffer_gc_realloc),
     };
 
     return cmocka_run_group_tests_name("buffer", tests, NULL, NULL);
Signature · 1 line
2.37.1 (Apple Git-137.1)
Acked-by on v1 2/2
#2 lore raw

[Openvpn-devel] [PATCH applied] Re: Replace realloc with new gc_realloc function

The initial tests were left to Github/ASAN builds, which are now "all green for real" -
https://github.com/cron2/openvpn/actions/runs/3789030448

Stare-at-code also looks good, though a bit complicated (and if I'm reading this right, doing an initial allocation with gc_malloc() and then calling gc_realloc() on it will double-free(), so *boom*) - sufficient for the use case at hand (initial allocation also done with gc_realloc()), but maybe it could be simplified and made robust against this case.

My manual "test one instance with 200 remotes" did what I expected (and I discovered --management-query-remote ;-) ).

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

commit e7f2169772f90f9bf158a17f5656a6a985e74e31 (master) commit 09267c1ccde6a8f0671c785413ca847e97bbc0a5 (release/2.6)

Author: Arne Schwabe
Date:   Tue Dec 27 15:02:45 2022 +0100
     Replace realloc with new gc_realloc function

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

--
kind regards,

Gert Doering

#3 lore raw

PATCH v2 1/4 Ensure we do not promote a TA_TIMEOUT to a TA_READ event with dco

In reply to Arne Schwabe

with dco sometimes we end up promoting a timeout event to read event. For the residual read, this problem is probably not solvable without changing the kernel DCO API (ie. passing our residual on new_peer to let the kernel handle assembling the next packet.)

---
 src/openvpn/forward.c | 24 ------------------------
 src/openvpn/forward.h | 30 ++++++++++++++++++++++++++++++
 src/openvpn/mtcp.c    | 12 +++++++++++-
 3 files changed, 41 insertions(+), 25 deletions(-)
src/openvpn/forward.c +0 -24
index af4ed05da..61caf1146 100644
@@ -1676,30 +1676,6 @@ process_ip_header(struct context *c, unsigned int flags, struct buffer *buf)
     }
 }
 
-/*
- * Linux DCO implementations pass the socket to the kernel and
- * disallow usage of it from userland for TCP, so (control) packets
- * sent and received by OpenVPN need to go through the DCO interface.
- *
- * Windows DCO needs control packets to be sent via the normal
- * standard Overlapped I/O.
- *
- * FreeBSD DCO allows control packets to pass through the socket in both
- * directions.
- *
- * Hide that complexity (...especially if more platforms show up
- * in the future...) in a small inline function.
- */
-static inline bool
-should_use_dco_socket(struct link_socket *ls)
-{
-#if defined(TARGET_LINUX)
-    return ls->dco_installed && proto_is_tcp(ls->info.proto);
-#else
-    return false;
-#endif
-}
-
 /*
  * Input: c->c2.to_link
  */
src/openvpn/forward.h +30 -0
index bd2d96010..5cddb5995 100644
@@ -424,4 +424,34 @@ connection_established(struct context *c)
     }
 }
 
+
+/**
+ * @param ls the link_socket the decision should be made for
+ * @return if we should use the dco kernel api or normal socket APIs for
+ *         write/send
+ *
+ *
+ * Linux DCO implementations pass the socket to the kernel and
+ * disallow usage of it from userland for TCP, so (control) packets
+ * sent and received by OpenVPN need to go through the DCO interface.
+ *
+ * Windows DCO needs control packets to be sent via the normal
+ * standard Overlapped I/O.
+ *
+ * FreeBSD DCO allows control packets to pass through the socket in both
+ * directions.
+ *
+ * Hide that complexity (...especially if more platforms show up
+ * in the future...) in a small inline function.
+ */
+static inline bool
+should_use_dco_socket(const struct link_socket *ls)
+{
+#if defined(TARGET_LINUX)
+    return ls->dco_installed && proto_is_tcp(ls->info.proto);
+#else
+    return false;
+#endif
+}
+
 #endif /* FORWARD_H */
src/openvpn/mtcp.c +11 -1
index ac06ddc64..519630544 100644
@@ -407,7 +407,7 @@ multi_tcp_wait_lite(struct multi_context *m, struct multi_instance *mi, const in
         /* If we got a socket that has been handed over to the kernel
          * we must not call the normal socket function to figure out
          * if it is readable or writable */
-        /* Assert that we only have the DCO exptected flags */
+        /* Assert that we only have the DCO expected flags */
         ASSERT(action & (TA_SOCKET_READ | TA_SOCKET_WRITE));
 
         /* We are always ready! */
@@ -586,6 +586,16 @@ multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const int act
         case MTP_NONE:
             if (mi && socket_read_residual(c->c2.link_socket))
             {
+                if (should_use_dco_socket(c->c2.link_socket))
+                {
+                    struct gc_arena gc = gc_new();
+                    msg(M_INFO, "ovpn-dco installed socket with residual read "
+                        "len=%d, mi=%s. This connection will probably"
+                        " break.",  BLEN(&c->c2.link_socket->stream_buf.residual),
+                        multi_instance_string(mi, false, &gc));
+                    gc_free(&gc);
+                    break;
+                }
                 newaction = TA_SOCKET_READ_RESIDUAL;
             }
             else
Signature · 1 line
2.37.1 (Apple Git-137.1)
#4 lore raw

PATCH v2 2/4 Bail out when trying to install a TCP socket with residual data to DCO

In reply to Arne Schwabe

Instead of getting the server in a very weird state, we bail out here. This is only a bandaid solution but better than the alternatives.

---
 src/openvpn/mtcp.c  |  2 +-
 src/openvpn/multi.c | 10 +++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
src/openvpn/mtcp.c +1 -1
index 519630544..c07b5d592 100644
@@ -591,7 +591,7 @@ multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const int act
                     struct gc_arena gc = gc_new();
                     msg(M_INFO, "ovpn-dco installed socket with residual read "
                         "len=%d, mi=%s. This connection will probably"
-                        " break.",  BLEN(&c->c2.link_socket->stream_buf.residual),
+                        " break.",  BLEN(&c->c2.link_socket->stream_buf.buf),
                         multi_instance_string(mi, false, &gc));
                     gc_free(&gc);
                     break;
src/openvpn/multi.c +9 -1
index 92e63dd26..6c6385c6e 100644
@@ -2316,9 +2316,17 @@ multi_client_setup_dco_initial(struct multi_context *m,
 {
     if (!dco_enabled(&mi->context.options))
     {
-        /* DCO not enabled, nothing to do, return sucess */
+        /* DCO not enabled, nothing to do, return success */
         return true;
     }
+
+    if (socket_read_residual(mi->context.c2.link_socket))
+    {
+        msg(M_INFO, "TCP socket with half read packet. Cannot install to "
+            "DCO: %s",  multi_instance_string(mi, false, gc));
+        return false;
+    }
+
     int ret = dco_multi_add_new_peer(m, mi);
     if (ret < 0)
     {
Signature · 1 line
2.37.1 (Apple Git-137.1)
#5 lore raw

PATCH v2 3/4 Ignore OVPN_DEL_PEER_REASON_USERSPACE to avoid race conditions

In reply to Arne Schwabe
---
 src/openvpn/multi.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
src/openvpn/multi.c +13 -4
index 6c6385c6e..50d88f19a 100644
@@ -3243,10 +3243,19 @@ process_incoming_del_peer(struct multi_context *m, struct multi_instance *mi,
             break;
 
         case OVPN_DEL_PEER_REASON_USERSPACE:
-            /* This very likely ourselves but might be another process, so
-             * still process it */
-            reason = "ovpn-dco: userspace request";
-            break;
+            /* We assume that is ourselves. UUnfortunately, sometimes these
+             * events happen with enough delay that they can have an order of
+             *
+             * dco_del_peer x
+             * [new client connecting]
+             * dco_new_peer x
+             * event from dco_del_peer arrives.
+             *
+             * if we do not ignore this we get desynced with the kernel
+             * since we assume the peer-id is free again. The other way would
+             * be to send a dco_del_peer again
+             */
+            return;
     }
 
     /* When kernel already deleted the peer, the socket is no longer
Signature · 1 line
2.37.1 (Apple Git-137.1)
#6 lore raw

PATCH v2 4/4 Log peer-id if loglevel is D_DCO_DEBUG and dco is enabled

In reply to Arne Schwabe

This enables logging the peer id in p2mp mode if dco is enabled and the log level is high enough

---
 src/openvpn/multi.c | 6 ++++++
 1 file changed, 6 insertions(+)
src/openvpn/multi.c +6 -0
index 50d88f19a..f8366be28 100644
@@ -473,6 +473,12 @@ multi_instance_string(const struct multi_instance *mi, bool null, struct gc_aren
             buf_printf(&out, "%s/", cn);
         }
         buf_printf(&out, "%s", mroute_addr_print(&mi->real, gc));
+        if (mi->context.c2.tls_multi
+            && mi->context.options.verbosity >= D_DCO_DEBUG
+            && dco_enabled(&mi->context.options))
+        {
+            buf_printf(&out, " peer-id=%d", mi->context.c2.tls_multi->peer_id);
+        }
         return BSTR(&out);
     }
     else if (null)
Signature · 1 line
2.37.1 (Apple Git-137.1)
Acked-by on v1 1/2
#7 lore raw
In reply to Arne Schwabe

Thanks.

Have not done much testing beyond "push to github and enjoy the green" (but this is very nice indeed). Well, except for the clang-asan red, which I did not notice before... but there is 2/2 for that :-)

Your patch has been applied to the master branch.

commit 841524153fbdcd377065e221e91ef163d83b9cc9 (master) commit f5919a42e4a6772c4116d19cd09132bedb6d77f5 (release/2.6)

Author: Arne Schwabe
Date:   Tue Dec 27 15:02:44 2022 +0100
     Replace custom min macro and use more C99 style in man_remote_entry_get

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

--
kind regards,

Gert Doering