Skip to contentSkip to conversation

tls-crypt-v2: fix server memory leak

PATCH Superseded Steffan Karger 4 participants 8 messages Dec 3, 2020 – Dec 4, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tls-crypt-v2: fix server memory leak

tls-crypt-v2 was developed in parallel with the changes that allowed to use tls-auth/tls-crypt in connection blocks. The tls-crypt-v2 patch set was never updated to the new reality after commit 5817b49b, causing a memory leak of about 600 bytes for each connecting client.

It would be nicer to not reload the tls-crypt-v2 server key for each connecting client, but that requires more refactoring (and thus more time to get right). So for now just plug the leak by free'ing the memory when we close a client connection.

To test this easily, compile openvpn with -fsanity=address, run a server with tls-crypt-v2, connect a client, stop the server.

---
 src/openvpn/init.c | 1 +
 1 file changed, 1 insertion(+)
src/openvpn/init.c +1 -0
index 27a4170d..5cde8a4b 100644
@@ -3619,6 +3619,7 @@ do_close_free_key_schedule(struct context *c, bool free_ssl_ctx)
      * always free the tls_auth/crypt key. If persist_key is true, the key will
      * be reloaded from memory (pre-cached)
      */
+    free_key_ctx(&c->c1.ks.tls_crypt_v2_server_key);
     free_key_ctx_bi(&c->c1.ks.tls_wrap_key);
     CLEAR(c->c1.ks.tls_wrap_key);
     buf_clear(&c->c1.ks.tls_crypt_v2_wkc);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 tls-crypt-v2: also preload tls-crypt-v2 keys (if --persist-key)

This allows tls-crypt-v2 servers to drop privileges after reading the keys. Without it, the server would try to read the key file for each connecting client. (And clients for each reconnect.)

As with the previous patch, the pre-loading was developed in parallel with tls-crypt-v2, and the tls-crypt-v2 patches were never amended to implement the pre-loading.

Also as with the previous patch, it would be nicer if servers would not reload the tls-crypt-v2 server key for each connecting client. But let's first fix the issue, and see if we can improve later.

---
 src/openvpn/options.c | 52 +++++++++++++++++++++----------------------
 1 file changed, 25 insertions(+), 27 deletions(-)
src/openvpn/options.c +25 -27
index 21f8d494..599f534c 100644
@@ -1980,6 +1980,23 @@ connection_entry_load_re(struct connection_entry *ce, const struct remote_entry
     }
 }
 
+static void
+connection_entry_preload_key(const char **key_file, bool *key_inline,
+                             struct gc_arena *gc)
+{
+    if (key_file && *key_file && !(*key_inline))
+    {
+        struct buffer in = buffer_read_from_file(*key_file, gc);
+        if (!buf_valid(&in))
+        {
+            msg(M_FATAL, "Cannot pre-load keyfile (%s)", *key_file);
+        }
+
+        *key_file = (const char *) in.data;
+        *key_inline = true;
+    }
+}
+
 static void
 options_postprocess_verify_ce(const struct options *options,
                               const struct connection_entry *ce)
@@ -2931,36 +2948,17 @@ options_postprocess_mutate_ce(struct options *o, struct connection_entry *ce)
         ce->tls_crypt_v2_file_inline = o->tls_crypt_v2_file_inline;
     }
 
-    /* pre-cache tls-auth/crypt key file if persist-key was specified and keys
-     * were not already embedded in the config file
+    /* Pre-cache tls-auth/crypt(-v2) key file if persist-key was specified and
+     * keys were not already embedded in the config file.
      */
     if (o->persist_key)
     {
-        if (ce->tls_auth_file && !ce->tls_auth_file_inline)
-        {
-            struct buffer in = buffer_read_from_file(ce->tls_auth_file, &o->gc);
-            if (!buf_valid(&in))
-            {
-                msg(M_FATAL, "Cannot pre-load tls-auth keyfile (%s)",
-                    ce->tls_auth_file);
-            }
-
-            ce->tls_auth_file = (char *)in.data;
-            ce->tls_auth_file_inline = true;
-        }
-
-        if (ce->tls_crypt_file && !ce->tls_crypt_file_inline)
-        {
-            struct buffer in = buffer_read_from_file(ce->tls_crypt_file, &o->gc);
-            if (!buf_valid(&in))
-            {
-                msg(M_FATAL, "Cannot pre-load tls-crypt keyfile (%s)",
-                    ce->tls_crypt_file);
-            }
-
-            ce->tls_crypt_file = (char *)in.data;
-            ce->tls_crypt_file_inline = true;
-        }
+        connection_entry_preload_key(&ce->tls_auth_file,
+                                     &ce->tls_auth_file_inline, &o->gc);
+        connection_entry_preload_key(&ce->tls_crypt_file,
+                                     &ce->tls_crypt_file_inline, &o->gc);
+        connection_entry_preload_key(&ce->tls_crypt_v2_file,
+                                     &ce->tls_crypt_v2_file_inline, &o->gc);
     }
 }
 
Signature · 1 line
2.25.1
Acked-by on v1 2/2
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/2] tls-crypt-v2: also preload tls-crypt-v2 keys (if --persist-key)

Am 03.12.20 um 16:49 schrieb Steffan Karger:

Quoted 11 lines

This allows tls-crypt-v2 servers to drop privileges after reading the keys. Without it, the server would try to read the key file for each connecting client. (And clients for each reconnect.)

