aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Disseldorp <ddiss@suse.de>2018-12-05 07:18:35 -0500
committerMartin K. Petersen <martin.petersen@oracle.com>2018-12-07 21:54:33 -0500
commitb2da4abf26e859c6c17b49f6f728db0eaab9bc4a (patch)
treed0050f258c6d6b707be82218373d9d96f0d609d6
parent0de263577de5d5e052be5f4f93334e63cc8a7f0b (diff)
scsi: target: consistently null-terminate t10_wwn strings
In preparation for supporting user provided vendor strings, add an extra byte to the vendor, model and revision arrays in struct t10_wwn. This ensures that the full INQUIRY data can be carried in the arrays along with a null-terminator. Change a number of array readers and writers so that they account for explicit null-termination: - The pscsi_set_inquiry_info() and emulate_model_alias_store() codepaths don't currently explicitly null-terminate; fix this. - Existing t10_wwn field dumps use for-loops which step over null-terminators for right-padding. + Use printf with width specifiers instead. Signed-off-by: David Disseldorp <ddiss@suse.de> Reviewed-by: Roman Bolshakov <r.bolshakov@yadro.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
-rw-r--r--drivers/target/target_core_configfs.c16
-rw-r--r--drivers/target/target_core_device.c46
-rw-r--r--drivers/target/target_core_pscsi.c50
-rw-r--r--drivers/target/target_core_spc.c7
-rw-r--r--drivers/target/target_core_stat.c32
-rw-r--r--include/target/target_core_base.h14
6 files changed, 63 insertions, 102 deletions
diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
index 0e8449be5115..aaf2a785e225 100644
--- a/drivers/target/target_core_configfs.c
+++ b/drivers/target/target_core_configfs.c
@@ -620,12 +620,17 @@ static void dev_set_t10_wwn_model_alias(struct se_device *dev)
620 const char *configname; 620 const char *configname;
621 621
622 configname = config_item_name(&dev->dev_group.cg_item); 622 configname = config_item_name(&dev->dev_group.cg_item);
623 if (strlen(configname) >= 16) { 623 if (strlen(configname) >= INQUIRY_MODEL_LEN) {
624 pr_warn("dev[%p]: Backstore name '%s' is too long for " 624 pr_warn("dev[%p]: Backstore name '%s' is too long for "
625 "INQUIRY_MODEL, truncating to 16 bytes\n", dev, 625 "INQUIRY_MODEL, truncating to 15 characters\n", dev,
626 configname); 626 configname);
627 } 627 }
628 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname); 628 /*
629 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
630 * here without potentially breaking existing setups, so continue to
631 * truncate one byte shorter than what can be carried in INQUIRY.
632 */
633 strlcpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
629} 634}
630 635
631static ssize_t emulate_model_alias_store(struct config_item *item, 636static ssize_t emulate_model_alias_store(struct config_item *item,
@@ -647,11 +652,12 @@ static ssize_t emulate_model_alias_store(struct config_item *item,
647 if (ret < 0) 652 if (ret < 0)
648 return ret; 653 return ret;
649 654
655 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
650 if (flag) { 656 if (flag) {
651 dev_set_t10_wwn_model_alias(dev); 657 dev_set_t10_wwn_model_alias(dev);
652 } else { 658 } else {
653 strncpy(&dev->t10_wwn.model[0], 659 strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
654 dev->transport->inquiry_prod, 16); 660 sizeof(dev->t10_wwn.model));
655 } 661 }
656 da->emulate_model_alias = flag; 662 da->emulate_model_alias = flag;
657 return count; 663 return count;
diff --git a/drivers/target/target_core_device.c b/drivers/target/target_core_device.c
index 15805dec697b..7655c426d6a5 100644
--- a/drivers/target/target_core_device.c
+++ b/drivers/target/target_core_device.c
@@ -720,36 +720,17 @@ void core_dev_free_initiator_node_lun_acl(
720static void scsi_dump_inquiry(struct se_device *dev) 720static void scsi_dump_inquiry(struct se_device *dev)
721{ 721{
722 struct t10_wwn *wwn = &dev->t10_wwn; 722 struct t10_wwn *wwn = &dev->t10_wwn;
723 char buf[17]; 723 int device_type = dev->transport->get_device_type(dev);
724 int i, device_type; 724
725 /* 725 /*
726 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer 726 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
727 */ 727 */
728 for (i = 0; i < 8; i++) 728 pr_debug(" Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
729 if (wwn->vendor[i] >= 0x20) 729 wwn->vendor);
730 buf[i] = wwn->vendor[i]; 730 pr_debug(" Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
731 else 731 wwn->model);
732 buf[i] = ' '; 732 pr_debug(" Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
733 buf[i] = '\0'; 733 wwn->revision);
734 pr_debug(" Vendor: %s\n", buf);
735
736 for (i = 0; i < 16; i++)
737 if (wwn->model[i] >= 0x20)
738 buf[i] = wwn->model[i];
739 else
740 buf[i] = ' ';
741 buf[i] = '\0';
742 pr_debug(" Model: %s\n", buf);
743
744 for (i = 0; i < 4; i++)
745 if (wwn->revision[i] >= 0x20)
746 buf[i] = wwn->revision[i];
747 else
748 buf[i] = ' ';
749 buf[i] = '\0';
750 pr_debug(" Revision: %s\n", buf);
751
752 device_type = dev->transport->get_device_type(dev);
753 pr_debug(" Type: %s ", scsi_device_type(device_type)); 734 pr_debug(" Type: %s ", scsi_device_type(device_type));
754} 735}
755 736
@@ -997,11 +978,12 @@ int target_configure_device(struct se_device *dev)
997 * passthrough because this is being provided by the backend LLD. 978 * passthrough because this is being provided by the backend LLD.
998 */ 979 */
999 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) { 980 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
1000 strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8); 981 strlcpy(dev->t10_wwn.vendor, "LIO-ORG",
1001 strncpy(&dev->t10_wwn.model[0], 982 sizeof(dev->t10_wwn.vendor));
1002 dev->transport->inquiry_prod, 16); 983 strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
1003 strncpy(&dev->t10_wwn.revision[0], 984 sizeof(dev->t10_wwn.model));
1004 dev->transport->inquiry_rev, 4); 985 strlcpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
986 sizeof(dev->t10_wwn.revision));
1005 } 987 }
1006 988
1007 scsi_dump_inquiry(dev); 989 scsi_dump_inquiry(dev);
diff --git a/drivers/target/target_core_pscsi.c b/drivers/target/target_core_pscsi.c
index 47d76c862014..c346ad3ee4e0 100644
--- a/drivers/target/target_core_pscsi.c
+++ b/drivers/target/target_core_pscsi.c
@@ -179,20 +179,20 @@ out_free:
179static void 179static void
180pscsi_set_inquiry_info(struct scsi_device *sdev, struct t10_wwn *wwn) 180pscsi_set_inquiry_info(struct scsi_device *sdev, struct t10_wwn *wwn)
181{ 181{
182 unsigned char *buf;
183
184 if (sdev->inquiry_len < INQUIRY_LEN) 182 if (sdev->inquiry_len < INQUIRY_LEN)
185 return; 183 return;
186
187 buf = sdev->inquiry;
188 if (!buf)
189 return;
190 /* 184 /*
191 * Use sdev->inquiry from drivers/scsi/scsi_scan.c:scsi_alloc_sdev() 185 * Use sdev->inquiry data from drivers/scsi/scsi_scan.c:scsi_add_lun()
192 */ 186 */
193 memcpy(&wwn->vendor[0], &buf[8], sizeof(wwn->vendor)); 187 BUILD_BUG_ON(sizeof(wwn->vendor) != INQUIRY_VENDOR_LEN + 1);
194 memcpy(&wwn->model[0], &buf[16], sizeof(wwn->model)); 188 snprintf(wwn->vendor, sizeof(wwn->vendor),
195 memcpy(&wwn->revision[0], &buf[32], sizeof(wwn->revision)); 189 "%." __stringify(INQUIRY_VENDOR_LEN) "s", sdev->vendor);
190 BUILD_BUG_ON(sizeof(wwn->model) != INQUIRY_MODEL_LEN + 1);
191 snprintf(wwn->model, sizeof(wwn->model),
192 "%." __stringify(INQUIRY_MODEL_LEN) "s", sdev->model);
193 BUILD_BUG_ON(sizeof(wwn->revision) != INQUIRY_REVISION_LEN + 1);
194 snprintf(wwn->revision, sizeof(wwn->revision),
195 "%." __stringify(INQUIRY_REVISION_LEN) "s", sdev->rev);
196} 196}
197 197
198static int 198static int
@@ -811,7 +811,6 @@ static ssize_t pscsi_show_configfs_dev_params(struct se_device *dev, char *b)
811 struct scsi_device *sd = pdv->pdv_sd; 811 struct scsi_device *sd = pdv->pdv_sd;
812 unsigned char host_id[16]; 812 unsigned char host_id[16];
813 ssize_t bl; 813 ssize_t bl;
814 int i;
815 814
816 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) 815 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
817 snprintf(host_id, 16, "%d", pdv->pdv_host_id); 816 snprintf(host_id, 16, "%d", pdv->pdv_host_id);
@@ -824,29 +823,12 @@ static ssize_t pscsi_show_configfs_dev_params(struct se_device *dev, char *b)
824 host_id); 823 host_id);
825 824
826 if (sd) { 825 if (sd) {
827 bl += sprintf(b + bl, " "); 826 bl += sprintf(b + bl, " Vendor: %."
828 bl += sprintf(b + bl, "Vendor: "); 827 __stringify(INQUIRY_VENDOR_LEN) "s", sd->vendor);
829 for (i = 0; i < 8; i++) { 828 bl += sprintf(b + bl, " Model: %."
830 if (ISPRINT(sd->vendor[i])) /* printable character? */ 829 __stringify(INQUIRY_MODEL_LEN) "s", sd->model);
831 bl += sprintf(b + bl, "%c", sd->vendor[i]); 830 bl += sprintf(b + bl, " Rev: %."
832 else 831 __stringify(INQUIRY_REVISION_LEN) "s\n", sd->rev);
833 bl += sprintf(b + bl, " ");
834 }
835 bl += sprintf(b + bl, " Model: ");
836 for (i = 0; i < 16; i++) {
837 if (ISPRINT(sd->model[i])) /* printable character ? */
838 bl += sprintf(b + bl, "%c", sd->model[i]);
839 else
840 bl += sprintf(b + bl, " ");
841 }
842 bl += sprintf(b + bl, " Rev: ");
843 for (i = 0; i < 4; i++) {
844 if (ISPRINT(sd->rev[i])) /* printable character ? */
845 bl += sprintf(b + bl, "%c", sd->rev[i]);
846 else
847 bl += sprintf(b + bl, " ");
848 }
849 bl += sprintf(b + bl, "\n");
850 } 832 }
851 return bl; 833 return bl;
852} 834}
diff --git a/drivers/target/target_core_spc.c b/drivers/target/target_core_spc.c
index e01ba2945a97..5d2993db345b 100644
--- a/drivers/target/target_core_spc.c
+++ b/drivers/target/target_core_spc.c
@@ -113,12 +113,13 @@ spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf)
113 * unused bytes at the end of the field (i.e., highest offset) and the 113 * unused bytes at the end of the field (i.e., highest offset) and the
114 * unused bytes shall be filled with ASCII space characters (20h). 114 * unused bytes shall be filled with ASCII space characters (20h).
115 */ 115 */
116 memset(&buf[8], 0x20, 8 + 16 + 4); 116 memset(&buf[8], 0x20,
117 INQUIRY_VENDOR_LEN + INQUIRY_MODEL_LEN + INQUIRY_REVISION_LEN);
117 memcpy(&buf[8], "LIO-ORG", sizeof("LIO-ORG") - 1); 118 memcpy(&buf[8], "LIO-ORG", sizeof("LIO-ORG") - 1);
118 memcpy(&buf[16], dev->t10_wwn.model, 119 memcpy(&buf[16], dev->t10_wwn.model,
119 strnlen(dev->t10_wwn.model, 16)); 120 strnlen(dev->t10_wwn.model, INQUIRY_MODEL_LEN));
120 memcpy(&buf[32], dev->t10_wwn.revision, 121 memcpy(&buf[32], dev->t10_wwn.revision,
121 strnlen(dev->t10_wwn.revision, 4)); 122 strnlen(dev->t10_wwn.revision, INQUIRY_REVISION_LEN));
122 buf[4] = 31; /* Set additional length to 31 */ 123 buf[4] = 31; /* Set additional length to 31 */
123 124
124 return 0; 125 return 0;
diff --git a/drivers/target/target_core_stat.c b/drivers/target/target_core_stat.c
index b80b3e990821..8d9ceedfd455 100644
--- a/drivers/target/target_core_stat.c
+++ b/drivers/target/target_core_stat.c
@@ -246,43 +246,25 @@ static ssize_t target_stat_lu_lu_name_show(struct config_item *item, char *page)
246static ssize_t target_stat_lu_vend_show(struct config_item *item, char *page) 246static ssize_t target_stat_lu_vend_show(struct config_item *item, char *page)
247{ 247{
248 struct se_device *dev = to_stat_lu_dev(item); 248 struct se_device *dev = to_stat_lu_dev(item);
249 int i;
250 char str[sizeof(dev->t10_wwn.vendor)+1];
251 249
252 /* scsiLuVendorId */ 250 return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_VENDOR_LEN)
253 for (i = 0; i < sizeof(dev->t10_wwn.vendor); i++) 251 "s\n", dev->t10_wwn.vendor);
254 str[i] = ISPRINT(dev->t10_wwn.vendor[i]) ?
255 dev->t10_wwn.vendor[i] : ' ';
256 str[i] = '\0';
257 return snprintf(page, PAGE_SIZE, "%s\n", str);
258} 252}
259 253
260static ssize_t target_stat_lu_prod_show(struct config_item *item, char *page) 254static ssize_t target_stat_lu_prod_show(struct config_item *item, char *page)
261{ 255{
262 struct se_device *dev = to_stat_lu_dev(item); 256 struct se_device *dev = to_stat_lu_dev(item);
263 int i;
264 char str[sizeof(dev->t10_wwn.model)+1];
265 257
266 /* scsiLuProductId */ 258 return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_MODEL_LEN)
267 for (i = 0; i < sizeof(dev->t10_wwn.model); i++) 259 "s\n", dev->t10_wwn.model);
268 str[i] = ISPRINT(dev->t10_wwn.model[i]) ?
269 dev->t10_wwn.model[i] : ' ';
270 str[i] = '\0';
271 return snprintf(page, PAGE_SIZE, "%s\n", str);
272} 260}
273 261
274static ssize_t target_stat_lu_rev_show(struct config_item *item, char *page) 262static ssize_t target_stat_lu_rev_show(struct config_item *item, char *page)
275{ 263{
276 struct se_device *dev = to_stat_lu_dev(item); 264 struct se_device *dev = to_stat_lu_dev(item);
277 int i; 265
278 char str[sizeof(dev->t10_wwn.revision)+1]; 266 return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_REVISION_LEN)
279 267 "s\n", dev->t10_wwn.revision);
280 /* scsiLuRevisionId */
281 for (i = 0; i < sizeof(dev->t10_wwn.revision); i++)
282 str[i] = ISPRINT(dev->t10_wwn.revision[i]) ?
283 dev->t10_wwn.revision[i] : ' ';
284 str[i] = '\0';
285 return snprintf(page, PAGE_SIZE, "%s\n", str);
286} 268}
287 269
288static ssize_t target_stat_lu_dev_type_show(struct config_item *item, char *page) 270static ssize_t target_stat_lu_dev_type_show(struct config_item *item, char *page)
diff --git a/include/target/target_core_base.h b/include/target/target_core_base.h
index bcff24d0a264..69b7b955902c 100644
--- a/include/target/target_core_base.h
+++ b/include/target/target_core_base.h
@@ -46,6 +46,10 @@
46/* Used by transport_get_inquiry_vpd_device_ident() */ 46/* Used by transport_get_inquiry_vpd_device_ident() */
47#define INQUIRY_VPD_DEVICE_IDENTIFIER_LEN 254 47#define INQUIRY_VPD_DEVICE_IDENTIFIER_LEN 254
48 48
49#define INQUIRY_VENDOR_LEN 8
50#define INQUIRY_MODEL_LEN 16
51#define INQUIRY_REVISION_LEN 4
52
49/* Attempts before moving from SHORT to LONG */ 53/* Attempts before moving from SHORT to LONG */
50#define PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD 3 54#define PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD 3
51#define PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT 3 /* In milliseconds */ 55#define PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT 3 /* In milliseconds */
@@ -315,9 +319,13 @@ struct t10_vpd {
315}; 319};
316 320
317struct t10_wwn { 321struct t10_wwn {
318 char vendor[8]; 322 /*
319 char model[16]; 323 * SCSI left aligned strings may not be null terminated. +1 to ensure a
320 char revision[4]; 324 * null terminator is always present.
325 */
326 char vendor[INQUIRY_VENDOR_LEN + 1];
327 char model[INQUIRY_MODEL_LEN + 1];
328 char revision[INQUIRY_REVISION_LEN + 1];
321 char unit_serial[INQUIRY_VPD_SERIAL_LEN]; 329 char unit_serial[INQUIRY_VPD_SERIAL_LEN];
322 spinlock_t t10_vpd_lock; 330 spinlock_t t10_vpd_lock;
323 struct se_device *t10_dev; 331 struct se_device *t10_dev;