[Openvpn-devel,v1] Null-terminate tls-crypt client keys when testing

Message ID 20260609102407.32590-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v1] Null-terminate tls-crypt client keys when testing |

Commit Message

Gert Doering June 9, 2026, 10:24 a.m. UTC
  From: Max Fillinger <maximilian.fillinger@sentyron.com>

After generating a tls-crypt-v2 client key, OpenVPN will try to load the
generated key to verify that it was generated correctly. If the client
key is not written to disk but printed out on the command line, the PEM
encoded key is stored in memory and read_pem_key_file is called with
key_file_inline = true. However, this key is not a null-terminated
string, so we end up calling strlen on a buffer that isn't
null-terminated.

This commit adds a null-byte at the end of the key.

Change-Id: I2ca8bf90a796f2b757c2fde0ae24468ef3abc3b5
Signed-off-by: Max Fillinger <maximilian.fillinger@sentyron.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1701
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1701
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Gert Doering <gert@greenie.muc.de>
  

Comments

Gert Doering June 10, 2026, 5:08 p.m. UTC | #1
Technically, this is a buffer read-overrun (strlen() being called on
a non-null-terminated buffer) - but since this happens only at command
line usage ("openvpn --gen-key ...") and this is not done in any sort
of privileged context, the consequences are "none".  So not treated as
security issue, just a bug.

Stared at code, ran a few simple tests ("openvpn --genkey tls-crypt..."),
and unit tests.  The code is not in use in "normal" operation, so not
subjected to t_client/t_server tests (beyond what BB already did).

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

commit b2dcffba0820a1a77472658623b5964e88cbb732 (master)
commit 4d22da0260fda3f1797f325f707e438286eebb24 (release/2.7)
commit e13b128112e474beb59f1b38d505a658c054c5b5 (release/2.6)
Author: Max Fillinger
Date:   Tue Jun 9 12:24:01 2026 +0200

     Null-terminate tls-crypt client keys when testing

     Signed-off-by: Max Fillinger <maximilian.fillinger@sentyron.com>
     Acked-by: Gert Doering <gert@greenie.muc.de>
     Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1701
     Message-Id: <20260609102407.32590-1-gert@greenie.muc.de>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg37116.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering
  

Patch

diff --git a/src/openvpn/tls_crypt.c b/src/openvpn/tls_crypt.c
index e91f80c..8c3d722 100644
--- a/src/openvpn/tls_crypt.c
+++ b/src/openvpn/tls_crypt.c
@@ -758,9 +758,10 @@ 
 
     if (!filename || streq(filename, ""))
     {
-        printf("%.*s\n", BLEN(&client_key_pem), BPTR(&client_key_pem));
+        buf_null_terminate(&client_key_pem);
         client_file = (const char *)BPTR(&client_key_pem);
         client_inline = true;
+        printf("%s\n", client_file);
     }
     else if (!buffer_write_file(filename, &client_key_pem))
     {