Skip to content

Commit 43c1ff8

Browse files
Quentin PerretMarc Zyngier
authored andcommitted
KVM: arm64: Prevent the donation of no-map pages
Memory regions marked as "no-map" in the host device-tree routinely include TrustZone carev-outs and DMA pools. Although donating such pages to the hypervisor may not breach confidentiality, it could be used to corrupt its state in uncontrollable ways. To prevent this, let's block host-initiated memory transitions targeting "no-map" pages altogether in nVHE protected mode as there should be no valid reason to do this in current operation. Thankfully, the pKVM EL2 hypervisor has a full copy of the host's list of memblock regions, so we can easily check for the presence of the MEMBLOCK_NOMAP flag on a region containing pages being donated from the host. Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Tested-by: Vincent Donnefort <[email protected]> Signed-off-by: Quentin Perret <[email protected]> Signed-off-by: Will Deacon <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 1ed5c24 commit 43c1ff8

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

arch/arm64/kvm/hyp/nvhe/mem_protect.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ struct kvm_mem_range {
193193
u64 end;
194194
};
195195

196-
static bool find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
196+
static struct memblock_region *find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
197197
{
198198
int cur, left = 0, right = hyp_memblock_nr;
199199
struct memblock_region *reg;
@@ -216,18 +216,28 @@ static bool find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
216216
} else {
217217
range->start = reg->base;
218218
range->end = end;
219-
return true;
219+
return reg;
220220
}
221221
}
222222

223-
return false;
223+
return NULL;
224224
}
225225

226226
bool addr_is_memory(phys_addr_t phys)
227227
{
228228
struct kvm_mem_range range;
229229

230-
return find_mem_range(phys, &range);
230+
return !!find_mem_range(phys, &range);
231+
}
232+
233+
static bool addr_is_allowed_memory(phys_addr_t phys)
234+
{
235+
struct memblock_region *reg;
236+
struct kvm_mem_range range;
237+
238+
reg = find_mem_range(phys, &range);
239+
240+
return reg && !(reg->flags & MEMBLOCK_NOMAP);
231241
}
232242

233243
static bool is_in_mem_range(u64 addr, struct kvm_mem_range *range)
@@ -346,7 +356,7 @@ static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot pr
346356
static int host_stage2_idmap(u64 addr)
347357
{
348358
struct kvm_mem_range range;
349-
bool is_memory = find_mem_range(addr, &range);
359+
bool is_memory = !!find_mem_range(addr, &range);
350360
enum kvm_pgtable_prot prot;
351361
int ret;
352362

@@ -424,7 +434,7 @@ static int __check_page_state_visitor(u64 addr, u64 end, u32 level,
424434
struct check_walk_data *d = arg;
425435
kvm_pte_t pte = *ptep;
426436

427-
if (kvm_pte_valid(pte) && !addr_is_memory(kvm_pte_to_phys(pte)))
437+
if (kvm_pte_valid(pte) && !addr_is_allowed_memory(kvm_pte_to_phys(pte)))
428438
return -EINVAL;
429439

430440
return d->get_page_state(pte) == d->desired ? 0 : -EPERM;

0 commit comments

Comments
 (0)