Skip to content

Commit e84ba47

Browse files
hansendcsuryasaimadhu
authored andcommitted
x86/fpu: Hook up PKRU into ptrace()
One nice thing about having PKRU be XSAVE-managed is that it gets naturally exposed into the XSAVE-using ABIs. Now that XSAVE will not be used to manage PKRU, these ABIs need to be manually enabled to deal with PKRU. ptrace() uses copy_uabi_xstate_to_kernel() to collect the tracee's XSTATE. As PKRU is not in the task's XSTATE buffer, use task->thread.pkru for filling in up the ptrace buffer. Signed-off-by: Dave Hansen <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 9782a71 commit e84ba47

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

arch/x86/include/asm/fpu/xstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ enum xstate_copy_mode {
139139
};
140140

141141
struct membuf;
142-
void copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,
142+
void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
143143
enum xstate_copy_mode mode);
144144

145145
#endif

arch/x86/kernel/fpu/regset.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
7878
sizeof(fpu->state.fxsave));
7979
}
8080

81-
copy_xstate_to_uabi_buf(to, &fpu->state.xsave, XSTATE_COPY_FX);
81+
copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_FX);
8282
return 0;
8383
}
8484

@@ -126,14 +126,12 @@ int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
126126
int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
127127
struct membuf to)
128128
{
129-
struct fpu *fpu = &target->thread.fpu;
130-
131129
if (!cpu_feature_enabled(X86_FEATURE_XSAVE))
132130
return -ENODEV;
133131

134-
sync_fpstate(fpu);
132+
sync_fpstate(&target->thread.fpu);
135133

136-
copy_xstate_to_uabi_buf(to, &fpu->state.xsave, XSTATE_COPY_XSAVE);
134+
copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_XSAVE);
137135
return 0;
138136
}
139137

@@ -336,7 +334,7 @@ int fpregs_get(struct task_struct *target, const struct user_regset *regset,
336334
struct membuf mb = { .p = &fxsave, .left = sizeof(fxsave) };
337335

338336
/* Handle init state optimized xstate correctly */
339-
copy_xstate_to_uabi_buf(mb, &fpu->state.xsave, XSTATE_COPY_FP);
337+
copy_xstate_to_uabi_buf(mb, target, XSTATE_COPY_FP);
340338
fx = &fxsave;
341339
} else {
342340
fx = &fpu->state.fxsave;

arch/x86/kernel/fpu/xstate.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,
962962
/**
963963
* copy_xstate_to_uabi_buf - Copy kernel saved xstate to a UABI buffer
964964
* @to: membuf descriptor
965-
* @xsave: The kernel xstate buffer to copy from
965+
* @tsk: The task from which to copy the saved xstate
966966
* @copy_mode: The requested copy mode
967967
*
968968
* Converts from kernel XSAVE or XSAVES compacted format to UABI conforming
@@ -971,10 +971,11 @@ static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,
971971
*
972972
* It supports partial copy but @to.pos always starts from zero.
973973
*/
974-
void copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,
974+
void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
975975
enum xstate_copy_mode copy_mode)
976976
{
977977
const unsigned int off_mxcsr = offsetof(struct fxregs_state, mxcsr);
978+
struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
978979
struct xregs_state *xinit = &init_fpstate.xsave;
979980
struct xstate_header header;
980981
unsigned int zerofrom;
@@ -1048,11 +1049,21 @@ void copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,
10481049
if (zerofrom < xstate_offsets[i])
10491050
membuf_zero(&to, xstate_offsets[i] - zerofrom);
10501051

1051-
copy_feature(header.xfeatures & BIT_ULL(i), &to,
1052-
__raw_xsave_addr(xsave, i),
1053-
__raw_xsave_addr(xinit, i),
1054-
xstate_sizes[i]);
1055-
1052+
if (i == XFEATURE_PKRU) {
1053+
struct pkru_state pkru = {0};
1054+
/*
1055+
* PKRU is not necessarily up to date in the
1056+
* thread's XSAVE buffer. Fill this part from the
1057+
* per-thread storage.
1058+
*/
1059+
pkru.pkru = tsk->thread.pkru;
1060+
membuf_write(&to, &pkru, sizeof(pkru));
1061+
} else {
1062+
copy_feature(header.xfeatures & BIT_ULL(i), &to,
1063+
__raw_xsave_addr(xsave, i),
1064+
__raw_xsave_addr(xinit, i),
1065+
xstate_sizes[i]);
1066+
}
10561067
/*
10571068
* Keep track of the last copied state in the non-compacted
10581069
* target buffer for gap zeroing.

0 commit comments

Comments
 (0)