PATCH? process_32.c:__switch_to() calls __math_state_restore() before updating current_task
I didn't try to verify __switch_to()->__math_state_restore() is really wrong, this is more the question than the patch. But at least the code looks wrong, it calls __math_state_restore() which uses curent before current_task was updated.
Uncompiled/untested.
arch/x86/kernel/process_32.c +3 -3
@@ -377,9 +377,6 @@ __switch_to(struct task_struct *prev_p,*/ arch_end_context_switch (next_p); -if (preload_fpu) -__math_state_restore (); -/* * Restore %gs if needed (which is common) */
@@ -388,6 +385,9 @@ __switch_to(struct task_struct *prev_p,percpu_write (current_task, next_p); +if (preload_fpu) +__math_state_restore (); +return prev_p; }
Quoted 22 lines · Oleg Nesterov
I didn't try to verify __switch_to()->__math_state_restore() is really wrong, this is more the question than the patch. But at least the code looks wrong, it calls
__math_state_restore()which uses curent beforecurrent_taskwas updated.Uncompiled/untested.
Signed-off-by Oleg Nesterov <[email protected]>arch/x86/kernel/process_32.c +0
-3@@ -377,9 +377,6 @@ __switch_to(struct task_struct *prev_p,*/ arch_end_context_switch (next_p); -if (preload_fpu) -__math_state_restore (); -/* * Restore %gs if needed (which is common) */
arch/x86/kernel/process_32.c __switch_to(struct task_struct *prev_p, · line 392
Quoted 8 lines
arch/x86/kernel/process_32.c +3
-0@@ -388,6 +385,9 @@ __switch_to(struct task_struct *prev_p,percpu_write (current_task, next_p); +if (preload_fpu) +__math_state_restore (); +return prev_p; }
Oleg, __math_state_restore() uses current_thread_info() which at that point already has the right esp and as such uses the correct thread struct etc.
After saying that, in the past I have also ran into this question and got satisfied by looking deeper. Best is to make the 32bit and 64bit code similar as much as possible and as such your patch is acceptable.
Can you please re-post with a proper changelog (and ofcourse testing etc)? You can add my Ack to that.
thanks,
suresh
Quoted 19 lines · Suresh Siddha
Quoted 17 lines · Oleg Nesterov
arch/x86/kernel/process_32.c +0
-3@@ -377,9 +377,6 @@ __switch_to(struct task_struct *prev_p,*/ arch_end_context_switch (next_p); -if (preload_fpu) -__math_state_restore (); -/* * Restore %gs if needed (which is common) */ @@ -388,6 +385,9 @@ __switch_to(struct task_struct *prev_p,percpu_write (current_task, next_p);
Quoted 9 lines
arch/x86/kernel/process_32.c __switch_to(struct task_struct *prev_p, · line 392
Quoted 5 lines
arch/x86/kernel/process_32.c +3
-0+if (preload_fpu) +__math_state_restore (); +return prev_p; }Oleg,
__math_state_restore()usescurrent_thread_info()which at that point already has the right esp and as such uses the correct thread struct etc.
Ah, indeed. I didn't notice that, unlike X86_64, X86_32 uses esp, not kernel_stack to get thread_info, and __math_state_restore() pathes never use get_current().
Quoted 6 lines
After saying that, in the past I have also ran into this question and got satisfied by looking deeper. Best is to make the 32bit and 64bit code similar as much as possible and as such your patch is acceptable.
Can you please re-post with a proper changelog (and ofcourse testing etc)? You can add my Ack to that.
OK, will do once I have a 32bit machine to test.
Thanks a lot for your explanation!
Could you please explain me another issue? I know nothing about fpu handling, I am reading this code trying to understand the unrelated bug with the old kernel.
In short: why restore_i387_xstate() does init_fpu() when !used_math(), can't (or shouldn't) it merely do set_used_math() ?
restore_i387_xstate:
if (!used_math()) {
err = init_fpu(tsk);
if (err)
return err;
}note that buf != NULL. This means that used_math() was true when get_sigframe() was called, otherwise buf == sigcontext->fpstate would be NULL, right?
So, the task must have the valid ->thread.xstate, and init_fpu() just resets the contents of *thread.xstate. Why? We are going to reload the FPU registers and set TS_USEDFPU. This means .xstate must be never used until we save the FPU registers back, correct?
IOW, I'd appreciate very much if you can explain me why the patch below is wrong. To clarify, even if the patch is correct I do not mean it is really needed, I am just trying to understand what I have missed here.
Thanks,
Oleg.
arch/x86/kernel/xsave.c +1 -5
@@ -215,11 +215,7 @@ int restore_i387_xstate(void __user *bufif (!access_ok (VERIFY_READ , buf, sig_xstate_size))return -EACCES ; -if (!used_math ()) { - err =init_fpu (tsk); -if (err) -return err; - } +set_stopped_child_used_math (tsk);if (!(task_thread_info (current)->status &TS_USEDFPU )) {clts ();
Quoted 12 lines · Oleg Nesterov
In short: why
restore_i387_xstate()doesinit_fpu()when !used_math(), can't (or shouldn't) it merely doset_used_math()?restore_i387_xstate: if (!used_math()) { err = init_fpu(tsk); if (err) return err; }
Quoted 5 lines
note that buf != NULL. This means that
used_math()was true whenget_sigframe()was called, otherwise buf ==sigcontext->fpstatewould be NULL, right?So, the task must have the valid ->thread.xstate,
This is probably correct. But a malicious user can set sc->fpsate and fool the kernel, so we must assume buf != NULL implies the valid ->xstate.
Oleg.
Quoted 14 lines · Oleg Nesterov
Quoted 12 lines · Oleg Nesterov
In short: why
restore_i387_xstate()doesinit_fpu()when !used_math(), can't (or shouldn't) it merely doset_used_math()?restore_i387_xstate: if (!used_math()) { err = init_fpu(tsk); if (err) return err; }
Quoted 8 lines
Quoted 5 lines
note that buf != NULL. This means that
used_math()was true whenget_sigframe()was called, otherwise buf ==sigcontext->fpstatewould be NULL, right?So, the task must have the valid ->thread.xstate,
This is probably correct. But a malicious user can set
sc->fpsateand fool the kernel, so we must assume buf != NULL implies the
^^^^
can'tQuoted 1 line
valid ->xstate.
Oleg.
Quoted 15 lines · Oleg Nesterov
Could you please explain me another issue? I know nothing about fpu handling, I am reading this code trying to understand the unrelated bug with the old kernel.
In short: why
restore_i387_xstate()doesinit_fpu()when !used_math(), can't (or shouldn't) it merely doset_used_math()?restore_i387_xstate: if (!used_math()) { err = init_fpu(tsk); if (err) return err; }
Quoted 3 lines
note that buf != NULL. This means that
used_math()was true whenget_sigframe()was called, otherwise buf ==sigcontext->fpstatewould be NULL, right?
No. When the signal is delivered sigcontext->fpstate can be null and when we return from the signal handler, user can update the sigcontext->fpstate pointer and the user expects the kernel to update the fpstate of the process accordingly.
Quoted 1 line
So, the task must have the valid ->thread.xstate,
Need not be.
thanks,
suresh
Quoted 17 lines · Suresh Siddha
Quoted 15 lines · Oleg Nesterov
Could you please explain me another issue? I know nothing about fpu handling, I am reading this code trying to understand the unrelated bug with the old kernel.
In short: why
restore_i387_xstate()doesinit_fpu()when !used_math(), can't (or shouldn't) it merely doset_used_math()?restore_i387_xstate: if (!used_math()) { err = init_fpu(tsk); if (err) return err; }
Quoted 7 lines
Quoted 3 lines
note that buf != NULL. This means that
used_math()was true whenget_sigframe()was called, otherwise buf ==sigcontext->fpstatewould be NULL, right?No. When the signal is delivered
sigcontext->fpstatecan be null and when we return from the signal handler, user can update thesigcontext->fpstatepointer
Yes, I already realized this, see my next emails.
Quoted 2 lines
and the user expects the kernel to update
the fpstate of the process accordingly.
but I didn't know it is "officially" allowed to populate .xstate this way.
Thanks Suresh for your explanations.
Oleg.