crypto: afalg: fix a NULL pointer dereference
PATCH 1/1 crypto: afalg: fix a NULL pointer dereference
Test-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if afalg_driver doesn't support requested algos:
ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov,
result, resultlen,
errp);
if (ret == 0) {
return ret;
}
error_free(*errp); // <--- hereSo we must check 'errp & *errp' before dereference.
--- crypto/hash.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/hash.c +3 -1
@@ -60,7 +60,9 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,* TODO: * Maybe we should treat some afalg errors as fatal */ -error_free (*errp); +if (errp && *errp) { +error_free (*errp); + }#endif return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov,
Signature · 1 line
1.8.3.1
Quoted 20 lines
-----Original Message-----
From: longpeng
Sent: Monday, November 06, 2017 2:21 PM
To: [email protected]; [email protected]; Gonglei (Arei)
Cc: longpeng; [email protected]
Subject: [PATCH] crypto: afalg: fix a NULL pointer dereferenceTest-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if
afalg_driverdoesn't support requested algos:ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); if (ret == 0) { return ret; } error_free(*errp); // <--- here
Quoted 4 lines
So we must check 'errp & *errp' before dereference.
Signed-off-by Longpeng(Mike) <[email protected]>---
Thanks,
-Gonglei
Quoted 21 lines
crypto/hash.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)crypto/hash.c +3
-2@@ -60,7 +60,9 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,* TODO: * Maybe we should treat some afalg errors as fatal */ -error_free (*errp); +if (errp && *errp) { +error_free (*errp); + }#endif return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov, --1.8.3.1
Quoted 2 lines · Longpeng(Mike)
Test-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if
afalg_driver
s/poniter deference/pointer dereference/
Quoted 11 lines
doesn't support requested algos:
ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); if (ret == 0) { return ret; } error_free(*errp); // <--- hereSo we must check 'errp & *errp' before dereference.
No, if we are going to blindly ignore the error from the hash_bytesv() call, then we should pass NULL rather than errp.
Quoted 11 lines
Signed-off-by Longpeng(Mike) <[email protected]>--- crypto/hash.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)crypto/hash.c +0
-0
crypto/hash.c int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, · line 65
Quoted 7 lines
crypto/hash.c +3
-1@@ -60,7 +60,9 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,* TODO: * Maybe we should treat some afalg errors as fatal */ -error_free (*errp); +if (errp && *errp) { +error_free (*errp); + }
Any code that uses 'if (errp && *errp)' is suspicious; I think you need a v2 that doesn't try to set errp in the first place, rather than cleaning it up to ignore it after the fact.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 6 lines · Eric Blake
Quoted 2 lines · Longpeng(Mike)
Test-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if
afalg_drivers/poniter deference/pointer dereference/
OK.
Quoted 8 lines
Quoted 8 lines
doesn't support requested algos:
ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); if (ret == 0) { return ret; }
Quoted 7 lines
Quoted 3 lines
error_free(*errp); // <--- hereSo we must check 'errp & *errp' before dereference.
No, if we are going to blindly ignore the error from the
hash_bytesv()call, then we should pass NULL rather than errp.
The 'errp' in this palce is convenient for debug, it can tell us the reason for failure without stepping into afalg_driver's hash_bytesv().
Quoted 6 lines
Quoted 6 lines
Signed-off-by Longpeng(Mike) <[email protected]>--- crypto/hash.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 14 lines
crypto/hash.c int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, · line 65
Quoted 12 lines
crypto/hash.c +3
-1@@ -60,7 +60,9 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,* TODO: * Maybe we should treat some afalg errors as fatal */ -error_free (*errp); +if (errp && *errp) { +error_free (*errp); + }Any code that uses 'if (errp && *errp)' is suspicious; I think you need
I see, thanks a lot.
Quoted 3 lines
a v2 that doesn't try to set errp in the first place, rather than cleaning it up to ignore it after the fact.
I answered above.
How about the following approach?
int qcrypto_hash_bytesv(...Error **errp)
{
#ifdef CONFIG_AF_ALG
int ret;
Error *err2 = NULL;
ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov,
result, resultlen,
&err2);
if (ret == 0) {
return ret;
}
error_free(*err2);#endif
return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov,
result, resultlen, errp);
}
I'll send a V2 if there's no objection, otherwise I'll pick your suggested approach. :)
Signature · 2 lines
Regards, Longpeng(Mike)
Quoted 23 lines · Longpeng (Mike)
Quoted 6 lines · Eric Blake
Quoted 2 lines · Longpeng(Mike)
Test-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if
afalg_drivers/poniter deference/pointer dereference/
OK.
Quoted 10 lines
Quoted 10 lines
doesn't support requested algos:
ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); if (ret == 0) { return ret; } error_free(*errp); // <--- here
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
So we must check 'errp & *errp' before dereference.
No, if we are going to blindly ignore the error from the
hash_bytesv()call, then we should pass NULL rather than errp.The 'errp' in this palce is convenient for debug, it can tell us the reason for failure without stepping into afalg_driver's
hash_bytesv().
It doesn't do anything useful for debug, because we are just immediately throwing away the error without printing it anywhere. Just pass NULL into the hash_bytesv call above.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 30 lines · Daniel P. Berrange
Quoted 29 lines · Longpeng (Mike)
Quoted 6 lines · Eric Blake
Quoted 2 lines · Longpeng(Mike)
Test-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if
afalg_drivers/poniter deference/pointer dereference/
OK.
Quoted 15 lines
Quoted 11 lines
doesn't support requested algos:
ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); if (ret == 0) { return ret; } error_free(*errp); // <--- hereSo we must check 'errp & *errp' before dereference.
No, if we are going to blindly ignore the error from the
hash_bytesv()call, then we should pass NULL rather than errp.
Quoted 7 lines
Quoted 2 lines
The 'errp' in this palce is convenient for debug, it can tell us the reason for failure without stepping into afalg_driver's
hash_bytesv().It doesn't do anything useful for debug, because we are just immediately throwing away the error without printing it anywhere. Just pass NULL into the
hash_bytesvcall above.
OK..
Afalg-backend cipher/hmac has the same usage, so maybe I need to correct all of them.
Quoted 2 lines
Regards,
Daniel
Signature · 2 lines
Regards, Longpeng(Mike)
Quoted 12 lines · Longpeng(Mike)
Test-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if
afalg_driverdoesn't support requested algos:ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); if (ret == 0) { return ret; } error_free(*errp); // <--- here
Quoted 1 line
So we must check 'errp & *errp' before dereference.
Only errp needs to be checked. It's okay to invoke error_free(NULL):
void error_free(Error *err)
{
if (err) {Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Longpeng(Mike)
Test-crypto-hash calls qcrypto_hash_bytesv/digest/base64 with errp=NULL, this will cause a NULL poniter deference if
afalg_driverdoesn't support requested algos:ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov, result, resultlen, errp); if (ret == 0) { return ret; } error_free(*errp); // <--- here
Quoted 7 lines
Quoted 1 line
So we must check 'errp & *errp' before dereference.
Only errp needs to be checked. It's okay to invoke
error_free(NULL):void error_free(Error *err) { if (err) {
Ah yes, thanks for your note :)
I'll pick another approach to fix this bug.
Signature · 2 lines
Regards, Longpeng(Mike)