From: "cron2 (Code Review)" Date: Wed, 8 Oct 2025 10:44:04 +0000 In-Reply-To: References: Reply-To: gert@...1296..., 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="gq4xzZXLzxE="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: buffer: Fix buf_parse eating input List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos Cc: openvpn-devel --gq4xzZXLzxE= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#2) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1246?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2, Code-Review+2 by plaisthos Change subject: buffer: Fix buf_parse eating input ...................................................................... buffer: Fix buf_parse eating input When parsing a "line" that is longer than the available line buffer, then buf_parse was eating up to 2 characters. It advanced past them but they were not part of the output. This can lead to unexpected results if buf_parse is used in a while loop on unrestricted input, like e.g. when reading configs (see in_src_get() used for check_inline_file_via_buf()). Change-Id: I3724660bf0f8336ee58c172acfb7c4f38e457393 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Acked-by: Arne Schwabe Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1246 Message-Id: <20251008103001.7696-1-gert@...1296...> URL: https://sourceforge.net/p/openvpn/mailman/message/59243829/ Signed-off-by: Gert Doering --- M src/openvpn/buffer.c M src/openvpn/buffer.h M tests/unit_tests/openvpn/test_buffer.c 3 files changed, 73 insertions(+), 7 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/46/1246/2 diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index c0b85b2..28de00f 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -832,19 +832,24 @@ do { - c =3D buf_read_u8(buf); + c =3D buf_peek_u8(buf); if (c < 0) { eol =3D true; + line[n] =3D 0; + break; } - if (c <=3D 0 || c =3D=3D delim) + if (c =3D=3D delim) { - c =3D 0; + buf_advance(buf, 1); + line[n] =3D 0; + break; } - if (n >=3D size) + if (n >=3D (size - 1)) { break; } + buf_advance(buf, 1); line[n++] =3D (char)c; } while (c); diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 1405667..148cee0 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -771,7 +771,7 @@ } =20 static inline int -buf_read_u8(struct buffer *buf) +buf_peek_u8(struct buffer *buf) { int ret; if (BLEN(buf) < 1) @@ -779,7 +779,17 @@ return -1; } ret =3D *BPTR(buf); - buf_advance(buf, 1); + return ret; +} + +static inline int +buf_read_u8(struct buffer *buf) +{ + int ret =3D buf_peek_u8(buf); + if (ret >=3D 0) + { + buf_advance(buf, 1); + } return ret; } diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/open= vpn/test_buffer.c index c86c19e..92c4c65 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -450,6 +450,56 @@ gc_free(&gc); } +/* for building long texts */ +#define A_TIMES_256 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO" + +void +test_buffer_parse(void **state) +{ + struct gc_arena gc =3D gc_new(); + struct buffer buf =3D alloc_buf_gc(1024, &gc); + char line[512]; + bool status; + const char test1[] =3D A_TIMES_256 "EOL\n" A_TIMES_256 "EOF"; + + /* line buffer bigger than actual line */ + assert_int_equal(buf_write(&buf, test1, sizeof(test1)), true); + status =3D buf_parse(&buf, '\n', line, sizeof(line)); + assert_int_equal(status, true); + assert_string_equal(line, A_TIMES_256 "EOL"); + status =3D buf_parse(&buf, '\n', line, sizeof(line)); + assert_int_equal(status, true); + assert_string_equal(line, A_TIMES_256 "EOF"); + + /* line buffer exactly same size as actual line + terminating \0 */ + buf_reset_len(&buf); + assert_int_equal(buf_write(&buf, test1, sizeof(test1)), true); + status =3D buf_parse(&buf, '\n', line, 260); + assert_int_equal(status, true); + assert_string_equal(line, A_TIMES_256 "EOL"); + status =3D buf_parse(&buf, '\n', line, 260); + assert_int_equal(status, true); + assert_string_equal(line, A_TIMES_256 "EOF"); + + /* line buffer smaller than actual line */ + buf_reset_len(&buf); + assert_int_equal(buf_write(&buf, test1, sizeof(test1)), true); + status =3D buf_parse(&buf, '\n', line, 257); + assert_int_equal(status, true); + assert_string_equal(line, A_TIMES_256); + status =3D buf_parse(&buf, '\n', line, 257); + assert_int_equal(status, true); + assert_string_equal(line, "EOL"); + status =3D buf_parse(&buf, '\n', line, 257); + assert_int_equal(status, true); + assert_string_equal(line, A_TIMES_256); + status =3D buf_parse(&buf, '\n', line, 257); + assert_int_equal(status, true); + assert_string_equal(line, "EOF"); + + gc_free(&gc); +} + int main(void) { @@ -478,7 +528,8 @@ cmocka_unit_test(test_character_class), cmocka_unit_test(test_character_string_mod_buf), cmocka_unit_test(test_snprintf), - cmocka_unit_test(test_buffer_chomp) + cmocka_unit_test(test_buffer_chomp), + cmocka_unit_test(test_buffer_parse) }; return cmocka_run_group_tests_name("buffer", tests, NULL, NULL); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1246?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: I3724660bf0f8336ee58c172acfb7c4f38e457393 Gerrit-Change-Number: 1246 Gerrit-PatchSet: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --gq4xzZXLzxE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #2 to the change originally created by flichtenhel= d.

