From: Kees Cook <keescook@chromium.org>
To: Yann Droneaud <ydroneaud@ouvaton.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>,
Arnd Bergmann <arnd@arndb.de>, Aleksa Sarai <cyphar@cyphar.com>,
Christian Brauner <christian.brauner@ubuntu.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Dinh Nguyen <dinguyen@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
Alexander Potapenko <glider@google.com>,
Christian Brauner <brauner@kernel.org>,
Stafford Horne <shorne@gmail.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christophe Leroy <christophe.leroy@csgroup.eu>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] uaccess: Add minimum bounds check on kernel buffer size
Date: Tue, 7 Feb 2023 15:28:23 -0800 [thread overview]
Message-ID: <63e2de98.170a0220.81eb8.02cb@mx.google.com> (raw)
In-Reply-To: <9f549c7b-bc72-8037-abfc-fe2f146ff82c@ouvaton.org>
On Tue, Feb 07, 2023 at 10:06:14AM +0100, Yann Droneaud wrote:
> Hi,
>
> Le 06/02/2023 à 21:03, Geert Uytterhoeven a écrit :
>
> > Hi Arnd,
> >
> > On Fri, Feb 3, 2023 at 10:23 PM Arnd Bergmann <arnd@arndb.de> wrote:
> > > On Fri, Feb 3, 2023, at 20:35, Kees Cook wrote:
> > > > --- a/include/linux/uaccess.h
> > > > +++ b/include/linux/uaccess.h
> > > > @@ -329,6 +329,10 @@ copy_struct_from_user(void *dst, size_t ksize,
> > > > const void __user *src,
> > > > size_t size = min(ksize, usize);
> > > > size_t rest = max(ksize, usize) - size;
> > > >
> > > > + /* Double check if ksize is larger than a known object size. */
> > > > + if (WARN_ON_ONCE(ksize > __builtin_object_size(dst, 1)))
> > > > + return -E2BIG;
> > > > +
> > > WARN_ON_ONCE() may be a little expensive since that adds two
> > > comparisons and a static variable to each copy, but it's probably
> > > fine.
> > When seeing this, I was a bit worried about the size increase.
> > Hence I gave it a try on atari_defconfig and ran bloat-o-meter.
> > Surprisingly, there was no size increase at all, as all checks
> > were optimized away.
> >
> > Hence perhaps this can become a compile-time check?
>
> It should be a compile-time check, because one would not want __builtin_object_size(dst, 1) to return -1 if dst' size is not known at compile-time.
Note that it's size_t, so it's actually SIZE_MAX, which is why these
tests will vanish most of the time. i.e. it cannot ever be possible for
the SIZE_MAX case so the entire test is elided.
And when ksize is known at compile time and __bos is not SIZE_MAX, the
result is also known to be either always true or always false, etc.
What's nice here is that when ksize is only run-time known and the buffer
size is compile-time known, we'll keep the test.
--
Kees Cook
next prev parent reply other threads:[~2023-02-07 23:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-03 19:35 [PATCH] uaccess: Add minimum bounds check on kernel buffer size Kees Cook
2023-02-03 21:23 ` Arnd Bergmann
2023-02-03 22:01 ` Arnd Bergmann
2023-02-03 22:27 ` Kees Cook
2023-02-06 20:03 ` Geert Uytterhoeven
2023-02-06 21:32 ` Kees Cook
[not found] ` <CAMuHMdXXSwYYoUMskhcgjF9SVjraZC-UsBT3sN+xkcUAYmJj4Q-2143@mail.gmail.com>
2023-02-07 9:06 ` Yann Droneaud
2023-02-07 23:28 ` Kees Cook [this message]
2023-02-08 5:48 ` Aleksa Sarai
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=63e2de98.170a0220.81eb8.02cb@mx.google.com \
--to=keescook@chromium.org \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=brauner@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=christian.brauner@ubuntu.com \
--cc=christophe.leroy@csgroup.eu \
--cc=cyphar@cyphar.com \
--cc=dinguyen@kernel.org \
--cc=geert@linux-m68k.org \
--cc=glider@google.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=shorne@gmail.com \
--cc=viro@zeniv.linux.org.uk \
--cc=ydroneaud@ouvaton.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.