Skip to contentSkip to conversation

target/i386: seg_helper: Correct segement selector nullification in the RET/IRET helper

PATCH Applied Bin Meng 3 participants 9 messages Nov 13, 2020 – Nov 30, 2020

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v2 1/1 target/i386: seg_helper: Correct segement selector nullification in the RET/IRET helper

From: Bin Meng <[email protected]>

Per the SDM, when returning to outer privilege level, for segment registers (ES, FS, GS, and DS) if the check fails, the segment selector becomes null, but QEMU clears the base/limit/flags as well as nullifying the segment selector, which should be a spec violation.

Real hardware seems to be compliant with the spec, at least on one Coffee Lake board I tested.

---

Changes in v2:
- clearing the DESC_P bit in the segment descriptor

 target/i386/seg_helper.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/i386/seg_helper.c +4 -1
index be88938..d539573 100644
@@ -2108,7 +2108,10 @@ static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl)
     if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
         /* data or non conforming code segment */
         if (dpl < cpl) {
-            cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
+            cpu_x86_load_seg_cache(env, seg_reg, 0,
+                                   env->segs[seg_reg].base,
+                                   env->segs[seg_reg].limit,
+                                   env->segs[seg_reg].flags & ~DESC_P_MASK);
         }
     }
 }
Signature · 1 line
2.7.4
#1 lore raw
Quoted 27 lines · Bin Meng

From: Bin Meng <[email protected]>

Per the SDM, when returning to outer privilege level, for segment registers (ES, FS, GS, and DS) if the check fails, the segment selector becomes null, but QEMU clears the base/limit/flags as well as nullifying the segment selector, which should be a spec violation.

Real hardware seems to be compliant with the spec, at least on one Coffee Lake board I tested.

---

Changes in v2:
- clearing the DESC_P bit in the segment descriptor

  target/i386/seg_helper.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
target/i386/seg_helper.c +0 -0
index be88938..d539573 100644
@@ -2108,7 +2108,10 @@ static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl)
      if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {

target/i386/seg_helper.c static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl) · line 2117

Quoted 10 lines
target/i386/seg_helper.c +4 -1
          /* data or non conforming code segment */
          if (dpl < cpl) {
-            cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
+            cpu_x86_load_seg_cache(env, seg_reg, 0,
+                                   env->segs[seg_reg].base,
+                                   env->segs[seg_reg].limit,
+                                   env->segs[seg_reg].flags & ~DESC_P_MASK);
          }
      }
  }

Queued, thanks. It would be nicer if the commit message explained how the guest can notice the difference.

Paolo

author
#2 lore raw

Hi Paolo,

Quoted 33 lines · Paolo Bonzini
Quoted 31 lines · Bin Meng

From: Bin Meng <[email protected]>

Per the SDM, when returning to outer privilege level, for segment registers (ES, FS, GS, and DS) if the check fails, the segment selector becomes null, but QEMU clears the base/limit/flags as well as nullifying the segment selector, which should be a spec violation.

Real hardware seems to be compliant with the spec, at least on one Coffee Lake board I tested.

---

Changes in v2:
- clearing the DESC_P bit in the segment descriptor

  target/i386/seg_helper.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
target/i386/seg_helper.c +2 -1
index be88938..d539573 100644
@@ -2108,7 +2108,10 @@ static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl)
      if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
          /* data or non conforming code segment */
          if (dpl < cpl) {
-            cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
+            cpu_x86_load_seg_cache(env, seg_reg, 0,
+                                   env->segs[seg_reg].base,
Quoted 8 lines

target/i386/seg_helper.c static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl) · line 2117

Quoted 6 lines
target/i386/seg_helper.c +2 -0
+                                   env->segs[seg_reg].limit,
+                                   env->segs[seg_reg].flags & ~DESC_P_MASK);
          }
      }
  }

Queued, thanks.

Thanks!

Quoted 2 lines

