Message ID | 20210222151231.22572-20-romain.perier@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Manual replacement of all strlcpy in favor of strscpy | expand |
On 2/22/21 8: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. > 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/usb/usbip/stub_main.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/drivers/usb/usbip/stub_main.c b/drivers/usb/usbip/stub_main.c > index 77a5b3f8736a..5bc2c09c0d10 100644 > --- a/drivers/usb/usbip/stub_main.c > +++ b/drivers/usb/usbip/stub_main.c > @@ -167,15 +167,15 @@ static ssize_t match_busid_show(struct device_driver *drv, char *buf) > static ssize_t match_busid_store(struct device_driver *dev, const char *buf, > size_t count) > { > - int len; > + ssize_t len; > char busid[BUSID_SIZE]; > > if (count < 5) > return -EINVAL; > > /* busid needs to include \0 termination */ > - len = strlcpy(busid, buf + 4, BUSID_SIZE); > - if (sizeof(busid) <= len) > + len = strscpy(busid, buf + 4, BUSID_SIZE); > + if (len == -E2BIG) > return -EINVAL; > > if (!strncmp(buf, "add ", 4)) { > Looks good to me. Thank you. Acked-by: Shuah Khan <skhan@linuxfoundation.org> thanks, -- Shuah
Hello! On 22.02.2021 18:12, 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. Possibly? > 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> [...] MBR, Sergei
On Monday, February 22, 2021, Romain Perier <romain.perier@gmail.com> wrote: > 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/usb/usbip/stub_main.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/drivers/usb/usbip/stub_main.c b/drivers/usb/usbip/stub_main.c > index 77a5b3f8736a..5bc2c09c0d10 100644 > --- a/drivers/usb/usbip/stub_main.c > +++ b/drivers/usb/usbip/stub_main.c > @@ -167,15 +167,15 @@ static ssize_t match_busid_show(struct device_driver > *drv, char *buf) > static ssize_t match_busid_store(struct device_driver *dev, const char > *buf, > size_t count) > { > - int len; > + ssize_t len; > char busid[BUSID_SIZE]; > > if (count < 5) > return -EINVAL; > > /* busid needs to include \0 termination */ > - len = strlcpy(busid, buf + 4, BUSID_SIZE); > - if (sizeof(busid) <= len) > + len = strscpy(busid, buf + 4, BUSID_SIZE); > + if (len == -E2BIG) > return -EINVAL; > > I’m wondering why you shadow the initial error. Should not be better if (Len < 0) return Len; ? > if (!strncmp(buf, "add ", 4)) { > >
diff --git a/drivers/usb/usbip/stub_main.c b/drivers/usb/usbip/stub_main.c index 77a5b3f8736a..5bc2c09c0d10 100644 --- a/drivers/usb/usbip/stub_main.c +++ b/drivers/usb/usbip/stub_main.c @@ -167,15 +167,15 @@ static ssize_t match_busid_show(struct device_driver *drv, char *buf) static ssize_t match_busid_store(struct device_driver *dev, const char *buf, size_t count) { - int len; + ssize_t len; char busid[BUSID_SIZE]; if (count < 5) return -EINVAL; /* busid needs to include \0 termination */ - len = strlcpy(busid, buf + 4, BUSID_SIZE); - if (sizeof(busid) <= len) + len = strscpy(busid, buf + 4, BUSID_SIZE); + if (len == -E2BIG) return -EINVAL; if (!strncmp(buf, "add ", 4)) {
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/usb/usbip/stub_main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)