From: Andy Lutomirski <luto@amacapital.net>
To: Thomas Garnier <thgarnie@google.com>
Cc: "Kees Cook" <keescook@chromium.org>,
"Dave Hansen" <dave.hansen@intel.com>,
"Arnd Bergmann" <arnd@arndb.de>,
"René Nyffenegger" <mail@renenyffenegger.ch>,
"Stephen Bates" <stephen.bates@pmcs.com>,
"Jeff Moyer" <jmoyer@redhat.com>,
"Milosz Tanski" <milosz@adfin.com>,
"Linux API" <linux-api@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
"kernel-hardening@lists.openwall.com"
<kernel-hardening@lists.openwall.com>,
"Will Deacon" <will.deacon@arm.com>,
"linux-arm-kernel@lists.infradead.org"
<linux-arm-kernel@lists.infradead.org>,
"linux-s390@vger.kernel.org" <linux-s390@vger.kernel.org>
Subject: Re: [RFC] syscalls: Restore address limit after a syscall
Date: Thu, 9 Feb 2017 18:42:34 -0800 [thread overview]
Message-ID: <CALCETrVS6J2_GPWBoxUFUhjdyce0TBdroA+FjfdNywr9_k6hew@mail.gmail.com> (raw)
In-Reply-To: <CAJcbSZHv7w5jYQ2AZ-g4Kc0mX0jRRbHCKwFKx1EFuOAby1yxXA@mail.gmail.com>
On Thu, Feb 9, 2017 at 3:41 PM, Thomas Garnier <thgarnie@google.com> wrote:
> On Thu, Feb 9, 2017 at 3:05 PM, Andy Lutomirski <luto@amacapital.net> wrote:
>> On Thu, Feb 9, 2017 at 11:31 AM, Kees Cook <keescook@chromium.org> wrote:
>>> On Thu, Feb 9, 2017 at 10:33 AM, Thomas Garnier <thgarnie@google.com> wrote:
>>>> This patch prevents a syscall to modify the address limit of the
>>>> caller. The address limit is kept by the syscall wrapper and restored
>>>> just after the syscall ends.
>>>>
>>>> For example, it would mitigation this bug:
>>>>
>>>> - https://bugs.chromium.org/p/project-zero/issues/detail?id=990
>>>>
>>>> Signed-off-by: Thomas Garnier <thgarnie@google.com>
>>>> ---
>>>> Based on next-20170209
>>>> ---
>>>> include/linux/syscalls.h | 5 ++++-
>>>> 1 file changed, 4 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
>>>> index 91a740f6b884..a1b6a62a9849 100644
>>>> --- a/include/linux/syscalls.h
>>>> +++ b/include/linux/syscalls.h
>>>> @@ -198,7 +198,10 @@ extern struct trace_event_functions exit_syscall_print_funcs;
>>>> asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \
>>>> asmlinkage long SyS##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \
>>>> { \
>>>> - long ret = SYSC##name(__MAP(x,__SC_CAST,__VA_ARGS__)); \
>>>> + long ret; \
>>>> + mm_segment_t fs = get_fs(); \
>>>> + ret = SYSC##name(__MAP(x,__SC_CAST,__VA_ARGS__)); \
>>>> + set_fs(fs); \
>>>> __MAP(x,__SC_TEST,__VA_ARGS__); \
>>>> __PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__)); \
>>>> return ret; \
>>>> --
>>>> 2.11.0.483.g087da7b7c-goog
>>>>
>>>
>>> I have a memory of Andy looking at this before, and there was some
>>> problem with how a bunch of compat code would set fs and then re-call
>>> the syscall... but I can't quite find the conversation. Andy, do you
>>> remember the details?
>>>
>>> This seems like an entirely reasonable thing to enforce for syscalls,
>>> though I'm sure there's a gotcha somewhere. :)
>>
>> This sounds vaguely familiar, but that's about all.
>>
>> Anyway, it seems reasonable that the SyS_foobar wrappers are genuinely
>> only used for syscalls and not for other things, so the code should
>> *work*. That being said, I think there's room for several
>> improvements.
>>
>> 1. Why save the old "fs" value? For that matter, why restore it?
>> IOW, I'd rather see BUG_ON(get_fs() != USER_DS) at the end.
>>
>
> I guess that make sense in the wrapper.
>
>> 2. I'd rather see the mechanism be more general. If we had, effectively:
>>
>> asmlinkage long SyS_foo(...) {
>> sys_foo();
>> verify_pre_usermode_state();
>> }
>>
>> and let verify_pre_usermode_state() potentially do more things, we'd
>> get a more flexible mechanism. On arches like x86_32, we could save a
>> decent amount of code size by moving verify_pre_usermode_state() into
>> prepare_exit_to_usermode(), but that would have to be a per-arch
>> opt-in. x86_64 probably would *not* select this due to the fast path
>> (or it would do it in asm. hmm.).
>>
>
> I will look into that. I like this design better.
>
>> 3. If this thing gets factored out, then arch code can call it for
>> non-syscall entries, too.
>>
>
> Yes, it makes sense.
>
>> 4. Can we make this configurable?
>>
>>
>> For x86, a nice implementation might be:
>>
>> select ARCH_NO_SYSCALL_VERIFY_PRE_USERMODE_STATE
>>
>> ... in prepare_exit_to_usermode():
>>
>> verify_pre_usermode_state(); // right at the beginning
>>
>> ... in the asm syscall fast path:
>>
>> #ifdef CONFIG_VERIFY_PRE_USERMODE_STATE
>> call verify_pre_usermode_staet
>> #endif
>>
>> (or just inline the interesting bit)
>>
>
> So by default it is in the wrapper. If selected, an architecture can
> disable the wrapper put it in the best places. Understood correctly?
Sounds good to me.
Presumably the result should go through -mm. Want to cc: akpm and
linux-arch@ on the next version?
I've also cc'd arm and s390 folks -- those are the other arches that
try to be on top of hardening.
next prev parent reply other threads:[~2017-02-10 2:42 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-09 18:33 [RFC] syscalls: Restore address limit after a syscall Thomas Garnier
[not found] ` <20170209183358.103094-1-thgarnie-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2017-02-09 19:31 ` Kees Cook
2017-02-09 23:05 ` Andy Lutomirski
2017-02-09 23:41 ` Thomas Garnier
2017-02-10 2:42 ` Andy Lutomirski [this message]
2017-02-10 19:22 ` Russell King - ARM Linux
2017-02-10 20:49 ` Kees Cook
[not found] ` <CAGXu5jJf5z5f0O=T4awfF98OR7+kaQaFFJu3kXXa402vq-Mj2Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-02-10 21:49 ` Russell King - ARM Linux
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=CALCETrVS6J2_GPWBoxUFUhjdyce0TBdroA+FjfdNywr9_k6hew@mail.gmail.com \
--to=luto@amacapital.net \
--cc=arnd@arndb.de \
--cc=dave.hansen@intel.com \
--cc=jmoyer@redhat.com \
--cc=keescook@chromium.org \
--cc=kernel-hardening@lists.openwall.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=mail@renenyffenegger.ch \
--cc=milosz@adfin.com \
--cc=stephen.bates@pmcs.com \
--cc=thgarnie@google.com \
--cc=will.deacon@arm.com \
/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;
as well as URLs for NNTP newsgroup(s).