[Openvpn-devel,v2] Check the result of unchecked allocations in the server

Message ID 20261115134718.22576-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v2] Check the result of unchecked allocations in the server |

Commit Message

Gert Doering Nov. 15, 2026, 1:47 p.m. UTC
  From: Alexandre Laroche <larochealexandre@hotmail.com>

Several allocations were used without a NULL check, so a failed
allocation crashed the server on a NULL pointer instead of stopping
with "Out of Memory" like the rest of the code:

- multi_init(): calloc() of m->instances; the server started and then
  crashed in multi_assign_peer_id() on the first client
- generate_auth_token(), verify_user_pass(): strdup() of auth tokens;
  generate_auth_token() then called strdup(NULL)
- override_locked_username(): strdup() of the override username; the
  strlcpy() from locked_username then crashed (with --auth-gen-token)
- backend_x509_get_serial_hex(): malloc() of the serial buffer, which
  BN_bn2binpad() then wrote to

Use string_alloc(..., NULL) instead of strdup(), add
check_malloc_return() after the calloc(), and allocate the serial
buffer with gc_malloc(), which also copes with a zero serial number
(BN_num_bytes() is 0 there, and malloc(0) may return NULL).

GitHub: closes OpenVPN/openvpn#1142

Assisted-by: Claude Code:claude-opus-5-5
Change-Id: I603321ae6893dbdb054deae5a9bff5e52cbb7429
Signed-off-by: Alexandre Laroche <larochealexandre@hotmail.com>
Acked-by: Razvan Cojocaru <razvanc@mailbox.org>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2155
---

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/+/2155
This mail reflects revision 2 of this Change.

Acked-by according to Gerrit (reflected above):
Razvan Cojocaru <razvanc@mailbox.org>
  

Comments

Gert Doering Nov. 15, 2026, 6:10 p.m. UTC | #1
Thanks for helping make OpenVPN more robust - even if we memory is usually
plentiful, there's always a Firefox process eating all of it :-) - so we
need to be ready...

Stared a bit at the code (all reasonable changes), ran GHA / t_server tests
on the 2.7 backport.

Your patch has been applied to the master and release/2.7 branch
(bugfix/hardening).

commit df3f9099bd16670a0a8a8571b4a7c9143c150d83 (master)
commit b98435d81774c5404ca3fb70d7a3842f8fe226b6 (release/2.7)
Author: Alexandre Laroche
Date:   Sun Nov 15 14:47:13 2026 +0100

     Check the result of unchecked allocations in the server

     Signed-off-by: Alexandre Laroche <larochealexandre@hotmail.com>
     Acked-by: Razvan Cojocaru <razvanc@mailbox.org>
     Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2155
     Message-Id: <20261115134718.22576-1-gert@greenie.muc.de>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg41355.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering
  

Patch

diff --git a/src/openvpn/auth_token.c b/src/openvpn/auth_token.c
index 84558af..10c656e 100644
--- a/src/openvpn/auth_token.c
+++ b/src/openvpn/auth_token.c
@@ -275,7 +275,7 @@ 
 
     /* free the auth-token if defined, we will replace it with a new one */
     free(multi->auth_token);
-    multi->auth_token = strdup((char *)BPTR(&session_token));
+    multi->auth_token = string_alloc((char *)BPTR(&session_token), NULL);
 
     dmsg(D_SHOW_KEYS, "Generated token for client: %s (%s)", multi->auth_token, up->username);
 
@@ -285,7 +285,7 @@ 
          * Save the initial auth token to continue using the same session ID
          * and timestamp in updates
          */
-        multi->auth_token_initial = strdup(multi->auth_token);
+        multi->auth_token_initial = string_alloc(multi->auth_token, NULL);
     }
 
     gc_free(&gc);
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index 3f514d6..662fc9d 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -395,6 +395,7 @@ 
     m->max_clients = t->options.max_clients;
 
     m->instances = calloc(m->max_clients, sizeof(struct multi_instance *));
+    check_malloc_return(m->instances);
 
     m->top.c2.event_set = t->c2.event_set;
 
@@ -2653,7 +2654,7 @@ 
         }
 
         multi->locked_original_username = multi->locked_username;
-        multi->locked_username = strdup(options->override_username);
+        multi->locked_username = string_alloc(options->override_username, NULL);
 
         /* Override also the common name if username should be set as common
          * name */
diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c
index 0cdec1e..9b3f86d 100644
--- a/src/openvpn/ssl_verify.c
+++ b/src/openvpn/ssl_verify.c
@@ -1658,7 +1658,7 @@ 
          * using the same session ID and initial timestamp in new tokens */
         if (!multi->auth_token_initial && (ks->auth_token_state_flags & AUTH_TOKEN_HMAC_OK))
         {
-            multi->auth_token_initial = strdup(up->password);
+            multi->auth_token_initial = string_alloc(up->password, NULL);
         }
 
         if (session->opt->auth_token_call_auth)
@@ -1777,7 +1777,7 @@ 
             if (!multi->auth_token && (ks->auth_token_state_flags & AUTH_TOKEN_HMAC_OK)
                 && !(ks->auth_token_state_flags & AUTH_TOKEN_EXPIRED))
             {
-                multi->auth_token = strdup(up->password);
+                multi->auth_token = string_alloc(up->password, NULL);
             }
 
             /*
diff --git a/src/openvpn/ssl_verify_openssl.c b/src/openvpn/ssl_verify_openssl.c
index 0c36cf2..3f1200d 100644
--- a/src/openvpn/ssl_verify_openssl.c
+++ b/src/openvpn/ssl_verify_openssl.c
@@ -323,11 +323,10 @@ 
     const ASN1_INTEGER *asn1_i = X509_get_serialNumber(cert);
     BIGNUM *bn_serial = ASN1_INTEGER_to_BN(asn1_i, NULL);
     int len_serial = BN_num_bytes(bn_serial);
-    unsigned char *buf = malloc(len_serial);
+    unsigned char *buf = gc_malloc(len_serial, false, gc);
     BN_bn2binpad(bn_serial, buf, len_serial);
 
     char *ret = format_hex_ex(buf, len_serial, 0, 1, ":", gc);
-    free(buf);
     BN_free(bn_serial);
 
     return ret;