All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Tyrel Datwyler <turtle.in.the.kernel@gmail.com>
Cc: Justin Stitt <justinstitt@google.com>,
	Michael Cyr <mikecyr@linux.ibm.com>,
	"James E.J. Bottomley" <jejb@linux.ibm.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>,
	linux-scsi@vger.kernel.org, target-devel@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH v2] scsi: ibmvscsi_tgt: replace deprecated strncpy with strscpy
Date: Thu, 18 Jan 2024 14:43:16 -0800	[thread overview]
Message-ID: <202401181442.ABC133B203@keescook> (raw)
In-Reply-To: <9f246fbc-b2e4-4585-912d-a5a00ac8cd04@gmail.com>

On Thu, Jan 18, 2024 at 12:21:04PM -0800, Tyrel Datwyler wrote:
> On 12/12/23 13:23, Kees Cook wrote:
> > On Tue, Dec 12, 2023 at 01:20:20AM +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.
> >>
> >> We don't need the NUL-padding behavior that strncpy() provides as vscsi
> >> is NUL-allocated in ibmvscsis_probe() which proceeds to call
> >> ibmvscsis_adapter_info():
> >> |       vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
> >>
> >> ibmvscsis_probe() -> ibmvscsis_handle_crq() -> ibmvscsis_parse_command()
> >> -> ibmvscsis_mad() -> ibmvscsis_process_mad() -> ibmvscsis_adapter_info()
> >>
> >> Following the same idea, `partition_name` is defiend as:
> >> |       static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
> >> ... which is NUL-padded already, meaning strscpy() is the best option.
> >>
> >> Considering the above, a suitable replacement is `strscpy` [2] due to
> >> the fact that it guarantees NUL-termination on the destination buffer
> >> without unnecessarily NUL-padding.
> >>
> >> However, for cap->name and info let's use strscpy_pad as they are
> >> allocated via dma_alloc_coherent():
> >> |       cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
> >> |                                GFP_ATOMIC);
> >> &
> >> |       info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
> >> |                                 GFP_ATOMIC);
> >>
> >> 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
> >> Signed-off-by: Justin Stitt <justinstitt@google.com>
> > 
> > This looks good to me. The only question that I haven't seen an answer
> > to from the maintainers is whether this is a __nonstring or not. It
> > really looks like it should be a C String, so with that assumption:
> 
> To reaffirm the assumption, as I mentioned in my response to v1 these are
> intended to be handled as C strings.

Great; thanks! Are you taking this, or should I carry it in the
hardening tree?

-Kees

> 
> Acked-by: Tyrel Datwyler <tyreld@linux.ibm.com>
> 
> > 
> > Reviewed-by: Kees Cook <keescook@chromium.org>
> > 
> > -Kees
> > 
> 

-- 
Kees Cook

  reply	other threads:[~2024-01-18 22:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-12  1:20 [PATCH v2] scsi: ibmvscsi_tgt: replace deprecated strncpy with strscpy Justin Stitt
2023-12-12 21:23 ` Kees Cook
2024-01-18 20:21   ` Tyrel Datwyler
2024-01-18 22:43     ` Kees Cook [this message]
2024-01-18 23:13       ` Martin K. Petersen
2024-01-24  2:41     ` Martin K. Petersen
2024-01-30  2:27 ` Martin K. Petersen

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=202401181442.ABC133B203@keescook \
    --to=keescook@chromium.org \
    --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=mikecyr@linux.ibm.com \
    --cc=target-devel@vger.kernel.org \
    --cc=turtle.in.the.kernel@gmail.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.