Livepatch fixes and general features for Xen 4.8.
PATCH v7 1/1 Livepatch fixes and general features for Xen 4.8.
Hey!
Since v6: [https://lists.xen.org/archives/html/xen-devel/2016-09/msg01719.html]
- Remade "livepatch: Disallow applying after an revert" to look at sh_size.
- Added some Reviewed-by/Acks.
- Added changes as requested.
- Included a new patch: "livepatch: Drop _jmp from arch_livepatch_[apply,revert]_jmp"
- Commited two patches which were Acked/Reviewed-by.
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 legend is:
r - Reviewed-by
a - Acked-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.
livepatch: Drop _jmp from
r livepach: Add .livepatch.hooks functions and test-case
Thanks!
The git tree is
git://xenbits.xen.org/people/konradwilk/xen.git livepatch.v4.8.v7
contains all the following patches (and more):
MAINTAINERS | 1 + docs/misc/livepatch.markdown | 52 ++++++++++++++++++-- xen/arch/arm/livepatch.c | 4 +- xen/arch/x86/alternative.c | 2 +- xen/arch/x86/livepatch.c | 52 +++++++++++++------- xen/arch/x86/test/xen_hello_world.c | 34 +++++++++++++ xen/common/livepatch.c | 95 +++++++++++++++++++++++++++++++++---- xen/common/livepatch_elf.c | 7 ++- xen/include/asm-x86/alternative.h | 1 + xen/include/asm-x86/livepatch.h | 21 ++++++++ xen/include/xen/livepatch.h | 16 ++++++- xen/include/xen/livepatch_payload.h | 49 +++++++++++++++++++ 12 files changed, 298 insertions(+), 36 deletions(-)
Konrad Rzeszutek Wilk (4):
livepatch: Disallow applying after an revert
livepatch: Add limit of 2MB to payload .bss sections.
livepatch: NOP if func->new_addr is zero.
livepatch: Drop _jmp from arch_livepatch_[apply,revert]_jmp
Ross Lagerwall (1):
livepach: Add .livepatch.hooks functions and test-case
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v7 1/5 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. We detect this rather simply (if there is one RW section and its name is .livepatch.funcs) - but the payload may have many other RW sections that are not used at all (such as .bss or .data sections with zero length). To not account those we also ignore sections with sh_size being zero.
---
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
v6: New submission.
v7: Use 'bool' instead of 'bool_t'
- Ignore sh_size==0 sections as a way to make the exception check work.
- Also add the sh_size==0 check in livepatch_elf_resolve_symbols.
- Update comment in load move_payload to mention "livepatch_elf_resolve_symbols"
--- docs/misc/livepatch.markdown | 7 +++++++ xen/common/livepatch.c | 36 +++++++++++++++++++++++++++++++++--- xen/common/livepatch_elf.c | 3 ++- 3 files changed, 42 insertions(+), 4 deletions(-)
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 +33 -3
@@ -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 reverted;/* Whether it was reverted. */ +bool 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 ;
@@ -325,8 +327,13 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)* and .shstrtab. For the non-relocate we allocate and copy these * via other means - and the .rel we can ignore as we only use it * once during loading. +* +* Also ignore sections with zero size. Those can be .data, or .bss. +* +* This logic must MATCH what is done in livepatch_elf_resolve_symbols. */ -if ( !(elf->sec[i].sec->sh_flags &SHF_ALLOC ) ) +if ( !(elf->sec[i].sec->sh_flags &SHF_ALLOC ) || + elf->sec[i].sec->sh_size ==0 ) offset[i] =UINT_MAX ;else if ( (elf->sec[i].sec->sh_flags &SHF_EXECINSTR ) && !(elf->sec[i].sec->sh_flags &SHF_WRITE ) )
@@ -374,14 +381,18 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)for ( i =1 ; i < elf->hdr->e_shnum; i++ ) { -if ( elf->sec[i].sec->sh_flags &SHF_ALLOC ) +if ( elf->sec[i].sec->sh_flags &SHF_ALLOC && elf->sec[i].sec->sh_size ) {void *buf;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 +413,10 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) } } +/* Only one RW section with non-zero size: .livepatch.funcs */ +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 +1072,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 +1454,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 ;
xen/common/livepatch_elf.c +2 -1
@@ -311,7 +311,8 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf) }/* Matches 'move_payload' which ignores such sections. */ -if ( !(elf->sec[idx].sec->sh_flags &SHF_ALLOC ) ) +if ( !(elf->sec[idx].sec->sh_flags &SHF_ALLOC ) || + elf->sec[idx].sec->sh_size ==0 )break ; st_value += (unsigned long )elf->sec[idx].load_addr;
Signature · 7 lines
2.4.11 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v7 1/5] livepatch: Disallow applying after an revert
xen/common/livepatch.c static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 331
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 21.09.16 at 18:57, <[email protected]> wrote:
xen/common/livepatch.c +2
-0@@ -325,8 +327,13 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)* and .shstrtab. For the non-relocate we allocate and copy these * via other means - and the .rel we can ignore as we only use it * once during loading. +* +* Also ignore sections with zero size. Those can be .data, or .bss.
Or any others. Please make this apparent by adding "e.g." or some such.
Review static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 333
Quoted 2 lines
(quoted diff) +2
-0+ * + * This logic must MATCH what is done in livepatch_elf_resolve_symbols.
Instead of such a comment, is it perhaps worth making an inline function or macro to cover the three instances where these checks need to match up?
xen/common/livepatch.c static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 384
Quoted 6 lines
xen/common/livepatch.c +1
-1@@ -374,14 +381,18 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)for ( i =1 ; i < elf->hdr->e_shnum; i++ ) { -if ( elf->sec[i].sec->sh_flags &SHF_ALLOC ) +if ( elf->sec[i].sec->sh_flags &SHF_ALLOC && elf->sec[i].sec->sh_size )
Please parenthesize the & in cases like this.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v7 1/5] livepatch: Disallow applying after an revert
Quoted 10 lines · Jan Beulich
xen/common/livepatch.c static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 331
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 21.09.16 at 18:57, <[email protected]> wrote:
xen/common/livepatch.c +2
-0@@ -325,8 +327,13 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)* and .shstrtab. For the non-relocate we allocate and copy these * via other means - and the .rel we can ignore as we only use it * once during loading. +* +* Also ignore sections with zero size. Those can be .data, or .bss. Or any others. Please make this apparent by adding "e.g." or some such.
<nods>
Quoted 7 lines
Review static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 333
Quoted 3 lines
(quoted diff) +2
-0+ * + * This logic must MATCH what is done in livepatch_elf_resolve_symbols.Instead of such a comment, is it perhaps worth making an inline function or macro to cover the three instances where these checks need to match up?
Yes. That would be much simpler.
Quoted 12 lines
xen/common/livepatch.c static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 384
Quoted 7 lines
xen/common/livepatch.c +1
-1@@ -374,14 +381,18 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)for ( i =1 ; i < elf->hdr->e_shnum; i++ ) { -if ( elf->sec[i].sec->sh_flags &SHF_ALLOC ) +if ( elf->sec[i].sec->sh_flags &SHF_ALLOC && elf->sec[i].sec->sh_size )Please parenthesize the & in cases like this.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v7 1/5] livepatch: Disallow applying after an revert
In reply to Jan BeulichQuoted 18 lines · Jan Beulich
Review static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 331
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 21.09.16 at 18:57, <[email protected]> wrote:
(quoted diff) +2
-0@@ -325,8 +327,13 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) * and .shstrtab. For the non-relocate we allocate and copy these * via other means - and the .rel we can ignore as we only use it * once during loading. + * + * Also ignore sections with zero size. Those can be .data, or .bss.Or any others. Please make this apparent by adding "e.g." or some such.
Review static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 333
Quoted 2 lines
(quoted diff) +2
-0+ * + * This logic must MATCH what is done in livepatch_elf_resolve_symbols.Instead of such a comment, is it perhaps worth making an inline function or macro to cover the three instances where these checks need to match up?
Quoted 8 lines
Review static int move_payload(struct payload *payload, struct livepatch_elf *elf) · line 384
Quoted 6 lines
(quoted diff) +1
-1@@ -374,14 +381,18 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) for ( i = 1; i < elf->hdr->e_shnum; i++ ) { - if ( elf->sec[i].sec->sh_flags & SHF_ALLOC ) + if ( elf->sec[i].sec->sh_flags & SHF_ALLOC && elf->sec[i].sec->sh_size )Please parenthesize the & in cases like this.
Thanks!
Please see the updated patch:
From 829aa410c06231906b5ee786b10ede343ac39884 Mon Sep 17 00:00:00 2001
From: Konrad Rzeszutek Wilk <[email protected]>
Date: Tue, 13 Sep 2016 12:02:20 -0400
Subject: [PATCH v8] 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. We detect this rather simply (if there is one RW section and its name is .livepatch.funcs) - but the payload may have many other RW sections that are not used at all (such as .bss or .data sections with zero length). To not account those we also ignore sections with sh_size being zero.
---
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
v6: New submission.
v7: Use 'bool' instead of 'bool_t'
- Ignore sh_size==0 sections as a way to make the exception check work.
- Also add the sh_size==0 check in livepatch_elf_resolve_symbols.
- Update comment in load move_payload to mention "livepatch_elf_resolve_symbols"
v8: Expand the comment
- Parenthesize the &
- Introduce livepatch_elf_ignore_section which does the sh_size and SHF_ALLOC check
--- docs/misc/livepatch.markdown | 7 +++++++ xen/common/livepatch.c | 34 +++++++++++++++++++++++++++++++--- xen/common/livepatch_elf.c | 3 +-- xen/include/xen/livepatch_elf.h | 4 ++++ 4 files changed, 43 insertions(+), 5 deletions(-)
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 the .data or .bss sections are of zero length. ### Inline patching
xen/common/livepatch.c +31 -3
@@ -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 reverted;/* Whether it was reverted. */ +bool 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 ;
@@ -325,8 +327,11 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)* and .shstrtab. For the non-relocate we allocate and copy these * via other means - and the .rel we can ignore as we only use it * once during loading. +* +* Also ignore sections with zero size. Those can be for example: +* data, or .bss. */ -if ( !(elf->sec[i].sec->sh_flags &SHF_ALLOC ) ) +if (livepatch_elf_ignore_section (elf->sec[i].sec) ) offset[i] =UINT_MAX ;else if ( (elf->sec[i].sec->sh_flags &SHF_EXECINSTR ) && !(elf->sec[i].sec->sh_flags &SHF_WRITE ) )
@@ -374,14 +379,18 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)for ( i =1 ; i < elf->hdr->e_shnum; i++ ) { -if ( elf->sec[i].sec->sh_flags &SHF_ALLOC ) +if ( !livepatch_elf_ignore_section (elf->sec[i].sec) ) {void *buf;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 +411,10 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) } } +/* Only one RW section with non-zero size: .livepatch.funcs */ +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 +1070,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 +1452,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 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 ;
xen/common/livepatch_elf.c +1 -2
@@ -310,8 +310,7 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)break ; } -/* Matches 'move_payload' which ignores such sections. */ -if ( !(elf->sec[idx].sec->sh_flags &SHF_ALLOC ) ) +if (livepatch_elf_ignore_section (elf->sec[idx].sec) )break ; st_value += (unsigned long )elf->sec[idx].load_addr;
xen/include/xen/livepatch_elf.h +4 -0
@@ -46,6 +46,10 @@ void livepatch_elf_free(struct livepatch_elf *elf);int livepatch_elf_resolve_symbols (struct livepatch_elf *elf);int livepatch_elf_perform_relocs (struct livepatch_elf *elf); +static inline bool livepatch_elf_ignore_section (const Elf_Shdr *sec) +{ +return ( !(sec->sh_flags &SHF_ALLOC ) || sec->sh_size ==0 ); +}#endif /* __XEN_LIVEPATCH_ELF_H__ */ /*
Signature · 7 lines
2.4.11 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Jan Beulich
Reviewed-byRe: [PATCH v7 1/5] livepatch: Disallow applying after an revert
xen/include/xen/livepatch_elf.h void livepatch_elf_free(struct livepatch_elf *elf); · line 51
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Konrad Rzeszutek Wilk <[email protected]> 09/23/16 1:56 AM >>>
xen/include/xen/livepatch_elf.h +3
-0@@ -46,6 +46,10 @@ void livepatch_elf_free(struct livepatch_elf *elf);int livepatch_elf_resolve_symbols (struct livepatch_elf *elf);int livepatch_elf_perform_relocs (struct livepatch_elf *elf); +static inline bool livepatch_elf_ignore_section (const Elf_Shdr *sec) +{ +return ( !(sec->sh_flags &SHF_ALLOC ) || sec->sh_size ==0 );
With the stray blanks (and perhaps even the outer parentheses) removed,
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v7 2/5 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.
We also bubble up the payload limit and this one in one #define called LIVEPATCH_MAX_SIZE to make it easier to find these arbitrary limits.
---
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.
v7:- s/MAX_BSS_SIZE/LIVEPATCH_MAX_SIZE/
- Also use this LIVEPATHCH_MAX_SIZE in verify_payload
--- xen/common/livepatch.c | 2 +- xen/common/livepatch_elf.c | 4 ++++ xen/include/xen/livepatch.h | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-)
xen/common/livepatch.c +1 -1
@@ -123,7 +123,7 @@ static int verify_payload(const xen_sysctl_livepatch_upload_t *upload, char *n)if ( !upload->size )return -EINVAL ; -if ( upload->size >MB (2 ) ) +if ( upload->size >LIVEPATCH_MAX_SIZE )return -EINVAL ;if ( !guest_handle_okay (upload->payload, upload->size) )
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 >LIVEPATCH_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 for payload size and .bss section size. */ +#define LIVEPATCH_MAX_SIZE MB(2)struct livepatch_symbol {const char *name;
Signature · 7 lines
2.4.11 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v7 2/5] livepatch: Add limit of 2MB to payload .bss sections.
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 21.09.16 at 18:57, <[email protected]> wrote:
The initial patch:
11ff40fa7bb5fdcc69a58d0fec49c904ffca4793"xen/xsplice: Hypervisor implementation ofXEN_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.
Quoted 6 lines
We also bubble up the payload limit and this one in one #define called
LIVEPATCH_MAX_SIZEto make it easier to find these arbitrary limits.Reviewed-by Ross Lagerwall <[email protected]>Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v7 3/5 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).
v6: Expand in the documentation about the old_addr being at least 5.
v7: Added the extra entry in the MAINTAINERS file.
- Expanded on the livepatch.markdown the description.
- Used add_nops(insn, .. instead of add_nops(&insn, ..)
--- MAINTAINERS | 1 + docs/misc/livepatch.markdown | 22 ++++++++++++++---- xen/arch/x86/alternative.c | 2 +- xen/arch/x86/livepatch.c | 48 +++++++++++++++++++++++++++------------ xen/common/livepatch.c | 3 ++- xen/include/asm-x86/alternative.h | 1 + xen/include/asm-x86/livepatch.h | 21 +++++++++++++++++ xen/include/xen/livepatch.h | 10 ++++++++ 8 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 xen/include/asm-x86/livepatch.h
MAINTAINERS +1 -0
@@ -270,6 +270,7 @@ F: docs/misc/livepatch.markdown F: tools/misc/xen-livepatch.c F: xen/arch/*/livepatch* F: xen/common/livepatch* +F: xen/include/asm-*/livepatch.h F: xen/include/xen/livepatch* MACHINE CHECK (MCA) & RAS
docs/misc/livepatch.markdown +17 -5
@@ -318,13 +318,24 @@ The size of the structure is 64 bytes on 64-bit hypervisors. It will be payload generation time if hypervisor function address is known. If unknown, the value *MUST* be zero and the hypervisor will attempt to resolve the address. -* `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. +* `new_addr` can either have a non-zero value or be zero. + * If there is a non-zero value, then it is the address of the function that is + replacing the old function and the address is recomputed during relocation. + The value **MUST** be the address of the new function in the payload file. -* `old_size` and `new_size` contain the sizes of the respective functions in bytes. + * If the value is zero, then we NOPing out at the `old_addr` location + `new_size` bytes. + +* `old_size` contains the sizes of the respective `old_addr` function in bytes. The value of `old_size` **MUST** not be zero. +* `new_size` depends on what `new_addr` contains: + * If `new_addr` contains an non-zero value, then `new_size` has the size of + the new function (which will replace the one at `old_addr`) in bytes. + * 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. * `opaque` **MUST** be zero.
@@ -1087,7 +1098,8 @@ limit that calls the next trampoline. Please note there is a small limitation for trampolines in function entries: The target function (+ trailing padding) must be able to accomodate the trampoline. On x86 with +-2 GB relative jumps, -this means 5 bytes are required. +this means 5 bytes are required which means that `old_size` **MUST** be +at least five bytes if patching in trampoline. Depending on compiler settings, there are several functions in Xen that are smaller (without inter-function padding).
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 +33 -15
75 lines not on this page · raw message
xen/common/livepatch.c +2 -1
@@ -525,7 +525,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 +10 -0
@@ -68,7 +68,17 @@ 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> int arch_livepatch_verify_func (const struct livepatch_func *func); + +static inline +unsigned int 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.4.11 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v7 3/5] livepatch: NOP if func->new_addr is zero.
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 21.09.16 at 18:57, <[email protected]> wrote:
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).
Quoted 6 lines
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]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v7 4/5 livepatch: Drop _jmp from arch_livepatch_[apply, revert]_jmp
In reply to Konrad Rzeszutek WilkWith "livepatch: NOP if func->new_addr is zero." that name makes no more sense as we also NOP now.
---
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
@@ -46,7 +46,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)];
@@ -75,7 +75,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 v7 4/5] livepatch: Drop _jmp from arch_livepatch_[apply, revert]_jmp
Quoted 5 lines · Konrad Rzeszutek Wilk
With "livepatch: NOP if
func->new_addris zero." that name makes no more sense as we also NOP now.Suggested-by Jan Beulich <[email protected]>Signed-off-by Konrad Rzeszutek Wilk <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v7 5/5 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
@@ -351,6 +351,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:
@@ -388,6 +395,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 const 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.4.11 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel