Skip to content

Commit 5529acf

Browse files
KAGA-KOKOsuryasaimadhu
authored andcommitted
x86/fpu: Add sanity checks for XFD
Add debug functionality to ensure that the XFD MSR is up to date for XSAVE* and XRSTOR* operations. [ tglx: Improve comment. ] Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Chang S. Bae <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 8bf2675 commit 5529acf

File tree

4 files changed

+95
-12
lines changed

4 files changed

+95
-12
lines changed

arch/x86/kernel/fpu/core.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask)
166166
*/
167167
mask = fpu_kernel_cfg.max_features & mask;
168168

169-
os_xrstor(&fpstate->regs.xsave, mask);
169+
os_xrstor(fpstate, mask);
170170
} else {
171171
if (use_fxsr())
172172
fxrstor(&fpstate->regs.fxsave);
@@ -534,7 +534,7 @@ void fpu__drop(struct fpu *fpu)
534534
static inline void restore_fpregs_from_init_fpstate(u64 features_mask)
535535
{
536536
if (use_xsave())
537-
os_xrstor(&init_fpstate.regs.xsave, features_mask);
537+
os_xrstor(&init_fpstate, features_mask);
538538
else if (use_fxsr())
539539
fxrstor(&init_fpstate.regs.fxsave);
540540
else
@@ -591,9 +591,8 @@ void fpu__clear_user_states(struct fpu *fpu)
591591
* corresponding registers.
592592
*/
593593
if (xfeatures_mask_supervisor() &&
594-
!fpregs_state_valid(fpu, smp_processor_id())) {
595-
os_xrstor(&fpu->fpstate->regs.xsave, xfeatures_mask_supervisor());
596-
}
594+
!fpregs_state_valid(fpu, smp_processor_id()))
595+
os_xrstor_supervisor(fpu->fpstate);
597596

598597
/* Reset user states in registers. */
599598
restore_fpregs_from_init_fpstate(XFEATURE_MASK_USER_RESTORE);

arch/x86/kernel/fpu/signal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures,
261261
ret = fxrstor_from_user_sigframe(buf);
262262

263263
if (!ret && unlikely(init_bv))
264-
os_xrstor(&init_fpstate.regs.xsave, init_bv);
264+
os_xrstor(&init_fpstate, init_bv);
265265
return ret;
266266
} else if (use_fxsr()) {
267267
return fxrstor_from_user_sigframe(buf);
@@ -322,7 +322,7 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore,
322322
* been restored from a user buffer directly.
323323
*/
324324
if (test_thread_flag(TIF_NEED_FPU_LOAD) && xfeatures_mask_supervisor())
325-
os_xrstor(&fpu->fpstate->regs.xsave, xfeatures_mask_supervisor());
325+
os_xrstor_supervisor(fpu->fpstate);
326326

327327
fpregs_mark_activate();
328328
fpregs_unlock();
@@ -432,7 +432,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,
432432
u64 mask = user_xfeatures | xfeatures_mask_supervisor();
433433

434434
fpregs->xsave.header.xfeatures &= mask;
435-
success = !os_xrstor_safe(&fpregs->xsave,
435+
success = !os_xrstor_safe(fpu->fpstate,
436436
fpu_kernel_cfg.max_features);
437437
} else {
438438
success = !fxrstor_safe(&fpregs->fxsave);

arch/x86/kernel/fpu/xstate.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,64 @@ EXPORT_SYMBOL_GPL(fpstate_clear_xstate_component);
13011301
#endif
13021302

13031303
#ifdef CONFIG_X86_64
1304+
1305+
#ifdef CONFIG_X86_DEBUG_FPU
1306+
/*
1307+
* Ensure that a subsequent XSAVE* or XRSTOR* instruction with RFBM=@mask
1308+
* can safely operate on the @fpstate buffer.
1309+
*/
1310+
static bool xstate_op_valid(struct fpstate *fpstate, u64 mask, bool rstor)
1311+
{
1312+
u64 xfd = __this_cpu_read(xfd_state);
1313+
1314+
if (fpstate->xfd == xfd)
1315+
return true;
1316+
1317+
/*
1318+
* The XFD MSR does not match fpstate->xfd. That's invalid when
1319+
* the passed in fpstate is current's fpstate.
1320+
*/
1321+
if (fpstate->xfd == current->thread.fpu.fpstate->xfd)
1322+
return false;
1323+
1324+
/*
1325+
* XRSTOR(S) from init_fpstate are always correct as it will just
1326+
* bring all components into init state and not read from the
1327+
* buffer. XSAVE(S) raises #PF after init.
1328+
*/
1329+
if (fpstate == &init_fpstate)
1330+
return rstor;
1331+
1332+
/*
1333+
* XSAVE(S): clone(), fpu_swap_kvm_fpu()
1334+
* XRSTORS(S): fpu_swap_kvm_fpu()
1335+
*/
1336+
1337+
/*
1338+
* No XSAVE/XRSTOR instructions (except XSAVE itself) touch
1339+
* the buffer area for XFD-disabled state components.
1340+
*/
1341+
mask &= ~xfd;
1342+
1343+
/*
1344+
* Remove features which are valid in fpstate. They
1345+
* have space allocated in fpstate.
1346+
*/
1347+
mask &= ~fpstate->xfeatures;
1348+
1349+
/*
1350+
* Any remaining state components in 'mask' might be written
1351+
* by XSAVE/XRSTOR. Fail validation it found.
1352+
*/
1353+
return !mask;
1354+
}
1355+
1356+
void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor)
1357+
{
1358+
WARN_ON_ONCE(!xstate_op_valid(fpstate, mask, rstor));
1359+
}
1360+
#endif /* CONFIG_X86_DEBUG_FPU */
1361+
13041362
static int validate_sigaltstack(unsigned int usize)
13051363
{
13061364
struct task_struct *thread, *leader = current->group_leader;

arch/x86/kernel/fpu/xstate.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ static inline u64 xfeatures_mask_independent(void)
130130
: "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
131131
: "memory")
132132

133+
#if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
134+
extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
135+
#else
136+
static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
137+
#endif
138+
133139
/*
134140
* Save processor xstate to xsave area.
135141
*
@@ -144,6 +150,7 @@ static inline void os_xsave(struct fpstate *fpstate)
144150
int err;
145151

146152
WARN_ON_FPU(!alternatives_patched);
153+
xfd_validate_state(fpstate, mask, false);
147154

148155
XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
149156

@@ -156,12 +163,23 @@ static inline void os_xsave(struct fpstate *fpstate)
156163
*
157164
* Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
158165
*/
159-
static inline void os_xrstor(struct xregs_state *xstate, u64 mask)
166+
static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
167+
{
168+
u32 lmask = mask;
169+
u32 hmask = mask >> 32;
170+
171+
xfd_validate_state(fpstate, mask, true);
172+
XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
173+
}
174+
175+
/* Restore of supervisor state. Does not require XFD */
176+
static inline void os_xrstor_supervisor(struct fpstate *fpstate)
160177
{
178+
u64 mask = xfeatures_mask_supervisor();
161179
u32 lmask = mask;
162180
u32 hmask = mask >> 32;
163181

164-
XSTATE_XRESTORE(xstate, lmask, hmask);
182+
XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
165183
}
166184

167185
/*
@@ -184,11 +202,14 @@ static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
184202
* internally, e.g. PKRU. That's user space ABI and also required
185203
* to allow the signal handler to modify PKRU.
186204
*/
187-
u64 mask = current->thread.fpu.fpstate->user_xfeatures;
205+
struct fpstate *fpstate = current->thread.fpu.fpstate;
206+
u64 mask = fpstate->user_xfeatures;
188207
u32 lmask = mask;
189208
u32 hmask = mask >> 32;
190209
int err;
191210

211+
xfd_validate_state(fpstate, mask, false);
212+
192213
stac();
193214
XSTATE_OP(XSAVE, buf, lmask, hmask, err);
194215
clac();
@@ -206,6 +227,8 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64
206227
u32 hmask = mask >> 32;
207228
int err;
208229

230+
xfd_validate_state(current->thread.fpu.fpstate, mask, true);
231+
209232
stac();
210233
XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
211234
clac();
@@ -217,12 +240,15 @@ static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64
217240
* Restore xstate from kernel space xsave area, return an error code instead of
218241
* an exception.
219242
*/
220-
static inline int os_xrstor_safe(struct xregs_state *xstate, u64 mask)
243+
static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
221244
{
245+
struct xregs_state *xstate = &fpstate->regs.xsave;
222246
u32 lmask = mask;
223247
u32 hmask = mask >> 32;
224248
int err;
225249

250+
/* Must enforce XFD update here */
251+
226252
if (cpu_feature_enabled(X86_FEATURE_XSAVES))
227253
XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
228254
else

0 commit comments

Comments
 (0)