Skip to contentSkip to conversation

Remove saving initial frame code

PATCH In review Gert Doering 1 participant 6 messages Nov 8, 2023 – Nov 9, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Remove saving initial frame code

From: Arne Schwabe <[email protected]...>

This code was necessary before the frame/buffer refactoring as we always did relative adjustment to the frame.

This also fixes also that previously initial_frame was initialised too early before the fragment related options were initialised and contained 0 for the maximum frame size. This resulted in a DIV by 0 that caused an abort on platforms that throw an exception for that.

CVE: 2023-46849

Only people with --fragment in their config are affected

Change-Id Icc612bab5700879606290639e1b8773f61ec670d
---
 src/openvpn/forward.c |  9 ---------
 src/openvpn/init.c    | 19 ++++++++-----------
 src/openvpn/openvpn.h |  3 ---
 3 files changed, 8 insertions(+), 23 deletions(-)
src/openvpn/forward.c +0 -9
index 2510410f9..0443ca0a0 100644
@@ -1078,15 +1078,6 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
             if (tls_pre_decrypt(c->c2.tls_multi, &c->c2.from, &c->c2.buf, &co,
                                 floated, &ad_start))
             {
-                /* Restore pre-NCP frame parameters */
-                if (is_hard_reset_method2(opcode))
-                {
-                    c->c2.frame = c->c2.frame_initial;
-#ifdef ENABLE_FRAGMENT
-                    c->c2.frame_fragment = c->c2.frame_fragment_initial;
-#endif
-                }
-
                 interval_action(&c->c2.tmp_int);
 
                 /* reset packet received timer if TLS packet */
src/openvpn/init.c +8 -11
index 019f5a4f6..8c707a463 100644
@@ -3537,15 +3537,6 @@ do_init_frame(struct context *c)
      */
     frame_finalize_options(c, NULL);
 
-#ifdef ENABLE_FRAGMENT
-    /*
-     * Set frame parameter for fragment code.  This is necessary because
-     * the fragmentation code deals with payloads which have already been
-     * passed through the compression code.
-     */
-    c->c2.frame_fragment = c->c2.frame;
-    c->c2.frame_fragment_initial = c->c2.frame_fragment;
-#endif
 
 #if defined(ENABLE_FRAGMENT)
     /*
@@ -3736,6 +3727,14 @@ static void
 do_init_fragment(struct context *c)
 {
     ASSERT(c->options.ce.fragment);
+
+    /*
+     * Set frame parameter for fragment code.  This is necessary because
+     * the fragmentation code deals with payloads which have already been
+     * passed through the compression code.
+     */
+    c->c2.frame_fragment = c->c2.frame;
+
     frame_calculate_dynamic(&c->c2.frame_fragment, &c->c1.ks.key_type,
                             &c->options, get_link_socket_info(c));
     fragment_frame_init(c->c2.fragment, &c->c2.frame_fragment);
@@ -4640,8 +4639,6 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
         c->c2.did_open_tun = do_open_tun(c, &error_flags);
     }
 
-    c->c2.frame_initial = c->c2.frame;
-
     /* print MTU info */
     do_print_data_channel_mtu_parms(c);
 
src/openvpn/openvpn.h +0 -3
index 077effeb9..5b2be63f9 100644
@@ -249,14 +249,11 @@ struct context_2
 
     /* MTU frame parameters */
     struct frame frame;                         /* Active frame parameters */
-    struct frame frame_initial;                 /* Restored on new session */
 
 #ifdef ENABLE_FRAGMENT
     /* Object to handle advanced MTU negotiation and datagram fragmentation */
     struct fragment_master *fragment;
     struct frame frame_fragment;
