Skip to contentSkip to conversation

buffer: use memcpy in buf_catrunc

PATCH Superseded Frank Lichtenheld 4 participants 12 messages Mar 28, 2023 – May 17, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 buffer: use memcpy in buf_catrunc

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends
on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

---
 src/openvpn/buffer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpn/buffer.c +2 -2
index d099795b..de40d690 100644
@@ -317,9 +317,9 @@ buf_catrunc(struct buffer *buf, const char *str)
     if (buf_forward_capacity(buf) <= 1)
     {
         int len = (int) strlen(str) + 1;
-        if (len < buf_forward_capacity_total(buf))
+        if ((len > 0) && (len < buf_forward_capacity_total(buf)))
         {
-            strncpynt((char *)(buf->data + buf->capacity - len), str, len);
+            memcpy((void *)(buf->data + buf->capacity - len), (void *)str, (size_t)len);
         }
     }
 }
Signature · 1 line
2.34.1
#1 lore raw

Hi,

Quoted 25 lines · Frank Lichtenheld

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends
on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

---
  src/openvpn/buffer.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpn/buffer.c +0 -0
index d099795b..de40d690 100644

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 320

Quoted 5 lines
src/openvpn/buffer.c +1 -1
@@ -317,9 +317,9 @@ buf_catrunc(struct buffer *buf, const char *str)
      if (buf_forward_capacity(buf) <= 1)
      {
          int len = (int) strlen(str) + 1;
-        if (len < buf_forward_capacity_total(buf))
+        if ((len > 0) && (len < buf_forward_capacity_total(buf)))

len cannot be negative, but I guess you want to check that it is actually not 0. Just a style thing I guess? (Because memcpy can happily deal with len=0.)

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 322

Quoted 3 lines
src/openvpn/buffer.c +1 -1
          {
-            strncpynt((char *)(buf->data + buf->capacity - len), str, len);
+            memcpy((void *)(buf->data + buf->capacity - len), (void *)str, (size_t)len);

I'd throw away the casts to 'void *'.
They are not really needed as you can assign everything to 'void *'.

Cheers,

Quoted 3 lines
src/openvpn/buffer.c +0 -0
          }
      }
  }
Signature · 1 line
Antonio Quartulli
#2 lore raw
In reply to Frank Lichtenheld

Am 28.03.23 um 17:12 schrieb Frank Lichtenheld:

Quoted 25 lines

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends
on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

---
  src/openvpn/buffer.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpn/buffer.c +0 -0
index d099795b..de40d690 100644

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 320

Quoted 5 lines
src/openvpn/buffer.c +1 -1
@@ -317,9 +317,9 @@ buf_catrunc(struct buffer *buf, const char *str)
      if (buf_forward_capacity(buf) <= 1)
      {
          int len = (int) strlen(str) + 1;
-        if (len < buf_forward_capacity_total(buf))
+        if ((len > 0) && (len < buf_forward_capacity_total(buf)))

I hope you don't mind but there are a few things I would like to bring to attention.

1. The first is clamping and wrapping around:

strlen(someptr) returns size_t, and this can be >= INT_MAX. Add +1 and this wraps to INT_MIN.
While you now trap this with the new "len > 0", there does not seem to be any error handling, in that the function silently skips its actual function.
Is this what such a function should do? Or should it be "complain an give up"?

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 322

Quoted 3 lines
src/openvpn/buffer.c +1 -1
          {
-            strncpynt((char *)(buf->data + buf->capacity - len), str, len);
+            memcpy((void *)(buf->data + buf->capacity - len), (void *)str, (size_t)len);

2. This opens the door for another compiler warning, which is that you are casting away the const-ness of "str". It is const char * when passed in, but you cast it to void * (non-const). The 2nd argument of memcpy would need to be cast to (const void *) to avoid that. memcpy is declared (..., const void *restrict src, ...), so that's safe.

3. However, strncpynt would set buf->data[buf->capacity] = '\0', which is now missing.

So: NAK, please revise on #3.

Cheers,
Matthias

#3 lore raw

Hi,

Quoted 51 lines · Matthias Andree

Am 28.03.23 um 17:12 schrieb Frank Lichtenheld:

Quoted 25 lines

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

---
  src/openvpn/buffer.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpn/buffer.c +0 -0
index d099795b..de40d690 100644

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 320

Quoted 5 lines
src/openvpn/buffer.c +1 -1
@@ -317,9 +317,9 @@ buf_catrunc(struct buffer *buf, const char *str)
       if (buf_forward_capacity(buf) <= 1)
       {
           int len = (int) strlen(str) + 1;
-        if (len < buf_forward_capacity_total(buf))
+        if ((len > 0) && (len < buf_forward_capacity_total(buf)))

I hope you don't mind but there are a few things I would like to bring to attention.

1. The first is clamping and wrapping around:

strlen(someptr) returns size_t, and this can be >= INT_MAX. Add +1 and this wraps to INT_MIN.
While you now trap this with the new "len > 0", there does not seem to be any error handling, in that the function silently skips its actual function.
Is this what such a function should do? Or should it be "complain an give up"?

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 324

Quoted 5 lines
src/openvpn/buffer.c +1 -1
           {
-            strncpynt((char *)(buf->data + buf->capacity - len), str, 
 len);
+            memcpy((void *)(buf->data + buf->capacity - len), (void 
 *)str, (size_t)len);
Quoted 8 lines

2. This opens the door for another compiler warning, which is that you are casting away the const-ness of "str". It is const char * when passed in, but you cast it to void * (non-const). The 2nd argument of memcpy would need to be cast to (const void *) to avoid that. memcpy is declared (..., const void *restrict src, ...), so that's safe.

3. However, strncpynt would set buf->data[buf->capacity] = '\0', which is now missing.

I thin this is not needed in this case, because str [0..strlen(str) + 1] includes the \0 which will be copied by memcpy. No?

Quoted 12 lines

So: NAK, please revise on #3.

Cheers,
Matthias

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Signature · 1 line
Antonio Quartulli
#4 lore raw

Am 28.03.23 um 20:58 schrieb Antonio Quartulli:

Quoted 60 lines

Hi,

Quoted 57 lines · Matthias Andree

Am 28.03.23 um 17:12 schrieb Frank Lichtenheld:

Quoted 25 lines

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

---
  src/openvpn/buffer.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpn/buffer.c +0 -0
index d099795b..de40d690 100644

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 320

Quoted 5 lines
src/openvpn/buffer.c +1 -1
@@ -317,9 +317,9 @@ buf_catrunc(struct buffer *buf, const char *str)
       if (buf_forward_capacity(buf) <= 1)
       {
           int len = (int) strlen(str) + 1;
-        if (len < buf_forward_capacity_total(buf))
+        if ((len > 0) && (len < buf_forward_capacity_total(buf)))

I hope you don't mind but there are a few things I would like to bring to attention.

1. The first is clamping and wrapping around:

strlen(someptr) returns size_t, and this can be >= INT_MAX. Add +1 and this wraps to INT_MIN.
While you now trap this with the new "len > 0", there does not seem to be any error handling, in that the function silently skips its actual function.
Is this what such a function should do? Or should it be "complain an give up"?

src/openvpn/buffer.c buf_catrunc(struct buffer *buf, const char *str) · line 324

Quoted 5 lines
src/openvpn/buffer.c +1 -1
           {
-            strncpynt((char *)(buf->data + buf->capacity - len),
 str, len);
+            memcpy((void *)(buf->data + buf->capacity - len), (void
 *)str, (size_t)len);

2. This opens the door for another compiler warning, which is that you are casting away the const-ness of "str". It is const char * when passed in, but you cast it to void * (non-const). The 2nd argument of memcpy would need to be cast to (const void *) to avoid that. memcpy is declared (..., const void *restrict src, ...), so that's safe.

Quoted 5 lines
Quoted 2 lines

3. However, strncpynt would set buf->data[buf->capacity] = '\0', which is now missing.

I thin this is not needed in this case, because str [0..strlen(str) + 1] includes the \0 which will be copied by memcpy. No?

See my earlier consideration of the wrapping of the INT_MAX, when the copy comes out short. It's not making matters worse, but I'm not a friend of silent failure.

#5 lore raw

PATCH v2 1/2 buffer: use memcpy in buf_catrunc

In reply to Frank Lichtenheld

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends
on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

---
 src/openvpn/buffer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

v2:
- make len size_t and change code to avoid any theoretical overflows
- remove useless casts

src/openvpn/buffer.c +2 -2
index d099795b..886eb2c3 100644
@@ -316,10 +316,10 @@ buf_catrunc(struct buffer *buf, const char *str)
 {
     if (buf_forward_capacity(buf) <= 1)
     {
-        int len = (int) strlen(str) + 1;
+        size_t len = strlen(str);
         if (len < buf_forward_capacity_total(buf))
         {
-            strncpynt((char *)(buf->data + buf->capacity - len), str, len);
+            memcpy(buf->data + buf->capacity - len - 1, str, len + 1);
         }
     }
 }
Signature · 1 line
2.34.1
#6 lore raw

PATCH 2/2 test_buffer: add tests for buf_catrunc and its caller format_hex_ex

Just some very basic tests.

---
 tests/unit_tests/openvpn/test_buffer.c | 59 ++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
tests/unit_tests/openvpn/test_buffer.c +59 -0
index 9e3b1d2e..5e61fb07 100644
@@ -56,6 +56,63 @@ test_buffer_strprefix(void **state)
     assert_int_equal(BLEN(buf), strlen(str)); \
     assert_memory_equal(BPTR(buf), str, BLEN(buf));
 
+static void
+test_buffer_printf_catrunc(void **state)
+{
+    struct gc_arena gc = gc_new();
+    struct buffer buf = alloc_buf_gc(16, &gc);
+
+    buf_printf(&buf, "%d", 123);
+    buf_printf(&buf, "%s", "some text, too long to fit");
+    assert_buf_equals_str(&buf, "123some text, t");
+
+    buf_catrunc(&buf, "...");
+    assert_buf_equals_str(&buf, "123some text...");
+
+    buf_catrunc(&buf, "some other text, much too long to fit");
+    assert_buf_equals_str(&buf, "123some text...");
+
+    buf_catrunc(&buf, "something other");
+    assert_buf_equals_str(&buf, "something other");
+
+    gc_free(&gc);
+}
+
+static void
+test_buffer_format_hex_ex(void **state)
+{
+    const int input_size = 10;
+    const uint8_t input[] = {
+        0x01, 0x00, 0xff, 0x10, 0xff, 0x00, 0xf0, 0x0f, 0x09, 0x0a
+    };
+    char *output;
+    struct gc_arena gc = gc_new();
+
+    int maxoutput = 0;
+    unsigned int blocksize = 5;
+    char *separator = " ";
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100ff10ff 00f00f090a");
+
+    maxoutput = 14;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100[more...]");
+
+    maxoutput = 10;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "[more...]");
+
+    maxoutput = 9;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100ff10");
+
+    maxoutput = 8;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100ff1");
+
+    gc_free(&gc);
+}
+
 struct test_buffer_list_aggregate_ctx {
     struct buffer_list *empty;
     struct buffer_list *one_two_three;
@@ -267,6 +324,8 @@ main(void)
 {
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(test_buffer_strprefix),
+        cmocka_unit_test(test_buffer_printf_catrunc),
+        cmocka_unit_test(test_buffer_format_hex_ex),
         cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_empty,
                                         test_buffer_list_setup,
                                         test_buffer_list_teardown),
Signature · 1 line
2.34.1
#7 lore raw

PATCH v3 1/2 buffer: use memcpy in buf_catrunc

In reply to Frank Lichtenheld

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends
on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

v2:
- make len size_t and change code to avoid any theoretical overflows
- remove useless casts
v3:
- fix off-by-one introduced by v2 %)

Change-Id If4a67adac4d2e870fd719b58075d39efcd67c671
---
 src/openvpn/buffer.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/openvpn/buffer.c +3 -3
index d099795b..36b28afb 100644
@@ -316,10 +316,10 @@ buf_catrunc(struct buffer *buf, const char *str)
 {
     if (buf_forward_capacity(buf) <= 1)
     {
-        int len = (int) strlen(str) + 1;
-        if (len < buf_forward_capacity_total(buf))
+        size_t len = strlen(str);
+        if (buf_size_valid(len) && (len + 1 < buf_forward_capacity_total(buf)))
         {
-            strncpynt((char *)(buf->data + buf->capacity - len), str, len);
+            memcpy(buf->data + buf->capacity - len - 1, str, len + 1);
         }
     }
 }
Signature · 1 line
2.34.1
#8 lore raw

PATCH v2 2/2 test_buffer: add tests for buf_catrunc and its caller format_hex_ex

Just some very basic tests.

v2:
- fix off-by-one

Change-Id I73fc893136387d1da05f4aea98cb37b02d6c3230
---
 tests/unit_tests/openvpn/test_buffer.c | 62 ++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
tests/unit_tests/openvpn/test_buffer.c +62 -0
index 9e3b1d2e..bff50567 100644
@@ -56,6 +56,66 @@ test_buffer_strprefix(void **state)
     assert_int_equal(BLEN(buf), strlen(str)); \
     assert_memory_equal(BPTR(buf), str, BLEN(buf));
 
+static void
+test_buffer_printf_catrunc(void **state)
+{
+    struct gc_arena gc = gc_new();
+    struct buffer buf = alloc_buf_gc(16, &gc);
+
+    buf_printf(&buf, "%d", 123);
+    buf_printf(&buf, "%s", "some text, too long to fit");
+    assert_buf_equals_str(&buf, "123some text, t");
+
+    buf_catrunc(&buf, "...");
+    assert_buf_equals_str(&buf, "123some text...");
+
+    buf_catrunc(&buf, "some other text, much too long to fit");
+    assert_buf_equals_str(&buf, "123some text...");
+
+    buf_catrunc(&buf, "something else"); /* exactly right */
+    assert_buf_equals_str(&buf, "1something else");
+
+    buf_catrunc(&buf, "something other"); /* 1 byte too long */
+    assert_buf_equals_str(&buf, "1something else");
+
+    gc_free(&gc);
+}
+
+static void
+test_buffer_format_hex_ex(void **state)
+{
+    const int input_size = 10;
+    const uint8_t input[] = {
+        0x01, 0x00, 0xff, 0x10, 0xff, 0x00, 0xf0, 0x0f, 0x09, 0x0a
+    };
+    char *output;
+    struct gc_arena gc = gc_new();
+
+    int maxoutput = 0;
+    unsigned int blocksize = 5;
+    char *separator = " ";
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100ff10ff 00f00f090a");
+
+    maxoutput = 14;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100[more...]");
+
+    maxoutput = 11;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0[more...]");
+
+    maxoutput = 10;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100ff10f");
+
+    maxoutput = 9;
+    output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc);
+    assert_string_equal(output, "0100ff10");
+
+    gc_free(&gc);
+}
+
 struct test_buffer_list_aggregate_ctx {
     struct buffer_list *empty;
     struct buffer_list *one_two_three;
@@ -267,6 +327,8 @@ main(void)
 {
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(test_buffer_strprefix),
+        cmocka_unit_test(test_buffer_printf_catrunc),
+        cmocka_unit_test(test_buffer_format_hex_ex),
         cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_empty,
                                         test_buffer_list_setup,
                                         test_buffer_list_teardown),
Signature · 1 line
2.34.1
#10 lore raw

PATCH v4 1/2 buffer: use memcpy in buf_catrunc

In reply to Frank Lichtenheld

Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy.

But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs:

error: ‘strncpy’ specified bound depends
on the length of the source argument [-Werror=stringop-overflow=]

Warning was at least triggered for mingw-gcc version 10-win32 20220113.

Also change the type of len to size_t which avoids potential problems with signed overflow.

v2:
- make len size_t and change code to avoid any theoretical overflows
- remove useless casts
v3:
- fix off-by-one introduced by v2 %)
v4:
- ignore unsigned overflow to simplify code

Change-Id If4a67adac4d2e870fd719b58075d39efcd67c671
---
 src/openvpn/buffer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpn/buffer.c +2 -2
index d099795b..7725438e 100644
@@ -316,10 +316,10 @@ buf_catrunc(struct buffer *buf, const char *str)
 {
     if (buf_forward_capacity(buf) <= 1)
     {
-        int len = (int) strlen(str) + 1;
+        size_t len = strlen(str) + 1;
         if (len < buf_forward_capacity_total(buf))
         {
-            strncpynt((char *)(buf->data + buf->capacity - len), str, len);
+            memcpy(buf->data + buf->capacity - len, str, len);
         }
     }
 }
Signature · 1 line
2.34.1
Acked-by on v2 2/2
#9 lore raw

[Openvpn-devel] [PATCH applied] Re: test_buffer: add tests for buf_catrunc and its caller format_hex_ex

In reply to Frank Lichtenheld

Unit Tests are good, this function-under-test is a bit weird, and having the UTs in tree (and working) before the actual patch gives double assurance that it's still working in the same weird way :-)

[ RUN ] test_buffer_printf_catrunc
[ OK ] test_buffer_printf_catrunc
[ RUN ] test_buffer_format_hex_ex
[ OK ] test_buffer_format_hex_ex

Your patch has been applied to the master branch.

commit eecf9e9d70955018225e47d96d57da72511c48a0

Author: Frank Lichtenheld
Date:   Wed May 17 10:44:22 2023 +0200
     test_buffer: add tests for buf_catrunc and its caller format_hex_ex

     Signed-off-by: Frank Lichtenheld <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26680.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

Acked-by on v4 1/2
#11 lore raw
In reply to Frank Lichtenheld

Change looks good (less +/-1, so easier for my old brain), and our nice and shiny new unit test confirms that things still work.

Your patch has been applied to the master branch.

commit c89a97e449baaf60924a362555d35184f188a646

Author: Frank Lichtenheld
Date:   Wed May 17 13:36:19 2023 +0200
     buffer: use memcpy in buf_catrunc

     Signed-off-by: Frank Lichtenheld <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26683.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering