Skip to contentSkip to conversation

Memory of server keeps increasing with key negotiations

Openvpn-devel Martin Gadbois 3 participants 7 messages May 28, 2008 – Jun 4, 2008

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Using OpenVPN 2.0.5 and 2.0.9, I notice that an somewhat idle connection increases the RSS of the server process linearly with time.

valgrind says that there are no memory leaks. So where is this memory kept?

The way to reproduce this problem fast is to set reneg-sec to 15 using RSA certificates.

Here's the server config, the one that increases:
=========
port 1194
proto udp
dev tun
ca ca/ca.crt
cert ca/server.crt
key ca/server.key # This file should be kept secret dh etc/dhparam1024.pem
server 169.254.0.0 255.255.255.0 ifconfig-pool-persist /tmp/ipp.txt 45 ifconfig-pool-linear
tun-mtu 1431
mtu-disc no
ping 5
ping-exit 60
cipher BF-CBC # Blowfish (default)
max-clients 200
tls-auth etc/ta.key 0
user nobody
group nobody
persist-key
persist-tun
verb 4
reneg-sec 15
=============

Any hints on where to look for this memory would be great, as I would like to clear this up.

Thanks!

- --

==============         +---------------------------------------------+
Martin Gadbois         |    You are            -- J. Steakley,       |
Sr. SW Designer        |    What you do             Armor, 1984      |
Colubris Networks Inc. |    When it counts.                          |

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIPaTkMPkSU9icdA0RAk/QAJ94r3tJhqToMtkoRf/lj38FuefnyQCgrLgD koTQZIWLrX6G+QU+1Gl2daA=
=uJ7l
-----END PGP SIGNATURE-----

#1 lore raw

Martin Gadbois <[email protected]...>:

Quoted 4 lines

Using OpenVPN 2.0.5 and 2.0.9, I notice that an somewhat idle connection increases the RSS of the server process linearly with time.

valgrind says that there are no memory leaks. So where is this memory kept?

You might be suffering from the problem I sent a patch for some months back. Basically, socket.c, socket_connect() accumulated unlimited amounts of memory if the server wasn't available. I added a pair of gc_free()/gc_new() in the middle of the loop.

==Begin pseudo-patch====================================================
msg (D_LINK_ERRORS | M_ERRNO_SOCK,
"TCP: connect to %s failed, will try again in %d seconds", print_sockaddr (remote, &gc), connect_retry_seconds);

+      /* avoid accumulating garbage when the server is not available */
+      gc_free (&gc);
+      gc = gc_new ();
       openvpn_close_socket (*sd);
       openvpn_sleep (connect_retry_seconds);

==End pseudo-patch======================================================

It was not garbage because all dynamic memory was kept track of and would be cleared at the end of the function (gc_free()). However, as long as a connection couldn't be formed, boundless amounts of memory were allocated.

Marko

#2 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 4 lines · Marko Rauhamaa

You might be suffering from the problem I sent a patch for some months back. Basically, socket.c, socket_connect() accumulated unlimited amounts of memory if the server wasn't available. I added a pair of gc_free()/gc_new() in the middle of the loop.

I saw your patch, but I do not think your situation fits mine, as my server is connected with exactly one client. It must be something like this though...

I've looked at the code (Yeah! Open Source) and could not see any lists, or code that accumulates data, apart from gc_*().

Maybe I should look into gc_*() closer...

(Or I could add debug code that logs when a gc() gets bigger than a particular threshold... mmmm...)

Thanks Marko.

- --

==============         +---------------------------------------------+
Martin Gadbois         |    You are            -- J. Steakley,       |
Sr. SW Designer        |    What you do             Armor, 1984      |
Colubris Networks Inc. |    When it counts.                          |

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIPceUMPkSU9icdA0RAqJMAKCcrXVCYepo2whovuCRZ1puK1se0QCfXfbN QIPPcBRXHxa/SAbzl/oA6fM=
=yCR3
-----END PGP SIGNATURE-----

#3 lore raw

Martin Gadbois <[email protected]...>:

Quoted 2 lines

I've looked at the code (Yeah! Open Source) and could not see any lists, or code that accumulates data, apart from gc_*().

There's this weird scheme by which you prepare a memory pool with gc_new(). Memory keeps on accumulating in the pool with alloc_buf_gc() (in print_sockaddr_ex(), for example). The memory is not freed until the whole pool is destroyed with gc_free().

