From: "plaisthos (Code Review)" Date: Wed, 10 Jul 2024 13:50:44 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <4ce80a73ef325b7ee7337d00e4385bcb82842e4c-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="kFGtHeJ+N5s="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Allow trailing \r and \n in control channel message List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: cron2 , flichtenheld Cc: openvpn-devel --kFGtHeJ+N5s= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, plaisthos. Hello cron2, flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/672?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: Allow trailing \r and \n in control channel message ...................................................................... Allow trailing \r and \n in control channel message Writing a reason from a script will easily end up adding extra \r\n charact= ers at the end of the reason. Our current code pushes this to the peer. So be m= ore liberal in accepting these message. Closes openvpn/openvpn#568 Change-Id: I47c992b6b73b1475cbff8a28f720cf50dc1fbe3e Signed-off-by: Arne Schwabe --- M src/openvpn/forward.c M src/openvpn/ssl_pkt.c M src/openvpn/ssl_pkt.h M tests/unit_tests/openvpn/test_pkt.c 4 files changed, 92 insertions(+), 30 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/72/672/4 diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 71b7167..40b7cc4 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -290,41 +290,14 @@ struct buffer buf =3D alloc_buf_gc(len, &gc); if (tls_rec_payload(c->c2.tls_multi, &buf)) { - while (BLEN(&buf) > 1) { - /* commands on the control channel are seperated by 0x00 bytes. - * cmdlen does not include the 0 byte of the string */ - int cmdlen =3D (int)strnlen(BSTR(&buf), BLEN(&buf)); + struct buffer cmdbuf =3D extract_command_buffer(&buf, &gc); - if (cmdlen < BLEN(&buf)) + if (cmdbuf.len > 0) { - /* include the NUL byte and ensure NUL termination */ - int cmdlen =3D (int)strlen(BSTR(&buf)) + 1; - - /* Construct a buffer that only holds the current command = and - * its closing NUL byte */ - struct buffer cmdbuf =3D alloc_buf_gc(cmdlen, &gc); - buf_write(&cmdbuf, BPTR(&buf), cmdlen); - - /* check we have only printable characters or null byte in= the - * command string and no newlines */ - if (!string_check_buf(&buf, CC_PRINT | CC_NULL, CC_CRLF)) - { - msg(D_PUSH_ERRORS, "WARNING: Received control with inv= alid characters: %s", - format_hex(BPTR(&buf), BLEN(&buf), 256, &gc)); - } - else - { - parse_incoming_control_channel_command(c, &cmdbuf); - } + parse_incoming_control_channel_command(c, &cmdbuf); } - else - { - msg(D_PUSH_ERRORS, "WARNING: Ignoring control channel " - "message command without NUL termination"); - } - buf_advance(&buf, cmdlen); } } else diff --git a/src/openvpn/ssl_pkt.c b/src/openvpn/ssl_pkt.c index 2ec0b2f..689cd7f 100644 --- a/src/openvpn/ssl_pkt.c +++ b/src/openvpn/ssl_pkt.c @@ -557,3 +557,43 @@ } return false; } + +struct buffer +extract_command_buffer(struct buffer *buf, struct gc_arena *gc) +{ + /* commands on the control channel are seperated by 0x00 bytes. + * cmdlen does not include the 0 byte of the string */ + int cmdlen =3D (int)strnlen(BSTR(buf), BLEN(buf)); + + if (cmdlen >=3D BLEN(buf)) + { + buf_advance(buf, cmdlen); + /* Return empty buffer */ + struct buffer empty =3D { 0 }; + return empty; + } + + /* include the NUL byte and ensure NUL termination */ + cmdlen +=3D 1; + + /* Construct a buffer that only holds the current command and + * its closing NUL byte */ + struct buffer cmdbuf =3D alloc_buf_gc(cmdlen, gc); + buf_write(&cmdbuf, BPTR(buf), cmdlen); + + /* Remove \r and \n at the end of the buffer to avoid + * problems with scripts and other that add extra \r and \n */ + buf_chomp(&cmdbuf); + + /* check we have only printable characters or null byte in the + * command string and no newlines */ + if (!string_check_buf(&cmdbuf, CC_PRINT | CC_NULL, CC_CRLF)) + { + msg(D_PUSH_ERRORS, "WARNING: Received control with invalid charact= ers: %s", + format_hex(BPTR(&cmdbuf), BLEN(&cmdbuf), 256, gc)); + cmdbuf.len =3D 0; + } + + buf_advance(buf, cmdlen); + return cmdbuf; +} diff --git a/src/openvpn/ssl_pkt.h b/src/openvpn/ssl_pkt.h index 88b9e8c..c8a27fb 100644 --- a/src/openvpn/ssl_pkt.h +++ b/src/openvpn/ssl_pkt.h @@ -230,6 +230,20 @@ uint8_t header, bool request_resend_wkc); + +/** + * Extracts a control channel message from buf and adjusts the size of + * buf after the message has been extracted + * @param buf The buffer the message should be extracted from + * @param gc gc_arena to be used for the returned buffer and displaying + * diagnostic messages + * @return A buffer with a control channel message or a buffer with + * with length 0 if there is no message or the message has + * invalid characters. + */ +struct buffer +extract_command_buffer(struct buffer *buf, struct gc_arena *gc); + static inline const char * packet_opcode_name(int op) { diff --git a/tests/unit_tests/openvpn/test_pkt.c b/tests/unit_tests/openvpn= /test_pkt.c index 1084d66..741c982 100644 --- a/tests/unit_tests/openvpn/test_pkt.c +++ b/tests/unit_tests/openvpn/test_pkt.c @@ -626,6 +626,40 @@ free_tas(&tas_server); } +static void +test_extract_control_message(void **ut_state) +{ + struct gc_arena gc =3D gc_new(); + struct buffer input_buf =3D alloc_buf_gc(1024, &gc); + + /* This message will have a \0x00 at the end since it is a C string */ + const char input[] =3D "valid control message\r\n\0\0Invalid\r\none\0v= alid one again"; + + buf_write(&input_buf, input, sizeof(input)); + struct buffer cmd1 =3D extract_command_buffer(&input_buf, &gc); + struct buffer cmd2 =3D extract_command_buffer(&input_buf, &gc); + struct buffer cmd3 =3D extract_command_buffer(&input_buf, &gc); + struct buffer cmd4 =3D extract_command_buffer(&input_buf, &gc); + struct buffer cmd5 =3D extract_command_buffer(&input_buf, &gc); + + assert_string_equal(BSTR(&cmd1), "valid control message"); + /* empty message with just a \0x00 */ + assert_int_equal(cmd2.len, 1); + assert_string_equal(BSTR(&cmd2), ""); + assert_int_equal(cmd3.len, 0); + assert_string_equal(BSTR(&cmd4), "valid one again"); + assert_int_equal(cmd5.len, 0); + + const uint8_t nonull[6] =3D { 'n', 'o', ' ', 'N', 'U', 'L'}; + struct buffer nonull_buf =3D alloc_buf_gc(1024, &gc); + + buf_write(&nonull_buf, nonull, sizeof(nonull)); + struct buffer nonullcmd =3D extract_command_buffer(&nonull_buf, &gc); + assert_int_equal(nonullcmd.len, 0); + + gc_free(&gc); +} + int main(void) { @@ -640,6 +674,7 @@ cmocka_unit_test(test_verify_hmac_tls_auth), cmocka_unit_test(test_generate_reset_packet_plain), cmocka_unit_test(test_generate_reset_packet_tls_auth), + cmocka_unit_test(test_extract_control_message) }; #if defined(ENABLE_CRYPTO_OPENSSL) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/672?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I47c992b6b73b1475cbff8a28f720cf50dc1fbe3e Gerrit-Change-Number: 672 Gerrit-PatchSet: 4 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --kFGtHeJ+N5s= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, plaisthos. <= /p>

plaisthos uploaded patch set #4 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/672?usp=3Demail">View Chang= e

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

Allow trailing \r and \n in control channel m=
essage

Writing a reason from a script will easily end up adding extr= a \r\n characters
at the end of the reason. Our current code pushes this= to the peer. So be more
liberal in accepting these message.

Clos= es openvpn/openvpn#568

Change-Id: I47c992b6b73b1475cbff8a28f720cf50d= c1fbe3e
Signed-off-by: Arne Schwabe <arne@...1227...>
---
M = src/openvpn/forward.c
M src/openvpn/ssl_pkt.c
M src/openvpn/ssl_pkt.h=
M tests/unit_tests/openvpn/test_pkt.c
4 files changed, 92 insertions= (+), 30 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/open=
vpn refs/changes/72/672/4
diff --git a/src/openvpn/forward.c b/src/=
openvpn/forward.c
index 71b7167..40b7cc4 100644
<= span>--- a/src/openvpn/forward.c
+++ b/src/openvpn/forward.= c
@@ -290,41 +290,14 @@
struct buffer = buf =3D alloc_buf_gc(len, &gc);
if (tls_rec_payloa= d(c->c2.tls_multi, &buf))
{
-
while (BLEN(&am= p;buf) > 1)
{
- /* commands on the control channel are sepe= rated by 0x00 bytes.
- = * cmdlen does not include the 0 byte of the string */- int cmdlen =3D (int= )strnlen(BSTR(&buf), BLEN(&buf));
+ struct buffer cmdbuf =3D extract_command_= buffer(&buf, &gc);

- if (cmdlen < BLEN(&buf))+ if (cmdbuf.len &= gt; 0)
{
- /* include the NUL byte and ensure NUL term= ination */
- = int cmdlen =3D (int)strlen(BSTR(&buf)) + 1;
-
- /* Construct a buffer that only holds the curren= t command and
- = * its closing NUL byte */
- struct buffer cmdbuf =3D alloc_buf_gc(cmdlen, = &gc);
- = buf_write(&cmdbuf, BPTR(&buf), cmdlen);
-
- /* check we have only printable characters or nu= ll byte in the
- = * command string and no newlines */
- if (!string_check_buf(&buf, CC_= PRINT | CC_NULL, CC_CRLF))
- {
= - msg(D_PUSH_ERRORS, "WARNING: Received control wit= h invalid characters: %s",
- format_hex(BPTR(&buf), BLEN(&buf)= , 256, &gc));
- = }
- = else
- = {
- = parse_incoming_control_channel_command(c, &cmdbuf);
- }
+ parse_incoming_contr= ol_channel_command(c, &cmdbuf);
}- else
- {
- msg(D_PUSH_ERRORS, "WA= RNING: Ignoring control channel "
- "message command without NUL term= ination");
- = }
- bu= f_advance(&buf, cmdlen);
}
= }
else
diff --git a/src/openvpn/ssl_= pkt.c b/src/openvpn/ssl_pkt.c
index 2ec0b2f..689cd7f 100644=
--- a/src/openvpn/ssl_pkt.c
+++ b/src/open= vpn/ssl_pkt.c
@@ -557,3 +557,43 @@
}
return false;
}
+
+struct buffer
+extract_command_buffer(struct buffer *buf, struct gc_arena *gc)

