sample-plugins: Fix memleak in client-connect example plugin
PATCH 1/1 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.
--- .../sample-plugins/client-connect/sample-client-connect.c | 6 ++++++ .../keying-material-exporter-demo/keyingmaterialexporter.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-)
sample/sample-plugins/client-connect/sample-client-connect.c +6 -0
@@ -454,6 +454,9 @@ openvpn_plugin_client_connect_v2(struct plugin_context *context,if (!rl->name || !rl->value) {plugin_log (PLOG_ERR ,MODULE ,"malloc(return_list->xx) failed" ); +free (rl->name); +free (rl->value); +free (rl);return OPENVPN_PLUGIN_FUNC_ERROR ; }
@@ -509,6 +512,9 @@ openvpn_plugin_client_connect_defer_v2(struct plugin_context *context,if (!rl->name || !rl->value) {plugin_log (PLOG_ERR ,MODULE ,"malloc(return_list->xx) failed" ); +free (rl->name); +free (rl->value); +free (rl);return OPENVPN_PLUGIN_FUNC_ERROR ; }
sample/sample-plugins/keying-material-exporter-demo/keyingmaterialexporter.c +1 -1
@@ -155,7 +155,7 @@ session_user_set(struct session *sess, X509 *x509)if (!strncasecmp (objbuf,"CN" ,2 )) { -snprintf (sess->user,sizeof (sess->user) -1 , (char *)buf); +snprintf (sess->user,sizeof (sess->user) -1 ,"%s" , (char *)buf); }OPENSSL_free (buf);
Signature · 1 line
2.34.1
Hi,
Quoted 20 lines · Frank Lichtenheld
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.
Change-Id Ibd1b282afc4a28768be3f165f84ab60ca4d24a9bSigned-off-by Frank Lichtenheld <[email protected]...>--- .../sample-plugins/client-connect/sample-client-connect.c | 6 ++++++ .../keying-material-exporter-demo/keyingmaterialexporter.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-)sample/sample-plugins/client-connect/sample-client-connect.c +0
-0
sample/sample-plugins/client-connect/sample-client-connect.c openvpn_plugin_client_connect_v2(struct plugin_context *context, · line 459
Quoted 6 lines
sample/sample-plugins/client-connect/sample-client-connect.c +3
-0@@ -454,6 +454,9 @@ openvpn_plugin_client_connect_v2(struct plugin_context *context,if (!rl->name || !rl->value) {plugin_log (PLOG_ERR ,MODULE ,"malloc(return_list->xx) failed" ); +free (rl->name); +free (rl->value); +free (rl);
ACK on that part..
sample/sample-plugins/client-connect/sample-client-connect.c session_user_set(struct session *sess, X509 *x509) · line 159
Quoted 7 lines
sample/sample-plugins/client-connect/sample-client-connect.c +1
-1@@ -155,7 +155,7 @@ session_user_set(struct session *sess, X509 *x509)if (!strncasecmp (objbuf,"CN" ,2 )) { -snprintf (sess->user,sizeof (sess->user) -1 , (char *)buf); +snprintf (sess->user,sizeof (sess->user) -1 ,"%s" , (char *)buf); }
... but adding "%s" to something that shouldn't have been an snprintf() in the first place feels wrong. Don't we have strncpy() or something for "copy a string over to a limited buffer, and null-terminate"?
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v2 1/1 sample-plugins: Fix memleak in client-connect example plugin
In reply to Frank LichtenheldI 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.
--- .../sample-plugins/client-connect/sample-client-connect.c | 6 ++++++ .../keying-material-exporter-demo/keyingmaterialexporter.c | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-)
sample/sample-plugins/client-connect/sample-client-connect.c +6 -0
@@ -454,6 +454,9 @@ openvpn_plugin_client_connect_v2(struct plugin_context *context,if (!rl->name || !rl->value) {plugin_log (PLOG_ERR ,MODULE ,"malloc(return_list->xx) failed" ); +free (rl->name); +free (rl->value); +free (rl);return OPENVPN_PLUGIN_FUNC_ERROR ; }
@@ -509,6 +512,9 @@ openvpn_plugin_client_connect_defer_v2(struct plugin_context *context,if (!rl->name || !rl->value) {plugin_log (PLOG_ERR ,MODULE ,"malloc(return_list->xx) failed" ); +free (rl->name); +free (rl->value); +free (rl);return OPENVPN_PLUGIN_FUNC_ERROR ; }
sample/sample-plugins/keying-material-exporter-demo/keyingmaterialexporter.c +2 -2
@@ -155,7 +155,7 @@ session_user_set(struct session *sess, X509 *x509)if (!strncasecmp (objbuf,"CN" ,2 )) { -snprintf (sess->user,sizeof (sess->user) -1 , (char *)buf); +strncpy (sess->user, (char *)buf,sizeof (sess->user) -1 ); }OPENSSL_free (buf);
@@ -234,7 +234,7 @@ tls_final(struct openvpn_plugin_args_func_in const *args,return OPENVPN_PLUGIN_FUNC_ERROR ; } -snprintf (sess->key,sizeof (sess->key) -1 ,"%s" , key); +strncpy (sess->key, key,sizeof (sess->key) -1 );ovpn_note ("app session key: %s" , sess->key); switch (plugin->type)
Signature · 1 line
2.34.1
I do not really think this is *necessary* (if malloc() fails for a few bytes, how relevant are these free() calls, and how likely is that the next malloc() inside OpenVPN will just make everything stop?) - but it's good practice, and if it silences semi-irrelevant warnings, the check tools can show us the *real* issues...
I have only test compiled the plugins, not actually run anything.
Your patch has been applied to the master and release/2.6 branch.
commit 2d36678a2be15f7c00a44354ab71e4521ee3a4f3 (master) commit 13b8e155af702a25e86f20da694867c76c169673 (release/2.6)
Author: Frank Lichtenheld Date: Tue May 16 11:35:34 2023 +0200
sample-plugins: Fix memleak in client-connect example plugin
Signed-off-by: Frank Lichtenheld <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg26668.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering