Re: [PATCH v3 1/3] x86/entry/64: Convert SYSRET validation tests to C

From: Brian Gerst
Date: Fri Oct 13 2023 - 09:06:15 EST


On Fri, Oct 13, 2023 at 8:39 AM Nikolay Borisov <nik.borisov@xxxxxxxx> wrote:
>
>
>
> On 12.10.23 г. 1:43 ч., Brian Gerst wrote:
> > Signed-off-by: Brian Gerst <brgerst@xxxxxxxxx>
> > ---
> > arch/x86/entry/common.c | 43 ++++++++++++++++++++++++++-
> > arch/x86/entry/entry_64.S | 53 ++--------------------------------
> > arch/x86/include/asm/syscall.h | 2 +-
> > 3 files changed, 45 insertions(+), 53 deletions(-)
> >
> > diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
> > index 0551bcb197fb..207149a0a9b3 100644
> > --- a/arch/x86/entry/common.c
> > +++ b/arch/x86/entry/common.c
> > @@ -71,7 +71,8 @@ static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr)
> > return false;
> > }
> >
> > -__visible noinstr void do_syscall_64(struct pt_regs *regs, int nr)
> > +/* Returns true to return using SYSRET, or false to use IRET */
> > +__visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)
> > {
> > add_random_kstack_offset();
> > nr = syscall_enter_from_user_mode(regs, nr);
> > @@ -85,6 +86,46 @@ __visible noinstr void do_syscall_64(struct pt_regs *regs, int nr)
> >
> > instrumentation_end();
> > syscall_exit_to_user_mode(regs);
> > +
> > + /*
> > + * Check that the register state is valid for using SYSRET to exit
> > + * to userspace. Otherwise use the slower but fully capable IRET
> > + * exit path.
> > + */
> > +
> > + /* XEN PV guests always use IRET path */
> > + if (cpu_feature_enabled(X86_FEATURE_XENPV))
> > + return false;
> > +
> > + /* SYSRET requires RCX == RIP and R11 == EFLAGS */
> > + if (unlikely(regs->cx != regs->ip || regs->r11 != regs->flags))
> > + return false;
>
>
> Under what conditions do we expect this to not be true since we've come
> via the syscall which adheres to this layout? IOW isn't this always
> going to be true and we can simply eliminate it?

Any syscall that can modify pt_regs, like sigreturn(), exec(), etc.
Also ptrace can change registers.

Brian Gerst