+{
+ /* commands on the control channel a= re seperated by 0x00 bytes.
+ * cmdlen does not include the 0 byte of the string */+ int cmdlen =3D (int)strn= len(BSTR(buf), BLEN(buf));
+
+ if (cmdl= en >=3D BLEN(buf))
+ {
+ buf_= advance(buf, cmdlen);
+ /* Return empty buffer */
+ struct buffer empty =3D { 0 };
+ return empty;
+ }
+
+ /* include the NUL byte and ensure NUL termination */
+ cmdlen +=3D 1;
+
+ /* Construct a buffer that only holds the curren= t command and
+ *= its closing NUL byte */
+ struct buffer cmdbuf =3D alloc_buf_gc(cmdlen, gc);
+ buf_write(&cmdbuf, BPTR(buf= ), cmdlen);
+<= br>+ /* Remove \r and \n at = the end of the buffer to avoid
+ * problems with scripts and other that add extra \r and \n = */
+ buf_chomp(&am= p;cmdbuf);
++ /* check we have only pr= intable characters or null byte in the
+ * command string and no newlines */
+ if (!string_check_buf(&cmdb= uf, CC_PRINT | CC_NULL, CC_CRLF))
+ {
+= msg(D_PUSH_ERRORS, "WARNING: Received control with invalid cha= racters: %s",
+ = format_hex(BPTR(&cmdbuf), BLEN(&cmdbuf), 256, gc));
+ cmdbuf.len =3D 0= ;
+ }
+

+ buf_advance(buf, cmdlen);
+ return cmdbuf;
+}
diff --git a/src/openvp= n/ssl_pkt.h b/src/openvpn/ssl_pkt.h
index 88b9e8c..c8a27fb = 100644
--- a/src/openvpn/ssl_pkt.h
+++ b/sr= c/openvpn/ssl_pkt.h
@@ -230,6 +230,20 @@
= uint8_t header,
b= ool request_resend_wkc);

