Skip to contentSkip to conversation

x86/altp2m: Fixed crash with INVALID_ALTP2M EPTP index

PATCH Superseded Razvan Cojocaru 2 participants 7 messages Jun 25, 2018 – Jun 26, 2018

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/altp2m: Fixed crash with INVALID_ALTP2M EPTP index

When SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS is set, vmx_vcpu_update_eptp() __vmwrites() EPTP_INDEX in altp2m_vcpu_destroy(). This means that when disabling altp2m on a domain after xc_altp2m_set_vcpu_enable_notify() has been successfully called, EPTP_INDEX ends up being stored as INVALID_ALTP2M. This makes it possible for vmx_vmexit_handler() to __vmread() the stale value after a subsequent call to xc_altp2m_set_vcpu_enable_notify(), and BUG_ON(idx >= MAX_ALTP2M).

---
Changes since V1:
- Re-wrote the fix to affect the altp2m code instead of the code around the BUG_ON().
- Updated the patch description (and title - since the crash is really a host, not a domain, crash).

---
 xen/arch/x86/mm/altp2m.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/mm/altp2m.c +1 -1
index 930bdc2..28c800b 100644
@@ -58,8 +58,8 @@ altp2m_vcpu_destroy(struct vcpu *v)
 
     altp2m_vcpu_reset(v);
 
-    altp2m_vcpu_update_p2m(v);
     altp2m_vcpu_update_vmfunc_ve(v);
+    altp2m_vcpu_update_p2m(v);
 
     if ( v != current )
         vcpu_unpause(v);
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.06.18 at 16:10, <[email protected]> wrote:

When SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS is set, vmx_vcpu_update_eptp() __vmwrites() EPTP_INDEX in altp2m_vcpu_destroy(). This means that when disabling altp2m on a domain after xc_altp2m_set_vcpu_enable_notify() has been successfully called, EPTP_INDEX ends up being stored as INVALID_ALTP2M. This makes it possible for vmx_vmexit_handler() to __vmread() the stale value after a subsequent call to xc_altp2m_set_vcpu_enable_notify(), and BUG_ON(idx >= MAX_ALTP2M).

Quoted 8 lines

---
Changes since V1:
- Re-wrote the fix to affect the altp2m code instead of the code around the BUG_ON().
- Updated the patch description (and title - since the crash is really a host, not a domain, crash).

I think we've been there before: Why "fixed" rather than "fix" (or "avoid") in the title? My general view is that a title says what a patch does, not what the state is after it has been applied.

xen/arch/x86/mm/altp2m.c altp2m_vcpu_destroy(struct vcpu *v) · line 62

Quoted 9 lines
xen/arch/x86/mm/altp2m.c +1 -1
@@ -58,8 +58,8 @@ altp2m_vcpu_destroy(struct vcpu *v)
 
     altp2m_vcpu_reset(v);
 
-    altp2m_vcpu_update_p2m(v);
     altp2m_vcpu_update_vmfunc_ve(v);
+    altp2m_vcpu_update_p2m(v);

I agree this addresses this specific incarnation of the problem. However, if the vCPU indeed runs while being manipulated, I don't think you get rid of the race this way. For one, there is e.g. a solitary call to altp2m_vcpu_update_vmfunc_ve() in the handling of HVMOP_altp2m_vcpu_enable_notify. That'll lead to SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS getting set, but EPTP_INDEX won't be written. Whatever previous value is in place would then be read back in VM exit handling.

With that it also looks to me as if the two step (and hence non-atomic from the perspective of the guest) update is a problem. Even with the change above, the VM exit may now happen exactly between the two function calls.

It seems to me that pausing the vCPU is almost unavoidable (and then the ordering of the two calls is relevant only because vmx_vcpu_update_eptp() would better respect the intended new setting of SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS).

I also think that you'd better Cc the VMX maintainers here, even if the fix itself is outside the code their maintainership covers.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 19 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.06.18 at 16:10, <[email protected]> wrote:

When SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS is set, vmx_vcpu_update_eptp() __vmwrites() EPTP_INDEX in altp2m_vcpu_destroy(). This means that when disabling altp2m on a domain after xc_altp2m_set_vcpu_enable_notify() has been successfully called, EPTP_INDEX ends up being stored as INVALID_ALTP2M. This makes it possible for vmx_vmexit_handler() to __vmread() the stale value after a subsequent call to xc_altp2m_set_vcpu_enable_notify(), and BUG_ON(idx >= MAX_ALTP2M).

Quoted 8 lines

