virtio: fix possible memory leak in virtqueue_add()
PATCH 1/1 virtio: fix possible memory leak in virtqueue_add()
desc may malloced in virtqueue_add() and should be freed before leaving from the error handling cases, otherwise it will cause memory leak.
--- drivers/virtio/virtio_ring.c | 1 + 1 file changed, 1 insertion(+)
drivers/virtio/virtio_ring.c +1 -0
@@ -328,6 +328,7 @@ static inline int virtqueue_add(struct virtqueue *_vq,if (out_sgs) vq->notify (&vq->vq);END_USE (vq); +kfree (desc);return -ENOSPC ; }
Michael S. Tsirkin
maintainerQuoted 14 lines · Wei Yongjun
desc may malloced in
virtqueue_add()and should be freed before leaving from the error handling cases, otherwise it will cause memory leak.Signed-off-by Wei Yongjun <[email protected]>--- drivers/virtio/virtio_ring.c | 1 + 1 file changed, 1 insertion(+)drivers/virtio/virtio_ring.c +0
-0
drivers/virtio/virtio_ring.c static inline int virtqueue_add(struct virtqueue *_vq, · line 331
Quoted 4 lines
drivers/virtio/virtio_ring.c +1
-0@@ -328,6 +328,7 @@ static inline int virtqueue_add(struct virtqueue *_vq,if (out_sgs) vq->notify (&vq->vq);END_USE (vq); +kfree (desc);
I think only if indirect is true, otherwise you will free vq->vring.desc.
Quoted 2 lines
drivers/virtio/virtio_ring.c +0
-0return -ENOSPC ; }
PATCH v2 1/1 virtio: fix possible memory leak in virtqueue_add()
In reply to Wei Yongjun'desc' is malloced in virtqueue_add() and should be freed before leaving from the error handling cases, otherwise it will cause memory leak.
--- drivers/virtio/virtio_ring.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/virtio/virtio_ring.c +2 -0
@@ -328,6 +328,8 @@ static inline int virtqueue_add(struct virtqueue *_vq,if (out_sgs) vq->notify (&vq->vq);END_USE (vq); +if (indirect) +kfree (desc);return -ENOSPC ; }
Michael S. Tsirkin
maintainerQuoted 5 lines · Wei Yongjun
'desc' is malloced in
virtqueue_add()and should be freed before leaving from the error handling cases, otherwise it will cause memory leak.Signed-off-by Wei Yongjun <[email protected]>
Appliecd except I moved this to before END_USE - seems cleaner as alloc is caller after START_USE.
Quoted 16 lines
--- drivers/virtio/virtio_ring.c | 2 ++ 1 file changed, 2 insertions(+)drivers/virtio/virtio_ring.c +2
-0@@ -328,6 +328,8 @@ static inline int virtqueue_add(struct virtqueue *_vq,if (out_sgs) vq->notify (&vq->vq);END_USE (vq); +if (indirect) +kfree (desc);return -ENOSPC ; }