View Change

The following approvals got outdated and were remove= d: Code-Review+2 by cron2, Code-Review+2 by plaisthos

buffer:=
 Fix buf_parse eating input

When parsing a "line" that is = longer than the
available line buffer, then buf_parse was
eating up t= o 2 characters. It advanced past
them but they were not part of the outp= ut.

This can lead to unexpected results if buf_parse
is used in a= while loop on unrestricted input,
like e.g. when reading configs (see i= n_src_get()
used for check_inline_file_via_buf()).

Change-Id: I37= 24660bf0f8336ee58c172acfb7c4f38e457393
Signed-off-by: Frank Lichtenheld = <frank@...2641...>
Acked-by: Gert Doering <gert@...1296...><= br>Acked-by: Arne Schwabe <arne-openvpn@...1227...>
Gerrit URL: ht= tps://gerrit.openvpn.net/c/openvpn/+/1246
Message-Id: <20251008103001= .7696-1-gert@...1296...>
URL: https://sourceforge.net/p/openvpn/mailm= an/message/59243829/
Signed-off-by: Gert Doering <gert@...1296...>=
---
M src/openvpn/buffer.c
M src/openvpn/buffer.h
M tests/unit= _tests/openvpn/test_buffer.c
3 files changed, 73 insertions(+), 7 deleti= ons(-)

git pull ssh://gerrit.openvpn.net:29418/o=
penvpn refs/changes/46/1246/2
diff --git a/src/openvpn/buffer.c b/s=
rc/openvpn/buffer.c
index c0b85b2..28de00f 100644--- a/src/openvpn/buffer.c
+++ b/src/openvpn/buffer.= c
@@ -832,19 +832,24 @@

= do
{
- c =3D buf_read_u8(buf);
+ c =3D buf_peek_u8(buf);
= if (c < 0)
{
eol= =3D true;
+ = line[n] =3D 0;
+ = break;
}
- if (c <=3D 0 || c =3D=3D delim)
= + if (c =3D=3D delim)
{
+ buf_advance(buf, 1);
+ line[n] =3D 0;
+ break;
}
- if (n >=3D size)=
+ if (n >= =3D (size - 1))
{
bre= ak;
}
+ buf_advance(buf, 1);
line[n++] = =3D (char)c;
} while (c);

= diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h
index 1405667..148cee0 100644

--- a/src/openvpn/buffer.= h
+++ b/src/openvpn/buffer.h
@@ -771,7 +771= ,7 @@
}

