Skip to contentSkip to conversation

handle strdup errors

PATCH In review Ilia Shipitsin 4 participants 18 messages Jul 8, 2024 – Sep 9, 2024

lore
Share & tools
lore
#0 lore raw

cover handle strdup errors

strdup might return NULL in case of failure, which is sometimes not handled

Ilia Shipitsin (5):
src/openvpn/init.c: handle strdup failures sample/sample-plugins/defer/multi-auth.c: handle strdup errors src/openvpn/auth_token.c: handle strdup errors src/openvpn/ssl_verify.c: handle strdup errors tests/unit_tests/openvpn/test_auth_token.c: handle strdup errors

 sample/sample-plugins/defer/multi-auth.c   | 15 +++++++++++++++
 src/openvpn/auth_token.c                   |  8 ++++++++
 src/openvpn/init.c                         |  4 ++++
 src/openvpn/ssl_verify.c                   |  8 ++++++++
 tests/unit_tests/openvpn/test_auth_token.c |  2 ++
 5 files changed, 37 insertions(+)
Signature · 1 line
2.43.0.windows.1
#1 lore raw

PATCH 1/5 src/openvpn/init.c: handle strdup failures

---
 src/openvpn/init.c | 4 ++++
 1 file changed, 4 insertions(+)
src/openvpn/init.c +4 -0
index a49e5639..59205ba6 100644
@@ -4967,6 +4967,10 @@ write_pid_file(const char *filename, const char *chroot_dir)
         if (!chroot_dir)
         {
             saved_pid_file_name = strdup(filename);
+            if (!saved_pid_file_name)
+            {
+                msg( M_FATAL, "Failed allocate memory saved_pid_file_name");
+            }
         }
     }
 }
Signature · 1 line
2.43.0.windows.1
#8 lore raw

PATCH 2/5 sample/sample-plugins/defer/multi-auth.c: handle strdup errors

In reply to Ilia Shipitsin
---
 sample/sample-plugins/defer/multi-auth.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
sample/sample-plugins/defer/multi-auth.c +15 -0
index 2c482799..109fa40c 100644
@@ -206,9 +206,24 @@ openvpn_plugin_open_v3(const int v3structver,
     if ((args->argv[4]) && !args->argv[5])
     {
         context->authid = strdup(args->argv[1]);
+        if (!context->authid)
+        {
+            plog(context, PLOG_ERR, "Out of memory");
+            goto error;
+        }
         context->test_deferred_auth = atoi_null0(args->argv[2]);
         context->test_valid_user = strdup(args->argv[3]);
+        if (!context->test_valid_user)
+        {
+            plog(context, PLOG_ERR, "Out of memory");
+            goto error;
+        }
         context->test_valid_pass = strdup(args->argv[4]);
+        if (!context->test_valid_pass)
+        {
+            plog(context, PLOG_ERR, "Out of memory");
+            goto error;
+        }
     }
     else
     {
Signature · 1 line
2.43.0.windows.1
#11 lore raw

PATCH 3/5 src/openvpn/auth_token.c: handle strdup errors

In reply to Ilia Shipitsin
---
 src/openvpn/auth_token.c | 8 ++++++++
 1 file changed, 8 insertions(+)
src/openvpn/auth_token.c +8 -0
index 6787ea7d..2278afe6 100644
@@ -260,6 +260,10 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi)
     /* 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));
+    if (!multi->auth_token)
+    {
+        msg( M_FATAL, "Failed allocate memory for multi->auth_token");
+    }
 
     dmsg(D_SHOW_KEYS, "Generated token for client: %s (%s)",
          multi->auth_token, up->username);
@@ -271,6 +275,10 @@ generate_auth_token(const struct user_pass *up, struct tls_multi *multi)
          * and timestamp in updates
          */
         multi->auth_token_initial = strdup(multi->auth_token);
+        if (!multi->auth_token_initial)
+        {
+            msg( M_FATAL, "Failed allocate memory for multi->auth_token_initial");
+        }
     }
 
     gc_free(&gc);
Signature · 1 line
2.43.0.windows.1
#14 lore raw

PATCH 4/5 src/openvpn/ssl_verify.c: handle strdup errors

In reply to Ilia Shipitsin
---
 src/openvpn/ssl_verify.c | 8 ++++++++
 1 file changed, 8 insertions(+)
src/openvpn/ssl_verify.c +8 -0
index 934ff8d8..c4b95553 100644
@@ -1629,6 +1629,10 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi,
         if (!multi->auth_token_initial)
         {
             multi->auth_token_initial = strdup(up->password);
+            if (!multi->auth_token_initial)
+            {
+                msg( M_FATAL, "Failed allocate memory for multi->auth_token_initial");
+            }
         }
 
         if (session->opt->auth_token_call_auth)
@@ -1749,6 +1753,10 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi,
                 && !(ks->auth_token_state_flags & AUTH_TOKEN_EXPIRED))
             {
                 multi->auth_token = strdup(up->password);
+                if (!multi->auth_token)
+                {
+                    msg( M_FATAL, "Failed allocate memory for multi->auth_token");
+                }
             }
 
             /*
Signature · 1 line
2.43.0.windows.1
#15 lore raw

PATCH 5/5 tests/unit_tests/openvpn/test_auth_token.c: handle strdup errors

In reply to Ilia Shipitsin
---
 tests/unit_tests/openvpn/test_auth_token.c | 2 ++
 1 file changed, 2 insertions(+)
tests/unit_tests/openvpn/test_auth_token.c +2 -0
index 3a3cb69b..8a0c16af 100644
@@ -245,6 +245,7 @@ auth_token_test_known_keys(void **state)
     now = 0;
     /* Preload the session id so the same session id is used here */
     ctx->multi.auth_token_initial = strdup(now0key0);
+    assert_non_null(ctx->multi.auth_token_initial);
 
     /* Zero the hmac part to ensure we have a newly generated token */
     zerohmac(ctx->multi.auth_token_initial);
@@ -374,6 +375,7 @@ auth_token_test_random_keys(void **state)
     now = 0x5c331e9c;
     /* Preload the session id so the same session id is used here */
     ctx->multi.auth_token_initial = strdup(random_token);
+    assert_non_null(ctx->multi.auth_token_initial);
 
     free_key_ctx(&ctx->multi.opt.auth_token_key);
     auth_token_init_secret(&ctx->multi.opt.auth_token_key, random_key, true);
Signature · 1 line
2.43.0.windows.1
#2 lore raw

Re: [Openvpn-devel] [PATCH 1/5] src/openvpn/init.c: handle strdup failures

In reply to Ilia Shipitsin

Hi,

Quoted 10 lines · Ilia Shipitsin
---
  src/openvpn/init.c | 4 ++++
  1 file changed, 4 insertions(+)
src/openvpn/init.c +0 -0
index a49e5639..59205ba6 100644

src/openvpn/init.c write_pid_file(const char *filename, const char *chroot_dir) · line 4972

Quoted 6 lines
src/openvpn/init.c +3 -0
@@ -4967,6 +4967,10 @@ write_pid_file(const char *filename, const char *chroot_dir)
          if (!chroot_dir)
          {
              saved_pid_file_name = strdup(filename);
+            if (!saved_pid_file_name)
+            {
+                msg( M_FATAL, "Failed allocate memory saved_pid_file_name");

patchset looks great, but (!!) there should be no space after the opening parenthesis..

Cheers,

Quoted 4 lines
src/openvpn/init.c +1 -0
+            }
          }
      }
  }
Signature · 1 line
Antonio Quartulli
#3 lore raw

Re: [Openvpn-devel] [PATCH 1/5] src/openvpn/init.c: handle strdup failures

пн, 8 июл. 2024 г. в 23:13, Antonio Quartulli <[email protected]...>:

Quoted 9 lines

Hi,

Quoted 5 lines · Ilia Shipitsin
---
  src/openvpn/init.c | 4 ++++
  1 file changed, 4 insertions(+)
Quoted 14 lines

src/openvpn/init.c write_pid_file(const char *filename, const char *chroot_dir) · line 4972

Quoted 11 lines
src/openvpn/init.c +3 -0
index a49e5639..59205ba6 100644
@@ -4967,6 +4967,10 @@ write_pid_file(const char *filename, const char *chroot_dir)
          if (!chroot_dir)
          {
              saved_pid_file_name = strdup(filename);
+            if (!saved_pid_file_name)
+            {
+                msg( M_FATAL, "Failed allocate memory saved_pid_file_name");

patchset looks great, but (!!) there should be no space after the opening parenthesis..

I just followed code style already present in repo

$ grep -r 'msg( M_FATAL' .
grep: ./.git/objects/pack/pack-d09f3eabac42224b1dc52ffb4f53d9af23bc5b7d.pack:
binary file matches
./src/openvpn/auth_token.c:        msg( M_FATAL, "Failed to get enough
randomness for "
./src/openvpn/tun.c:            msg( M_FATAL, "init_tun: problem
converting IPv6 ifconfig addresses %s and %s to binary",
ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
./src/openvpn/tun.c:            msg( M_FATAL, "cannot find unused tap device" );
./src/openvpn/tun.c:            msg( M_FATAL, "TAP device name must be
'--dev tapNNNN'" );

also, uncrustify GHA jobs agreed that it is no formatting violation

Quoted 10 lines

Cheers,

src/openvpn/init.c write_pid_file(const char *filename, const char *chroot_dir) · line 4976

Quoted 4 lines
src/openvpn/init.c +1 -0
+            }
          }
      }
  }

--
Antonio Quartulli

#4 lore raw

Re: [Openvpn-devel] [PATCH 1/5] src/openvpn/init.c: handle strdup failures

Hi,

Quoted 7 lines · Илья Шипицин
Quoted 4 lines
Quoted 1 line

+ msg( M_FATAL, "Failed allocate memory saved_pid_file_name");

patchset looks great, but (!!) there should be no space after the opening parenthesis..

I just followed code style already present in repo

Quoted 11 lines
$ grep -r 'msg( M_FATAL' .
grep: ./.git/objects/pack/pack-d09f3eabac42224b1dc52ffb4f53d9af23bc5b7d.pack:
binary file matches
./src/openvpn/auth_token.c:        msg( M_FATAL, "Failed to get enough
randomness for "
./src/openvpn/tun.c:            msg( M_FATAL, "init_tun: problem
converting IPv6 ifconfig addresses %s and %s to binary",
ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
./src/openvpn/tun.c:            msg( M_FATAL, "cannot find unused tap device" );
./src/openvpn/tun.c:            msg( M_FATAL, "TAP device name must be
'--dev tapNNNN'" );

Unfortunately those are unlucky leftovers that haven't been fixed yet:

$ grep -r 'msg(M_FATAL' . |wc -l
286
$ grep -r 'msg( M_FATAL' . |wc -l
4
Quoted 2 lines

also, uncrustify GHA jobs agreed that it is no formatting violation

doubly unfortunate as I think uncrustify can't force this or it gets confused somehow :(

Cheers,

Signature · 1 line
Antonio Quartulli
#5 lore raw

Re: [Openvpn-devel] [PATCH 1/5] src/openvpn/init.c: handle strdup failures

пн, 8 июл. 2024 г. в 23:47, Antonio Quartulli <[email protected]...>:

Quoted 28 lines

Hi,

Quoted 7 lines · Илья Шипицин
Quoted 4 lines
Quoted 1 line

+ msg( M_FATAL, "Failed allocate memory saved_pid_file_name");

patchset looks great, but (!!) there should be no space after the opening parenthesis..

I just followed code style already present in repo

Quoted 11 lines
$ grep -r 'msg( M_FATAL' .
grep: ./.git/objects/pack/pack-d09f3eabac42224b1dc52ffb4f53d9af23bc5b7d.pack:
binary file matches
./src/openvpn/auth_token.c:        msg( M_FATAL, "Failed to get enough
randomness for "
./src/openvpn/tun.c:            msg( M_FATAL, "init_tun: problem
converting IPv6 ifconfig addresses %s and %s to binary",
ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
./src/openvpn/tun.c:            msg( M_FATAL, "cannot find unused tap device" );
./src/openvpn/tun.c:            msg( M_FATAL, "TAP device name must be
'--dev tapNNNN'" );

Unfortunately those are unlucky leftovers that haven't been fixed yet:

$ grep -r 'msg(M_FATAL' . |wc -l
286
Quoted 8 lines
$ grep -r 'msg( M_FATAL' . |wc -l
4
Quoted 2 lines

also, uncrustify GHA jobs agreed that it is no formatting violation

doubly unfortunate as I think uncrustify can't force this or it gets confused somehow :(

if those spaces makes you unhappy, I'm fine if you fix them during commit apply

Quoted 8 lines

Cheers,

--
Antonio Quartulli

#6 lore raw

Re: [Openvpn-devel] [PATCH 1/5] src/openvpn/init.c: handle strdup failures

Quoted 33 lines · Илья Шипицин

пн, 8 июл. 2024 г. в 23:47, Antonio Quartulli <[email protected]...>:

Quoted 32 lines

Hi,

Quoted 7 lines · Илья Шипицин
Quoted 4 lines
Quoted 1 line

+ msg( M_FATAL, "Failed allocate memory saved_pid_file_name");

patchset looks great, but (!!) there should be no space after the opening parenthesis..

I just followed code style already present in repo

Quoted 11 lines
$ grep -r 'msg( M_FATAL' .
grep: ./.git/objects/pack/pack-d09f3eabac42224b1dc52ffb4f53d9af23bc5b7d.pack:
binary file matches
./src/openvpn/auth_token.c:        msg( M_FATAL, "Failed to get enough
randomness for "
./src/openvpn/tun.c:            msg( M_FATAL, "init_tun: problem
converting IPv6 ifconfig addresses %s and %s to binary",
ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
./src/openvpn/tun.c:            msg( M_FATAL, "cannot find unused tap device" );
./src/openvpn/tun.c:            msg( M_FATAL, "TAP device name must be
'--dev tapNNNN'" );

Unfortunately those are unlucky leftovers that haven't been fixed yet:

$ grep -r 'msg(M_FATAL' . |wc -l
286
$ grep -r 'msg( M_FATAL' . |wc -l
4
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

also, uncrustify GHA jobs agreed that it is no formatting violation

doubly unfortunate as I think uncrustify can't force this or it gets confused somehow :(

if those spaces makes you unhappy, I'm fine if you fix them during commit apply

I don't think Gert like to change code lines while committing a patch (We can wait for him to say something)

However, I can also offer myself to send a patch with a full cleanup afterwards.

Cheers,

Signature · 1 line
Antonio Quartulli
Acked-by on v1 5/5
#16 lore raw

Re: [Openvpn-devel] [PATCH 5/5] tests/unit_tests/openvpn/test_auth_token.c: handle strdup errors

In reply to Ilia Shipitsin
Quoted 18 lines · Ilia Shipitsin
---
 tests/unit_tests/openvpn/test_auth_token.c | 2 ++
 1 file changed, 2 insertions(+)
tests/unit_tests/openvpn/test_auth_token.c +1 -0
index 3a3cb69b..8a0c16af 100644
@@ -245,6 +245,7 @@ auth_token_test_known_keys(void **state)
     now = 0;
     /* Preload the session id so the same session id is used here */
     ctx->multi.auth_token_initial = strdup(now0key0);
+    assert_non_null(ctx->multi.auth_token_initial);
 
     /* Zero the hmac part to ensure we have a newly generated token */
     zerohmac(ctx->multi.auth_token_initial);

tests/unit_tests/openvpn/test_auth_token.c auth_token_test_random_keys(void **state) · line 381

Quoted 7 lines
tests/unit_tests/openvpn/test_auth_token.c +1 -0
@@ -374,6 +375,7 @@ auth_token_test_random_keys(void **state)
     now = 0x5c331e9c;
     /* Preload the session id so the same session id is used here */
     ctx->multi.auth_token_initial = strdup(random_token);
+    assert_non_null(ctx->multi.auth_token_initial);
 
     free_key_ctx(&ctx->multi.opt.auth_token_key);
     auth_token_init_secret(&ctx->multi.opt.auth_token_key, random_key, true);

Regards,

Signature · 1 line
  Frank Lichtenheld
Acked-by on v1 2/5
#9 lore raw

Re: [Openvpn-devel] [PATCH 2/5] sample/sample-plugins/defer/multi-auth.c: handle strdup errors

In reply to Ilia Shipitsin
Quoted 24 lines · Ilia Shipitsin
---
 sample/sample-plugins/defer/multi-auth.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
sample/sample-plugins/defer/multi-auth.c +9 -0
index 2c482799..109fa40c 100644
@@ -206,9 +206,24 @@ openvpn_plugin_open_v3(const int v3structver,
     if ((args->argv[4]) && !args->argv[5])
     {
         context->authid = strdup(args->argv[1]);
+        if (!context->authid)
+        {
+            plog(context, PLOG_ERR, "Out of memory");
+            goto error;
+        }
         context->test_deferred_auth = atoi_null0(args->argv[2]);
         context->test_valid_user = strdup(args->argv[3]);
+        if (!context->test_valid_user)
+        {
+            plog(context, PLOG_ERR, "Out of memory");
+            goto error;

sample/sample-plugins/defer/multi-auth.c openvpn_plugin_open_v3(const int v3structver, · line 229

Quoted 10 lines
sample/sample-plugins/defer/multi-auth.c +6 -0
+        }
         context->test_valid_pass = strdup(args->argv[4]);
+        if (!context->test_valid_pass)
+        {
+            plog(context, PLOG_ERR, "Out of memory");
+            goto error;
+        }
     }
     else
     {

A bit ugly. Might be nicer to combine the error handling into one if check, instead of doing separate ones for each call. But OTOH no need to overengineer sample code. So

Regards,

Signature · 1 line
  Frank Lichtenheld
Acked-by on v1 1/5
#7 lore raw

[Openvpn-devel] [PATCH applied] Re: src/openvpn/init.c: handle strdup failures

In reply to Ilia Shipitsin

Taken the "patchset looks great" from Antonio as ACK, fixed the "msg( M_FATAL," space on the go (trivial whitespace fixes are acceptable).

Not tested beyond minimal compile test and stare-at-code.

Your patch has been applied to the master branch.

commit b7c6920eab5d56067a69805f64239b8dd5a0ae27

Author: Ilia Shipitsin
Date:   Mon Jul 8 23:08:18 2024 +0200
     src/openvpn/init.c: handle strdup failures

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

--
kind regards,

Gert Doering

#10 lore raw

[Openvpn-devel] [PATCH applied] Re: sample/sample-plugins/defer/multi-auth.c: handle strdup errors

In reply to Ilia Shipitsin

Stared-at-code, and compile-tested. Agree with Frank :-)

Your patch has been applied to the master branch.

commit aef8a872aa51331f781265fdb6b3c340463637a8

Author: Ilia Shipitsin
Date:   Mon Jul 8 23:08:19 2024 +0200
     sample/sample-plugins/defer/multi-auth.c: handle strdup errors

     Signed-off-by: Ilia Shipitsin <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg28886.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#12 lore raw

Re: [Openvpn-devel] [PATCH 3/5] src/openvpn/auth_token.c: handle strdup errors

In reply to Ilia Shipitsin

Hi,

src/openvpn/auth_token.c generate_auth_token(const struct user_pass *up, struct tls_multi *multi) · line 266

Quoted 5 lines · Ilia Shipitsin
     multi->auth_token = strdup((char *)BPTR(&session_token));
src/openvpn/auth_token.c +4 -0
+    if (!multi->auth_token)
+    {
+        msg( M_FATAL, "Failed allocate memory for multi->auth_token");
+    }

I do wonder if this is the right approach here. For "openvpn itself" we have the check_malloc_return() macro, which will purposely not call msg() - as msg() does internal malloc()s, and if we cannot allocate 20 bytes for an auth_token, the chance that msg() will not succeed is fairly high...

In plugins or unit tests, the infrastructure is different, but for 3/ and 4/, please resend with

      multi->auth_token = strdup((char *)BPTR(&session_token));

+ check_malloc_return(multi->auth_token);

(etc)

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]...
#17 lore raw

[Openvpn-devel] [PATCH applied] Re: tests/unit_tests/openvpn/test_auth_token.c: handle strdup errors

In reply to Ilia Shipitsin

Your patch has been applied to the master branch.

commit 611fa55ed1ef7e78e6015e77ace19aa4b2bf744e

Author: Ilia Shipitsin
Date:   Mon Jul 8 23:08:22 2024 +0200
     tests/unit_tests/openvpn/test_auth_token.c: handle strdup errors

     Signed-off-by: Ilia Shipitsin <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg28882.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#13 lore raw

Re: [Openvpn-devel] [PATCH 3/5] src/openvpn/auth_token.c: handle strdup errors

In reply to Gert Doering

I'll have a look, thanks!

Quoted 35 lines · On Mon, Sep 9, 2024, 09:30 Gert Doering <[email protected]...> wrote:

Hi,

src/openvpn/auth_token.c generate_auth_token(const struct user_pass *up, struct tls_multi *multi) · line 266

Quoted 5 lines · Ilia Shipitsin
     multi->auth_token = strdup((char *)BPTR(&session_token));
src/openvpn/auth_token.c +4 -0
+    if (!multi->auth_token)
+    {
+        msg( M_FATAL, "Failed allocate memory for multi->auth_token");
+    }

I do wonder if this is the right approach here. For "openvpn itself" we have the check_malloc_return() macro, which will purposely not call msg() - as msg() does internal malloc()s, and if we cannot allocate 20 bytes for an auth_token, the chance that msg() will not succeed is fairly high...

In plugins or unit tests, the infrastructure is different, but for 3/ and 4/, please resend with

      multi->auth_token = strdup((char *)BPTR(&session_token));

+ check_malloc_return(multi->auth_token);

(etc)

gert
--
"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]...