Skip to content

Commit e8061f0

Browse files
Nico Boehrhcahca
authored andcommitted
KVM: s390: gaccess: Check if guest address is in memslot
Previously, access_guest_page() did not check whether the given guest address is inside of a memslot. This is not a problem, since kvm_write_guest_page/kvm_read_guest_page return -EFAULT in this case. However, -EFAULT is also returned when copy_to/from_user fails. When emulating a guest instruction, the address being outside a memslot usually means that an addressing exception should be injected into the guest. Failure in copy_to/from_user however indicates that something is wrong in userspace and hence should be handled there. To be able to distinguish these two cases, return PGM_ADDRESSING in access_guest_page() when the guest address is outside guest memory. In access_guest_real(), populate vcpu->arch.pgm.code such that kvm_s390_inject_prog_cond() can be used in the caller for injecting into the guest (if applicable). Since this adds a new return value to access_guest_page(), we need to make sure that other callers are not confused by the new positive return value. There are the following users of access_guest_page(): - access_guest_with_key() does the checking itself (in guest_range_to_gpas()), so this case should never happen. Even if, the handling is set up properly. - access_guest_real() just passes the return code to its callers, which are: - read_guest_real() - see below - write_guest_real() - see below There are the following users of read_guest_real(): - ar_translation() in gaccess.c which already returns PGM_* - setup_apcb10(), setup_apcb00(), setup_apcb11() in vsie.c which always return -EFAULT on read_guest_read() nonzero return - no change - shadow_crycb(), handle_stfle() always present this as validity, this could be handled better but doesn't change current behaviour - no change There are the following users of write_guest_real(): - kvm_s390_store_status_unloaded() always returns -EFAULT on write_guest_real() failure. Fixes: 2293897 ("KVM: s390: add architecture compliant guest access functions") Cc: [email protected] Signed-off-by: Nico Boehr <[email protected]> Reviewed-by: Heiko Carstens <[email protected]> Link: https://lore.kernel.org/r/[email protected] Acked-by: Janosch Frank <[email protected]> Signed-off-by: Heiko Carstens <[email protected]>
1 parent 78f636e commit e8061f0

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

arch/s390/kvm/gaccess.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ static int access_guest_page(struct kvm *kvm, enum gacc_mode mode, gpa_t gpa,
828828
const gfn_t gfn = gpa_to_gfn(gpa);
829829
int rc;
830830

831+
if (!gfn_to_memslot(kvm, gfn))
832+
return PGM_ADDRESSING;
831833
if (mode == GACC_STORE)
832834
rc = kvm_write_guest_page(kvm, gfn, data, offset, len);
833835
else
@@ -985,6 +987,8 @@ int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
985987
gra += fragment_len;
986988
data += fragment_len;
987989
}
990+
if (rc > 0)
991+
vcpu->arch.pgm.code = rc;
988992
return rc;
989993
}
990994

arch/s390/kvm/gaccess.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,12 @@ int read_guest_abs(struct kvm_vcpu *vcpu, unsigned long gpa, void *data,
405405
* @len: number of bytes to copy
406406
*
407407
* Copy @len bytes from @data (kernel space) to @gra (guest real address).
408-
* It is up to the caller to ensure that the entire guest memory range is
409-
* valid memory before calling this function.
410408
* Guest low address and key protection are not checked.
411409
*
412-
* Returns zero on success or -EFAULT on error.
410+
* Returns zero on success, -EFAULT when copying from @data failed, or
411+
* PGM_ADRESSING in case @gra is outside a memslot. In this case, pgm check info
412+
* is also stored to allow injecting into the guest (if applicable) using
413+
* kvm_s390_inject_prog_cond().
413414
*
414415
* If an error occurs data may have been copied partially to guest memory.
415416
*/
@@ -428,11 +429,12 @@ int write_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, void *data,
428429
* @len: number of bytes to copy
429430
*
430431
* Copy @len bytes from @gra (guest real address) to @data (kernel space).
431-
* It is up to the caller to ensure that the entire guest memory range is
432-
* valid memory before calling this function.
433432
* Guest key protection is not checked.
434433
*
435-
* Returns zero on success or -EFAULT on error.
434+
* Returns zero on success, -EFAULT when copying to @data failed, or
435+
* PGM_ADRESSING in case @gra is outside a memslot. In this case, pgm check info
436+
* is also stored to allow injecting into the guest (if applicable) using
437+
* kvm_s390_inject_prog_cond().
436438
*
437439
* If an error occurs data may have been copied partially to kernel space.
438440
*/

0 commit comments

Comments
 (0)