From: "plaisthos (Code Review)" Attention is currently required from: cron2, flichtenheld, plaisthos. <=
/p>
plaisthos uploaded patch set #3 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 cron2, Code-Review-1 by flichtenheldAllow trailing \r and=
\n in control channel message
Writing a reason from a script will e=
asily end up adding extra \r\n characters
at the end of the reason. Our =
current code pushes this to the peer. So be more
liberal in accepting th=
ese message.
Closes openvpn/openvpn#568
Change-Id: I47c992b6b=
73b1475cbff8a28f720cf50dc1fbe3e
Signed-off-by: Arne Schwabe <arne@...493...=
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 file=
s changed, 92 insertions(+), 30 deletions(-)git pull ssh://gerri=
t.openvpn.net:29418/openvpn refs/changes/72/672/3
diff --git a/src/=
openvpn/forward.c b/src/openvpn/forward.c
index 71b7167..a9=
f25a5 100644
--- a/src/openvpn/forward.c
++=
+ b/src/openvpn/forward.c
@@ -290,41 +290,14 @@
<=
span> 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 cmd=
buf =3D extract_command_buffer(buf, &gc);
<=
span style=3D"color: hsl(0, 100%, 40%);">- 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 onl=
y holds the current command and
- * its closing NUL byte */
- struct buffer cmdbuf =3D al=
loc_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: Rec=
eived control with invalid characters: %s",
- format_hex(BPTR(&buf=
), BLEN(&buf), 256, &gc));
- }
- else
- {
- parse_incoming_control_channel_command(c, &cmdb=
uf);
- }=
+ par=
se_incoming_control_channel_command(c, &cmdbuf);
=
}
- e=
lse
- {
- 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 2ec0b2=
f..cc23da2 100644
--- a/src/openvpn/ssl_pkt.c
@@ -557,3 +557,43 @@<=
br> }
return false;
}
+
+struct buffer
+extract_command_buffer(struct buffer *buf, struct =
gc_arena *gc)
+{
+ /* commands on the c=
ontrol channel are seperated by 0x00 bytes.
+ * cmdlen does not include the 0 byte of the st=
ring */
+ int cmdl=
en =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;<=
/span>
+ }
+ /* include the NUL byte and ensure NUL terminatio=
n */
+ int buflen =
=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(buflen, gc);
+ buf_write(&cmdbuf, BPTR(buf), buflen);
+
+ /* Remove \r and \n at the end of the buffer to avoid=
+ * problems wit=
h scripts and other that add extra \r and \n */
+ buf_chomp(&cmdbuf);
+
+ /* check we have only printable characters or null byt=
e in the
+ * comm=
and string and no newlines */
+ if (!string_check_buf(&cmdbuf, CC_PRINT | CC_NULL, CC_CRL=
F))
+ {
=
+ msg(D_PUSH_ERRORS, &qu=
ot;WARNING: Received control with invalid characters: %s",
<=
span style=3D"color: hsl(120, 100%, 40%);">+ format_hex(BPTR(&am=
p;cmdbuf), BLEN(&cmdbuf), 256, gc));
+ cmdbuf.len =3D 0;
+ }
+
+ bu=
f_advance(buf, buflen);
+ return cmdbuf;
+}
diff --git a/src/openvpn/ssl_pkt.h b/src/openvpn/ssl_p=
kt.h
index 88b9e8c..c8a27fb 100644
--- a/sr=
c/openvpn/ssl_pkt.h
+++ b/src/openvpn/ssl_pkt.h
<=
span>@@ -230,6 +230,20 @@
uint8_t hea=
der,
bool request_resend_wkc);=
+<=
br>+/**
+ * Extracts a control channel message fro=
m buf and adjusts the size of
+ * buf after the message has been extracted
+ * @param buf The buffer the message sh=
ould be extracted from
+ * @param gc gc_arena to be used for the returned buffer and displayi=
ng
+ * d=
iagnostic messages
+ =
* @return A buffer with a control channel message or a buffer with
+ * with le=
ngth 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(i=
nt op)
{
diff --git a/tests/unit_tests/ope=
nvpn/test_pkt.c b/tests/unit_tests/openvpn/test_pkt.c
index=
1084d66..741c982 100644
--- a/tests/unit_tests/openvpn/tes=
t_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_bu=
f_gc(1024, &gc);
=
+
+ /* This messag=
e 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\0valid one again";+
+ buf_write(&input_buf, input, sizeof(input))=
;
+ struct buffer =
cmd1 =3D extract_command_buffer(&input_buf, &gc);
+ struct buffer cmd2 =3D extract_co=
mmand_buffer(&input_buf, &gc);
+ struct buffer cmd3 =3D extract_command_buffer(&i=
nput_buf, &gc);
+=
struct buffer cmd4 =3D extract_command_buffer(&input_buf, &gc);=
+ struct buffer c=
md5 =3D extract_command_buffer(&input_buf, &gc);
+
+ assert_string_equal(BSTR(&cmd1), "valid cont=
rol 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(non=
ull));
+ struct bu=
ffer nonullcmd =3D extract_command_buffer(&nonull_buf, &gc);=
+ assert_int_equal(nonul=
lcmd.len, 0);
+
+ gc_free(&gc);
+}
+
int
ma=
in(void)
{
@@ -640,6 +674,7 @@
<=
span> cmocka_unit_test(test_verify_hmac_tls_auth),
=
cmocka_unit_test(test_generate_reset_packet_plain),
+ cmocka_unit_test(test=
_extract_control_message)
};
<=
br> #if defined(ENABLE_CRYPTO_OPENSSL)
To view, visit change 672. To unsubscribe, or for help writing mail filter= s, visit settings.