Skip to contentSkip to conversation

KVM: s390: vsie: Do not overwrite guest 3 state on early validity icpt

PATCH New Gautam Gala 2 participants 5 messages Nov 19

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover KVM: s390: vsie: Do not overwrite guest 3 state on early validity icpt

If prepare_cpuflags() rejects the SCB provided by guest 2 with a validity interception, shadow_scb() still calls unshadow_scb(). That copies the guest state from the shadow SCB back into the guest 2 SCB, although the shadow SCB does not hold guest 3 state at that point. Guest 2 then sees its guest 3 state overwritten with zeroes (new or recycled vsie page) or with the state of the previous run (cached vsie page), without guest 3 having run.

Patch 1 copies back only the interception data in this case and clears the stale interception data (icptstatus, ipa, ipb) when a shadow SCB is reused.

Patch 2 adds a selftest for early and late validity interceptions.

Gautam Gala (2):
KVM: s390: vsie: Do not overwrite guest 3 state on early validity icpt
KVM: s390: selftests: Add vsie validity interception tests

 arch/s390/kvm/s390/vsie.c                     |  24 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/s390/vsie_validity_test.c   | 435 ++++++++++++++++++
 3 files changed, 453 insertions(+), 7 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/s390/vsie_validity_test.c
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/2 KVM: s390: vsie: Do not overwrite guest 3 state on early validity icpt

shadow_scb() calls prepare_cpuflags() first, which can reject the SCB provided by guest 2 with a validity interception. This happens before the guest state (gpsw, gg14, gg15, gcr, cputm, ckc, todpr, pp) and the control fields (gbea, fpf) are copied from the original SCB into the shadow SCB.

The error path still calls unshadow_scb(), which copies that state from the shadow SCB back into the original one. The shadow SCB does not hold guest 3 state at this point:

- For a new or recycled vsie page, get_vsie_page() has cleared the shadow SCB, so the guest 3 state in the guest 2 SCB is overwritten with zeroes.
- For a cached vsie page that is looked up again by SCB address, the shadow SCB still holds the state from the end of the previous run, so the current guest 3 state is replaced by the old one.

No guest 3 instruction ran, yet guest 2 sees its guest 3 state (PSW, GRs, CRs, timers) modified. unshadow_scb() also clears FPF_BPBC if facility 82 is available, because fpf was cleared in the shadow SCB just before, and copies back a stale ihcpu.

The SCB and the validity condition are controlled by guest 2, so this is a correctness problem for guest 2 only and does not cross a guest or host boundary.

Only the interception data needs to be passed on in this case. Add unshadow_icpt() for that and use it from unshadow_scb() and from the prepare_cpuflags() failure path, which now returns directly. The other failure paths of shadow_scb() are reached after the guest state has been copied and keep using unshadow_scb().

On a cached vsie page, the interception data in the shadow SCB still holds the values of the previous run. set_validity_icpt() writes ipa, ipb and icptcode, but not icptstatus, so a stale icptstatus would be passed on together with the new validity interception. Also, it is not defined what SIE stores in icptstatus, ipa and ipb when it exits without an interception, so they must not be relied upon to be zero or unchanged. Clear them together with icptcode when the shadow SCB is reused, so that no stale interception data is passed on to guest 2.

Fixes a3508fbe9dc6 ("KVM: s390: vsie: initial support for nested virtualization")
---
 arch/s390/kvm/s390/vsie.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
arch/s390/kvm/s390/vsie.c +17 -7
index 5926689fa226..1f618bb274a8 100644
@@ -424,6 +424,15 @@ static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	}
 }
 
