From: Denys Vlasenko <dvlasenk@redhat.com>
To: Andy Lutomirski <luto@amacapital.net>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Oleg Nesterov <oleg@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
Frederic Weisbecker <fweisbec@gmail.com>, X86 ML <x86@kernel.org>,
Alexei Starovoitov <ast@plumgrid.com>,
Will Drewry <wad@chromium.org>, Kees Cook <keescook@chromium.org>
Subject: Re: [PATCH 17/17] x86: simplify iret stack handling on SYSCALL64 fastpath
Date: Mon, 11 Aug 2014 14:24:15 +0200 [thread overview]
Message-ID: <53E8B5EF.9080209@redhat.com> (raw)
In-Reply-To: <CALCETrVTw5amX-v_C+Bp5Y0B0KbzNKEEq_ZMke-AC7cZopGd9Q@mail.gmail.com>
On 08/11/2014 12:42 AM, Andy Lutomirski wrote:
> On Mon, Aug 11, 2014 at 12:00 AM, Denys Vlasenko <dvlasenk@redhat.com> wrote:
>> On 08/09/2014 12:59 AM, Andy Lutomirski wrote:
>>>> + * When returning through fast path, userspace sees rcx = return address,
>>>> + * r11 = rflags. When returning through iret (e.g. if audit is active),
>>>> + * these registers may contain garbage.
>>>> + * For ptrace we manage to avoid that: when we hit slow path on entry,
>>>> + * we do save rcx and r11 in pt_regs, so ptrace on exit also sees them.
>>>> + * If slow path is entered only on exit, there will be garbage.
>>>
>>> I don't like this. At least the current code puts something
>>> deterministic in there (-1) for the slow path, even though it's wrong
>>> and makes the slow path behave visibly differently from the fast path.
>>>
>>> Leaking uninitialized data here is extra bad, though. Keep in mind
>>> that, when a syscall entry is interrupted before fully setting up
>>> pt_regs, the interrupt frame overlaps task_pt_regs, so it's possible,
>>> depending on the stack slot ordering, for a kernel secret
>>> (kernel_stack?) to end up somewhere in task_pt_regs.
>>
>> It's easy to fix. We jump off fast path to slow path here:
>>
>> movl TI_flags+THREAD_INFO(%rsp,SIZEOF_PTREGS),%edx
>> andl %edi,%edx
>> jnz sysret_careful
>>
>> This is the only use of "sysret_careful" label.
>> Therefore, there we don't need to think about any other scenarios
>> besides "we are returning from syscall here".
>
> I may be missing something here (on vacation, not really testing
> things, no big monitor, etc), but how is this compatible with things
> like rt_sigreturn? rt_sigreturn is call from the fastpath, via a
> stub, and it returns through int_ret_from_syscall. The C part needs
> to modify all the regs, and those regs need to stick, so fixing up rcx
> and r11 after rt_sigreturn can't work.
Code at "sysret_careful" label is only reachable
on fast path return.
We don't go down this code path after rt_sigreturn.
stub_rt_sigreturn manually steers into iret code path instead:
ENTRY(stub_rt_sigreturn)
CFI_STARTPROC
addq $8, %rsp
DEFAULT_FRAME 0
SAVE_EXTRA_REGS
STORE_IRET_FRAME_CS_SS
call sys_rt_sigreturn
movq %rax,RAX(%rsp)
RESTORE_EXTRA_REGS
jmp int_ret_from_sys_call <==== NOTE THIS
So, we don't do any rcx/r11 fixups after sys_rt_sigreturn.
BTW, looks like "SAVE_EXTRA_REGS" here is not necessary:
sys_rt_sigreturn overwrites all registers anyway.
I have its removal on my TODO list.
(It was there before my patch set because stack needed
*resizing to full pt_regs size*; now it does not need that).
>
> On the flip side, fork probably needs to *read* those regs, so the
> fixup needs to happen before fork.
>
> I'm sure it's possible to get this right, but it's complicated and
> error-prone, and I'm not at all convinced it's worth it to save two
> stores on the fast path.
>
> (I'm pretty sure that current kernels have all kinds of bugs in this area.)
>
>>
>> My patch only fixes up segment regs:
>>
>> /* Handle reschedules */
>> /* edx: work, edi: workmask */
>> sysret_careful:
>> + STORE_IRET_FRAME_CS_SS
>> bt $TIF_NEED_RESCHED,%edx
>>
>> pt_regs->rcx and ->r11 aren't initialized.
>>
>> We can fix that e.g. by adding here four more insns:
>>
>> movq RIP(%rsp),%rcx
>> movq %rcx,RCX(%rsp)
>> movq EFLAGS(%rsp),%rcx
>> movq %rcx,R11(%rsp)
>>
>> similar to what my patch did on slow path on entry (see changes at
>> "tracesys" label).
>>
>> Or we can just bite the bullet and save rcx and r11 on fast path
>> (there it will require just two insns). +2 cycles on most CPUs.
>>
>
>
> --Andy
>
next prev parent reply other threads:[~2014-08-11 12:24 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-08 17:44 [PATCH v4 0/17] x86: entry.S optimizations Denys Vlasenko
2014-08-08 17:44 ` [PATCH 01/17] x86: entry_64.S: delete unused code Denys Vlasenko
2014-08-08 17:44 ` [PATCH 02/17] x86: ia32entry.S: fix wrong symbolic constant usage: R11->ARGOFFSET Denys Vlasenko
2014-08-08 17:44 ` [PATCH 03/17] x86: open-code register save/restore in trace_hardirqs thunks Denys Vlasenko
2014-08-08 17:44 ` [PATCH 04/17] x86: entry_64.S: fold SAVE_ARGS_IRQ macro into its sole user Denys Vlasenko
2014-08-08 17:44 ` [PATCH 05/17] x86: entry_64.S: always allocate complete "struct pt_regs" Denys Vlasenko
2014-08-08 17:44 ` [PATCH 06/17] x86: mass removal of ARGOFFSET Denys Vlasenko
2014-08-08 17:44 ` [PATCH 07/17] x86: rename some macros and labels, no code changes Denys Vlasenko
2014-08-08 17:44 ` [PATCH 08/17] x86: add comments about various syscall instructions, " Denys Vlasenko
2014-08-08 17:44 ` [PATCH 09/17] x86: entry_64.S: move save_paranoid and ret_from_fork closer to their users Denys Vlasenko
2014-08-08 17:44 ` [PATCH 10/17] x86: entry_64.S: rename save_paranoid to paranoid_entry, no code changes Denys Vlasenko
2014-08-08 17:44 ` [PATCH 11/17] x86: entry_64.S: fold test_in_nmi macro into its only user Denys Vlasenko
2014-08-08 17:44 ` [PATCH 12/17] x86: get rid of KERNEL_STACK_OFFSET Denys Vlasenko
2014-08-08 17:44 ` [PATCH 13/17] x86: ia32entry.S: fold IA32_ARG_FIXUP macro into its callers Denys Vlasenko
2014-08-08 17:44 ` [PATCH 14/17] x86: ia32entry.S: use mov instead of push/pop where possible Denys Vlasenko
2014-08-08 17:44 ` [PATCH 15/17] x86: code shrink in paranoid_exit Denys Vlasenko
2014-08-08 17:44 ` [PATCH 16/17] x86: entry_64.S: trivial optimization for ENOSYS Denys Vlasenko
2014-08-08 22:48 ` Andy Lutomirski
2014-08-08 17:44 ` [PATCH 17/17] x86: simplify iret stack handling on SYSCALL64 fastpath Denys Vlasenko
2014-08-08 22:59 ` Andy Lutomirski
2014-08-10 15:00 ` Denys Vlasenko
2014-08-10 22:42 ` Andy Lutomirski
2014-08-11 12:24 ` Denys Vlasenko [this message]
2014-08-11 20:06 ` Andy Lutomirski
2014-08-12 9:21 ` Denys Vlasenko
2014-08-13 1:02 ` Andy Lutomirski
2014-08-10 18:47 ` [PATCH 17/17 v2] " Denys Vlasenko
2014-08-09 0:27 ` [PATCH v4 0/17] x86: entry.S optimizations H. Peter Anvin
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=53E8B5EF.9080209@redhat.com \
--to=dvlasenk@redhat.com \
--cc=ast@plumgrid.com \
--cc=fweisbec@gmail.com \
--cc=hpa@zytor.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=oleg@redhat.com \
--cc=torvalds@linux-foundation.org \
--cc=wad@chromium.org \
--cc=x86@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
Powered by JetHome