From: Eric Biggers <[email protected]>

It was forgotten to increase DH_KPP_SECRET_MIN_SIZE to include 'q_size',
causing an out-of-bounds write of 4 bytes in crypto_dh_encode_key(), and
an out-of-bounds read of 4 bytes in crypto_dh_decode_key().  Fix it.
Also add a BUG_ON() if crypto_dh_encode_key() doesn't exactly fill the
buffer, as that would have found this bug without resorting to KASAN.

Reported-by: [email protected]
Fixes: e3fe0ae12962 ("crypto: dh - add public key verification test")
Signed-off-by: Eric Biggers <[email protected]>
---
 crypto/dh_helper.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/crypto/dh_helper.c b/crypto/dh_helper.c
index a7de3d9ce5ace..87ad6e2e87644 100644
--- a/crypto/dh_helper.c
+++ b/crypto/dh_helper.c
@@ -14,7 +14,7 @@
 #include <crypto/dh.h>
 #include <crypto/kpp.h>
 
-#define DH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 3 * sizeof(int))
+#define DH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 4 * sizeof(int))
 
 static inline u8 *dh_pack_data(void *dst, const void *src, size_t size)
 {
@@ -61,7 +61,8 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const 
struct dh *params)
        ptr = dh_pack_data(ptr, params->key, params->key_size);
        ptr = dh_pack_data(ptr, params->p, params->p_size);
        ptr = dh_pack_data(ptr, params->q, params->q_size);
-       dh_pack_data(ptr, params->g, params->g_size);
+       ptr = dh_pack_data(ptr, params->g, params->g_size);
+       BUG_ON(ptr != (u8 *)buf + len);
 
        return 0;
 }
-- 
2.18.0

Reply via email to