All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] cryptodev: fix memory leak
@ 2022-11-02  9:02 Lei He
  2022-11-02  9:02 ` [PATCH 1/2] cryptodev: avoid unreasonable use of g_autoptr Lei He
  2022-11-02  9:02 ` [PATCH 2/2] cryptodev: fix memory-leak occurs on error path Lei He
  0 siblings, 2 replies; 3+ messages in thread
From: Lei He @ 2022-11-02  9:02 UTC (permalink / raw)
  To: mst, arei.gonglei, berrange; +Cc: pizhenwei, qemu-devel, Lei He

- Avoid using g_autoptr to free memory that not allocated
in the same function.
- Fix memory-leak when 'virtio_crypto_handle_request' returns
no-zero value.
- When error occurred, always pass negative status to function
'vritio_crypto_req_complete'.

Lei He (2):
  cryptodev: avoid unreasonable use of g_autoptr
  cryptodev: fix memory-leak occurs on error path

 hw/virtio/virtio-crypto.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

-- 
2.11.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] cryptodev: avoid unreasonable use of g_autoptr
  2022-11-02  9:02 [PATCH 0/2] cryptodev: fix memory leak Lei He
@ 2022-11-02  9:02 ` Lei He
  2022-11-02  9:02 ` [PATCH 2/2] cryptodev: fix memory-leak occurs on error path Lei He
  1 sibling, 0 replies; 3+ messages in thread
From: Lei He @ 2022-11-02  9:02 UTC (permalink / raw)
  To: mst, arei.gonglei, berrange; +Cc: pizhenwei, qemu-devel, Lei He

Avoid using g_autoptr to free memory that not allocated in the same
function, just use g_free instead.

Signed-off-by: Lei He <helei.sig11@bytedance.com>
---
 hw/virtio/virtio-crypto.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
index 39c8f5914e..7ba63790d5 100644
--- a/hw/virtio/virtio-crypto.c
+++ b/hw/virtio/virtio-crypto.c
@@ -495,6 +495,7 @@ static void virtio_crypto_free_request(VirtIOCryptoReq *req)
         }
     }
 
+    g_free(req->in_iov);
     g_free(req);
 }
 
@@ -505,6 +506,7 @@ virtio_crypto_sym_input_data_helper(VirtIODevice *vdev,
                 CryptoDevBackendSymOpInfo *sym_op_info)
 {
     size_t s, len;
+    struct iovec *in_iov = req->in_iov;
 
     if (status != VIRTIO_CRYPTO_OK) {
         return;
@@ -512,18 +514,18 @@ virtio_crypto_sym_input_data_helper(VirtIODevice *vdev,
 
     len = sym_op_info->src_len;
     /* Save the cipher result */
-    s = iov_from_buf(req->in_iov, req->in_num, 0, sym_op_info->dst, len);
+    s = iov_from_buf(in_iov, req->in_num, 0, sym_op_info->dst, len);
     if (s != len) {
         virtio_error(vdev, "virtio-crypto dest data incorrect");
         return;
     }
 
-    iov_discard_front(&req->in_iov, &req->in_num, len);
+    iov_discard_front(&in_iov, &req->in_num, len);
 
     if (sym_op_info->op_type ==
                       VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
         /* Save the digest result */
-        s = iov_from_buf(req->in_iov, req->in_num, 0,
+        s = iov_from_buf(in_iov, req->in_num, 0,
                          sym_op_info->digest_result,
                          sym_op_info->digest_result_len);
         if (s != sym_op_info->digest_result_len) {
@@ -538,6 +540,7 @@ virtio_crypto_akcipher_input_data_helper(VirtIODevice *vdev,
         CryptoDevBackendAsymOpInfo *asym_op_info)
 {
     size_t s, len;
+    struct iovec *in_iov = req->in_iov;
 
     if (status != VIRTIO_CRYPTO_OK) {
         return;
@@ -548,13 +551,13 @@ virtio_crypto_akcipher_input_data_helper(VirtIODevice *vdev,
         return;
     }
 
-    s = iov_from_buf(req->in_iov, req->in_num, 0, asym_op_info->dst, len);
+    s = iov_from_buf(in_iov, req->in_num, 0, asym_op_info->dst, len);
     if (s != len) {
         virtio_error(vdev, "virtio-crypto asym dest data incorrect");
         return;
     }
 
-    iov_discard_front(&req->in_iov, &req->in_num, len);
+    iov_discard_front(&in_iov, &req->in_num, len);
 
     /* For akcipher, dst_len may be changed after operation */
     req->in_len = sizeof(struct virtio_crypto_inhdr) + asym_op_info->dst_len;
@@ -566,7 +569,6 @@ static void virtio_crypto_req_complete(void *opaque, int ret)
     VirtIOCrypto *vcrypto = req->vcrypto;
     VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
     uint8_t status = -ret;
-    g_autofree struct iovec *in_iov_copy = req->in_iov;
 
     if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
         virtio_crypto_sym_input_data_helper(vdev, req, status,
@@ -863,7 +865,7 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request)
      */
     request->in_num = in_num;
     request->in_iov = in_iov;
-    /* now, we free the in_iov_copy inside virtio_crypto_req_complete */
+    /* now, we free the in_iov_copy inside virtio_crypto_free_request */
     in_iov_copy = NULL;
 
     opcode = ldl_le_p(&req.header.opcode);
-- 
2.11.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] cryptodev: fix memory-leak occurs on error path
  2022-11-02  9:02 [PATCH 0/2] cryptodev: fix memory leak Lei He
  2022-11-02  9:02 ` [PATCH 1/2] cryptodev: avoid unreasonable use of g_autoptr Lei He
@ 2022-11-02  9:02 ` Lei He
  1 sibling, 0 replies; 3+ messages in thread
From: Lei He @ 2022-11-02  9:02 UTC (permalink / raw)
  To: mst, arei.gonglei, berrange; +Cc: pizhenwei, qemu-devel, Lei He

- Fix memory-leak when 'virtio_crypto_handle_request' returns non-zero
value.
- When error occurred, always pass negative status to function
'virtio_crypto_req_complete'.

Signed-off-by: Lei He <helei.sig11@bytedance.com>
---
 hw/virtio/virtio-crypto.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
index 7ba63790d5..97da74e719 100644
--- a/hw/virtio/virtio-crypto.c
+++ b/hw/virtio/virtio-crypto.c
@@ -895,12 +895,15 @@ check_result:
         if (ret == -EFAULT) {
             return -1;
         } else if (ret == -VIRTIO_CRYPTO_NOTSUPP) {
-            virtio_crypto_req_complete(request, VIRTIO_CRYPTO_NOTSUPP);
+            virtio_crypto_req_complete(request, -VIRTIO_CRYPTO_NOTSUPP);
         } else {
-            cryptodev_backend_crypto_operation(vcrypto->cryptodev,
+            ret = cryptodev_backend_crypto_operation(vcrypto->cryptodev,
                                     request, queue_index,
                                     virtio_crypto_req_complete,
                                     request);
+            if (ret < 0) {
+                virtio_crypto_req_complete(request, ret);
+            }
         }
         break;
 
@@ -911,7 +914,7 @@ check_result:
     default:
         error_report("virtio-crypto unsupported dataq opcode: %u",
                      opcode);
-        virtio_crypto_req_complete(request, VIRTIO_CRYPTO_NOTSUPP);
+        virtio_crypto_req_complete(request, -VIRTIO_CRYPTO_NOTSUPP);
     }
 
     return 0;
-- 
2.11.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-02  9:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-02  9:02 [PATCH 0/2] cryptodev: fix memory leak Lei He
2022-11-02  9:02 ` [PATCH 1/2] cryptodev: avoid unreasonable use of g_autoptr Lei He
2022-11-02  9:02 ` [PATCH 2/2] cryptodev: fix memory-leak occurs on error path Lei He

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.