static inline i= nt
-buf_read_u8(struct = buffer *buf)
+buf_pee= k_u8(struct buffer *buf)
{
int ret;
if (BLEN(buf) < 1)
@@ -779,7 +779,17= @@
return -1;
}
ret =3D *BPTR(buf);

- buf_advance(buf, 1);
+ return ret;
+}
++static inline int
<= span style=3D"color: hsl(120, 100%, 40%);">+buf_read_u8(struct buffer *buf)=

+{
+ int ret =3D buf_peek_u8(buf);
+ if (ret >=3D 0)<= /span>
+ {
+ buf_advance(buf, 1);
+ }
= return ret;
}

diff --g= it a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test= _buffer.c
index c86c19e..92c4c65 100644
---= a/tests/unit_tests/openvpn/test_buffer.c
+++ b/tests/unit_= tests/openvpn/test_buffer.c
@@ -450,6 +450,56 @@
= gc_free(&gc);
}
+/* for building long texts */=
+#define A_TIMES_256= "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO"
+
+void
+test_buff= er_parse(void **state)
+ struct gc_a= rena gc =3D gc_new();
+ struct buffer buf =3D alloc_buf_gc(1024, &gc);
+ char line[512];
+ bool status;
+ const char test1[] =3D A_TIMES_256 &q= uot;EOL\n" A_TIMES_256 "EOF";
+
+ /* line buffer bigger than actual line */
+ assert_int_equal(buf_write(&buf, = test1, sizeof(test1)), true);
+ status =3D buf_parse(&buf, '\n', line, sizeof(lin= e));
+ assert_int_= equal(status, true);
= + assert_string_equal(line, A_TIMES_256 "EOL");
+ status =3D buf_parse(&buf,= '\n', line, sizeof(line));

+ assert_int_equal(status, true);
+ assert_string_equal(line, A_TIMES_256= "EOF");
+<= /span>
+ /* line buffer e= xactly same size as actual line + terminating \0 */
+ buf_reset_len(&buf);
+ assert_int_equal(buf_write(&= amp;buf, test1, sizeof(test1)), true);

+ status =3D buf_parse(&buf, '\n', line, 2= 60);
+ assert_int_= equal(status, true);
= + assert_string_equal(line, A_TIMES_256 "EOL");
+ status =3D buf_parse(&buf,= '\n', line, 260);

+ assert_int_equal(status, true);
+ assert_string_equal(line, A_TIMES_256 "EOF&= quot;);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ /* line buffer smaller than= actual line */
+ = buf_reset_len(&buf);
+ assert_int_equal(buf_write(&buf, test1, sizeof(test1)), true);=
+ status =3D buf_= parse(&buf, '\n', line, 257);
+ assert_int_equal(status, true);
+ assert_string_equal(line, A_TIMES= _256);
+ status = =3D buf_parse(&buf, '\n', line, 257);
+ assert_int_equal(status, true);+ assert_string_equal(line,= "EOL");
+ = status =3D buf_parse(&buf, '\n', line, 257);
+ assert_int_equal(status, true);=
+ assert_string_e= qual(line, A_TIMES_256);
+ status =3D buf_parse(&buf, '\n', line, 257);+ assert_int_equal(status, = true);
+ assert_st= ring_equal(line, "EOF");
+
+ = gc_free(&gc);
+}<= /span>
+
in= t
main(void)
{
@@ -478,7 = +528,8 @@
cmocka_unit_test(test_character_class),<= /span>
cmocka_unit_test(test_character_string_mod_buf),
cmocka_unit_test(test_snprintf),
- cmocka_unit_test(test_buffer_cho= mp)
+ cmocka_u= nit_test(test_buffer_chomp),
+ cmocka_unit_test(test_buffer_parse)
}= ;

return cmocka_run_group_tests_name= ("buffer", tests, NULL, NULL);

<= p>To view, visit change 1246. To unsubscribe, or for help writing mail filters= , visit settings= .

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I3724660bf0f8336ee58c172acfb= 7c4f38e457393
Gerrit-Change-Number: 1246
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --gq4xzZXLzxE=--