From 1fb5fd9662f69bfa9962d056b10c5a87609e3fc1 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Wed, 20 Jan 2021 16:53:30 +0100 Subject: [PATCH] Verify whether input device path is actually a block device This applies to add/momove core, start/stop cache, zero superblock commands. Signed-off-by: Adam Rutkowski --- casadm/cas_lib.c | 14 ++++++++++++++ casadm/cas_main.c | 9 +++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/casadm/cas_lib.c b/casadm/cas_lib.c index 8c80a4a..3450583 100644 --- a/casadm/cas_lib.c +++ b/casadm/cas_lib.c @@ -159,10 +159,24 @@ cas_printf_t cas_printf = std_printf; int validate_dev(const char *dev_path) { struct fstab *fstab_entry; + struct stat status; + fstab_entry = getfsspec(dev_path); if (fstab_entry != NULL) { + printf("Device entry present in fstab, please remove it.\n"); return FAILURE; } + + if (stat(dev_path, &status) == -1) { + printf("Failed to query device status.\n"); + return FAILURE; + } + + if (!S_ISBLK(status.st_mode)) { + printf("Path does not describe a block device\n"); + return FAILURE; + } + return SUCCESS; } diff --git a/casadm/cas_main.c b/casadm/cas_main.c index ecd9ba8..c9e2632 100644 --- a/casadm/cas_main.c +++ b/casadm/cas_main.c @@ -92,16 +92,13 @@ static struct command_args command_args_values = { }; int validate_device_name(const char *dev_name) { - if (validate_dev(dev_name)) { - cas_printf(LOG_ERR, "Cache creation aborted, %s entry exists in /etc/fstab. Please remove it!\n", - dev_name); + if (strnlen(dev_name, MAX_STR_LEN) >= MAX_STR_LEN) { + cas_printf(LOG_ERR, "Illegal device name\n"); return FAILURE; } - if (strnlen(dev_name, MAX_STR_LEN) >= MAX_STR_LEN) { - cas_printf(LOG_ERR, "Illegal device %s\n", dev_name); + if (validate_dev(dev_name)) return FAILURE; - } return SUCCESS; }