Skip to content

Commit 4280a0a

Browse files
JustinStittmartinkpetersen
authored andcommitted
scsi: message: fusion: Replace deprecated strncpy() with strscpy_pad()
strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. Since all these structs are copied out to userspace let's keep them NUL-padded by using strscpy_pad() which guarantees NUL-termination of the destination buffer while also providing the NUL-padding behavior that strncpy() has. Let's also opt to use the more idiomatic strscpy() usage of: 'dest, src, sizeof(dest)' in cases where the compiler can determine the size of the destination buffer. Do this for all cases of strscpy...() in this file. To be abundantly sure we don't leak stack data out to user space let's also change a strscpy() to strscpy_pad(). This strscpy() was introduced in commit dbe37c7 ("scsi: message: fusion: Replace all non-returning strlcpy() with strscpy()") Note that since we are creating these structs with a copy_from_user() and modifying fields and then copying back out to the user it is probably OK not to explicitly NUL-pad everything as any data leak is probably just data from the user themselves. If this is too eager, let's opt for strscpy() which is still in the spirit of removing deprecated strncpy() usage treewide. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: KSPP#90 Cc: linux-hardening@vger.kernel.org Cc: Kees Cook <keescook@chromium.org> Signed-off-by: Justin Stitt <justinstitt@google.com> Link: https://lore.kernel.org/r/20230927-strncpy-drivers-message-fusion-mptctl-c-v1-1-bb2eddc1743c@google.com Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 971237b commit 4280a0a

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

drivers/message/fusion/mptctl.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,8 +1328,8 @@ mptctl_getiocinfo (MPT_ADAPTER *ioc, unsigned long arg, unsigned int data_size)
13281328

13291329
/* Set the Version Strings.
13301330
*/
1331-
strncpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);
1332-
karg->driverVersion[MPT_IOCTL_VERSION_LENGTH-1]='\0';
1331+
strscpy_pad(karg->driverVersion, MPT_LINUX_PACKAGE_NAME,
1332+
sizeof(karg->driverVersion));
13331333

13341334
karg->busChangeEvent = 0;
13351335
karg->hostId = ioc->pfacts[port].PortSCSIID;
@@ -1493,10 +1493,8 @@ mptctl_readtest (MPT_ADAPTER *ioc, unsigned long arg)
14931493
#else
14941494
karg.chip_type = ioc->pcidev->device;
14951495
#endif
1496-
strncpy (karg.name, ioc->name, MPT_MAX_NAME);
1497-
karg.name[MPT_MAX_NAME-1]='\0';
1498-
strncpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);
1499-
karg.product[MPT_PRODUCT_LENGTH-1]='\0';
1496+
strscpy_pad(karg.name, ioc->name, sizeof(karg.name));
1497+
strscpy_pad(karg.product, ioc->prod_name, sizeof(karg.product));
15001498

15011499
/* Copy the data from kernel memory to user memory
15021500
*/
@@ -2394,7 +2392,7 @@ mptctl_hp_hostinfo(MPT_ADAPTER *ioc, unsigned long arg, unsigned int data_size)
23942392
cfg.dir = 0; /* read */
23952393
cfg.timeout = 10;
23962394

2397-
strncpy(karg.serial_number, " ", 24);
2395+
strscpy_pad(karg.serial_number, " ", sizeof(karg.serial_number));
23982396
if (mpt_config(ioc, &cfg) == 0) {
23992397
if (cfg.cfghdr.hdr->PageLength > 0) {
24002398
/* Issue the second config page request */
@@ -2408,8 +2406,9 @@ mptctl_hp_hostinfo(MPT_ADAPTER *ioc, unsigned long arg, unsigned int data_size)
24082406
if (mpt_config(ioc, &cfg) == 0) {
24092407
ManufacturingPage0_t *pdata = (ManufacturingPage0_t *) pbuf;
24102408
if (strlen(pdata->BoardTracerNumber) > 1) {
2411-
strscpy(karg.serial_number,
2412-
pdata->BoardTracerNumber, 24);
2409+
strscpy_pad(karg.serial_number,
2410+
pdata->BoardTracerNumber,
2411+
sizeof(karg.serial_number));
24132412
}
24142413
}
24152414
dma_free_coherent(&ioc->pcidev->dev,
@@ -2456,7 +2455,7 @@ mptctl_hp_hostinfo(MPT_ADAPTER *ioc, unsigned long arg, unsigned int data_size)
24562455
}
24572456
}
24582457

2459-
/*
2458+
/*
24602459
* Gather ISTWI(Industry Standard Two Wire Interface) Data
24612460
*/
24622461
if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL) {

0 commit comments

Comments
 (0)