[PATCH v2 04/13] KVM: selftests: Assert that vcpu_{g,s}et_reg() won't truncate
Sean Christopherson
seanjc at google.com
Thu Sep 12 09:17:36 PDT 2024
On Thu, Sep 12, 2024, Andrew Jones wrote:
> On Wed, Sep 11, 2024 at 01:41:49PM GMT, Sean Christopherson wrote:
> > Assert that the the register being read/written by vcpu_{g,s}et_reg() is
> > no larger than a uint64_t, i.e. that a selftest isn't unintentionally
> > truncating the value being read/written.
> >
> > Ideally, the assert would be done at compile-time, but that would limit
> > the checks to hardcoded accesses and/or require fancier compile-time
> > assertion infrastructure to filter out dynamic usage.
> >
> > Signed-off-by: Sean Christopherson <seanjc at google.com>
> > ---
> > tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++
> > 1 file changed, 4 insertions(+)
> >
> > diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> > index 429a7f003fe3..80230e49e35f 100644
> > --- a/tools/testing/selftests/kvm/include/kvm_util.h
> > +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> > @@ -683,6 +683,8 @@ static inline uint64_t vcpu_get_reg(struct kvm_vcpu *vcpu, uint64_t id)
> > uint64_t val;
> > struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val };
> >
> > + TEST_ASSERT(KVM_REG_SIZE(id) <= sizeof(val), "Reg %lx too big", id);
> > +
> > vcpu_ioctl(vcpu, KVM_GET_ONE_REG, ®);
> > return val;
> > }
> > @@ -690,6 +692,8 @@ static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val
> > {
> > struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val };
> >
> > + TEST_ASSERT(KVM_REG_SIZE(id) <= sizeof(val), "Reg %lx too big", id);
> > +
> > vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®);
> > }
> >
> > --
> > 2.46.0.598.g6f2099f65c-goog
> >
>
> Reviewed-by: Andrew Jones <ajones at ventanamicro.com>
>
> Shouldn't patches 3 and 4 come before patch 2 in this series?
Ideally, yes, but for this patch, it gets weird because the output param of
vcpu_reg_get() isn't actually restricted to a 64-bit value prior to patch 2.
E.g. if this patch were merged without that rework, then the assert would be
confusing and arguably flat out wrong.
As for the hack-a-fix, I deliberately ordered it after patch 2 so that it would
be easier for others to (try to) reproduce the bug. I have no objection to
swapping 2 and 3 in the next version.
More information about the kvm-riscv
mailing list