Skip to contentSkip to conversation

virtio: fix possible memory leak in virtqueue_add()

PATCH Superseded Wei Yongjun 2 participants 4 messages Aug 2, 2016 – Aug 3, 2016 virtio core

lore
Share & tools
lore
#0 lore raw

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
index 114a0c8..bda71ef 100644
@@ -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;
 	}
#1 lore raw
Quoted 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.

---
 drivers/virtio/virtio_ring.c | 1 +
 1 file changed, 1 insertion(+)
drivers/virtio/virtio_ring.c +0 -0
index 114a0c8..bda71ef 100644

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 -0
 		return -ENOSPC;
 	}
#2 lore raw

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
index 114a0c8..e4be912 100644
@@ -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;
 	}
#3 lore raw
Quoted 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.

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
index 114a0c8..e4be912 100644
@@ -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;
 	}