2019-02-25 12:54:08

by Peter Zijlstra

[permalink] [raw]
Subject: [PATCH 2/6] x86/ia32: Fix ia32_restore_sigcontext AC leak

Don't call load_gs_index() with AC set; delay the segment setting
until after the AC section.

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
---
arch/x86/ia32/ia32_signal.c | 29 +++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)

--- a/arch/x86/ia32/ia32_signal.c
+++ b/arch/x86/ia32/ia32_signal.c
@@ -60,17 +60,21 @@
regs->seg = GET_SEG(seg) | 3; \
} while (0)

-#define RELOAD_SEG(seg) { \
- unsigned int pre = GET_SEG(seg); \
- unsigned int cur = get_user_seg(seg); \
- pre |= 3; \
- if (pre != cur) \
- set_user_seg(seg, pre); \
+#define LOAD_SEG(seg) { \
+ pre_##seg = 3 | GET_SEG(seg); \
+ cur_##seg = get_user_seg(seg); \
+}
+
+#define RELOAD_SEG(seg) { \
+ if (pre_##seg != cur_##seg) \
+ set_user_seg(seg, pre_##seg); \
}

static int ia32_restore_sigcontext(struct pt_regs *regs,
struct sigcontext_32 __user *sc)
{
+ u16 pre_gs, pre_fs, pre_ds, pre_es;
+ u16 cur_gs, cur_fs, cur_ds, cur_es;
unsigned int tmpflags, err = 0;
void __user *buf;
u32 tmp;
@@ -85,10 +89,10 @@ static int ia32_restore_sigcontext(struc
* the handler, but does not clobber them at least in the
* normal case.
*/
- RELOAD_SEG(gs);
- RELOAD_SEG(fs);
- RELOAD_SEG(ds);
- RELOAD_SEG(es);
+ LOAD_SEG(gs);
+ LOAD_SEG(fs);
+ LOAD_SEG(ds);
+ LOAD_SEG(es);

COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
COPY(dx); COPY(cx); COPY(ip); COPY(ax);
@@ -106,6 +110,11 @@ static int ia32_restore_sigcontext(struc
buf = compat_ptr(tmp);
} get_user_catch(err);

+ RELOAD_SEG(gs);
+ RELOAD_SEG(fs);
+ RELOAD_SEG(ds);
+ RELOAD_SEG(es);
+
err |= fpu__restore_sig(buf, 1);

force_iret();




2019-02-25 15:43:09

by Andy Lutomirski

[permalink] [raw]
Subject: Re: [PATCH 2/6] x86/ia32: Fix ia32_restore_sigcontext AC leak

On Mon, Feb 25, 2019 at 4:53 AM Peter Zijlstra <[email protected]> wrote:
>
> Don't call load_gs_index() with AC set; delay the segment setting
> until after the AC section.
>
> Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
> ---
> arch/x86/ia32/ia32_signal.c | 29 +++++++++++++++++++----------
> 1 file changed, 19 insertions(+), 10 deletions(-)
>
> --- a/arch/x86/ia32/ia32_signal.c
> +++ b/arch/x86/ia32/ia32_signal.c
> @@ -60,17 +60,21 @@
> regs->seg = GET_SEG(seg) | 3; \
> } while (0)
>
> -#define RELOAD_SEG(seg) { \
> - unsigned int pre = GET_SEG(seg); \
> - unsigned int cur = get_user_seg(seg); \
> - pre |= 3; \
> - if (pre != cur) \
> - set_user_seg(seg, pre); \
> +#define LOAD_SEG(seg) { \
> + pre_##seg = 3 | GET_SEG(seg); \
> + cur_##seg = get_user_seg(seg); \
> +}
> +
> +#define RELOAD_SEG(seg) { \
> + if (pre_##seg != cur_##seg) \
> + set_user_seg(seg, pre_##seg); \
> }

This is so tangled.

How about changing RELOAD_SEG to replace unsigned int pre =
GET_SEG(seg); with unsigned int pre = (seg); to make it less magic.
Then do:

unsigned int gs = GET_SEG(gs);

...

RELOAD_SEG(gs);

And now the code actually does what it looks like it does.

2019-02-25 16:11:59

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/6] x86/ia32: Fix ia32_restore_sigcontext AC leak

On Mon, Feb 25, 2019 at 07:41:50AM -0800, Andy Lutomirski wrote:
> This is so tangled.
>
> How about changing RELOAD_SEG to replace unsigned int pre =
> GET_SEG(seg); with unsigned int pre = (seg); to make it less magic.
> Then do:
>
> unsigned int gs = GET_SEG(gs);
>
> ...
>
> RELOAD_SEG(gs);
>
> And now the code actually does what it looks like it does.

Is this what you mean?

---
diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c
index 321fe5f5d0e9..e04eeeddcc35 100644
--- a/arch/x86/ia32/ia32_signal.c
+++ b/arch/x86/ia32/ia32_signal.c
@@ -53,17 +53,16 @@
#define GET_SEG(seg) ({ \
unsigned short tmp; \
get_user_ex(tmp, &sc->seg); \
- tmp; \
+ tmp | 3; \
})

#define COPY_SEG_CPL3(seg) do { \
- regs->seg = GET_SEG(seg) | 3; \
+ regs->seg = GET_SEG(seg); \
} while (0)

#define RELOAD_SEG(seg) { \
- unsigned int pre = GET_SEG(seg); \
+ unsigned int pre = (seg); \
unsigned int cur = get_user_seg(seg); \
- pre |= 3; \
if (pre != cur) \
set_user_seg(seg, pre); \
}
@@ -72,6 +71,7 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
struct sigcontext_32 __user *sc)
{
unsigned int tmpflags, err = 0;
+ u16 gs, fs, es, ds;
void __user *buf;
u32 tmp;

@@ -79,16 +79,10 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
current->restart_block.fn = do_no_restart_syscall;

get_user_try {
- /*
- * 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.
- */
- RELOAD_SEG(gs);
- RELOAD_SEG(fs);
- RELOAD_SEG(ds);
- RELOAD_SEG(es);
+ gs = GET_SEG(gs);
+ fs = GET_SEG(fs);
+ ds = GET_SEG(ds);
+ es = GET_SEG(es);

COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
COPY(dx); COPY(cx); COPY(ip); COPY(ax);
@@ -106,6 +100,17 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
buf = compat_ptr(tmp);
} get_user_catch(err);

+ /*
+ * 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.
+ */
+ RELOAD_SEG(gs);
+ RELOAD_SEG(fs);
+ RELOAD_SEG(ds);
+ RELOAD_SEG(es);
+
err |= fpu__restore_sig(buf, 1);

force_iret();

2019-02-25 16:29:53

by Andy Lutomirski

[permalink] [raw]
Subject: Re: [PATCH 2/6] x86/ia32: Fix ia32_restore_sigcontext AC leak




> On Feb 25, 2019, at 8:10 AM, Peter Zijlstra <[email protected]> wrote:
>
>> On Mon, Feb 25, 2019 at 07:41:50AM -0800, Andy Lutomirski wrote:
>> This is so tangled.
>>
>> How about changing RELOAD_SEG to replace unsigned int pre =
>> GET_SEG(seg); with unsigned int pre = (seg); to make it less magic.
>> Then do:
>>
>> unsigned int gs = GET_SEG(gs);
>>
>> ...
>>
>> RELOAD_SEG(gs);
>>
>> And now the code actually does what it looks like it does.
>
> Is this what you mean?

Yes, except:

>
> ---
> diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c
> index 321fe5f5d0e9..e04eeeddcc35 100644
> --- a/arch/x86/ia32/ia32_signal.c
> +++ b/arch/x86/ia32/ia32_signal.c
> @@ -53,17 +53,16 @@
> #define GET_SEG(seg) ({ \
> unsigned short tmp; \
> get_user_ex(tmp, &sc->seg); \
> - tmp; \
> + tmp | 3; \
> })
>

Drop this part.

> #define COPY_SEG_CPL3(seg) do { \
> - regs->seg = GET_SEG(seg) | 3; \
> + regs->seg = GET_SEG(seg); \
> } while (0)

And this.

Unfortunately, whether we want the | 3 varies by segment. For FS and GS, we definitely don’t want it, since 0 is a common and important value, and 3 is a deeply screwed up value. (3 is legal to *write* to GS, and it sticks, but IRET silently changes it to 0, because the original 386 designers (I assume) confused themselves.

>
> #define RELOAD_SEG(seg) { \
> - unsigned int pre = GET_SEG(seg); \
> + unsigned int pre = (seg); \
> unsigned int cur = get_user_seg(seg); \
> - pre |= 3; \
> if (pre != cur) \
> set_user_seg(seg, pre); \
> }
> @@ -72,6 +71,7 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
> struct sigcontext_32 __user *sc)
> {
> unsigned int tmpflags, err = 0;
> + u16 gs, fs, es, ds;
> void __user *buf;
> u32 tmp;
>
> @@ -79,16 +79,10 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
> current->restart_block.fn = do_no_restart_syscall;
>
> get_user_try {
> - /*
> - * 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.
> - */
> - RELOAD_SEG(gs);
> - RELOAD_SEG(fs);
> - RELOAD_SEG(ds);
> - RELOAD_SEG(es);
> + gs = GET_SEG(gs);
> + fs = GET_SEG(fs);
> + ds = GET_SEG(ds);
> + es = GET_SEG(es);
>
> COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
> COPY(dx); COPY(cx); COPY(ip); COPY(ax);
> @@ -106,6 +100,17 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
> buf = compat_ptr(tmp);
> } get_user_catch(err);
>
> + /*
> + * 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.
> + */
> + RELOAD_SEG(gs);
> + RELOAD_SEG(fs);
> + RELOAD_SEG(ds);
> + RELOAD_SEG(es);
> +
> err |= fpu__restore_sig(buf, 1);
>
> force_iret();

I

2019-02-25 16:39:47

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/6] x86/ia32: Fix ia32_restore_sigcontext AC leak

