From: David Laight <David.Laight@ACULAB.COM>
To: 'Joe Perches' <joe@perches.com>, Dwaipayan Ray <dwaipayanray1@gmail.com>
Cc: "linux-kernel-mentees@lists.linuxfoundation.org"
<linux-kernel-mentees@lists.linuxfoundation.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [Linux-kernel-mentees] [PATCH] checkpatch: add a new check for strcpy/strlcpy uses
Date: Tue, 5 Jan 2021 10:20:07 +0000 [thread overview]
Message-ID: <d081ca22bbd44f969fa45a8abc543606@AcuMS.aculab.com> (raw)
In-Reply-To: <50cc861121b62b3c1518222f24f679c3f72b868d.camel@perches.com>
From: Joe Perches
> Sent: 05 January 2021 08:44
>
> On Tue, 2021-01-05 at 13:53 +0530, Dwaipayan Ray wrote:
> > strcpy() performs no bounds checking on the destination buffer.
> > This could result in linear overflows beyond the end of the buffer.
> >
> > strlcpy() reads the entire source buffer first. This read
> > may exceed the destination size limit. This can be both inefficient
> > and lead to linear read overflows.
> >
> > The safe replacement to both of these is to use strscpy() instead.
> > Add a new checkpatch warning which alerts the user on finding usage of
> > strcpy() or strlcpy().
>
> I do not believe that strscpy is preferred over strcpy.
>
> When the size of the output buffer is known to be larger
> than the input, strcpy is faster.
>
> There are about 2k uses of strcpy.
> Is there a use where strcpy use actually matters?
> I don't know offhand...
>
> But I believe compilers do not optimize away the uses of strscpy
> to a simple memcpy like they do for strcpy with a const from
>
> strcpy(foo, "bar");
It ought to be possible to convert:
strscpy(foo, "bar", constant_sz)
to a memcpy() within the .h file.
Similarly it should be possible to error
strcpy(foo, "bar")
Unless foo is large enough and "bar" is constant.
After all with a length check
strcpy(foo, "bar")
is actually safer than
strspy(foo, "bar", sizeof foo)
because there is less room for error.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
next prev parent reply other threads:[~2021-01-05 10:22 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-05 8:23 [Linux-kernel-mentees] [PATCH] checkpatch: add a new check for strcpy/strlcpy uses Dwaipayan Ray
2021-01-05 8:44 ` Joe Perches
2021-01-05 8:59 ` Dwaipayan Ray
2021-01-05 9:28 ` [Linux-kernel-mentees] deprecated.rst: deprecated strcpy ? (was: [PATCH] checkpatch: add a new check for strcpy/strlcpy uses) Joe Perches
2021-01-07 21:16 ` Kees Cook
2021-01-08 0:51 ` Joe Perches
2021-01-08 10:05 ` David Laight
2021-01-08 19:48 ` Kees Cook
2021-01-05 10:20 ` David Laight [this message]
-- strict thread matches above, loose matches on Subject: below --
2021-01-04 13:25 [Linux-kernel-mentees] [PATCH] checkpatch: trivial style fixes Dwaipayan Ray
2021-01-04 13:25 ` [Linux-kernel-mentees] [PATCH] checkpatch: add a new check for strcpy/strlcpy uses Dwaipayan Ray
2021-01-05 8:02 ` Lukas Bulwahn
2021-01-05 8:19 ` Dwaipayan Ray
2021-01-05 8:58 ` Lukas Bulwahn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d081ca22bbd44f969fa45a8abc543606@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=dwaipayanray1@gmail.com \
--cc=joe@perches.com \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).