From: "plaisthos (Code Review)" Date: Tue, 20 Jan 2026 18:02:37 +0000 In-Reply-To: References: Reply-To: 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="vPHObYDq6TA="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --vPHObYDq6TA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=3Demail to look at the new patch set (#3). Change subject: Merge stream_buf_get_next and stream_buf_set_next ...................................................................... Merge stream_buf_get_next and stream_buf_set_next The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used. This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly. This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whene= ver it is required. This assertion happens when we do not expect a read event from the socket a= nd then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next)); To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur. This also adds documentation about the methods and field associated with the stream_buf structure. Reproducing this bug requires very special circumstances. The reproduction = is openvpn --cert server-secp384r1.crt --key server-secp384r1.key \ --dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology subn= et \ --port 2201 --verb 3 --mode server --tls-server --proto tcp6-server --ifconfig 10.0.0.1 255.255.255.0 as the server side and openvpn --client --proto tcp --remote poseidon --port 2201 --dev tap The client side must be on Linux. Other platform do not reproduce this bug. Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic. Now forcefully reset the TCP connection. E.g. by executing on the server sudo ss --kill sport 2201 This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up. As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run. But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the link_socket_read_tcp will run into the assert. Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc --- M src/openvpn/socket.c M src/openvpn/socket.h 2 files changed, 84 insertions(+), 40 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/3 diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index f8c0b5c..c6d5ee9 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -44,9 +44,7 @@ bool sockets_read_residual(const struct context *c) { - int i; - - for (i =3D 0; i < c->c1.link_sockets_num; i++) + for (int i =3D 0; i < c->c1.link_sockets_num; i++) { if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) { @@ -2065,13 +2063,19 @@ * stream connection. */ +/** + * resets the stream buffer to be setup for the next round of + * reassembling a packet + * + * But still leaves the current packet in sb->buf to be potentially + * read. + */ static inline void stream_buf_reset(struct stream_buf *sb) { dmsg(D_STREAM_DEBUG, "STREAM: RESET"); sb->residual_fully_formed =3D false; sb->buf =3D sb->buf_init; - buf_reset(&sb->next); sb->len =3D -1; } @@ -2093,19 +2097,35 @@ dmsg(D_STREAM_DEBUG, "STREAM: INIT maxlen=3D%d", sb->maxlen); } -static inline void -stream_buf_set_next(struct stream_buf *sb) +/** + * Return a buffer that is backed by the same backend as sb->buf that + * determines where the next read should be done by also having the + * right offset into sb->buf + * @param sb the stream buffer from which to construct the next buffer + */ +static inline struct buffer +stream_buf_get_next(struct stream_buf *sb) { /* set up 'next' for next i/o read */ - sb->next =3D sb->buf; - sb->next.offset =3D sb->buf.offset + sb->buf.len; - sb->next.len =3D (sb->len >=3D 0 ? sb->len : sb->maxlen) - sb->buf.len; - dmsg(D_STREAM_DEBUG, "STREAM: SET NEXT, buf=3D[%d,%d] next=3D[%d,%d] l= en=3D%d maxlen=3D%d", - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->l= en, sb->maxlen); - ASSERT(sb->next.len > 0); - ASSERT(buf_safe(&sb->buf, sb->next.len)); + struct buffer next; + next =3D sb->buf; + next.offset =3D sb->buf.offset + sb->buf.len; + next.len =3D (sb->len >=3D 0 ? sb->len : sb->maxlen) - sb->buf.len; + dmsg(D_STREAM_DEBUG, "STREAM: GET NEXT, buf=3D[%d,%d] next=3D[%d,%d] l= en=3D%d maxlen=3D%d", + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->= maxlen); + ASSERT(next.len > 0); + ASSERT(buf_safe(&sb->buf, next.len)); + return next; } +/** + * Sets the parameter buf to the current buffer of sb->buf. + * This function assumes that caller already checked if the packet in sb->= buf + * is fully assembled. + * + * @param sb stream buffer to operate on + * @param buf buffer to point to the contents of buf + */ static inline void stream_buf_get_final(struct stream_buf *sb, struct buffer *buf) { @@ -2114,33 +2134,36 @@ *buf =3D sb->buf; } -static inline void -stream_buf_get_next(struct stream_buf *sb, struct buffer *buf) -{ - dmsg(D_STREAM_DEBUG, "STREAM: GET NEXT len=3D%d", buf_defined(&sb->nex= t) ? sb->next.len : -1); - ASSERT(buf_defined(&sb->next)); - *buf =3D sb->next; -} - bool stream_buf_read_setup_dowork(struct stream_buf *sb) { if (sb->residual.len && !sb->residual_fully_formed) { - ASSERT(buf_copy(sb->buf, &sock->stream_buf.residual)); + ASSERT(buf_copy(&sb->buf, &sb->residual)); ASSERT(buf_init(&sb->residual, 0)); sb->residual_fully_formed =3D stream_buf_added(sb, 0); dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=3D%d= ", - stream_buf->residual_fully_formed ? "YES" : "NO", sb->residua= l.len); + sb->residual_fully_formed ? "YES" : "NO", sb->residual.len); } - if (!sb->residual_fully_formed) - { - stream_buf_set_next(sb); - } return !sb->residual_fully_formed; } +/** + * This will determine if sb->buf contains a full packet. It will also + * move anything in sb->buf beyond a full beyond packet to sb->residual + * + * If this is the first time the function is called with a valid buffer + * and port sharing is enabled the function will also determine if this + * is OpenVPN protocol or not and store the result in sb->port_share_state. + * + * If a packet is outside of the allowed range is detected, the error state + * on sb is set. + * + * @param sb the stream buffer + * @param length_added The length that has been added to sb->buf + * @return true if sb->buf contains fully reassembled packet + */ static bool stream_buf_added(struct stream_buf *sb, int length_added) { @@ -2203,7 +2226,6 @@ else { dmsg(D_STREAM_DEBUG, "STREAM: ADD returned FALSE (have=3D%d need= =3D%d)", sb->buf.len, sb->len); - stream_buf_set_next(sb); return false; } } @@ -2273,8 +2295,7 @@ sockethandle_t sh =3D { .s =3D sock->sd }; len =3D sockethandle_finalize(sh, &sock->reads, buf, NULL); #else - struct buffer frag; - stream_buf_get_next(&sock->stream_buf, &frag); + struct buffer frag =3D stream_buf_get_next(&sock->stream_buf); len =3D recv(sock->sd, BPTR(&frag), BLEN(&frag), MSG_NOSIGNAL); #endif @@ -2550,7 +2571,7 @@ } else if (proto_is_tcp(sock->info.proto)) { - stream_buf_get_next(&sock->stream_buf, &sock->reads.buf); + sock->reads.buf =3D stream_buf_get_next(&sock->stream_buf); } else { diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index 8664666f..f2bf906 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -95,23 +95,41 @@ int mtu_changed; /* Set to true when mtu value is changed */ }; -/* - * Used to extract packets encapsulated in streams into a buffer, - * in this case IP packets embedded in a TCP stream. +/** + * struct used to extract packets encapsulated in streams into a buffer, + * in this case OpenVPN packets (data or control) embedded in a TCP stream. + * + * This struct is used to packetise the TCP stream into the + * OpenVPN packet. Each OpenVPN packet has a two-byte header determining + * the length of the packet. */ struct stream_buf { + /* Buffer to hold the initial buffer that will be used to reset buf */ struct buffer buf_init; + + /** buffer holding the excess bytes that are not part of the + * packet. */ struct buffer residual; + + /** Maximum length of a packet that we accept */ int maxlen; + + /** The buffer in buf contains a full packet without a header. Any + * extra data is in residual */ bool residual_fully_formed; + /** Holds the data of the current packet. This might be a partial pack= et */ struct buffer buf; - struct buffer next; - int len; /* -1 if not yet known */ - bool error; /* if true, fatal TCP error has occurred, - * requiring that connection be restarted */ + /** -1 if not yet known. Otherwise holds the length of the + * packet. If >=3D 0, buf is already moved past the initial + * size header */ + int len; + + /** if true, a fatal TCP error has occurred, + * requiring that connection be restarted */ + bool error; #if PORT_SHARE #define PS_DISABLED 0 #define PS_ENABLED 1 @@ -529,9 +547,14 @@ } /** + * Will try to check if the buffers in stream form a + * full packet. Will return true if further reads are + * required and other false. (full packet is ready) * - * @param sock - * @return + * With UDP we always return true as there is no reassembly. + * + * @param sb the stream buffer that should be worked on + * @return true if more reads are required. */ bool stream_buf_read_setup_dowork(struct stream_buf *sb); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?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: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc Gerrit-Change-Number: 1477 Gerrit-PatchSet: 3 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --vPHObYDq6TA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

plaisthos uploaded patch set #3 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/1477?usp=3Demail">View Chan= ge

Merge stream_buf_get_next and stream_buf_set_next
The stream_buf_set_next prepares a buffer in the stream_buf
struct= ure that will be retrieved by stream_buf_get the next
time it is used.
This temporary copy of the buffer is unnecessary as the buffer
nex= t can also be constructed on the fly.

This also fixes a rare crash w= hen read buffer are not initialised and
read is still signalled as the i= nitialisation of next will now happen whenever
it is required.

Th= is assertion happens when we do not expect a read event from the socket and=
then in link_socket_read_tcp the function stream_buf_get_next can trigg= er
an assert on ASSERT(buf_defined(&sb->next));

To avoid t= his weird corner case, just always initialise the read buffer
whether or= not we expect a read to occur.

This also adds documentation about t= he methods and field associated with
the stream_buf structure.

Re= producing this bug requires very special circumstances. The reproduction is=
openvpn --cert server-secp384r1.crt --key server-secp384r1.key \ --dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology sub= net \
--port 2201 --verb 3 --mode server --tls-server --proto tcp6-= server
--ifconfig 10.0.0.1 255.255.255.0

as the server side an= d

openvpn --client --proto tcp --remote poseidon --port 2201 --dev= tap

The client side must be on Linux. Other platform do not reprodu= ce this
bug.

Note that the client will not configure any IP or IP= v6 on the interface
and will also not bring up the interface. The server= must also send at least
one real data packet to the client (no keepaliv= e ping). Just having the
interface up normally produces enough traffic.<= br>
Now forcefully reset the TCP connection. E.g. by executing on the se= rver

sudo ss --kill sport 2201

This will now trigger the = assertion. This happens since OpenVPN waits
forever to get a write back = from the poll from the tun/tap device but
this never happens since the d= evice is not up.

As long as we do not get back the tun device for wr= iting, we also do
not put the socket back into the EVENT_READ state. And= this also means
that code to initialise the read buffer (stream_buf_set= _next) is never
run.

But the reset on the TCP socket triggers the= TCP socket to be available
for read, even if it is just for a read of 0= bytes to indicate the reset.
So the link_socket_read_tcp will run into = the assert.

Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
= ---
M src/openvpn/socket.c
M src/openvpn/socket.h
2 files changed,= 84 insertions(+), 40 deletions(-)

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/3
diff --=
git a/src/openvpn/socket.c b/src/openvpn/socket.c
index f8c= 0b5c..c6d5ee9 100644
--- a/src/openvpn/socket.c
<= span>+++ b/src/openvpn/socket.c
@@ -44,9 +44,7 @@ bool
sockets_read_residual(const struct context *c= )
{
- = int i;
-
<= span style=3D"color: hsl(0, 100%, 40%);">- for (i =3D 0; i < c->c1= .link_sockets_num; i++)
+ for (int i =3D 0; i < c->c1.link_sockets_num; i++)
= {
if (c->c2.link_sockets[i]->stre= am_buf.residual_fully_formed)
{
@@= -2065,13 +2063,19 @@
* stream connection.
*/

+/**
+ * resets t= he stream buffer to be setup for the next round of
+ * reassembling a packet
+ *
+ * But still leaves the current packet in sb->buf to = be potentially
+ * re= ad.
+ */
static inline void
stream_buf_reset(struct stream_buf= *sb)
{
dmsg(D_STREAM_DEBUG, "ST= REAM: RESET");
sb->residual_fully_formed =3D f= alse;
sb->buf =3D sb->buf_init;
- buf_reset(&sb->next);
sb->len =3D -1;
}

@@ -2093,19 +2097,35 @@
dmsg(D_STREAM_DEB= UG, "STREAM: INIT maxlen=3D%d", sb->maxlen);
= }

-st= atic inline void
-strea= m_buf_set_next(struct stream_buf *sb)
+/**
+ * Return a buffer that is backed by the same backend as sb->buf that<= /span>
+ * determines where = the next read should be done by also having the
+ * right offset into sb->buf
+ * @param sb the stream buffer from= which to construct the next buffer

+ */
+= static inline struct buffer
+stream_buf_get_next(struct stream_buf *sb)
{
/* set up 'next' for next i/o read */
- sb->next =3D sb->buf;
- sb->next.offset = =3D sb->buf.offset + sb->buf.len;
- sb->next.len =3D (sb->len >=3D 0 ? sb->le= n : sb->maxlen) - sb->buf.len;
- dmsg(D_STREAM_DEBUG, "STREAM: SET NEXT, buf=3D[%d,%= d] next=3D[%d,%d] len=3D%d maxlen=3D%d",
- sb->buf.offset, sb->buf.len, sb->= next.offset, sb->next.len, sb->len, sb->maxlen);
- ASSERT(sb->next.len > 0);
- ASSERT(buf_safe(&= sb->buf, sb->next.len));
+ struct buffer next;
+ next =3D sb->buf;
+ next.offset =3D sb->buf.offset + sb->buf.len;=
+ next.len =3D (s= b->len >=3D 0 ? sb->len : sb->maxlen) - sb->buf.len;<= br>+ dmsg(D_STREAM_DEBUG, &q= uot;STREAM: GET NEXT, buf=3D[%d,%d] next=3D[%d,%d] len=3D%d maxlen=3D%d&quo= t;,
+ sb->= buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxle= n);
+ ASSERT(next.= len > 0);
+ ASS= ERT(buf_safe(&sb->buf, next.len));
+ return next;
}
=
+/**
+ * Sets the parameter buf to the c= urrent buffer of sb->buf.

+ * This function assumes that caller already checked if the packet= in sb->buf
+ * is= fully assembled.
+ *=
+ * @param sb str= eam buffer to operate on
+ * @param buf buffer to point to the contents of buf
+ */

static inline = void
stream_buf_get_final(struct stream_buf *sb, struct bu= ffer *buf)
{
@@ -2114,33 +2134,36 @@
*buf =3D sb->buf;
}

-static inline void

-stream_buf_get_next(struct= stream_buf *sb, struct buffer *buf)
-{
- d= msg(D_STREAM_DEBUG, "STREAM: GET NEXT len=3D%d", buf_defined(&= ;sb->next) ? sb->next.len : -1);
- ASSERT(buf_defined(&sb->next));
- *buf =3D sb->next;
<= span style=3D"color: hsl(0, 100%, 40%);">-}
-
bool
stream_buf_re= ad_setup_dowork(struct stream_buf *sb)
{
= if (sb->residual.len && !sb->residual_fully_formed)

{
- = ASSERT(buf_copy(sb->buf, &sock->stream_buf.residual));=
+ ASSERT(buf_copy(&a= mp;sb->buf, &sb->residual));
ASSERT(buf_= init(&sb->residual, 0));
sb->residual_fu= lly_formed =3D stream_buf_added(sb, 0);
dmsg(D_STR= EAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=3D%d",=
- stream_buf->= residual_fully_formed ? "YES" : "NO", sb->residual.l= en);
+ sb= ->residual_fully_formed ? "YES" : "NO", sb->resid= ual.len);
}

- if (!sb->residual_fully_formed)<= br>- {
- stream_buf_set_next(sb);
= - }
retur= n !sb->residual_fully_formed;
}

+/**
+ * This will determine if sb->buf cont= ains a full packet. It will also
+ * move anything in sb->buf beyond a full beyond packet to = sb->residual
+ *
+ * If this is the fir= st time the function is called with a valid buffer
+ * and port sharing is enabled the function = will also determine if this
+ * is OpenVPN protocol or not and store the result in sb->port_s= hare_state.
+ *
+ * If a packet is outside= of the allowed range is detected, the error state
+ * on sb is set.
+ *
+ * @param sb the stream buffer
+ * @param length_added The length that has been added= to sb->buf
+ * @r= eturn true if sb->buf contains fully reassembled packet
+ */
static bool
stream_buf_added(struct stream_buf *sb, int length_added)
{
@@ -2203,7 +2226,6 @@
= else
{
dmsg(D_STREAM_DEBUG, &= quot;STREAM: ADD returned FALSE (have=3D%d need=3D%d)", sb->buf.len= , sb->len);
- = stream_buf_set_next(sb);
return false;
= }
}
@@ -2273,8 +2295,7 @@
sockethandle_t sh =3D { .s =3D sock->sd }; len =3D sockethandle_finalize(sh, &sock->reads, buf,= NULL);
#else
- struct buffer frag;
- stream_buf_get_next(&sock->stream_buf, &f= rag);
+ struct= buffer frag =3D stream_buf_get_next(&sock->stream_buf);
<= span> len =3D recv(sock->sd, BPTR(&frag), BLEN(&frag), M= SG_NOSIGNAL);

#endif

@@ = -2550,7 +2571,7 @@
}
else= if (proto_is_tcp(sock->info.proto))
{- stream_buf_get_nex= t(&sock->stream_buf, &sock->reads.buf);
+ sock->reads.buf =3D stream= _buf_get_next(&sock->stream_buf);
}<= br> else
{
diff --gi= t a/src/openvpn/socket.h b/src/openvpn/socket.h
index 86646= 66f..f2bf906 100644
--- a/src/openvpn/socket.h
+++ b/src/openvpn/socket.h

@@ -95,23 +95,41 @@ int mtu_changed; /* Set to true when mtu value is changed */
};

-/*
- * U= sed to extract packets encapsulated in streams into a buffer,
- * in this case IP packets embedded= in a TCP stream.

+/*= *
+ * struct used to = extract packets encapsulated in streams into a buffer,
+ * in this case OpenVPN packets (data or= control) embedded in a TCP stream.
+ *
+ = * This struct is used to packetise the TCP stream into the
+ * OpenVPN packet. Each OpenVPN pack= et has a two-byte header determining
+ * the length of the packet.
*/ struct stream_buf
{
+ /* Buffer to hold the initial buffer that wil= l be used to reset buf */
struct buffer buf_init;
+
+ /** buffer holding the excess bytes t= hat are not part of the
+ * packet. */
struct buffer residual;<= br>+
+ /** Maximum length of a packet that we acc= ept */
int maxlen;
+
= + /** The buffer in buf contains a full packet without a header. Any
+ * extra data is in= residual */
bool residual_fully_formed;

+ /** Holds t= he data of the current packet. This might be a partial packet */
= struct buffer buf;
- struct buffer next;
- int len; /* -1 if not yet known */

- bool error; /* if true,= fatal TCP error has occurred,
- * requiring that connection be restarted */
+ /** -1 if not yet kn= own. Otherwise holds the length of the
+ * packet. If >=3D 0, buf is already moved past= the initial
+ * = size header */
+ = int len;
+
= + /** if true, a fatal TCP e= rror has occurred,
+ = * requiring that connection be restarted */
+ bool error;
#if PORT_SHAR= E
#define PS_DISABLED 0
#define PS_ENABLE= D 1
@@ -529,9 +547,14 @@
}

/**
+ * Will try to check if the buffers in stream form a
+ * full packet. Will return true if f= urther reads are
+ * = required and other false. (full packet is ready)
*=
- * @param sock
- * @return

+ * With UDP we always return true as there i= s no reassembly.
+ *<= /span>
+ * @param sb the = stream buffer that should be worked on
+ * @return true if more reads are required.<= br> */
bool stream_buf_read_setup_dowork(struct str= eam_buf *sb);


To view= , visit = change 1477. To unsubscribe, or for help writing mail filters, visit settings.

<= div itemscope itemtype=3D"http://schema.org/EmailMessage">
<= meta itemprop=3D"name" content=3D"View Change"/>
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e1798= 65e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --vPHObYDq6TA=--