[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <6dd7d45d8dccacb6d37ad5f6f137413b229d8565.camel@perches.com>
Date: Tue, 24 Aug 2021 12:05:52 -0700
From: Joe Perches <joe@...ches.com>
To: Borislav Petkov <bp@...en8.de>, Len Baker <len.baker@....com>
Cc: Mauro Carvalho Chehab <mchehab@...nel.org>,
Tony Luck <tony.luck@...el.com>,
James Morse <james.morse@....com>,
Robert Richter <rric@...nel.org>,
David Laight <David.Laight@...lab.com>,
Kees Cook <keescook@...omium.org>,
linux-hardening@...r.kernel.org, linux-edac@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v4] EDAC/mc: Prefer strscpy over strcpy
On Tue, 2021-08-24 at 20:26 +0200, Borislav Petkov wrote:
> On Tue, Aug 24, 2021 at 12:28:07PM +0200, Len Baker wrote:
> > This is a task of the KSPP [1] and the main reason is to clean up the
> > proliferation of str*cpy functions in the kernel.
>
> That I understood - you prominently explain where the patches stem from.
>
> What I can't parse is that formulation "previous step". What previous
> step do you mean?
>
> > Yes, you are right. The same discussion happened in the v3 review [2] and
> > I agree with the reasons that Robert Richter exposed. Using the strlen()
> > implementation it is not necessary to check the return code of strcpy and
> > we can assume a silent truncation.
> >
> > [2] https://lore.kernel.org/linux-hardening/YRN+8u59lJ6MWsOL@rric.localdomain/
>
> Ok, looking at the asm, gcc is actually smart enough not to call
> strlen() twice on the same buffer.
>
> But then there's this in the strscpy() kernel-doc comment:
>
> "The destination buffer is always NUL terminated, unless it's
> zero-sized."
>
> so looking at the code, we're merrily decrementing len but nothing's
> checking whether len can become 0. Because if it does, strscpy() will
> do:
>
> if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
> return -E2BIG;
>
> so if p ends up pointing to something which is *not* '\0', strlen() will
> go off into the weeds.
>
> So I don't care if it doesn't look just as nice - it better be correct
> in all cases first.
It's all somehat unnecessary as it seems it's guaranteed not to overflow.
$ git grep -n -w OTHER_LABEL next-20210820
next-20210820:drivers/edac/edac_mc.c:1118: strcpy(p, OTHER_LABEL);
next-20210820:drivers/edac/edac_mc.c:1119: p += strlen(OTHER_LABEL);
next-20210820:include/linux/edac.h:57:#define OTHER_LABEL " or "
next-20210820:include/linux/edac.h:470: char label[(EDAC_MC_LABEL_LEN + 1 + sizeof(OTHER_LABEL)) * EDAC_MAX_LABELS];
Also the array size of char label is too large.
First by 1 * EDAC_MAX_LABELS as sizeof(OTHER_LABEL) is 5 not 4 and
second there can only be EDAC_MAX_LABELS - 1 uses of OTHER_LABEL.
And I would remove the define for OTHER_LABEL and use " or " directly.
Lastly, this could be easier to understand using stpcpy and/or scnprintf.
Powered by blists - more mailing lists