Skip to contentSkip to conversation

crypto: afalg: fix a NULL pointer dereference

PATCH Superseded Longpeng(Mike) 5 participants 8 messages Nov 6, 2017 – Nov 7, 2017 Crypto API

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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);  // <--- here

So we must check 'errp & *errp' before dereference.

---
 crypto/hash.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/hash.c +3 -1
index ac59c63..c464c78 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
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 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);  // <--- here
Quoted 4 lines

So we must check 'errp & *errp' before dereference.

---

Thanks,
-Gonglei

Quoted 21 lines
 crypto/hash.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/hash.c +3 -2
index ac59c63..c464c78 100644
@@ -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

#2 lore raw
In reply to Longpeng(Mike)
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);  // <--- here

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.

Quoted 11 lines
---
 crypto/hash.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/hash.c +0 -0
index ac59c63..c464c78 100644

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
#6 lore raw
In reply to 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_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);  // <--- 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) {
#7 lore raw
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_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);  // <--- 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)
#3 lore raw
In reply to Eric Blake
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_driver

s/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);  // <--- here

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().

Quoted 6 lines
Quoted 6 lines
---
 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
index ac59c63..c464c78 100644
@@ -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)
#4 lore raw
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_driver

s/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 :|
#5 lore raw
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_driver

s/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);  // <--- here

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.

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_bytesv call 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)