Skip to content

Commit 1506129

Browse files
committed
Filter out non block devices from /dev/
1 parent 4a1976d commit 1506129

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/devices/virtio-blk.c

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

0 commit comments

Comments
 (0)