+
+/**
+ * Extracts = a control channel message from buf and adjusts the size of
+ * buf after the message has been ex= tracted
+ * @param bu= f The buffer the message should be extracted from
+ * @param gc gc_arena to be used for t= he returned buffer and displaying
+ * diagnostic messages
+ * @return A buffer with a control chann= el message or a buffer with
+ * with length 0 if there is no message or the message= has
+ * = invalid characters.
= + */
+struct buffer
+extract_command_buffe= r(struct buffer *buf, struct gc_arena *gc);
+
static inline const char *=
packet_opcode_name(int op)
{
di= ff --git a/tests/unit_tests/openvpn/test_pkt.c b/tests/unit_tests/openvpn/t= est_pkt.c
index 1084d66..741c982 100644
---= a/tests/unit_tests/openvpn/test_pkt.c
+++ b/tests/unit_tes= ts/openvpn/test_pkt.c
@@ -626,6 +626,40 @@
= free_tas(&tas_server);
}
=
+static void
+test_extract_control_message(void = **ut_state)

+{=
+ struct gc_arena gc =3D= gc_new();
+ struc= t buffer input_buf =3D alloc_buf_gc(1024, &gc);
+
+ /* This message will have a \0x00 at the end since it is= a C string */
+ c= onst char input[] =3D "valid control message\r\n\0\0Invalid\r\none\0va= lid one again";
= +
+ buf_write(&= ;input_buf, input, sizeof(input));
+ struct buffer cmd1 =3D extract_command_buffer(&input= _buf, &gc);
+ = struct buffer cmd2 =3D extract_command_buffer(&input_buf, &gc);

+ struct buffer cmd3 = =3D extract_command_buffer(&input_buf, &gc);
+ struct buffer cmd4 =3D extract_comman= d_buffer(&input_buf, &gc);
+ struct buffer cmd5 =3D extract_command_buffer(&input= _buf, &gc);
+

+ assert_string_equal= (BSTR(&cmd1), "valid control message");
+ /* empty message with just a \0x00 */=
+ assert_int_equa= l(cmd2.len, 1);
+ = assert_string_equal(BSTR(&cmd2), "");
+ assert_int_equal(cmd3.len, 0);=
+ assert_string_equal(BS= TR(&cmd4), "valid one again");
+ assert_int_equal(cmd5.len, 0);
+
+ const uint8_t nonull[6] =3D { 'n', 'o&= #39;, ' ', 'N', 'U', 'L'};
+ struct buffer nonull_buf =3D all= oc_buf_gc(1024, &gc);
+
+ buf_write= (&nonull_buf, nonull, sizeof(nonull));
+ struct buffer nonullcmd =3D extract_command_buff= er(&nonull_buf, &gc);
+ assert_int_equal(nonullcmd.len, 0);
+
+ gc_free(&gc);
+}
+
int
main(void)
{
<= span>@@ -640,6 +674,7 @@

cmocka_unit_test(test_ver= ify_hmac_tls_auth),
cmocka_unit_test(test_generate= _reset_packet_plain),
cmocka_unit_test(test_genera= te_reset_packet_tls_auth),
+ cmocka_unit_test(test_extract_control_message)
};

#if defined(ENABLE_CRYPTO_OPE= NSSL)

To view, visit change 672. To unsubscr= ibe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I47c992b6b73b1475cbff8a28f72= 0cf50dc1fbe3e
Gerrit-Change-Number: 672
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--kFGtHeJ+N5s=--