Attention is currently required from: Arne Schwabe, Frank Lichtenheld.

Hello Arne Schwabe, Frank Lichtenheld,

I'd like you to reexamine a change. Please visit

    https://gerrit.openvpn.net/c/openvpn/+/2346?usp=email

to look at the new patch set (#2).

The following approvals got outdated and were removed:
Code-Review-1 by Frank Lichtenheld


Change subject: push: check INFO_PRE length before sending AUTH_PENDING
......................................................................

push: check INFO_PRE length before sending AUTH_PENDING

send_auth_pending_messages() sent AUTH_PENDING, then rejected an
oversized INFO_PRE and returned false. The client was left pending
with no INFO_PRE, auth_deferred_expire was not set, and the management
client got an error for a message that had already gone out.

Do the length check before sending anything. The check also counted
the terminating NUL twice; count it once. Log both early returns, as
callers fail the auth on them.

Add a push unit test driver covering both the longest accepted and a
too-long INFO_PRE.

Change-Id: I1e845250b6e606ffb49fa5fe1bfd034be7eaaf2b
Signed-off-by: Razvan Cojocaru <[email protected]>
---
M CMakeLists.txt
M src/openvpn/push.c
M tests/unit_tests/openvpn/Makefile.am
A tests/unit_tests/openvpn/test_push.c
4 files changed, 394 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/46/2346/2

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 50b2d68..554e117 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -695,6 +695,7 @@
         "test_packet_id"
         "test_pkt"
         "test_provider"
+        "test_push"
         "test_socket"
         "test_ssl"
         "test_user_pass"
@@ -943,6 +944,16 @@
         src/openvpn/run_command.c
         )

+    target_sources(test_push PRIVATE
+        tests/unit_tests/openvpn/mock_get_random.c
+        tests/unit_tests/openvpn/mock_management.c
+        tests/unit_tests/openvpn/mock_push_dependencies.c
+        src/openvpn/base64.c
+        src/openvpn/options_util.c
+        src/openvpn/otime.c
+        src/openvpn/ssl_util.c
+    )
+
     target_sources(test_push_update_msg PRIVATE
         tests/unit_tests/openvpn/mock_msg.c
         tests/unit_tests/openvpn/mock_get_random.c
diff --git a/src/openvpn/push.c b/src/openvpn/push.c
index eb11107..bbc4f71 100644
--- a/src/openvpn/push.c
+++ b/src/openvpn/push.c
@@ -508,11 +508,21 @@
     time_t time_elapsed = now - ks->initial;
     if (time_elapsed < 0 || time_elapsed >= (time_t)max_timeout)
     {
+        msg(M_WARN, "Handshake time elapsed (%ds, max %us), not sending 
AUTH_PENDING",
+            (int)time_elapsed, max_timeout);
         return false;
     }
     max_timeout -= (unsigned int)time_elapsed;
     timeout = min_uint(max_timeout, timeout);

+    const size_t info_len = strlen(extra) + sizeof(info_pre);
+    if (info_len > PUSH_BUNDLE_SIZE)
+    {
+        msg(M_WARN, "INFO_PRE message too long (%zu > %d), not sending 
AUTH_PENDING",
+            info_len, PUSH_BUNDLE_SIZE);
+        return false;
+    }
+
     struct gc_arena gc = gc_new();
     if ((proto & IV_PROTO_AUTH_PENDING_KW) == 0)
     {
@@ -530,14 +540,7 @@
         send_control_channel_string_dowork(session, BSTR(&buf), D_PUSH);
     }

-    size_t len = strlen(extra) + 1 + sizeof(info_pre);
-    if (len > PUSH_BUNDLE_SIZE)
-    {
-        gc_free(&gc);
-        return false;
-    }
-
-    struct buffer buf = alloc_buf_gc(len, &gc);
+    struct buffer buf = alloc_buf_gc(info_len, &gc);
     buf_printf(&buf, info_pre);
     buf_printf(&buf, "%s", extra);
     send_control_channel_string_dowork(session, BSTR(&buf), D_PUSH);
diff --git a/tests/unit_tests/openvpn/Makefile.am 
b/tests/unit_tests/openvpn/Makefile.am
index e3404754..b068028 100644
--- a/tests/unit_tests/openvpn/Makefile.am
+++ b/tests/unit_tests/openvpn/Makefile.am
@@ -18,6 +18,7 @@
        packet_id_testdriver \
        pkt_testdriver \
        provider_testdriver \
+       push_testdriver \
        push_update_msg_testdriver \
        socket_testdriver \
        ssl_testdriver \
@@ -413,6 +414,25 @@
        $(top_srcdir)/src/openvpn/siphash_reference.c \
        $(top_srcdir)/src/openvpn/session_id.c

+push_testdriver_CFLAGS  = \
+       -I$(top_srcdir)/include -I$(top_srcdir)/src/compat 
-I$(top_srcdir)/src/openvpn \
+       @TEST_CFLAGS@
+push_testdriver_LDFLAGS = @TEST_LDFLAGS@
+
+push_testdriver_SOURCES = test_push.c \
+       mock_get_random.c \
+       mock_management.c \
+       mock_msg.c test_common.h \
+       mock_push_dependencies.c \
+       $(top_srcdir)/src/compat/compat-strlcpy.c \
+       $(top_srcdir)/src/openvpn/base64.c \
+       $(top_srcdir)/src/openvpn/buffer.c \
+       $(top_srcdir)/src/openvpn/options_util.c \
+       $(top_srcdir)/src/openvpn/otime.c \
+       $(top_srcdir)/src/openvpn/platform.c \
+       $(top_srcdir)/src/openvpn/ssl_util.c \
+       $(top_srcdir)/src/openvpn/win32-util.c
+
 push_update_msg_testdriver_CFLAGS = -I$(top_srcdir)/src/openvpn \
        -I$(top_srcdir)/src/compat \
        -I$(top_srcdir)/tests/unit_tests/openvpn \
diff --git a/tests/unit_tests/openvpn/test_push.c 
b/tests/unit_tests/openvpn/test_push.c
new file mode 100644
index 0000000..728bebd
--- /dev/null
+++ b/tests/unit_tests/openvpn/test_push.c
@@ -0,0 +1,352 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2026 OpenVPN Inc <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "syshead.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "push.c"
+#include "mock_msg.h"
+#include "test_common.h"
+
+/* Control channel messages sent by the code under test */
+static int sent_count;
+/* larger than PUSH_BUNDLE_SIZE so an oversized message is not truncated */
+static char sent_msgs[2][2 * PUSH_BUNDLE_SIZE];
+
+bool
+send_control_channel_string_dowork(struct tls_session *session, const char 
*str,
+                                   msglvl_t msglevel)
+{
+    if (sent_count < (int)SIZE(sent_msgs))
+    {
+        strlcpy(sent_msgs[sent_count], str, sizeof(sent_msgs[0]));
+    }
+    sent_count++;
+    return true;
+}
+
+/* Remaining dependencies of push.c, not reached by these tests */
+#ifdef ENABLE_MANAGEMENT
+void
+management_notify(const char *severity, const char *type, const char *text)
+{
+}
+
+void
+management_notify_generic(const char *str)
+{
+}
+
+void
+management_notify_client_cr_response(unsigned mda_key_id, const struct 
man_def_auth_context *mdac,
+                                     const struct env_set *es, const char 
*response)
+{
+}
+
+void
+ssl_put_auth_challenge(const char *cr_str)
+{
+}
+#endif /* ifdef ENABLE_MANAGEMENT */
+
+#ifdef ENABLE_PLUGIN
+void
+verify_crresponse_plugin(struct tls_multi *multi, const char *cr_response)
+{
+}
+#endif
+
+void
+verify_crresponse_script(struct tls_multi *multi, const char *cr_response)
+{
+}
+
+bool
+apply_push_options(struct context *c, struct options *options, struct buffer 
*buf,
+                   uint64_t permission_mask, uint64_t *option_types_found, 
struct env_set *es,
+                   bool is_update)
+{
+    return true;
+}
+
+int
+auth_retry_get(void)
+{
+    return 0;
+}
+
+bool
+do_up(struct context *c, bool pulled_options, uint64_t option_types_found)
+{
+    return true;
+}
+
+bool
+do_update(struct context *c, uint64_t option_types_found)
+{
+    return true;
+}
+
+in_addr_t
+getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, 
bool *succeeded,
+        struct signal_info *sig_info)
+{
+    return 0;
+}
+
+bool
+get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int 
*netbits,
+              msglvl_t msglevel)
+{
+    return false;
+}
+
+struct buffer
+get_list_intersection(struct gc_arena *gc, const char *list1, const char 
*list2)
+{
+    return alloc_buf_gc(0, gc);
+}
+
+const char **
+make_extended_arg_array(char **p, bool is_inline, struct gc_arena *gc)
+{
+    return NULL;
+}
+
+void
+md_ctx_cleanup(md_ctx_t *ctx)
+{
+}
+
+void
+md_ctx_final(md_ctx_t *ctx, uint8_t *dst)
+{
+}
+
+void
+md_ctx_free(md_ctx_t *ctx)
+{
+}
+
+void
+md_ctx_init(md_ctx_t *ctx, const char *mdname)
+{
+}
+
+md_ctx_t *
+md_ctx_new(void)
+{
+    return NULL;
+}
+
+void
+md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len)
+{
+}
+
+void
+notify_client_acc_protocols(struct tls_multi *multi)
+{
+}
+
+char *
+parse_acc_parameters(const char *capabilities, const char *delim, int 
*max_acc_len, bool server,
+                     struct gc_arena *gc)
+{
+    return NULL;
+}
+
+int
+parse_line(const char *line, char *p[], const int n, const char *file, const 
int line_num,
+           msglvl_t msglevel, struct gc_arena *gc)
+{
+    return 0;
+}
+
+const char *
+print_in6_addr(struct in6_addr addr6, unsigned int flags, struct gc_arena *gc)
+{
+    return "";
+}
+
+const char *
+print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
+{
+    return "";
+}
+
+int
+process_push_update(struct context *c, struct options *o, uint64_t 
permission_mask,
+                    uint64_t *option_types_found, struct buffer *buf, bool 
msg_sender)
+{
+    return PUSH_MSG_ERROR;
+}
+
+void
+register_signal(struct signal_info *si, int sig, const char *text)
+{
+}
+
+void
+reschedule_multi_process(struct context *c)
+{
+}
+
+const char *
+sanitize_control_message(const char *str, struct gc_arena *gc)
+{
+    return str;
+}
+
+bool
+schedule_exit(struct context *c)
+{
+    return true;
+}
+
+bool
+send_control_channel_string(struct context *c, const char *str, msglvl_t 
msglevel)
+{
+    return true;
+}
+
+bool
+ssl_clean_auth_token(void)
+{
+    return false;
+}
+
+void
+ssl_purge_auth(const bool auth_user_pass_only)
+{
+}
+
+enum tls_auth_status
+tls_authentication_status(struct tls_multi *multi)
+{
+    return TLS_AUTHENTICATION_FAILED;
+}
+
+bool
+tls_peer_supports_ncp(const char *peer_info)
+{
+    return false;
+}
+
+/* "INFO_PRE," + extra + NUL must fit in PUSH_BUNDLE_SIZE */
+#define INFO_EXTRA_MAX (PUSH_BUNDLE_SIZE - sizeof("INFO_PRE,"))
+
+struct auth_pending_ctx
+{
+    struct tls_multi multi;
+    struct tls_session session;
+    char extra[PUSH_BUNDLE_SIZE + 1];
+};
+
+static int
+auth_pending_setup(void **state)
+{
+    struct auth_pending_ctx *ctx = calloc(1, sizeof(*ctx));
+    ctx->multi.peer_info = "IV_PROTO=16\n"; /* IV_PROTO_AUTH_PENDING_KW */
+    ctx->multi.opt.handshake_window = 60;
+    ctx->multi.opt.renegotiate_seconds = 3600;
+
+    now = 1000;
+    ctx->session.key[KS_PRIMARY].initial = now;
+    ctx->session.key[KS_PRIMARY].auth_deferred_expire = 42;
+
+    sent_count = 0;
+    CLEAR(sent_msgs);
+    CLEAR(mock_msg_buf);
+
+    *state = ctx;
+    return 0;
+}
+
+static int
+auth_pending_teardown(void **state)
+{
+    free(*state);
+    return 0;
+}
+
+static void
+set_extra(struct auth_pending_ctx *ctx, size_t len)
+{
+    memset(ctx->extra, 'x', len);
+    ctx->extra[len] = '\0';
+}
+
+static void
+test_auth_pending_sent(void **state)
+{
+    struct auth_pending_ctx *ctx = *state;
+    set_extra(ctx, INFO_EXTRA_MAX);
+
+    assert_true(send_auth_pending_messages(&ctx->multi, &ctx->session, 
ctx->extra, 120));
+
+    assert_int_equal(sent_count, 2);
+    assert_string_equal(sent_msgs[0], "AUTH_PENDING,timeout 120");
+    assert_int_equal(strlen(sent_msgs[1]), sizeof("INFO_PRE,") - 1 + 
INFO_EXTRA_MAX);
+    assert_true(strprefix(sent_msgs[1], "INFO_PRE,xxx"));
+    assert_int_equal(ctx->session.key[KS_PRIMARY].auth_deferred_expire, now + 
120);
+}
+
+static void
+test_auth_pending_info_too_long(void **state)
+{
+    struct auth_pending_ctx *ctx = *state;
+    set_extra(ctx, INFO_EXTRA_MAX + 1);
+
+    assert_false(send_auth_pending_messages(&ctx->multi, &ctx->session, 
ctx->extra, 120));
+
+    /* nothing may reach the client, not even AUTH_PENDING */
+    assert_int_equal(sent_count, 0);
+    assert_int_equal(ctx->session.key[KS_PRIMARY].auth_deferred_expire, 42);
+    assert_string_equal(mock_msg_buf,
+                        "INFO_PRE message too long (1025 > 1024), not sending 
AUTH_PENDING");
+}
+
+int
+main(void)
+{
+    openvpn_unit_test_setup();
+
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test_setup_teardown(test_auth_pending_sent, 
auth_pending_setup,
+                                        auth_pending_teardown),
+        cmocka_unit_test_setup_teardown(test_auth_pending_info_too_long, 
auth_pending_setup,
+                                        auth_pending_teardown),
+    };
+
+    return cmocka_run_group_tests_name("push tests", tests, NULL, NULL);
+}

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2346?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1e845250b6e606ffb49fa5fe1bfd034be7eaaf2b
Gerrit-Change-Number: 2346
Gerrit-PatchSet: 2
Gerrit-Owner: Răzvan Cojocaru <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Frank Lichtenheld <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Attention: Frank Lichtenheld <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to