Skip to content

Commit 18daea7

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm fix from Paolo Bonzini: "A pretty straightforward fix for a NULL pointer dereference, plus the accompanying reproducer" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: selftests: Add test for uaccesses to non-existent vgic-v2 CPUIF KVM: arm64: vgic-v2: Check for non-NULL vCPU in vgic_v2_parse_attr()
2 parents 50dffbf + 16c2020 commit 18daea7

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

arch/arm64/kvm/vgic/vgic-kvm-device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,12 @@ int kvm_register_vgic_device(unsigned long type)
338338
int vgic_v2_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
339339
struct vgic_reg_attr *reg_attr)
340340
{
341-
int cpuid;
341+
int cpuid = FIELD_GET(KVM_DEV_ARM_VGIC_CPUID_MASK, attr->attr);
342342

343-
cpuid = FIELD_GET(KVM_DEV_ARM_VGIC_CPUID_MASK, attr->attr);
344-
345-
reg_attr->vcpu = kvm_get_vcpu_by_id(dev->kvm, cpuid);
346343
reg_attr->addr = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
344+
reg_attr->vcpu = kvm_get_vcpu_by_id(dev->kvm, cpuid);
345+
if (!reg_attr->vcpu)
346+
return -EINVAL;
347347

348348
return 0;
349349
}

tools/testing/selftests/kvm/aarch64/vgic_init.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ static struct vm_gic vm_gic_create_with_vcpus(uint32_t gic_dev_type,
8484
return v;
8585
}
8686

87+
static struct vm_gic vm_gic_create_barebones(uint32_t gic_dev_type)
88+
{
89+
struct vm_gic v;
90+
91+
v.gic_dev_type = gic_dev_type;
92+
v.vm = vm_create_barebones();
93+
v.gic_fd = kvm_create_device(v.vm, gic_dev_type);
94+
95+
return v;
96+
}
97+
98+
8799
static void vm_gic_destroy(struct vm_gic *v)
88100
{
89101
close(v->gic_fd);
@@ -357,6 +369,40 @@ static void test_vcpus_then_vgic(uint32_t gic_dev_type)
357369
vm_gic_destroy(&v);
358370
}
359371

372+
#define KVM_VGIC_V2_ATTR(offset, cpu) \
373+
(FIELD_PREP(KVM_DEV_ARM_VGIC_OFFSET_MASK, offset) | \
374+
FIELD_PREP(KVM_DEV_ARM_VGIC_CPUID_MASK, cpu))
375+
376+
#define GIC_CPU_CTRL 0x00
377+
378+
static void test_v2_uaccess_cpuif_no_vcpus(void)
379+
{
380+
struct vm_gic v;
381+
u64 val = 0;
382+
int ret;
383+
384+
v = vm_gic_create_barebones(KVM_DEV_TYPE_ARM_VGIC_V2);
385+
subtest_dist_rdist(&v);
386+
387+
ret = __kvm_has_device_attr(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
388+
KVM_VGIC_V2_ATTR(GIC_CPU_CTRL, 0));
389+
TEST_ASSERT(ret && errno == EINVAL,
390+
"accessed non-existent CPU interface, want errno: %i",
391+
EINVAL);
392+
ret = __kvm_device_attr_get(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
393+
KVM_VGIC_V2_ATTR(GIC_CPU_CTRL, 0), &val);
394+
TEST_ASSERT(ret && errno == EINVAL,
395+
"accessed non-existent CPU interface, want errno: %i",
396+
EINVAL);
397+
ret = __kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
398+
KVM_VGIC_V2_ATTR(GIC_CPU_CTRL, 0), &val);
399+
TEST_ASSERT(ret && errno == EINVAL,
400+
"accessed non-existent CPU interface, want errno: %i",
401+
EINVAL);
402+
403+
vm_gic_destroy(&v);
404+
}
405+
360406
static void test_v3_new_redist_regions(void)
361407
{
362408
struct kvm_vcpu *vcpus[NR_VCPUS];
@@ -675,6 +721,9 @@ void run_tests(uint32_t gic_dev_type)
675721
test_vcpus_then_vgic(gic_dev_type);
676722
test_vgic_then_vcpus(gic_dev_type);
677723

724+
if (VGIC_DEV_IS_V2(gic_dev_type))
725+
test_v2_uaccess_cpuif_no_vcpus();
726+
678727
if (VGIC_DEV_IS_V3(gic_dev_type)) {
679728
test_v3_new_redist_regions();
680729
test_v3_typer_accesses();

0 commit comments

Comments
 (0)