mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrey Rybainin <ryabinin.a.a@gmail.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	Kees Cook <keescook@chromium.org>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Daniel Micay <danielmicay@gmail.com>,
	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 23:26:22 +0300	[thread overview]
Message-ID: <e1a3afb4-dd45-ac0a-cc07-031757ef3463@gmail.com> (raw)
In-Reply-To: <CA+55aFyH-bL6NhX_jvqS50L4G5Y1QBgYhdiX9d1OH8Ze25EU6w@mail.gmail.com>



On 07/14/2017 10:58 PM, Linus Torvalds wrote:
> On Fri, Jul 14, 2017 at 12:43 PM, Andrey Ryabinin
> <aryabinin@virtuozzo.com> wrote:
>>
>>> yet when I look at the generated code for __ip_map_lookup, I see
>>>
>>>        movl    $32, %edx       #,
>>>        movq    %r13, %rsi      # class,
>>>        leaq    48(%rax), %rdi  #, tmp126
>>>        call    strscpy #
>>>
>>> what's the bug here? Look at that third argume8nt - %rdx. It is
>>> initialized to 32.
>>
>> It's not a compiler bug, it's a bug in our strcpy().
>> Whoever wrote this strcpy() into strscpy() code apparently didn't read carefully
>> enough gcc manual about __builtin_object_size().
>>
>> Summary from https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html :
>>
>>         __builtin_object_size(ptr, type) returns a constant number of bytes from 'ptr' to the end of the object 'ptr'
>>         pointer points to. "type" is an integer constant from 0 to 3. If the least significant bit is clear, objects
>>         are whole variables, if it is set, a closest surrounding subobject is considered the object a pointer points to.
>>         The second bit determines if maximum or minimum of remaining bytes is computed.
>>
>> We have type = 0 in strcpy(), so the least significant bit is clear. So the 'ptr' is considered as a pointer to the whole
>> variable i.e. pointer to struct ip_map ip;
>> And the number of bytes from 'ip.m_class' to the end of the ip object is exactly 32.
>>
>> I suppose that changing the type to 1 should fix this bug.
> 
> Oh, that absolutely needs to be done.
> 
> Because that "strcpy() -> strscpy()" conversion really depends on that
> size being the right size (well, in this case minimal safe size) for
> the actual accesses, exactly because "strscpy()" is perfectly willing
> to write *past* the end of the destination string within that given
> size limit (ie it reads and writes in the same 8-byte chunks).
> 
> So if you have a small target string that is contained in a big
> object, then the "hardened" strcpy() code can actually end up
> overwriting things past the end of the strring, even if the string
> itself were to have fit in the buffer.
> 
> I note that every single use in string.h is buggy, and it worries me
> that __compiletime_object_size() does this too. The only user of that
> seems to be check_copy_size(), and now I'm a bit worried what that bug
> may have hidden.
> 
> I find "hardening" code that adds bugs to be particularly bad and
> ugly, the same way that I absolutely *hate* debugging code that turns
> out to make debugging impossible (we had that with the "better" stack
> tracing code that caused kernel panics to kill the machine entirely
> rather than show the backtrace, and I'm still bitter about it a decade
> after the fact).
> 

A have some more news to make you even more "happier" :)
strcpy() choose to copy 32-bytes instead of smaller 5-bytes because it has one more bug :)

GCC couldn't determine size of class (which is 5-byte string):
	strcpy(ip.m_class, class);

So, p_size = 32 and q_size  = -1, this "if (p_size == (size_t)-1 && q_size == (size_t)-1)" is false
(because of bogus '&&', obviously we should have '||' here)

and since (32 < (size_t)-1) 
	if (strscpy(p, q, p_size < q_size ? p_size : q_size) < 0)

we end up with 32-bytes strscpy().

Enjoy :)


> There is something actively *evil* about it. Daniel, Kees, please jump on this.
> 
> Andrey, thanks for noticing this thing,
> 
>                           Linus
> 

  reply	other threads:[~2017-07-14 20:28 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-13 21:16 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 [this message]
2017-07-14 20:38         ` Daniel Micay
2017-07-14 20:50           ` Linus Torvalds
2017-07-14 21:01             ` Daniel Micay
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=e1a3afb4-dd45-ac0a-cc07-031757ef3463@gmail.com \
    --to=ryabinin.a.a@gmail.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=bfields@fieldses.org \
    --cc=danielmicay@gmail.com \
    --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

Powered by JetHome