Skip to content

Commit

Permalink
target: Manual replacement of the deprecated strlcpy() with return va…
Browse files Browse the repository at this point in the history
…lues

The strlcpy() reads the entire source buffer first, it is dangerous if
the source buffer lenght is unbounded or possibility non NULL-terminated.
It can lead to linear read overflows, crashes, etc...

As recommended in the deprecated interfaces [1], it should be replaced
by strscpy.

This commit replaces all calls to strlcpy that handle the return values
by the corresponding strscpy calls with new handling of the return
values (as it is quite different between the two functions).

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

Signed-off-by: Romain Perier <romain.perier@gmail.com>
  • Loading branch information
rperier authored and intel-lab-lkp committed Feb 22, 2021
1 parent 08eebe1 commit 8b76bc6
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions drivers/target/target_core_configfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1325,16 +1325,11 @@ 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)) {
/* Strip any newline added from userspace. */
stripped = strstrip(buf);
len = strlen(stripped);
}
if (len > INQUIRY_VENDOR_LEN) {
len = strscpy(buf, page, sizeof(buf));
if (len == -E2BIG) {
pr_err("Emulated T10 Vendor Identification exceeds"
" INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
"\n");
Expand Down Expand Up @@ -1381,16 +1376,11 @@ 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)) {
/* Strip any newline added from userspace. */
stripped = strstrip(buf);
len = strlen(stripped);
}
if (len > INQUIRY_MODEL_LEN) {
len = strscpy(buf, page, sizeof(buf));
if (len == -E2BIG) {
pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
__stringify(INQUIRY_MODEL_LEN)
"\n");
Expand Down Expand Up @@ -1437,16 +1427,11 @@ 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)) {
/* Strip any newline added from userspace. */
stripped = strstrip(buf);
len = strlen(stripped);
}
if (len > INQUIRY_REVISION_LEN) {
len = strscpy(buf, page, sizeof(buf));
if (len == -E2BIG) {
pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
__stringify(INQUIRY_REVISION_LEN)
"\n");
Expand Down

0 comments on commit 8b76bc6

Please sign in to comment.