From: Mathieu Poirier <mathieu.poirier@linaro.org>
To: Justin Stitt <justinstitt@google.com>
Cc: Bjorn Andersson <andersson@kernel.org>,
linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH v2] rpmsg: virtio: replace deprecated strncpy with strscpy/_pad
Date: Mon, 23 Oct 2023 13:40:48 -0600 [thread overview]
Message-ID: <ZTbMQC9IhcJR3a/a@p14s> (raw)
In-Reply-To: <20231023-strncpy-drivers-rpmsg-virtio_rpmsg_bus-c-v2-1-dc591c36f5ed@google.com>
On Mon, Oct 23, 2023 at 06:12:28PM +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.
>
> This patch replaces 3 callsites of strncpy().
>
> The first two populate the destination buffer `nsm.name` -- which we
> expect to be NUL-terminated based on their use with format strings.
>
> Firstly, as I understand it, virtio_rpmsg_announce_create() creates an
> rpmsg_ns_msg and sends via:
>
> virtio_rpmsg_bus.c:
> 336: err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
>
> ... which uses:
> virtio_rpmsg_sendto() -> rpmsg_send_offchannel_raw()
>
> ... which copies its data into an rpmsg_hdr `msg` in virtio_rpmsg_bus.c
> 618: memcpy(msg->data, data, len);
>
> This callback is invoked when a message is received from the remote
> processor:
>
> rpmsg_ns.c:
> 30: /* invoked when a name service announcement arrives */
> 31: static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
> 32: void *priv, u32 src)
> 33: {
> 34: struct rpmsg_ns_msg *msg = data;
> ...
> 50: /* don't trust the remote processor for null terminating the name */
> 51: msg->name[RPMSG_NAME_SIZE - 1] = '\0';
>
> ... which leads into the use of `name` within a format string:
> rpmsg_ns.c:
> 57: dev_info(dev, "%sing channel %s addr 0x%x\n",
> 58: rpmsg32_to_cpu(rpdev, msg->flags) & RPMSG_NS_DESTROY ?
> 59: "destroy" : "creat", msg->name, chinfo.dst);
>
> We can also observe that `nsm` is not zero-initialized and as such we
> should maintain the NUL-padding behavior that strncpy() provides:
>
> virtio_rpmsg_bus.c:
> 330: struct rpmsg_ns_msg nsm;
>
> Considering the above, a suitable replacement is `strscpy_pad` due to
> the fact that it guarantees both NUL-termination and NUL-padding on the
> destination buffer.
>
> Now, for the third and final destination buffer rpdev->id.name we can
> just go for strscpy() (not _pad()) as rpdev points to &vch->rpdev:
> | rpdev = &vch->rpdev;
>
> ... and vch is zero-allocated:
> | vch = kzalloc(sizeof(*vch), GFP_KERNEL);
>
> ... this renders any additional NUL-byte assignments (like the ones
> strncpy() or strscpy_pad() does) redundant.
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Changes in v2:
> - update commit msg (thanks Mathieu)
> - Link to v1: https://lore.kernel.org/r/20231021-strncpy-drivers-rpmsg-virtio_rpmsg_bus-c-v1-1-8abb919cbe24@google.com
> ---
> Note: build-tested only.
>
> Found with: $ rg "strncpy\("
> ---
> drivers/rpmsg/virtio_rpmsg_bus.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
I have applied all 3 of your patches.
Thanks,
Mathieu
> diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
> index 905ac7910c98..dc87965f8164 100644
> --- a/drivers/rpmsg/virtio_rpmsg_bus.c
> +++ b/drivers/rpmsg/virtio_rpmsg_bus.c
> @@ -329,7 +329,7 @@ static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
> virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
> struct rpmsg_ns_msg nsm;
>
> - strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> + strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
> nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
> nsm.flags = cpu_to_rpmsg32(rpdev, RPMSG_NS_CREATE);
>
> @@ -353,7 +353,7 @@ static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
> virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
> struct rpmsg_ns_msg nsm;
>
> - strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
> + strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
> nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
> nsm.flags = cpu_to_rpmsg32(rpdev, RPMSG_NS_DESTROY);
>
> @@ -424,7 +424,7 @@ static struct rpmsg_device *__rpmsg_create_channel(struct virtproc_info *vrp,
> */
> rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
>
> - strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
> + strscpy(rpdev->id.name, chinfo->name, sizeof(rpdev->id.name));
>
> rpdev->dev.parent = &vrp->vdev->dev;
> rpdev->dev.release = virtio_rpmsg_release_device;
>
> ---
> base-commit: 9c5d00cb7b6bbc5a7965d9ab7d223b5402d1f02c
> change-id: 20231020-strncpy-drivers-rpmsg-virtio_rpmsg_bus-c-dba15db4e890
>
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
>
next prev parent reply other threads:[~2023-10-23 19:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-23 18:12 [PATCH v2] rpmsg: virtio: replace deprecated strncpy with strscpy/_pad Justin Stitt
2023-10-23 19:40 ` Mathieu Poirier [this message]
2023-10-23 19:52 ` 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=ZTbMQC9IhcJR3a/a@p14s \
--to=mathieu.poirier@linaro.org \
--cc=andersson@kernel.org \
--cc=justinstitt@google.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-remoteproc@vger.kernel.org \
/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.