Commit 40b2519d authored by Kees Cook's avatar Kees Cook

samples: Replace strlcpy() with strscpy()

strlcpy() reads the entire source buffer first. This read may exceed
the destination size limit. This is both inefficient and can lead
to linear read overflows if a source string is not NUL-terminated[1].
Additionally, it returns the size of the source string, not the
resulting size of the destination string. In an effort to remove strlcpy()
completely[2], replace strlcpy() here with strscpy().

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [1]
Link: https://github.com/KSPP/linux/issues/89 [2]
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Valentin Schneider <vschneid@redhat.com>
Cc: "Steven Rostedt (Google)" <rostedt@goodmis.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Geliang Tang <geliang.tang@suse.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Acked-by: default avatar"Steven Rostedt (Google)" <rostedt@goodmis.org>
Link: https://lore.kernel.org/r/20231116191510.work.550-kees@kernel.orgSigned-off-by: default avatarKees Cook <keescook@chromium.org>
parent cb6d2fd3
...@@ -305,7 +305,7 @@ TRACE_EVENT(foo_bar, ...@@ -305,7 +305,7 @@ TRACE_EVENT(foo_bar,
), ),
TP_fast_assign( TP_fast_assign(
strlcpy(__entry->foo, foo, 10); strscpy(__entry->foo, foo, 10);
__entry->bar = bar; __entry->bar = bar;
memcpy(__get_dynamic_array(list), lst, memcpy(__get_dynamic_array(list), lst,
__length_of(lst) * sizeof(int)); __length_of(lst) * sizeof(int));
......
...@@ -291,8 +291,8 @@ static int skeleton_querycap(struct file *file, void *priv, ...@@ -291,8 +291,8 @@ static int skeleton_querycap(struct file *file, void *priv,
{ {
struct skeleton *skel = video_drvdata(file); struct skeleton *skel = video_drvdata(file);
strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
strlcpy(cap->card, "V4L2 PCI Skeleton", sizeof(cap->card)); strscpy(cap->card, "V4L2 PCI Skeleton", sizeof(cap->card));
snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
pci_name(skel->pdev)); pci_name(skel->pdev));
return 0; return 0;
...@@ -597,11 +597,11 @@ static int skeleton_enum_input(struct file *file, void *priv, ...@@ -597,11 +597,11 @@ static int skeleton_enum_input(struct file *file, void *priv,
i->type = V4L2_INPUT_TYPE_CAMERA; i->type = V4L2_INPUT_TYPE_CAMERA;
if (i->index == 0) { if (i->index == 0) {
i->std = SKEL_TVNORMS; i->std = SKEL_TVNORMS;
strlcpy(i->name, "S-Video", sizeof(i->name)); strscpy(i->name, "S-Video", sizeof(i->name));
i->capabilities = V4L2_IN_CAP_STD; i->capabilities = V4L2_IN_CAP_STD;
} else { } else {
i->std = 0; i->std = 0;
strlcpy(i->name, "HDMI", sizeof(i->name)); strscpy(i->name, "HDMI", sizeof(i->name));
i->capabilities = V4L2_IN_CAP_DV_TIMINGS; i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
} }
return 0; return 0;
...@@ -845,7 +845,7 @@ static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent) ...@@ -845,7 +845,7 @@ static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
/* Initialize the video_device structure */ /* Initialize the video_device structure */
vdev = &skel->vdev; vdev = &skel->vdev;
strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name)); strscpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
/* /*
* There is nothing to clean up, so release is set to an empty release * There is nothing to clean up, so release is set to an empty release
* function. The release callback must be non-NULL. * function. The release callback must be non-NULL.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment