Skip to content

Commit d877550

Browse files
avaginhansendc
authored andcommitted
x86/fpu: Stop relying on userspace for info to fault in xsave buffer
Before this change, the expected size of the user space buffer was taken from fx_sw->xstate_size. fx_sw->xstate_size can be changed from user-space, so it is possible construct a sigreturn frame where: * fx_sw->xstate_size is smaller than the size required by valid bits in fx_sw->xfeatures. * user-space unmaps parts of the sigrame fpu buffer so that not all of the buffer required by xrstor is accessible. In this case, xrstor tries to restore and accesses the unmapped area which results in a fault. But fault_in_readable succeeds because buf + fx_sw->xstate_size is within the still mapped area, so it goes back and tries xrstor again. It will spin in this loop forever. Instead, fault in the maximum size which can be touched by XRSTOR (taken from fpstate->user_size). [ dhansen: tweak subject / changelog ] Fixes: fcb3635 ("x86/fpu/signal: Handle #PF in the direct restore path") Reported-by: Konstantin Bogomolov <[email protected]> Suggested-by: Thomas Gleixner <[email protected]> Signed-off-by: Andrei Vagin <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Cc:[email protected] Link: https://lore.kernel.org/all/20240130063603.3392627-1-avagin%40google.com
1 parent 8eed4e0 commit d877550

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

arch/x86/kernel/fpu/signal.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,13 @@ static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures,
274274
* Attempt to restore the FPU registers directly from user memory.
275275
* Pagefaults are handled and any errors returned are fatal.
276276
*/
277-
static bool restore_fpregs_from_user(void __user *buf, u64 xrestore,
278-
bool fx_only, unsigned int size)
277+
static bool restore_fpregs_from_user(void __user *buf, u64 xrestore, bool fx_only)
279278
{
280279
struct fpu *fpu = &current->thread.fpu;
281280
int ret;
282281

282+
/* Restore enabled features only. */
283+
xrestore &= fpu->fpstate->user_xfeatures;
283284
retry:
284285
fpregs_lock();
285286
/* Ensure that XFD is up to date */
@@ -309,7 +310,7 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore,
309310
if (ret != X86_TRAP_PF)
310311
return false;
311312

312-
if (!fault_in_readable(buf, size))
313+
if (!fault_in_readable(buf, fpu->fpstate->user_size))
313314
goto retry;
314315
return false;
315316
}
@@ -339,7 +340,6 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,
339340
struct user_i387_ia32_struct env;
340341
bool success, fx_only = false;
341342
union fpregs_state *fpregs;
342-
unsigned int state_size;
343343
u64 user_xfeatures = 0;
344344

345345
if (use_xsave()) {
@@ -349,17 +349,14 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,
349349
return false;
350350

351351
fx_only = !fx_sw_user.magic1;
352-
state_size = fx_sw_user.xstate_size;
353352
user_xfeatures = fx_sw_user.xfeatures;
354353
} else {
355354
user_xfeatures = XFEATURE_MASK_FPSSE;
356-
state_size = fpu->fpstate->user_size;
357355
}
358356

359357
if (likely(!ia32_fxstate)) {
360358
/* Restore the FPU registers directly from user memory. */
361-
return restore_fpregs_from_user(buf_fx, user_xfeatures, fx_only,
362-
state_size);
359+
return restore_fpregs_from_user(buf_fx, user_xfeatures, fx_only);
363360
}
364361

365362
/*

0 commit comments

Comments
 (0)