]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Replace realloc with new gc_realloc function
authorArne Schwabe <arne@rfc2549.org>
Tue, 27 Dec 2022 14:02:45 +0000 (15:02 +0100)
committerGert Doering <gert@greenie.muc.de>
Tue, 27 Dec 2022 19:07:42 +0000 (20:07 +0100)
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.

Signed-off-by: Arne Schwabe <arne@rfc2549.org>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20221227140249.3524943-2-arne@rfc2549.org>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg25829.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
(cherry picked from commit e7f2169772f90f9bf158a17f5656a6a985e74e31)

src/openvpn/buffer.c
src/openvpn/buffer.h
src/openvpn/options.c
tests/unit_tests/openvpn/test_buffer.c

index 575d45a1fc4bdf1b3498422059e3f358e4d4ddd1..e3d5ba24125b3b8c51cf16a2ad2a8c58c458c758 100644 (file)
@@ -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)
 {
index fece6336d433b4ee0a4356dc0083d1c33ddf09a1..185226f7c99a12eb11b0aa9fb30dfae8bfcf874e 100644 (file)
@@ -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__)
index 4e018fb8473571c0da7fa8fc807cda865a017b6a..e454b2ac024316093a4f86e682917072002760ef 100644 (file)
@@ -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);
index ac701669f56c6137e159619b8e1babdb442926db..9e3b1d2e92ac8887f860a2406a4585c28f3c8999 100644 (file)
@@ -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);