Skip to content

Commit

Permalink
scsi: target: 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 sizeof(dest).

[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]
Reviewed-by: Kees Cook <[email protected]>
Signed-off-by: Martin K. Petersen <[email protected]>
  • Loading branch information
azeemshaikh38 authored and martinkpetersen committed Sep 5, 2023
1 parent d0b0822 commit 5c584fe
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions drivers/target/target_core_configfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1392,16 +1392,16 @@ static ssize_t target_wwn_vendor_id_store(struct config_item *item,
/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
unsigned char buf[INQUIRY_VENDOR_LEN + 2];
char *stripped = NULL;
size_t len;
ssize_t len;
ssize_t ret;

len = strlcpy(buf, page, sizeof(buf));
if (len < sizeof(buf)) {
len = strscpy(buf, page, sizeof(buf));
if (len > 0) {
/* Strip any newline added from userspace. */
stripped = strstrip(buf);
len = strlen(stripped);
}
if (len > INQUIRY_VENDOR_LEN) {
if (len < 0 || len > INQUIRY_VENDOR_LEN) {
pr_err("Emulated T10 Vendor Identification exceeds"
" INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
"\n");
Expand Down Expand Up @@ -1448,16 +1448,16 @@ static ssize_t target_wwn_product_id_store(struct config_item *item,
/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
unsigned char buf[INQUIRY_MODEL_LEN + 2];
char *stripped = NULL;
size_t len;
ssize_t len;
ssize_t ret;

len = strlcpy(buf, page, sizeof(buf));
if (len < sizeof(buf)) {
len = strscpy(buf, page, sizeof(buf));
if (len > 0) {
/* Strip any newline added from userspace. */
stripped = strstrip(buf);
len = strlen(stripped);
}
if (len > INQUIRY_MODEL_LEN) {
if (len < 0 || len > INQUIRY_MODEL_LEN) {
pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
__stringify(INQUIRY_MODEL_LEN)
"\n");
Expand Down Expand Up @@ -1504,16 +1504,16 @@ static ssize_t target_wwn_revision_store(struct config_item *item,
/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
unsigned char buf[INQUIRY_REVISION_LEN + 2];
char *stripped = NULL;
size_t len;
ssize_t len;
ssize_t ret;

len = strlcpy(buf, page, sizeof(buf));
if (len < sizeof(buf)) {
len = strscpy(buf, page, sizeof(buf));
if (len > 0) {
/* Strip any newline added from userspace. */
stripped = strstrip(buf);
len = strlen(stripped);
}
if (len > INQUIRY_REVISION_LEN) {
if (len < 0 || len > INQUIRY_REVISION_LEN) {
pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
__stringify(INQUIRY_REVISION_LEN)
"\n");
Expand Down

0 comments on commit 5c584fe

Please sign in to comment.