See if there are repeated messages in your log file. It was those log messages that caused my router to run out of memory over a couple of weeks, although there must be other similar problem sources.

Marko

#4 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 9 lines · Marko Rauhamaa

Martin Gadbois <[email protected]...>:

Quoted 2 lines

I've looked at the code (Yeah! Open Source) and could not see any lists, or code that accumulates data, apart from gc_*().

There's this weird scheme by which you prepare a memory pool with gc_new(). Memory keeps on accumulating in the pool with alloc_buf_gc() (in print_sockaddr_ex(), for example). The memory is not freed until the whole pool is destroyed with gc_free().

Yep. It seems that whenever env_set_add() is called, context->c2.gc is used to allocate stuff using gc*() functions at every key renegotiation.

The environement list is kept up-to-date, but all elements from this list is allocated using context->c2.gc, and never freed.

Now, I need to fix that without breaking anything...

- --

==============         +---------------------------------------------+
Martin Gadbois         |    You are            -- J. Steakley,       |
Sr. SW Designer        |    What you do             Armor, 1984      |
Colubris Networks Inc. |    When it counts.                          |

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIPsQxMPkSU9icdA0RAmHYAJ4yA+7Vw3SVY7X/Q+pH5icLkwlu/ACcDubu Y6+Q9emSaFl2kbwkOmInPCc=
=ZRWI
-----END PGP SIGNATURE-----

#5 lore raw
In reply to Martin Gadbois

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 3 lines · Martin Gadbois

Using OpenVPN 2.0.5 and 2.0.9, I notice that an somewhat idle connection increases the RSS of the server process linearly with time.

Here is my patch to fix this issue. It does create (according to valgrind) a memory leak, but it does not increase with time. Feel free to fix it better...

I short, environments values were allocated using gc*(), and every key change the environement list changes, but the allocated values stayed in the context's gc.

I modified the environement values to always allocate using malloc() and always free() values when the env list changes.

- --

==============         +---------------------------------------------+
Martin Gadbois         |    You are            -- J. Steakley,       |
Sr. SW Designer        |    What you do             Armor, 1984      |
Colubris Networks Inc. |    When it counts.                          |

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIPtCmMPkSU9icdA0RAiXTAJ4g3qzaubnyRTOCsCEF9Iz3BU+2AQCeMzzE /7mbJluQBOfzjy4jph4T4RI=
=fksj
-----END PGP SIGNATURE-----

misc.c +5 -4
diff -ur openvpn-2.0.5-org/misc.c openvpn-2.0.5/misc.c
@@ -651,7 +652,7 @@
 	    prev->next = current->next;
 	  else
 	    *list = current->next;
-	  if (do_free)
+//	  if (do_free)
 	    {
 	      memset (current->string, 0, strlen (current->string));
 	      free (current->string);
@@ -671,9 +672,10 @@
 
   ASSERT (str);
   ASSERT (list);
-
-  ALLOC_OBJ_GC (item, struct env_item, gc);
-  item->string = do_alloc ? string_alloc (str, gc): str;
+	dmsg(M_WARN,"ENV: Adding %s %s",str,do_alloc?"Alloc":"");
+  ALLOC_OBJ (item, struct env_item);
+//  item->string = do_alloc ? string_alloc (str, gc): str;
+  item->string = strdup(str);
   item->next = *list;
   *list = item;
 }
#6 lore raw
Quoted 15 lines · Martin Gadbois

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 4 lines · Martin Gadbois

Using OpenVPN 2.0.5 and 2.0.9, I notice that an somewhat idle connection increases the RSS of the server process linearly with time.

Here is my patch to fix this issue. It does create (according to valgrind) a memory leak, but it does not increase with time. Feel free to fix it better...

Quoted 9 lines

I short, environments values were allocated using gc*(), and every key change the environement list changes, but the allocated values stayed in the context's gc.

I modified the environement values to always allocate using malloc() and always free() values when the env list changes.

I've committed a fix for this issue:

------------------------------------------------------------------------
r2981 | james | 2008-06-04 01:23:20 -0600 (Wed, 04 Jun 2008) | 6 lines Changed paths:
M /branches/BETA21/openvpn/debug/valgrind-suppress
M /branches/BETA21/openvpn/doval
M /branches/BETA21/openvpn/openvpn.c

Fixed unbounded memory growth bug in environmental variable code that could have caused long-running OpenVPN sessions with many TLS renegotiations to incrementally increase memory usage over time.

------------------------------------------------------------------------

James