public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Daniel Micay <danielmicay@gmail.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Kees Cook <keescook@chromium.org>,
	Dave Jones <davej@codemonkey.org.uk>,
	Anna Schumaker <schumaker.anna@gmail.com>,
	Linux NFS Mailing List <linux-nfs@vger.kernel.org>,
	linux-fsdevel <linux-fsdevel@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	"J. Bruce Fields" <bfields@fieldses.org>,
	Alexander Potapenko <glider@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	kasan-dev@googlegroups.com
Subject: Re: [GIT PULL] Please pull NFS client changes for Linux 4.13
Date: Fri, 14 Jul 2017 17:01:54 -0400	[thread overview]
Message-ID: <1500066114.3755.5.camel@gmail.com> (raw)
In-Reply-To: <CA+55aFxN4D4MJpNGYS+vbcMgb4KnHArnwsar58vvK_2gSoZQmQ@mail.gmail.com>

On Fri, 2017-07-14 at 13:50 -0700, Linus Torvalds wrote:
> On Fri, Jul 14, 2017 at 1:38 PM, Daniel Micay <danielmicay@gmail.com>
> wrote:
> > 
> > If strscpy treats the count parameter as a *guarantee* of the dest
> > size
> > rather than a limit,
> 
> No, it's a *limit*.
> 
> And by a *limit*, I mean that we know that we can access both source
> and destination within that limit.

FORTIFY_SOURCE needs to be able to pass a limit without implying that
there's a minimum. That's the distinction I was trying to make. It's
wrong to use anything where it's interpreted as a minimum here. Using
__builtin_object_size(ptr, 0) vs. __builtin_object_size(ptr, 1) doesn't
avoid the problem. __builtin_object_size(ptr, 1) returns a maximum among
the possible buffer sizes just like 0. It's just stricter, i.e. catches
intra-object overflow, which isn't desirable for the first take since it
will cause compatibility issues. There's code using memcpy,
copy_to_user, etc. to read / write multiple fields with a pointer to the
first one passed as the source / destination.

> > My initial patch used strlcpy there, because I wasn't aware of
> > strscpy
> > before it was suggested:
> 
> Since I'm looking at this, I note that the "strlcpy()" code is
> complete garbage too, and has that same
> 
>      p_size == (size_t)-1 && q_size == (size_t)-1
> 
> check which is wrong.  Of course, in strlcpy, q_size is never actually
> *used*, so the whole check seems bogus.

That check is only an optimization. __builtin_object_size always returns
a constant, and it's (size_t)-1 when no limit could be found.

The reason q_size isn't used is because it doesn't yet prevent read
overflow. The commit message mentions that among the current limitations
along with __builtin_object_size(ptr, 1).

> But no, strlcpy() is complete garbage, and should never be used. It is
> truly a shit interface, and anybody who uses it is by definition
> buggy.
> 
> Why? Because the return value of "strlcpy()" is defined to be ignoring
> the limit, so you FUNDAMENTALLY must not use that thing on untrusted
> source strings.
> 
> But since the whole *point* of people using it is for untrusted
> sources, it by definition is garbage.
> 
> Ergo: don't use strlcpy(). It's unbelievable crap. It's wrong. There's
> a reason we defined "strscpy()" as the way to do safe copies
> (strncpy(), of course, is broken for both lack of NUL termination
> _and_ for excessive NUL termination when a NUL did exist).

Sure, it doesn't prevent read overflow (but it's not worse than strcpy,
which is the purpose) which is why I said this:

"The fortified string functions should place a limit on reads from the
source. For strcat/strcpy, these could just be a variant of strlcat /
strlcpy using the size limit as a bound on both the source and
destination, with the return value only reporting whether truncation
occurred rather than providing the source length. It would be an easier
API for developers to use too and not that it really matters but it
would be more efficient for the case where truncation is intended."

That's why strscpy was suggested and I switched to that + updated the
commit message to only mention strcat, but it's wrong to use it here
because __builtin_object_size(p, 0) / __builtin_object_size(p, 1) are
only a guaranteed maximum length with no minimum guarantee.

  reply	other threads:[~2017-07-14 21:01 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-13 21:16 [GIT PULL] Please pull NFS client changes for Linux 4.13 Anna Schumaker
2017-07-13 21:43 ` Linus Torvalds
2017-07-14  7:09   ` Christoph Hellwig
2017-07-14 11:33     ` Anna Schumaker
2017-07-14 14:25 ` Dave Jones
2017-07-14 16:36   ` J. Bruce Fields
2017-07-14 19:05   ` Linus Torvalds
2017-07-14 19:43     ` Andrey Ryabinin
2017-07-14 19:58       ` Linus Torvalds
2017-07-14 20:26         ` Andrey Rybainin
2017-07-14 20:38         ` Daniel Micay
2017-07-14 20:50           ` Linus Torvalds
2017-07-14 21:01             ` Daniel Micay [this message]
2017-07-14 21:05               ` Daniel Micay
2017-07-14 20:50           ` Daniel Micay
2017-07-14 23:59         ` Daniel Micay
2017-07-14 19:48     ` Dave Jones
2017-07-16 21:15   ` Dave Jones
2017-07-16 22:57     ` Trond Myklebust
2017-07-17  3:05       ` davej
2017-07-17 19:02         ` Linus Torvalds
2017-07-18 14:20           ` [GIT PULL] Please pull an nfsd bugfix for 4.13 bfields
2017-07-31 15:43           ` [GIT PULL] Please pull NFS client changes for Linux 4.13 davej
2017-08-01  5:35             ` Linus Torvalds
2017-08-01 15:51               ` davej
2017-08-01 17:20                 ` Linus Torvalds
2017-08-01 17:30                   ` Trond Myklebust
2017-08-01 17:50                   ` davej
2017-08-01 17:58                     ` Trond Myklebust
2017-08-01 17:53                   ` Linus Torvalds

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=1500066114.3755.5.camel@gmail.com \
    --to=danielmicay@gmail.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=bfields@fieldses.org \
    --cc=davej@codemonkey.org.uk \
    --cc=dvyukov@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=keescook@chromium.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=schumaker.anna@gmail.com \
    --cc=torvalds@linux-foundation.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