Skip to contentSkip to conversation

livepatch: account for patch offset when applying NOP patch

PATCH Superseded Jan Beulich 2 participants 4 messages Mar 30, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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().

Fixes 6974c75180f1 ("xen/x86: Livepatch: support patching CET-enhanced functions")

---
Only build tested, as I don't have a live patching environment available.

For Arm this assumes that the patch_offset field starts out as zero; I think we can make such an assumption, yet otoh on x86 explicit initialization was added by the cited commit.

xen/include/xen/livepatch.h +1 -1
@@ -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;
 }
#1 lore raw
Quoted 17 lines · Jan Beulich

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().

Fixes 6974c75180f1 ("xen/x86: Livepatch: support patching CET-enhanced functions")

---
Only build tested, as I don't have a live patching environment available.

For Arm this assumes that the patch_offset field starts out as zero; I think we can make such an assumption, yet otoh on x86 explicit initialization was added by the cited commit.

xen/include/xen/livepatch.h static inline · line 96

Quoted 8 lines
xen/include/xen/livepatch.h +1 -1
@@ -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;
 }

Don't you also need to move the call to livepatch_insn_len() in arch_livepatch_apply() after func->patch_offset has been adjusted to account for ENDBR presence?

Thanks, Roger.

#2 lore raw
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich

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().

Fixes 6974c75180f1 ("xen/x86: Livepatch: support patching CET-enhanced functions")

---
Only build tested, as I don't have a live patching environment available.

For Arm this assumes that the patch_offset field starts out as zero; I think we can make such an assumption, yet otoh on x86 explicit initialization was added by the cited commit.

xen/include/xen/livepatch.h +1 -1
@@ -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;
 
Quoted 6 lines

xen/include/xen/livepatch.h static inline · line 96

Quoted 2 lines
xen/include/xen/livepatch.h +0 -0
     return ARCH_PATCH_INSN_SIZE;
 }

Don't you also need to move the call to livepatch_insn_len() in arch_livepatch_apply() after func->patch_offset has been adjusted to account for ENDBR presence?

Oh, yes, I definitely need to.

Jan

#3 lore raw
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich

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().

Fixes 6974c75180f1 ("xen/x86: Livepatch: support patching CET-enhanced functions")

---
Only build tested, as I don't have a live patching environment available.

For Arm this assumes that the patch_offset field starts out as zero; I think we can make such an assumption, yet otoh on x86 explicit initialization was added by the cited commit.

xen/include/xen/livepatch.h +1 -1
@@ -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;
 
Quoted 8 lines
Quoted 6 lines

xen/include/xen/livepatch.h static inline · line 96

Quoted 2 lines
xen/include/xen/livepatch.h +0 -0
     return ARCH_PATCH_INSN_SIZE;
 }

Don't you also need to move the call to livepatch_insn_len() in arch_livepatch_apply() after func->patch_offset has been adjusted to account for ENDBR presence?

Oh, yes, I definitely need to.

Actually - not quite. I need to call the function a 2nd time. And this then also points out that is_endbr64() and is_endbr64_poison() may overrun the range pointed to by old_ptr (which I'll take care of at the same time).

Jan