Skip to contentSkip to conversation

x86/svm: Don't clobber eax and edx if an RDMSR intercept fails

PATCH In review Andrew Cooper 4 participants 5 messages Nov 9, 2016 – Nov 10, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Don't clobber eax and edx if an RDMSR intercept fails

The original code has a bug; eax and edx get unconditionally updated even when hvm_msr_read_intercept() doesn't return X86EMUL_OKAY.

It is only by blind luck (vmce_rdmsr() eagerly initialising its msr_content pointer) that this isn't an information leak into guests.

While fixing this bug, reduce the scope of msr_content and initialise it to 0. This makes it obvious that a stack leak won't occur, even if there were to be a buggy codepath in hvm_msr_read_intercept().

Also make some non-functional improvements. Make the insn_len calculation common, and reduce the quantity of explicit casting by making better use of the existing register names.

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/hvm/svm/svm.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/svm/svm.c +17 -15
index 16427f6..6530e22 100644
@@ -1948,26 +1948,28 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
 
 static void svm_do_msr_access(struct cpu_user_regs *regs)
 {
-    int rc, inst_len;
     struct vcpu *v = current;
-    struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
-    uint64_t msr_content;
+    bool rdmsr = v->arch.hvm_svm.vmcb->exitinfo1 == 0;
+    int rc, inst_len = __get_instruction_length(
+        v, rdmsr ? INSTR_RDMSR : INSTR_WRMSR);
+
+    if ( inst_len == 0 )
+        return;
 
-    if ( vmcb->exitinfo1 == 0 )
+    if ( rdmsr )
     {
-        if ( (inst_len = __get_instruction_length(v, INSTR_RDMSR)) == 0 )
-            return;
-        rc = hvm_msr_read_intercept(regs->ecx, &msr_content);
-        regs->eax = (uint32_t)msr_content;
-        regs->edx = (uint32_t)(msr_content >> 32);
+        uint64_t msr_content = 0;
+
+        rc = hvm_msr_read_intercept(regs->_ecx, &msr_content);
+        if ( rc == X86EMUL_OKAY )
+        {
+            regs->rax = (uint32_t)msr_content;
+            regs->rdx = (uint32_t)(msr_content >> 32);
+        }
     }
     else
-    {
-        if ( (inst_len = __get_instruction_length(v, INSTR_WRMSR)) == 0 )
-            return;
-        msr_content = ((uint64_t)regs->edx << 32) | (uint32_t)regs->eax;
-        rc = hvm_msr_write_intercept(regs->ecx, msr_content, 1);
-    }
+        rc = hvm_msr_write_intercept(regs->_ecx,
+                                     (regs->rdx << 32) | regs->_eax, 1);
 
     if ( rc == X86EMUL_OKAY )
         __update_guest_eip(regs, inst_len);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 09.11.16 at 13:28, <[email protected]> wrote:

The original code has a bug; eax and edx get unconditionally updated even when hvm_msr_read_intercept() doesn't return X86EMUL_OKAY.

It is only by blind luck (vmce_rdmsr() eagerly initialising its msr_content pointer) that this isn't an information leak into guests.

While fixing this bug, reduce the scope of msr_content and initialise it to 0. This makes it obvious that a stack leak won't occur, even if there were to be a buggy codepath in hvm_msr_read_intercept().

Quoted 5 lines

Also make some non-functional improvements. Make the insn_len calculation common, and reduce the quantity of explicit casting by making better use of the existing register names.

with two possible further adjustments:

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 1954

Quoted 13 lines
xen/arch/x86/hvm/svm/svm.c +3 -3
@@ -1948,26 +1948,28 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
 
 static void svm_do_msr_access(struct cpu_user_regs *regs)
 {
-    int rc, inst_len;
     struct vcpu *v = current;
-    struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
-    uint64_t msr_content;
+    bool rdmsr = v->arch.hvm_svm.vmcb->exitinfo1 == 0;
+    int rc, inst_len = __get_instruction_length(
+        v, rdmsr ? INSTR_RDMSR : INSTR_WRMSR);

With all of this I think it wouldn't make the patch worse to look at if you also renamed v -> curr.

Quoted 8 lines
xen/arch/x86/hvm/svm/svm.c +3 -3
+    if ( inst_len == 0 )
+        return;
 
-    if ( vmcb->exitinfo1 == 0 )
+    if ( rdmsr )
     {
-        if ( (inst_len = __get_instruction_length(v, INSTR_RDMSR)) == 0 )
-            return;

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 1967

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +7 -3
-        rc = hvm_msr_read_intercept(regs->ecx, &msr_content);
-        regs->eax = (uint32_t)msr_content;
-        regs->edx = (uint32_t)(msr_content >> 32);
+        uint64_t msr_content = 0;
+
+        rc = hvm_msr_read_intercept(regs->_ecx, &msr_content);
+        if ( rc == X86EMUL_OKAY )
+        {
+            regs->rax = (uint32_t)msr_content;
+            regs->rdx = (uint32_t)(msr_content >> 32);

While the first of the casts is needed, the second one isn't.

Jan

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

#2 lore raw
Quoted 23 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 09.11.16 at 13:28, <[email protected]> wrote:

The original code has a bug; eax and edx get unconditionally updated even when hvm_msr_read_intercept() doesn't return X86EMUL_OKAY.

It is only by blind luck (vmce_rdmsr() eagerly initialising its msr_content pointer) that this isn't an information leak into guests.

While fixing this bug, reduce the scope of msr_content and initialise it to 0. This makes it obvious that a stack leak won't occur, even if there were to be a buggy codepath in hvm_msr_read_intercept().

Quoted 5 lines

Also make some non-functional improvements. Make the insn_len calculation common, and reduce the quantity of explicit casting by making better use of the existing register names.

with two possible further adjustments:

Quoted 4 lines
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -1948,26 +1948,28 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
 
Quoted 11 lines

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 1954

Quoted 9 lines
xen/arch/x86/hvm/svm/svm.c +3 -3
 static void svm_do_msr_access(struct cpu_user_regs *regs)
 {
-    int rc, inst_len;
     struct vcpu *v = current;
-    struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
-    uint64_t msr_content;
+    bool rdmsr = v->arch.hvm_svm.vmcb->exitinfo1 == 0;
+    int rc, inst_len = __get_instruction_length(
+        v, rdmsr ? INSTR_RDMSR : INSTR_WRMSR);

With all of this I think it wouldn't make the patch worse to look at if you also renamed v -> curr.

I am sure at least one version of this patch had that adjustment. I will fix it.

Quoted 19 lines
Quoted 9 lines
xen/arch/x86/hvm/svm/svm.c +3 -3
+    if ( inst_len == 0 )
+        return;
 
-    if ( vmcb->exitinfo1 == 0 )
+    if ( rdmsr )
     {
-        if ( (inst_len = __get_instruction_length(v, INSTR_RDMSR)) == 0 )
-            return;

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 1967

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +7 -3
-        rc = hvm_msr_read_intercept(regs->ecx, &msr_content);
-        regs->eax = (uint32_t)msr_content;
-        regs->edx = (uint32_t)(msr_content >> 32);
+        uint64_t msr_content = 0;
+
+        rc = hvm_msr_read_intercept(regs->_ecx, &msr_content);
+        if ( rc == X86EMUL_OKAY )
+        {
+            regs->rax = (uint32_t)msr_content;
+            regs->rdx = (uint32_t)(msr_content >> 32);
Quoted 1 line

While the first of the casts is needed, the second one isn't.

Right, but removing the cast make the code harder to read. This at least is visually obvious that msr_content is being split in half.

~Andrew

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

Reviewed-by on v1 1/1
#3 lore raw
In reply to Andrew Cooper
Quoted 14 lines · Andrew Cooper

The original code has a bug; eax and edx get unconditionally updated even when hvm_msr_read_intercept() doesn't return X86EMUL_OKAY.

It is only by blind luck (vmce_rdmsr() eagerly initialising its msr_content pointer) that this isn't an information leak into guests.

While fixing this bug, reduce the scope of msr_content and initialise it to 0. This makes it obvious that a stack leak won't occur, even if there were to be a buggy codepath in hvm_msr_read_intercept().

Also make some non-functional improvements. Make the insn_len calculation common, and reduce the quantity of explicit casting by making better use of the existing register names.

Quoted 6 lines

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

(with or without changes suggested by Jan)

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

#4 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

The original code has a bug; eax and edx get unconditionally updated even when hvm_msr_read_intercept() doesn't return X86EMUL_OKAY.

It is only by blind luck (vmce_rdmsr() eagerly initialising its msr_content pointer) that this isn't an information leak into guests.

While fixing this bug, reduce the scope of msr_content and initialise it to 0. This makes it obvious that a stack leak won't occur, even if there were to be a buggy codepath in hvm_msr_read_intercept().

Quoted 5 lines

Also make some non-functional improvements. Make the insn_len calculation common, and reduce the quantity of explicit casting by making better use of the existing register names.

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