x86/entry/64: Add an LFENCE after SAVE_AND_SET_GSBASE
Subsystems (2)
Borislav Petkov
maintainerPATCH 1/1 x86/entry/64: Add an LFENCE after SAVE_AND_SET_GSBASE
From: Borislav Petkov <[email protected]>
The FSGSBASE macro to swap current GSBASE with the kernel GSBASE probably had a speculation-stopping MSR write at some point but not anymore.
Therefore, a fence is needed to stop speculation. Add it.
--- arch/x86/entry/entry_64.S | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/entry/entry_64.S +1 -3
@@ -839,11 +839,9 @@ SYM_CODE_START_LOCAL(paranoid_entry) * Read the current GSBASE and store it in %rbx unconditionally, * retrieve and set the current CPUs kernel GSBASE. The stored value * has to be restored in paranoid_exit unconditionally. - * - * The MSR write ensures that no subsequent load is based on a - * mispredicted GSBASE. No extra FENCE required. */ SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx + FENCE_SWAPGS_KERNEL_ENTRY ret .Lparanoid_entry_checkgs:
Signature · 1 line
2.21.0
Quoted 8 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
The FSGSBASE macro to swap current GSBASE with the kernel GSBASE probably had a speculation-stopping MSR write at some point but not anymore.
No, the macro has not any MSR write ever before.
arch/x86/entry/entry_64.S SYM_CODE_START_LOCAL(paranoid_entry) · line 844
Quoted 11 lines
arch/x86/entry/entry_64.S +0
-3@@ -839,11 +839,9 @@ SYM_CODE_START_LOCAL(paranoid_entry) * Read the current GSBASE and store it in %rbx unconditionally, * retrieve and set the current CPUs kernel GSBASE. The stored value * has to be restored in paranoid_exit unconditionally. - * - * The MSR write ensures that no subsequent load is based on a - * mispredicted GSBASE. No extra FENCE required.
Thomas looks to add this text when he picked v7 [1].
FWIW, we added our text from v8, after reviewed the speculation impact on this series:
This unconditional write of GS base ensures no subsequent load based on a mispredicted GS base.
I think somehow the “MSR write” made confusion. Our conclusion was the same as Thomas' that no FENCE is needed here.
Thanks,
Chang
[1] https://lore.kernel.org/lkml/[email protected]/t/#m45f380a23231b87498b40b708f0401147f3b0278
Quoted 9 lines
arch/x86/entry/entry_64.S +1
-0*/ SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx + FENCE_SWAPGS_KERNEL_ENTRY ret.Lparanoid_entry_checkgs:
Signature · 1 line
2.21.0
Borislav Petkov
maintainerPATCH 1/1 x86/entry/64: Correct the comment over SAVE_AND_SET_GSBASE
Quoted 2 lines · Chang Seok
I think somehow the “MSR write” made confusion. Our conclusion was the same as Thomas' that no FENCE is needed here.
Ok, here's v2 with a corrected comment:
---
From: Borislav Petkov <[email protected]>
Add the proper explanation why an LFENCE is not needed in the FSGSBASE case.
--- arch/x86/entry/entry_64.S | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64.S +3 -2
@@ -840,8 +840,9 @@ SYM_CODE_START_LOCAL(paranoid_entry) * retrieve and set the current CPUs kernel GSBASE. The stored value * has to be restored in paranoid_exit unconditionally. * - * The MSR write ensures that no subsequent load is based on a - * mispredicted GSBASE. No extra FENCE required. + * The unconditional write to GS base below ensures that no subsequent + * loads based on a mispredicted GS base can happen, therefore no LFENCE + * is needed here. */ SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx ret
Signature · 9 lines
2.21.0
Thx.
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette[tip: x86/fsgsbase] x86/entry/64: Correct the comment over SAVE_AND_SET_GSBASE
The following commit has been merged into the x86/fsgsbase branch of tip:
The following commit has been merged into the x86/fsgsbase branch of tip:
Commit-ID: 0b2c605fa4ee3117c00b97b7af67791576b28f88 Gitweb: https://git.kernel.org/tip/0b2c605fa4ee3117c00b97b7af67791576b28f88 Author: Borislav Petkov <[email protected]> AuthorDate: Thu, 20 Aug 2020 11:10:15 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 24 Aug 2020 10:23:40 +02:00
x86/entry/64: Correct the comment over SAVE_AND_SET_GSBASE
Add the proper explanation why an LFENCE is not needed in the FSGSBASE case.
--- arch/x86/entry/entry_64.S | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64.S +3 -2
@@ -840,8 +840,9 @@ SYM_CODE_START_LOCAL(paranoid_entry) * retrieve and set the current CPUs kernel GSBASE. The stored value * has to be restored in paranoid_exit unconditionally. * - * The MSR write ensures that no subsequent load is based on a - * mispredicted GSBASE. No extra FENCE required. + * The unconditional write to GS base below ensures that no subsequent + * loads based on a mispredicted GS base can happen, therefore no LFENCE + * is needed here. */ SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx ret