Skip to contentSkip to conversation

Properly handle null bytes and invalid characters in control messages

PATCH In review Gert Doering 2 participants 4 messages Jun 19, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Properly handle null bytes and invalid characters in control messages

From: Arne Schwabe <[email protected]...>

This makes OpenVPN more picky in accepting control message in two aspects:
- Characters are checked in the whole buffer and not until the first NUL byte
- if the message contains invalid characters, we no longer continue evaluating a fixed up version of the message but rather stop processing it completely.

Previously it was possible to get invalid characters to end up in log files or on a terminal.

This also prepares the logic a bit in the direction of having a proper framing of control messages separated by null bytes instead of relying on the TLS framing for that. All OpenVPN implementations write the 0 bytes between control commands.

This patch also include several improvement suggestion from Reynir (thanks!).

CVE: 2024-5594

Change-Id I0d926f910637dabc89bf5fa919dc6beef1eb46d9
---
 src/openvpn/buffer.c                   |  17 ++++
 src/openvpn/buffer.h                   |  12 +++
 src/openvpn/forward.c                  | 123 ++++++++++++++++---------
 tests/unit_tests/openvpn/test_buffer.c |  24 +++++
 4 files changed, 132 insertions(+), 44 deletions(-)
src/openvpn/buffer.c +17 -0
index 3a8069c56..abe6a9c89 100644
@@ -1087,6 +1087,23 @@ string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive
     return ret;
 }
 
+bool
+string_check_buf(struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
+{
+    ASSERT(buf);
+
+    for (int i = 0; i < BLEN(buf); i++)
+    {
+        char c = BSTR(buf)[i];
+
+        if (!char_inc_exc(c, inclusive, exclusive))
+        {
+            return false;
+        }
+    }
+    return true;
+}
+
 const char *
 string_mod_const(const char *str,
                  const unsigned int inclusive,
src/openvpn/buffer.h +12 -0
index 27c319952..8a40010bc 100644
@@ -943,6 +943,18 @@ bool string_class(const char *str, const unsigned int inclusive, const unsigned
  */
 bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace);
 
+
+/**
+ * Check a buffer if it only consists of allowed characters.
+ *
+ * @param buf The buffer to be checked.
+ * @param inclusive The character classes that are allowed.
+ * @param exclusive Character classes that are not allowed even if they are also in inclusive.
+ * @return True if the string consists only of allowed characters, false otherwise.
+ */
+bool
+string_check_buf(struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive);
+
 /**
  * Returns a copy of a string with certain classes of characters of it replaced with a specified
  * character.
src/openvpn/forward.c +78 -43

135 lines not on this page · raw message

tests/unit_tests/openvpn/test_buffer.c +24 -0
index fbfb937d1..6b860b4cd 100644
@@ -354,6 +354,29 @@ test_character_class(void **state)
     assert_string_equal(buf, "There is a .'nice.' \"1234\" [.] year old .tree!");
 }
 
+
+static void
+test_character_string_mod_buf(void **state)
+{
+    struct gc_arena gc = gc_new();
+
+    struct buffer buf = alloc_buf_gc(1024, &gc);
+
+    const char test1[] =  "There is a nice 1234\x00 year old tree!";
+    buf_write(&buf, test1, sizeof(test1));
+
+    /* allow the null bytes and string but not the ! */
+    assert_false(string_check_buf(&buf, CC_ALNUM | CC_SPACE | CC_NULL, 0));
+
+    /* remove final ! and null byte to pass */
+    buf_inc_len(&buf, -2);
+    assert_true(string_check_buf(&buf, CC_ALNUM | CC_SPACE | CC_NULL, 0));
+
+    /* Check excluding digits works */
+    assert_false(string_check_buf(&buf, CC_ALNUM | CC_SPACE | CC_NULL, CC_DIGIT));
+    gc_free(&gc);
+}
+
 static void
 test_snprintf(void **state)
 {
@@ -437,6 +460,7 @@ main(void)
         cmocka_unit_test(test_buffer_free_gc_two),
         cmocka_unit_test(test_buffer_gc_realloc),
         cmocka_unit_test(test_character_class),
+        cmocka_unit_test(test_character_string_mod_buf),
         cmocka_unit_test(test_snprintf)
     };
 
Signature · 1 line
2.39.3 (Apple Git-146)
#1 lore raw

Hi,

Quoted 8 lines · Gert Doering

From: Arne Schwabe <[email protected]...>

This makes OpenVPN more picky in accepting control message in two aspects:
- Characters are checked in the whole buffer and not until the first NUL byte
- if the message contains invalid characters, we no longer continue evaluating a fixed up version of the message but rather stop processing it completely.

[..]

Quoted 1 line

CVE: 2024-5594

So, for the record - this patch was discussed and reviewed "in private" on the [email protected]... list, because it was seen as having security implications.

2.6.11 release will happen today or tomorrow, so I'm posting this patch to the public list now for transparency, and will proceed with testing and merging.

The security impact is fairly moderate - namely, a malicious peer can send (hand crafted) control channel messages that mess up logging(!) on the other side. No breach of crypto, leak of information, or remote code execution. But still an annoyance to be fixed and properly documented.

Thanks, Reynir, for reading our sources with so much passion for details :-)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#2 lore raw
In reply to Gert Doering

Hi,

I noticed a typo in a comment which is easy to understand correctly but makes it technically incorrect:

src/openvpn/forward.c check_incoming_control_channel(struct context *c) · line 297

Quoted 2 lines
src/openvpn/forward.c +2 -0
+            /* commands on the control channel are seperated by \0x00 bytes.
+             * cmdlen does not include the 0 byte of the string */

Here I think \0x00 should be \x00 (or \000 or some other way to write the NUL byte).

#3 lore raw
In reply to Gert Doering

I have tested this with lots of well-behaved peers - namely, client against 2.3/2.4/2.5 servers, and (master) server against 2.2-master clients. All works :-) (I did not test with a malicious endpoint).

Also, it has unit tests ;-)

Your patch has been applied to the master, release/2.6 and release/2.5 branch.

The 2.6 pullup pulls in "other unit tests" that have been master-only so far. Which is a bit of an annoyance, but having the UTs is good, they *pass* on 2.6, and it's easier on me than trying to only bring in this new test.

On the 2.5 pullup I have left out the unit tests, and had to amend the code lightly to remove the EXIT and AUTH_PENDING message handling.

release/2.4 is considered end of maintenance.

commit 414f428fa29694090ec4c46b10a8aba419c85659 (master) commit 90e7a858e5594d9a019ad2b4ac6154124986291a (release/2.6) commit d4921ba22f5ae4537d808986743a228617c86328 (release/2.5)

Author: Arne Schwabe
Date:   Mon May 27 15:02:41 2024 +0200
     Properly handle null bytes and invalid characters in control messages

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg28791.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering