public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: "Darrick J. Wong" <darrick.wong@oracle.com>
Cc: Dave Chinner <david@fromorbit.com>, linux-xfs@vger.kernel.org
Subject: Re: [PATCH] xfs: Use scnprintf() for avoiding potential buffer overflow
Date: Sun, 15 Mar 2020 09:49:01 +0100	[thread overview]
Message-ID: <s5hpnde0zr6.wl-tiwai@suse.de> (raw)
In-Reply-To: <20200313155248.GV1752567@magnolia>

On Fri, 13 Mar 2020 16:52:48 +0100,
Darrick J. Wong wrote:
> 
> On Fri, Mar 13, 2020 at 08:18:42AM +0100, Takashi Iwai wrote:
> > On Fri, 13 Mar 2020 06:00:00 +0100,
> > Dave Chinner wrote:
> > > 
> > > On Thu, Mar 12, 2020 at 03:43:42PM -0700, Darrick J. Wong wrote:
> > > > On Thu, Mar 12, 2020 at 03:27:01PM -0700, Dave Chinner wrote:
> > > > > 
> > > > > I'm annoyed that every time a fundamental failing or technical debt
> > > > > is uncovered in the kernel, nobody takes responsibility to fix the
> > > > > problem completely, for everyone, for ever.
> > > > > 
> > > > > As Thomas said recently: correctness first.
> > > > > 
> > > > > https://lwn.net/ml/linux-kernel/87v9nc63io.fsf@nanos.tec.linutronix.de/
> > > > > 
> > > > > This is not "good enough" - get rid of snprintf() altogether.
> > > > 
> > > > $ git grep snprintf | wc -l
> > > > 8534
> > > > 
> > > > That's somebody's 20 year project... :/
> > > 
> > > Or half an hour with sed.
> > > 
> > > Indeed, not all of them are problematic:
> > > 
> > > $ git grep "= snprintf" |wc -l
> > > 1744
> > > $ git grep "return snprintf"|wc -l
> > > 1306
> > > 
> > > Less than half of them use the return value.
> > > 
> > > Anything that calls snprintf() without checking the return
> > > value (just to prevent formatting overruning the buffer) can be
> > > converted by search and replace because the behaviour is the
> > > same for both functions in this case.
> > > 
> > > Further, code written properly to catch a snprintf overrun will also
> > > correctly pick up scnprintf filling the buffer. However, code that
> > > overruns with snprintf()s return value is much more likely to work
> > > correctly with scnprintf as the calculated buffer length won't
> > > overrun into memory beyond the buffer.
> > > 
> > > And that's likely all of the snprintf() calls dealt with in half an
> > > hour. Now snprintf can be removed.
> > > 
> > > What's more scary is this:
> > > 
> > > $ git grep "+= sprintf"  |wc -l
> > > 1834
> > > 
> > > which is indicative of string formatting iterating over buffers with
> > > no protection against the formatting overwriting the end of the
> > > buffer.  Those are much more dangerous (i.e. potential buffer
> > > overflows) than the snprintf problem being fixed here, and those
> > > will need to be checked and fixed manually to use scnprintf().
> > > That's where the really nasty technical debt lies, not snprintf...
> > 
> > Right, that's how I started looking through the whole tree and
> > submitting patches like this.  I've submitted to per-subsystem patches
> > and many of them have been already covered; after my tons of patches:
> > 
> >   % git grep '+= snprintf' | wc -l
> >   147
> >   
> > The remaining codes are either doing right or it's a user-space code
> > that have no scnprintf() available.  For other snprintf() usages can
> > be converted to scnprintf() easily as you mentioned.
> > 
> > An open question is what we should do for the code that uses
> > snprintf() in a right way.  snprintf() is useful to predict the
> > non-fitted formatted string.  Some warns if such a situation happens.
> > Replacing with scnprintf(), this would never hit, so you'll lose the
> > way of message truncation there.
> > 
> > Maybe we may keep snprintf() but put a checkpatch warning for any new
> > usage?
> > 
> > In anyway, if you prefer, I'll resubmit the patch to convert all
> > snprintf() calls in xfs.
> 
> I already put the first patch in -next, so send a second patch to
> convert the rest, please.

Well, if that's so, I'd rather leave the rest to you guys :)
There are different opinions how to handle the code like

	return snprintf(buf, PAGE_SIZE, ...);

for a simple sysfs output.  Some prefer sprintf() as it's obviously
safe, while others prefer replacing with scnprintf() for a
precaution.  Which to take depends on maintainers, after all.


thanks,

Takashi

  reply	other threads:[~2020-03-15  8:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-11  9:35 [PATCH] xfs: Use scnprintf() for avoiding potential buffer overflow Takashi Iwai
2020-03-11 18:21 ` Darrick J. Wong
2020-03-11 20:00   ` Takashi Iwai
2020-03-11 22:09 ` Dave Chinner
2020-03-12  7:01   ` Takashi Iwai
2020-03-12 22:27     ` Dave Chinner
2020-03-12 22:43       ` Darrick J. Wong
2020-03-13  5:00         ` Dave Chinner
2020-03-13  7:18           ` Takashi Iwai
2020-03-13 15:52             ` Darrick J. Wong
2020-03-15  8:49               ` Takashi Iwai [this message]
2020-03-13  6:52       ` Christoph Hellwig

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=s5hpnde0zr6.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=darrick.wong@oracle.com \
    --cc=david@fromorbit.com \
    --cc=linux-xfs@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