x86/fpu: Deduplicate copy_uabi_from_user/kernel_to_xstate()
copy_uabi_from_user_to_xstate() and copy_uabi_from_kernel_to_xstate() are almost identical except for the copy function. Unify them. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Borislav Petkov <bp@suse.de> Acked-by: Andy Lutomirski <luto@kernel.org> Link: https://lkml.kernel.org/r/20210623121454.414215896@linutronix.de
This commit is contained in:
parent
1cc34413ff
commit
522e92743b
@ -953,23 +953,6 @@ int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
|
|||||||
}
|
}
|
||||||
#endif /* ! CONFIG_ARCH_HAS_PKEYS */
|
#endif /* ! CONFIG_ARCH_HAS_PKEYS */
|
||||||
|
|
||||||
/*
|
|
||||||
* Weird legacy quirk: SSE and YMM states store information in the
|
|
||||||
* MXCSR and MXCSR_FLAGS fields of the FP area. That means if the FP
|
|
||||||
* area is marked as unused in the xfeatures header, we need to copy
|
|
||||||
* MXCSR and MXCSR_FLAGS if either SSE or YMM are in use.
|
|
||||||
*/
|
|
||||||
static inline bool xfeatures_mxcsr_quirk(u64 xfeatures)
|
|
||||||
{
|
|
||||||
if (!(xfeatures & (XFEATURE_MASK_SSE|XFEATURE_MASK_YMM)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (xfeatures & XFEATURE_MASK_FP)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,
|
static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,
|
||||||
void *init_xstate, unsigned int size)
|
void *init_xstate, unsigned int size)
|
||||||
{
|
{
|
||||||
@ -1082,39 +1065,53 @@ out:
|
|||||||
membuf_zero(&to, to.left);
|
membuf_zero(&to, to.left);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool mxcsr_valid(struct xstate_header *hdr, const u32 *mxcsr)
|
static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size,
|
||||||
|
const void *kbuf, const void __user *ubuf)
|
||||||
{
|
{
|
||||||
u64 mask = XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM;
|
if (kbuf) {
|
||||||
|
memcpy(dst, kbuf + offset, size);
|
||||||
/* Only check if it is in use */
|
} else {
|
||||||
if (hdr->xfeatures & mask) {
|
if (copy_from_user(dst, ubuf + offset, size))
|
||||||
/* Reserved bits in MXCSR must be zero. */
|
return -EFAULT;
|
||||||
if (*mxcsr & ~mxcsr_feature_mask)
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] format
|
static int copy_uabi_to_xstate(struct xregs_state *xsave, const void *kbuf,
|
||||||
* and copy to the target thread. This is called from xstateregs_set().
|
const void __user *ubuf)
|
||||||
*/
|
|
||||||
int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
|
|
||||||
{
|
{
|
||||||
unsigned int offset, size;
|
unsigned int offset, size;
|
||||||
int i;
|
|
||||||
struct xstate_header hdr;
|
struct xstate_header hdr;
|
||||||
|
u64 mask;
|
||||||
|
int i;
|
||||||
|
|
||||||
offset = offsetof(struct xregs_state, header);
|
offset = offsetof(struct xregs_state, header);
|
||||||
size = sizeof(hdr);
|
if (copy_from_buffer(&hdr, offset, sizeof(hdr), kbuf, ubuf))
|
||||||
|
return -EFAULT;
|
||||||
memcpy(&hdr, kbuf + offset, size);
|
|
||||||
|
|
||||||
if (validate_user_xstate_header(&hdr))
|
if (validate_user_xstate_header(&hdr))
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (!mxcsr_valid(&hdr, kbuf + offsetof(struct fxregs_state, mxcsr)))
|
/* Validate MXCSR when any of the related features is in use */
|
||||||
return -EINVAL;
|
mask = XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM;
|
||||||
|
if (hdr.xfeatures & mask) {
|
||||||
|
u32 mxcsr[2];
|
||||||
|
|
||||||
|
offset = offsetof(struct fxregs_state, mxcsr);
|
||||||
|
if (copy_from_buffer(mxcsr, offset, sizeof(mxcsr), kbuf, ubuf))
|
||||||
|
return -EFAULT;
|
||||||
|
|
||||||
|
/* Reserved bits in MXCSR must be zero. */
|
||||||
|
if (mxcsr[0] & ~mxcsr_feature_mask)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
/* SSE and YMM require MXCSR even when FP is not in use. */
|
||||||
|
if (!(hdr.xfeatures & XFEATURE_MASK_FP)) {
|
||||||
|
xsave->i387.mxcsr = mxcsr[0];
|
||||||
|
xsave->i387.mxcsr_mask = mxcsr[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; i < XFEATURE_MAX; i++) {
|
for (i = 0; i < XFEATURE_MAX; i++) {
|
||||||
u64 mask = ((u64)1 << i);
|
u64 mask = ((u64)1 << i);
|
||||||
@ -1125,16 +1122,11 @@ int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
|
|||||||
offset = xstate_offsets[i];
|
offset = xstate_offsets[i];
|
||||||
size = xstate_sizes[i];
|
size = xstate_sizes[i];
|
||||||
|
|
||||||
memcpy(dst, kbuf + offset, size);
|
if (copy_from_buffer(dst, offset, size, kbuf, ubuf))
|
||||||
|
return -EFAULT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
|
|
||||||
offset = offsetof(struct fxregs_state, mxcsr);
|
|
||||||
size = MXCSR_AND_FLAGS_SIZE;
|
|
||||||
memcpy(&xsave->i387.mxcsr, kbuf + offset, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The state that came in from userspace was user-state only.
|
* The state that came in from userspace was user-state only.
|
||||||
* Mask all the user states out of 'xfeatures':
|
* Mask all the user states out of 'xfeatures':
|
||||||
@ -1149,6 +1141,16 @@ int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S]
|
||||||
|
* format and copy to the target thread. This is called from
|
||||||
|
* xstateregs_set().
|
||||||
|
*/
|
||||||
|
int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
|
||||||
|
{
|
||||||
|
return copy_uabi_to_xstate(xsave, kbuf, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Convert from a sigreturn standard-format user-space buffer to kernel
|
* Convert from a sigreturn standard-format user-space buffer to kernel
|
||||||
* XSAVE[S] format and copy to the target thread. This is called from the
|
* XSAVE[S] format and copy to the target thread. This is called from the
|
||||||
@ -1157,52 +1159,7 @@ int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
|
|||||||
int copy_sigframe_from_user_to_xstate(struct xregs_state *xsave,
|
int copy_sigframe_from_user_to_xstate(struct xregs_state *xsave,
|
||||||
const void __user *ubuf)
|
const void __user *ubuf)
|
||||||
{
|
{
|
||||||
unsigned int offset, size;
|
return copy_uabi_to_xstate(xsave, NULL, ubuf);
|
||||||
int i;
|
|
||||||
struct xstate_header hdr;
|
|
||||||
|
|
||||||
offset = offsetof(struct xregs_state, header);
|
|
||||||
size = sizeof(hdr);
|
|
||||||
|
|
||||||
if (copy_from_user(&hdr, ubuf + offset, size))
|
|
||||||
return -EFAULT;
|
|
||||||
|
|
||||||
if (validate_user_xstate_header(&hdr))
|
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
for (i = 0; i < XFEATURE_MAX; i++) {
|
|
||||||
u64 mask = ((u64)1 << i);
|
|
||||||
|
|
||||||
if (hdr.xfeatures & mask) {
|
|
||||||
void *dst = __raw_xsave_addr(xsave, i);
|
|
||||||
|
|
||||||
offset = xstate_offsets[i];
|
|
||||||
size = xstate_sizes[i];
|
|
||||||
|
|
||||||
if (copy_from_user(dst, ubuf + offset, size))
|
|
||||||
return -EFAULT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
|
|
||||||
offset = offsetof(struct fxregs_state, mxcsr);
|
|
||||||
size = MXCSR_AND_FLAGS_SIZE;
|
|
||||||
if (copy_from_user(&xsave->i387.mxcsr, ubuf + offset, size))
|
|
||||||
return -EFAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The state that came in from userspace was user-state only.
|
|
||||||
* Mask all the user states out of 'xfeatures':
|
|
||||||
*/
|
|
||||||
xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR_ALL;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Add back in the features that came in from userspace:
|
|
||||||
*/
|
|
||||||
xsave->header.xfeatures |= hdr.xfeatures;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user