diff mbox series

[v2] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection

Message ID 7bf9a4fa-1675-45a6-88dd-82549ae2c6e0@web.de (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series [v2] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection | expand

Commit Message

Markus Elfring Dec. 26, 2023, 10:12 a.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Tue, 26 Dec 2023 11:00:20 +0100

The kfree() function was called in up to two cases by the
__virtio_crypto_akcipher_do_req() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

* Adjust jump targets.

* Delete two initialisations which became unnecessary
  with this refactoring.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---

v2:
A typo was fixed for the delimiter of a label.

 drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

--
2.43.0

Comments

Gonglei (Arei) Dec. 26, 2023, 12:22 p.m. UTC | #1
> -----Original Message-----
> From: Markus Elfring [mailto:Markus.Elfring@web.de]
> Sent: Tuesday, December 26, 2023 6:12 PM
> To: kernel test robot <lkp@intel.com>; virtualization@lists.linux.dev;
> linux-crypto@vger.kernel.org; kernel-janitors@vger.kernel.org; David S. Miller
> <davem@davemloft.net>; Gonglei (Arei) <arei.gonglei@huawei.com>; Herbert
> Xu <herbert@gondor.apana.org.au>; Jason Wang <jasowang@redhat.com>;
> Michael S. Tsirkin <mst@redhat.com>; Xuan Zhuo
> <xuanzhuo@linux.alibaba.com>
> Cc: llvm@lists.linux.dev; oe-kbuild-all@lists.linux.dev; netdev@vger.kernel.org;
> LKML <linux-kernel@vger.kernel.org>; cocci@inria.fr
> Subject: [PATCH v2] crypto: virtio - Less function calls in
> __virtio_crypto_akcipher_do_req() after error detection
> 
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 26 Dec 2023 11:00:20 +0100
> 
> The kfree() function was called in up to two cases by the
> __virtio_crypto_akcipher_do_req() function during error handling even if the
> passed variable contained a null pointer.
> This issue was detected by using the Coccinelle software.
> 
> * Adjust jump targets.
> 
> * Delete two initialisations which became unnecessary
>   with this refactoring.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
> 
> v2:
> A typo was fixed for the delimiter of a label.
> 
>  drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 

Reviewed-by: Gonglei <arei.gonglei@huawei.com>


Regards,
-Gonglei
Justin Stitt Jan. 9, 2024, 11:42 p.m. UTC | #2
Hi,

On Tue, Dec 26, 2023 at 11:12:23AM +0100, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 26 Dec 2023 11:00:20 +0100
>
> The kfree() function was called in up to two cases by the
> __virtio_crypto_akcipher_do_req() function during error handling
> even if the passed variable contained a null pointer.
> This issue was detected by using the Coccinelle software.

If the script is short and simple would you mind, in the future,
including it below the fold -- this may help others do similar work down
the line -- Or you could also link to a git-managed version like what
Kees has been doing with his __counted_by patches [1].

>
> * Adjust jump targets.
>
> * Delete two initialisations which became unnecessary
>   with this refactoring.
>
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---

Nonetheless,

Reviewed-by: Justin Stitt <justinstitt@google.com>
>
> v2:
> A typo was fixed for the delimiter of a label.
>
>  drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
> index 2621ff8a9376..057da5bd8d30 100644
> --- a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
> +++ b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
> @@ -224,11 +224,11 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
>  	struct virtio_crypto *vcrypto = ctx->vcrypto;
>  	struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
>  	struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
> -	void *src_buf = NULL, *dst_buf = NULL;
> +	void *src_buf, *dst_buf = NULL;
>  	unsigned int num_out = 0, num_in = 0;
>  	int node = dev_to_node(&vcrypto->vdev->dev);
>  	unsigned long flags;
> -	int ret = -ENOMEM;
> +	int ret;
>  	bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
>  	unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;
>
> @@ -239,7 +239,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
>  	/* src data */
>  	src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
>  	if (!src_buf)
> -		goto err;
> +		return -ENOMEM;
>
>  	if (verify) {
>  		/* for verify operation, both src and dst data work as OUT direction */
> @@ -254,7 +254,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
>  		/* dst data */
>  		dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
>  		if (!dst_buf)
> -			goto err;
> +			goto free_src;
>
>  		sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
>  		sgs[num_out + num_in++] = &dstdata_sg;
> @@ -277,9 +277,9 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
>  	return 0;
>
>  err:
> -	kfree(src_buf);
>  	kfree(dst_buf);
> -
> +free_src:
> +	kfree(src_buf);
>  	return -ENOMEM;
>  }
>
> --
> 2.43.0
>

[1]: https://lore.kernel.org/all/20230922175023.work.239-kees@kernel.org/

Thanks
Justin
Markus Elfring Jan. 10, 2024, 7:46 a.m. UTC | #3
>> The kfree() function was called in up to two cases by the
>> __virtio_crypto_akcipher_do_req() function during error handling
>> even if the passed variable contained a null pointer.
>> This issue was detected by using the Coccinelle software.
>
> If the script is short and simple would you mind, in the future,
> including it below the fold -- this may help others do similar work down
> the line -- Or …

Would you like to take another look at the clarification approach
“Reconsidering kfree() calls for null pointers (with SmPL)”?
https://lore.kernel.org/cocci/6cbcf640-55e5-2f11-4a09-716fe681c0d2@web.de/
https://sympa.inria.fr/sympa/arc/cocci/2023-03/msg00096.html

Regards,
Markus
Herbert Xu Jan. 26, 2024, 8:55 a.m. UTC | #4
On Tue, Dec 26, 2023 at 11:12:23AM +0100, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 26 Dec 2023 11:00:20 +0100
> 
> The kfree() function was called in up to two cases by the
> __virtio_crypto_akcipher_do_req() function during error handling
> even if the passed variable contained a null pointer.
> This issue was detected by using the Coccinelle software.
> 
> * Adjust jump targets.
> 
> * Delete two initialisations which became unnecessary
>   with this refactoring.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
> 
> v2:
> A typo was fixed for the delimiter of a label.
> 
>  drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)

Patch applied.  Thanks.
diff mbox series

Patch

diff --git a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
index 2621ff8a9376..057da5bd8d30 100644
--- a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
+++ b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
@@ -224,11 +224,11 @@  static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
 	struct virtio_crypto *vcrypto = ctx->vcrypto;
 	struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
 	struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
-	void *src_buf = NULL, *dst_buf = NULL;
+	void *src_buf, *dst_buf = NULL;
 	unsigned int num_out = 0, num_in = 0;
 	int node = dev_to_node(&vcrypto->vdev->dev);
 	unsigned long flags;
-	int ret = -ENOMEM;
+	int ret;
 	bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
 	unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;

@@ -239,7 +239,7 @@  static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
 	/* src data */
 	src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
 	if (!src_buf)
-		goto err;
+		return -ENOMEM;

 	if (verify) {
 		/* for verify operation, both src and dst data work as OUT direction */
@@ -254,7 +254,7 @@  static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
 		/* dst data */
 		dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
 		if (!dst_buf)
-			goto err;
+			goto free_src;

 		sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
 		sgs[num_out + num_in++] = &dstdata_sg;
@@ -277,9 +277,9 @@  static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
 	return 0;

 err:
-	kfree(src_buf);
 	kfree(dst_buf);
-
+free_src:
+	kfree(src_buf);
 	return -ENOMEM;
 }