-    struct frame frame_fragment_initial;
-    struct frame frame_fragment_omit;
 #endif
 
     /*
Signature · 1 line
2.39.3 (Apple Git-145)
#1 lore raw

PATCH 2/3 Double check that we do not use a freed buffer when freeing a session

From: Arne Schwabe <[email protected]...>

This is a find cases where the session already has planned to send out a packet but encounters some other errors that invalidate the session, setting it to S_ERROR and leaving the buffer behind.

This will detect and clear that to_link buffer in that case.

Change-Id I5ffb41bed1c9237946b13d787eb4c4013e0bec68
---
 src/openvpn/ssl.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
src/openvpn/ssl.c +48 -0
index 5e6205cc2..e15f951d6 100644
@@ -3155,6 +3155,53 @@ tls_process(struct tls_multi *multi,
     return false;
 }
 
+
+/**
+ * This is a safe guard function to double check that a buffer from a session is
+ * not used in a session to avoid a use after free.
+ *
+ * @param to_link
+ * @param session
+ */
+static void
+check_session_buf_not_used(struct buffer *to_link, struct tls_session *session)
+{
+    uint8_t *dataptr = to_link->data;
+    if (!dataptr)
+    {
+        return;
+    }
+
+    /* Checks buffers in tls_wrap */
+    if (session->tls_wrap.work.data == dataptr)
+    {
+        msg(M_INFO, "Warning buffer of freed TLS session is "
+            "still in use (tls_wrap.work.data)");
+        goto used;
+    }
+
+    for (int i = 0; i < KS_SIZE; i++)
+    {
+        struct key_state *ks = &session->key[i];
+        for (int j = 0; j < ks->send_reliable->size; j++)
+        {
+            if (ks->send_reliable->array[i].buf.data == dataptr)
+            {
+                msg(M_INFO, "Warning buffer of freed TLS session is still in"
+                    " use (session->key[%d].send_reliable->array[%d])",
+                    i, j);
+
+                goto used;
+            }
+        }
+    }
+    return;
+
+used:
+    to_link->len = 0;
+    to_link->data = 0;
+    /* for debugging, you can add an ASSERT(0); here to trigger an abort */
+}
 /*
  * Called by the top-level event loop.
  *
@@ -3253,6 +3300,7 @@ tls_multi_process(struct tls_multi *multi,
                 }
                 else
                 {
+                    check_session_buf_not_used(to_link, session);
                     reset_session(multi, session);
                 }
             }
Signature · 1 line
2.39.3 (Apple Git-145)
#2 lore raw

[Openvpn-devel] [PATCH applied] Re: Double check that we do not use a freed buffer when freeing a session

Second in the series. This one is "should we ever do stupid things again, catch us doing so", so not the actual bugfix but belt&suspenders.

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

commit cd4d819c99266fa727c294225cafdb4ae331d02e (release/2.6) commit f09d75083e8d7f6db3d90a25480e44de91bd8154 (master)

Author: Arne Schwabe
Date:   Wed Oct 25 17:46:24 2023 +0200
     Double check that we do not use a freed buffer when freeing a session

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: David Sommerseth <[email protected]...>
     Acked-by: Heiko Hund <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#3 lore raw

PATCH 3/3 Fix using to_link buffer after freed

In reply to Gert Doering

From: Arne Schwabe <[email protected]...>

When I refactored the tls_state_change method in 9a7b95fda5 I accidentally changed a break into a return true while it should return a false.

The code here is extremely fragile in the sense that it assumes that settings a keystate to S_ERROR cannot have any outgoing buffer or we will have a use after free. The previous break and now restored return false ensure this by skipping any further tls_process_state loops that might set to ks->S_ERROR and ensure that the to_link is sent out and cleared before having more loops in tls_state_change.

CVE: 2023-46850

This affects everyone, even with tls-auth/tls-crypt enabled.

Change-Id I2a0f1c665d992da8e24a421ff0ddcb40f7945ea8
---
 src/openvpn/ssl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
src/openvpn/ssl.c +7 -1
index e15f951d6..cee4afe19 100644
@@ -2903,7 +2903,13 @@ tls_process_state(struct tls_multi *multi,
                            CONTROL_SEND_ACK_MAX, true);
         *to_link = b;
         dmsg(D_TLS_DEBUG, "Reliable -> TCP/UDP");
-        return true;
+
+        /* This changed the state of the outgoing buffer. In order to avoid
+         * running this function again/further and invalidating the key_state
+         * buffer and accessing the buffer that is now in to_link after it being
+         * freed for a potential error, we shortcircuit exiting of the outer
+         * process here. */
+        return false;
     }
 
     /* Write incoming ciphertext to TLS object */
Signature · 1 line
2.39.3 (Apple Git-145)
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix using to_link buffer after freed

Third in the series. This is the actual bug fix for the use-after-free leak - looks trivial enough... as Arne says, this part of the code is too full of magic and easy to break.

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

commit 57a5cd1e12f193927c9b7429f8778fec7e04c50a (release/2.6) commit a0afe035cbca26f8c74b670a8c2a20b3d9c2294b (master)

Author: Arne Schwabe
Date:   Fri Oct 27 14:19:37 2023 +0200
     Fix using to_link buffer after freed

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: David Sommerseth <[email protected]...>
     Acked-by: Heiko Hund <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#5 lore raw
In reply to Gert Doering

Since this was security related (CVE) it was posted to the security@ list first, and reviewed and ACKed by core team members there. Re-Posting the patches + the commit message here, after 2.6.7 release.

I have skimmed the code change and it looks reasonable - and I have subjected this (+ 2/3 and 3/3) to the full set of t_client and t_server patches, and things work as well as before.

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

commit 1cfca659244e362f372d9843351257f456392a2f (release/2.6) commit a341914246d16ff277f2d521c2dc9273d41fa5ed (master)

Author: Arne Schwabe
Date:   Thu Oct 19 15:14:33 2023 +0200
     Remove saving initial frame code

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: David Sommerseth <[email protected]...>
     Acked-by: Heiko Hund <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/search?l=mid&[email protected]...
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering