linux/drivers/rpmsg
Justin Stitt 2a6e483ad0 rpmsg: virtio: 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.

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>
Link: https://lore.kernel.org/r/20231023-strncpy-drivers-rpmsg-virtio_rpmsg_bus-c-v2-1-dc591c36f5ed@google.com
Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>
2023-10-23 13:11:07 -06:00
..
Kconfig rpmsg: Move the rpmsg control device from rpmsg_char to rpmsg_ctrl 2022-03-13 11:49:53 -05:00
Makefile rpmsg: Move the rpmsg control device from rpmsg_char to rpmsg_ctrl 2022-03-13 11:49:53 -05:00
mtk_rpmsg.c rpmsg: mtk_rpmsg: Fix circular locking dependency 2022-06-14 16:41:10 -06:00
qcom_glink_native.c rpmsg: glink: Avoid dereferencing NULL channel 2023-07-18 10:12:26 -06:00
qcom_glink_native.h rpmsg: glink: Fix spelling of peek 2023-02-14 20:28:48 -08:00
qcom_glink_rpm.c rpmsg: qcom_glink_rpm: Convert to platform remove callback returning void 2023-04-05 20:58:32 -07:00
qcom_glink_smem.c rpmsg: glink: Fix spelling of peek 2023-02-14 20:28:48 -08:00
qcom_glink_ssr.c rpmsg: move from strlcpy with unused retval to strscpy 2022-12-28 09:47:41 -06:00
qcom_smd.c rpmsg: qcom_smd: Use qcom_smem_is_available() 2023-07-13 22:18:56 -07:00
rpmsg_char.c rpmsg: char: Add RPMSG GET/SET FLOWCONTROL IOCTL support 2023-07-15 11:35:02 -07:00
rpmsg_char.h rpmsg: char: Export eptdev create and destroy functions 2022-03-13 11:49:53 -05:00
rpmsg_core.c rpmsg: core: Replace deprecated strncpy with strscpy 2023-10-23 11:57:05 -06:00
rpmsg_ctrl.c rpmsg: ctrl: Add lock to rpmsg_ctrldev_remove 2022-12-28 09:54:03 -06:00
rpmsg_internal.h rpmsg: core: Add signal API support 2023-07-15 11:34:49 -07:00
rpmsg_ns.c rpmsg: Replace deprecated strncpy with strscpy_pad 2023-10-23 11:57:57 -06:00
virtio_rpmsg_bus.c rpmsg: virtio: Replace deprecated strncpy with strscpy/_pad 2023-10-23 13:11:07 -06:00