From 018df6aecab9274dceafff8ca14818ac772da862 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Sun, 3 May 2015 09:43:03 +0200 Subject: [PATCH] dev_handlers: Report invalid block sizes If while attaching a pass-through device the READ CAPACITY command reports an invalid sector size, log an error message. Signed-off-by: Bart Van Assche --- scst/src/dev_handlers/scst_cdrom.c | 6 ++++++ scst/src/dev_handlers/scst_disk.c | 6 ++++++ scst/src/dev_handlers/scst_modisk.c | 6 ++++++ scst/src/dev_handlers/scst_user.c | 9 ++++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/scst/src/dev_handlers/scst_cdrom.c b/scst/src/dev_handlers/scst_cdrom.c index 1e5826d77..a08999b4f 100644 --- a/scst/src/dev_handlers/scst_cdrom.c +++ b/scst/src/dev_handlers/scst_cdrom.c @@ -129,6 +129,12 @@ static int cdrom_attach(struct scst_device *dev) dev->block_shift = scst_calc_block_shift(sector_size); TRACE_DBG("Sector size is %i scsi_level %d(SCSI_2 %d)", sector_size, dev->scsi_dev->scsi_level, SCSI_2); + if (dev->block_shift < 9) { + PRINT_ERROR("READ CAPACITY reported an invalid sector size: %d", + sector_size); + res = -EINVAL; + goto out_free_buf; + } } else { dev->block_shift = CDROM_DEF_BLOCK_SHIFT; TRACE(TRACE_MINOR, "Read capacity failed: %x, using default " diff --git a/scst/src/dev_handlers/scst_disk.c b/scst/src/dev_handlers/scst_disk.c index 974e01bd1..ff6c581f1 100644 --- a/scst/src/dev_handlers/scst_disk.c +++ b/scst/src/dev_handlers/scst_disk.c @@ -221,6 +221,12 @@ static int disk_attach(struct scst_device *dev) dev->block_shift = DISK_DEF_BLOCK_SHIFT; else dev->block_shift = scst_calc_block_shift(sector_size); + if (dev->block_shift < 9) { + PRINT_ERROR("READ CAPACITY reported an invalid sector size: %d", + sector_size); + res = -EINVAL; + goto out_free_buf; + } } else { dev->block_shift = DISK_DEF_BLOCK_SHIFT; TRACE(TRACE_MINOR, "Read capacity failed: %x, using default " diff --git a/scst/src/dev_handlers/scst_modisk.c b/scst/src/dev_handlers/scst_modisk.c index b3461b1a9..850f8efe2 100644 --- a/scst/src/dev_handlers/scst_modisk.c +++ b/scst/src/dev_handlers/scst_modisk.c @@ -228,6 +228,12 @@ static int modisk_attach(struct scst_device *dev) dev->block_shift = scst_calc_block_shift(sector_size); TRACE_DBG("Sector size is %i scsi_level %d(SCSI_2 %d)", sector_size, dev->scsi_dev->scsi_level, SCSI_2); + if (dev->block_shift < 9) { + PRINT_ERROR("READ CAPACITY reported an invalid sector size: %d", + sector_size); + res = -EINVAL; + goto out_free_buf; + } } else { dev->block_shift = MODISK_DEF_BLOCK_SHIFT; TRACE(TRACE_MINOR, "Read capacity failed: %x, using default " diff --git a/scst/src/dev_handlers/scst_user.c b/scst/src/dev_handlers/scst_user.c index 8ad6d79bd..13da69e4c 100644 --- a/scst/src/dev_handlers/scst_user.c +++ b/scst/src/dev_handlers/scst_user.c @@ -1031,7 +1031,9 @@ static void dev_user_set_block_shift(struct scst_cmd *cmd, int block_shift) */ new_block_shift = block_shift ? : scst_calc_block_shift(udev->def_block_size); - if (dev->block_shift != new_block_shift) { + if (new_block_shift < 9) { + PRINT_ERROR("Invalid block shift %d", new_block_shift); + } else if (dev->block_shift != new_block_shift) { PRINT_INFO("%s: Changed block shift from %d into %d / %d", dev->virt_name, dev->block_shift, block_shift, new_block_shift); @@ -2614,6 +2616,11 @@ static int dev_user_attach(struct scst_device *sdev) case TYPE_ROM: case TYPE_MOD: sdev->block_shift = scst_calc_block_shift(sdev->block_size); + if (sdev->block_shift < 9) { + PRINT_ERROR("Invalid block size %d", sdev->block_size); + res = -EINVAL; + goto out; + } break; default: sdev->block_shift = -1; /* not used */