Skip to content

Commit 3714e1f

Browse files
committed
Filter out non block devices from /dev/
1 parent 249bdc3 commit 3714e1f

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/devices/virtio-blk.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ uint32_t *virtio_blk_init(virtio_blk_state_t *vblk,
445445
/* Get the disk size */
446446
uint64_t disk_size;
447447
if (!strcmp(disk_file_dirname, "/dev")) { /* from /dev/, leverage ioctl */
448+
if ((st.st_mode & S_IFMT) != S_IFBLK) {
449+
rv_log_error("%s is not block device", disk_file);
450+
goto fail;
451+
}
448452
#if !defined(__EMSCRIPTEN__)
449453
#if defined(__APPLE__)
450454
uint32_t block_size;
@@ -465,12 +469,7 @@ uint32_t *virtio_blk_init(virtio_blk_state_t *vblk,
465469
}
466470
#endif
467471
#endif /* !defined(__EMSCRIPTEN__) */
468-
} else { /* other path, stat it as normal file */
469-
struct stat st;
470-
if (fstat(disk_fd, &st) == -1) {
471-
rv_log_error("fstat failed");
472-
goto disk_size_fail;
473-
}
472+
} else { /* other path, get the size of block device via stat buffer */
474473
disk_size = st.st_size;
475474
}
476475
VBLK_PRIV(vblk)->disk_size = disk_size;

0 commit comments

Comments
 (0)