Fix KVM_GET_REG_LIST invalid register ID regression
cover Fix KVM_GET_REG_LIST invalid register ID regression
Since commit d26c25a9d19b ("arm64: KVM: Tighten guest core register access from userspace"), KVM_{GET,SET}_ONE_REG rejects register IDs that do not correspond to a single underlying architectural register.
This series proposed a fix for this regression along with some related refactoring.
For ease of review, the series is expanded out a little more than necessary. I also want a clean base to build SVE support on, since SVE needs to do some additional filtering to remove FPSIMD V-regs fromKVM_GET_REG_LIST: thus, this series does a bit of refactoring beyond the basic needs of the fix.
An equivalent fix should go to stable, but we may want something more self-contained for that case.
Comments welcome.
This series has not been tested yet.
Dave Martin (3):
KVM: arm64: Factor out core register size determination and validation
KVM: arm64: Factor out KVM_GET_REG_LIST core register enumeration
KVM: arm64: Filter out invalid core register IDs in KVM_GET_REG_LIST
arch/arm64/kvm/guest.c | 96 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 25 deletions(-)
Signature · 1 line
2.1.4
PATCH 1/3 KVM: arm64: Factor out core register size determination and validation
Currently, the only code that needs to deduce the proper size of a KVM core register on arm64 is validate_core_offset().
In order to make this code easier to reuse, this patch factors out the size determination into a separate function core_reg_size_from_offset().
Since validate_core_offset() can be relevant in situations where only a register ID is available, this patch also modifies the interface to validate_core_offset() to take a register ID instead of a struct kvm_one_reg. In order to avoid confusion, the function is renamed to validate_core_reg_id() to reflect the changed semantics.
Subsequent patches will make use of these changes.
No functional change.
--- arch/arm64/kvm/guest.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
arch/arm64/kvm/guest.c +18 -17
@@ -57,11 +57,8 @@ static u64 core_reg_offset_from_id(u64 id)return id & ~(KVM_REG_ARCH_MASK |KVM_REG_SIZE_MASK |KVM_REG_ARM_CORE ); } -static int validate_core_offset (const struct kvm_one_reg *reg) +static int core_reg_size_from_offset (u64 off) { -u64 off =core_reg_offset_from_id (reg->id); -int size; -switch (off) { case KVM_REG_ARM_CORE_REG(regs.regs[0]) ...KVM_REG_ARM_CORE_REG (regs.regs[30 ]):
@@ -72,25 +69,29 @@ static int validate_core_offset(const struct kvm_one_reg *reg)case KVM_REG_ARM_CORE_REG (elr_el1): case KVM_REG_ARM_CORE_REG(spsr[0]) ...KVM_REG_ARM_CORE_REG (spsr[KVM_NR_SPSR -1 ]): - size =sizeof (__u64); -break ; +return sizeof (__u64); case KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]) ...KVM_REG_ARM_CORE_REG (fp_regs.vregs[31 ]): - size =sizeof (__uint128_t); -break ; +return sizeof (__uint128_t);case KVM_REG_ARM_CORE_REG (fp_regs.fpsr):case KVM_REG_ARM_CORE_REG (fp_regs.fpcr): - size =sizeof (__u32); -break ; - -default : -return -EINVAL ; +return sizeof (__u32); } -if (KVM_REG_SIZE (reg->id) == size && -IS_ALIGNED (off, size /sizeof (__u32))) +return -EINVAL ; +} + +static int validate_core_reg_id (u64 id) +{ +u64 off =core_reg_offset_from_id (id); +int size =core_reg_size_from_offset (off); + +if (size <0 ) +return size; + +if (KVM_REG_SIZE (id) == size &&IS_ALIGNED (off, size /sizeof (__u32)))return 0 ;return -EINVAL ;
@@ -115,7 +116,7 @@ static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)return -ENOENT ; -if (validate_core_offset (reg)) +if (validate_core_reg_id (reg->id))return -EINVAL ;if (copy_to_user (uaddr, ((u32 *)regs) + off,KVM_REG_SIZE (reg->id)))
@@ -140,7 +141,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)return -ENOENT ; -if (validate_core_offset (reg)) +if (validate_core_reg_id (reg->id))return -EINVAL ;if (KVM_REG_SIZE (reg->id) >sizeof (tmp))
Signature · 1 line
2.1.4
PATCH 2/3 KVM: arm64: Factor out KVM_GET_REG_LIST core register enumeration
In reply to Dave MartinCurrently, enumeration of the core register IDs for KVM_GET_REG_LIST is open-coded in kvm_arm_copy_reg_indices().
This will become cumbersome as the enumeration logic becomes more complex. In preparation for future patches, this patch factors the code out into a separate function copy_core_reg_indices(), consistently with the way other classes of registers are handled.
No functional change.
--- arch/arm64/kvm/guest.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
arch/arm64/kvm/guest.c +17 -7
@@ -194,6 +194,20 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)return -EINVAL ; } +static int copy_core_reg_indices (u64 __user **uind) +{ +unsigned int i; +const u64 core_reg =KVM_REG_ARM64 |KVM_REG_SIZE_U64 |KVM_REG_ARM_CORE ; + +for (i =0 ; i <sizeof (struct kvm_regs ) /sizeof (__u32); i++) { +if (put_user (core_reg | i, *uind)) +return -EFAULT ; + ++*uind; + } + +return 0 ; +} +static unsigned long num_core_regs (void ) {return sizeof (struct kvm_regs ) /sizeof (__u32);
@@ -270,15 +284,11 @@ unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)*/ int kvm_arm_copy_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) { -unsigned int i; -const u64 core_reg =KVM_REG_ARM64 |KVM_REG_SIZE_U64 |KVM_REG_ARM_CORE ;int ret; -for (i =0 ; i <sizeof (struct kvm_regs ) /sizeof (__u32); i++) { -if (put_user (core_reg | i, uindices)) -return -EFAULT ; - uindices++; - } + ret =copy_core_reg_indices (&uindices); +if (ret) +return ret; ret =kvm_arm_copy_fw_reg_indices (vcpu, uindices);if (ret)
Signature · 1 line
2.1.4
PATCH 3/3 KVM: arm64: Filter out invalid core register IDs in KVM_GET_REG_LIST
In reply to Dave MartinSince commit d26c25a9d19b ("arm64: KVM: Tighten guest core register access from userspace"), KVM_{GET,SET}_ONE_REG rejects register IDs that do not correspond to a single underlying architectural register.
KVM_GET_REG_LIST was not changed to match however: instead, it simply yields a list of 32-bit register IDs that together cover the whole kvm_regs struct. This means that if userspace tries to use the resulting list of IDs directly to drive calls to KVM_*_ONE_REG, some of those calls will now fail.
This was not the intention. Instead, iterating KVM_*_ONE_REG over the list of IDs returned by KVM_GET_REG_LIST should be guaranteed to work.
This patch fixes the problem by marking each core register ID with the appropriate size, and by filtering out any misaligned IDs using validate_core_reg_id().
The number of core register IDs resulting from the process is now not trivial to determine (even though it is still theoretically compile-time constant). In order to avoid implementing complex logic twice, copy_core_reg_indices() is now used both to count and copy the indices. This will add a small runtime cost, but this is deemed acceptable since KVM_GET_REG_LIST is not performance- critical for userspace.
--- arch/arm64/kvm/guest.c | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-)
arch/arm64/kvm/guest.c +42 -7
@@ -197,20 +197,55 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)static int copy_core_reg_indices (u64 __user **uind) {unsigned int i; -const u64 core_reg =KVM_REG_ARM64 |KVM_REG_SIZE_U64 |KVM_REG_ARM_CORE ; +int total =0 ;for (i =0 ; i <sizeof (struct kvm_regs ) /sizeof (__u32); i++) { -if (put_user (core_reg | i, *uind)) -return -EFAULT ; - ++*uind; +u64 id =KVM_REG_ARM64 |KVM_REG_ARM_CORE | i; +int size =core_reg_size_from_offset (i); + +if (size <0 ) +continue ; + +switch (size) { +case sizeof (__u32): + id |=KVM_REG_SIZE_U32 ; +break ; + +case sizeof (__u64): + id |=KVM_REG_SIZE_U64 ; +break ; + +case sizeof (__uint128_t): + id |=KVM_REG_SIZE_U128 ; +break ; + +default : +WARN_ON (1 ); +/* +* leave reg size field as zero: +* validate_core_reg_id() will reject it +*/ +break ; + } + +if (validate_core_reg_id (id)) +continue ; + +if (uind) { +if (put_user (id, *uind)) +return -EFAULT ; + ++*uind; + } + + ++total; } -return 0 ; +return total; }static unsigned long num_core_regs (void ) { -return sizeof (struct kvm_regs ) /sizeof (__u32); +return copy_core_reg_indices (NULL ); }/**
@@ -287,7 +322,7 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)int ret; ret =copy_core_reg_indices (&uindices); -if (ret) +if (ret <0 )return ret; ret =kvm_arm_copy_fw_reg_indices (vcpu, uindices);
Signature · 1 line
2.1.4