Blame SOURCES/gnutls-3.6.14-fix-iovec-memory-leak.patch

cc169b
From 6fbff7fc8aabeee2254405f254220bbe8c05c67d Mon Sep 17 00:00:00 2001
cc169b
From: Daiki Ueno <ueno@gnu.org>
cc169b
Date: Fri, 5 Jun 2020 16:26:33 +0200
cc169b
Subject: [PATCH] crypto-api: always allocate memory when serializing iovec_t
cc169b
cc169b
The AEAD iov interface falls back to serializing the input buffers if
cc169b
the low-level cipher doesn't support scatter/gather encryption.
cc169b
However, there was a bug in the functions used for the serialization,
cc169b
which causes memory leaks under a certain condition (i.e. the number
cc169b
of input buffers is 1).
cc169b
cc169b
This patch makes the logic of the functions simpler, by removing a
cc169b
micro-optimization that tries to minimize the number of calls to
cc169b
malloc/free.
cc169b
cc169b
The original problem was reported by Marius Steffen in:
cc169b
https://bugzilla.samba.org/show_bug.cgi?id=14399
cc169b
and the cause was investigated by Alexander Haase in:
cc169b
https://gitlab.com/gnutls/gnutls/-/merge_requests/1277
cc169b
cc169b
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cc169b
---
cc169b
 lib/crypto-api.c        | 36 +++++++++++-------------------------
cc169b
 tests/aead-cipher-vec.c | 33 ++++++++++++++++++---------------
cc169b
 2 files changed, 29 insertions(+), 40 deletions(-)
cc169b
cc169b
diff --git a/lib/crypto-api.c b/lib/crypto-api.c
cc169b
index 45be64ed1..8524f5ed4 100644
cc169b
--- a/lib/crypto-api.c
cc169b
+++ b/lib/crypto-api.c
cc169b
@@ -891,32 +891,23 @@ gnutls_aead_cipher_encrypt(gnutls_aead_cipher_hd_t handle,
cc169b
 struct iov_store_st {
cc169b
 	void *data;
cc169b
 	size_t size;
cc169b
-	unsigned allocated;
cc169b
 };
cc169b
 
cc169b
 static void iov_store_free(struct iov_store_st *s)
cc169b
 {
cc169b
-	if (s->allocated) {
cc169b
-		gnutls_free(s->data);
cc169b
-		s->allocated = 0;
cc169b
-	}
cc169b
+	gnutls_free(s->data);
cc169b
 }
cc169b
 
cc169b
 static int iov_store_grow(struct iov_store_st *s, size_t length)
cc169b
 {
cc169b
-	if (s->allocated || s->data == NULL) {
cc169b
-		s->size += length;
cc169b
-		s->data = gnutls_realloc(s->data, s->size);
cc169b
-		if (s->data == NULL)
cc169b
-			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
cc169b
-		s->allocated = 1;
cc169b
-	} else {
cc169b
-		void *data = s->data;
cc169b
-		size_t size = s->size + length;
cc169b
-		s->data = gnutls_malloc(size);
cc169b
-		memcpy(s->data, data, s->size);
cc169b
-		s->size += length;
cc169b
-	}
cc169b
+	void *data;
cc169b
+
cc169b
+	s->size += length;
cc169b
+	data = gnutls_realloc(s->data, s->size);
cc169b
+	if (data == NULL)
cc169b
+		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
cc169b
+
cc169b
+	s->data = data;
cc169b
 	return 0;
cc169b
 }
cc169b
 
cc169b
@@ -926,11 +917,6 @@ copy_from_iov(struct iov_store_st *dst, const giovec_t *iov, int iovcnt)
cc169b
 	memset(dst, 0, sizeof(*dst));
cc169b
 	if (iovcnt == 0) {
cc169b
 		return 0;
cc169b
-	} else if (iovcnt == 1) {
cc169b
-		dst->data = iov[0].iov_base;
cc169b
-		dst->size = iov[0].iov_len;
cc169b
-		/* implies: dst->allocated = 0; */
cc169b
-		return 0;
cc169b
 	} else {
cc169b
 		int i;
cc169b
 		uint8_t *p;
cc169b
@@ -944,11 +930,11 @@ copy_from_iov(struct iov_store_st *dst, const giovec_t *iov, int iovcnt)
cc169b
 
cc169b
 		p = dst->data;
cc169b
 		for (i=0;i
cc169b
-			memcpy(p, iov[i].iov_base, iov[i].iov_len);
cc169b
+			if (iov[i].iov_len > 0)
cc169b
+				memcpy(p, iov[i].iov_base, iov[i].iov_len);
cc169b
 			p += iov[i].iov_len;
cc169b
 		}
cc169b
 
cc169b
-		dst->allocated = 1;
cc169b
 		return 0;
cc169b
 	}
cc169b
 }
cc169b
diff --git a/tests/aead-cipher-vec.c b/tests/aead-cipher-vec.c
cc169b
index fba9010d9..6a30a35f7 100644
cc169b
--- a/tests/aead-cipher-vec.c
cc169b
+++ b/tests/aead-cipher-vec.c
cc169b
@@ -49,6 +49,7 @@ static void start(const char *name, int algo)
cc169b
 	giovec_t auth_iov[2];
cc169b
 	uint8_t tag[64];
cc169b
 	size_t tag_size = 0;
cc169b
+	size_t i;
cc169b
 
cc169b
 	key.data = key16;
cc169b
 	key.size = gnutls_cipher_get_key_size(algo);
cc169b
@@ -82,21 +83,23 @@ static void start(const char *name, int algo)
cc169b
 	if (ret < 0)
cc169b
 		fail("gnutls_cipher_init: %s\n", gnutls_strerror(ret));
cc169b
 
cc169b
-	ret = gnutls_aead_cipher_encryptv2(ch,
cc169b
-					   iv.data, iv.size,
cc169b
-					   auth_iov, 2,
cc169b
-					   iov, 3,
cc169b
-					   tag, &tag_size);
cc169b
-	if (ret < 0)
cc169b
-		fail("could not encrypt data: %s\n", gnutls_strerror(ret));
cc169b
-
cc169b
-	ret = gnutls_aead_cipher_decryptv2(ch,
cc169b
-					   iv.data, iv.size,
cc169b
-					   auth_iov, 2,
cc169b
-					   iov, 3,
cc169b
-					   tag, tag_size);
cc169b
-	if (ret < 0)
cc169b
-		fail("could not decrypt data: %s\n", gnutls_strerror(ret));
cc169b
+	for (i = 0; i < 2; i++) {
cc169b
+		ret = gnutls_aead_cipher_encryptv2(ch,
cc169b
+						   iv.data, iv.size,
cc169b
+						   auth_iov, 2,
cc169b
+						   iov, i + 1,
cc169b
+						   tag, &tag_size);
cc169b
+		if (ret < 0)
cc169b
+			fail("could not encrypt data: %s\n", gnutls_strerror(ret));
cc169b
+
cc169b
+		ret = gnutls_aead_cipher_decryptv2(ch,
cc169b
+						   iv.data, iv.size,
cc169b
+						   auth_iov, 2,
cc169b
+						   iov, i + 1,
cc169b
+						   tag, tag_size);
cc169b
+		if (ret < 0)
cc169b
+			fail("could not decrypt data: %s\n", gnutls_strerror(ret));
cc169b
+	}
cc169b
 
cc169b
 	gnutls_aead_cipher_deinit(ch);
cc169b
 }
cc169b
-- 
cc169b
2.25.4
cc169b