Skip to content

Commit 51a71ab

Browse files
committed
virt: acrn: Annotate struct vm_memory_region_batch with __counted_by
Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). As found with Coccinelle[1], add __counted_by for struct vm_memory_region_batch. Additionally, since the element count member must be set before accessing the annotated flexible array member, move its initialization earlier. [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci Cc: Fei Li <fei1.li@intel.com> Reviewed-by: "Gustavo A. R. Silva" <gustavoars@kernel.org> Link: https://lore.kernel.org/r/20230922175102.work.020-kees@kernel.org Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 4a530cb commit 51a71ab

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

drivers/virt/acrn/acrn_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct vm_memory_region_batch {
6060
u16 reserved[3];
6161
u32 regions_num;
6262
u64 regions_gpa;
63-
struct vm_memory_region_op regions_op[];
63+
struct vm_memory_region_op regions_op[] __counted_by(regions_num);
6464
};
6565

6666
/**

drivers/virt/acrn/mm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
250250
ret = -ENOMEM;
251251
goto unmap_kernel_map;
252252
}
253+
regions_info->regions_num = nr_regions;
253254

254255
/* Fill each vm_memory_region_op */
255256
vm_region = regions_info->regions_op;
256257
regions_info->vmid = vm->vmid;
257-
regions_info->regions_num = nr_regions;
258258
regions_info->regions_gpa = virt_to_phys(vm_region);
259259
user_vm_pa = memmap->user_vm_pa;
260260
i = 0;

0 commit comments

Comments
 (0)