x86/ia32: Leave NULL selector values 0~3 as is
PATCH v3 1/1 x86/ia32: Leave NULL selector values 0~3 as is
The first GDT descriptor is reserved as 'NULL descriptor'. As bits 0 and 1 of a segment selector, i.e., the RPL bits, are NOT used to index GDT, selector values 0~3 all point to the NULL descriptor, thus values 0, 1, 2 and 3 are all valid NULL selector values.
When a NULL selector value is to be loaded into a segment register, reload_segments() sets its RPL bits. Later IRET zeros ES, FS, GS, and DS segment registers if any of them is found to have any nonzero NULL selector value. The two operations offset each other to actually effect a nop.
Besides, zeroing of RPL in NULL selector values is an information leak in pre-FRED systems as userspace can spot any interrupt/exception by loading a nonzero NULL selector, and waiting for it to drop to zero. But there is nothing software can do to prevent it before FRED.
ERETU, the only legit instruction to return to userspace from kernel under FRED, by design does NOT zero any segment register to avoid this problem behavior.
As such, leave NULL selector values 0~3 as is.
Do the same on 32-bit kernel as well.
---
Changes since v2:
* No, don't zero non-zero NULL selector values, essentially revert to v1 (Andrew Cooper).
Changes since v1:
* Normalize non-zero NULL selector values to 0 (Eric W. Biederman).
* Apply the same normalization logic in a 32bit kernel (Eric W. Biederman).
--- arch/x86/kernel/signal_32.c | 47 +++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-)
arch/x86/kernel/signal_32.c +35 -12
@@ -33,6 +33,29 @@#include <asm/smap.h> #include <asm/gsseg.h> +/* +* The first GDT descriptor is reserved as 'NULL descriptor'. As bits 0 +* and 1 of a segment selector, i.e., the RPL bits, are NOT used to index +* GDT, selector values 0~3 all point to the NULL descriptor, thus values +* 0, 1, 2 and 3 are all valid NULL selector values. +* +* However IRET zeros ES, FS, GS, and DS segment registers if any of them +* is found to have any nonzero NULL selector value, which can be used by +* userspace in pre-FRED systems to spot any interrupt/exception by loading +* a nonzero NULL selector and waiting for it to drop to zero. Before FRED +* there is nothing we can do to prevent such an information leak. +* +* ERETU, the only legit instruction to return to userspace from kernel +* under FRED, by design does NOT zero any segment register to avoid this +* problem behavior. +* +* As such, leave NULL selector values 0~3 as is. +*/ +static inline u16 usrseg (u16 sel) +{ +return sel <=3 ? sel : sel |3 ; +} +#ifdef CONFIG_IA32_EMULATION #include <asm/unistd_32_ia32.h>
@@ -41,17 +64,17 @@ static inline void reload_segments(struct sigcontext_32 *sc)unsigned int cur;savesegment (gs, cur); -if ((sc->gs |0x03 ) != cur) -load_gs_index (sc->gs |0x03 ); +if (usrseg (sc->gs) != cur) +load_gs_index (usrseg (sc->gs));savesegment (fs, cur); -if ((sc->fs |0x03 ) != cur) -loadsegment (fs, sc->fs |0x03 ); +if (usrseg (sc->fs) != cur) +loadsegment (fs,usrseg (sc->fs));savesegment (ds, cur); -if ((sc->ds |0x03 ) != cur) -loadsegment (ds, sc->ds |0x03 ); +if (usrseg (sc->ds) != cur) +loadsegment (ds,usrseg (sc->ds));savesegment (es, cur); -if ((sc->es |0x03 ) != cur) -loadsegment (es, sc->es |0x03 ); +if (usrseg (sc->es) != cur) +loadsegment (es,usrseg (sc->es)); }#define sigset32_t compat_sigset_t
@@ -113,10 +136,10 @@ static bool ia32_restore_sigcontext(struct pt_regs *regs,*/ reload_segments (&sc);#else -loadsegment (gs, sc.gs); - regs->fs = sc.fs; - regs->es = sc.es; - regs->ds = sc.ds; +loadsegment (gs,usrseg (sc.gs)); + regs->fs =usrseg (sc.fs); + regs->es =usrseg (sc.es); + regs->ds =usrseg (sc.ds);#endif return fpu__restore_sig (compat_ptr (sc.fpstate),1 );
base-commit: 25f324dbc68cd6ff972f5ab3bb07f1bf6200f0e0
Signature · 1 line
2.47.0
Quoted 20 lines · Xin Li (Intel)
The first GDT descriptor is reserved as 'NULL descriptor'. As bits 0 and 1 of a segment selector, i.e., the RPL bits, are NOT used to index GDT, selector values 0~3 all point to the NULL descriptor, thus values 0, 1, 2 and 3 are all valid NULL selector values.
When a NULL selector value is to be loaded into a segment register,
reload_segments()sets its RPL bits. Later IRET zeros ES, FS, GS, and DS segment registers if any of them is found to have any nonzero NULL selector value. The two operations offset each other to actually effect a nop.Besides, zeroing of RPL in NULL selector values is an information leak in pre-FRED systems as userspace can spot any interrupt/exception by loading a nonzero NULL selector, and waiting for it to drop to zero. But there is nothing software can do to prevent it before FRED.
ERETU, the only legit instruction to return to userspace from kernel under FRED, by design does NOT zero any segment register to avoid this problem behavior.
Quoted 5 lines
As such, leave NULL selector values 0~3 as is.
Do the same on 32-bit kernel as well.
Signed-off-by Xin Li (Intel) <[email protected]>
As far as fixing up RPL goes, I think the patch is fine, and probably wants to be taken in roughly this form (new minor points below).
However, the pre-existing code is doing something entirely bizarre, which warrants further investigation, and maybe fixes.
Quoted 8 lines
arch/x86/kernel/signal_32.c +0
-0@@ -33,6 +33,29 @@#include <asm/smap.h> #include <asm/gsseg.h>
arch/x86/kernel/signal_32.c line 45
Quoted 10 lines
arch/x86/kernel/signal_32.c +10
-0+/* +* The first GDT descriptor is reserved as 'NULL descriptor'. As bits 0 +* and 1 of a segment selector, i.e., the RPL bits, are NOT used to index +* GDT, selector values 0~3 all point to the NULL descriptor, thus values +* 0, 1, 2 and 3 are all valid NULL selector values. +* +* However IRET zeros ES, FS, GS, and DS segment registers if any of them +* is found to have any nonzero NULL selector value, which can be used by +* userspace in pre-FRED systems to spot any interrupt/exception by loading +* a nonzero NULL selector and waiting for it to drop to zero.
I know I wrote "drop to zero", but in hindsight, I think "become zero" would be better.
arch/x86/kernel/signal_32.c line 54
Quoted 10 lines
arch/x86/kernel/signal_32.c +9
-0Before FRED +* there is nothing we can do to prevent such an information leak. +* +* ERETU, the only legit instruction to return to userspace from kernel +* under FRED, by design does NOT zero any segment register to avoid this +* problem behavior. +* +* As such, leave NULL selector values 0~3 as is. +*/ +static inline u16 usrseg (u16 sel)
I would suggest naming this fixup_rpl() which is a bit clearer as to its intent.
However, I would also recommend u32 (or at least, unsigned int).
It's absolutely marginal, but you do get better code generation by avoiding u16 specifically where possible.
Quoted 34 lines
arch/x86/kernel/signal_32.c +12
-8+{ +return sel <=3 ? sel : sel |3 ; +} +#ifdef CONFIG_IA32_EMULATION #include <asm/unistd_32_ia32.h> @@ -41,17 +64,17 @@ static inline void reload_segments(struct sigcontext_32 *sc)unsigned int cur;savesegment (gs, cur); -if ((sc->gs |0x03 ) != cur) -load_gs_index (sc->gs |0x03 ); +if (usrseg (sc->gs) != cur) +load_gs_index (usrseg (sc->gs));savesegment (fs, cur); -if ((sc->fs |0x03 ) != cur) -loadsegment (fs, sc->fs |0x03 ); +if (usrseg (sc->fs) != cur) +loadsegment (fs,usrseg (sc->fs));savesegment (ds, cur); -if ((sc->ds |0x03 ) != cur) -loadsegment (ds, sc->ds |0x03 ); +if (usrseg (sc->ds) != cur) +loadsegment (ds,usrseg (sc->ds));savesegment (es, cur); -if ((sc->es |0x03 ) != cur) -loadsegment (es, sc->es |0x03 ); +if (usrseg (sc->es) != cur) +loadsegment (es,usrseg (sc->es)); }#define sigset32_t compat_sigset_t
arch/x86/kernel/signal_32.c static bool ia32_restore_sigcontext(struct pt_regs *regs, · line 137
Quoted 2 lines
arch/x86/kernel/signal_32.c +0
-0@@ -113,10 +136,10 @@ static bool ia32_restore_sigcontext(struct pt_regs *regs,*/ reload_segments (&sc);
This is the singular caller of reload_segments(), and the comment out of context does not match the implementation.
It probably wants inlining just so all the segment juggling is in one place.
arch/x86/kernel/signal_32.c static bool ia32_restore_sigcontext(struct pt_regs *regs, · line 143
Quoted 10 lines
arch/x86/kernel/signal_32.c +4
-4#else -loadsegment (gs, sc.gs); - regs->fs = sc.fs; - regs->es = sc.es; - regs->ds = sc.ds; +loadsegment (gs,usrseg (sc.gs)); + regs->fs =usrseg (sc.fs); + regs->es =usrseg (sc.es); + regs->ds =usrseg (sc.ds);#endif
Why is GS handled specially?
Both, 1) Why is regs->gs the only value that doesn't an RPL-adjusted value, and 2) why do we need to reload it here? We need to keep it as the per_cpu pointer anyway, and we're going to reload on exit-to-user, aren't we?
Also, why do we have such wildly-different behaviours depending on IA32_EMULATION or not?
~Andrew
Quoted 10 lines · Andrew Cooper
Quoted 5 lines · Xin Li (Intel)
As such, leave NULL selector values 0~3 as is.
Do the same on 32-bit kernel as well.
Signed-off-by Xin Li (Intel) <[email protected]>As far as fixing up RPL goes, I think the patch is fine, and probably wants to be taken in roughly this form (new minor points below).
Quoted 7 lines
However, the pre-existing code is doing something entirely bizarre, which warrants further investigation, and maybe fixes.
Quoted 1 line
+ * a nonzero NULL selector and waiting for it to drop to zero.
I know I wrote "drop to zero", but in hindsight, I think "become zero" would be better.
Sure. They both look good to me, but I'm not a native English speaker, so it doesn't count :-P.
Quoted 12 lines
Quoted 11 lines
Before FRED
+ * there is nothing we can do to prevent such an information leak.
+ *
+ * ERETU, the only legit instruction to return to userspace from kernel
+ * under FRED, by design does NOT zero any segment register to avoid this
+ * problem behavior.
+ *
+ * As such, leave NULL selector values 0~3 as is.
+ */
+static inline u16 usrseg(u16 sel)
Quoted 2 lines
I would suggest naming this
fixup_rpl()which is a bit clearer as to its intent.
The rename makes sense.
Quoted 7 lines
However, I would also recommend u32 (or at least, unsigned int).
It's absolutely marginal, but you do get better code generation by avoiding u16 specifically where possible.
Oh, you created a live sample, I appreciate it!
Quoted 41 lines
Quoted 35 lines
arch/x86/kernel/signal_32.c +12
-8+{ +return sel <=3 ? sel : sel |3 ; +} +#ifdef CONFIG_IA32_EMULATION #include <asm/unistd_32_ia32.h> @@ -41,17 +64,17 @@ static inline void reload_segments(struct sigcontext_32 *sc)unsigned int cur;savesegment (gs, cur); -if ((sc->gs |0x03 ) != cur) -load_gs_index (sc->gs |0x03 ); +if (usrseg (sc->gs) != cur) +load_gs_index (usrseg (sc->gs));savesegment (fs, cur); -if ((sc->fs |0x03 ) != cur) -loadsegment (fs, sc->fs |0x03 ); +if (usrseg (sc->fs) != cur) +loadsegment (fs,usrseg (sc->fs));savesegment (ds, cur); -if ((sc->ds |0x03 ) != cur) -loadsegment (ds, sc->ds |0x03 ); +if (usrseg (sc->ds) != cur) +loadsegment (ds,usrseg (sc->ds));savesegment (es, cur); -if ((sc->es |0x03 ) != cur) -loadsegment (es, sc->es |0x03 ); +if (usrseg (sc->es) != cur) +loadsegment (es,usrseg (sc->es)); }#define sigset32_t compat_sigset_tarch/x86/kernel/signal_32.c static bool ia32_restore_sigcontext(struct pt_regs *regs, · line 137
Quoted 2 lines
arch/x86/kernel/signal_32.c +0
-0@@ -113,10 +136,10 @@ static bool ia32_restore_sigcontext(struct pt_regs *regs,*/ reload_segments (&sc);This is the singular caller of
reload_segments(), and the comment out of context does not match the implementation.
Quoted 1 line
It probably wants inlining just so all the segment juggling is in one place.
So move the comment (C&P below) above invoking reload_segments(&sc) into the function definition?
/*
* Reload fs and gs if they have changed in the signal
* handler. This does not handle long fs/gs base changes in
* the handler, but does not clobber them at least in the
* normal case.
*/
Quoted 14 lines
arch/x86/kernel/signal_32.c static bool ia32_restore_sigcontext(struct pt_regs *regs, · line 143
Quoted 11 lines
arch/x86/kernel/signal_32.c +4
-4#else -loadsegment (gs, sc.gs); - regs->fs = sc.fs; - regs->es = sc.es; - regs->ds = sc.ds; +loadsegment (gs,usrseg (sc.gs)); + regs->fs =usrseg (sc.fs); + regs->es =usrseg (sc.es); + regs->ds =usrseg (sc.ds);#endif Why is GS handled specially?
Quoted 7 lines
Both, 1) Why is
regs->gsthe only value that doesn't an RPL-adjusted value, and 2) why do we need to reload it here? We need to keep it as theper_cpupointer anyway, and we're going to reload on exit-to-user, aren't we?Also, why do we have such wildly-different behaviours depending on
IA32_EMULATIONor not?
Maybe because 32-bit exit code skips popping gs?
And 64-bit exit code doesn't load segment registers as 32-bit does.
Thanks!
Xin
H. Peter Anvin
reviewerIn this case you want to keep u16, though, because you are otherwise comparing with the wrong value!