xen/arm: grant-table: Protect gnttab_clear_flag against guest misbehavior
authorJulien Grall <julien.grall@arm.com>
Mon, 29 Apr 2019 14:05:30 +0000 (15:05 +0100)
committerJulien Grall <julien.grall@arm.com>
Fri, 14 Jun 2019 13:27:32 +0000 (14:27 +0100)
commit70d2f27b592bfcf76750b9fed5906e53423eebd7
treebd9ff9e02d1c9c7d477ca47262d95afdf3dc4e65
parent48584b4b90a9d4ff3fd2545822d487544b7d0718
xen/arm: grant-table: Protect gnttab_clear_flag against guest misbehavior

The function gnttab_clear_flag is used to clear the access flags. On
Arm, it is implemented using a loop and guest_cmpxchg.

It is possible that guest_cmpxchg will always return a different value
than old. This can happen if the guest updated the memory before Xen has
time to do the exchange. Because of that, there are no way for to
promise the loop will end.

It is possible to make the current code safe by re-using the same
principle as applied on the guest atomic helper. However this patch
takes a different approach that should lead to more efficient code in
the default case.

A new helper is introduced to clear a set of bits on a 16-bits word.
This should avoid a an extra loop to check cmpxchg succeeded.

Note that a mask is used instead of a bit, so the helper can be re-used
later on for clearing multiple flags at the same time.

This is part of XSA-295.

Reported-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Julien Grall <julien.grall@arm.com>
Signed-off-by: Stefano Stabellini <stefanos@xilinx.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
xen/arch/arm/arm32/lib/bitops.c
xen/arch/arm/arm64/lib/bitops.c
xen/arch/arm/mm.c
xen/include/asm-arm/bitops.h
xen/include/asm-arm/guest_atomics.h