Skip to contentSkip to conversation

crypto_openssl.c: fix heap-buffer-overflow found by AddressSanitizer

PATCH Superseded Lev Stipakov 3 participants 10 messages Jan 22, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 crypto_openssl.c: fix heap-buffer-overflow found by AddressSanitizer

From: Lev Stipakov <[email protected]...>

OpenSSL's version of crypto_pem_encode() uses PEM_write_bio() function to write PEM-encoded data to BIO object. That method doesn't add NUL termanator, unlike its mbedTLS counterpart mbedtls_pem_write_buffer().

The code which uses PEM data treats it as a string, so missing NUL terminator makes sanitizer to compain.

Fix by adding a NUL terminator.

---
 src/openvpn/crypto_openssl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/openvpn/crypto_openssl.c +2 -1
index 9691ce0..6a49067 100644
@@ -400,8 +400,9 @@ crypto_pem_encode(const char *name, struct buffer *dst,
     BUF_MEM *bptr;
     BIO_get_mem_ptr(bio, &bptr);
 
-    *dst = alloc_buf_gc(bptr->length, gc);
+    *dst = alloc_buf_gc(bptr->length + 1, gc);
     ASSERT(buf_write(dst, bptr->data, bptr->length));
+    *BEND(dst) = '\0';
 
     ret = true;
 cleanup:
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 test_tls_crypt.c: fix global-buffer-overflow found by AddressSanitizer

From: Lev Stipakov <[email protected]...>

When writing data to buffer we incorrectly specify source length
- sizeof for pointer returns 8, but actual buffer length is 1.

Fix by replacing empty global string to local string literal and specifying the correct length.

---
 tests/unit_tests/openvpn/test_tls_crypt.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
tests/unit_tests/openvpn/test_tls_crypt.c +1 -3
index b793a7a..b4ce03d 100644
@@ -49,8 +49,6 @@
 #define PARAM1      "param1"
 #define PARAM2      "param two"
 
-static const char *plaintext_short = "";
-
 static const char *test_server_key = \
         "-----BEGIN OpenVPN tls-crypt-v2 server key-----\n"
         "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
@@ -148,7 +146,7 @@ test_tls_crypt_setup(void **state) {
     ctx->unwrapped = alloc_buf(TESTBUF_SIZE);
 
     /* Write test plaintext */
-    buf_write(&ctx->source, plaintext_short, sizeof(plaintext_short));
+    buf_write(&ctx->source, "1234567890", 10);
 
     /* Write dummy opcode and session id */
     buf_write(&ctx->ciphertext, "012345678", 1 + 8);
Signature · 1 line
2.7.4
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/2] test_tls_crypt.c: fix global-buffer-overflow found by AddressSanitizer

Am 22.01.19 um 12:02 schrieb Lev Stipakov:

Quoted 28 lines

From: Lev Stipakov <[email protected]...>

When writing data to buffer we incorrectly specify source length
- sizeof for pointer returns 8, but actual buffer length is 1.

Fix by replacing empty global string to local string literal and specifying the correct length.

---
 tests/unit_tests/openvpn/test_tls_crypt.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
tests/unit_tests/openvpn/test_tls_crypt.c +0 -2
index b793a7a..b4ce03d 100644
@@ -49,8 +49,6 @@
 #define PARAM1      "param1"
 #define PARAM2      "param two"
 
-static const char *plaintext_short = "";
-
 static const char *test_server_key = \
         "-----BEGIN OpenVPN tls-crypt-v2 server key-----\n"
         "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"

tests/unit_tests/openvpn/test_tls_crypt.c test_tls_crypt_setup(void **state) { · line 152

Quoted 8 lines
tests/unit_tests/openvpn/test_tls_crypt.c +1 -1
@@ -148,7 +146,7 @@ test_tls_crypt_setup(void **state) {
     ctx->unwrapped = alloc_buf(TESTBUF_SIZE);
 
     /* Write test plaintext */
-    buf_write(&ctx->source, plaintext_short, sizeof(plaintext_short));
+    buf_write(&ctx->source, "1234567890", 10);
 
     /* Write dummy opcode and session id */
     buf_write(&ctx->ciphertext, "012345678", 1 + 8);

I would suggest plaintext_short="12345678"; and then use strlen(plaintext_short)+1 in the buf_write.

But I can confirm that this indeed fixes the issue it should fix.

Arne

#3 lore raw

PATCH v2 1/1 test_tls_crypt.c: fix global-buffer-overflow found by AddressSanitizer

From: Lev Stipakov <[email protected]...>

When writing data to buffer we incorrectly specify source length
- sizeof for pointer returns 8, but actual buffer length is 1.

Fix by replacing empty global string to local string literal and specifying the correct length.

---
v2: use strlen(), fix misleading comments

 tests/unit_tests/openvpn/test_tls_crypt.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tests/unit_tests/openvpn/test_tls_crypt.c +5 -5
index b793a7a..17f7d89 100644
@@ -49,8 +49,6 @@
 #define PARAM1      "param1"
 #define PARAM2      "param two"
 
-static const char *plaintext_short = "";
-
 static const char *test_server_key = \
         "-----BEGIN OpenVPN tls-crypt-v2 server key-----\n"
         "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
@@ -148,10 +146,12 @@ test_tls_crypt_setup(void **state) {
     ctx->unwrapped = alloc_buf(TESTBUF_SIZE);
 
     /* Write test plaintext */
-    buf_write(&ctx->source, plaintext_short, sizeof(plaintext_short));
+    const char *plaintext = "1234567890";
+    buf_write(&ctx->source, plaintext, strlen(plaintext));
 
-    /* Write dummy opcode and session id */
-    buf_write(&ctx->ciphertext, "012345678", 1 + 8);
+    /* Write test ciphertext */
+    const char *ciphertext = "012345678";
+    buf_write(&ctx->ciphertext, ciphertext, strlen(ciphertext));
 
     return 0;
 }
Signature · 1 line
2.7.4
Acked-by on v2 1/1
#4 lore raw

Re: [Openvpn-devel] [PATCH v2] test_tls_crypt.c: fix global-buffer-overflow found by AddressSanitizer

Am 22.01.19 um 14:34 schrieb Lev Stipakov:

Quoted 7 lines

From: Lev Stipakov <[email protected]...>

When writing data to buffer we incorrectly specify source length
- sizeof for pointer returns 8, but actual buffer length is 1.

Fix by replacing empty global string to local string literal and specifying the correct length.

Acked-by Arne Schwabe

Arne

#5 lore raw

[Openvpn-devel] [PATCH applied] Re: test_tls_crypt.c: fix global-buffer-overflow found by AddressSanitizer

In reply to Lev Stipakov

Your patch has been applied to the master branch.

commit a3fd78d48616ab21908b116d5ce785986893e02d

Author: Lev Stipakov
Date:   Tue Jan 22 15:34:20 2019 +0200
     test_tls_crypt.c: fix global-buffer-overflow found by AddressSanitizer

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg18140.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#6 lore raw
In reply to Lev Stipakov

Am 22.01.19 um 12:02 schrieb Lev Stipakov:

Quoted 21 lines

From: Lev Stipakov <[email protected]...>

OpenSSL's version of crypto_pem_encode() uses PEM_write_bio() function to write PEM-encoded data to BIO object. That method doesn't add NUL termanator, unlike its mbedTLS counterpart mbedtls_pem_write_buffer().

The code which uses PEM data treats it as a string, so missing NUL terminator makes sanitizer to compain.

Fix by adding a NUL terminator.

---
 src/openvpn/crypto_openssl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/openvpn/crypto_openssl.c +0 -0
index 9691ce0..6a49067 100644

src/openvpn/crypto_openssl.c crypto_pem_encode(const char *name, struct buffer *dst, · line 405

Quoted 7 lines
src/openvpn/crypto_openssl.c +2 -1
@@ -400,8 +400,9 @@ crypto_pem_encode(const char *name, struct buffer *dst,
     BUF_MEM *bptr;
     BIO_get_mem_ptr(bio, &bptr);
 
-    *dst = alloc_buf_gc(bptr->length, gc);
+    *dst = alloc_buf_gc(bptr->length + 1, gc);
     ASSERT(buf_write(dst, bptr->data, bptr->length));
+    *BEND(dst) = '\0';

buf_null_terminate(dst) is a better function here :)

Otherwise ACK, fixes the problem.

Arne

#7 lore raw

PATCH v2 1/1 crypto_openssl.c: fix heap-buffer-overflow found by AddressSanitizer

From: Lev Stipakov <[email protected]...>

OpenSSL's version of crypto_pem_encode() uses PEM_write_bio() function to write PEM-encoded data to BIO object. That method doesn't add NUL termanator, unlike its mbedTLS counterpart mbedtls_pem_write_buffer().

The code which uses PEM data treats it as a string, so missing NUL terminator makes sanitizer to compain.

Fix by adding a NUL terminator.

---
v2: use a dedivcated function to add a nul terminator

 src/openvpn/crypto_openssl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/openvpn/crypto_openssl.c +2 -1
index 9691ce0..c049e52 100644
@@ -400,8 +400,9 @@ crypto_pem_encode(const char *name, struct buffer *dst,
     BUF_MEM *bptr;
     BIO_get_mem_ptr(bio, &bptr);
 
-    *dst = alloc_buf_gc(bptr->length, gc);
+    *dst = alloc_buf_gc(bptr->length + 1, gc);
     ASSERT(buf_write(dst, bptr->data, bptr->length));
+    buf_null_terminate(dst);
 
     ret = true;
 cleanup:
Signature · 1 line
2.7.4
Acked-by on v2 1/1
#8 lore raw

Am 22.01.19 um 14:41 schrieb Lev Stipakov:

Quoted 6 lines

From: Lev Stipakov <[email protected]...>

OpenSSL's version of crypto_pem_encode() uses PEM_write_bio() function to write PEM-encoded data to BIO object. That method doesn't add NUL termanator, unlike its mbedTLS counterpart mbedtls_pem_write_buffer().

Quoted 8 lines

The code which uses PEM data treats it as a string, so missing NUL terminator makes sanitizer to compain.

Fix by adding a NUL terminator.

---
v2: use a dedivcated function to add a nul terminator

                 ^^^ :)
Acked-by Arne Schwabe

Arne

#9 lore raw
In reply to Lev Stipakov

Your patch has been applied to the master branch.

commit eb1fed3f3bb817332183672dd1ca665ece83d6a8

Author: Lev Stipakov
Date:   Tue Jan 22 15:41:03 2019 +0200
     crypto_openssl.c: fix heap-buffer-overflow found by AddressSanitizer

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg18141.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering