]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Add connection_established as state in tls_multi->context_auth
authorArne Schwabe <arne@rfc2549.org>
Thu, 20 May 2021 15:11:42 +0000 (17:11 +0200)
committerGert Doering <gert@greenie.muc.de>
Thu, 24 Jun 2021 13:31:04 +0000 (15:31 +0200)
The socket_info->connection_establish is set through
link_socket_set_outgoing_addr when we reach FULL_SYNC. This patch
introduces a new state in context_auth that replaces the
connection_established state for TLS connections. This make the state
machine easier to understand.

Also, rename "enum client_connect_status" to "multi_status", re-order
states so CAS_NOT_CONNECTED (=0) is the default state, and introduce
CAS_CONNECT_DONE as numerically highest so "are we done?" can be
easily checked.

This is part of the patchset to fix CVE-2020-15078 in "master" by
reorganizing the handling of incoming new and renegotiated TLS sessions
to make the code easier to understand and less prone to "edge case"
issues.

Patch v2: fix p2p mode server without (without ncp)

CVE: 2020-15078

Signed-off-by: Arne Schwabe <arne@rfc2549.org>
Acked-by: Antonio Quartulli <antonio@openvpn.net>
Message-Id: <20210520151148.2565578-3-arne@rfc2549.org>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg22419.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
src/openvpn/forward.c
src/openvpn/forward.h
src/openvpn/multi.c
src/openvpn/occ.c
src/openvpn/openvpn.h
src/openvpn/push.c
src/openvpn/ssl.c
src/openvpn/ssl_common.h

index 89017b8cfde4a4fb426f65c493169d672e89417d..e4d0fa7d05f87371b9b37d49ad1b08cad9d5c235 100644 (file)
@@ -280,7 +280,7 @@ static void
 check_connection_established(struct context *c)
 {
 
-    if (CONNECTION_ESTABLISHED(c))
+    if (connection_established(c))
     {
         /* if --pull was specified, send a push request to server */
         if (c->c2.tls_multi && c->options.pull)
@@ -536,7 +536,7 @@ encrypt_sign(struct context *c, bool comp_frag)
      * has not yet succeeded. In non-TLS tls_multi mode is not defined
      * and we always pass packets.
      */
-    if (c->c2.tls_multi && c->c2.tls_multi->multi_state != CAS_SUCCEEDED)
+    if (c->c2.tls_multi && c->c2.tls_multi->multi_state < CAS_CONNECT_DONE)
     {
         c->c2.buf.len = 0;
     }
@@ -971,7 +971,7 @@ process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, boo
          * has not yet succeeded. In non-TLS mode tls_multi is not defined
          * and we always pass packets.
          */
-        if (c->c2.tls_multi && c->c2.tls_multi->multi_state != CAS_SUCCEEDED)
+        if (c->c2.tls_multi && c->c2.tls_multi->multi_state < CAS_CONNECT_DONE)
         {
             c->c2.buf.len = 0;
         }
index e27fcf7c54dc061d7d114a6866104258c2fa455b..c9d9de7a4402659a9a132e95ba774136ff800942 100644 (file)
@@ -411,6 +411,17 @@ io_wait(struct context *c, const unsigned int flags)
     }
 }
 
-#define CONNECTION_ESTABLISHED(c) (get_link_socket_info(c)->connection_established)
+static inline bool
+connection_established(struct context *c)
+{
+    if (c->c2.tls_multi)
+    {
+        return c->c2.tls_multi->multi_state >= CAS_CONNECT_DONE;
+    }
+    else
+    {
+        return get_link_socket_info(c)->connection_established;
+    }
+}
 
 #endif /* FORWARD_H */
