From: Kees Cook <keescook@chromium.org>
To: Justin Stitt <justinstitt@google.com>
Cc: Don Brace <don.brace@microchip.com>,
"James E.J. Bottomley" <jejb@linux.ibm.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>,
storagedev@microchip.com, linux-scsi@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH v2] scsi: hpsa: replace deprecated strncpy
Date: Fri, 27 Oct 2023 09:04:11 -0700 [thread overview]
Message-ID: <202310270901.B49F63CD5@keescook> (raw)
In-Reply-To: <20231026-strncpy-drivers-scsi-hpsa-c-v2-1-2fe2d05122fd@google.com>
On Thu, Oct 26, 2023 at 11:13:41PM +0000, Justin Stitt wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
>
> Instances of strncpy()'ing a string into a buffer and manually
> NUL-terminating followed by sccanf with just "%d" as the format
> specifier can be accomplished by strscpy() and kstrtoint().
>
> strscpy() guarantees NUL-termination on the destination buffer and
> kstrtoint is better way of getting strings turned into ints.
>
> For the last two strncpy() use cases in init_driver_version(), we can
> actually drop this function entirely.
>
> Firstly, we are kmalloc()'ing driver_version. Then, we are calling
> init_driver_version() which memset's it to 0 followed by a strncpy().
> The pattern is 1) allocating memory for a string, 2) setting all bytes
> to NUL, 3) copy bytes from another string + ensure NUL-padded.
>
> For these, we can just stack allocate driver_version and
> old_driver_version. This simplifies the code greatly as we don't have
> any malloc/free or strncpy's.
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Changes in v2:
> - use stack for buffers (thanks Kees)
> - use kstrtoint (thanks Kees)
> - Link to v1: https://lore.kernel.org/r/20231026-strncpy-drivers-scsi-hpsa-c-v1-1-75519d7a191b@google.com
> ---
> Note: build-tested only.
>
> Found with: $ rg "strncpy\("
> ---
> drivers/scsi/hpsa.c | 53 ++++++++++++++++++++---------------------------------
> 1 file changed, 20 insertions(+), 33 deletions(-)
>
> diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
> index af18d20f3079..4d42fbb071cf 100644
> --- a/drivers/scsi/hpsa.c
> +++ b/drivers/scsi/hpsa.c
> @@ -452,18 +452,18 @@ static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
> struct device_attribute *attr,
> const char *buf, size_t count)
> {
> - int status, len;
> + int status;
> struct ctlr_info *h;
> struct Scsi_Host *shost = class_to_shost(dev);
> char tmpbuf[10];
>
> if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
> return -EACCES;
> - len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
> - strncpy(tmpbuf, buf, len);
> - tmpbuf[len] = '\0';
> - if (sscanf(tmpbuf, "%d", &status) != 1)
> +
> + strscpy(tmpbuf, buf, sizeof(tmpbuf));
> + if (kstrtoint(tmpbuf, 0, &status))
I actually meant:
if (kstrtoint(buf, 0, &status))
I don't see any reason for "tmpbuf" at all.
> @@ -7234,25 +7234,15 @@ static int hpsa_controller_hard_reset(struct pci_dev *pdev,
> return 0;
> }
>
> -static void init_driver_version(char *driver_version, int len)
> -{
> - memset(driver_version, 0, len);
> - strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
> -}
> -
> static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
> {
> - char *driver_version;
> int i, size = sizeof(cfgtable->driver_version);
> + char driver_version[sizeof(cfgtable->driver_version)] =
> + HPSA " " HPSA_DRIVER_VERSION;
>
> - driver_version = kmalloc(size, GFP_KERNEL);
> - if (!driver_version)
> - return -ENOMEM;
> -
> - init_driver_version(driver_version, size);
> for (i = 0; i < size; i++)
> writeb(driver_version[i], &cfgtable->driver_version[i]);
> - kfree(driver_version);
> +
> return 0;
> }
>
> @@ -7268,21 +7258,18 @@ static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
> static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
> {
>
> - char *driver_ver, *old_driver_ver;
> - int rc, size = sizeof(cfgtable->driver_version);
> -
> - old_driver_ver = kmalloc_array(2, size, GFP_KERNEL);
> - if (!old_driver_ver)
> - return -ENOMEM;
> - driver_ver = old_driver_ver + size;
> + char driver_ver[sizeof(cfgtable->driver_version)] = "";
> + char old_driver_ver[sizeof(cfgtable->driver_version)] =
> + HPSA " " HPSA_DRIVER_VERSION;
> + int rc;
>
> /* After a reset, the 32 bytes of "driver version" in the cfgtable
> * should have been changed, otherwise we know the reset failed.
> */
> - init_driver_version(old_driver_ver, size);
> read_driver_ver_from_cfgtable(cfgtable, driver_ver);
> - rc = !memcmp(driver_ver, old_driver_ver, size);
> - kfree(old_driver_ver);
> + rc = !memcmp(driver_ver, old_driver_ver,
> + sizeof(cfgtable->driver_version));
> +
> return rc;
> }
> /* This does a hard reset of the controller using PCI power management
These two look good now; thanks!
-Kees
--
Kees Cook
next prev parent reply other threads:[~2023-10-27 16:04 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-26 23:13 [PATCH v2] scsi: hpsa: replace deprecated strncpy Justin Stitt
2023-10-27 16:04 ` Kees Cook [this message]
2023-10-27 20:00 ` Justin Stitt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202310270901.B49F63CD5@keescook \
--to=keescook@chromium.org \
--cc=don.brace@microchip.com \
--cc=jejb@linux.ibm.com \
--cc=justinstitt@google.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=storagedev@microchip.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.