Browse code

sample-plugins: Fix memleak in client-connect example plugin

I was looking for memleaks in the code and found
this one with cppcheck. Only an example, but no
need to leave this bug in it.

Also fix fortify problem in keying-material-exporter-demo
so I can actually test the compilation of the sample
plugins.

v2:
- remove unneccessary usages of snprintf, replace
with strncpy.

Change-Id: Ibd1b282afc4a28768be3f165f84ab60ca4d24a9b
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20230516093534.26384-1-frank@lichtenheld.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg26668.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Frank Lichtenheld authored on 2023/05/16 18:35:34
Showing 2 changed files
... ...
@@ -454,6 +454,9 @@ openvpn_plugin_client_connect_v2(struct plugin_context *context,
454 454
     if (!rl->name || !rl->value)
455 455
     {
456 456
         plugin_log(PLOG_ERR, MODULE, "malloc(return_list->xx) failed");
457
+        free(rl->name);
458
+        free(rl->value);
459
+        free(rl);
457 460
         return OPENVPN_PLUGIN_FUNC_ERROR;
458 461
     }
459 462
 
... ...
@@ -509,6 +512,9 @@ openvpn_plugin_client_connect_defer_v2(struct plugin_context *context,
509 509
     if (!rl->name || !rl->value)
510 510
     {
511 511
         plugin_log(PLOG_ERR, MODULE, "malloc(return_list->xx) failed");
512
+        free(rl->name);
513
+        free(rl->value);
514
+        free(rl);
512 515
         return OPENVPN_PLUGIN_FUNC_ERROR;
513 516
     }
514 517
 
... ...
@@ -155,7 +155,7 @@ session_user_set(struct session *sess, X509 *x509)
155 155
 
156 156
         if (!strncasecmp(objbuf, "CN", 2))
157 157
         {
158
-            snprintf(sess->user, sizeof(sess->user) - 1, (char *)buf);
158
+            strncpy(sess->user, (char *)buf, sizeof(sess->user) - 1);
159 159
         }
160 160
 
161 161
         OPENSSL_free(buf);
... ...
@@ -234,7 +234,7 @@ tls_final(struct openvpn_plugin_args_func_in const *args,
234 234
         return OPENVPN_PLUGIN_FUNC_ERROR;
235 235
     }
236 236
 
237
-    snprintf(sess->key, sizeof(sess->key) - 1, "%s", key);
237
+    strncpy(sess->key, key, sizeof(sess->key) - 1);
238 238
     ovpn_note("app session key:  %s", sess->key);
239 239
 
240 240
     switch (plugin->type)