Re: [PATCH] usb: gadget: function: printer: Replace strlcpy with strscpy

From: Kees Cook
Date: Wed Jun 14 2023 - 13:51:07 EST


On Wed, Jun 14, 2023 at 10:12:14AM -0400, Azeem Shaikh wrote:
> On Tue, Jun 13, 2023 at 3:30 PM Kees Cook <keescook@xxxxxxxxxxxx> wrote:
> >
> > On Tue, Jun 13, 2023 at 12:43:41AM +0000, Azeem Shaikh wrote:
> [...]
> > > - result = strlcpy(page, opts->pnp_string, PAGE_SIZE);
> > > - if (result >= PAGE_SIZE) {
> > > + result = strscpy(page, opts->pnp_string, PAGE_SIZE);
> > > + if (result == -E2BIG) {
> >
> > I think "< 1" might be a better test here.
>
> Curious, why "< 1" instead of "< 0"?
>
> > > result = PAGE_SIZE;
> > > } else if (page[result - 1] != '\n' && result + 1 < PAGE_SIZE) {

It's for this case above where "result" may be used in an array index,
and if it's 0 or less, there will be a negative array index (due to the "-
1"). So, here, it needs to be "< 1" instead of the more traditional "< 0".

--
Kees Cook