Skip to contentSkip to conversation

block-dns using iservice: fix a potential double free

PATCH Superseded [email protected]... 3 participants 8 messages Feb 1, 2023 – Feb 2, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block-dns using iservice: fix a potential double free

From: Selva Nair <[email protected]...>

- An item added to undo-list was not removed on error, causing attempt to free again in Undo().
Also fix a memory leak possibility in the same context.

Github: fixes OpenVPN/openvpn#232

---
 src/openvpnserv/interactive.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
src/openvpnserv/interactive.c +14 -3
index 03361d66..636f32e9 100644
@@ -780,7 +780,7 @@ static DWORD
 HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
 {
     DWORD err = 0;
-    block_dns_data_t *interface_data;
+    block_dns_data_t *interface_data = NULL;
     HANDLE engine = NULL;
     LPCWSTR exe_path;
 
@@ -794,6 +794,7 @@ HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
             interface_data = malloc(sizeof(block_dns_data_t));
             if (!interface_data)
             {
+                delete_block_dns_filters(engine);
                 return ERROR_OUTOFMEMORY;
             }
             interface_data->engine = engine;
@@ -818,8 +819,17 @@ HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
                                            BLOCK_DNS_IFACE_METRIC);
                 if (!err)
                 {
-                    set_interface_metric(msg->iface.index, AF_INET6,
-                                         BLOCK_DNS_IFACE_METRIC);
+                    err = set_interface_metric(msg->iface.index, AF_INET6,
+                                               BLOCK_DNS_IFACE_METRIC);
+                }
+                if (err)
+                {
+                    /* try to restore the interface metric values in case changed */
+                    set_interface_metric(msg->iface.index, AF_INET,
+                                         interface_data->metric_v4);
+                    set_interface_metric(msg->iface.index, AF_INET,
+                                         interface_data->metric_v4);
+                    RemoveListItem(&(*lists)[block_dns], CmpAny, NULL);
                 }
             }
         }
@@ -853,6 +863,7 @@ HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
     if (err && engine)
     {
         delete_block_dns_filters(engine);
+        free(interface_data);
     }
 
     return err;
Signature · 1 line
2.34.1
#1 lore raw

Hi,

I made a slightly different fix but then noticed your mail.

Indeed the problem is that get/set_interface_metric fails, and we call FwpmEngineClose0 after updating the undo list. When openvpn process exits, we execute commands in undo list, and second call to FwpmEngineClose0 causes Access Violation inside WFP.

Note that since get_interface_metric() fails, it sets interface_data->metric_v4 to -1. In this case we should not restore metrics back (which will fail in any case).
We have a very similar code (including metrics value check) just below which handles msg_del_block_dns. Can we factor it out into a separate function?

Signature · 1 line
-Lev
author
#2 lore raw

Hi,

Quoted 10 lines · Lev Stipakov

Hi,

I made a slightly different fix but then noticed your mail.

Indeed the problem is that get/set_interface_metric fails, and we call FwpmEngineClose0 after updating the undo list. When openvpn process exits, we execute commands in undo list, and second call to FwpmEngineClose0 causes Access Violation inside WFP.

Quoted 8 lines

Note that since get_interface_metric() fails, it sets interface_data->metric_v4
to -1. In this case we should not restore metrics back (which will fail in any case).
We have a very similar code (including metrics value check) just below which
handles msg_del_block_dns. Can we factor it out into a separate function?

Good point. I have a version that splits "add" and "delete" actions into separate functions and does something like this.

Please take a look here:
https://github.com/selvanair/openvpn/tree/block-dns-fix
The add and delete functions are in that order (with a forward declaration) only to make review easier (less changes with ignore-all-spaces), will move the latter up if it looks okay.

That said, if you have a better fix, I'm ready to review it in lieu of this.

Thanks,

Selva

#3 lore raw

Hi,

Quoted 4 lines

Good point. I have a version that splits "add" and "delete" actions into separate functions and does something like this.

Please take a look here: https://github.com/selvanair/openvpn/tree/block-dns-fix
The add and delete functions are in that order (with a forward declaration) only to make review easier (less changes with ignore-all-spaces), will move the latter up if it looks okay.

Looks good to me, especially without whitespaces
https://github.com/OpenVPN/openvpn/pull/235/files?diff=unified&w=1

Quoted 1 line

That said, if you have a better fix, I'm ready to review it in lieu of this.

My change is more intrusive, idea is to bail out if get_interface_metrics fails and update undo_list only if both filters and metrics are applied,

https://github.com/lstipakov/openvpn/commit/8c2d0de178e0771295764ea773596c13ff5f2aa6

It is a bit sloppy on metrics cleanup, though - for example if get_interface_metric for ipv6 fails but ipv4 is not - it won't restore ipv4 metrics. On the other hand this is unlikely to happen.

Also I replaced 0x%x with %u in win_block_dns_service() for consistency. You may want to do it in your patch too :)

Signature · 1 line
-Lev
author
#4 lore raw
Quoted 5 lines

Also I replaced 0x%x with %u in win_block_dns_service() for consistency. You may want to do it in your patch too :)

We have at least another place where it's %x, so will leave that for another day. btw, shouldn't it be %d?

Selva

#5 lore raw

PATCH v2 1/1 block-dns using iservice: fix a potential double free

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- An item added to undo-list was not removed on error, causing attempt to free again in Undo().
Also fix a memory leak possibility in the same context.

Github: fixes OpenVPN/openvpn#232

v2: Split add and delete functions and reuse the delete function for cleanup.

---

Same as PR #235 except for DeleteBlockDNS moved up and the its forward declaration removed.

 src/openvpnserv/interactive.c | 132 ++++++++++++++++++++--------------
 1 file changed, 78 insertions(+), 54 deletions(-)
src/openvpnserv/interactive.c +78 -54
index 03361d66..a3d43752 100644
@@ -777,87 +777,111 @@ CmpAny(LPVOID item, LPVOID any)
 }
 
 static DWORD
-HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
+DeleteBlockDNS(const block_dns_message_t *msg, undo_lists_t *lists)
 {
     DWORD err = 0;
-    block_dns_data_t *interface_data;
+    block_dns_data_t *interface_data = RemoveListItem(&(*lists)[block_dns], CmpAny, NULL);
+
+    if (interface_data)
+    {
+        err = delete_block_dns_filters(interface_data->engine);
+        if (interface_data->metric_v4 >= 0)
+        {
+            set_interface_metric(msg->iface.index, AF_INET,
+                                 interface_data->metric_v4);
+        }
+        if (interface_data->metric_v6 >= 0)
+        {
+            set_interface_metric(msg->iface.index, AF_INET6,
+                                 interface_data->metric_v6);
+        }
+        free(interface_data);
+    }
+    else
+    {
+        MsgToEventLog(M_ERR, TEXT("No previous block DNS filters to delete"));
+    }
+
+    return err;
+}
+
+static DWORD
+AddBlockDNS(const block_dns_message_t *msg, undo_lists_t *lists)
+{
+    DWORD err = 0;
+    block_dns_data_t *interface_data = NULL;
     HANDLE engine = NULL;
     LPCWSTR exe_path;
 
     exe_path = settings.exe_path;
 
-    if (msg->header.type == msg_add_block_dns)
+    err = add_block_dns_filters(&engine, msg->iface.index, exe_path, BlockDNSErrHandler);
+    if (!err)
     {
-        err = add_block_dns_filters(&engine, msg->iface.index, exe_path, BlockDNSErrHandler);
+        interface_data = malloc(sizeof(block_dns_data_t));
+        if (!interface_data)
+        {
+            err = ERROR_OUTOFMEMORY;
+            goto out;
+        }
+        interface_data->engine = engine;
+        interface_data->index = msg->iface.index;
+        int is_auto = 0;
+        interface_data->metric_v4 = get_interface_metric(msg->iface.index,
+                                                         AF_INET, &is_auto);
+        if (is_auto)
+        {
+            interface_data->metric_v4 = 0;
+        }
+        interface_data->metric_v6 = get_interface_metric(msg->iface.index,
+                                                         AF_INET6, &is_auto);
+        if (is_auto)
+        {
+            interface_data->metric_v6 = 0;
+        }
+
+        err = AddListItem(&(*lists)[block_dns], interface_data);
         if (!err)
         {
-            interface_data = malloc(sizeof(block_dns_data_t));
-            if (!interface_data)
-            {
-                return ERROR_OUTOFMEMORY;
-            }
-            interface_data->engine = engine;
-            interface_data->index = msg->iface.index;
-            int is_auto = 0;
-            interface_data->metric_v4 = get_interface_metric(msg->iface.index,
-                                                             AF_INET, &is_auto);
-            if (is_auto)
-            {
-                interface_data->metric_v4 = 0;
-            }
-            interface_data->metric_v6 = get_interface_metric(msg->iface.index,
-                                                             AF_INET6, &is_auto);
-            if (is_auto)
-            {
-                interface_data->metric_v6 = 0;
-            }
-            err = AddListItem(&(*lists)[block_dns], interface_data);
+            err = set_interface_metric(msg->iface.index, AF_INET,
+                                       BLOCK_DNS_IFACE_METRIC);
             if (!err)
             {
-                err = set_interface_metric(msg->iface.index, AF_INET,
+                err = set_interface_metric(msg->iface.index, AF_INET6,
                                            BLOCK_DNS_IFACE_METRIC);
-                if (!err)
-                {
-                    set_interface_metric(msg->iface.index, AF_INET6,
-                                         BLOCK_DNS_IFACE_METRIC);
-                }
-            }
-        }
-    }
-    else
-    {
-        interface_data = RemoveListItem(&(*lists)[block_dns], CmpAny, NULL);
-        if (interface_data)
-        {
-            engine = interface_data->engine;
-            err = delete_block_dns_filters(engine);
-            engine = NULL;
-            if (interface_data->metric_v4 >= 0)
-            {
-                set_interface_metric(msg->iface.index, AF_INET,
-                                     interface_data->metric_v4);
             }
-            if (interface_data->metric_v6 >= 0)
+            if (err)
             {
-                set_interface_metric(msg->iface.index, AF_INET6,
-                                     interface_data->metric_v6);
+                /* delete the filters, remove undo item and free interface data */
+                DeleteBlockDNS(msg, lists);
+                engine = NULL;
             }
-            free(interface_data);
-        }
-        else
-        {
-            MsgToEventLog(M_ERR, TEXT("No previous block DNS filters to delete"));
         }
     }
 
+out:
     if (err && engine)
     {
         delete_block_dns_filters(engine);
+        free(interface_data);
     }
 
     return err;
 }
 
+static DWORD
+HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
+{
+    if (msg->header.type == msg_add_block_dns)
+    {
+        return AddBlockDNS(msg, lists);
+    }
+    else
+    {
+        return DeleteBlockDNS(msg, lists);
+    }
+}
+
 /*
  * Execute a command and return its exit code. If timeout > 0, terminate
  * the process if still running after timeout milliseconds. In that case
Signature · 1 line
2.34.1
Acked-by on v2 1/1
#6 lore raw

Hi,

Stared at the code and tested with/without Citrix DME (which caused crash) - code is now cleaner
(add/delete separation) and no crash anymore. Next we will fix the driver :)

#7 lore raw
In reply to [email protected]...

I've stared a bit at the code, and it looks good. Also, MinGW and GH compile it just fine ;-) - did not test actual installation.

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

commit b761cb9bc942b6077f0b6e2b85a72e33fc618a0f (master) commit a85257e78f0d9f922941ad981bc4272d0aaf5594 (release/2.6)

Author: Selva Nair
Date:   Wed Feb 1 12:07:35 2023 -0500
     block-dns using iservice: fix a potential double free

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Lev Stipakov <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26130.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering