Skip to contentSkip to conversation

Add missing check for nl_socket_alloc failure

PATCH Superseded Arne Schwabe 3 participants 8 messages Feb 14, 2023 – Dec 2, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add missing check for nl_socket_alloc failure

This can happen if the memory alloc fails.

---
 src/openvpn/dco_linux.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/openvpn/dco_linux.c +6 -2
index c84f9cfe1..b1103c8d5 100644
@@ -79,10 +79,14 @@ typedef int (*ovpn_nl_cb)(struct nl_msg *msg, void *arg);
 static int
 resolve_ovpn_netlink_id(int msglevel)
 {
-    int ret;
     struct nl_sock *nl_sock = nl_socket_alloc();
 
-    ret = genl_connect(nl_sock);
+    if (!nl_sock)
+    {
+        msg(msglevel, "Allocating net link socket failed");
+    }
+
+    int ret = genl_connect(nl_sock);
     if (ret)
     {
         msg(msglevel, "Cannot connect to generic netlink: %s",
Signature · 1 line
2.37.1 (Apple Git-137.1)
#1 lore raw

Hi,

src/openvpn/dco_linux.c typedef int (*ovpn_nl_cb)(struct nl_msg *msg, void *arg); · line 87

Quoted 10 lines · Arne Schwabe

resolve_ovpn_netlink_id(int msglevel)
{

src/openvpn/dco_linux.c +4 -2
-    int ret;
     struct nl_sock *nl_sock = nl_socket_alloc();
 
-    ret = genl_connect(nl_sock);
+    if (!nl_sock)
+    {
+        msg(msglevel, "Allocating net link socket failed");
+    }

... shouldn't we abort in this case, aka, M_FATAL (or "return -errno")?

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

Am 14.02.23 um 15:01 schrieb Gert Doering:

Quoted 8 lines

Hi,

Quoted 5 lines · Arne Schwabe

resolve_ovpn_netlink_id(int msglevel)
{

-    int ret;
      struct nl_sock *nl_sock = nl_socket_alloc();
Quoted 7 lines

src/openvpn/dco_linux.c typedef int (*ovpn_nl_cb)(struct nl_msg *msg, void *arg); · line 87

Quoted 5 lines
src/openvpn/dco_linux.c +4 -1
-    ret = genl_connect(nl_sock);
+    if (!nl_sock)
+    {
+        msg(msglevel, "Allocating net link socket failed");
+    }

... shouldn't we abort in this case, aka, M_FATAL (or "return -errno")?

I don't think so. This function is called from two code paths:

- dco_available: I don't think if detection of dco fails, we should quit openvpn completely.

- ovpn_dco_init_netlink: Here we already call it with M_ERR, which is (M_FATAL | M_ERRNO)

Arne

#3 lore raw

PATCH v2 1/1 Add missing check for nl_socket_alloc failure

In reply to Arne Schwabe

This can happen if the memory alloc fails.

Patch V2: add goto error

Change-Id Iee66caa794d267ac5f8bee584633352893047171
---
 src/openvpn/dco_linux.c | 7 +++++++
 1 file changed, 7 insertions(+)
src/openvpn/dco_linux.c +7 -0
index 41540c0f8..95fe94848 100644
@@ -83,6 +83,13 @@ resolve_ovpn_netlink_id(int msglevel)
     int ret;
     struct nl_sock *nl_sock = nl_socket_alloc();
 
+    if (!nl_sock)
+    {
+        msg(msglevel, "Allocating net link socket failed");
+        ret = -1;
+        goto err_sock;
+    }
+
     ret = genl_connect(nl_sock);
     if (ret)
     {
Signature · 1 line
2.37.1 (Apple Git-137.1)
#4 lore raw
Quoted 15 lines · Arne Schwabe

This can happen if the memory alloc fails.

Patch V2: add goto error

Change-Id Iee66caa794d267ac5f8bee584633352893047171
---
  src/openvpn/dco_linux.c | 7 +++++++
  1 file changed, 7 insertions(+)
src/openvpn/dco_linux.c +0 -0
index 41540c0f8..95fe94848 100644

src/openvpn/dco_linux.c resolve_ovpn_netlink_id(int msglevel) · line 89

Quoted 7 lines
src/openvpn/dco_linux.c +4 -0
@@ -83,6 +83,13 @@ resolve_ovpn_netlink_id(int msglevel)
      int ret;
      struct nl_sock *nl_sock = nl_socket_alloc();
 
+    if (!nl_sock)
+    {
+        msg(msglevel, "Allocating net link socket failed");
+        ret = -1;

Please use -ENOMEM here - it is always better to return an actual reason rather than just "failed".

src/openvpn/dco_linux.c resolve_ovpn_netlink_id(int msglevel) · line 90

Quoted 1 line
src/openvpn/dco_linux.c +1 -0
+        goto err_sock;

There is no need to jump to cleanup.
You can just return -1 here and save one line. (this is what we do in other functions of this file)

Cheers,

Signature · 1 line
Antonio Quartulli
#5 lore raw

PATCH v3 1/1 Add missing check for nl_socket_alloc failure

In reply to Arne Schwabe

This can happen if the memory alloc fails.

Patch V2: add goto error
Patch V3: return -ENOMEM instead of going to error

Change-Id Iee66caa794d267ac5f8bee584633352893047171
---
 src/openvpn/dco_linux.c | 6 ++++++
 1 file changed, 6 insertions(+)
src/openvpn/dco_linux.c +6 -0
index b033f8543..3c91606b7 100644
@@ -81,6 +81,12 @@ resolve_ovpn_netlink_id(int msglevel)
     int ret;
     struct nl_sock *nl_sock = nl_socket_alloc();
 
+    if (!nl_sock)
+    {
+        msg(msglevel, "Allocating net link socket failed");
+        return -ENOMEM;
+    }
+
     ret = genl_connect(nl_sock);
     if (ret)
     {
Signature · 1 line
2.39.3 (Apple Git-145)
Acked-by on v3 1/1
#6 lore raw

Hi,

Quoted 7 lines · Arne Schwabe

This can happen if the memory alloc fails.

Patch V2: add goto error
Patch V3: return -ENOMEM instead of going to error

Change-Id Iee66caa794d267ac5f8bee584633352893047171
Quoted 21 lines
---
  src/openvpn/dco_linux.c | 6 ++++++
  1 file changed, 6 insertions(+)
src/openvpn/dco_linux.c +6 -0
index b033f8543..3c91606b7 100644
@@ -81,6 +81,12 @@ resolve_ovpn_netlink_id(int msglevel)
      int ret;
      struct nl_sock *nl_sock = nl_socket_alloc();
 
+    if (!nl_sock)
+    {
+        msg(msglevel, "Allocating net link socket failed");
+        return -ENOMEM;
+    }
+
      ret = genl_connect(nl_sock);
      if (ret)
      {
Signature · 1 line
Antonio Quartulli
#7 lore raw
In reply to Arne Schwabe

Took us long enough for such a simple fix... :-)

I have no easy way to trigger this, so only compile tested.

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

commit d1c31e428120bb0fc9488c62c1691c92a37d94c3 (master) commit aa19a6a90f7ef24df12eed205663f3cfbb7d8174 (release/2.6)

Author: Arne Schwabe
Date:   Tue Nov 21 18:06:03 2023 +0100
     Add missing check for nl_socket_alloc failure

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

--
kind regards,

Gert Doering