From: "flichtenheld (Code Review)" Date: Wed, 1 Oct 2025 15:00:38 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <572c527fe5c7bc2d7d810f042f6adbae273b7eb2-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="iJ5+EBFa2O4="; 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: plaisthos Cc: openvpn-devel --iJ5+EBFa2O4= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1246?usp=3Demail to review the following change. 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 --- 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/1 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 @@ } 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: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I3724660bf0f8336ee58c172acfb7c4f38e457393 Gerrit-Change-Number: 1246 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --iJ5+EBFa2O4= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

buffer: Fix buf_parse eating input

W= hen parsing a "line" that is longer than the
available line bu= ffer, 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 unexpect= ed 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: I3724660bf0f8336ee58c172acfb7c4f38e4573= 93
Signed-off-by: Frank Lichtenheld <frank@...2641...>
---
M= src/openvpn/buffer.c
M src/openvpn/buffer.h
M tests/unit_tests/openv= pn/test_buffer.c
3 files changed, 73 insertions(+), 7 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/=
changes/46/1246/1
diff --git a/src/openvpn/buffer.c b/src/openvpn/b=
uffer.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;
+ b= reak;
}
- 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 14= 05667..148cee0 100644
--- a/src/openvpn/buffer.h
= +++ b/src/openvpn/buffer.h
@@ -771,7 +771,7 @@=
}

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);
- b= uf_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 r= et;
}

diff --git a/tests= /unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c<= /span>
index c86c19e..92c4c65 100644
--- a/tests/u= nit_tests/openvpn/test_buffer.c
+++ b/tests/unit_tests/open= vpn/test_buffer.c
@@ -450,6 +450,56 @@
= gc_free(&gc);
}

+/* for building long texts */+#define A_TIMES_256 "AAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= AAAAAAAAAAAAAAAAAAAAAAAAAAAO"
+
+void=
+test_buffer_parse(v= oid **state)
+{

+ struct gc_arena gc = =3D gc_new();
+ st= ruct 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, siz= eof(test1)), true);
+= status =3D buf_parse(&buf, '\n', line, sizeof(line));
+ assert_int_equal(stat= us, true);
+ asser= t_string_equal(line, A_TIMES_256 "EOL");
+ status =3D buf_parse(&buf, '\n&#= 39;, line, sizeof(line));
+ assert_int_equal(status, true);
+ assert_string_equal(line, A_TIMES_256 "EOF&q= uot;);
+
+ /* 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_st= ring_equal(line, A_TIMES_256 "EOL");
+ status =3D buf_parse(&buf, '\n',= line, 260);
+ ass= ert_int_equal(status, true);
+ assert_string_equal(line, A_TIMES_256 "EOF");=
+
+ /* line buffer smaller than actual line *= /
+ buf_reset_len(= &buf);
+ asser= t_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);<= br>+ status =3D buf_parse(&a= mp;buf, '\n', line, 257);
+ assert_int_equal(status, true);
+ assert_string_equal(line, "EOL"= );
+ status =3D bu= f_parse(&buf, '\n', line, 257);
+ assert_int_equal(status, true);
+ assert_string_equal(line, A_TIM= ES_256);
+ status = =3D buf_parse(&buf, '\n', line, 257);
+ assert_int_equal(status, true);+ assert_string_equal(line,= "EOF");
+<= /span>
+ 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_bu= ffer_chomp),
+ = cmocka_unit_test(test_buffer_parse)
};

return cmocka_run_group_tests_name("buffer&qu= ot;, tests, NULL, NULL);

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

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I3724660bf0f8336ee58c172acfb= 7c4f38e457393
Gerrit-Change-Number: 1246
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --iJ5+EBFa2O4=--