+static void unshadow_icpt(struct kvm_s390_sie_block *scb_o,
+			  struct kvm_s390_sie_block *scb_s)
+{
+	scb_o->icptcode = scb_s->icptcode;
+	scb_o->icptstatus = scb_s->icptstatus;
+	scb_o->ipa = scb_s->ipa;
+	scb_o->ipb = scb_s->ipb;
+}
+
 /* unshadow the scb, copying parameters back to the real scb */
 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 {
@@ -431,10 +440,7 @@ static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
 
 	/* interception */
-	scb_o->icptcode = scb_s->icptcode;
-	scb_o->icptstatus = scb_s->icptstatus;
-	scb_o->ipa = scb_s->ipa;
-	scb_o->ipb = scb_s->ipb;
+	unshadow_icpt(scb_o, scb_s);
 	scb_o->gbea = scb_s->gbea;
 
 	/* timer */
@@ -490,6 +496,9 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 
 	/* make sure we don't have any leftovers when reusing the scb */
 	scb_s->icptcode = 0;
+	scb_s->icptstatus = 0;
+	scb_s->ipa = 0;
+	scb_s->ipb = 0;
 	scb_s->eca = 0;
 	scb_s->ecb = 0;
 	scb_s->ecb2 = 0;
@@ -499,8 +508,10 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	scb_s->fpf = 0;
 
 	rc = prepare_cpuflags(vcpu, vsie_page);
-	if (rc)
-		goto out;
+	if (rc) {
+		unshadow_icpt(scb_o, scb_s);
+		return rc;
+	}
 
 	/* timer */
 	scb_s->cputm = scb_o->cputm;
@@ -614,7 +625,6 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 
 	prepare_ibc(vcpu, vsie_page);
 	rc = shadow_crycb(vcpu, vsie_page);
-out:
 	if (rc)
 		unshadow_scb(vcpu, vsie_page);
 	return rc;
Signature · 1 line
2.56.0
#2 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

shadow_scb() calls prepare_cpuflags() first, which can reject the SCB provided by guest 2 with a validity interception. This happens before the guest state (gpsw, gg14, gg15, gcr, cputm, ckc, todpr, pp) and the control fields (gbea, fpf) are copied from the original SCB into the shadow SCB.

The error path still calls unshadow_scb(), which copies that state from the shadow SCB back into the original one. The shadow SCB does not hold guest 3 state at this point:

- For a new or recycled vsie page, get_vsie_page() has cleared the shadow SCB, so the guest 3 state in the guest 2 SCB is overwritten with zeroes.
- For a cached vsie page that is looked up again by SCB address, the shadow SCB still holds the state from the end of the previous run,
[ ... ]
so that no stale interception data is passed on to guest 2.

Quoted 3 lines
Fixes a3508fbe9dc6 ("KVM: s390: vsie: initial support for nested virtualization")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/2 KVM: s390: selftests: Add vsie validity interception tests

In reply to Gautam Gala

Add tests for validity interceptions raised during vsie shadow SCB setup. An interception raised before any guest 3 state has been copied into the shadow SCB must report only the interception data (icptcode, ipa, ipb) to guest 2. It must not write back guest 3 state, in particular cpuflags, ihcpu and icptstatus.

Check the early validity interception with a new and a cached vsie page, for the RRF, SLSV, GED+GED2 and ESA mode cases. The cached case leaves stale state in the shadow SCB (for example a non-zero icptstatus), so that a bogus write back to guest 2 is detected.

Also check that a valid guest 3 still has its state written back, and that validity interceptions raised later (shadow_crycb(), pin_blocks(), unmapped prefix) still report the reason code and leave the SCB unchanged.

The early validity cases fail without the preceding vsie fix. Skip the test if SIEF2 is not available, and skip the GED+GED2 and CRYCB cases if facility 78 or 76 is missing.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/s390/vsie_validity_test.c   | 435 ++++++++++++++++++
 2 files changed, 436 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390/vsie_validity_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 17a4256e9081..e9b1fec3903f 100644
@@ -227,6 +227,7 @@ TEST_GEN_PROGS_s390 += s390/shared_zeropage_test
 TEST_GEN_PROGS_s390 += s390/ucontrol_test
 TEST_GEN_PROGS_s390 += s390/user_operexec
 TEST_GEN_PROGS_s390 += s390/keyop
+TEST_GEN_PROGS_s390 += s390/vsie_validity_test
 TEST_GEN_PROGS_s390 += rseq_test
 TEST_GEN_PROGS_s390 += s390/irq_routing
 TEST_GEN_PROGS_s390 += mmu_stress_test
tools/testing/selftests/kvm/s390/vsie_validity_test.c new file +435 -0

435 lines not on this page · raw message

Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH v1 2/2] KVM: s390: selftests: Add vsie validity interception tests

Sashiko has reviewed this patch and found no issues.
Quoted 16 lines

Add tests for validity interceptions raised during vsie shadow SCB setup. An interception raised before any guest 3 state has been copied into the shadow SCB must report only the interception data (icptcode, ipa, ipb) to guest 2. It must not write back guest 3 state, in particular cpuflags, ihcpu and icptstatus.

Check the early validity interception with a new and a cached vsie page, for the RRF, SLSV, GED+GED2 and ESA mode cases. The cached case leaves stale state in the shadow SCB (for example a non-zero icptstatus), so that a bogus write back to guest 2 is detected.

Also check that a valid guest 3 still has its state written back, and that validity interceptions raised later (shadow_crycb(), pin_blocks(), unmapped prefix) still report the reason code and leave the SCB unchanged.

Quoted 5 lines

The early validity cases fail without the preceding vsie fix. Skip the test if SIEF2 is not available, and skip the GED+GED2 and CRYCB cases if facility 78 or 76 is missing.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2