From: "Mika Penttilä" <mpenttil@redhat.com>
To: Brian Gerst <brgerst@gmail.com>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Borislav Petkov <bp@alien8.de>, "H . Peter Anvin" <hpa@zytor.com>,
Andy Lutomirski <luto@kernel.org>
Subject: Re: [PATCH 2/6] x86/entry/64: Convert SYSRET validation tests to C
Date: Tue, 18 Jul 2023 17:49:36 +0300 [thread overview]
Message-ID: <f25d597b-a0e5-95db-5538-79893b8d37b2@redhat.com> (raw)
In-Reply-To: <CAMzpN2jzsbUh=2JyCxvOCGJNpycaKPW9WL3JniwrzZB+-mf+tg@mail.gmail.com>
On 18.7.2023 17.25, Brian Gerst wrote:
> On Tue, Jul 18, 2023 at 10:17 AM Mika Penttilä <mpenttil@redhat.com> wrote:
>>
>> Hi,
>>
>>
>> On 18.7.2023 16.44, Brian Gerst wrote:
>>> Signed-off-by: Brian Gerst <brgerst@gmail.com>
>>> ---
>>> arch/x86/entry/common.c | 50 ++++++++++++++++++++++++++++++-
>>> arch/x86/entry/entry_64.S | 55 ++--------------------------------
>>> arch/x86/include/asm/syscall.h | 2 +-
>>> 3 files changed, 52 insertions(+), 55 deletions(-)
>>>
>>> diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
>>> index 6c2826417b33..afe79c3f1c5b 100644
>>> --- a/arch/x86/entry/common.c
>>> +++ b/arch/x86/entry/common.c
>>> @@ -70,8 +70,12 @@ 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)
>>> {
>>> + long rip;
>>> + unsigned int shift_rip;
>>> +
>>> add_random_kstack_offset();
>>> nr = syscall_enter_from_user_mode(regs, nr);
>>>
>>> @@ -84,6 +88,50 @@ __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;
>>> +
>>> + /* CS and SS must match the values set in MSR_STAR */
>>> + if (unlikely(regs->cs != __USER_CS || regs->ss != __USER_DS))
>>> + return false;
>>> +
>>> + /*
>>> + * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
>>> + * in kernel space. This essentially lets the user take over
>>> + * the kernel, since userspace controls RSP.
>>> + *
>>> + * Change top bits to match most significant bit (47th or 56th bit
>>> + * depending on paging mode) in the address.
>>> + */
>>> + shift_rip = (64 - __VIRTUAL_MASK_SHIFT + 1);
>>
>> Should this be:
>>
>> shift_rip = (64 - __VIRTUAL_MASK_SHIFT - 1);
>> ?
>
> I removed a set of parentheses, which switched the sign from -1 to +1.
> I could put it back if that's less confusing.
>
I mean isn't it supposed to be:
shift_rip = (64 - 48) for 4 level, now it's
shift_rip = (64 - 46)
__VIRTUAL_MASK_SHIFT == 47
> Brian Gerst
>
next prev parent reply other threads:[~2023-07-18 14:50 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-18 13:44 [PATCH 0/6] x86: Clean up fast syscall return validation Brian Gerst
2023-07-18 13:44 ` [PATCH 1/6] x86/entry/64: Remove obsolete comment on tracing vs. SYSRET Brian Gerst
2023-07-18 13:44 ` [PATCH 2/6] x86/entry/64: Convert SYSRET validation tests to C Brian Gerst
2023-07-18 14:16 ` Mika Penttilä
2023-07-18 14:25 ` Brian Gerst
2023-07-18 14:49 ` Mika Penttilä [this message]
2023-07-18 15:21 ` Brian Gerst
2023-07-18 15:46 ` Brian Gerst
2023-07-18 13:44 ` [PATCH 3/6] x86/entry/compat: Combine return value test from syscall handler Brian Gerst
2023-07-18 13:44 ` [PATCH 4/6] x86/entry/32: Convert do_fast_syscall_32() to bool return type Brian Gerst
2023-07-18 13:44 ` [PATCH 5/6] x86/entry/32: Remove SEP test for SYSEXIT Brian Gerst
2023-07-18 13:44 ` [PATCH 6/6] x86/entry/32: Clean up syscall fast exit tests Brian Gerst
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=f25d597b-a0e5-95db-5538-79893b8d37b2@redhat.com \
--to=mpenttil@redhat.com \
--cc=bp@alien8.de \
--cc=brgerst@gmail.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@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
all inboxes | Powered by JetHome®