The default static array initializer of 0 is not suitable for the glib hash/hmac backends, as G_CHECKSUM_MD5 has a value of 0. Instead elements must be initialized to -1, but remembering todo that as new algorithms are defined is errorprone. The SM3 algorithm initializer is missing, leading to it being mapped to MD5. This could potentially be a security flaw through use of the wrong (much weaker) hashing algorithm, if a further bug elsewhere didn't mask the issue.
Set a default array initializer so unlisted algorithms are always set to -1 instead of 0. Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/4786 Signed-off-by: Daniel P. Berrangé <[email protected]> --- crypto/hash-glib.c | 3 +-- crypto/hmac-glib.c | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/crypto/hash-glib.c b/crypto/hash-glib.c index 809cef98aee..d915430f459 100644 --- a/crypto/hash-glib.c +++ b/crypto/hash-glib.c @@ -26,13 +26,12 @@ static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { + [0 ... QCRYPTO_HASH_ALGO__MAX - 1] = -1, [QCRYPTO_HASH_ALGO_MD5] = G_CHECKSUM_MD5, [QCRYPTO_HASH_ALGO_SHA1] = G_CHECKSUM_SHA1, - [QCRYPTO_HASH_ALGO_SHA224] = -1, [QCRYPTO_HASH_ALGO_SHA256] = G_CHECKSUM_SHA256, [QCRYPTO_HASH_ALGO_SHA384] = G_CHECKSUM_SHA384, [QCRYPTO_HASH_ALGO_SHA512] = G_CHECKSUM_SHA512, - [QCRYPTO_HASH_ALGO_RIPEMD160] = -1, }; gboolean qcrypto_hash_supports(QCryptoHashAlgo alg) diff --git a/crypto/hmac-glib.c b/crypto/hmac-glib.c index 1f17769c1c7..4453e0d5f06 100644 --- a/crypto/hmac-glib.c +++ b/crypto/hmac-glib.c @@ -18,13 +18,11 @@ #include "hmacpriv.h" static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] = { + [0 ... QCRYPTO_HASH_ALGO__MAX - 1] = -1, [QCRYPTO_HASH_ALGO_MD5] = G_CHECKSUM_MD5, [QCRYPTO_HASH_ALGO_SHA1] = G_CHECKSUM_SHA1, [QCRYPTO_HASH_ALGO_SHA256] = G_CHECKSUM_SHA256, [QCRYPTO_HASH_ALGO_SHA512] = G_CHECKSUM_SHA512, - [QCRYPTO_HASH_ALGO_SHA224] = -1, - [QCRYPTO_HASH_ALGO_SHA384] = -1, - [QCRYPTO_HASH_ALGO_RIPEMD160] = -1, }; typedef struct QCryptoHmacGlib QCryptoHmacGlib; -- 2.56.0
