forked from robur/blog.robur.coop
229 lines
17 KiB
HTML
229 lines
17 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>
|
|
Robur's blogMirageVPN and OpenVPN
|
|
</title>
|
|
<meta name="description" content="Discoveries made implementing MirageVPN, a OpenVPN-compatible VPN library">
|
|
<link type="text/css" rel="stylesheet" href="https://blog.robur.coop/css/hl.css">
|
|
<link type="text/css" rel="stylesheet" href="https://blog.robur.coop/css/style.css">
|
|
<script src="https://blog.robur.coop/js/hl.js"></script>
|
|
<link rel="alternate" type="application/rss+xml" href="https://blog.robur.coop/feed.xml" title="blog.robur.coop">
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>blog.robur.coop</h1>
|
|
<blockquote>
|
|
The <strong>Robur</strong> cooperative blog.
|
|
</blockquote>
|
|
</header>
|
|
<main><a href="https://blog.robur.coop/index.html">Back to index</a>
|
|
|
|
<article>
|
|
<h1>MirageVPN and OpenVPN</h1>
|
|
<ul class="tags-list"><li>MirageVPN</li><li>OpenVPN</li><li>security</li></ul><p>At <a href="https://robur.coop/">Robur</a> we have been busy at work implementing our OpenVPN™-compatible MirageVPN software.
|
|
Recently we have implemented the <a href="https://blog.robur.coop/articles/miragevpn-server.html">server side</a>.
|
|
In order to implement this side of the protocol I studied parts of the OpenVPN™ source code and performed experiments to understand what the implementation does at the protocol level.
|
|
Studying the OpenVPN™ implementation has lead me to discover two security issues: CVE-2024-28882 and CVE-2024-5594.
|
|
In this article I will talk about the relevant parts of the protocol, and describe the security issues in detail.</p>
|
|
<p>A VPN establishes a secure tunnel in which (usually) IP packets are sent.
|
|
The OpenVPN protocol establishes a TLS tunnel[^openvpn-tls] with which key material and configuration options are negotiated.
|
|
Once established the TLS tunnel is used to exchange so-called control channel messages.
|
|
They are NUL-terminated (well, more on that later) text messages sent in a single TLS record frame (mostly, more on that later).</p>
|
|
<p>I will describe two (groups) of control channel messages (and a bonus control channel message):</p>
|
|
<ul>
|
|
<li><code>EXIT</code>, <code>RESTART</code>, and <code>HALT</code></li>
|
|
<li><code>PUSH_REQUEST</code> / <code>PUSH_REPLY</code></li>
|
|
<li>(<code>AUTH_FAILED</code>)</li>
|
|
</ul>
|
|
<p>The <code>EXIT</code>, <code>RESTART</code>, and <code>HALT</code> messages share similarity.
|
|
They are all three used to signal to the client that it should disconnect[^disconnect] from the server.
|
|
<code>HALT</code> tells the client to disconnect and suggests the client should terminate.
|
|
<code>RESTART</code> also tells the client to disconnect and suggests the client can reconnect either to the same server or the next server if multiple are configured depending on flags in the message.
|
|
<code>EXIT</code> tells the <em>peer</em> that it is exiting and the <em>peer</em> should disconnect.
|
|
The last one can be sent by either the server or the client and is useful when the underlying transport is UDP.
|
|
It informs the peer that the sender is exiting and will (soon) not be receiving and ACK'ing messages; for UDP the peer would otherwise (re)send messages until a timeout.</p>
|
|
<p>Because the underlying transport can either be TCP or UDP the sender may have no guarantees that the message arrives.
|
|
OpenVPN's control channel implements a reliable layer with ACKs and retransmissions to work around that.
|
|
To accomodate this OpenVPN™ will wait five seconds before disconnecting to allow for retransmission of the exit message.</p>
|
|
<h3 id="the-bug"><a class="anchor" aria-hidden="true" href="#the-bug"></a>The bug</h3>
|
|
<p>While I was working on implementing more control channel message types I modified a client application that connects to a server and sends pings over the tunnel - instead of ICMPv4 echo requests I modified it to send the <code>EXIT</code> control channel message once a second.
|
|
In the server logs I saw that the server successfully received the <code>EXIT</code> message!
|
|
But nothing else happened.
|
|
The server just kept receiving <code>EXIT</code> messages but for some reason it never disconnected the client.</p>
|
|
<p>Curious about this behavior I dived into the OpenVPN™ source code and found that on each <code>EXIT</code> message it (re)schedules an exit (disconnect) timer! That is, every time the server receives an <code>EXIT</code> message it'll go "OK! I'll shut down this connection in five seconds" forgetting it promised to do so earlier, too.</p>
|
|
<h3 id="implications"><a class="anchor" aria-hidden="true" href="#implications"></a>Implications</h3>
|
|
<p>At first this seemed like a relatively benign bug.
|
|
What's the worst that could happen if a client says "let's stop in five second! No, five seconds from now! No, five seconds from now!" etc?
|
|
Well, it turns out the same timer is used when the server sends an exit message.
|
|
Ok, so what?
|
|
The client can hold open a resource it <em>was</em> authorized to use <em>longer</em>.
|
|
So we have a somewhat boring potential denial of service attack.</p>
|
|
<p>Then I learned more about the management interface.
|
|
The management interface is a text protocol to communicate with the OpenVPN server (or client) and query for information or send commands.
|
|
One command is the <code>client-kill</code> command.
|
|
The documentation says to use this command to "[i]mmediately kill a client instance[...]".
|
|
In practice it sends an exit message to the client (either a custom one or the default <code>RESTART</code>).
|
|
I learnt that it shares code paths with the exit control messages to schedule an exit (disconnect)[^kill-immediately].
|
|
That is, <code>client-kill</code> schedules the same five second timer.</p>
|
|
<p>Thus a malicious client can, instead of exiting on receiving an exit or <code>RESTART</code> message, send back repeatedly <code>EXIT</code> to the server to reset the five second timer.
|
|
This way the client can indefinitely delay the exit/disconnect assuming sufficiently stable and responsive network.
|
|
This is suddenly not so good.
|
|
The application using the management interface might be enforcing a security policy which we can now circumvent!
|
|
The client might be a former employee in a company, and the security team might want to revoke access to the internal network for the former employee, and in that process uses <code>client-kill</code> to kick off all of his connecting clients.
|
|
The former employee, if prepared, can circumvent this by sending back <code>EXIT</code> messages repeatedly and thus keep unauthorized access.
|
|
Or a commercial VPN service may try to enforce a data transfer limit with the same mechanism which is then rather easily circumvented by sending <code>EXIT</code> messages.</p>
|
|
<p>Does anyone use the management interface in this way?
|
|
I don't know.
|
|
If you do or are aware of software that enforces policies this way please do reach out to <a href="https://reyn.ir/contact.html">me</a>.
|
|
It would be interesting to hear and discuss.
|
|
The OpenVPN security@ mailing list took it seriously enough to assign it CVE-2024-28882.</p>
|
|
<h2 id="openvpn-configuration-language"><a class="anchor" aria-hidden="true" href="#openvpn-configuration-language"></a>OpenVPN configuration language</h2>
|
|
<p>Next up we have <code>PUSH_REQUEST</code> / <code>PUSH_REPLY</code>.
|
|
As the names suggest it's a request/response protocol.
|
|
It is used to communicate configuration options from the server to the client.
|
|
These options include routes, ip address configuration, negotiated cryptographic algorithms.
|
|
The client signals it would like to receive configuration options from the server by sending the <code>PUSH_REQUEST</code> control channel message[^proto-push-request].
|
|
The server then sends a <code>PUSH_REPLY</code> message.</p>
|
|
<p>The format of a <code>PUSH_REPLY</code> message is <code>PUSH_REPLY,</code> followed by a comma separated list of OpenVPN configuration directives terminated by a NUL byte as in other control channel messages.
|
|
Note that this means pushed configuration directives cannot contain commas.</p>
|
|
<p>When implementing the <code>push</code> server configuration directive, which tells the server to send the parameter of <code>push</code> as a configuration option to the client in the <code>PUSH_REPLY</code>, I studied how exactly OpenVPN™ parses configuration options.
|
|
I learned some quirks of the configuration language which I find surprising and somewhat hard to implement.
|
|
I will not cover all corners of the configuration language.</p>
|
|
<p>In some sense you could say the configuration language of OpenVPN™ is line based.
|
|
At least, the first step to parsing configuration directives as OpenVPN 2.X does is to read one line at a time and parse it as one configuration directive[^inline-files].
|
|
A line is whatever <code>fgets()</code> says it is - this includes the newline if not at the end of the file[^configuration-newlines].
|
|
This is how it is for configuration files.
|
|
However, if it is a <code>PUSH_REPLY</code> a <em>"line"</em> is the text string up to a comma or the end of file (or, importantly, a NUL byte).
|
|
This "line" tokenization is done by repeatedly calling OpenVPN™'s <code>buf_parse(buf, ',', line, sizeof(line))</code> function.</p>
|
|
<pre><code class="language-C">/* file: src/openvpn/buffer.c */
|
|
bool
|
|
buf_parse(struct buffer *buf, const int delim, char *line, const int size)
|
|
{
|
|
bool eol = false;
|
|
int n = 0;
|
|
int c;
|
|
|
|
ASSERT(size > 0);
|
|
|
|
do
|
|
{
|
|
c = buf_read_u8(buf);
|
|
if (c < 0)
|
|
{
|
|
eol = true;
|
|
}
|
|
if (c <= 0 || c == delim)
|
|
{
|
|
c = 0;
|
|
}
|
|
if (n >= size)
|
|
{
|
|
break;
|
|
}
|
|
line[n++] = c;
|
|
}
|
|
while (c);
|
|
|
|
line[size-1] = '\0';
|
|
return !(eol && !strlen(line));
|
|
}
|
|
</code></pre>
|
|
<p><code>buf_parse()</code> takes a <code>struct buffer*</code> which is a pointer to a byte array with a offset and length field, a delimiter character (in our case <code>','</code>), a destination buffer <code>line</code> and its length <code>size</code>.
|
|
It calls <code>buf_read_u8()</code> which returns the first character in the buffer and advances the offset and decrements the length, or returns <code>-1</code> if the buffer is empty.
|
|
In essence, <code>buf_parse()</code> "reads" from the buffer and copies over to <code>line</code> until it encounters <code>delim</code>, a NUL byte or the end of the buffer.
|
|
In that case a NUL byte is written to <code>line</code>.</p>
|
|
<p>What is interesting is that a NUL byte is effectively considered a delimiter, too, and that it is consumed by <code>buf_parse()</code>.
|
|
Next, let's look at how incoming control channel messages are handled (modified for brevity):</p>
|
|
<pre><code class="language-C">/* file: src/openvpn/forward.c (before fix) */
|
|
/*
|
|
* Handle incoming configuration
|
|
* messages on the control channel.
|
|
*/
|
|
static void
|
|
check_incoming_control_channel(struct context *c, struct buffer buf)
|
|
{
|
|
/* force null termination of message */
|
|
buf_null_terminate(&buf);
|
|
|
|
/* enforce character class restrictions */
|
|
string_mod(BSTR(&buf), CC_PRINT, CC_CRLF, 0);
|
|
|
|
if (buf_string_match_head_str(&buf, "AUTH_FAILED"))
|
|
{
|
|
receive_auth_failed(c, &buf);
|
|
}
|
|
else if (buf_string_match_head_str(&buf, "PUSH_"))
|
|
{
|
|
incoming_push_message(c, &buf);
|
|
}
|
|
/* SNIP */
|
|
}
|
|
</code></pre>
|
|
<p>First, the buffer is ensured to be NUL terminated by replacing the last byte with a NUL byte.
|
|
This is already somewhat questionable as it could make an otherwise invalid message valid.
|
|
Next, character class restrictions are "enforced".
|
|
What this roughly does is removing non-printable characters and carriage returns and line feeds from the C string.
|
|
The macro <code>BSTR()</code> returns the underlying buffer behind the <code>struct buffer</code> with the offset added.
|
|
Notably, <code>string_mod()</code> works on (NUL terminated) C strings and not <code>struct buffer</code>s.
|
|
As an example, the string (with the usual C escape sequences):</p>
|
|
<pre><code>"PUSH_REPLY,line \nfeeds\n,are\n,removed\n\000"
|
|
</code></pre>
|
|
<p>becomes</p>
|
|
<pre><code>"PUSH_REPLY,line feeds,are,removed\000ed\n\000"
|
|
</code></pre>
|
|
<p>As you can see, if interpreted as a C string we have removed the line feeds.
|
|
But what is this at the end?
|
|
It is the same last 4 bytes from the original string.
|
|
More generally, it is the last N bytes from the original string if the original string has N line feeds (or other disallowed characters).</p>
|
|
<p>The whole buffer is still passed to the push reply parsing.
|
|
Remember that the "line" parser will not only consume commas as the line delimiter, but also NUL bytes!
|
|
This means the configuration directives are parsed as lines:</p>
|
|
<pre><code class="language-C">"line feeds"
|
|
"are"
|
|
"removed"
|
|
"ed\n"
|
|
</code></pre>
|
|
<p>With this technique we can now inject (almost; the exception is NUL) arbitrary bytes as configuration directive lines.
|
|
This is bad because the configuration directive is printed to the console if it doesn't parse.
|
|
As a proof of concept I sent a <code>PUSH_REPLY</code> with an embedded BEL character, and the OpenVPN™ client prints to console (abbreviated):</p>
|
|
<pre><code>Unrecognized option or missing or extra parameter(s): ^G
|
|
</code></pre>
|
|
<p>The <code>^G</code> is how the BEL character is printed in my terminal.
|
|
I was also able to hear an audible bell.</p>
|
|
<p>A more thorough explanation on how terminal escape sequences can be exploited can be found on <a href="https://www.gresearch.com/news/g-research-the-terminal-escapes/">G-Reasearch's blog</a>.</p>
|
|
<h3 id="the-fix"><a class="anchor" aria-hidden="true" href="#the-fix"></a>The fix</h3>
|
|
<p>The fix also is also a first step towards decoupling the control channel messaging from the TLS record frames.
|
|
First, the data is split on NUL bytes in order to get the control channel message(s), and then messages are rejected if they contain illegal characters.
|
|
This solves the vulnerability described previously.</p>
|
|
<p>Unfortunately, it turns out that especially for the <code>AUTH_FAILED</code> control channel message it is easy to create invalid messages:
|
|
If 2FA is implemented using the script mechanism sending custom messages they easily end with a newline asking the client to enter the verification code.
|
|
I believe in 2.6.12 the client tolerates trailing newline characters.</p>
|
|
<h2 id="conclusion"><a class="anchor" aria-hidden="true" href="#conclusion"></a>Conclusion</h2>
|
|
<p>The first bug, the timer rescheduling bug, is at least 20 years old!
|
|
It hasn't always been exploitable, but the bug itself goes back as far as the git history does.
|
|
I haven't attempted further software archeology to find the exact time of introduction.
|
|
Either way, it's old and gone unnoticed for quite a while.</p>
|
|
<p>I think this shows that diversity in implementations is a great way to exercise corner cases, push forward (protocol) documentation efforts and get thorough code review by motivated peers.
|
|
This work was funded by <a href="https://nlnet.nl/project/MirageVPN/">the EU NGI Assure Fund through NLnet</a>.
|
|
In my opinion, this shows that funding one open source project can have a positive impact on other open source projects, too.</p>
|
|
<p>[^openvpn-tls]: This is not always the case. It is possible to use static shared secret keys, but it is mostly considered deprecated.
|
|
[^disconnect]: I say "disconnect" even when the underlying transport is the connection-less UDP.
|
|
[^kill-immediately]: As the alert reader might have realized this is inaccurate. It does not kill the client "immediately" as it will wait five seconds after the exit message is sent before exiting. At best this will kill a cooperating client once it's received the kill message.
|
|
[^proto-push-request]: There is another mechanism to request a <code>PUSH_REPLY</code> earlier with less roundtrips, but let's ignore that for now. The exact message is <code>PUSH_REQUEST<NUL-BYTE></code> as messages need to be NUL-terminated.
|
|
[^inline-files]: An exception being inline files which can span multiple lines. They vaguely resemble XML tags with an open <code><tag></code> and close <code></tag></code> each on their own line with the data in between. I doubt these are sent in <code>PUSH_REPLY</code>s, but I can't rule out without diving into the source code that it isn't possible to send inline files.
|
|
[^configuration-newlines]: This results in the quirk that it is possible to sort-of escape a newline in a configuration directive. But since the line splitting is done <em>first</em> it's not possible to continue the directive on the next line! I believe this is mostly useless, but it is a way to inject line feeds in configuration options without modifying the OpenVPN source code.</p>
|
|
|
|
</article>
|
|
|
|
</main>
|
|
<footer>
|
|
<a href="https://github.com/xhtmlboi/yocaml">Powered by <strong>YOCaml</strong></a>
|
|
<br />
|
|
</footer>
|
|
<script>hljs.highlightAll();</script>
|
|
</body>
|
|
</html>
|