Re: [PATCH] x86/fpu: verify xstate buffer size according with requested features

From: Dave Hansen
Date: Wed Jan 17 2024 - 14:34:42 EST


.. adding LKML. More context here:

https://lore.kernel.org/all/20240116234901.3238852-1-avagin@xxxxxxxxxx/

On 1/16/24 15:49, Andrei Vagin wrote:
> + /* xstate_size has to fit all requested components. */
> + if (fx_sw->xstate_size != fpstate->user_size) {
> + int min_xstate_size =
> + xstate_calculate_size(fx_sw->xfeatures, false);
> +
> + if (min_xstate_size < 0 ||
> + fx_sw->xstate_size < min_xstate_size ||
> + fx_sw->xstate_size > fpstate->user_size)
> + goto setfx;
> + }

The bug here is that the buffer from userspace is garbage and the (XSAVE
XSTATE_BV) metadata doesn't match the size of the buffer. Right?

This proposed fix just checks another piece of user-supplied metadata
instead: fx_sw->xstate_size.

Can't userspace just provide more bad data there and end up with the
same problem?

Seems like the real problem here is that the fault_in_readable() doesn't
match the XRSTOR. It's going to continue to be a problem as long as we
don't know what memory XRSTOR tried to access. We can try all day long
to precalculate what XRSTOR _will_ do, but that seems a bit silly
because the CPU knows where the fault happened. It told us in CR2 and
all we have to do is plumb that back to fault_in_readable().

It would take a little XSTATE_OP() munging to pass something back other
than 'err', but that doesn't seem insurmountable.

Anybody have better ideas?