---
Changes since V1:
- Re-wrote the fix to affect the altp2m code instead of the code around the BUG_ON().
- Updated the patch description (and title - since the crash is really a host, not a domain, crash).

Quoted 3 lines

I think we've been there before: Why "fixed" rather than "fix" (or "avoid") in the title? My general view is that a title says what a patch does, not what the state is after it has been applied.

I'll change the patch subject.

Quoted 24 lines

xen/arch/x86/mm/altp2m.c altp2m_vcpu_destroy(struct vcpu *v) · line 62

Quoted 9 lines
xen/arch/x86/mm/altp2m.c +1 -1
@@ -58,8 +58,8 @@ altp2m_vcpu_destroy(struct vcpu *v)
 
     altp2m_vcpu_reset(v);
 
-    altp2m_vcpu_update_p2m(v);
     altp2m_vcpu_update_vmfunc_ve(v);
+    altp2m_vcpu_update_p2m(v);

I agree this addresses this specific incarnation of the problem. However, if the vCPU indeed runs while being manipulated, I don't think you get rid of the race this way. For one, there is e.g. a solitary call to altp2m_vcpu_update_vmfunc_ve() in the handling of HVMOP_altp2m_vcpu_enable_notify. That'll lead to SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS getting set, but EPTP_INDEX won't be written. Whatever previous value is in place would then be read back in VM exit handling.

With that it also looks to me as if the two step (and hence non-atomic from the perspective of the guest) update is a problem. Even with the change above, the VM exit may now happen exactly between the two function calls.

Quoted 4 lines

It seems to me that pausing the vCPU is almost unavoidable (and then the ordering of the two calls is relevant only because vmx_vcpu_update_eptp() would better respect the intended new setting of SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS).

I see. Would not then your scenario be covered by altp2m_vcpu_update_vmfunc_ve() always calling altp2m_vcpu_update_p2m() at the very end (and removing duplicate calls to altp2m_vcpu_update_p2m())?

Unless something is very wrong, all calls to altp2m_vcpu_update_vmfunc_ve() _should_ happen within a pause, so no calls to the VM exit handler should occur between them if they become a single block of logic within altp2m_vcpu_update_vmfunc_ve().

Quoted 2 lines

I also think that you'd better Cc the VMX maintainers here, even if the fix itself is outside the code their maintainership covers.

Sorry, I don't follow. The MAINTAINTERS file lists only Jun Nakajima and Kevin Tian under "INTEL(R) VT FOR X86 (VT-X)" (where xen/arch/x86/hvm/vmx/ and xen/include/asm-x86/hvm/vmx/ live), and they are both CCd here. What am I missing?

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 26.06.18 at 12:55, <[email protected]> wrote:
Quoted 25 lines · Jan Beulich

xen/arch/x86/mm/altp2m.c altp2m_vcpu_destroy(struct vcpu *v) · line 62

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.06.18 at 16:10, <[email protected]> wrote:
xen/arch/x86/mm/altp2m.c +1 -1
@@ -58,8 +58,8 @@ altp2m_vcpu_destroy(struct vcpu *v)
 
     altp2m_vcpu_reset(v);
 
-    altp2m_vcpu_update_p2m(v);
     altp2m_vcpu_update_vmfunc_ve(v);
+    altp2m_vcpu_update_p2m(v);

I agree this addresses this specific incarnation of the problem. However, if the vCPU indeed runs while being manipulated, I don't think you get rid of the race this way. For one, there is e.g. a solitary call to altp2m_vcpu_update_vmfunc_ve() in the handling of HVMOP_altp2m_vcpu_enable_notify. That'll lead to SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS getting set, but EPTP_INDEX won't be written. Whatever previous value is in place would then be read back in VM exit handling.

With that it also looks to me as if the two step (and hence non-atomic from the perspective of the guest) update is a problem. Even with the change above, the VM exit may now happen exactly between the two function calls.

Quoted 8 lines
Quoted 4 lines

It seems to me that pausing the vCPU is almost unavoidable (and then the ordering of the two calls is relevant only because vmx_vcpu_update_eptp() would better respect the intended new setting of SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS).

I see. Would not then your scenario be covered by altp2m_vcpu_update_vmfunc_ve() always calling altp2m_vcpu_update_p2m() at the very end (and removing duplicate calls to altp2m_vcpu_update_p2m())?

No - there would then still be a window of time where the bit is set but the index was not (yet) written.

Quoted 4 lines

