From: "mrbff (Code Review)" Date: Wed, 29 Oct 2025 13:18:20 +0000 In-Reply-To: References: Reply-To: marco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="IMB1D2axsWQ="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: PUSH_UPDATE server: invalid read bug-fix and unit-tests improvements List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --IMB1D2axsWQ= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, mrbff, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1316?usp=3Demail to look at the new patch set (#4). The following approvals got outdated and were removed: Code-Review-1 by flichtenheld Change subject: PUSH_UPDATE server: invalid read bug-fix and unit-tests imp= rovements ...................................................................... PUSH_UPDATE server: invalid read bug-fix and unit-tests improvements The number of messages calculated before the call to message_splitter(), us= ed in the memory allocation in the buffer array, could in certain cases be less than one than the actual number of messages, thus causing an override of the sentinel buffer in message_splitter and therefore an invalid read in send_single_push_update(). The case in question would be, for example, a sequence of three options "A, B, C" with the size of B equal to safe_cap - 1 and the sum of the sizes of A and C less than safe_cap - 2. The buffer array was therefore replaced with a list of buffers to completely avoid calculating the number of messages before it was actually computed. The test case in question has been added to the unit tests. The unit tests have been improved using cmocka macros. Change-Id: Idba419681fe3ccc4e6e2f6ce7592332dcff62cd9 Signed-off-by: Marco Baffo --- M src/openvpn/push_util.c M tests/unit_tests/openvpn/test_push_update_msg.c 2 files changed, 137 insertions(+), 57 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/16/1316/4 diff --git a/src/openvpn/push_util.c b/src/openvpn/push_util.c index 552679a..51c7b5f 100644 --- a/src/openvpn/push_util.c +++ b/src/openvpn/push_util.c @@ -91,7 +91,7 @@ * Return `false` on failure an `true` on success. */ static bool -message_splitter(const char *s, struct buffer *msgs, struct gc_arena *gc, = const size_t safe_cap) +message_splitter(const char *s, struct buffer_list *msgs, struct gc_arena = *gc, const size_t safe_cap) { if (!s || !*s) { @@ -100,7 +100,6 @@ char *str =3D gc_strdup(s, gc); size_t i =3D 0; - int im =3D 0; while (*str) { @@ -115,37 +114,38 @@ } str[ci] =3D '\0'; /* copy from i to (ci -1) */ - msgs[im] =3D forge_msg(str, ",push-continuation 2", gc); + struct buffer tmp =3D forge_msg(str, ",push-continuation 2", g= c); + buffer_list_push(msgs, BSTR(&tmp)); i =3D ci + 1; } else { - if (im) + if (msgs->head) { - msgs[im] =3D forge_msg(str, ",push-continuation 1", gc); + struct buffer tmp =3D forge_msg(str, ",push-continuation 1= ", gc); + buffer_list_push(msgs, BSTR(&tmp)); } else { - msgs[im] =3D forge_msg(str, NULL, gc); + struct buffer tmp =3D forge_msg(str, NULL, gc); + buffer_list_push(msgs, BSTR(&tmp)); } i =3D strlen(str); } str =3D &str[i]; - im++; } return true; } /* send the message(s) prepared to one single client */ static bool -send_single_push_update(struct multi_context *m, struct multi_instance *mi= , struct buffer *msgs) +send_single_push_update(struct multi_context *m, struct multi_instance *mi= , struct buffer_list *msgs) { - if (!msgs[0].data || !*(msgs[0].data)) + if (!msgs->head) { return false; } - int i =3D -1; unsigned int option_types_found =3D 0; struct context *c =3D &mi->context; struct options o; @@ -160,9 +160,10 @@ o.ifconfig_local =3D canary; o.ifconfig_ipv6_local =3D canary; - while (msgs[++i].data && *(msgs[i].data)) + struct buffer_entry *e =3D msgs->head; + while (e) { - if (!send_control_channel_string(c, BSTR(&msgs[i]), D_PUSH)) + if (!send_control_channel_string(c, BSTR(&e->buf), D_PUSH)) { return false; } @@ -182,13 +183,14 @@ * Also we need to make a temporary copy so we can buf_advance() * without modifying original buffer. */ - struct buffer tmp_msg =3D msgs[i]; + struct buffer tmp_msg =3D e->buf; buf_string_compare_advance(&tmp_msg, push_update_cmd); unsigned int permission_mask =3D pull_permission_mask(c); if (process_push_update(c, &o, permission_mask, &option_types_foun= d, &tmp_msg, true) =3D=3D PUSH_MSG_ERROR) { msg(M_WARN, "Failed to process push update message sent to cli= ent ID: %u", c->c2.tls_multi->peer_id); } + e =3D e->next; } if (option_types_found & OPT_P_UP) @@ -270,12 +272,11 @@ * we want to send exceeds that size we have to split it into smaller = messages */ ASSERT(push_bundle_size > extra); const size_t safe_cap =3D push_bundle_size - extra; - size_t msgs_num =3D (strlen(msg) / safe_cap) + ((strlen(msg) % safe_ca= p) !=3D 0); - struct buffer *msgs =3D gc_malloc((msgs_num + 1) * sizeof(struct buffe= r), true, &gc); + struct buffer_list *msgs =3D buffer_list_new(); - msgs[msgs_num].data =3D NULL; if (!message_splitter(msg, msgs, &gc, safe_cap)) { + buffer_list_free(msgs); gc_free(&gc); return -EINVAL; } @@ -286,6 +287,7 @@ if (!mi) { + buffer_list_free(msgs); gc_free(&gc); return -ENOENT; } @@ -293,6 +295,7 @@ if (!support_push_update(mi)) { msg(M_CLIENT, "PUSH_UPDATE: not sending message to unsupported= peer with ID: %u", mi->context.c2.tls_multi->peer_id); + buffer_list_free(msgs); gc_free(&gc); return 0; } @@ -300,11 +303,13 @@ if (!mi->halt && send_single_push_update(m, mi, msgs)) { + buffer_list_free(msgs); gc_free(&gc); return 1; } else { + buffer_list_free(msgs); gc_free(&gc); return 0; } @@ -334,6 +339,7 @@ } hash_iterator_free(&hi); + buffer_list_free(msgs); gc_free(&gc); return count; } diff --git a/tests/unit_tests/openvpn/test_push_update_msg.c b/tests/unit_t= ests/openvpn/test_push_update_msg.c index 516e94c..6ef1266 100644 --- a/tests/unit_tests/openvpn/test_push_update_msg.c +++ b/tests/unit_tests/openvpn/test_push_update_msg.c @@ -130,18 +130,11 @@ return true; } #else /* ifndef ENABLE_MANAGEMENT */ -char **res; -int i; bool send_control_channel_string(struct context *c, const char *str, msglvl_t m= sglevel) { - if (res && res[i] && strcmp(res[i], str)) - { - printf("\n\nexpected: %s\n\n actual: %s\n\n", res[i], str); - return false; - } - i++; + check_expected(str); return true; } @@ -301,44 +294,75 @@ #ifdef ENABLE_MANAGEMENT char *r0[] =3D { - "PUSH_UPDATE,redirect-gateway local,route 192.168.1.0 255.255.255.0" + "PUSH_UPDATE,redirect-gateway local,route 192.168.1.0 255.255.255.0", + NULL }; char *r1[] =3D { "PUSH_UPDATE,-dhcp-option,blablalalalalalalalalalalalalf, lalalalalala= lalalalalalalalaf,push-continuation 2", "PUSH_UPDATE, akakakakakakakakakakakaf, dhcp-option DNS 8.8.8.8,redire= ct-gateway local,push-continuation 2", - "PUSH_UPDATE,route 192.168.1.0 255.255.255.0,push-continuation 1" + "PUSH_UPDATE,route 192.168.1.0 255.255.255.0,push-continuation 1", + NULL }; char *r3[] =3D { - "PUSH_UPDATE,,," + "PUSH_UPDATE,,,", + NULL }; char *r4[] =3D { "PUSH_UPDATE,-dhcp-option, blablalalalalalalalalalalalalf, lalalalalal= alalalalalalalalaf,push-continuation 2", "PUSH_UPDATE, akakakakakakakakakakakaf,dhcp-option DNS 8.8.8.8, redire= ct-gateway local,push-continuation 2", - "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,,push-continuation 1" + "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,,push-continuation 1", + NULL }; char *r5[] =3D { "PUSH_UPDATE,,-dhcp-option, blablalalalalalalalalalalalalf, lalalalala= lalalalalalalalalaf,push-continuation 2", "PUSH_UPDATE, akakakakakakakakakakakaf,dhcp-option DNS 8.8.8.8, redire= ct-gateway local,push-continuation 2", - "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,push-continuation 1" + "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,push-continuation 1", + NULL }; char *r6[] =3D { "PUSH_UPDATE,-dhcp-option,blablalalalalalalalalalalalalf, lalalalalala= lalalalalalalalaf,push-continuation 2", "PUSH_UPDATE, akakakakakakakakakakakaf, dhcp-option DNS 8.8.8.8, redir= ect-gateway 10.10.10.10,,push-continuation 2", - "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,,push-continuation 1" + "PUSH_UPDATE, route 192.168.1.0 255.255.255.0,,push-continuation 1", + NULL }; char *r7[] =3D { "PUSH_UPDATE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,= ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,push-continuation 2", - "PUSH_UPDATE,,,,,,,,,,,,,,,,,,,push-continuation 1" + "PUSH_UPDATE,,,,,,,,,,,,,,,,,,,push-continuation 1", + NULL }; char *r8[] =3D { "PUSH_UPDATE,-dhcp-option,blablalalalalalalalalalalalalf, lalalalalala= lalalalalalalalaf,push-continuation 2", "PUSH_UPDATE, akakakakakakakakakakakaf, dhcp-option DNS 8.8.8.8,redire= ct-gateway\n local,push-continuation 2", - "PUSH_UPDATE,route 192.168.1.0 255.255.255.0\n\n\n,push-continuation 1" + "PUSH_UPDATE,route 192.168.1.0 255.255.255.0\n\n\n,push-continuation 1= ", + NULL }; char *r9[] =3D { - "PUSH_UPDATE,," + "PUSH_UPDATE,,", + NULL }; - +char *r11[] =3D { + "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2", + "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2", + "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2", + "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2", + "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-cont= inuation 1", + NULL +}; +char *r12[] =3D { + "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,push-continuation 2", + "PUSH_UPDATE,abc,push-continuation 1", + NULL +}; +char *r13[] =3D { + "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,", + NULL +}; +char *r14[] =3D { + "PUSH_UPDATE,a,push-continuation 2", + "PUSH_UPDATE,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa= aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,push-continuation 2", + "PUSH_UPDATE,a,push-continuation 1", + NULL +}; const char *msg0 =3D "redirect-gateway local,route 192.168.1.0 255.255.255= .0"; const char *msg1 =3D "-dhcp-option,blablalalalalalalalalalalalalf, lalalal= alalalalalalalalalalaf," @@ -365,32 +389,50 @@ "daisy damage damp dance danger daring dash daughter d= awn day deal debate debris decade december decide decline" "decorate decrease deer defense define defy degree del= ay deliver demand demise denial"; =20 +const char *msg11 =3D "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a," + "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a," + "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a," + "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a," + "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a"; + +const char *msg12 =3D "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,abc"; + +const char *msg13 =3D "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,"; + +const char *msg14 =3D "a,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa= aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,a"; + #define PUSH_BUNDLE_SIZE_TEST 184 +#define expect_control_channel_strings(res) \ + do \ + { \ + for (int j =3D 0; res[j] !=3D NULL; j++) \ + { \ + expect_string(send_control_channel_string, str, res[j]); \ + } \ + } while (0) + static void test_send_push_msg0(void **state) { - i =3D 0; - res =3D r0; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r0); assert_int_equal(send_push_update(m, &cid, msg0, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } + static void test_send_push_msg1(void **state) { - i =3D 0; - res =3D r1; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r1); assert_int_equal(send_push_update(m, &cid, msg1, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg2(void **state) { - i =3D 0; - res =3D NULL; struct multi_context *m =3D *state; const unsigned long cid =3D 0; assert_int_equal(send_push_update(m, &cid, msg2, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), -EINVAL); @@ -399,83 +441,110 @@ static void test_send_push_msg3(void **state) { - i =3D 0; - res =3D r3; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r3); assert_int_equal(send_push_update(m, &cid, msg3, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg4(void **state) { - i =3D 0; - res =3D r4; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r4); assert_int_equal(send_push_update(m, &cid, msg4, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg5(void **state) { - i =3D 0; - res =3D r5; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r5); assert_int_equal(send_push_update(m, &cid, msg5, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg6(void **state) { - i =3D 0; - res =3D r6; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r6); assert_int_equal(send_push_update(m, &cid, msg6, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg7(void **state) { - i =3D 0; - res =3D r7; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r7); assert_int_equal(send_push_update(m, &cid, msg7, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg8(void **state) { - i =3D 0; - res =3D r8; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r8); assert_int_equal(send_push_update(m, &cid, msg8, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg9(void **state) { - i =3D 0; - res =3D r9; struct multi_context *m =3D *state; const unsigned long cid =3D 0; + expect_control_channel_strings(r9); assert_int_equal(send_push_update(m, &cid, msg9, UPT_BY_CID, PUSH_BUND= LE_SIZE_TEST), 1); } static void test_send_push_msg10(void **state) { - i =3D 0; - res =3D NULL; struct multi_context *m =3D *state; const unsigned long cid =3D 0; assert_int_equal(send_push_update(m, &cid, msg10, UPT_BY_CID, PUSH_BUN= DLE_SIZE_TEST), -EINVAL); } +static void +test_send_push_msg11(void **state) +{ + struct multi_context *m =3D *state; + const unsigned long cid =3D 0; + expect_control_channel_strings(r11); + assert_int_equal(send_push_update(m, &cid, msg11, UPT_BY_CID, PUSH_BUN= DLE_SIZE_TEST), 1); +} + +static void +test_send_push_msg12(void **state) +{ + struct multi_context *m =3D *state; + const unsigned long cid =3D 0; + expect_control_channel_strings(r12); + assert_int_equal(send_push_update(m, &cid, msg12, UPT_BY_CID, PUSH_BUN= DLE_SIZE_TEST), 1); +} + +static void +test_send_push_msg13(void **state) +{ + struct multi_context *m =3D *state; + const unsigned long cid =3D 0; + expect_control_channel_strings(r13); + assert_int_equal(send_push_update(m, &cid, msg13, UPT_BY_CID, PUSH_BUN= DLE_SIZE_TEST), 1); +} + +static void +test_send_push_msg14(void **state) +{ + struct multi_context *m =3D *state; + const unsigned long cid =3D 0; + expect_control_channel_strings(r14); + assert_int_equal(send_push_update(m, &cid, msg14, UPT_BY_CID, PUSH_BUN= DLE_SIZE_TEST), 1); +} + #undef PUSH_BUNDLE_SIZE_TEST static int @@ -535,6 +604,7 @@ cmocka_unit_test_setup_teardown(test_incoming_push_message_mix, se= tup, teardown), cmocka_unit_test_setup_teardown(test_incoming_push_message_mix2, s= etup, teardown), #ifdef ENABLE_MANAGEMENT + cmocka_unit_test_setup_teardown(test_send_push_msg0, setup2, teard= own2), cmocka_unit_test_setup_teardown(test_send_push_msg1, setup2, teard= own2), cmocka_unit_test_setup_teardown(test_send_push_msg2, setup2, teard= own2), @@ -545,7 +615,11 @@ cmocka_unit_test_setup_teardown(test_send_push_msg7, setup2, teard= own2), cmocka_unit_test_setup_teardown(test_send_push_msg8, setup2, teard= own2), cmocka_unit_test_setup_teardown(test_send_push_msg9, setup2, teard= own2), - cmocka_unit_test_setup_teardown(test_send_push_msg10, setup2, tear= down2) + cmocka_unit_test_setup_teardown(test_send_push_msg10, setup2, tear= down2), + cmocka_unit_test_setup_teardown(test_send_push_msg11, setup2, tear= down2), + cmocka_unit_test_setup_teardown(test_send_push_msg12, setup2, tear= down2), + cmocka_unit_test_setup_teardown(test_send_push_msg13, setup2, tear= down2), + cmocka_unit_test_setup_teardown(test_send_push_msg14, setup2, tear= down2) #endif }; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1316?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Idba419681fe3ccc4e6e2f6ce7592332dcff62cd9 Gerrit-Change-Number: 1316 Gerrit-PatchSet: 4 Gerrit-Owner: mrbff Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-Attention: mrbff --IMB1D2axsWQ= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: flichtenheld, mrbff, plaisthos.

mrbff uploaded patch set #4 to this change.

View Change

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

PUSH_UPDATE server: invalid =
read bug-fix and unit-tests improvements

The number of messages calc= ulated before the call to message_splitter(), used
in the memory allocat= ion in the buffer array, could in certain cases be less
than one than th= e actual number of messages, thus causing an override of the
sentinel bu= ffer in message_splitter and therefore an invalid read in
send_single_pu= sh_update(). The case in question would be, for example, a
sequence of t= hree options "A, B, C" with the size of B equal to safe_cap - 1and the sum of the sizes of A and C less than safe_cap - 2.
The buffer= array was therefore replaced with a list of buffers to completely
avoid= calculating the number of messages before it was actually computed.
The= test case in question has been added to the unit tests.
The unit tests = have been improved using cmocka macros.

Change-Id: Idba419681fe3ccc4= e6e2f6ce7592332dcff62cd9
Signed-off-by: Marco Baffo <marco@...2726...= >
---
M src/openvpn/push_util.c
M tests/unit_tests/openvpn/test= _push_update_msg.c
2 files changed, 137 insertions(+), 57 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn r=
efs/changes/16/1316/4
diff --git a/src/openvpn/push_util.c b/src/op=
envpn/push_util.c
index 552679a..51c7b5f 100644
<= span>--- a/src/openvpn/push_util.c
+++ b/src/openvpn/push_u= til.c
@@ -91,7 +91,7 @@
* Return `false` = on failure an `true` on success.
*/
stat= ic bool
-message_splitt= er(const char *s, struct buffer *msgs, struct gc_arena *gc, const size_t sa= fe_cap)
+message_spli= tter(const char *s, struct buffer_list *msgs, struct gc_arena *gc, const si= ze_t safe_cap)
{
if (!s || !*s)
{
@@ -100,7 +100,6 @@

char *str =3D gc_strdup(s, gc);
size= _t i =3D 0;
- int im= =3D 0;

while (*str)
{

@@ -115,37 +114,38 @@
= }
str[ci] =3D '\0';
= /* copy from i to (ci -1) */
- msgs[im] =3D forge_msg(str, ",push-conti= nuation 2", gc);
+ struct buffer tmp =3D forge_msg(str, ",push-continuation= 2", gc);
+ = buffer_list_push(msgs, BSTR(&tmp));
= i =3D ci + 1;
}
else
{
- if (im)
+ if (msgs->head)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- msgs[im] =3D forg= e_msg(str, ",push-continuation 1", gc);

+ struct buffer tmp =3D forge_m= sg(str, ",push-continuation 1", gc);
+ buffer_list_push(msgs, BSTR(&= ;tmp));
}
else
{
- msgs[im] =3D forge_msg(str, NULL, gc);
+ struct buffer tmp= =3D forge_msg(str, NULL, gc);

+ buffer_list_push(msgs, BSTR(&tmp)); }
i =3D strlen(str);
}
str =3D &str[i];=
- im++;
}

return true;
}

/* send the message(s) prepared to one single client = */
static bool
-send_single_push_update(struct multi_context *m, struct multi_ins= tance *mi, struct buffer *msgs)
+send_single_push_update(struct multi_context *m, struct multi_i= nstance *mi, struct buffer_list *msgs)
{
- if (!msgs[0].data || !*(msgs[0].dat= a))
+ if (!msgs-&g= t;head)
{
return false;
}

- int i =3D -1;
unsigned int option_ty= pes_found =3D 0;
struct context *c =3D &mi->con= text;
struct options o;
@@ -160,9 +160= ,10 @@
o.ifconfig_local =3D canary;
= o.ifconfig_ipv6_local =3D canary;

- while (msgs[++i].data && *(msg= s[i].data))
+ stru= ct buffer_entry *e =3D msgs->head;
+ while (e)
{
- if (!send_control_channel_string(c= , BSTR(&msgs[i]), D_PUSH))
+ if (!send_control_channel_string(c, BSTR(&e->buf)= , D_PUSH))
{
return f= alse;
}
@@ -182,13 +183,14 @@
* Also we need to make a temporary copy so we can buf= _advance()
* without modifying original buffer.
*/
- struct buffer tmp_msg =3D msgs[i];
+ struct buffer tmp_msg =3D e->buf;
buf_string_compare_advance(&tmp_msg, push_updat= e_cmd);
unsigned int permission_mask =3D pull_perm= ission_mask(c);
if (process_push_update(c, &o,= permission_mask, &option_types_found, &tmp_msg, true) =3D=3D PUSH_= MSG_ERROR)
{
msg(M_WA= RN, "Failed to process push update message sent to client ID: %u"= , c->c2.tls_multi->peer_id);
}
+ e =3D e->next;
<= br> }

if (option_types_fou= nd & OPT_P_UP)
@@ -270,12 +272,11 @@
= * we want to send exceeds that size we have to split it into smaller me= ssages */
ASSERT(push_bundle_size > extra);<= br> const size_t safe_cap =3D push_bundle_size - extra;- size_t msgs_num =3D (strlen= (msg) / safe_cap) + ((strlen(msg) % safe_cap) !=3D 0);
- struct buffer *msgs =3D gc_malloc((msg= s_num + 1) * sizeof(struct buffer), true, &gc);
+ struct buffer_list *msgs =3D buffer_l= ist_new();

- msgs[msgs_num].data =3D NULL;
if (!message_= splitter(msg, msgs, &gc, safe_cap))
{
+ buffer_list_free(msgs);<= /span>
gc_free(&gc);
return = -EINVAL;
}
@@ -286,6 +287,7 @@<= br>
if (!mi)
{
+ buffer_list= _free(msgs);
gc_free(&gc);
return -ENOENT;

}
@@= -293,6 +295,7 @@
if (!support_push_update(mi))

{
msg(M_CLIENT, "P= USH_UPDATE: not sending message to unsupported peer with ID: %u", mi-&= gt;context.c2.tls_multi->peer_id);
+ buffer_list_free(msgs);
= gc_free(&gc);
return 0;
= }
@@ -300,11 +303,13 @@
= if (!mi->halt
&& send_single_p= ush_update(m, mi, msgs))
{
+ buffer_list_free(msgs);
gc_free(&gc);
retu= rn 1;
}
else
{

+ = buffer_list_free(msgs);
gc_free(&gc= );
return 0;
}=
@@ -334,6 +339,7 @@
}

hash_iterator_free(&hi);
+ buffer_list_free(msgs);
= gc_free(&gc);
return count;
}
diff --git a/tests/unit_tests/openvpn/test_push_update_msg.c b= /tests/unit_tests/openvpn/test_push_update_msg.c
index 516e= 94c..6ef1266 100644
--- a/tests/unit_tests/openvpn/test_pus= h_update_msg.c
+++ b/tests/unit_tests/openvpn/test_push_upd= ate_msg.c
@@ -130,18 +130,11 @@
return= true;
}
#else /* ifndef ENABLE_MANAGEME= NT */
-char **res;
-int i;

bool
send_control_channel_string(struct co= ntext *c, const char *str, msglvl_t msglevel)
{
= - if (res && res[i] &a= mp;& strcmp(res[i], str))
- {
- = printf("\n\nexpected: %s\n\n actual: %s\n\n", res[i], str);
- return false;
- }
- i++;
+ check_expected(str);
return = true;
}

@@ -301,44 +294,= 75 @@

#ifdef ENABLE_MANAGEMENT char *r0[] =3D {
+ "= ;PUSH_UPDATE,redirect-gateway local,route 192.168.1.0 255.255.255.0",<= /span>
+ NULL
<= span> };

char *r1[] =3D {
"PUSH_= UPDATE,-dhcp-option,blablalalalalalalalalalalalalf, lalalalalalalalalalalal= alalaf,push-continuation 2",
"PUSH_UPDATE, a= kakakakakakakakakakakaf, dhcp-option DNS 8.8.8.8,redirect-gateway local,pus= h-continuation 2",
- "PUSH_UPDATE,route 192.168.1.0 255.255.255.0,push-continuation 1= "
+ "PUS= H_UPDATE,route 192.168.1.0 255.255.255.0,push-continuation 1",<= br>+ NULL
}= ;
char *r3[] =3D {
- "PUSH_UPDATE,,,"
+ "PUSH_UPDATE,,,",
+ NULL
}; char *r4[] =3D {
"PUSH_UPDATE,-dhcp-optio= n, blablalalalalalalalalalalalalf, lalalalalalalalalalalalalalaf,push-conti= nuation 2",
"PUSH_UPDATE, akakakakakakakakak= akakaf,dhcp-option DNS 8.8.8.8, redirect-gateway local,push-continuation 2&= quot;,
- "PUSH_= UPDATE, route 192.168.1.0 255.255.255.0,,push-continuation 1"+ "PUSH_UPDATE, route= 192.168.1.0 255.255.255.0,,push-continuation 1",
+ NULL
};
<= span> char *r5[] =3D {

"PUSH_UPDATE,,-dhcp-option= , blablalalalalalalalalalalalalf, lalalalalalalalalalalalalalaf,push-contin= uation 2",
"PUSH_UPDATE, akakakakakakakakaka= kakaf,dhcp-option DNS 8.8.8.8, redirect-gateway local,push-continuation 2&q= uot;,
- "PUSH_U= PDATE, route 192.168.1.0 255.255.255.0,push-continuation 1"
= + "PUSH_UPDATE, route 1= 92.168.1.0 255.255.255.0,push-continuation 1",
+ NULL
};
char *r6[] =3D {

"PUSH_UPDATE,-dhcp-option,b= lablalalalalalalalalalalalalf, lalalalalalalalalalalalalalaf,push-continuat= ion 2",
"PUSH_UPDATE, akakakakakakakakakakak= af, dhcp-option DNS 8.8.8.8, redirect-gateway 10.10.10.10,,push-continuatio= n 2",
- "P= USH_UPDATE, route 192.168.1.0 255.255.255.0,,push-continuation 1"
+ "PUSH_UPDATE, r= oute 192.168.1.0 255.255.255.0,,push-continuation 1",
+ NULL
};<= br> char *r7[] =3D {
"PUSH_UPDATE,,,,,,,,,,= ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,= ,,,,,push-continuation 2",
- "PUSH_UPDATE,,,,,,,,,,,,,,,,,,,push-continuation 1"=
+ "PUSH_UPDA= TE,,,,,,,,,,,,,,,,,,,push-continuation 1",
+ NULL
};
c= har *r8[] =3D {
"PUSH_UPDATE,-dhcp-option,blablal= alalalalalalalalalalalf, lalalalalalalalalalalalalalaf,push-continuation 2&= quot;,
"PUSH_UPDATE, akakakakakakakakakakakaf, dh= cp-option DNS 8.8.8.8,redirect-gateway\n local,push-continuation 2",
- "PUSH_UPDATE,r= oute 192.168.1.0 255.255.255.0\n\n\n,push-continuation 1"
+ "PUSH_UPDATE,route 192.= 168.1.0 255.255.255.0\n\n\n,push-continuation 1",

+ NULL
};
<= span> char *r9[] =3D {

= - "PUSH_UPDATE,,"
+ "PUSH_UPDATE,,",
+ NULL
};
-
+char *r11[] =3D {
+ "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2",
+ "PUSH_UPDATE,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,= a,a,a,a,a,push-continuation 2",
+ "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,= a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,push-continuation 2&quo= t;,
+ "PUSH_U= PDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,push-continuation 2",
+ "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,push-continuation 1",
+ NULL
+};
+= char *r12[] =3D {
+ = "PUSH_UPDATE,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a,push-continuation 2",
+ "PUSH_UPDATE,abc,push-conti= nuation 1",
+ = NULL
+};
<= span style=3D"color: hsl(120, 100%, 40%);">+char *r13[] =3D {

+ "PUSH_UPDATE,a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a= ,",

+ NULL
+};
+char *r14[] =3D {
+ "PUSH_UPDATE,a,push-continuation= 2",
+ "= PUSH_UPDATE,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa= aaaaaaaaaaaaaaaaaaaaaaaa,push-continuation 2",
+ "PUSH_UPDATE,a,push-continuation= 1",
+ NULL
+};
<= /span>
const char *msg0 =3D "redirect-gateway local,route 19= 2.168.1.0 255.255.255.0";
const char *msg1 =3D "= -dhcp-option,blablalalalalalalalalalalalalf, lalalalalalalalalalalalalalaf,= "
@@ -365,32 +389,50 @@
= "daisy damage damp dance danger daring dash daughter dawn day d= eal debate debris decade december decide decline"
= "decorate decrease deer defense define defy degree de= lay deliver demand demise denial";

+const char *msg11 =3D "a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,&quo= t;
+ = "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,= a,a,a,a,a,a,a,"
= + "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,= a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,"
+ "a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,= a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,"
+ "a,a,a,a,a,a,= a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a";<= /span>
+
+const char *msg12 =3D "a,a,a,a,a,a= ,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,,,,,,a= ,abc";
+<= br>+const char *msg13 =3D "= ;a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,= a,a,a,,,,,,a,";
= +
+const char *msg14 = =3D "a,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa= aaaaaaaaaaaaaaaaaaaaaaaa,a";
+
#define PUSH_BUNDLE_SIZE_TEST 184
+#define exp= ect_control_channel_strings(res) \
+ do = \

+ { = \
+ = for (int j =3D 0; res[j] !=3D NULL; j++) \+ { = \
+ expect_string(send_control_channel_strin= g, str, res[j]); \
+ = } \
+ } while (0)+
static void=
test_send_push_msg0(void **state)
{
- i =3D 0;
- res =3D r0;

= struct multi_context *m =3D *state;
const unsigned l= ong cid =3D 0;
+ e= xpect_control_channel_strings(r0);
assert_int_equal(se= nd_push_update(m, &cid, msg0, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
}
+
static void
test_send_push_msg1(void **sta= te)
{
= - i =3D 0;
- res = =3D r1;
struct multi_context *m =3D *state;
= const unsigned long cid =3D 0;
+ expect_control_channel_strings(r1);
assert_int_equal(send_push_update(m, &cid, msg1, UPT_BY_CID, PU= SH_BUNDLE_SIZE_TEST), 1);

}

static void

test_send_push_msg2(void **state)<= br> {
- i =3D = 0;
- res =3D NULL;
struct multi_context *m =3D *state;
= const unsigned long cid =3D 0;
assert_int_equal(send= _push_update(m, &cid, msg2, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), -EINVAL= );
@@ -399,83 +441,110 @@
static void
test_send_push_msg3(void **state)
{- i =3D 0;
- res =3D r3;
str= uct multi_context *m =3D *state;
const unsigned long c= id =3D 0;
+ expect= _control_channel_strings(r3);
assert_int_equal(send_pu= sh_update(m, &cid, msg3, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);=
}

static void
test_send_push_msg4(void **state)

{
- i =3D 0;
- res =3D r4;
struct multi_c= ontext *m =3D *state;
const unsigned long cid =3D 0;
+ expect_control_ch= annel_strings(r4);
assert_int_equal(send_push_update(m= , &cid, msg4, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
= }

static void
test_send= _push_msg5(void **state)
{
- i =3D 0;
- res =3D r5;
struct multi_context *m = =3D *state;
const unsigned long cid =3D 0;
<= span style=3D"color: hsl(120, 100%, 40%);">+ expect_control_channel_stri= ngs(r5);

assert_int_equal(send_push_update(m, &cid= , msg5, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
}
static void
test_send_push_msg6= (void **state)
{
- i =3D 0;
+ expect_control_channel_strings(r6);
assert_int_equal(send_push_update(m, &cid, msg6, UP= T_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);
}
<= /span>
static void
test_send_push_msg7(void **st= ate)
{
- i =3D 0;
- res= =3D r7;
struct multi_context *m =3D *state; const unsigned long cid =3D 0;
+ expect_control_channel_strings(r7);
assert_int_equal(send_push_update(m, &cid, msg7, UPT_BY_CID, P= USH_BUNDLE_SIZE_TEST), 1);

}

<= span> static void

test_send_push_msg8(void **state)=
{
- i =3D= 0;
- res =3D r8;
struct multi_context *m =3D *state;
= const unsigned long cid =3D 0;
+ expect_control_channel_strings(r8);
as= sert_int_equal(send_push_update(m, &cid, msg8, UPT_BY_CID, PUSH_BUNDLE_= SIZE_TEST), 1);
}

stati= c void
test_send_push_msg9(void **state)
= {
- i =3D 0;<= br>- res =3D r9;
struct multi_context *m =3D *state;

const unsi= gned long cid =3D 0;
= + expect_control_channel_strings(r9);
assert_int_eq= ual(send_push_update(m, &cid, msg9, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST),= 1);
}

static void
test_send_push_msg10(void **state)
{- i =3D 0;
- res =3D NULL;
= struct multi_context *m =3D *state;
const unsigned lon= g cid =3D 0;
assert_int_equal(send_push_update(m, &= ;cid, msg10, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), -EINVAL);
= }

= +static void
+test_se= nd_push_msg11(void **state)
+{
+ struct= multi_context *m =3D *state;
+ const unsigned long cid =3D 0;
+ expect_control_channel_strings(r11);+ assert_int_equal(send_pus= h_update(m, &cid, msg11, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST), 1);=
+}
+
+static void
+test_send_push_msg12(void **state)
+{
= + struct multi_context *m =3D *state;
+ const unsigned long cid =3D 0;
+ expect_control_channel_strings(r12)= ;
+ assert_int_equ= al(send_push_update(m, &cid, msg12, UPT_BY_CID, PUSH_BUNDLE_SIZE_TEST),= 1);
+}
+

+static void
+test_send_push_msg13(void **state)
+{
+ struct multi_context *m =3D *state;
+ const unsigned long cid =3D 0;=
+ expect_control_channel= _strings(r13);
+ a= ssert_int_equal(send_push_update(m, &cid, msg13, UPT_BY_CID, PUSH_BUNDL= E_SIZE_TEST), 1);
+}<= /span>
+
+static void
+test_send_push_msg14(void **state)
+{

+ struct multi_context *m =3D *state;
= + const unsigned long cid = =3D 0;
+ expect_co= ntrol_channel_strings(r14);
+ assert_int_equal(send_push_update(m, &cid, msg14, UPT_BY_CI= D, PUSH_BUNDLE_SIZE_TEST), 1);
+}
+=
#undef PUSH_BUNDLE_SIZE_TEST

= static int
@@ -535,6 +604,7 @@
cmo= cka_unit_test_setup_teardown(test_incoming_push_message_mix, setup, teardow= n),
cmocka_unit_test_setup_teardown(test_incoming_= push_message_mix2, setup, teardown),
#ifdef ENABLE_MANAGEM= ENT
+
cmocka_unit_test_setup_teardown(test_send_push_msg0, setup2, tear= down2),

cmocka_unit_test_setup_teardown(test_send_= push_msg1, setup2, teardown2),
cmocka_unit_test_se= tup_teardown(test_send_push_msg2, setup2, teardown2),
@@ -5= 45,7 +615,11 @@
cmocka_unit_test_setup_teardown(te= st_send_push_msg7, setup2, teardown2),
cmocka_unit= _test_setup_teardown(test_send_push_msg8, setup2, teardown2),
cmocka_unit_test_setup_teardown(test_send_push_msg9, setup2, te= ardown2),

- cmoc= ka_unit_test_setup_teardown(test_send_push_msg10, setup2, teardown2)=
+ cmocka_unit_test_s= etup_teardown(test_send_push_msg10, setup2, teardown2),
+ cmocka_unit_test_setup_teardown= (test_send_push_msg11, setup2, teardown2),
+ cmocka_unit_test_setup_teardown(test_send_pu= sh_msg12, setup2, teardown2),
+ cmocka_unit_test_setup_teardown(test_send_push_msg13, set= up2, teardown2),
+ = cmocka_unit_test_setup_teardown(test_send_push_msg14, setup2, teardown= 2)
#endif
};
=

To view, visit change 1316. To unsubscribe, or fo= r help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Idba419681fe3ccc4e6e2f6ce759= 2332dcff62cd9
Gerrit-Change-Number: 1316
Gerrit-PatchSet: 4
Gerrit-Owner: mrbff <marco@...2726...> <= /div>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: mrbff <marco@...2726...&g= t;
= --IMB1D2axsWQ=--