]> xenbits.xensource.com Git - xen.git/commit
xen: avoid UB in guest handle arithmetic
authorJan Beulich <jbeulich@suse.com>
Wed, 19 Jun 2024 12:11:07 +0000 (14:11 +0200)
committerJan Beulich <jbeulich@suse.com>
Wed, 19 Jun 2024 12:11:07 +0000 (14:11 +0200)
commit43d5c5d5f70b3f5419e7ef30399d23adf6ddfa8e
tree39588d141916d5e682d3b8aa4dd675f5d41cc0a7
parent267122a24c499d26278ab2dbdfb46ebcaaf38474
xen: avoid UB in guest handle arithmetic

At least XENMEM_memory_exchange can have huge values passed in the
nr_extents and nr_exchanged fields. Adding such values to pointers can
overflow, resulting in UB. Cast respective pointers to "unsigned long"
while at the same time making the necessary multiplication explicit.
Remaining arithmetic is, despite there possibly being mathematical
overflow, okay as per the C99 spec: "A computation involving unsigned
operands can never overflow, because a result that cannot be represented
by the resulting unsigned integer type is reduced modulo the number that
is one greater than the largest value that can be represented by the
resulting type." The overflow that we need to guard against is checked
for in array_access_ok().

Note that in / down from array_access_ok() the address value is only
ever cast to "unsigned long" anyway, which is why in the invocation from
guest_handle_subrange_okay() the value doesn't need casting back to
pointer type.

In compat grant table code change two guest_handle_add_offset() to avoid
passing in negative offsets.

Since {,__}clear_guest_offset() need touching anyway, also deal with
another (latent) issue there: They were losing the handle type, i.e. the
size of the individual objects accessed. Luckily the few users we
presently have all pass char or uint8 handles.

Reported-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
Tested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Release-Acked-By: Oleksii Kurochko <oleksii.kurochko@gmail.com>
xen/arch/x86/include/asm/guest_access.h
xen/common/compat/grant_table.c
xen/include/xen/guest_access.h