It would be nicer if the commit message explained how the guest can notice the difference.

The commit message says "Per the SDM" :) The actual failure case involves a special code sequence that is exposed in VxWorks guest testing. Linux does not expose this however.

Regards,
Bin

#3 lore raw
Quoted 6 lines · Bin Meng
Quoted 2 lines

It would be nicer if the commit message explained how the guest can notice the difference.

The commit message says "Per the SDM" :) The actual failure case involves a special code sequence that is exposed in VxWorks guest testing. Linux does not expose this however.

I see. Is there any chance you could write a testcase for kvm-unit-tests? Or just explain how to write such a test, and then I can write it myself; it's not clear to me how the guest can observe the base and limit of a non-present segment.

Paolo

author
#4 lore raw

Hi Paolo,

Quoted 5 lines · Paolo Bonzini
Quoted 3 lines · Bin Meng
Quoted 2 lines

It would be nicer if the commit message explained how the guest can notice the difference.

Quoted 8 lines
Quoted 3 lines

The commit message says "Per the SDM" :) The actual failure case involves a special code sequence that is exposed in VxWorks guest testing. Linux does not expose this however.

I see. Is there any chance you could write a testcase for kvm-unit-tests? Or just explain how to write such a test, and then I can write it myself; it's not clear to me how the guest can observe the base and limit of a non-present segment.

I am not familiar with kvm-unit-test. The original issue cannot be reproduced with a KVM enabled QEMU as the codes-in-flaw is in the emulation path.

Regards,
Bin

#5 lore raw
Quoted 8 lines · Bin Meng
Quoted 4 lines

I see. Is there any chance you could write a testcase for kvm-unit-tests? Or just explain how to write such a test, and then I can write it myself; it's not clear to me how the guest can observe the base and limit of a non-present segment.

I am not familiar with kvm-unit-test. The original issue cannot be reproduced with a KVM enabled QEMU as the codes-in-flaw is in the emulation path.

kvm-unit-tests, despite the name, is a set generic tests for CPU behavior; it works with other accelerators that QEMU supports including the emulation path. You can find it at https://gitlab.com/kvm-unit-tests/kvm-unit-tests.

If you explain in enough detail how VxWorks triggers the bug, I can take care of writing the test.

Paolo

author
#6 lore raw

Hi Paolo,

Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Bin Meng
Quoted 4 lines

I see. Is there any chance you could write a testcase for kvm-unit-tests? Or just explain how to write such a test, and then I can write it myself; it's not clear to me how the guest can observe the base and limit of a non-present segment.

Quoted 8 lines
Quoted 3 lines

I am not familiar with kvm-unit-test. The original issue cannot be reproduced with a KVM enabled QEMU as the codes-in-flaw is in the emulation path.

kvm-unit-tests, despite the name, is a set generic tests for CPU behavior; it works with other accelerators that QEMU supports including the emulation path. You can find it at
https://gitlab.com/kvm-unit-tests/kvm-unit-tests.

I see. Thanks for the info.

Quoted 2 lines

If you explain in enough detail how VxWorks triggers the bug, I can take care of writing the test.

I will try to create a test case using the kvm-unit-tests framework.

Regards,
Bin

#7 lore raw
In reply to Paolo Bonzini
Quoted 1 line · Paolo Bonzini

On 13/11/20 10:56, Bin Meng wrote:

...

Quoted 3 lines

Queued, thanks.  It would be nicer if the commit message explained how the guest can notice the difference.

Typo "segement" -> "segment" in subject.

Quoted 4 lines

Paolo

author
#8 lore raw
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 1 line · Paolo Bonzini

On 13/11/20 10:56, Bin Meng wrote:

...

Quoted 3 lines

Queued, thanks. It would be nicer if the commit message explained how the guest can notice the difference.

Typo "segement" -> "segment" in subject.

Thanks Philippe.

Paolo, please let me know if you need me to respin this patch, or you can fix this when applying.

Regards,
Bin