On Mon, Feb 25, 2019 at 08:29:12AM -0800, Andy Lutomirski wrote:

> > diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c
> > index 321fe5f5d0e9..e04eeeddcc35 100644
> > --- a/arch/x86/ia32/ia32_signal.c
> > +++ b/arch/x86/ia32/ia32_signal.c
> > @@ -53,17 +53,16 @@
> > #define GET_SEG(seg) ({ \
> > unsigned short tmp; \
> > get_user_ex(tmp, &sc->seg); \
> > - tmp; \
> > + tmp | 3; \
> > })
> >
>
> Drop this part.
>
> > #define COPY_SEG_CPL3(seg) do { \
> > - regs->seg = GET_SEG(seg) | 3; \
> > + regs->seg = GET_SEG(seg); \
> > } while (0)
>
> And this.
>
> Unfortunately, whether we want the | 3 varies by segment. For FS and
> GS, we definitely don’t want it, since 0 is a common and important
> value, and 3 is a deeply screwed up value. (3 is legal to *write* to
> GS, and it sticks, but IRET silently changes it to 0, because the
> original 386 designers (I assume) confused themselves.

> >
> > #define RELOAD_SEG(seg) { \
> > - unsigned int pre = GET_SEG(seg); \
> > + unsigned int pre = (seg); \
> > unsigned int cur = get_user_seg(seg); \
> > - pre |= 3; \

And here ?

> > if (pre != cur) \
> > set_user_seg(seg, pre); \
> > }

Thing is; afaict the current code _always_ does |3 on any GET_SET()
result.

If you want to change that; I'm fine with that, but lets not do that in
this same patch.

So then?

diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c
index 321fe5f5d0e9..4d5fcd47ab75 100644
--- a/arch/x86/ia32/ia32_signal.c
+++ b/arch/x86/ia32/ia32_signal.c
@@ -61,9 +61,8 @@
} while (0)

#define RELOAD_SEG(seg) { \
- unsigned int pre = GET_SEG(seg); \
+ unsigned int pre = (seg) | 3; \
unsigned int cur = get_user_seg(seg); \
- pre |= 3; \
if (pre != cur) \
set_user_seg(seg, pre); \
}
@@ -72,6 +71,7 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
struct sigcontext_32 __user *sc)
{
unsigned int tmpflags, err = 0;
+ u16 gs, fs, es, ds;
void __user *buf;
u32 tmp;

@@ -79,16 +79,10 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
current->restart_block.fn = do_no_restart_syscall;

get_user_try {
- /*
- * 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.
- */
- RELOAD_SEG(gs);
- RELOAD_SEG(fs);
- RELOAD_SEG(ds);
- RELOAD_SEG(es);
+ gs = GET_SEG(gs);
+ fs = GET_SEG(fs);
+ ds = GET_SEG(ds);
+ es = GET_SEG(es);

COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
COPY(dx); COPY(cx); COPY(ip); COPY(ax);
@@ -106,6 +100,17 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
buf = compat_ptr(tmp);
} get_user_catch(err);

+ /*
+ * 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.
+ */
+ RELOAD_SEG(gs);
+ RELOAD_SEG(fs);
+ RELOAD_SEG(ds);
+ RELOAD_SEG(es);
+
err |= fpu__restore_sig(buf, 1);

force_iret();

2019-02-25 16:42:36

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/6] x86/ia32: Fix ia32_restore_sigcontext AC leak

On Mon, Feb 25, 2019 at 05:37:45PM +0100, Peter Zijlstra wrote:
> + RELOAD_SEG(gs);
> + RELOAD_SEG(fs);
> + RELOAD_SEG(ds);
> + RELOAD_SEG(es);

Also; is that the canonical order ? It bugs the hell out of me to not
have that alphabetically correct. Shall I correct that?

2019-02-25 16:50:00

by Andy Lutomirski

[permalink] [raw]
Subject: Re: [PATCH 2/6] x86/ia32: Fix ia32_restore_sigcontext AC leak

On Mon, Feb 25, 2019 at 8:37 AM Peter Zijlstra <[email protected]> wrote:
>
> On Mon, Feb 25, 2019 at 08:29:12AM -0800, Andy Lutomirski wrote:
>
> > > diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c
> > > index 321fe5f5d0e9..e04eeeddcc35 100644
> > > --- a/arch/x86/ia32/ia32_signal.c
> > > +++ b/arch/x86/ia32/ia32_signal.c
> > > @@ -53,17 +53,16 @@
> > > #define GET_SEG(seg) ({ \
> > > unsigned short tmp; \
> > > get_user_ex(tmp, &sc->seg); \
> > > - tmp; \
> > > + tmp | 3; \
> > > })
> > >
> >
> > Drop this part.
> >
> > > #define COPY_SEG_CPL3(seg) do { \
> > > - regs->seg = GET_SEG(seg) | 3; \
> > > + regs->seg = GET_SEG(seg); \
> > > } while (0)
> >
> > And this.
> >
> > Unfortunately, whether we want the | 3 varies by segment. For FS and
> > GS, we definitely don’t want it, since 0 is a common and important
> > value, and 3 is a deeply screwed up value. (3 is legal to *write* to
> > GS, and it sticks, but IRET silently changes it to 0, because the
> > original 386 designers (I assume) confused themselves.
>
> > >
> > > #define RELOAD_SEG(seg) { \
> > > - unsigned int pre = GET_SEG(seg); \
> > > + unsigned int pre = (seg); \
> > > unsigned int cur = get_user_seg(seg); \
> > > - pre |= 3; \
>
> And here ?
>
> > > if (pre != cur) \
> > > set_user_seg(seg, pre); \
> > > }
>
> Thing is; afaict the current code _always_ does |3 on any GET_SET()
> result.
>
> If you want to change that; I'm fine with that, but lets not do that in
> this same patch.

Ugh, you're right. I bet I can come up with some awful case involving
ptrace and sigreturn where this causes problems.

I have a patch series I need to dust off that deletes this entire
file. It's this and its parents:

https://git.kernel.org/pub/scm/linux/kernel/git/luto/linux.git/commit/?h=execve&id=0618fe90d8224979f70b15d33dcae75a403592e5

>
> So then?
>
> diff --git a/arch/x86/ia32/ia32_signal.c b/arch/x86/ia32/ia32_signal.c
> index 321fe5f5d0e9..4d5fcd47ab75 100644
> --- a/arch/x86/ia32/ia32_signal.c
> +++ b/arch/x86/ia32/ia32_signal.c
> @@ -61,9 +61,8 @@
> } while (0)
>
> #define RELOAD_SEG(seg) { \
> - unsigned int pre = GET_SEG(seg); \
> + unsigned int pre = (seg) | 3; \
> unsigned int cur = get_user_seg(seg); \
> - pre |= 3; \
> if (pre != cur) \
> set_user_seg(seg, pre); \
> }
> @@ -72,6 +71,7 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
> struct sigcontext_32 __user *sc)
> {
> unsigned int tmpflags, err = 0;
> + u16 gs, fs, es, ds;
> void __user *buf;
> u32 tmp;
>
> @@ -79,16 +79,10 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
> current->restart_block.fn = do_no_restart_syscall;
>
> get_user_try {
> - /*
> - * 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.
> - */
> - RELOAD_SEG(gs);
> - RELOAD_SEG(fs);
> - RELOAD_SEG(ds);
> - RELOAD_SEG(es);
> + gs = GET_SEG(gs);
> + fs = GET_SEG(fs);
> + ds = GET_SEG(ds);
> + es = GET_SEG(es);
>
> COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
> COPY(dx); COPY(cx); COPY(ip); COPY(ax);
> @@ -106,6 +100,17 @@ static int ia32_restore_sigcontext(struct pt_regs *regs,
> buf = compat_ptr(tmp);
> } get_user_catch(err);
>
> + /*
> + * 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.
> + */
> + RELOAD_SEG(gs);
> + RELOAD_SEG(fs);
> + RELOAD_SEG(ds);
> + RELOAD_SEG(es);
> +
> err |= fpu__restore_sig(buf, 1);
>
> force_iret();

Looks good to me. The order of the restores shouldn't matter at all.