stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chuck Ebbert <cebbert.lkml@gmail.com>
To: Andy Lutomirski <luto@amacapital.net>
Cc: Thomas Gleixner <tglx@linutronix.de>, X86 ML <x86@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
	Sebastian Lackner <sebastian@fds-team.de>,
	Anish Bhatt <anish@chelsio.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	stable@vger.kernel.org
Subject: Re: [PATCH v2 1/2] x86_64,entry: Filter RFLAGS.NT on entry from userspace
Date: Wed, 1 Oct 2014 09:09:13 -0500	[thread overview]
Message-ID: <20141001090915.16c8b1db@as> (raw)
In-Reply-To: <0e906bdeba3660c9766248d3d7229e78a423ca5b.1412138935.git.luto@amacapital.net>

On Tue, 30 Sep 2014 21:51:27 -0700
Andy Lutomirski <luto@amacapital.net> wrote:

> The NT flag doesn't do anything in long mode other than causing IRET
> to #GP.  Oddly, CPL3 code can still set NT using popf.
> 
> Entry via hardware or software interrupt clears NT automatically, so
> the only relevant entries are fast syscalls.
> 
> If user code causes kernel code to run with NT set, then there's at
> least some (small) chance that it could cause trouble.  For example,
> user code could cause a call to EFI code with NT set, and who knows
> what would happen?  Apparently some games on Wine sometimes do
> this (!), and, if an IRET return happens, they will segfault.  That
> segfault cannot be handled, because signal delivery fails, too.
> 
> This patch programs the CPU to clear NT on entry via SYSCALL (both
> 32-bit and 64-bit, by my reading of the AMD APM), and it clears NT
> in software on entry via SYSENTER.
> 
> To save a few cycles, this borrows a trick from Jan Beulich in Xen:
> it checks whether NT is set before trying to clear it.  As a result,
> it seems to have very little effect on SYSENTER performance on my
> machine.
> 
> Testers beware: on Xen, SYSENTER with NT set turns into a GPF.
> 
> I haven't touched anything on 32-bit kernels.
> 
> The syscall mask change comes from a variant of this patch by Anish
> Bhatt.
> 
> Cc: stable@vger.kernel.org
> Reported-by: Anish Bhatt <anish@chelsio.com>
> Signed-off-by: Andy Lutomirski <luto@amacapital.net>
> ---
>  arch/x86/ia32/ia32entry.S    | 12 ++++++++++++
>  arch/x86/kernel/cpu/common.c |  2 +-
>  2 files changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/ia32/ia32entry.S b/arch/x86/ia32/ia32entry.S
> index 4299eb05023c..44d1dd371454 100644
> --- a/arch/x86/ia32/ia32entry.S
> +++ b/arch/x86/ia32/ia32entry.S
> @@ -151,6 +151,18 @@ ENTRY(ia32_sysenter_target)
>  1:	movl	(%rbp),%ebp
>  	_ASM_EXTABLE(1b,ia32_badarg)
>  	ASM_CLAC
> +
> +	/*
> +	 * Sysenter doesn't filter flags, so we need to clear NT
> +	 * ourselves.  To save a few cycles, we can check whether
> +	 * NT was set instead of doing an unconditional popfq.
> +	 */
> +	testl $X86_EFLAGS_NT,EFLAGS(%rsp)	/* saved EFLAGS match cpu */
> +	jz 1f
> +	pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_FIXED)
> +	popfq_cfi
> +1:
> +

I think you've gone backwards with this version. The earlier one got
some of the performance loss back by not needing to do the "cld" insn.

You should just replace that "cld" (line 146) with

	pushfq_cfi $2
	popfq_cfi

Unfortunately I'm not set up to test that yet. But I did look at
the SDM and can't see a need to preserve any of the flags.

>  	orl     $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP-ARGOFFSET)
>  	testl   $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
>  	CFI_REMEMBER_STATE
> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> index e4ab2b42bd6f..31265580c38a 100644
> --- a/arch/x86/kernel/cpu/common.c
> +++ b/arch/x86/kernel/cpu/common.c
> @@ -1184,7 +1184,7 @@ void syscall_init(void)
>  	/* Flags to clear on syscall */
>  	wrmsrl(MSR_SYSCALL_MASK,
>  	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
> -	       X86_EFLAGS_IOPL|X86_EFLAGS_AC);
> +	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
>  }
>  
>  /*


  parent reply	other threads:[~2014-10-01 14:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1412138935.git.luto@amacapital.net>
2014-10-01  4:51 ` [PATCH v2 1/2] x86_64,entry: Filter RFLAGS.NT on entry from userspace Andy Lutomirski
2014-10-01  5:09   ` Sebastian Lackner
2014-10-01  5:24     ` Andy Lutomirski
2014-10-01 15:19       ` H. Peter Anvin
2014-10-01 14:09   ` Chuck Ebbert [this message]
2014-10-01 14:32     ` Chuck Ebbert
2014-10-01 14:46       ` Andy Lutomirski
2014-10-01 14:56         ` Chuck Ebbert
2014-10-01 15:03           ` Andy Lutomirski
2014-10-01 15:22   ` H. Peter Anvin
2014-10-01 15:26     ` H. Peter Anvin
2014-10-01 15:50       ` Andy Lutomirski
2014-10-01 16:04         ` Andy Lutomirski
2014-10-01 16:17           ` 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=20141001090915.16c8b1db@as \
    --to=cebbert.lkml@gmail.com \
    --cc=anish@chelsio.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mingo@redhat.com \
    --cc=sebastian@fds-team.de \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --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;
as well as URLs for NNTP newsgroup(s).