As with the previous patch, the pre-loading was developed in parallel with tls-crypt-v2, and the tls-crypt-v2 patches were never amended to implement the pre-loading.

Also as with the previous patch, it would be nicer if servers would not reload the tls-crypt-v2 server key for each connecting client. But let's first fix the issue, and see if we can improve later.

Agreed. One thing at a time.

#3 lore raw

[Openvpn-devel] [PATCH applied] Re: tls-crypt-v2: also preload tls-crypt-v2 keys (if --persist-key)

In reply to Steffan Karger

As for 1/2, I have not really reviewed or tested anything here. Trusting you and Arne here.

From a quick view this looks like a nice "code cleanup en passant", though :-) (and it does pass the client side tests, of course).

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

commit 4d307ed431bf18d554f524ebaf111f5e136147fe (master) commit bac760f84b39bf8f6b97778a15e5fcf863f6d366 (release/2.5)

Author: Steffan Karger
Date:   Thu Dec 3 16:49:51 2020 +0100
     tls-crypt-v2: also preload tls-crypt-v2 keys (if --persist-key)

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

--
kind regards,

Gert Doering

#4 lore raw
In reply to Steffan Karger

Hi Steffan,

src/openvpn/init.c do_close_free_key_schedule(struct context *c, bool free_ssl_ctx) · line 3625

Quoted 12 lines · Steffan Karger
src/openvpn/init.c +1 -0
index 27a4170d..5cde8a4b 100644
@@ -3619,6 +3619,7 @@ do_close_free_key_schedule(struct context *c, bool free_ssl_ctx)
      * always free the tls_auth/crypt key. If persist_key is true, the key will
      * be reloaded from memory (pre-cached)
      */
+    free_key_ctx(&c->c1.ks.tls_crypt_v2_server_key);
     free_key_ctx_bi(&c->c1.ks.tls_wrap_key);
     CLEAR(c->c1.ks.tls_wrap_key);
     buf_clear(&c->c1.ks.tls_crypt_v2_wkc);

A few lines below we call key_schedule_free() (under certain conditions) which also performs:

free_key_ctx(&ks->tls_crypt_v2_server_key);

I believe it is safe to call free_key_ctx() twice on the same object, but wouldn't it be better to have it called once only along the same code path?

Regards,

Signature · 1 line
Antonio Quartulli
#5 lore raw

PATCH v2 1/2 tls-crypt-v2: fix server memory leak

tls-crypt-v2 was developed in parallel with the changes that allowed to use tls-auth/tls-crypt in connection blocks. The tls-crypt-v2 patch set was never updated to the new reality after commit 5817b49b, causing a memory leak of about 600 bytes for each connecting client.

It would be nicer to not reload the tls-crypt-v2 server key for each connecting client, but that requires more refactoring (and thus more time to get right). So for now just plug the leak by free'ing the memory when we close a client connection.

To test this easily, compile openvpn with -fsanity=address, run a server with tls-crypt-v2, connect a client, stop the server.

---
v2: remove now-redundant free_key_ctx() from key_schedule_free()

 src/openvpn/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/init.c +1 -1
index 27a4170d..c3493c42 100644
@@ -2561,7 +2561,6 @@ key_schedule_free(struct key_schedule *ks, bool free_ssl_ctx)
     if (tls_ctx_initialised(&ks->ssl_ctx) && free_ssl_ctx)
     {
         tls_ctx_free(&ks->ssl_ctx);
-        free_key_ctx(&ks->tls_crypt_v2_server_key);
     }
     CLEAR(*ks);
 }
@@ -3619,6 +3618,7 @@ do_close_free_key_schedule(struct context *c, bool free_ssl_ctx)
      * always free the tls_auth/crypt key. If persist_key is true, the key will
      * be reloaded from memory (pre-cached)
      */
+    free_key_ctx(&c->c1.ks.tls_crypt_v2_server_key);
     free_key_ctx_bi(&c->c1.ks.tls_wrap_key);
     CLEAR(c->c1.ks.tls_wrap_key);
     buf_clear(&c->c1.ks.tls_crypt_v2_wkc);
Signature · 1 line
2.25.1
Acked-by on v2 1/2
#6 lore raw

Hi,

Quoted 10 lines · Steffan Karger

tls-crypt-v2 was developed in parallel with the changes that allowed to use tls-auth/tls-crypt in connection blocks. The tls-crypt-v2 patch set was never updated to the new reality after commit 5817b49b, causing a memory leak of about 600 bytes for each connecting client.

It would be nicer to not reload the tls-crypt-v2 server key for each connecting client, but that requires more refactoring (and thus more time to get right). So for now just plug the leak by free'ing the memory when we close a client connection.

Quoted 4 lines

To test this easily, compile openvpn with -fsanity=address, run a server with tls-crypt-v2, connect a client, stop the server.

Signature · 1 line
Antonio Quartulli
#7 lore raw
In reply to Steffan Karger

I have not done much testing, and not even stared much at the code. But it's your code and Antonio has a keen eye :-)

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

commit fb169c3b8fdfa9792c0eee8441956f062dfd7982 (master) commit 06e769552481729ddae28ee46b30f2dc8ca77509 (release/2.5)

Author: Steffan Karger
Date:   Thu Dec 3 19:22:30 2020 +0100
     tls-crypt-v2: fix server memory leak

     Signed-off-by: Steffan Karger <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg21310.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering