Skip to contentSkip to conversation

crypto: caam - fix use after free issue in *_crypt_done

PATCH In review Zhenzhong Duan 2 participants 3 messages Apr 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: caam - fix use after free issue in *_crypt_done

In both aead_crypt_done and skcipher_crypt_done, edesc->bklog is referenced after the structure pointed by edesc is freed.

Fix them by moving kfree(edesc) to the end of function call.

---
 drivers/crypto/caam/caamalg.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/caam/caamalg.c +4 -4
index b7bb7c30adeb..6d746ef5e650 100644
@@ -973,8 +973,6 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 
 	aead_unmap(jrdev, edesc, req);
 
-	kfree(edesc);
-
 	/*
 	 * If no backlog flag, the completion of the request is done
 	 * by CAAM, not crypto engine.
@@ -983,6 +981,8 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 		aead_request_complete(req, ecode);
 	else
 		crypto_finalize_aead_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
 }
 
 static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
@@ -1022,8 +1022,6 @@ static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
 		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
 
-	kfree(edesc);
-
 	/*
 	 * If no backlog flag, the completion of the request is done
 	 * by CAAM, not crypto engine.
@@ -1032,6 +1030,8 @@ static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 		skcipher_request_complete(req, ecode);
 	else
 		crypto_finalize_skcipher_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
 }
 
 /*
Signature · 1 line
2.17.1
#1 lore raw
Quoted 6 lines · Zhenzhong Duan

In both aead_crypt_done and skcipher_crypt_done, edesc->bklog is referenced after the structure pointed by edesc is freed.

Fix them by moving kfree(edesc) to the end of function call.

These issues were already fixed, and applied on cryptodev, by this
series: https://patchwork.kernel.org/cover/11476799/

Regards,
Iulia

Quoted 45 lines
---
  drivers/crypto/caam/caamalg.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/caam/caamalg.c +4 -4
index b7bb7c30adeb..6d746ef5e650 100644
@@ -973,8 +973,6 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 
  	aead_unmap(jrdev, edesc, req);
 
-	kfree(edesc);
-
  	/*
  	 * If no backlog flag, the completion of the request is done
  	 * by CAAM, not crypto engine.
@@ -983,6 +981,8 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
  		aead_request_complete(req, ecode);
  	else
  		crypto_finalize_aead_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
  }
 
  static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
@@ -1022,8 +1022,6 @@ static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
  		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
  		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
 
-	kfree(edesc);
-
  	/*
  	 * If no backlog flag, the completion of the request is done
  	 * by CAAM, not crypto engine.
@@ -1032,6 +1030,8 @@ static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
  		skcipher_request_complete(req, ecode);
  	else
  		crypto_finalize_skcipher_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
  }
 
  /*