index 82c3886982f48b869721cee9c3ab12b8f11eac9f..52b177c1b080222d049dbde46af8fb9cc7b29855 100644 (file)
@@ -674,7 +674,8 @@ multi_close_instance(struct multi_context *m,
 #ifdef ENABLE_MANAGEMENT
     set_cc_config(mi, NULL);
 #endif
-    if (mi->context.c2.tls_multi->multi_state == CAS_SUCCEEDED)
+
+    if (mi->context.c2.tls_multi->multi_state >= CAS_CONNECT_DONE)
     {
         multi_client_disconnect_script(mi);
     }
@@ -775,7 +776,7 @@ multi_create_instance(struct multi_context *m, const struct mroute_addr *real)
         goto err;
     }
 
-    mi->context.c2.tls_multi->multi_state = CAS_PENDING;
+    mi->context.c2.tls_multi->multi_state = CAS_NOT_CONNECTED;
 
     if (hash_n_elements(m->hash) >= m->max_clients)
     {
@@ -2407,7 +2408,7 @@ multi_client_connect_late_setup(struct multi_context *m,
     mi->reporting_addr_ipv6 = mi->context.c2.push_ifconfig_ipv6_local;
 
     /* set context-level authentication flag */
-    mi->context.c2.tls_multi->multi_state = CAS_SUCCEEDED;
+    mi->context.c2.tls_multi->multi_state = CAS_CONNECT_DONE;
 
     /* authentication complete, calculate dynamic client specific options */
     if (!multi_client_set_protocol_options(&mi->context))
@@ -2649,9 +2650,9 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
             case CC_RET_DEFERRED:
                 /*
-                 * we already set client_connect_status to DEFERRED_RESULT or
+                 * we already set multi_status to DEFERRED_RESULT or
                  * DEFERRED_NO_RESULT. We just return
-                 * from the function as having client_connect_status
+                 * from the function as having multi_status
                  */
                 return;
 
@@ -3003,8 +3004,8 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
         {
             /* connection is "established" when SSL/TLS key negotiation succeeds
              * and (if specified) auth user/pass succeeds */
-            if (is_cas_pending(mi->context.c2.tls_multi->multi_state)
-                && CONNECTION_ESTABLISHED(&mi->context))
+
+            if (is_cas_pending(mi->context.c2.tls_multi->multi_state))
             {
                 multi_connection_established(m, mi);
             }
index 3a2bcabe379dac3a1c06b18a0936896057367fdb..610c05f5f6e921edcfa8a1be1c3466e760268fa1 100644 (file)
@@ -185,7 +185,7 @@ check_send_occ_req_dowork(struct context *c)
 void
 check_send_occ_load_test_dowork(struct context *c)
 {
-    if (CONNECTION_ESTABLISHED(c))
+    if (connection_established(c))
     {
         const struct mtu_load_test *entry;
 
index 3fe43180ca5b40d931397c1a2dd115284db16a10..f925f1f67394373387b20275172e07e61f935148 100644 (file)
@@ -206,7 +206,7 @@ struct context_1
 
 
 static inline bool
-is_cas_pending(enum client_connect_status cas)
+is_cas_pending(enum multi_status cas)
 {
     return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED
            || cas == CAS_PENDING_DEFERRED_PARTIAL;
@@ -237,6 +237,8 @@ struct context_2
 
     struct link_socket *link_socket;     /* socket used for TCP/UDP connection to remote */
     bool link_socket_owned;
+
+    /** This variable is used instead link_socket->info for P2MP UDP childs */
     struct link_socket_info *link_socket_info;
     const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
 
index 098b890ebe6ab4d813ce1ce7a4695f76a4dc02c6..f3a0054a9eee556dbd1ce75b6c6eb641129d2aec 100644 (file)
@@ -867,7 +867,7 @@ process_incoming_push_request(struct context *c)
         send_auth_failed(c, client_reason);
         ret = PUSH_MSG_AUTH_FAILURE;
     }
-    else if (c->c2.tls_multi->multi_state == CAS_SUCCEEDED)
+    else if (c->c2.tls_multi->multi_state >= CAS_CONNECT_DONE)
     {
         time_t now;
 
index 7caeb209d1d76fb02c13dec535c6d7d1f42a11ce..3fd2cefb55fbec5d688860cfcd069b26f7f3c88b 100644 (file)
@@ -2381,19 +2381,22 @@ key_method_2_write(struct buffer *buf, struct tls_multi *multi, struct tls_sessi
      * If we're a p2mp server to allow NCP, the first key
      * generation is postponed until after the connect script finished and the
      * NCP options can be processed. Since that always happens at after connect
-     * script options are available the CAS_SUCCEEDED status is identical to
-     * NCP options are processed and we have no extra state for NCP finished.
+     * script options are available the CAS_CONNECT_DONE status is identical to
+     * NCP options are processed and do not wait for NCP being finished.
      */
-    if (session->opt->server && (session->opt->mode != MODE_SERVER
-            || multi->multi_state == CAS_SUCCEEDED))
+    if (ks->authenticated > KS_AUTH_FALSE && session->opt->server
+        && ((session->opt->mode == MODE_SERVER && multi->multi_state >= CAS_CONNECT_DONE)
+            || (session->opt->mode == MODE_POINT_TO_POINT && !session->opt->pull)))
     {
-        if (ks->authenticated > KS_AUTH_FALSE)
+        /* if key_id >= 1, is a renegotiation, so we use the already established
+         * parameters and do not need to delay anything. */
+
+        /* key-id == 0 and multi_state >= CAS_CONNECT_DONE is a special case of
+         * the server reusing the session of a reconnecting client. */
+        if (!tls_session_generate_data_channel_keys(session))
         {
-            if (!tls_session_generate_data_channel_keys(session))
-            {
-                msg(D_TLS_ERRORS, "TLS Error: server generate_key_expansion failed");
-                goto error;
-            }
+            msg(D_TLS_ERRORS, "TLS Error: server generate_key_expansion failed");
+            goto error;
         }
     }
 
@@ -2801,6 +2804,21 @@ tls_process(struct tls_multi *multi,
                 /* Set outgoing address for data channel packets */
                 link_socket_set_outgoing_addr(to_link_socket_info, &ks->remote_addr, session->common_name, session->opt->es);
 
+                /* Check if we need to advance the tls_multi state machine */
+                if (multi->multi_state == CAS_NOT_CONNECTED)
+                {
+                    if (session->opt->mode == MODE_SERVER)
+                    {
+                        /* On a server we continue with running connect scripts next */
+                        multi->multi_state = CAS_PENDING;
+                    }
+                    else
+                    {
+                        /* Skip the connect script related states */
+                        multi->multi_state = CAS_CONNECT_DONE;
+                    }
+                }
+
                 /* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */
                 flush_payload_buffer(ks);
 
index 845d2243d09de35aac4386540afcc6664a73211a..ded86050dd0d3cd5b755ee0330e00c75d5c4b2b6 100644 (file)
@@ -504,12 +504,18 @@ struct tls_session
 /* client authentication state, CAS_SUCCEEDED must be 0 since
  * non multi code path still checks this variable but does not initialise it
  * so the code depends on zero initialisation */
-enum client_connect_status {
-    CAS_SUCCEEDED=0,
+
+/* CAS_NOT_CONNECTED is the initial state for every context. When the *first*
+ * tls_session reaches S_ACTIVE, this state machine moves to CAS_PENDING (server)
+ * or CAS_CONNECT_DONE (client/p2p) as clients skip the stages associated with
+ * connect scripts/plugins */
+enum multi_status {
+    CAS_NOT_CONNECTED,
     CAS_PENDING,
     CAS_PENDING_DEFERRED,
     CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
     CAS_FAILED,
+    CAS_CONNECT_DONE,
 };
 
 
@@ -548,7 +554,7 @@ struct tls_multi
 
     int n_sessions;             /**< Number of sessions negotiated thus
                                  *   far. */
-    enum client_connect_status multi_state;
+    enum multi_status multi_state;
 
     /*
      * Number of errors.