From: James Bottomley <James.Bottomley@HansenPartnership.com>
To: Helge Deller <deller@gmx.de>, yang.yang29@zte.com.cn
Cc: linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org,
xu.panda@zte.com.cn
Subject: Re: [PATCH linux-next] parisc: use strscpy() to instead of strncpy()
Date: Tue, 27 Dec 2022 17:43:56 -0500 [thread overview]
Message-ID: <ca6e40fff619aa16cb178382f5404d047ed08f3c.camel@HansenPartnership.com> (raw)
In-Reply-To: <279ea596-6a20-0bb8-39c3-67b45d7425a6@gmx.de>
On Tue, 2022-12-27 at 22:38 +0100, Helge Deller wrote:
> Hi James,
>
> On 12/27/22 13:38, James Bottomley wrote:
> > On Fri, 2022-12-23 at 08:55 +0100, Helge Deller wrote:
> > > On 12/23/22 03:40, yang.yang29@zte.com.cn wrote:
> > > > From: Xu Panda <xu.panda@zte.com.cn>
> > > >
> > > > The implementation of strscpy() is more robust and safer.
> > > > That's now the recommended way to copy NUL-terminated strings.
> > >
> > > Thanks for your patch, but....
> > >
> > > > Signed-off-by: Xu Panda <xu.panda@zte.com.cn>
> > > > Signed-off-by: Yang Yang <yang.yang29@zte.com>
> > > > ---
> > > > drivers/parisc/pdc_stable.c | 9 +++------
> > > > 1 file changed, 3 insertions(+), 6 deletions(-)
> > > >
> > > > diff --git a/drivers/parisc/pdc_stable.c
> > > > b/drivers/parisc/pdc_stable.c
> > > > index d6af5726ddf3..403bca0021c5 100644
> > > > --- a/drivers/parisc/pdc_stable.c
> > > > +++ b/drivers/parisc/pdc_stable.c
> > > > @@ -274,8 +274,7 @@ pdcspath_hwpath_write(struct pdcspath_entry
> > > > *entry, const char *buf, size_t coun
> > > >
> > > > /* We'll use a local copy of buf */
> > > > count = min_t(size_t, count, sizeof(in)-1);
> > > > - strncpy(in, buf, count);
> > > > - in[count] = '\0';
> > > > + strscpy(in, buf, count + 1);
> > >
> > > could you resend it somewhat simplified, e.g.
> > > strscpy(in, buf, sizeof(in));
> >
> > I don't think you can: count is the size of buf, if that's <
> > sizeof(in) you've introduced a write beyond end of buffer. In fact
> > sysfs tends to pass pages as buffers, so there's no actual problem,
> > but if that ever changed ...
>
> Huh?... he doesn't change "count", so what's wrong with the latest
> patch?
the array buf[] is actually buf[count], so if count < 64 then
sizeof(buf) < sizeof(in) and you're copying whatever is after buf on
the stack or wherever it comes from. The amount you copy into in[]
truly has to be the smaller of count and sizeof(in). These are file
operations, so you shouldn't rely on buf[] being null terminated
(kernfs ensures it is, but it's a dangerous thing to rely on in the
face of someone trying to exploit a stack smashing attack).
James
next prev parent reply other threads:[~2022-12-27 22:44 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-23 2:40 [PATCH linux-next] parisc: use strscpy() to instead of strncpy() yang.yang29
2022-12-23 7:55 ` Helge Deller
2022-12-27 12:38 ` James Bottomley
2022-12-27 21:38 ` Helge Deller
2022-12-27 22:43 ` James Bottomley [this message]
2022-12-28 1:25 ` yang.yang29
2022-12-28 7:28 ` Helge Deller
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=ca6e40fff619aa16cb178382f5404d047ed08f3c.camel@HansenPartnership.com \
--to=james.bottomley@hansenpartnership.com \
--cc=deller@gmx.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-parisc@vger.kernel.org \
--cc=xu.panda@zte.com.cn \
--cc=yang.yang29@zte.com.cn \
/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