Skip to contentSkip to conversation

Fix assertion if environment es is null

PATCH NAK Arne Schwabe 3 participants 6 messages Feb 2, 2013 – Feb 3, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix assertion if environment es is null

The construct_name_value eventually call gc_malloc with NULL as gc which will trigger an assertion

---
 src/openvpn/misc.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/misc.c +1 -1
index fcc8552..1e9a7fc 100644
@@ -804,7 +804,7 @@ setenv_str_ex (struct env_set *es,
     }
   else
     {
-      char *str = construct_name_value (name_tmp, val_tmp, NULL);
+      char *str = construct_name_value (name_tmp, val_tmp, &gc);
       if (platform_putenv(str))
       {
         msg (M_WARN | M_ERRNO, "putenv('%s') failed", str);
Signature · 1 line
1.7.9.5
#1 lore raw

Hi,

Quoted 1 line · Arne Schwabe

The construct_name_value eventually call gc_malloc with NULL as gc which will trigger an assertion

[..]

src/openvpn/misc.c setenv_str_ex (struct env_set *es, · line 810

Quoted 5 lines
src/openvpn/misc.c +1 -1
-      char *str = construct_name_value (name_tmp, val_tmp, NULL);
+      char *str = construct_name_value (name_tmp, val_tmp, &gc);
       if (platform_putenv(str))
       {
         msg (M_WARN | M_ERRNO, "putenv('%s') failed", str);

NAK!

platform_putenv() calls putenv() on non-windows platforms, and that one will just add a pointer to the string passed to it to envp[] (at least that's how I read the manpage: "The string pointed to by string becomes part of the environment, so altering the string changes the environment").

So your environment now points to a garbage buffer, which is then destroyed again.

I think there needs to be a strdup() here...

(And I seem to remember that "some of the buffer functions" did exactly this when &gc was NULL, but maybe we threw this out last year... David?)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#2 lore raw
Quoted 11 lines · Gert Doering

Hi,

Quoted 1 line · Arne Schwabe

The construct_name_value eventually call gc_malloc with NULL as gc which will trigger an assertion

[..]

src/openvpn/misc.c setenv_str_ex (struct env_set *es, · line 810

Quoted 5 lines
src/openvpn/misc.c +1 -1
-      char *str = construct_name_value (name_tmp, val_tmp, NULL);
+      char *str = construct_name_value (name_tmp, val_tmp, &gc);
       if (platform_putenv(str))
       {
         msg (M_WARN | M_ERRNO, "putenv('%s') failed", str);
Quoted 6 lines

NAK!

platform_putenv() calls putenv() on non-windows platforms, and that one will just add a pointer to the string passed to it to envp[] (at least that's how I read the manpage: "The string pointed to by string becomes part of the environment, so altering the string changes the environment").

Correct ... Please see this thread for more info (including proof-of-concept code)

<http://thread.gmane.org/gmane.network.openvpn.devel/7090>

Quoted 4 lines

So your environment now points to a garbage buffer, which is then destroyed again.

I think there needs to be a strdup() here...

Yeah, but that would be prone to leak memory again ...

Quoted 2 lines

(And I seem to remember that "some of the buffer functions" did exactly this when &gc was NULL, but maybe we threw this out last year... David?)

I think the commit you think about is this one:

commit bee92b479414d12035b0422f81ac5fcfe14fa645

Author: Adriaan de Jong <[email protected]...>
Date:   Sun Feb 5 12:51:25 2012 +0100
    Removed support for calling gc_malloc with a NULL gc_arena struct

    Signed-off-by: Adriaan de Jong <[email protected]...>
    Acked-by: David Sommerseth <[email protected]...>
    Signed-off-by: David Sommerseth <[email protected]...>

That basically impacted all calls ending up in gc_malloc(). But this was prone to leak memory when called with NULL as the gc_arena pointer.
After a discussion, we ended up agreeing to solve issues this patch
would flush open - instead of trying to fix those leaking areas instantly; then it would just have been a temporary fix until it would be abused again.

Signature · 3 lines
kind regards,

David Sommerseth
#3 lore raw

PATCH 1/1 Remove dead code path and putenv functionality

The construct_name_value function call will always fail so this code path is never used. Since we now always have a valid environment in static_context->c2.es it is better to have a ASSERT(es) than to call this code patch.

---
 src/openvpn/misc.c     |   24 ++++--------------------
 src/openvpn/platform.c |   28 ----------------------------
 2 files changed, 4 insertions(+), 48 deletions(-)
src/openvpn/misc.c +4 -20
index fcc8552..b8fb9d7 100644
@@ -707,13 +707,6 @@ env_set_remove_from_environment (const struct env_set *es)
 
 static struct env_item *global_env = NULL; /* GLOBAL */
 
-void
-manage_env (char *str)
-{
-  remove_env_item (str, true, &global_env);
-  add_env_item (str, false, &global_env, NULL);
-}
-
 #endif
 
 /* add/modify/delete environmental strings */
@@ -789,9 +782,9 @@ setenv_str_ex (struct env_set *es,
   if (value)
     val_tmp = string_mod_const (value, value_include, value_exclude, value_replace, &gc);
 
-  if (es)
-    {
-      if (val_tmp)
+  ASSERT (es);
+
+  if (val_tmp)
 	{
 	  const char *str = construct_name_value (name_tmp, val_tmp, &gc);
 	  env_set_add (es, str);
@@ -799,17 +792,8 @@ setenv_str_ex (struct env_set *es,
 	  msg (M_INFO, "SETENV_ES '%s'", str);
 #endif
 	}
-      else
-	env_set_del (es, name_tmp);
-    }
   else
-    {
-      char *str = construct_name_value (name_tmp, val_tmp, NULL);
-      if (platform_putenv(str))
-      {
-        msg (M_WARN | M_ERRNO, "putenv('%s') failed", str);
-      }
-    }
+	env_set_del (es, name_tmp);
 
   gc_free (&gc);
 }
src/openvpn/platform.c +0 -28
index e79de7a..16d4dac 100644
@@ -275,34 +275,6 @@ platform_unlink (const char *filename)
 #endif
 }
 
-int platform_putenv(char *string)
-{
-  int status;
-#if defined(WIN32)
-  struct gc_arena gc = gc_new ();
-  char *s = string_alloc(string, &gc);
-  char *value = strchr(s, '=');
-  if (value!=NULL)
-    {
-      *value = '\0';
-      value++;
-      if (*value == '\0')
-        value = NULL;
-    }
-
-  status = SetEnvironmentVariableW (wide_string (s, &gc),
-      wide_string (value, &gc)) ? 1: 0;
-  gc_free (&gc);
-#elif defined(HAVE_PUTENV)
-  void manage_env (char *str); /* TODO: Resolve properly */
-  status = putenv (string);
-  if (!status)
-    manage_env (string);
-#endif
-
-  return status;
-}
-
 FILE *
 platform_fopen (const char *path, const char *mode)
 {
Signature · 1 line
1.7.9.5
#4 lore raw

Re: [Openvpn-devel] [PATCH] Remove dead code path and putenv functionality

HI,

Quoted 3 lines · Arne Schwabe

The construct_name_value function call will always fail so this code path is never used. Since we now always have a valid environment in static_context->c2.es it is better to have a ASSERT(es) than to call this code patch.

After some discussion, we (Gert, David, Arne) agreed to do it this way (ACK!)

- *if* we reach this point with es==NULL it's a bug, and we should assert-crash in a way that makes it obvious where the real problem is, and not "3 levels down in the buffer code"

- the offending code path was in the Android branch and does not affect the "git openvpn" - but if we have more offending code, we want to known, instead of silently ignoring it.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#5 lore raw

[Openvpn-devel] [PATCH applied] Re: Remove dead code path and putenv functionality

In reply to Arne Schwabe

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

commit 4d0fb44754d91504b84092eb2197bc635740d540 (master) commit d84002f419abd3ac18fe906fa58d03c1a648a3fb (release/2.3)

Author: Arne Schwabe
Date:   Sun Feb 3 12:12:31 2013 +0100
     Remove dead code path and putenv functionality

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: David Sommerseth <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: [email protected]...
     URL: http://article.gmane.org/gmane.network.openvpn.devel/7323
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering