]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Fix assert() situations where gc_malloc() is called without a gc_arena object
authorDavid Sommerseth <davids@redhat.com>
Sun, 5 Feb 2012 23:30:47 +0000 (00:30 +0100)
committerDavid Sommerseth <davids@redhat.com>
Wed, 8 Feb 2012 13:37:46 +0000 (14:37 +0100)
In commit bee92b479414d12035b0422f81ac5fcfe14fa645 the gc_malloc() was hardened
to always require a gc_arena object for garbage collection.  Some places in the
code expected the old behaviour of a normal malloc() in these cases, that is a
memory allocation without garbage collection.

This old behaviour is partly restored by allowing string_alloc() to do a non-gc
based allocation if no gc_arena object is available.  In addition some other
places string_alloc() will now be called with a gc_arena pointer where such an
object is available.

The alloc_buf() function has also been refactored to not use gc_malloc() at
all.

v2: - removes a memleak when --ifconfig-ipv6 is used several times
    - makes string_alloc() behave properly if DMALLOC is enabled

Signed-off-by: David Sommerseth <davids@redhat.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
buffer.c
init.c
openvpn.c
options.c
pf.c
ssl_verify.c

index c39bbcbbb0cfb2aed5eea911af690b06a8d5f1a4..fca6a90ae7b425bb558da6b315d6330ea8f43498 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -54,11 +54,21 @@ alloc_buf_debug (size_t size, const char *file, int line)
 alloc_buf (size_t size)
 #endif
 {
+  struct buffer buf;
+
+  if (!buf_size_valid (size))
+    buf_size_error (size);
+  buf.capacity = (int)size;
+  buf.offset = 0;
+  buf.len = 0;
 #ifdef DMALLOC
-  return alloc_buf_gc_debug (size, NULL, file, line);
+  buf.data = openvpn_dmalloc (file, line, size);
 #else
-  return alloc_buf_gc (size, NULL);
+  buf.data = calloc (1, size);
 #endif
+  check_malloc_return(buf.data);
+
+  return buf;
 }
 
 struct buffer
@@ -515,11 +525,25 @@ string_alloc (const char *str, struct gc_arena *gc)
       const int n = strlen (str) + 1;
       char *ret;
 
+      if (gc) {
+#ifdef DMALLOC
+        ret = (char *) gc_malloc_debug (n, false, gc, file, line);
+#else
+        ret = (char *) gc_malloc (n, false, gc);
+#endif
+      } else {
+        /* If there are no garbage collector available, it's expected
+         * that the caller cleans up afterwards.  This is coherent with the
+         * earlier behaviour when gc_malloc() would be called with gc == NULL
+         */
 #ifdef DMALLOC
-      ret = (char *) gc_malloc_debug (n, false, gc, file, line);
+        ret = openvpn_dmalloc (file, line, n);
+        memset(ret, 0, n);
 #else
-      ret = (char *) gc_malloc (n, false, gc);
+        ret = calloc(1, n);
 #endif
+        check_malloc_return(ret);
+      }
       memcpy (ret, str, n);
       return ret;
     }
diff --git a/init.c b/init.c
index 525f44126433cca50d69ee79ea53ec5df553d22a..f0c369363850a4c6317131f687df682bc3b5de2b 100644 (file)
--- a/init.c
+++ b/init.c
@@ -3012,7 +3012,7 @@ do_close_ifconfig_pool_persist (struct context *c)
 static void
 do_inherit_env (struct context *c, const struct env_set *src)
 {
-  c->c2.es = env_set_create (NULL);
+  c->c2.es = env_set_create (&c->c2.gc);
   c->c2.es_owned = true;
   env_set_inherit (c->c2.es, src);
 }
index f5f2bce052d1c9fe48a8d5a25c7fa9ee6cea63df..84289d22b082f2a90871e2ec32b04c145f31f0aa 100644 (file)
--- a/openvpn.c
+++ b/openvpn.c
@@ -164,7 +164,7 @@ main (int argc, char *argv[])
          gc_init (&c.gc);
 
          /* initialize environmental variable store */
-         c.es = env_set_create (NULL);
+         c.es = env_set_create (&c.gc);
 #ifdef WIN32
          set_win_sys_path_via_env (c.es);
 #endif
index 6b8ae223d9e377eb54fe80d3638331fba383e188..a0b34318b4be58bd6f270e9df944a59eb35dff64 100644 (file)
--- a/options.c
+++ b/options.c
@@ -4291,7 +4291,7 @@ add_option (struct options *options,
     {
       unsigned int netbits;
       char * ipv6_local;
-       
+
       VERIFY_PERMISSION (OPT_P_UP);
       if ( get_ipv6_addr( p[1], NULL, &netbits, &ipv6_local, msglevel ) &&
            ipv6_addr_safe( p[2] ) )
@@ -4301,6 +4301,11 @@ add_option (struct options *options,
              msg( msglevel, "ifconfig-ipv6: /netbits must be between 64 and 124, not '/%d'", netbits );
              goto err;
            }
+
+          if (options->ifconfig_ipv6_local)
+            /* explicitly ignoring this is a const char */
+            free ((char *) options->ifconfig_ipv6_local);
+
          options->ifconfig_ipv6_local = ipv6_local;
          options->ifconfig_ipv6_netbits = netbits;
          options->ifconfig_ipv6_remote = p[2];
diff --git a/pf.c b/pf.c
index 6b4cba42933c6e30003c0517192b23d63457e802..79915fab328d096421a96430061aa4933423b329 100644 (file)
--- a/pf.c
+++ b/pf.c
@@ -566,7 +566,7 @@ pf_init_context (struct context *c)
         if (plugin_call (c->plugins, OPENVPN_PLUGIN_ENABLE_PF, NULL, NULL, c->c2.es) == OPENVPN_PLUGIN_FUNC_SUCCESS)
           {
             event_timeout_init (&c->c2.pf.reload, 1, now);
-            c->c2.pf.filename = string_alloc (pf_file, NULL);
+            c->c2.pf.filename = string_alloc (pf_file, &c->c2.gc);
             c->c2.pf.enabled = true;
 #ifdef ENABLE_DEBUG
             if (check_debug_level (D_PF_DEBUG))
index e45f149c3078835ce7f67f2c1a3198435eb0e6ad..37d49825fed7867caa87ee1bfb84303fc826cd26 100644 (file)
@@ -83,6 +83,7 @@ set_common_name (struct tls_session *session, const char *common_name)
     }
   if (common_name)
     {
+      /* FIXME: Last alloc will never be freed */
       session->common_name = string_alloc (common_name, NULL);
 #ifdef ENABLE_PF
       {
@@ -703,6 +704,7 @@ man_def_auth_set_client_reason (struct tls_multi *multi, const char *client_reas
       multi->client_reason = NULL;
     }
   if (client_reason && strlen (client_reason))
+    /* FIXME: Last alloc will never be freed */
     multi->client_reason = string_alloc (client_reason, NULL);
 }