Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

drivers: misc: ti-st: replace deprecated strncpy with strscpy

`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 expect both `kim_data->dev_name` and `kim_gdata->dev_name` to be
NUL-terminated.

`kim_data->dev_name` seems to not require NUL-padding.

`kim_gdata` is already zero-allocated and as such does not require
NUL-padding:
| kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_KERNEL);

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.

Let's also opt to use the more idiomatic strscpy usage of:
strscpy(dest, src, sizeof(dest))

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>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20231003-strncpy-drivers-misc-ti-st-st_kim-c-v2-1-79630447b0a1@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Justin Stitt and committed by
Greg Kroah-Hartman
14388ec0 2801badd

+3 -2
+3 -2
drivers/misc/ti-st/st_kim.c
··· 590 590 { 591 591 struct kim_data_s *kim_data = dev_get_drvdata(dev); 592 592 pr_debug("storing dev name >%s<", buf); 593 - strncpy(kim_data->dev_name, buf, count); 593 + strscpy(kim_data->dev_name, buf, sizeof(kim_data->dev_name)); 594 594 pr_debug("stored dev name >%s<", kim_data->dev_name); 595 595 return count; 596 596 } ··· 751 751 } 752 752 753 753 /* copying platform data */ 754 - strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN); 754 + strscpy(kim_gdata->dev_name, pdata->dev_name, 755 + sizeof(kim_gdata->dev_name)); 755 756 kim_gdata->flow_cntrl = pdata->flow_cntrl; 756 757 kim_gdata->baud_rate = pdata->baud_rate; 757 758 pr_info("sysfs entries created\n");