crypto: assert that qcrypto_hash_digest_len is in range
PATCH 1/1 crypto: assert that qcrypto_hash_digest_len is in range
Otherwise unintended results could happen. For example, Coverity reports a division by zero in qcrypto_afsplit_hash. While this cannot really happen, it shows that the contract of qcrypto_hash_digest_len can be improved.
--- crypto/hash.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
crypto/hash.c +1 -3
@@ -36,9 +36,7 @@ static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = {size_t qcrypto_hash_digest_len (QCryptoHashAlgorithm alg) { -if (alg >=G_N_ELEMENTS (qcrypto_hash_alg_size)) { -return 0 ; - } +assert (alg <G_N_ELEMENTS (qcrypto_hash_alg_size));return qcrypto_hash_alg_size[alg]; }
Signature · 1 line
2.5.5
Quoted 9 lines · Paolo Bonzini
Otherwise unintended results could happen. For example, Coverity reports a division by zero in
qcrypto_afsplit_hash. While this cannot really happen, it shows that the contract ofqcrypto_hash_digest_lencan be improved.Signed-off-by Paolo Bonzini <[email protected]>--- crypto/hash.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 6 lines
crypto/hash.c +0
-0
crypto/hash.c static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = { · line 40
Quoted 8 lines
crypto/hash.c +1
-3@@ -36,9 +36,7 @@ static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = {size_t qcrypto_hash_digest_len (QCryptoHashAlgorithm alg) { -if (alg >=G_N_ELEMENTS (qcrypto_hash_alg_size)) { -return 0 ; - } +assert (alg <G_N_ELEMENTS (qcrypto_hash_alg_size));return qcrypto_hash_alg_size[alg];
The assertion doesn't protect us if QCryptoHashAlgorithm gains another member but we forget to update qcrypto_hash_alg_size[] to match. Do you want an additional assertion that you are returning a non-zero value?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 6 lines · Eric Blake
Quoted 5 lines · Paolo Bonzini
Otherwise unintended results could happen. For example, Coverity reports a division by zero in
qcrypto_afsplit_hash. While this cannot really happen, it shows that the contract ofqcrypto_hash_digest_lencan be improved.
Quoted 6 lines
Quoted 4 lines
Signed-off-by Paolo Bonzini <[email protected]>--- crypto/hash.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)Reviewed-by Eric Blake <[email protected]>
Thanks, will queue this in my crypto-next branch
Quoted 6 lines
Quoted 6 lines
crypto/hash.c +0
-0@@ -36,9 +36,7 @@ static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = {
Quoted 11 lines
crypto/hash.c static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = { · line 40
Quoted 7 lines
crypto/hash.c +1
-3size_t qcrypto_hash_digest_len (QCryptoHashAlgorithm alg) { -if (alg >=G_N_ELEMENTS (qcrypto_hash_alg_size)) { -return 0 ; - } +assert (alg <G_N_ELEMENTS (qcrypto_hash_alg_size));return qcrypto_hash_alg_size[alg];The assertion doesn't protect us if QCryptoHashAlgorithm gains another member but we forget to update qcrypto_hash_alg_size[] to match. Do you want an additional assertion that you are returning a non-zero value?
It will gain more entries with a patch I have pending, but at the same time the impl of the hash methods will change, so this code won't exist in its current form. So I'll just queue this patch as is.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|