From: Kees Cook <keescook@chromium.org>
To: Lee Jones <lee@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
"James E.J. Bottomley" <jejb@linux.ibm.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>,
linux-scsi@vger.kernel.org
Subject: Re: [PATCH 06/10] scsi: aha1542: Replace snprintf() with the safer scnprintf() variant
Date: Fri, 9 Feb 2024 23:06:10 -0800 [thread overview]
Message-ID: <202402092303.F5F31A17@keescook> (raw)
In-Reply-To: <20240208084512.3803250-7-lee@kernel.org>
On Thu, Feb 08, 2024 at 08:44:18AM +0000, Lee Jones wrote:
> There is a general misunderstanding amongst engineers that {v}snprintf()
> returns the length of the data *actually* encoded into the destination
> array. However, as per the C99 standard {v}snprintf() really returns
> the length of the data that *would have been* written if there were
> enough space for it. This misunderstanding has led to buffer-overruns
> in the past. It's generally considered safer to use the {v}scnprintf()
> variants in their place (or even sprintf() in simple cases). So let's
> do that.
It may be better to do a global replace of snprintf() where the return
value is not used.
$ git grep '\bsnprintf\b' | grep -v = | wc -l
6400
There's plenty of work to do to remove just the ones taking the result:
$ git grep ' = snprintf\b' | wc -l
764
Regardless,
Reviewed-by: Kees Cook <keescook@chromium.org>
-Kees
>
> Link: https://lwn.net/Articles/69419/
> Link: https://github.com/KSPP/linux/issues/105
> Signed-off-by: Lee Jones <lee@kernel.org>
> ---
> Cc: "James E.J. Bottomley" <jejb@linux.ibm.com>
> Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
> Cc: linux-scsi@vger.kernel.org
> ---
> drivers/scsi/aha1542.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/scsi/aha1542.c b/drivers/scsi/aha1542.c
> index 9503996c63256..b5ec7887801a5 100644
> --- a/drivers/scsi/aha1542.c
> +++ b/drivers/scsi/aha1542.c
> @@ -772,7 +772,7 @@ static struct Scsi_Host *aha1542_hw_init(const struct scsi_host_template *tpnt,
> goto unregister;
>
> if (sh->dma_channel != 0xFF)
> - snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
> + scnprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
> shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
> sh->this_id, base_io, sh->irq, dma_info);
> if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
> --
> 2.43.0.594.gd9cf4e227d-goog
>
>
--
Kees Cook
next prev parent reply other threads:[~2024-02-10 7:06 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-08 8:44 [PATCH 00/10] scsi: Replace {v}snprintf() variants with safer alternatives Lee Jones
2024-02-08 8:44 ` [PATCH 01/10] scsi: 3w-xxxx: Trivial: Remove trailing whitespace Lee Jones
2024-02-10 6:58 ` Kees Cook
2024-02-08 8:44 ` [PATCH 02/10] scsi: 53c700: " Lee Jones
2024-02-10 6:58 ` Kees Cook
2024-02-08 8:44 ` [PATCH 03/10] scsi: NCR5380: Replace snprintf() with the safer scnprintf() variant Lee Jones
2024-02-08 10:22 ` Geert Uytterhoeven
2024-02-08 10:29 ` Lee Jones
2024-02-10 9:32 ` Finn Thain
2024-02-10 12:56 ` James Bottomley
2024-02-19 15:23 ` Lee Jones
2024-02-19 16:25 ` James Bottomley
2024-02-20 8:28 ` Lee Jones
2024-02-19 21:30 ` Kees Cook
2024-02-20 8:24 ` Lee Jones
2024-02-10 7:14 ` Kees Cook
2024-02-08 8:44 ` [PATCH 04/10] scsi: aacraid: linit: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() Lee Jones
2024-02-10 7:00 ` Kees Cook
2024-02-08 8:44 ` [PATCH 05/10] scsi: aacraid: linit: Replace snprintf() with the safer scnprintf() variant Lee Jones
2024-02-10 7:03 ` Kees Cook
2024-02-08 8:44 ` [PATCH 06/10] scsi: aha1542: " Lee Jones
2024-02-10 7:06 ` Kees Cook [this message]
2024-02-08 8:44 ` [PATCH 07/10] scsi: aic7xxx: aicasm: Trivial: Remove trailing whitespace Lee Jones
2024-02-10 6:58 ` Kees Cook
2024-02-08 8:44 ` [PATCH 08/10] scsi: aic7xxx: aicasm: Replace snprintf() with the safer scnprintf() variant Lee Jones
2024-02-10 7:06 ` Kees Cook
2024-02-08 8:44 ` [PATCH 09/10] scsi: aic94xx: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() Lee Jones
2024-02-10 7:13 ` Kees Cook
2024-02-08 8:44 ` [PATCH 10/10] scsi: arcmsr: " Lee Jones
2024-02-10 7:13 ` Kees Cook
2024-02-08 17:40 ` [PATCH 00/10] scsi: Replace {v}snprintf() variants with safer alternatives Bart Van Assche
2024-02-08 17:49 ` Lee Jones
2024-02-10 6:56 ` .mailmap support for removals (was Re: [PATCH 00/10] scsi: Replace {v}snprintf() variants with safer alternatives) Kees Cook
2024-02-10 22:56 ` Joe Perches
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=202402092303.F5F31A17@keescook \
--to=keescook@chromium.org \
--cc=jejb@linux.ibm.com \
--cc=lee@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.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.