Livepatch fixes and general features for Xen4.8.
PATCH v6 1/1 Livepatch fixes and general features for Xen4.8.
Hey!
Since v5:[https://lists.xen.org/archives/html/xen-devel/2016-09/msg01114.html]
- Acted on the review comments.
- Replaced "livepatch/docs: Document .bss not being cleared, and .data potentially having changed values" with "livepatch: Disallow applying after an revert"
- Added two minor fixes to the test-cases (one of them was part of the ARM32/64 livepatch support).
Since v4:
[https://lists.xenproject.org/archives/html/xen-devel/2016-08/msg02705.html]
- Committed Acked/Reviewed patches.
- Discarded couple of patches to address later.
Since v3: [https://lists.xen.org/archives/html/xen-devel/2016-08/msg01825.html]
- Acked on reviews
v2, v1:
- Left over fixes and features that didn't get quite done in 4.7
Included are:
- Bug-fixes
- NOP patching
- Hooks
The first one deals with .bss and replaces the "livepatch/docs: Document .bss not being cleared, and .data potentially" which was posted in v5.
The legend is:
r - Reviewed-by
livepatch: Disallow applying after an revert
r livepatch: Add limit of 2MB to payload .bss sections.
livepatch: NOP if func->new_addr is zero.
r livepach: Add .livepatch.hooks functions and test-case
livepatch/tests: Make .livepatch.depends be read-only livepatch/tests: Move the .name value to .rodata
Thanks!
The git tree is
git://xenbits.xen.org/people/konradwilk/xen.git livepatch.v4.8.v6
contains all the following patches (and more):
Konrad Rzeszutek Wilk (5):
livepatch: Disallow applying after an revert
livepatch: Add limit of 2MB to payload .bss sections.
livepatch: NOP if func->new_addr is zero. livepatch/tests: Make .livepatch.depends be read-only livepatch/tests: Move the .name value to .rodata
Ross Lagerwall (1):
livepach: Add .livepatch.hooks functions and test-case
docs/misc/livepatch.markdown | 37 +++++++++++++++- xen/arch/x86/alternative.c | 2 +- xen/arch/x86/livepatch.c | 40 ++++++++++++------ xen/arch/x86/test/Makefile | 4 +- xen/arch/x86/test/xen_bye_world.c | 2 +- xen/arch/x86/test/xen_hello_world.c | 36 +++++++++++++++- xen/arch/x86/test/xen_replace_world.c | 2 +- xen/common/livepatch.c | 80 +++++++++++++++++++++++++++++++++-- xen/common/livepatch_elf.c | 4 ++ xen/include/asm-x86/alternative.h | 1 + xen/include/asm-x86/livepatch.h | 21 +++++++++ xen/include/xen/livepatch.h | 11 +++++ xen/include/xen/livepatch_payload.h | 49 +++++++++++++++++++++ 13 files changed, 264 insertions(+), 25 deletions(-)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v6 1/6 livepatch: Disallow applying after an revert
On general this is unhealthy - as the payload's .bss (definitly) or .data (maybe) will be modified once the payload is running.
Doing an revert and then re-applying the payload with a non-pristine .bss or .data can lead to unforseen consequences (.bss are assumed to always contain zero value but now they may have a different value).
There is one exception - if the payload contains only one .data section
- the .livepatch.funcs, then it is OK to re-apply an revert.
---
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
v6: New submission
--- docs/misc/livepatch.markdown | 7 +++++++ xen/common/livepatch.c | 27 ++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-)
docs/misc/livepatch.markdown +7 -0
@@ -1061,6 +1061,13 @@ depending on the current state of data. As such it should not be attempted. That said we should provide hook functions so that the existing data can be changed during payload application. +To guarantee safety we disallow re-applying an payload after it has been +reverted. This is because we cannot guarantee that the state of .bss +and .data to be exactly as it was during loading. Hence the administrator +MUST unload the payload and upload it again to apply it. + +There is an exception to this: if the payload only has .livepatch.funcs; +and no .data or .bss sections. ### Inline patching
xen/common/livepatch.c +26 -1
@@ -52,6 +52,8 @@ struct livepatch_build_id {struct payload {uint32_t state;/* One of the LIVEPATCH_STATE_*. */ int32_t rc;/* 0 or -XEN_EXX. */ +bool_t reverted;/* Whether it was reverted. */ +bool_t safe_to_reapply;/* Can apply safely after revert. */ struct list_head list;/* Linked to 'payload_list'. */ const void *text_addr;/* Virtual address of .text. */ size_t text_size;/* .. and its size. */
@@ -308,7 +310,7 @@ static void calc_section(const struct livepatch_elf_sec *sec, size_t *size,static int move_payload (struct payload *payload,struct livepatch_elf *elf) {void *text_buf, *ro_buf, *rw_buf; -unsigned int i; +unsigned int i, rw_buf_sec, rw_buf_cnt =0 ;size_t size =0 ;unsigned int *offset;int rc =0 ;
@@ -381,7 +383,11 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)if ( elf->sec[i].sec->sh_flags &SHF_EXECINSTR ) buf = text_buf;else if ( elf->sec[i].sec->sh_flags &SHF_WRITE ) + { buf = rw_buf; + rw_buf_sec = i; + rw_buf_cnt++; + }else buf = ro_buf;
@@ -402,6 +408,10 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) } } +/* No .bss and no .data, and only on RW - .livepatch.funcs section. */ +if ( rw_buf_cnt ==1 && + !strcmp (elf->sec[rw_buf_sec].name,ELF_LIVEPATCH_FUNC ) ) + payload->safe_to_reapply = true; out:xfree (offset);
@@ -1057,6 +1067,7 @@ static int revert_payload(struct payload *data)list_del_rcu (&data->applied_list);unregister_virtual_region (&data->region); + data->reverted = true;return 0 ; }
@@ -1438,6 +1449,20 @@ static int livepatch_action(xen_sysctl_livepatch_action_t *action)case LIVEPATCH_ACTION_APPLY :if ( data->state ==LIVEPATCH_STATE_CHECKED ) { +/* +* It is unsafe to apply an reverted payload as the .data (or .bss) +* may not be in in pristine condition. Hence MUST unload and then +* apply patch again. Unless the payload has no .bss or only one +* RW section (.livepatch.funcs). +*/ +if ( data->reverted && !data->safe_to_reapply ) + { +dprintk (XENLOG_ERR ,"%s%s: can't revert as payload has .data. Please unload!\n" , +LIVEPATCH , data->name); + data->rc = -EINVAL ; +break ; + } + rc =build_id_dep (data, !!list_empty (&applied_list));if ( rc )break ;
Signature · 7 lines
2.5.5 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v6 1/6] livepatch: Disallow applying after an revert
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
On general this is unhealthy - as the payload's .bss (definitly) or .data (maybe) will be modified once the payload is running.
Doing an revert and then re-applying the payload with a non-pristine .bss or .data can lead to unforseen consequences (.bss are assumed to always contain zero value but now they may have a different value).
There is one exception - if the payload contains only one .data section
- the .livepatch.funcs, then it is OK to re-apply an revert.
Quoted 6 lines
Suggested-by Jan Beulich <[email protected]>Signed-off-by Konrad Rzeszutek Wilk <[email protected]>---
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
I think Ross is actually the primary person to be listed here.
xen/common/livepatch.c struct livepatch_build_id { · line 56
Quoted 8 lines
xen/common/livepatch.c +2
-0@@ -52,6 +52,8 @@ struct livepatch_build_id {struct payload {uint32_t state;/* One of the LIVEPATCH_STATE_*. */ int32_t rc;/* 0 or -XEN_EXX. */ +bool_t reverted;/* Whether it was reverted. */ +bool_t safe_to_reapply;/* Can apply safely after revert. */
You nicely use "true" int the code below - please also use plain "bool" here.
xen/common/livepatch.c static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 389
Quoted 8 lines
xen/common/livepatch.c +3
-0@@ -381,7 +383,11 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)if ( elf->sec[i].sec->sh_flags &SHF_EXECINSTR ) buf = text_buf;else if ( elf->sec[i].sec->sh_flags &SHF_WRITE ) + { buf = rw_buf; + rw_buf_sec = i; + rw_buf_cnt++;
Wouldn't it make sense to exclude zero-size sections here? Perhaps even worth skipping them together with !SHF_ALLOC ones (and then of course also in the other earlier loop)? The more that ISTR you mentioning that empty .data and .bss get created by the assembler even if there's no respective source contribution?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 1/6] livepatch: Disallow applying after an revert
In reply to Konrad Rzeszutek WilkQuoted 7 lines · Konrad Rzeszutek Wilk
On general this is unhealthy - as the payload's .bss (definitly) or .data (maybe) will be modified once the payload is running.
Doing an revert and then re-applying the payload with a non-pristine .bss or .data can lead to unforseen consequences (.bss are assumed to always contain zero value but now they may have a different value).
Quoted 6 lines
There is one exception - if the payload contains only one .data section
- the .livepatch.funcs, then it is OK to re-apply an revert.Suggested-by Jan Beulich <[email protected]>Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
This looks reasonable, with Jan's comments addressed.
Signature · 6 lines
Ross Lagerwall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v6 2/6 livepatch: Add limit of 2MB to payload .bss sections.
In reply to Konrad Rzeszutek WilkThe initial patch: 11ff40fa7bb5fdcc69a58d0fec49c904ffca4793 "xen/xsplice: Hypervisor implementation of XEN_XSPLICE_op" caps the size of the binary at 2MB. We follow that in capping the size of the .BSSes to be at maximum 2MB.
---
Cc: Ross Lagerwall <[email protected]>
Cc: Jan Beulich <[email protected]>
v5: Initial submission. Came about from conversation about
"livepatch: Clear .bss when payload is reverted"
- Use only one sh_flags comparison instead of two.
- And check for the _right_ combination (WA).
v6: Remove the logging
Move the MB(2) to a #define in the header file. Add the newline after the addition in livepatch_elf.c. Added Reviewed-by from Ross.
--- xen/common/livepatch_elf.c | 4 ++++ xen/include/xen/livepatch.h | 2 ++ 2 files changed, 6 insertions(+)
xen/common/livepatch_elf.c +4 -0
@@ -86,6 +86,10 @@ static int elf_resolve_sections(struct livepatch_elf *elf, const void *data) delta < sizeof(Elf_Ehdr) ? "at ELF header" : "is past end");return -EINVAL ; } +else if ( (sec[i].sec->sh_flags & (SHF_WRITE |SHF_ALLOC )) && + sec[i].sec->sh_type ==SHT_NOBITS && + sec[i].sec->sh_size >BSS_MAX_SIZE ) +return -EINVAL ; sec[i].data = data + delta;/* Name is populated in elf_resolve_section_names. */
xen/include/xen/livepatch.h +2 -0
@@ -30,6 +30,8 @@ struct xen_sysctl_livepatch_op;#define ELF_LIVEPATCH_FUNC ".livepatch.funcs"#define ELF_LIVEPATCH_DEPENDS ".livepatch.depends"#define ELF_BUILD_ID_NOTE ".note.gnu.build-id" +/* Arbitrary limit. */ +#define BSS_MAX_SIZE MB(2)struct livepatch_symbol {const char *name;
Signature · 7 lines
2.5.5 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v6 2/6] livepatch: Add limit of 2MB to payload .bss sections.
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
xen/common/livepatch_elf.c +4
-0@@ -86,6 +86,10 @@ static int elf_resolve_sections(struct livepatch_elf *elf, const void *data) delta < sizeof(Elf_Ehdr) ? "at ELF header" : "is past end");return -EINVAL ; } +else if ( (sec[i].sec->sh_flags & (SHF_WRITE |SHF_ALLOC )) && + sec[i].sec->sh_type ==SHT_NOBITS && + sec[i].sec->sh_size >BSS_MAX_SIZE ) +return -EINVAL ; sec[i].data = data + delta;/* Name is populated in elf_resolve_section_names. */
xen/include/xen/livepatch.h struct xen_sysctl_livepatch_op; · line 34
Quoted 5 lines
xen/include/xen/livepatch.h +2
-0@@ -30,6 +30,8 @@ struct xen_sysctl_livepatch_op;#define ELF_LIVEPATCH_FUNC ".livepatch.funcs"#define ELF_LIVEPATCH_DEPENDS ".livepatch.depends"#define ELF_BUILD_ID_NOTE ".note.gnu.build-id" +/* Arbitrary limit. */ +#define BSS_MAX_SIZE MB(2)
Hmm, this wasn't quite what I was thinking about in the v5
comments: I really meant to unify this and the other 2Mb limit into one (and then obviously with a name that's more generic). I'm sorry for not having expressed this in an explicit enough way.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v6 3/6 livepatch: NOP if func->new_addr is zero.
In reply to Konrad Rzeszutek WilkThe NOP functionality will NOP any of the code at the 'old_addr' or at 'name' if the 'new_addr' is zero. The purpose of this is to NOP out calls, such as:
e8 <4-bytes-offset>
(5 byte insn), or on ARM a 4 byte insn for branching.
We need the EIP of where we need to the NOP, and that can be provided via the old_addr or name.
If the old_addr is provided we will NOP 'new_size' amount of bytes at that location.
The amount is up to 31 instructions if desired (which is the size of the opaque member). If there is a need to NOP more then: a) more 'struct livepatch_func' structures need to be present, b) we have to implement a variable size buffer (in the future), or c) first byte an unconditional branch skipping the to be disabled code (of course provided there are no branch targets in the middle).
While at it, also unify the code on x86 patching so it is a bit simpler (instead of two seperate writes just make it one memcpy).
And introduce a general livepatch_insn_len inline function that would depend on platform specific instruction size (for a unconditional branch). As such we also rename the PATCH_INSN_SIZE to ARCH_PATCH_INSN_SIZE.
---
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Ross Lagerwall <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
v3: First submission
v4: Fix description - e9 -> e8
Remove the restriction of only doing 5 or 4 bytes.
Redo the patching code to deal with variable size of new_size. Expand the amount of bytes we can NOP.
Move the PATCH_INSN_SIZE definition in platform specific headers Move the get_len to livepatch_get_insn_len inline function.
v5: s/PATCH_INSN_SIZE/ARCH_PATCH_INSN_SIZE/
s/arch_livepatch_insn_len/livepatch_insn_len/
s/size_t len/unsigned int len/
Add in commit description the c) mechanism (insert an unconditional branch).
--- docs/misc/livepatch.markdown | 7 +++++-- xen/arch/x86/alternative.c | 2 +- xen/arch/x86/livepatch.c | 40 +++++++++++++++++++++++++-------------- xen/common/livepatch.c | 3 ++- xen/include/asm-x86/alternative.h | 1 + xen/include/asm-x86/livepatch.h | 21 ++++++++++++++++++++ xen/include/xen/livepatch.h | 9 +++++++++ 7 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 xen/include/asm-x86/livepatch.h
docs/misc/livepatch.markdown +5 -2
@@ -320,10 +320,13 @@ The size of the structure is 64 bytes on 64-bit hypervisors. It will be * `new_addr` is the address of the function that is replacing the old function. The address is filled in during relocation. The value **MUST** be - the address of the new function in the file. + either the address of the new function in the file, or zero if we are NOPing out + at `old_addr` (and `new_size` **MUST** not be zero). * `old_size` and `new_size` contain the sizes of the respective functions in bytes. - The value of `old_size` **MUST** not be zero. + The value of `old_size` **MUST** not be zero. If the value of `new_addr` is + zero then `new_size` determines how many instruction bytes to NOP (up to + opaque size modulo smallest platform instruction - 1 byte x86 and 4 bytes on ARM). * `version` is to be one.
xen/arch/x86/alternative.c +1 -1
@@ -101,7 +101,7 @@ static void __init arch_init_ideal_nops(void) }/* Use this to add nops to a buffer, then text_poke the whole buffer. */ -static void init_or_livepatch add_nops(void *insns, unsigned int len) +void init_or_livepatch add_nops(void *insns, unsigned int len) {while ( len >0 ) {
xen/arch/x86/livepatch.c +26 -14
@@ -12,8 +12,7 @@#include <xen/livepatch.h> #include <asm/nmi.h> - -#define PATCH_INSN_SIZE 5 +#include <asm/livepatch.h> int arch_livepatch_quiesce (void ) {
@@ -31,11 +30,11 @@ void arch_livepatch_revive(void)int arch_livepatch_verify_func (const struct livepatch_func *func) { -/* No NOP patching yet. */ -if ( !func->new_size ) +/* If NOPing only do up to maximum amount we can put in the ->opaque. */ +if ( !func->new_addr && func->new_size >sizeof (func->opaque) )return -EOPNOTSUPP ; -if ( func->old_size <PATCH_INSN_SIZE ) +if ( func->old_size <ARCH_PATCH_INSN_SIZE )return -EINVAL ;return 0 ;
@@ -43,23 +42,36 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)void arch_livepatch_apply_jmp (struct livepatch_func *func) { -int32_t val;uint8_t *old_ptr; - -BUILD_BUG_ON (PATCH_INSN_SIZE >sizeof (func->opaque)); -BUILD_BUG_ON (PATCH_INSN_SIZE != (1 +sizeof (val))); +uint8_t insn[sizeof (func->opaque)]; +unsigned int len; old_ptr = func->old_addr; -memcpy (func->opaque, old_ptr,PATCH_INSN_SIZE ); + len =livepatch_insn_len (func); +if ( !len ) +return ; + +memcpy (func->opaque, old_ptr, len); +if ( func->new_addr ) + { +int32_t val; + +BUILD_BUG_ON (ARCH_PATCH_INSN_SIZE != (1 +sizeof (val))); + + insn[0 ] =0xe9 ; + val = func->new_addr - func->old_addr -ARCH_PATCH_INSN_SIZE ; + +memcpy (&insn[1 ], &val,sizeof (val)); + } +else +add_nops (&insn, len); - *old_ptr++ =0xe9 ;/* Relative jump */ - val = func->new_addr - func->old_addr -PATCH_INSN_SIZE ; -memcpy (old_ptr, &val,sizeof (val)); +memcpy (old_ptr, insn, len); }void arch_livepatch_revert_jmp (const struct livepatch_func *func) { -memcpy (func->old_addr, func->opaque,PATCH_INSN_SIZE ); +memcpy (func->old_addr, func->opaque,livepatch_insn_len (func)); }/* Serialise the CPU pipeline. */
xen/common/livepatch.c +2 -1
@@ -520,7 +520,8 @@ static int prepare_payload(struct payload *payload,return -EOPNOTSUPP ; } -if ( !f->new_addr || !f->new_size ) +/* 'old_addr', 'new_addr', 'new_size' can all be zero. */ +if ( !f->old_size ) {dprintk (XENLOG_ERR ,LIVEPATCH "%s: Address or size fields are zero!\n" , elf->name);
xen/include/asm-x86/alternative.h +1 -0
@@ -27,6 +27,7 @@ struct alt_instr {#define ALT_ORIG_PTR (a) __ALT_PTR(a, instr_offset)#define ALT_REPL_PTR (a) __ALT_PTR(a, repl_offset) +extern void add_nops (void *insns,unsigned int len);/* Similar to alternative_instructions except it can be run with IRQs enabled. */ extern void apply_alternatives (const struct alt_instr *start,const struct alt_instr *end);
xen/include/asm-x86/livepatch.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. +* +*/ + +#ifndef __XEN_X86_LIVEPATCH_H__ +#define __XEN_X86_LIVEPATCH_H__ + +#define ARCH_PATCH_INSN_SIZE 5 + +#endif /* __XEN_X86_LIVEPATCH_H__ */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/include/xen/livepatch.h +9 -0
@@ -68,7 +68,16 @@ int arch_livepatch_secure(const void *va, unsigned int pages, enum va_type typesvoid arch_livepatch_init (void );#include <public/sysctl.h> /* For struct livepatch_func. */ +#include <asm/livepatch.h> /* For ARCH_PATCH_INSN_SIZE. */ int arch_livepatch_verify_func (const struct livepatch_func *func); + +static inline size_t livepatch_insn_len (const struct livepatch_func *func) +{ +if ( !func->new_addr ) +return func->new_size; + +return ARCH_PATCH_INSN_SIZE ; +}/* * These functions are called around the critical region patching live code, * for an architecture to take make appropratie global state adjustments.
Signature · 7 lines
2.5.5 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
xen/arch/x86/livepatch.c +0
-0@@ -31,11 +30,11 @@ void arch_livepatch_revive(void)int arch_livepatch_verify_func (const struct livepatch_func *func)
xen/arch/x86/livepatch.c void arch_livepatch_revive(void) · line 38
Quoted 10 lines
xen/arch/x86/livepatch.c +3
-3{ -/* No NOP patching yet. */ -if ( !func->new_size ) +/* If NOPing only do up to maximum amount we can put in the ->opaque. */ +if ( !func->new_addr && func->new_size >sizeof (func->opaque) )return -EOPNOTSUPP ; -if ( func->old_size <PATCH_INSN_SIZE ) +if ( func->old_size <ARCH_PATCH_INSN_SIZE )return -EINVAL ;
Is that indeed a requirement when NOPing? You can easily NOP out just a single byte on x86. Or shouldn't in that case old_size == new_size anyway? In which case the comment further down stating that new_size can be zero would also be wrong.
Quoted 24 lines
xen/arch/x86/livepatch.c +12
-5@@ -43,23 +42,36 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)void arch_livepatch_apply_jmp (struct livepatch_func *func) { -int32_t val;uint8_t *old_ptr; - -BUILD_BUG_ON (PATCH_INSN_SIZE >sizeof (func->opaque)); -BUILD_BUG_ON (PATCH_INSN_SIZE != (1 +sizeof (val))); +uint8_t insn[sizeof (func->opaque)]; +unsigned int len; old_ptr = func->old_addr; -memcpy (func->opaque, old_ptr,PATCH_INSN_SIZE ); + len =livepatch_insn_len (func); +if ( !len ) +return ; + +memcpy (func->opaque, old_ptr, len); +if ( func->new_addr ) + { +int32_t val; + +BUILD_BUG_ON (ARCH_PATCH_INSN_SIZE != (1 +sizeof (val)));
xen/arch/x86/livepatch.c int arch_livepatch_verify_func(const struct livepatch_func *func) · line 55
Quoted 10 lines
xen/arch/x86/livepatch.c +8
-1+ + insn[0 ] =0xe9 ; + val = func->new_addr - func->old_addr -ARCH_PATCH_INSN_SIZE ; + +memcpy (&insn[1 ], &val,sizeof (val)); + } +else +add_nops (&insn, len); - *old_ptr++ =0xe9 ;/* Relative jump */
Are you btw intentionally getting rid of this comment? And with the NOP addition here, perhaps worth dropping the _jmp from the function name (and its revert counterpart)?
xen/include/xen/livepatch.h int arch_livepatch_secure(const void *va, unsigned int pages, enum va_type types · line 74
Quoted 1 line
xen/include/xen/livepatch.h +1
-0+static inline size_t livepatch_insn_len (const struct livepatch_func *func)
I think it would be nice to use consistent types: The current sole caller stores the result of the function in an unsigned int, and I see no reason why the function couldn't also return such.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
xen/arch/x86/livepatch.c +2
-2@@ -31,11 +30,11 @@ void arch_livepatch_revive(void)int arch_livepatch_verify_func (const struct livepatch_func *func) { -/* No NOP patching yet. */ -if ( !func->new_size ) +/* If NOPing only do up to maximum amount we can put in the ->opaque. */ +if ( !func->new_addr && func->new_size >sizeof (func->opaque) )return -EOPNOTSUPP ;
Quoted 7 lines
xen/arch/x86/livepatch.c void arch_livepatch_revive(void) · line 38
Quoted 3 lines
xen/arch/x86/livepatch.c +1
-1-if ( func->old_size <PATCH_INSN_SIZE ) +if ( func->old_size <ARCH_PATCH_INSN_SIZE )return -EINVAL ;Is that indeed a requirement when NOPing? You can easily NOP out just a single byte on x86. Or shouldn't in that case
old_size==new_sizeanyway? In which case the comment further down stating thatnew_size
The original intent behind .old_size was to guard against patching functions that were less than our relative jump.
(The tools end up computing the .old_size as the size of the whole function which is fine).
But with this NOPing support, you are right - we could have now an function that is say 4 bytes long and we only need to NOP three bytes out of it (the last instruction I assume would be 'ret').
So perhaps this check needs just needs an 'else if' , like so:
int arch_livepatch_verify_func(const struct livepatch_func *func)
{
/* If NOPing.. */
if ( !func->new_addr )
{
/* Only do up to maximum amount we can put in the ->opaque. */
if ( func->new_size > sizeof(func->opaque) )
return -EOPNOTSUPP;
/* One instruction for 'ret' and the other to NOP. */
if ( func->old_size < 2 )
return -EINVAL;
}
else if ( func->old_size < ARCH_PATCH_INSN_SIZE )
return -EINVAL;
return 0;
}[And update the design]
Quoted 35 lines
can be zero would also be wrong.
Quoted 33 lines
xen/arch/x86/livepatch.c +20
-5@@ -43,23 +42,36 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)void arch_livepatch_apply_jmp (struct livepatch_func *func) { -int32_t val;uint8_t *old_ptr; - -BUILD_BUG_ON (PATCH_INSN_SIZE >sizeof (func->opaque)); -BUILD_BUG_ON (PATCH_INSN_SIZE != (1 +sizeof (val))); +uint8_t insn[sizeof (func->opaque)]; +unsigned int len; old_ptr = func->old_addr; -memcpy (func->opaque, old_ptr,PATCH_INSN_SIZE ); + len =livepatch_insn_len (func); +if ( !len ) +return ; + +memcpy (func->opaque, old_ptr, len); +if ( func->new_addr ) + { +int32_t val; + +BUILD_BUG_ON (ARCH_PATCH_INSN_SIZE != (1 +sizeof (val))); + + insn[0 ] =0xe9 ; + val = func->new_addr - func->old_addr -ARCH_PATCH_INSN_SIZE ; + +memcpy (&insn[1 ], &val,sizeof (val)); + } +else +add_nops (&insn, len);
Quoted 3 lines
xen/arch/x86/livepatch.c int arch_livepatch_verify_func(const struct livepatch_func *func) · line 55
Quoted 1 line
xen/arch/x86/livepatch.c +0
-1- *old_ptr++ =0xe9 ;/* Relative jump */ Are you btw intentionally getting rid of this comment? And with the
Not at all. Just missed it.
Quoted 2 lines
NOP addition here, perhaps worth dropping the
_jmpfrom the function name (and its revert counterpart)?
Ooh, good idea. But I think it maybe better as a seperate patch (as it also touches the ARM code).
Quoted 6 lines
xen/include/xen/livepatch.h int arch_livepatch_secure(const void *va, unsigned int pages, enum va_type types · line 74
Quoted 2 lines
xen/include/xen/livepatch.h +1
-0+static inline size_t livepatch_insn_len (const struct livepatch_func *func)I think it would be nice to use consistent types: The current sole caller stores the result of the function in an unsigned int, and I see no reason why the function couldn't also return such.
/me nods.
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
Quoted 43 lines
Quoted 1 line
Quoted 1 line · On 19.09.16 at 18:11, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
xen/arch/x86/livepatch.c +2
-2@@ -31,11 +30,11 @@ void arch_livepatch_revive(void)int arch_livepatch_verify_func (const struct livepatch_func *func) { -/* No NOP patching yet. */ -if ( !func->new_size ) +/* If NOPing only do up to maximum amount we can put in the ->opaque. */ +if ( !func->new_addr && func->new_size >sizeof (func->opaque) )return -EOPNOTSUPP ;Quoted 7 lines
xen/arch/x86/livepatch.c void arch_livepatch_revive(void) · line 38
Quoted 3 lines
xen/arch/x86/livepatch.c +1
-1-if ( func->old_size <PATCH_INSN_SIZE ) +if ( func->old_size <ARCH_PATCH_INSN_SIZE )return -EINVAL ;Is that indeed a requirement when NOPing? You can easily NOP out just a single byte on x86. Or shouldn't in that case
old_size==new_sizeanyway? In which case the comment further down stating thatnew_sizeThe original intent behind .old_size was to guard against patching functions that were less than our relative jump.
(The tools end up computing the .old_size as the size of the whole function which is fine).
But with this NOPing support, you are right - we could have now an function that is say 4 bytes long and we only need to NOP three bytes out of it (the last instruction I assume would be 'ret').
So perhaps this check needs just needs an 'else if' , like so:
int
arch_livepatch_verify_func(const struct livepatch_func *func){ /* If NOPing.. */ if ( !func->new_addr ) { /* Only do up to maximum amount we can put in the ->opaque. */ if ( func->new_size > sizeof(func->opaque) ) return -EOPNOTSUPP; /* One instruction for 'ret' and the other to NOP. */
Quoted 8 lines
if ( func->old_size < 2 )
return -EINVAL;
}
else if ( func->old_size < ARCH_PATCH_INSN_SIZE )
return -EINVAL;
return 0;
}Except that I wouldn't use 2, to not exclude patching out some single byte in the middle of a function, without regard to what the function's actual size is.
Quoted 39 lines
Quoted 35 lines
can be zero would also be wrong.
Quoted 33 lines
xen/arch/x86/livepatch.c +20
-5@@ -43,23 +42,36 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)void arch_livepatch_apply_jmp (struct livepatch_func *func) { -int32_t val;uint8_t *old_ptr; - -BUILD_BUG_ON (PATCH_INSN_SIZE >sizeof (func->opaque)); -BUILD_BUG_ON (PATCH_INSN_SIZE != (1 +sizeof (val))); +uint8_t insn[sizeof (func->opaque)]; +unsigned int len; old_ptr = func->old_addr; -memcpy (func->opaque, old_ptr,PATCH_INSN_SIZE ); + len =livepatch_insn_len (func); +if ( !len ) +return ; + +memcpy (func->opaque, old_ptr, len); +if ( func->new_addr ) + { +int32_t val; + +BUILD_BUG_ON (ARCH_PATCH_INSN_SIZE != (1 +sizeof (val))); + + insn[0 ] =0xe9 ; + val = func->new_addr - func->old_addr -ARCH_PATCH_INSN_SIZE ; + +memcpy (&insn[1 ], &val,sizeof (val)); + } +else +add_nops (&insn, len);Quoted 3 lines
xen/arch/x86/livepatch.c int arch_livepatch_verify_func(const struct livepatch_func *func) · line 55
Quoted 1 line
xen/arch/x86/livepatch.c +0
-1- *old_ptr++ =0xe9 ;/* Relative jump */ Are you btw intentionally getting rid of this comment? And with the
Quoted 6 lines
Not at all. Just missed it.
Quoted 2 lines
NOP addition here, perhaps worth dropping the
_jmpfrom the function name (and its revert counterpart)?Ooh, good idea. But I think it maybe better as a seperate patch (as it also touches the ARM code).
That's in the other series, isn't it?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
Quoted 49 lines · Jan Beulich
Quoted 49 lines
Quoted 1 line
Quoted 1 line · On 19.09.16 at 18:11, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
(quoted diff) +2
-2@@ -31,11 +30,11 @@ void arch_livepatch_revive(void) int arch_livepatch_verify_func(const struct livepatch_func *func) { - /* No NOP patching yet. */ - if ( !func->new_size ) + /* If NOPing only do up to maximum amount we can put in the ->opaque. */ + if ( !func->new_addr && func->new_size > sizeof(func->opaque) ) return -EOPNOTSUPP;Quoted 7 lines
Review void arch_livepatch_revive(void) · line 38
Quoted 3 lines
(quoted diff) +1
-1- if ( func->old_size < PATCH_INSN_SIZE ) + if ( func->old_size < ARCH_PATCH_INSN_SIZE ) return -EINVAL;Is that indeed a requirement when NOPing? You can easily NOP out just a single byte on x86. Or shouldn't in that case
old_size==new_sizeanyway? In which case the comment further down stating thatnew_sizeThe original intent behind .old_size was to guard against patching functions that were less than our relative jump.
(The tools end up computing the .old_size as the size of the whole function which is fine).
But with this NOPing support, you are right - we could have now an function that is say 4 bytes long and we only need to NOP three bytes out of it (the last instruction I assume would be 'ret').
So perhaps this check needs just needs an 'else if' , like so:
int
arch_livepatch_verify_func(const struct livepatch_func *func){ /* If NOPing.. */ if ( !func->new_addr ) { /* Only do up to maximum amount we can put in the ->opaque. */ if ( func->new_size > sizeof(func->opaque) ) return -EOPNOTSUPP; /* One instruction for 'ret' and the other to NOP. */ if ( func->old_size < 2 ) return -EINVAL; } else if ( func->old_size < ARCH_PATCH_INSN_SIZE ) return -EINVAL;
Quoted 6 lines
Quoted 2 lines
return 0; }Except that I wouldn't use 2, to not exclude patching out some single byte in the middle of a function, without regard to what the function's actual size is.
Uh-uh.
The _new_size_ determines how many bytes to NOP (in the context of this patch). The old_size (where we check to be at min 2) is a safety valve to make sure we don't NOP something outside the function.
..snip..
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
NOP addition here, perhaps worth dropping the
_jmpfrom the function name (and its revert counterpart)?Ooh, good idea. But I think it maybe better as a seperate patch (as it also touches the ARM code).
That's in the other series, isn't it?
It expands the existing ones. Right now in 'staging' branch we have an arch/arm/livepatch.c which has these functions in it.
Granted nothing compiles them, so I could break it in this patch.
But I already cobbled up the patch so may as well use it?
From 45abdd6a0c3a6a2ca7180c7340032ac5b2b186a4 Mon Sep 17 00:00:00 2001
From: Konrad Rzeszutek Wilk <[email protected]>
Date: Mon, 19 Sep 2016 12:20:27 -0400
Subject: [PATCH] livepatch: Drop _jmp from arch_livepatch_[apply,revert]_jmp
With "livepatch: NOP if func->new_addr is zero." that name makes no more sense.
---
v7: New submission
--- xen/arch/arm/livepatch.c | 4 ++-- xen/arch/x86/livepatch.c | 4 ++-- xen/include/xen/livepatch.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
xen/arch/arm/livepatch.c +2 -2
@@ -21,11 +21,11 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)return -ENOSYS ; } -void arch_livepatch_apply_jmp (struct livepatch_func *func) +void arch_livepatch_apply (struct livepatch_func *func) { } -void arch_livepatch_revert_jmp (const struct livepatch_func *func) +void arch_livepatch_revert (const struct livepatch_func *func) { }
xen/arch/x86/livepatch.c +2 -2
@@ -47,7 +47,7 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)return 0 ; } -void arch_livepatch_apply_jmp (struct livepatch_func *func) +void arch_livepatch_apply (struct livepatch_func *func) {uint8_t *old_ptr;uint8_t insn[sizeof (func->opaque)];
@@ -76,7 +76,7 @@ void arch_livepatch_apply_jmp(struct livepatch_func *func)memcpy (old_ptr, insn, len); } -void arch_livepatch_revert_jmp (const struct livepatch_func *func) +void arch_livepatch_revert (const struct livepatch_func *func) {memcpy (func->old_addr, func->opaque,livepatch_insn_len (func)); }
xen/include/xen/livepatch.h +2 -2
@@ -86,8 +86,8 @@ unsigned int livepatch_insn_len(const struct livepatch_func *func)int arch_livepatch_quiesce (void );void arch_livepatch_revive (void ); -void arch_livepatch_apply_jmp (struct livepatch_func *func); -void arch_livepatch_revert_jmp (const struct livepatch_func *func); +void arch_livepatch_apply (struct livepatch_func *func); +void arch_livepatch_revert (const struct livepatch_func *func);void arch_livepatch_post_action (void );void arch_livepatch_mask (void );
Signature · 1 line
2.4.11
Quoted 3 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
Quoted 12 lines
Quoted 4 lines
Quoted 2 lines
Ooh, good idea. But I think it maybe better as a seperate patch (as it also touches the ARM code).
That's in the other series, isn't it?
It expands the existing ones. Right now in 'staging' branch we have an
arch/arm/livepatch.cwhich has these functions in it.Granted nothing compiles them, so I could break it in this patch.
But I already cobbled up the patch so may as well use it?
Tested version:
From 0b0ee8f270219f5c9092960ed8560d8e039332a9 Mon Sep 17 00:00:00 2001
From: Konrad Rzeszutek Wilk <[email protected]>
Date: Mon, 19 Sep 2016 12:20:27 -0400
Subject: [PATCH] livepatch: Drop _jmp from arch_livepatch_[apply,revert]_jmp
With "livepatch: NOP if func->new_addr is zero." that name makes no more sense.
---
v7: New submission
--- xen/arch/arm/livepatch.c | 4 ++-- xen/arch/x86/livepatch.c | 4 ++-- xen/common/livepatch.c | 4 ++-- xen/include/xen/livepatch.h | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-)
xen/arch/arm/livepatch.c +2 -2
@@ -21,11 +21,11 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)return -ENOSYS ; } -void arch_livepatch_apply_jmp (struct livepatch_func *func) +void arch_livepatch_apply (struct livepatch_func *func) { } -void arch_livepatch_revert_jmp (const struct livepatch_func *func) +void arch_livepatch_revert (const struct livepatch_func *func) { }
xen/arch/x86/livepatch.c +2 -2
@@ -47,7 +47,7 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)return 0 ; } -void arch_livepatch_apply_jmp (struct livepatch_func *func) +void arch_livepatch_apply (struct livepatch_func *func) {uint8_t *old_ptr;uint8_t insn[sizeof (func->opaque)];
@@ -76,7 +76,7 @@ void arch_livepatch_apply_jmp(struct livepatch_func *func)memcpy (old_ptr, insn, len); } -void arch_livepatch_revert_jmp (const struct livepatch_func *func) +void arch_livepatch_revert (const struct livepatch_func *func) {memcpy (func->old_addr, func->opaque,livepatch_insn_len (func)); }
xen/common/livepatch.c +2 -2
@@ -1033,7 +1033,7 @@ static int apply_payload(struct payload *data) }for ( i =0 ; i < data->nfuncs; i++ ) -arch_livepatch_apply_jmp (&data->funcs[i]); +arch_livepatch_apply (&data->funcs[i]);arch_livepatch_revive ();
@@ -1062,7 +1062,7 @@ static int revert_payload(struct payload *data) }for ( i =0 ; i < data->nfuncs; i++ ) -arch_livepatch_revert_jmp (&data->funcs[i]); +arch_livepatch_revert (&data->funcs[i]);arch_livepatch_revive ();
xen/include/xen/livepatch.h +2 -2
@@ -86,8 +86,8 @@ unsigned int livepatch_insn_len(const struct livepatch_func *func)int arch_livepatch_quiesce (void );void arch_livepatch_revive (void ); -void arch_livepatch_apply_jmp (struct livepatch_func *func); -void arch_livepatch_revert_jmp (const struct livepatch_func *func); +void arch_livepatch_apply (struct livepatch_func *func); +void arch_livepatch_revert (const struct livepatch_func *func);void arch_livepatch_post_action (void );void arch_livepatch_mask (void );
Signature · 7 lines
2.4.11 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
In reply to Konrad Rzeszutek WilkQuoted 59 lines
Quoted 1 line
Quoted 1 line · On 19.09.16 at 19:02, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.09.16 at 18:11, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Review void arch_livepatch_revive(void) · line 33
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
(quoted diff) +1
-2@@ -31,11 +30,11 @@ void arch_livepatch_revive(void) int arch_livepatch_verify_func(const struct livepatch_func *func) { - /* No NOP patching yet. */ - if ( !func->new_size ) + /* If NOPing only do up to maximum amount we can put in the ->opaque.*/
Quoted 39 lines
Quoted 39 lines
Quoted 10 lines
Review void arch_livepatch_revive(void) · line 38
Quoted 6 lines
(quoted diff) +2
-1+ if ( !func->new_addr && func->new_size > sizeof(func->opaque) ) return -EOPNOTSUPP; - if ( func->old_size < PATCH_INSN_SIZE ) + if ( func->old_size < ARCH_PATCH_INSN_SIZE ) return -EINVAL;Is that indeed a requirement when NOPing? You can easily NOP out just a single byte on x86. Or shouldn't in that case
old_size==new_sizeanyway? In which case the comment further down stating thatnew_sizeThe original intent behind .old_size was to guard against patching functions that were less than our relative jump.
(The tools end up computing the .old_size as the size of the whole function which is fine).
But with this NOPing support, you are right - we could have now an function that is say 4 bytes long and we only need to NOP three bytes out of it (the last instruction I assume would be 'ret').
So perhaps this check needs just needs an 'else if' , like so:
int
arch_livepatch_verify_func(const struct livepatch_func *func){ /* If NOPing.. */ if ( !func->new_addr ) { /* Only do up to maximum amount we can put in the ->opaque. */ if ( func->new_size > sizeof(func->opaque) ) return -EOPNOTSUPP; /* One instruction for 'ret' and the other to NOP. */ if ( func->old_size < 2 ) return -EINVAL; } else if ( func->old_size < ARCH_PATCH_INSN_SIZE ) return -EINVAL;Quoted 6 lines
Quoted 2 lines
return 0; }Except that I wouldn't use 2, to not exclude patching out some single byte in the middle of a function, without regard to what the function's actual size is.
Quoted 5 lines
Uh-uh.
The _new_size_ determines how many bytes to NOP (in the context of this patch). The
old_size(where we check to be at min 2) is a safety valve to make sure we don't NOP something outside the function.
Well, all this looks a little fishy to me: I don't see the relation to functions at all here. Patching can be done anywhere - at the start of a function, in its middle, at the end, or - in extreme cases - even spanning function boundaries.
So perhaps all you really want then (without altering that basic concept) is new_size <= old_size?
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
NOP addition here, perhaps worth dropping the
_jmpfrom the function name (and its revert counterpart)?Ooh, good idea. But I think it maybe better as a seperate patch (as it also touches the ARM code).
Quoted 8 lines
Quoted 1 line
That's in the other series, isn't it?
It expands the existing ones. Right now in 'staging' branch we have an
arch/arm/livepatch.cwhich has these functions in it.Granted nothing compiles them, so I could break it in this patch.
But I already cobbled up the patch so may as well use it?
Oh, sure.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
In reply to Konrad Rzeszutek WilkQuoted 10 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
docs/misc/livepatch.markdown | 7 +++++-- xen/arch/x86/alternative.c | 2 +- xen/arch/x86/livepatch.c | 40 +++++++++++++++++++++++++-------------- xen/common/livepatch.c | 3 ++- xen/include/asm-x86/alternative.h | 1 + xen/include/asm-x86/livepatch.h | 21 ++++++++++++++++++++ xen/include/xen/livepatch.h | 9 +++++++++ 7 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 xen/include/asm-x86/livepatch.h
I've noticed only while reviewing the other series that this addition should imo be accompanied by a change to ./MAINTAINERS.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 3/6] livepatch: NOP if func->new_addr is zero.
In reply to Konrad Rzeszutek WilkQuoted 33 lines · Konrad Rzeszutek Wilk
The NOP functionality will NOP any of the code at the '
old_addr' or at 'name' if the 'new_addr' is zero. The purpose of this is to NOP out calls, such as:e8 <4-bytes-offset>
(5 byte insn), or on ARM a 4 byte insn for branching.
We need the EIP of where we need to the NOP, and that can be provided via the
old_addrorname.If the
old_addris provided we will NOP 'new_size' amount of bytes at that location.The amount is up to 31 instructions if desired (which is the size of the opaque member). If there is a need to NOP more then: a) more 'struct
livepatch_func' structures need to be present, b) we have to implement a variable size buffer (in the future), or c) first byte an unconditional branch skipping the to be disabled code (of course provided there are no branch targets in the middle).While at it, also unify the code on x86 patching so it is a bit simpler (instead of two seperate writes just make it one memcpy).
And introduce a general
livepatch_insn_leninline function that would depend on platform specific instruction size (for a unconditional branch). As such we also rename thePATCH_INSN_SIZEtoARCH_PATCH_INSN_SIZE.Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
snip
docs/misc/livepatch.markdown The size of the structure is 64 bytes on 64-bit hypervisors. It will be · line 324
Quoted 11 lines
docs/misc/livepatch.markdown +2
-1@@ -320,10 +320,13 @@ The size of the structure is 64 bytes on 64-bit hypervisors. It will be * `new_addr` is the address of the function that is replacing the old function. The address is filled in during relocation. The value **MUST** be - the address of the new function in the file. + either the address of the new function in the file, or zero if we are NOPing out + at `old_addr` (and `new_size` **MUST** not be zero).
The preceding untouched lines "...is the address of the function that is replacing the old function. The address is filled in during relocation..." are a bit stale now. Perhaps the whole paragraph needs to be replaced?
docs/misc/livepatch.markdown The size of the structure is 64 bytes on 64-bit hypervisors. It will be · line 329
Quoted 6 lines
docs/misc/livepatch.markdown +3
-1* `old_size` and `new_size` contain the sizes of the respective functions in bytes. - The value of `old_size` **MUST** not be zero. + The value of `old_size` **MUST** not be zero. If the value of `new_addr` is + zero then `new_size` determines how many instruction bytes to NOP (up to + opaque size modulo smallest platform instruction - 1 byte x86 and 4 bytes on ARM).
This line is probably also stale now: "...contain the sizes of the respective functions in bytes..."
Quoted 16 lines
docs/misc/livepatch.markdown +0
-0* `version` is to be one.xen/arch/x86/alternative.c +1
-1@@ -101,7 +101,7 @@ static void __init arch_init_ideal_nops(void) }/* Use this to add nops to a buffer, then text_poke the whole buffer. */ -static void init_or_livepatch add_nops(void *insns, unsigned int len) +void init_or_livepatch add_nops(void *insns, unsigned int len) {while ( len >0 ) {
snip
Quoted 20 lines
void
arch_livepatch_apply_jmp(struct livepatch_func *func){xen/arch/x86/alternative.c +10
-5-int32_t val;uint8_t *old_ptr; - -BUILD_BUG_ON (PATCH_INSN_SIZE >sizeof (func->opaque)); -BUILD_BUG_ON (PATCH_INSN_SIZE != (1 +sizeof (val))); +uint8_t insn[sizeof (func->opaque)]; +unsigned int len; old_ptr = func->old_addr; -memcpy (func->opaque, old_ptr,PATCH_INSN_SIZE ); + len =livepatch_insn_len (func); +if ( !len ) +return ; + +memcpy (func->opaque, old_ptr, len); +if ( func->new_addr ) + { +int32_t val;
xen/arch/x86/alternative.c
Quoted 10 lines
xen/arch/x86/alternative.c +10
-0+ +BUILD_BUG_ON (ARCH_PATCH_INSN_SIZE != (1 +sizeof (val))); + + insn[0 ] =0xe9 ; + val = func->new_addr - func->old_addr -ARCH_PATCH_INSN_SIZE ; + +memcpy (&insn[1 ], &val,sizeof (val)); + } +else +add_nops (&insn, len);
It probably doesn't make a difference, but I'd prefer this to be "add_nops(insn, len);".
Quoted 13 lines
xen/arch/x86/alternative.c +2
-4- *old_ptr++ =0xe9 ;/* Relative jump */ - val = func->new_addr - func->old_addr -PATCH_INSN_SIZE ; -memcpy (old_ptr, &val,sizeof (val)); +memcpy (old_ptr, insn, len); }void arch_livepatch_revert_jmp (const struct livepatch_func *func) { -memcpy (func->old_addr, func->opaque,PATCH_INSN_SIZE ); +memcpy (func->old_addr, func->opaque,livepatch_insn_len (func)); }
Signature · 6 lines
Ross Lagerwall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v6 4/6 livepach: Add .livepatch.hooks functions and test-case
In reply to Konrad Rzeszutek WilkFrom: Ross Lagerwall <[email protected]>
Add hook functions which run during patch apply and patch revert. Hook functions are used by livepatch payloads to manipulate data structures during patching, etc.
One use case is the XSA91. As Martin mentions it:
"If we have shadow variables, we also need an unload hook to garbage collect all the variables introduced by a hotpatch to prevent memory leaks. Potentially, we also want to pre-reserve memory for static or existing dynamic objects in the load-hook instead of on the fly.
For testing and debugging, various applications are possible.
In general, the hooks provide flexibility when having to deal with unforeseen cases, but their application should be rarely required (< 10%)."
Furthermore include a test-case for it.
---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
v0.4..v0.11: Defered for v4.8
v0.12: s/xsplice/livepatch/
v3: Clarify the comments about spin_debug_enable
Rename one of the hooks to lower-case (Z->z) to guarantee it being called last.
Learned a lot of about 'const'
v4: Do the actual const of the hooks.
Remove the requirement for the tests-case hooks to be sorted (they never were to start with).
Fix up the comment about spin_debug_enable so more.
v5: Added Reviewed-by from Andrew.
Dropped the check_fnc hook
Added the check_fnc hook back again as "livepatch: Disallow applying after an revert" guarantees we won't hit the BUG_ON check.
--- docs/misc/livepatch.markdown | 23 +++++++++++++++++ xen/arch/x86/test/xen_hello_world.c | 34 +++++++++++++++++++++++++ xen/common/livepatch.c | 50 ++++++++++++++++++++++++++++++++++++- xen/include/xen/livepatch_payload.h | 49 ++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 xen/include/xen/livepatch_payload.h
docs/misc/livepatch.markdown +23 -0
@@ -343,6 +343,13 @@ When reverting a patch, the hypervisor iterates over each `livepatch_func` and the core code copies the data from the undo buffer (private internal copy) to `old_addr`. +It optionally may contain the address of functions to be called right before +being applied and after being reverted: + + * `.livepatch.hooks.load` - an array of function pointers. + * `.livepatch.hooks.unload` - an array of function pointers. + + ### Example of .livepatch.funcs A simple example of what a payload file can be:
@@ -380,6 +387,22 @@ struct livepatch_func livepatch_hello_world = { Code must be compiled with -fPIC. +### .livepatch.hooks.load and .livepatch.hooks.unload + +This section contains an array of function pointers to be executed +before payload is being applied (.livepatch.funcs) or after reverting +the payload. This is useful to prepare data structures that need to +be modified patching. + +Each entry in this array is eight bytes. + +The type definition of the function are as follow: + +<pre> +typedef void (*livepatch_loadcall_t)(void); +typedef void (*livepatch_unloadcall_t)(void); +</pre> + ### .livepatch.depends and .note.gnu.build-id To support dependencies checking and safe loading (to load the
xen/arch/x86/test/xen_hello_world.c +34 -0
@@ -4,14 +4,48 @@*/ #include "config.h" +#include <xen/lib.h> #include <xen/types.h> #include <xen/version.h> #include <xen/livepatch.h> +#include <xen/livepatch_payload.h> #include <public/sysctl.h> static char hello_world_patch_this_fnc[] ="xen_extra_version" ;extern const char *xen_hello_world (void ); +static unsigned int cnt; + +static void apply_hook (void ) +{ +printk (KERN_DEBUG "Hook executing.\n" ); +} + +static void revert_hook (void ) +{ +printk (KERN_DEBUG "Hook unloaded.\n" ); +} + +static void hi_func (void ) +{ +printk (KERN_DEBUG "%s: Hi! (called %u times)\n" , __func__, ++cnt); +}; + +static void check_fnc (void ) +{ +printk (KERN_DEBUG "%s: Hi func called %u times\n" , __func__, cnt); +BUG_ON (cnt ==0 || cnt >2 ); +} + +LIVEPATCH_LOAD_HOOK (apply_hook); +LIVEPATCH_UNLOAD_HOOK (revert_hook); + +/* Imbalance here. Two load and three unload. */ + +LIVEPATCH_LOAD_HOOK (hi_func); +LIVEPATCH_UNLOAD_HOOK (hi_func); + +LIVEPATCH_UNLOAD_HOOK (check_fnc); struct livepatch_func __section(".livepatch.funcs") livepatch_xen_hello_world = { .version = LIVEPATCH_PAYLOAD_VERSION,
xen/common/livepatch.c +49 -1
80 lines not on this page · raw message
xen/include/xen/livepatch_payload.h new file +49 -0
49 lines not on this page · raw message
Signature · 7 lines
2.5.5 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v6 5/6 livepatch/tests: Make .livepatch.depends be read-only
In reply to Konrad Rzeszutek WilkAs currently during the injection of the build-id it ends up being marked as AW. We want it to be read-only.
---
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
v6: New submission.
--- xen/arch/x86/test/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/test/Makefile +2 -2
@@ -55,7 +55,7 @@ $(LIVEPATCH): xen_hello_world_func.o xen_hello_world.o note.o note.o: $(OBJCOPY) -O binary --only-section=.note.gnu.build-id $(BASEDIR)/xen-syms [email protected] $(OBJCOPY) -I binary -O elf64-x86-64 -B i386:x86-64 \ - --rename-section=.data=.livepatch.depends -S [email protected] $@ + --rename-section=.data=.livepatch.depends,alloc,load,readonly,data,contents -S [email protected] $@ rm -f [email protected] #
@@ -66,7 +66,7 @@ note.o: hello_world_note.o: $(LIVEPATCH) $(OBJCOPY) -O binary --only-section=.note.gnu.build-id $(LIVEPATCH) [email protected] $(OBJCOPY) -I binary -O elf64-x86-64 -B i386:x86-64 \ - --rename-section=.data=.livepatch.depends -S [email protected] $@ + --rename-section=.data=.livepatch.depends,alloc,load,readonly,data,contents -S [email protected] $@ rm -f [email protected] xen_bye_world.o: config.h
Signature · 7 lines
2.5.5 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v6 5/6] livepatch/tests: Make .livepatch.depends be read-only
Quoted 4 lines · Konrad Rzeszutek Wilk
As currently during the injection of the build-id it ends up being marked as AW. We want it to be read-only.
Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v6 6/6 livepatch/tests: Move the .name value to .rodata
In reply to Konrad Rzeszutek WilkRight now the contents of 'name' are all located in the .data section. We want them in the .rodata section so change the type to have const on them.
---
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
v3: First submission (as part of the ARM 32 and 64 support of Livepatch)
v6: Now moved to the Common Livepatch fixes for 4.8
--- xen/arch/x86/test/xen_bye_world.c | 2 +- xen/arch/x86/test/xen_hello_world.c | 2 +- xen/arch/x86/test/xen_replace_world.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/test/xen_bye_world.c +1 -1
@@ -11,7 +11,7 @@#include <public/sysctl.h> -static char bye_world_patch_this_fnc[] ="xen_extra_version" ; +static const char bye_world_patch_this_fnc[] ="xen_extra_version" ;extern const char *xen_bye_world (void ); struct livepatch_func __section(".livepatch.funcs") livepatch_xen_bye_world = {
xen/arch/x86/test/xen_hello_world.c +1 -1
@@ -12,7 +12,7 @@#include <public/sysctl.h> -static char hello_world_patch_this_fnc[] ="xen_extra_version" ; +static const char hello_world_patch_this_fnc[] ="xen_extra_version" ;extern const char *xen_hello_world (void );static unsigned int cnt;
xen/arch/x86/test/xen_replace_world.c +1 -1
@@ -10,7 +10,7 @@#include <public/sysctl.h> -static char xen_replace_world_name[] ="xen_extra_version" ; +static const char xen_replace_world_name[] ="xen_extra_version" ;extern const char *xen_replace_world (void ); struct livepatch_func __section(".livepatch.funcs") livepatch_xen_replace_world = {
Signature · 7 lines
2.5.5 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v6 6/6] livepatch/tests: Move the .name value to .rodata
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 16.09.16 at 17:29, <[email protected]> wrote:
Right now the contents of 'name' are all located in the .data section. We want them in the .rodata section so change the type to have const on them.
Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
I've only now noticed that the tests get covered in the LIVEPATCH section only by patch 8 of the other series, so patch 5 and 6 here:
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v6 6/6] livepatch/tests: Move the .name value to .rodata
In reply to Konrad Rzeszutek WilkQuoted 5 lines · Konrad Rzeszutek Wilk
Right now the contents of 'name' are all located in the .data section. We want them in the .rodata section so change the type to have const on them.
Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel