Skip to content

Commit

Permalink
usb: gadget: function: printer: Replace strlcpy with strscpy
Browse files Browse the repository at this point in the history
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].
In an effort to remove strlcpy() completely [2], replace
strlcpy() here with strscpy().

Direct replacement is safe here since return value of -errno
is used to check for truncation instead of PAGE_SIZE.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
[2] KSPP#89

Signed-off-by: Azeem Shaikh <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
azeemshaikh38 authored and gregkh committed Jun 19, 2023
1 parent 8be558d commit ee400a1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drivers/usb/gadget/function/f_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,8 @@ static ssize_t f_printer_opts_pnp_string_show(struct config_item *item,
if (!opts->pnp_string)
goto unlock;

result = strlcpy(page, opts->pnp_string, PAGE_SIZE);
if (result >= PAGE_SIZE) {
result = strscpy(page, opts->pnp_string, PAGE_SIZE);
if (result < 1) {
result = PAGE_SIZE;
} else if (page[result - 1] != '\n' && result + 1 < PAGE_SIZE) {
page[result++] = '\n';
Expand Down

0 comments on commit ee400a1

Please sign in to comment.