Unless something is very wrong, all calls to altp2m_vcpu_update_vmfunc_ve() _should_ happen within a pause, so no calls to the VM exit handler should occur between them if they become a single block of logic within altp2m_vcpu_update_vmfunc_ve().

Did I misunderstand your analysis mails then? It looked to me as if you were observing exactly such races, because of the vCPU not being paused.

Quoted 7 lines
Quoted 2 lines

I also think that you'd better Cc the VMX maintainers here, even if the fix itself is outside the code their maintainership covers.

Sorry, I don't follow. The MAINTAINTERS file lists only Jun Nakajima and Kevin Tian under "INTEL(R) VT FOR X86 (VT-X)" (where xen/arch/x86/hvm/vmx/ and xen/include/asm-x86/hvm/vmx/ live), and they are both CCd here. What am I missing?

Your patch submission didn't have them on Cc, I've added them when replying.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 39 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 26.06.18 at 12:55, <[email protected]> wrote:
Quoted 25 lines · Jan Beulich

xen/arch/x86/mm/altp2m.c altp2m_vcpu_destroy(struct vcpu *v) · line 62

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.06.18 at 16:10, <[email protected]> wrote:
xen/arch/x86/mm/altp2m.c +1 -1
@@ -58,8 +58,8 @@ altp2m_vcpu_destroy(struct vcpu *v)
 
     altp2m_vcpu_reset(v);
 
-    altp2m_vcpu_update_p2m(v);
     altp2m_vcpu_update_vmfunc_ve(v);
+    altp2m_vcpu_update_p2m(v);

I agree this addresses this specific incarnation of the problem. However, if the vCPU indeed runs while being manipulated, I don't think you get rid of the race this way. For one, there is e.g. a solitary call to altp2m_vcpu_update_vmfunc_ve() in the handling of HVMOP_altp2m_vcpu_enable_notify. That'll lead to SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS getting set, but EPTP_INDEX won't be written. Whatever previous value is in place would then be read back in VM exit handling.

With that it also looks to me as if the two step (and hence non-atomic from the perspective of the guest) update is a problem. Even with the change above, the VM exit may now happen exactly between the two function calls.

Quoted 8 lines
Quoted 4 lines

It seems to me that pausing the vCPU is almost unavoidable (and then the ordering of the two calls is relevant only because vmx_vcpu_update_eptp() would better respect the intended new setting of SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS).

I see. Would not then your scenario be covered by altp2m_vcpu_update_vmfunc_ve() always calling altp2m_vcpu_update_p2m() at the very end (and removing duplicate calls to altp2m_vcpu_update_p2m())?

No - there would then still be a window of time where the bit is set but the index was not (yet) written.

Quoted 8 lines
Quoted 4 lines

Unless something is very wrong, all calls to altp2m_vcpu_update_vmfunc_ve() _should_ happen within a pause, so no calls to the VM exit handler should occur between them if they become a single block of logic within altp2m_vcpu_update_vmfunc_ve().

Did I misunderstand your analysis mails then? It looked to me as if you were observing exactly such races, because of the vCPU not being paused.

No, you've understood my initial email perfectly, but I had unfortunately misread the log. I've attempted to clarify things with the follow-up (which clearly has not been eloquent) (the "apologies, I seem to have misread the crash timing" email).

I've misread my log because altp2m_vcpu_update_vmfunc_ve() was called the first time on handling HVMOP_altp2m_vcpu_enable_notify, and a second time on altp2m_vcpu_destroy() - and looking at it I've first thought that the call leading to the crash happened in altp2m_vcpu_destroy() (which would have indeed implied a nasty race with the VM exit handler).

But then I've looked closer and found that the crash was happening soon after the HVMOP_altp2m_vcpu_enable_notify handling, in the second run of the test application (the first run was writing INVALID_ALTP2M in EPTP_INDEX on altp2m_vcpu_destroy()).

The VCPU pausing seem to work as expected. Sorry for the misunderstanding.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

I also think that you'd better Cc the VMX maintainers here, even if the fix itself is outside the code their maintainership covers.

Sorry, I don't follow. The MAINTAINTERS file lists only Jun Nakajima and Kevin Tian under "INTEL(R) VT FOR X86 (VT-X)" (where xen/arch/x86/hvm/vmx/ and xen/include/asm-x86/hvm/vmx/ live), and they are both CCd here. What am I missing?

Your patch submission didn't have them on Cc, I've added them when replying.

Understood, thanks.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 60 lines · Razvan Cojocaru
Quoted 39 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 26.06.18 at 12:55, <[email protected]> wrote:
Quoted 25 lines · Jan Beulich

xen/arch/x86/mm/altp2m.c altp2m_vcpu_destroy(struct vcpu *v) · line 62

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.06.18 at 16:10, <[email protected]> wrote:
xen/arch/x86/mm/altp2m.c +1 -1
@@ -58,8 +58,8 @@ altp2m_vcpu_destroy(struct vcpu *v)
 
     altp2m_vcpu_reset(v);
 
-    altp2m_vcpu_update_p2m(v);
     altp2m_vcpu_update_vmfunc_ve(v);
+    altp2m_vcpu_update_p2m(v);

I agree this addresses this specific incarnation of the problem. However, if the vCPU indeed runs while being manipulated, I don't think you get rid of the race this way. For one, there is e.g. a solitary call to altp2m_vcpu_update_vmfunc_ve() in the handling of HVMOP_altp2m_vcpu_enable_notify. That'll lead to SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS getting set, but EPTP_INDEX won't be written. Whatever previous value is in place would then be read back in VM exit handling.

With that it also looks to me as if the two step (and hence non-atomic from the perspective of the guest) update is a problem. Even with the change above, the VM exit may now happen exactly between the two function calls.

Quoted 8 lines
Quoted 4 lines

It seems to me that pausing the vCPU is almost unavoidable (and then the ordering of the two calls is relevant only because vmx_vcpu_update_eptp() would better respect the intended new setting of SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS).

I see. Would not then your scenario be covered by altp2m_vcpu_update_vmfunc_ve() always calling altp2m_vcpu_update_p2m() at the very end (and removing duplicate calls to altp2m_vcpu_update_p2m())?

No - there would then still be a window of time where the bit is set but the index was not (yet) written.

Quoted 8 lines
Quoted 4 lines

Unless something is very wrong, all calls to altp2m_vcpu_update_vmfunc_ve() _should_ happen within a pause, so no calls to the VM exit handler should occur between them if they become a single block of logic within altp2m_vcpu_update_vmfunc_ve().

Did I misunderstand your analysis mails then? It looked to me as if you were observing exactly such races, because of the vCPU not being paused.

No, you've understood my initial email perfectly, but I had unfortunately misread the log. I've attempted to clarify things with the follow-up (which clearly has not been eloquent) (the "apologies, I seem to have misread the crash timing" email).

I've misread my log because altp2m_vcpu_update_vmfunc_ve() was called the first time on handling HVMOP_altp2m_vcpu_enable_notify, and a second time on altp2m_vcpu_destroy() - and looking at it I've first thought that the call leading to the crash happened in altp2m_vcpu_destroy() (which would have indeed implied a nasty race with the VM exit handler).

Quoted 6 lines

But then I've looked closer and found that the crash was happening soon after the HVMOP_altp2m_vcpu_enable_notify handling, in the second run of the test application (the first run was writing INVALID_ALTP2M in EPTP_INDEX on altp2m_vcpu_destroy()).

The VCPU pausing seem to work as expected. Sorry for the misunderstanding.

So assuming that the HVMOPs are properly handled atomically with respect to their corresponding VCPU (which I now believe to be the case), the only possible issue that remains (that I can think of) is the case where EPTP_INDEX has been saved in a previous run of a test application, but is not correct for the current state of the guest.

Then HVMOP_altp2m_vcpu_enable_notify is handled, SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS is set, and the VM exit handler is called, which would result in working with the wrong index. This, I believe, can be fixed by either calling altp2m_vcpu_update_p2m() all the time immediately after all altp2m_vcpu_update_vmfunc_ve() calls, or making it a part of altp2m_vcpu_update_vmfunc_ve() (hence my previous proposal).

Does that sound reasonable?

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 26.06.18 at 14:49, <[email protected]> wrote:

So assuming that the HVMOPs are properly handled atomically with respect to their corresponding VCPU (which I now believe to be the case), the only possible issue that remains (that I can think of) is the case where EPTP_INDEX has been saved in a previous run of a test application, but is not correct for the current state of the guest.

Then HVMOP_altp2m_vcpu_enable_notify is handled, SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS is set, and the VM exit handler is called, which would result in working with the wrong index. This, I believe, can be fixed by either calling altp2m_vcpu_update_p2m() all the time immediately after all altp2m_vcpu_update_vmfunc_ve() calls, or making it a part of altp2m_vcpu_update_vmfunc_ve() (hence my previous proposal).

Quoted 1 line

Does that sound reasonable?

I think so, yes, if the pausing works as expected.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel