xen/x86: Livepatch: support patching CET-enhanced functions
authorBjoern Doebel <doebel@amazon.de>
Thu, 10 Mar 2022 07:35:36 +0000 (07:35 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 25 Mar 2022 17:06:38 +0000 (17:06 +0000)
commitdcd44e3b9ad2f0491bd7f4751232a389e4ee57e7
tree935a44535f67396b88c9dae14d289b170d7a3c16
parent27dc916a39e8be9de331a580a43f10ef85633133
xen/x86: Livepatch: support patching CET-enhanced functions

Xen enabled CET for supporting architectures. The control flow aspect of
CET require functions that can be called indirectly (i.e., via function
pointers) to start with an ENDBR64 instruction. Otherwise a control flow
exception is raised.

This expectation breaks livepatching flows because we patch functions by
overwriting their first 5 bytes with a JMP + <offset>, thus breaking the
ENDBR64. We fix this by checking the start of a patched function for
being ENDBR64. In the positive case we move the livepatch JMP to start
behind the ENDBR64 instruction.

To avoid having to guess the ENDBR64 offset again on patch reversal
(which might race with other mechanisms adding/removing ENDBR
dynamically), use the livepatch metadata to store the computed offset
along with the saved bytes of the overwritten function.

Signed-off-by: Bjoern Doebel <doebel@amazon.de>
Acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Reviewed-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Tested-by: Jiamei Xie <jiamei.xie@arm.com>
(cherry picked from commit 6974c75180f1aad44e5428eabf2396b2b50fb0e4)

Note: For backports to 4.14 thru 4.16, there is no endbr-clobbering, hence no
      is_endbr64_poison() logic.
xen/arch/x86/livepatch.c
xen/include/public/sysctl.h