From af933af991e0e2f81ab757430c98a814f4c5e65b Mon Sep 17 00:00:00 2001 From: Vladislav Bolkhovitin Date: Thu, 24 Jan 2008 11:52:01 +0000 Subject: [PATCH] - Version protection added - Cleanups git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@249 d57e44dd-8a1f-0410-8b47-8ef2f437770f --- iscsi-scst/kernel/iscsi.c | 6 ++-- mpt/mpt_scst.c | 7 ++--- qla2x00t/qla2x00-target/qla2x00t.c | 7 ++--- scst/include/scsi_tgt.h | 28 ++++++++++++++---- scst/include/scst_const.h | 2 ++ scst/include/scst_user.h | 5 ++-- scst/src/dev_handlers/scst_cdrom.c | 6 ++-- scst/src/dev_handlers/scst_changer.c | 6 ++-- scst/src/dev_handlers/scst_disk.c | 12 ++++---- scst/src/dev_handlers/scst_modisk.c | 12 ++++---- scst/src/dev_handlers/scst_processor.c | 6 ++-- scst/src/dev_handlers/scst_raid.c | 6 ++-- scst/src/dev_handlers/scst_tape.c | 12 ++++---- scst/src/dev_handlers/scst_user.c | 39 ++++++++++++++++++++------ scst/src/scst_main.c | 35 +++++++++++++++++++---- usr/fileio/fileio.c | 2 +- 16 files changed, 128 insertions(+), 63 deletions(-) diff --git a/iscsi-scst/kernel/iscsi.c b/iscsi-scst/kernel/iscsi.c index a60e54612..3a6f3d074 100644 --- a/iscsi-scst/kernel/iscsi.c +++ b/iscsi-scst/kernel/iscsi.c @@ -2768,10 +2768,10 @@ static int __init iscsi_init(void) goto out_event; } - if (scst_register_target_template(&iscsi_template) < 0) { - err = -ENODEV; + err = scst_register_target_template(&iscsi_template); + if (err < 0) goto out_kmem; - } + iscsi_template_registered = 1; if ((err = iscsi_procfs_init()) < 0) diff --git a/mpt/mpt_scst.c b/mpt/mpt_scst.c index b0ebdfc99..9c8524815 100644 --- a/mpt/mpt_scst.c +++ b/mpt/mpt_scst.c @@ -5523,11 +5523,10 @@ static int __init mpt_target_init(void) int res = 0; TRACE_ENTRY(); - - if (scst_register_target_template(&tgt_template) < 0) { - res = -ENODEV; + + res = scst_register_target_template(&tgt_template); + if (res < 0) goto out; - } res = mpt_proc_log_entry_build(&tgt_template); if (res < 0) { diff --git a/qla2x00t/qla2x00-target/qla2x00t.c b/qla2x00t/qla2x00-target/qla2x00t.c index d7c4a5f9c..4fa5a9466 100644 --- a/qla2x00t/qla2x00-target/qla2x00t.c +++ b/qla2x00t/qla2x00-target/qla2x00t.c @@ -2211,11 +2211,10 @@ static int __init q2t_init(void) res = -ENOMEM; goto out; } - - if (scst_register_target_template(&tgt_template) < 0) { - res = -ENODEV; + + res = scst_register_target_template(&tgt_template); + if (res < 0) goto out; - } /* qla2xxx_tgt_register_driver() happens in q2t_target_detect * called via scst_register_target_template() diff --git a/scst/include/scsi_tgt.h b/scst/include/scsi_tgt.h index cfb56fc16..91ded86f0 100644 --- a/scst/include/scsi_tgt.h +++ b/scst/include/scsi_tgt.h @@ -41,9 +41,10 @@ typedef _Bool bool; #endif /* Version numbers, the same as for the kernel */ -#define SCST_VERSION_CODE 0x000906 -#define SCST_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) +#define SCST_VERSION_CODE 0x00090601 +#define SCST_VERSION(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + d) #define SCST_VERSION_STRING "0.9.6-rc1" +#define SCST_INTERFACE_VERSION SCST_VERSION_STRING "$Revision$" SCST_CONST_VERSION /************************************************************* ** States of command processing state machine. At first, @@ -1539,7 +1540,12 @@ struct scst_tgt_dev_UA * Registers target template * Returns 0 on success or appropriate error code otherwise */ -int scst_register_target_template(struct scst_tgt_template *vtt); +int __scst_register_target_template(struct scst_tgt_template *vtt, + const char *version); +static inline int scst_register_target_template(struct scst_tgt_template *vtt) +{ + return __scst_register_target_template(vtt, SCST_INTERFACE_VERSION); +} /* * Unregisters target template @@ -1646,7 +1652,12 @@ void scst_unregister_session(struct scst_session *sess, int wait, * Registers dev handler driver * Returns 0 on success or appropriate error code otherwise */ -int scst_register_dev_driver(struct scst_dev_type *dev_type); +int __scst_register_dev_driver(struct scst_dev_type *dev_type, + const char *version); +static inline int scst_register_dev_driver(struct scst_dev_type *dev_type) +{ + return __scst_register_dev_driver(dev_type, SCST_INTERFACE_VERSION); +} /* * Unregisters dev handler driver @@ -1657,7 +1668,14 @@ void scst_unregister_dev_driver(struct scst_dev_type *dev_type); * Registers dev handler driver for virtual devices (eg VDISK) * Returns 0 on success or appropriate error code otherwise */ -int scst_register_virtual_dev_driver(struct scst_dev_type *dev_type); +int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type, + const char *version); +static inline int scst_register_virtual_dev_driver( + struct scst_dev_type *dev_type) +{ + return __scst_register_virtual_dev_driver(dev_type, + SCST_INTERFACE_VERSION); +} /* * Unregisters dev handler driver for virtual devices diff --git a/scst/include/scst_const.h b/scst/include/scst_const.h index 24288c6ec..7121c7b24 100644 --- a/scst/include/scst_const.h +++ b/scst/include/scst_const.h @@ -21,6 +21,8 @@ #include +#define SCST_CONST_VERSION "$Revision$" + /*** Shared constants between user and kernel spaces ***/ /* Max size of CDB */ diff --git a/scst/include/scst_user.h b/scst/include/scst_user.h index 98792fd9f..90614be2e 100644 --- a/scst/include/scst_user.h +++ b/scst/include/scst_user.h @@ -25,7 +25,8 @@ #define DEV_USER_NAME "scst_user" #define DEV_USER_PATH "/dev/" -#define DEV_USER_VERSION 963 +#define DEV_USER_VERSION_NAME "0.9.6" +#define DEV_USER_VERSION DEV_USER_VERSION_NAME "$Revision$" SCST_CONST_VERSION /* * Chosen so sizeof(scst_user_sess) <= sizeof(scst_user_scsi_cmd_exec) @@ -105,7 +106,7 @@ struct scst_user_opt struct scst_user_dev_desc { - uint32_t version; + aligned_u64 version_str; uint8_t type; struct scst_user_opt opt; uint32_t block_size; diff --git a/scst/src/dev_handlers/scst_cdrom.c b/scst/src/dev_handlers/scst_cdrom.c index 57acb045a..89eae1bf8 100644 --- a/scst/src/dev_handlers/scst_cdrom.c +++ b/scst/src/dev_handlers/scst_cdrom.c @@ -276,10 +276,10 @@ static int __init cdrom_init(void) TRACE_ENTRY(); cdrom_devtype.module = THIS_MODULE; - if (scst_register_dev_driver(&cdrom_devtype) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&cdrom_devtype); + if (res < 0) goto out; - } res = scst_dev_handler_build_std_proc(&cdrom_devtype); if (res != 0) diff --git a/scst/src/dev_handlers/scst_changer.c b/scst/src/dev_handlers/scst_changer.c index 1931ce9e1..0e7816743 100644 --- a/scst/src/dev_handlers/scst_changer.c +++ b/scst/src/dev_handlers/scst_changer.c @@ -196,10 +196,10 @@ static int __init changer_init(void) TRACE_ENTRY(); changer_devtype.module = THIS_MODULE; - if (scst_register_dev_driver(&changer_devtype) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&changer_devtype); + if (res < 0) goto out; - } res = scst_dev_handler_build_std_proc(&changer_devtype); if (res != 0) diff --git a/scst/src/dev_handlers/scst_disk.c b/scst/src/dev_handlers/scst_disk.c index 2afb09679..7d2754dec 100644 --- a/scst/src/dev_handlers/scst_disk.c +++ b/scst/src/dev_handlers/scst_disk.c @@ -84,20 +84,20 @@ static int __init init_scst_disk_driver(void) TRACE_ENTRY(); disk_devtype.module = THIS_MODULE; - if (scst_register_dev_driver(&disk_devtype) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&disk_devtype); + if (res < 0) goto out; - } res = scst_dev_handler_build_std_proc(&disk_devtype); if (res != 0) goto out_unreg1; disk_devtype_perf.module = THIS_MODULE; - if (scst_register_dev_driver(&disk_devtype_perf) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&disk_devtype_perf); + if (res < 0) goto out_unreg1_err1; - } res = scst_dev_handler_build_std_proc(&disk_devtype_perf); if (res != 0) diff --git a/scst/src/dev_handlers/scst_modisk.c b/scst/src/dev_handlers/scst_modisk.c index 3391954e0..c7edea331 100644 --- a/scst/src/dev_handlers/scst_modisk.c +++ b/scst/src/dev_handlers/scst_modisk.c @@ -84,20 +84,20 @@ static int __init init_scst_modisk_driver(void) TRACE_ENTRY(); modisk_devtype.module = THIS_MODULE; - if (scst_register_dev_driver(&modisk_devtype) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&modisk_devtype); + if (res < 0) goto out; - } res = scst_dev_handler_build_std_proc(&modisk_devtype); if (res != 0) goto out_unreg1; modisk_devtype_perf.module = THIS_MODULE; - if (scst_register_dev_driver(&modisk_devtype_perf) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&modisk_devtype_perf); + if (res < 0) goto out_unreg1_err1; - } res = scst_dev_handler_build_std_proc(&modisk_devtype_perf); if (res != 0) diff --git a/scst/src/dev_handlers/scst_processor.c b/scst/src/dev_handlers/scst_processor.c index a30088d88..764c6c056 100644 --- a/scst/src/dev_handlers/scst_processor.c +++ b/scst/src/dev_handlers/scst_processor.c @@ -195,10 +195,10 @@ static int __init processor_init(void) TRACE_ENTRY(); processor_devtype.module = THIS_MODULE; - if (scst_register_dev_driver(&processor_devtype) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&processor_devtype); + if (res < 0) goto out; - } res = scst_dev_handler_build_std_proc(&processor_devtype); if (res != 0) diff --git a/scst/src/dev_handlers/scst_raid.c b/scst/src/dev_handlers/scst_raid.c index acd097d21..82a8c2ed6 100644 --- a/scst/src/dev_handlers/scst_raid.c +++ b/scst/src/dev_handlers/scst_raid.c @@ -195,10 +195,10 @@ static int __init raid_init(void) TRACE_ENTRY(); raid_devtype.module = THIS_MODULE; - if (scst_register_dev_driver(&raid_devtype) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&raid_devtype); + if (res < 0) goto out; - } res = scst_dev_handler_build_std_proc(&raid_devtype); if (res != 0) diff --git a/scst/src/dev_handlers/scst_tape.c b/scst/src/dev_handlers/scst_tape.c index 7b111b46c..9d7a96fdc 100644 --- a/scst/src/dev_handlers/scst_tape.c +++ b/scst/src/dev_handlers/scst_tape.c @@ -89,20 +89,20 @@ static int __init init_scst_tape_driver(void) TRACE_ENTRY(); tape_devtype.module = THIS_MODULE; - if (scst_register_dev_driver(&tape_devtype) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&tape_devtype); + if (res < 0) goto out; - } res = scst_dev_handler_build_std_proc(&tape_devtype); if (res != 0) goto out_unreg1; tape_devtype_perf.module = THIS_MODULE; - if (scst_register_dev_driver(&tape_devtype_perf) < 0) { - res = -ENODEV; + + res = scst_register_dev_driver(&tape_devtype_perf); + if (res < 0) goto out_unreg1_err1; - } res = scst_dev_handler_build_std_proc(&tape_devtype_perf); if (res != 0) diff --git a/scst/src/dev_handlers/scst_user.c b/scst/src/dev_handlers/scst_user.c index ed85d979d..1ab385f3d 100644 --- a/scst/src/dev_handlers/scst_user.c +++ b/scst/src/dev_handlers/scst_user.c @@ -1755,6 +1755,7 @@ static long dev_user_ioctl(struct file *file, unsigned int cmd, goto out; } TRACE_BUFFER("dev_desc", dev_desc, sizeof(*dev_desc)); + dev_desc->name[sizeof(dev_desc->name)-1] = '\0'; res = dev_user_register_dev(file, dev_desc); kfree(dev_desc); break; @@ -2551,6 +2552,31 @@ static void dev_user_setup_functions(struct scst_user_dev *dev) return; } +static int dev_user_check_version(const struct scst_user_dev_desc *dev_desc) +{ + char ver[sizeof(DEV_USER_VERSION)+1]; + int res; + + res = copy_from_user(ver, (void*)(unsigned long)dev_desc->version_str, + sizeof(ver)); + if (res < 0) { + PRINT_ERROR("%s", "Unable to get version string"); + goto out; + } + ver[sizeof(ver)-1] = '\0'; + + if (strcmp(ver, DEV_USER_VERSION) != 0) { + /* ->name already 0-terminated in dev_user_ioctl() */ + PRINT_ERROR("Incorrect version of user device %s (%s)", + dev_desc->name, ver); + res = -EINVAL; + goto out; + } + +out: + return res; +} + static int dev_user_register_dev(struct file *file, const struct scst_user_dev_desc *dev_desc) { @@ -2560,12 +2586,9 @@ static int dev_user_register_dev(struct file *file, TRACE_ENTRY(); - if (dev_desc->version != DEV_USER_VERSION) { - PRINT_ERROR("Version mismatch (requested %d, required %d)", - dev_desc->version, DEV_USER_VERSION); - res = -EINVAL; + res = dev_user_check_version(dev_desc); + if (res != 0) goto out; - } switch(dev_desc->type) { case TYPE_DISK: @@ -3062,10 +3085,10 @@ static int __init init_scst_user(void) } dev_user_devtype.module = THIS_MODULE; - if (scst_register_virtual_dev_driver(&dev_user_devtype) < 0) { - res = -ENODEV; + + res = scst_register_virtual_dev_driver(&dev_user_devtype); + if (res < 0) goto out_cache; - } res = scst_dev_handler_build_std_proc(&dev_user_devtype); if (res != 0) diff --git a/scst/src/scst_main.c b/scst/src/scst_main.c index 6248d8756..9365d02a0 100644 --- a/scst/src/scst_main.c +++ b/scst/src/scst_main.c @@ -142,7 +142,8 @@ struct scst_dev_type scst_null_devtype = name: "none", }; -int scst_register_target_template(struct scst_tgt_template *vtt) +int __scst_register_target_template(struct scst_tgt_template *vtt, + const char *version) { int res = 0; struct scst_tgt_template *t; @@ -152,6 +153,12 @@ int scst_register_target_template(struct scst_tgt_template *vtt) INIT_LIST_HEAD(&vtt->tgt_list); + if (strcmp(version, SCST_INTERFACE_VERSION) != 0) { + PRINT_ERROR("Incorrect version of target %s", vtt->name); + res = -EINVAL; + goto out_err; + } + if (!vtt->detect) { PRINT_ERROR("Target driver %s doesn't have a " "detect() method.", vtt->name); @@ -742,7 +749,8 @@ out_unblock: return; } -int scst_register_dev_driver(struct scst_dev_type *dev_type) +int __scst_register_dev_driver(struct scst_dev_type *dev_type, + const char *version) { struct scst_dev_type *dt; struct scst_device *dev; @@ -751,6 +759,13 @@ int scst_register_dev_driver(struct scst_dev_type *dev_type) TRACE_ENTRY(); + if (strcmp(version, SCST_INTERFACE_VERSION) != 0) { + PRINT_ERROR("Incorrect version of dev handler %s", + dev_type->name); + res = -EINVAL; + goto out_error; + } + res = scst_dev_handler_check(dev_type); if (res != 0) goto out_error; @@ -872,12 +887,20 @@ out_up: goto out; } -int scst_register_virtual_dev_driver(struct scst_dev_type *dev_type) +int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type, + const char *version) { int res; TRACE_ENTRY(); + if (strcmp(version, SCST_INTERFACE_VERSION) != 0) { + PRINT_ERROR("Incorrect version of virtual dev handler %s", + dev_type->name); + res = -EINVAL; + goto out_err; + } + res = scst_dev_handler_check(dev_type); if (res != 0) goto out_err; @@ -1658,14 +1681,14 @@ static void __exit exit_scst(void) /* * Device Handler Side (i.e. scst_vdisk) */ -EXPORT_SYMBOL(scst_register_dev_driver); +EXPORT_SYMBOL(__scst_register_dev_driver); EXPORT_SYMBOL(scst_unregister_dev_driver); EXPORT_SYMBOL(scst_register); EXPORT_SYMBOL(scst_unregister); EXPORT_SYMBOL(scst_register_virtual_device); EXPORT_SYMBOL(scst_unregister_virtual_device); -EXPORT_SYMBOL(scst_register_virtual_dev_driver); +EXPORT_SYMBOL(__scst_register_virtual_dev_driver); EXPORT_SYMBOL(scst_unregister_virtual_dev_driver); EXPORT_SYMBOL(scst_set_busy); @@ -1683,7 +1706,7 @@ EXPORT_SYMBOL(scst_process_active_cmd); EXPORT_SYMBOL(scst_register_session); EXPORT_SYMBOL(scst_unregister_session); -EXPORT_SYMBOL(scst_register_target_template); +EXPORT_SYMBOL(__scst_register_target_template); EXPORT_SYMBOL(scst_unregister_target_template); EXPORT_SYMBOL(scst_cmd_init_done); diff --git a/usr/fileio/fileio.c b/usr/fileio/fileio.c index a21011322..6ed7b5c48 100644 --- a/usr/fileio/fileio.c +++ b/usr/fileio/fileio.c @@ -374,7 +374,7 @@ int main(int argc, char **argv) } memset(&desc, 0, sizeof(desc)); - desc.version = DEV_USER_VERSION; + desc.version_str = (unsigned long)DEV_USER_VERSION; strncpy(desc.name, dev.name, sizeof(desc.name)-1); desc.name[sizeof(desc.name)-1] = '\0'; desc.type = dev.type;