[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen stable-4.16] livepatch: account for patch offset when applying NOP patch



commit e34c16cc6ee029fa75c35bd21f75103d5502ea30
Author:     Jan Beulich <jbeulich@xxxxxxxx>
AuthorDate: Thu Mar 31 11:00:57 2022 +0200
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Thu Mar 31 11:00:57 2022 +0200

    livepatch: account for patch offset when applying NOP patch
    
    While not triggered by the trivial xen_nop in-tree patch on
    staging/master, that patch exposes a problem on the stable trees, where
    all functions have ENDBR inserted. When NOP-ing out a range, we need to
    account for this. Handle this right in livepatch_insn_len().
    
    This requires livepatch_insn_len() to be called _after_ ->patch_offset
    was set.
    
    Fixes: 6974c75180f1 ("xen/x86: Livepatch: support patching CET-enhanced 
functions")
    Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
    Reviewed-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
    master commit: 8a87b9a0fb0564f9d68f0be0a0d1a17c34117b8b
    master date: 2022-03-31 10:45:46 +0200
---
 xen/arch/x86/livepatch.c    | 8 +++++---
 xen/include/xen/livepatch.h | 2 +-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c
index e94ac9b228..a3cb63a7ea 100644
--- a/xen/arch/x86/livepatch.c
+++ b/xen/arch/x86/livepatch.c
@@ -144,9 +144,6 @@ void noinline arch_livepatch_apply(struct livepatch_func 
*func)
 
     func->patch_offset = 0;
     old_ptr = func->old_addr;
-    len = livepatch_insn_len(func);
-    if ( !len )
-        return;
 
     /*
      * CET hotpatching support: We may have functions starting with an ENDBR64
@@ -159,6 +156,11 @@ void noinline arch_livepatch_apply(struct livepatch_func 
*func)
     if ( is_endbr64(old_ptr) )
         func->patch_offset += ENDBR64_LEN;
 
+    /* This call must be done with ->patch_offset already set. */
+    len = livepatch_insn_len(func);
+    if ( !len )
+        return;
+
     memcpy(func->opaque, old_ptr + func->patch_offset, len);
     if ( func->new_addr )
     {
diff --git a/xen/include/xen/livepatch.h b/xen/include/xen/livepatch.h
index f3ae10f007..9fdb29c382 100644
--- a/xen/include/xen/livepatch.h
+++ b/xen/include/xen/livepatch.h
@@ -90,7 +90,7 @@ static inline
 unsigned int livepatch_insn_len(const struct livepatch_func *func)
 {
     if ( !func->new_addr )
-        return func->new_size;
+        return func->new_size - func->patch_offset;
 
     return ARCH_PATCH_INSN_SIZE;
 }
--
generated by git-patchbot for /home/xen/git/xen.git#stable-4.16



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.