Message ID | 20210222151231.22572-21-romain.perier@gmail.com (mailing list archive) |
---|---|
State | Changes Requested |
Headers | show |
Series | Manual replacement of all strlcpy in favor of strscpy | expand |
On 2/22/21 7:12 AM, Romain Perier wrote: > The strlcpy() reads the entire source buffer first, it is dangerous if > the source buffer lenght is unbounded or possibility non NULL-terminated. length > It can lead to linear read overflows, crashes, etc... > That won't happen here since both buffers have the same length and the source is known to be NULL_terminated. So this is another misleading patch. If strlcpy() is deemed so dangerous nowadays, replace it with memcpy(). That is expensive too, but at least it won't create the impression that a non-existing error would ever occur On top of that, the code is handling all MAX_CMDLEN bytes twice anyway later on, so we can for sure afford the extra cost of the memcpy(). Pus, we'll have the added benefit that the unused part of the buffer will be cleared and no longer contain random data. Guenter > 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> > --- > drivers/watchdog/diag288_wdt.c | 12 ++++++++---- > 1 file changed, 8 insertions(+), 4 deletions(-) > > diff --git a/drivers/watchdog/diag288_wdt.c b/drivers/watchdog/diag288_wdt.c > index aafc8d98bf9f..5703f35dd0b7 100644 > --- a/drivers/watchdog/diag288_wdt.c > +++ b/drivers/watchdog/diag288_wdt.c > @@ -111,7 +111,7 @@ static unsigned long wdt_status; > static int wdt_start(struct watchdog_device *dev) > { > char *ebc_cmd; > - size_t len; > + ssize_t len; > int ret; > unsigned int func; > > @@ -126,7 +126,9 @@ static int wdt_start(struct watchdog_device *dev) > clear_bit(DIAG_WDOG_BUSY, &wdt_status); > return -ENOMEM; > } > - len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); > + len = strscpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); > + if (len == -E2BIG) > + return -E2BIG; > ASCEBC(ebc_cmd, MAX_CMDLEN); > EBC_TOUPPER(ebc_cmd, MAX_CMDLEN); > > @@ -163,7 +165,7 @@ static int wdt_stop(struct watchdog_device *dev) > static int wdt_ping(struct watchdog_device *dev) > { > char *ebc_cmd; > - size_t len; > + ssize_t len; > int ret; > unsigned int func; > > @@ -173,7 +175,9 @@ static int wdt_ping(struct watchdog_device *dev) > ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL); > if (!ebc_cmd) > return -ENOMEM; > - len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); > + len = strscpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); > + if (len == -E2BIG) > + return -E2BIG; > ASCEBC(ebc_cmd, MAX_CMDLEN); > EBC_TOUPPER(ebc_cmd, MAX_CMDLEN); > >
diff --git a/drivers/watchdog/diag288_wdt.c b/drivers/watchdog/diag288_wdt.c index aafc8d98bf9f..5703f35dd0b7 100644 --- a/drivers/watchdog/diag288_wdt.c +++ b/drivers/watchdog/diag288_wdt.c @@ -111,7 +111,7 @@ static unsigned long wdt_status; static int wdt_start(struct watchdog_device *dev) { char *ebc_cmd; - size_t len; + ssize_t len; int ret; unsigned int func; @@ -126,7 +126,9 @@ static int wdt_start(struct watchdog_device *dev) clear_bit(DIAG_WDOG_BUSY, &wdt_status); return -ENOMEM; } - len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); + len = strscpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); + if (len == -E2BIG) + return -E2BIG; ASCEBC(ebc_cmd, MAX_CMDLEN); EBC_TOUPPER(ebc_cmd, MAX_CMDLEN); @@ -163,7 +165,7 @@ static int wdt_stop(struct watchdog_device *dev) static int wdt_ping(struct watchdog_device *dev) { char *ebc_cmd; - size_t len; + ssize_t len; int ret; unsigned int func; @@ -173,7 +175,9 @@ static int wdt_ping(struct watchdog_device *dev) ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL); if (!ebc_cmd) return -ENOMEM; - len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); + len = strscpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); + if (len == -E2BIG) + return -E2BIG; ASCEBC(ebc_cmd, MAX_CMDLEN); EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
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> --- drivers/watchdog/diag288_wdt.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)