Skip to content

Commit 7fe28d7

Browse files
jingzhangosoupton
authored andcommitted
KVM: arm64: vgic-its: Add a data length check in vgic_its_save_*
In all the vgic_its_save_*() functinos, they do not check whether the data length is 8 bytes before calling vgic_write_guest_lock. This patch adds the check. To prevent the kernel from being blown up when the fault occurs, KVM_BUG_ON() is used. And the other BUG_ON()s are replaced together. Cc: [email protected] Signed-off-by: Kunkun Jiang <[email protected]> [Jing: Update with the new entry read/write helpers] Signed-off-by: Jing Zhang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Oliver Upton <[email protected]>
1 parent 8198375 commit 7fe28d7

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,6 @@ static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz,
20862086
static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
20872087
struct its_ite *ite, gpa_t gpa, int ite_esz)
20882088
{
2089-
struct kvm *kvm = its->dev->kvm;
20902089
u32 next_offset;
20912090
u64 val;
20922091

@@ -2095,7 +2094,8 @@ static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
20952094
((u64)ite->irq->intid << KVM_ITS_ITE_PINTID_SHIFT) |
20962095
ite->collection->collection_id;
20972096
val = cpu_to_le64(val);
2098-
return vgic_write_guest_lock(kvm, gpa, &val, ite_esz);
2097+
2098+
return vgic_its_write_entry_lock(its, gpa, val, ite_esz);
20992099
}
21002100

21012101
/**
@@ -2239,7 +2239,6 @@ static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
22392239
static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
22402240
gpa_t ptr, int dte_esz)
22412241
{
2242-
struct kvm *kvm = its->dev->kvm;
22432242
u64 val, itt_addr_field;
22442243
u32 next_offset;
22452244

@@ -2250,7 +2249,8 @@ static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
22502249
(itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
22512250
(dev->num_eventid_bits - 1));
22522251
val = cpu_to_le64(val);
2253-
return vgic_write_guest_lock(kvm, ptr, &val, dte_esz);
2252+
2253+
return vgic_its_write_entry_lock(its, ptr, val, dte_esz);
22542254
}
22552255

22562256
/**
@@ -2437,7 +2437,8 @@ static int vgic_its_save_cte(struct vgic_its *its,
24372437
((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) |
24382438
collection->collection_id);
24392439
val = cpu_to_le64(val);
2440-
return vgic_write_guest_lock(its->dev->kvm, gpa, &val, esz);
2440+
2441+
return vgic_its_write_entry_lock(its, gpa, val, esz);
24412442
}
24422443

24432444
/*
@@ -2453,8 +2454,7 @@ static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)
24532454
u64 val;
24542455
int ret;
24552456

2456-
BUG_ON(esz > sizeof(val));
2457-
ret = kvm_read_guest_lock(kvm, gpa, &val, esz);
2457+
ret = vgic_its_read_entry_lock(its, gpa, &val, esz);
24582458
if (ret)
24592459
return ret;
24602460
val = le64_to_cpu(val);
@@ -2492,7 +2492,6 @@ static int vgic_its_save_collection_table(struct vgic_its *its)
24922492
u64 baser = its->baser_coll_table;
24932493
gpa_t gpa = GITS_BASER_ADDR_48_to_52(baser);
24942494
struct its_collection *collection;
2495-
u64 val;
24962495
size_t max_size, filled = 0;
24972496
int ret, cte_esz = abi->cte_esz;
24982497

@@ -2516,10 +2515,7 @@ static int vgic_its_save_collection_table(struct vgic_its *its)
25162515
* table is not fully filled, add a last dummy element
25172516
* with valid bit unset
25182517
*/
2519-
val = 0;
2520-
BUG_ON(cte_esz > sizeof(val));
2521-
ret = vgic_write_guest_lock(its->dev->kvm, gpa, &val, cte_esz);
2522-
return ret;
2518+
return vgic_its_write_entry_lock(its, gpa, 0, cte_esz);
25232519
}
25242520

25252521
/*

arch/arm64/kvm/vgic/vgic.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,29 @@ static inline int vgic_write_guest_lock(struct kvm *kvm, gpa_t gpa,
146146
return ret;
147147
}
148148

149+
static inline int vgic_its_read_entry_lock(struct vgic_its *its, gpa_t eaddr,
150+
u64 *eval, unsigned long esize)
151+
{
152+
struct kvm *kvm = its->dev->kvm;
153+
154+
if (KVM_BUG_ON(esize != sizeof(*eval), kvm))
155+
return -EINVAL;
156+
157+
return kvm_read_guest_lock(kvm, eaddr, eval, esize);
158+
159+
}
160+
161+
static inline int vgic_its_write_entry_lock(struct vgic_its *its, gpa_t eaddr,
162+
u64 eval, unsigned long esize)
163+
{
164+
struct kvm *kvm = its->dev->kvm;
165+
166+
if (KVM_BUG_ON(esize != sizeof(eval), kvm))
167+
return -EINVAL;
168+
169+
return vgic_write_guest_lock(kvm, eaddr, &eval, esize);
170+
}
171+
149172
/*
150173
* This struct provides an intermediate representation of the fields contained
151174
* in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC

0 commit comments

Comments
 (0)