On the correctness of dbe3ed1c078c193be34326728d494c5c4bc115e2
A truly ancient commit (v2.6.23), dbe3ed1c078c193be34326728d494c5c4bc115e2:
x86-64: page faults from user mode are always user faults
Randy Dunlap noticed an interesting "crashme" behaviour on his dual Prescott Xeon setup, where he gets page faults with the error code having a zero "user" bit, but the register state points back to user mode.
This may be a CPU microcode buglet triggered by some strange instruction pattern that crashme generates, and loading a microcode update seems to possibly have fixed it.
Regardless, we really should trust the register state more than the error code, since it's really the register state that determines whether we can actually send a signal, or whether we're in kernel mode and need to oops/kill the process in the case of a page fault.
... introduced the following code (since slightly modified):
+ /*
+ * User-mode registers count as a user access even for any
+ * potential system fault or CPU buglet.
+ */
+ if (user_mode_vm(regs))
+ error_code |= PF_USER;
+
This has the end result that we treat a user space instruction which touches a privileged data structure that then page faults (e.g. a segment load which causes #PF on the GDT) as a user-space fault.
This seems very wrong to me, since such a #PF would indicate a serious error in the kernel.
If this was a buglet introduced by a specific processor ("Prescott Xeon" I presume means Nocona) and then even fixed in a patch, I'm concerned that we are putting the cart before the horse with this change.
I went through the errata sheets for the CPUs of the time, but nothing jumped out at me as causing this kind of problem, although there is a mention of a couple of undefined opcodes which ought to #UD being able to generate a "load to an incorrect address". Kind of a stretch, though.
-hpa
Quoted 7 lines · H. Peter Anvin
This has the end result that we treat a user space instruction which touches a privileged data structure that then page faults (e.g. a segment load which causes #PF on the GDT) as a user-space fault.
This seems very wrong to me, since such a #PF would indicate a serious error in the kernel.
Not necessarily. Don't we basically do exactly that for the F00F bug workaround, for example?
Linus
Quoted 6 lines · Linus Torvalds
Quoted 5 lines · H. Peter Anvin
This has the end result that we treat a user space instruction which touches a privileged data structure that then page faults (e.g. a segment load which causes #PF on the GDT) as a user-space fault.
Quoted 6 lines
Quoted 2 lines
This seems very wrong to me, since such a #PF would indicate a serious error in the kernel.
Not necessarily. Don't we basically do exactly that for the F00F bug workaround, for example?
We do, but only after matching on an exact address (is_f00f_bug()). Note also that is_f00f_bug() isn't conditional on PF_USER.
-hpa
Quoted 6 lines · Linus Torvalds
Quoted 5 lines · H. Peter Anvin
This has the end result that we treat a user space instruction which touches a privileged data structure that then page faults (e.g. a segment load which causes #PF on the GDT) as a user-space fault.
Quoted 8 lines
Quoted 2 lines
This seems very wrong to me, since such a #PF would indicate a serious error in the kernel.
Not necessarily. Don't we basically do exactly that for the F00F bug workaround, for example?
Linus
Actually, from looking at it, the F00F workaround is broken *exactly* because of this patch. By forcing PF_USER to set, we go into the if (error_code & PF_USER) branch of __bad_area_semaphore(), which means we *don't* do the F00F checking, and will deliver a SIGSEGV with the IDT address to the user space process instead of SIGILL.
-hpa
Quoted 7 lines · H. Peter Anvin
Quoted 3 lines · Linus Torvalds
Not necessarily. Don't we basically do exactly that for the F00F bug workaround, for example?
We do, but only after matching on an exact address (
is_f00f_bug()). Note also thatis_f00f_bug()isn't conditional onPF_USER.
Right. But I'm wondering why you care? There's nothing we can do about spurious page faults if they dp happen. The PF_USER thing we do means that bad_area_nosemaphore will go through the "send signal" path.
I guess we can remove the setting of PF_USER, but that would just mean that then we have to test for "is_user_vm()" in bad_area_semaphore instead. So the end result would be exactly the same.
And my point was that we actually do have this "users can cause page faults on IDT etc accesses" as a real thing.
So basically: what do you propose to do? You basically can't remove the line without adding it somewhere else.
Linus
Quoted 17 lines · Linus Torvalds
Quoted 7 lines · H. Peter Anvin
Quoted 3 lines · Linus Torvalds
Not necessarily. Don't we basically do exactly that for the F00F bug workaround, for example?
We do, but only after matching on an exact address (
is_f00f_bug()). Note also thatis_f00f_bug()isn't conditional onPF_USER.Right. But I'm wondering why you care? There's nothing we can do about spurious page faults if they dp happen. The
PF_USERthing we do means thatbad_area_nosemaphorewill go through the "send signal" path.I guess we can remove the setting of
PF_USER, but that would just mean that then we have to test for "is_user_vm()" inbad_area_semaphoreinstead. So the end result would be exactly the same.
Quoted 6 lines
And my point was that we actually do have this "users can cause page faults on IDT etc accesses" as a real thing.
So basically: what do you propose to do? You basically can't remove the line without adding it somewhere else.
is_f00f_bug() already contains:
if (nr == 6) {
do_invalid_op(regs, 0);
return 1;
}... that's where we're supposed to issue SIGILL.
-hpa