Skip to content

Commit fcbc564

Browse files
zhijianli88NanlinXie
authored andcommitted
DM: virtio_rnd: fix rnd->fd and vbs_k->fd leak
Previously, either rnd->fd or vbs_k->fd isn't be closed in some cases. this patch will close them in time. V2: fix vbs_k->fd leak as well Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
1 parent 789899d commit fcbc564

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

devicemodel/hw/pci/virtio/virtio_rnd.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ virtio_rnd_notify(void *base, struct virtio_vq_info *vq)
333333
static int
334334
virtio_rnd_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
335335
{
336-
struct virtio_rnd *rnd;
336+
struct virtio_rnd *rnd = NULL;
337337
int fd;
338338
int len;
339339
uint8_t v;
@@ -367,13 +367,13 @@ virtio_rnd_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
367367
len = read(fd, &v, sizeof(v));
368368
if (len <= 0) {
369369
WPRINTF(("virtio_rnd: /dev/random not ready, read(): %d", len));
370-
return -1;
370+
goto fail;
371371
}
372372

373373
rnd = calloc(1, sizeof(struct virtio_rnd));
374374
if (!rnd) {
375375
WPRINTF(("virtio_rnd: calloc returns NULL\n"));
376-
return -1;
376+
goto fail;
377377
}
378378

379379
rnd->vbs_k.status = kstat;
@@ -431,14 +431,23 @@ virtio_rnd_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
431431
pci_set_cfgdata16(dev, PCIR_SUBVEND_0, VIRTIO_VENDOR);
432432

433433
if (virtio_interrupt_init(&rnd->base, virtio_uses_msix())) {
434-
if (rnd)
435-
free(rnd);
436-
return -1;
434+
goto fail;
437435
}
438436

439437
virtio_set_io_bar(&rnd->base, 0);
440438

441439
return 0;
440+
441+
fail:
442+
close(fd);
443+
if (rnd) {
444+
if (rnd->vbs_k.status == VIRTIO_DEV_INIT_SUCCESS) {
445+
/* VBS-K is in use */
446+
close(rnd->vbs_k.fd);
447+
}
448+
free(rnd);
449+
}
450+
return -1;
442451
}
443452

444453
static void
@@ -462,6 +471,8 @@ virtio_rnd_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
462471
rnd->vbs_k.fd = -1;
463472
}
464473

474+
assert(rnd->fd >= 0);
475+
close(rnd->fd);
465476
DPRINTF(("%s: free struct virtio_rnd!\n", __func__));
466477
free(rnd);
467478
}

0 commit comments

Comments
 (0)