Blame libkcapi-1.1.1-Coverity_PR_follow-up.patch

e55cfcd
From f24f3435be39cab2aa54a49d31968a023ab6d1d5 Mon Sep 17 00:00:00 2001
e55cfcd
From: Ondrej Mosnacek <omosnace@redhat.com>
e55cfcd
Date: Thu, 26 Jul 2018 14:09:27 +0200
e55cfcd
Subject: [PATCH 1/3] kcapi-kdf: Clear the whole out buffer on error
e55cfcd
e55cfcd
The KDF functions were decrementing the output length variable in the
e55cfcd
loop, but on error they would clear the output buffer based on this
e55cfcd
decremented value. This patch backs up the original length and uses it
e55cfcd
when clearing the output buffer.
e55cfcd
e55cfcd
The kcapi_pbkdf() function also used an incremented output buffer
e55cfcd
pointer. This one is now also backed-up and the original value is used
e55cfcd
when clearing the output.
e55cfcd
e55cfcd
Signed-off-by: Stephan Mueller <smueller@chronox.de>
e55cfcd
---
e55cfcd
 lib/kcapi-kdf.c | 16 +++++++++++-----
e55cfcd
 1 file changed, 11 insertions(+), 5 deletions(-)
e55cfcd
e55cfcd
diff --git a/lib/kcapi-kdf.c b/lib/kcapi-kdf.c
e55cfcd
index 78a7e0d..6eccbe1 100644
e55cfcd
--- a/lib/kcapi-kdf.c
e55cfcd
+++ b/lib/kcapi-kdf.c
e55cfcd
@@ -99,6 +99,7 @@ int32_t kcapi_kdf_dpi(struct kcapi_handle *handle,
e55cfcd
 	uint32_t h = kcapi_md_digestsize(handle);
e55cfcd
 	int32_t err = 0;
e55cfcd
 	uint8_t *dst_orig = dst;
e55cfcd
+	uint32_t dlen_orig = dlen;
e55cfcd
 	uint8_t Ai[h];
e55cfcd
 	uint32_t i = 1;
e55cfcd
 
e55cfcd
@@ -161,7 +162,7 @@ int32_t kcapi_kdf_dpi(struct kcapi_handle *handle,
e55cfcd
 	return 0;
e55cfcd
 
e55cfcd
 err:
e55cfcd
-	kcapi_memset_secure(dst_orig, 0, dlen);
e55cfcd
+	kcapi_memset_secure(dst_orig, 0, dlen_orig);
e55cfcd
 	kcapi_memset_secure(Ai, 0, h);
e55cfcd
 	return err;
e55cfcd
 }
e55cfcd
@@ -174,6 +175,7 @@ int32_t kcapi_kdf_fb(struct kcapi_handle *handle,
e55cfcd
 	uint32_t h = kcapi_md_digestsize(handle);
e55cfcd
 	int32_t err = 0;
e55cfcd
 	uint8_t *dst_orig = dst;
e55cfcd
+	uint32_t dlen_orig = dlen;
e55cfcd
 	const uint8_t *label;
e55cfcd
 	uint32_t labellen = 0;
e55cfcd
 	uint32_t i = 1;
e55cfcd
@@ -238,7 +240,7 @@ int32_t kcapi_kdf_fb(struct kcapi_handle *handle,
e55cfcd
 	return 0;
e55cfcd
 
e55cfcd
 err:
e55cfcd
-	kcapi_memset_secure(dst_orig, 0, dlen);
e55cfcd
+	kcapi_memset_secure(dst_orig, 0, dlen_orig);
e55cfcd
 	return err;
e55cfcd
 }
e55cfcd
 
e55cfcd
@@ -250,6 +252,7 @@ int32_t kcapi_kdf_ctr(struct kcapi_handle *handle,
e55cfcd
 	uint32_t h = kcapi_md_digestsize(handle);
e55cfcd
 	int32_t err = 0;
e55cfcd
 	uint8_t *dst_orig = dst;
e55cfcd
+	uint32_t dlen_orig = dlen;
e55cfcd
 	uint32_t i = 1;
e55cfcd
 
e55cfcd
 	if (dlen > INT_MAX)
e55cfcd
@@ -295,7 +298,7 @@ int32_t kcapi_kdf_ctr(struct kcapi_handle *handle,
e55cfcd
 	return 0;
e55cfcd
 
e55cfcd
 err:
e55cfcd
-	kcapi_memset_secure(dst_orig, 0, dlen);
e55cfcd
+	kcapi_memset_secure(dst_orig, 0, dlen_orig);
e55cfcd
 	return err;
e55cfcd
 }
e55cfcd
 
e55cfcd
@@ -316,6 +319,7 @@ int32_t kcapi_hkdf(const char *hashname,
e55cfcd
 	uint8_t *prev = NULL;
e55cfcd
 	int32_t err = 0;
e55cfcd
 	uint8_t *dst_orig = dst;
e55cfcd
+	uint32_t dlen_orig = dlen;
e55cfcd
 	uint8_t ctr = 0x01;
e55cfcd
 	struct kcapi_handle *handle = NULL;
e55cfcd
 
e55cfcd
@@ -415,7 +419,7 @@ int32_t kcapi_hkdf(const char *hashname,
e55cfcd
 	goto out;
e55cfcd
 
e55cfcd
 err:
e55cfcd
-	kcapi_memset_secure(dst_orig, 0, dlen);
e55cfcd
+	kcapi_memset_secure(dst_orig, 0, dlen_orig);
e55cfcd
 out:
e55cfcd
 	kcapi_memset_secure(prk_tmp, 0, h);
e55cfcd
 	kcapi_md_destroy(handle);
e55cfcd
@@ -552,6 +556,8 @@ int32_t kcapi_pbkdf(const char *hashname,
e55cfcd
 		    uint8_t *key, uint32_t keylen)
e55cfcd
 {
e55cfcd
 	struct kcapi_handle *handle;
e55cfcd
+	uint8_t *key_orig = key;
e55cfcd
+	uint32_t keylen_orig = keylen;
e55cfcd
 	uint32_t h, i = 1;
e55cfcd
 #define MAX_DIGESTSIZE 64
e55cfcd
 	uint8_t u[MAX_DIGESTSIZE] __attribute__ ((aligned (sizeof(uint64_t))));
e55cfcd
@@ -633,7 +639,7 @@ int32_t kcapi_pbkdf(const char *hashname,
e55cfcd
 err:
e55cfcd
 	kcapi_memset_secure(u, 0, h);
e55cfcd
 	if (err)
e55cfcd
-		kcapi_memset_secure(key, 0, keylen);
e55cfcd
+		kcapi_memset_secure(key_orig, 0, keylen_orig);
e55cfcd
 	kcapi_md_destroy(handle);
e55cfcd
 
e55cfcd
 	return err;
e55cfcd
e55cfcd
From eacb82b193a94d46d2ea70c621176d79a5486008 Mon Sep 17 00:00:00 2001
e55cfcd
From: Ondrej Mosnacek <omosnace@redhat.com>
e55cfcd
Date: Thu, 26 Jul 2018 14:12:51 +0200
e55cfcd
Subject: [PATCH 2/3] kcapi-kdf: Simplify handling of final blocks
e55cfcd
e55cfcd
This patch avoids the use of temporary buffers when handling the last
e55cfcd
block in the KDF functions, taking advantage of the fact that
e55cfcd
kcapi_md_final() can be used to retrieve also a truncated hash directly.
e55cfcd
e55cfcd
The new code no longer produces a false-positive warning with CLang
e55cfcd
static analysis, so the workaround (which Coverity identifies as
e55cfcd
unreachable code) can be removed.
e55cfcd
e55cfcd
Signed-off-by: Stephan Mueller <smueller@chronox.de>
e55cfcd
---
e55cfcd
 lib/kcapi-kdf.c | 43 +++++++++----------------------------------
e55cfcd
 1 file changed, 9 insertions(+), 34 deletions(-)
e55cfcd
e55cfcd
diff --git a/lib/kcapi-kdf.c b/lib/kcapi-kdf.c
e55cfcd
index 6eccbe1..afa6eb3 100644
e55cfcd
--- a/lib/kcapi-kdf.c
e55cfcd
+++ b/lib/kcapi-kdf.c
e55cfcd
@@ -140,13 +140,9 @@ int32_t kcapi_kdf_dpi(struct kcapi_handle *handle,
e55cfcd
 		}
e55cfcd
 
e55cfcd
 		if (dlen < h) {
e55cfcd
-			uint8_t tmpbuffer[h];
e55cfcd
-
e55cfcd
-			err = kcapi_md_final(handle, tmpbuffer, h);
e55cfcd
+			err = kcapi_md_final(handle, dst, dlen);
e55cfcd
 			if (err < 0)
e55cfcd
 				goto err;
e55cfcd
-			memcpy(dst, tmpbuffer, dlen);
e55cfcd
-			kcapi_memset_secure(tmpbuffer, 0, h);
e55cfcd
 			dlen = 0;
e55cfcd
 		} else {
e55cfcd
 			err = kcapi_md_final(handle, dst, h);
e55cfcd
@@ -219,14 +215,10 @@ int32_t kcapi_kdf_fb(struct kcapi_handle *handle,
e55cfcd
 		}
e55cfcd
 
e55cfcd
 		if (dlen < h) {
e55cfcd
-			uint8_t tmpbuffer[h];
e55cfcd
-
e55cfcd
-			err = kcapi_md_final(handle, tmpbuffer, h);
e55cfcd
+			err = kcapi_md_final(handle, dst, dlen);
e55cfcd
 			if (err < 0)
e55cfcd
 				goto err;
e55cfcd
-			memcpy(dst, tmpbuffer, dlen);
e55cfcd
-			kcapi_memset_secure(tmpbuffer, 0, h);
e55cfcd
-			return 0;
e55cfcd
+			dlen = 0;
e55cfcd
 		} else {
e55cfcd
 			err = kcapi_md_final(handle, dst, h);
e55cfcd
 			if (err < 0)
e55cfcd
@@ -276,14 +268,10 @@ int32_t kcapi_kdf_ctr(struct kcapi_handle *handle,
e55cfcd
 		}
e55cfcd
 
e55cfcd
 		if (dlen < h) {
e55cfcd
-			uint8_t tmpbuffer[h];
e55cfcd
-
e55cfcd
-			err = kcapi_md_final(handle, tmpbuffer, h);
e55cfcd
+			err = kcapi_md_final(handle, dst, dlen);
e55cfcd
 			if (err < 0)
e55cfcd
 				goto err;
e55cfcd
-			memcpy(dst, tmpbuffer, dlen);
e55cfcd
-			kcapi_memset_secure(tmpbuffer, 0, h);
e55cfcd
-			return 0;
e55cfcd
+			dlen = 0;
e55cfcd
 		} else {
e55cfcd
 			err = kcapi_md_final(handle, dst, h);
e55cfcd
 			if (err < 0)
e55cfcd
@@ -392,16 +380,10 @@ int32_t kcapi_hkdf(const char *hashname,
e55cfcd
 			goto err;
e55cfcd
 
e55cfcd
 		if (dlen < h) {
e55cfcd
-			err = kcapi_md_final(handle, prk_tmp, h);
e55cfcd
+			err = kcapi_md_final(handle, dst, dlen);
e55cfcd
 			if (err < 0)
e55cfcd
 				goto err;
e55cfcd
 
e55cfcd
-			/* Shut up Clang */
e55cfcd
-			if (!dst) {
e55cfcd
-				err = -EFAULT;
e55cfcd
-				goto err;
e55cfcd
-			}
e55cfcd
-			memcpy(dst, prk_tmp, dlen);
e55cfcd
 			dlen = 0;
e55cfcd
 		} else {
e55cfcd
 			err = kcapi_md_final(handle, dst, h);
e55cfcd
@@ -561,8 +543,6 @@ int32_t kcapi_pbkdf(const char *hashname,
e55cfcd
 	uint32_t h, i = 1;
e55cfcd
 #define MAX_DIGESTSIZE 64
e55cfcd
 	uint8_t u[MAX_DIGESTSIZE] __attribute__ ((aligned (sizeof(uint64_t))));
e55cfcd
-	uint8_t T[MAX_DIGESTSIZE] __attribute__ ((aligned (sizeof(uint64_t)))) =
e55cfcd
-									{ 0 };
e55cfcd
 	int32_t err = 0;
e55cfcd
 
e55cfcd
 	if (keylen > INT_MAX)
e55cfcd
@@ -617,17 +597,12 @@ int32_t kcapi_pbkdf(const char *hashname,
e55cfcd
 			if (err < 0)
e55cfcd
 				goto err;
e55cfcd
 
e55cfcd
-			if (keylen < h)
e55cfcd
-				kcapi_xor_64_aligned(T, u, h);
e55cfcd
-			else
e55cfcd
-				kcapi_xor_64(key, u, h);
e55cfcd
+			kcapi_xor_64(key, u, keylen < h ? keylen : h);
e55cfcd
 		}
e55cfcd
 
e55cfcd
-		if (keylen < h) {
e55cfcd
-			memcpy(key, T, keylen);
e55cfcd
-			kcapi_memset_secure(T, 0, keylen);
e55cfcd
+		if (keylen < h)
e55cfcd
 			keylen = 0;
e55cfcd
-		} else {
e55cfcd
+		else {
e55cfcd
 			keylen -= h;
e55cfcd
 			key += h;
e55cfcd
 			i++;
e55cfcd
e55cfcd
From c9ed6b2c07026e9bafd99e6c288cfbd175fd237f Mon Sep 17 00:00:00 2001
e55cfcd
From: Ondrej Mosnacek <omosnace@redhat.com>
e55cfcd
Date: Thu, 26 Jul 2018 14:28:53 +0200
e55cfcd
Subject: [PATCH 3/3] kcapi-kdf: Fix unused function warning on 32-bit
e55cfcd
e55cfcd
The kcapi_xor_64_aligned() is now unused when compiling in 32-bit mode,
e55cfcd
so we need to define it only in the 64-bit case, otherwise the build
e55cfcd
fails under CLang due to an usnused function warning.
e55cfcd
e55cfcd
Signed-off-by: Stephan Mueller <smueller@chronox.de>
e55cfcd
---
e55cfcd
 lib/kcapi-kdf.c | 6 ++----
e55cfcd
 1 file changed, 2 insertions(+), 4 deletions(-)
e55cfcd
e55cfcd
diff --git a/lib/kcapi-kdf.c b/lib/kcapi-kdf.c
e55cfcd
index afa6eb3..a219d63 100644
e55cfcd
--- a/lib/kcapi-kdf.c
e55cfcd
+++ b/lib/kcapi-kdf.c
e55cfcd
@@ -503,10 +503,10 @@ static inline void kcapi_xor_32(uint8_t *dst, const uint8_t *src, uint32_t size)
e55cfcd
 		kcapi_xor_8(dst, src, size);
e55cfcd
 }
e55cfcd
 
e55cfcd
+#ifdef __LP64__
e55cfcd
 static inline void kcapi_xor_64_aligned(uint8_t *dst, const uint8_t *src,
e55cfcd
 				        uint32_t size)
e55cfcd
 {
e55cfcd
-#ifdef __LP64__
e55cfcd
 	uint64_t *dst_dword = (uint64_t *)dst;
e55cfcd
 	uint64_t *src_dword = (uint64_t *)src;
e55cfcd
 
e55cfcd
@@ -514,10 +514,8 @@ static inline void kcapi_xor_64_aligned(uint8_t *dst, const uint8_t *src,
e55cfcd
 		*dst_dword++ ^= *src_dword++;
e55cfcd
 
e55cfcd
 	kcapi_xor_32_aligned((uint8_t *)dst_dword, (uint8_t *)src_dword, size);
e55cfcd
-#else
e55cfcd
-	kcapi_xor_32_aligned(dst, src, size);
e55cfcd
-#endif
e55cfcd
 }
e55cfcd
+#endif
e55cfcd
 
e55cfcd
 static inline void kcapi_xor_64(uint8_t *dst, const uint8_t *src, uint32_t size)
e55cfcd
 {