livepatch: fix handling of (some) relocations
cover livepatch: fix handling of (some) relocations
Hello,
Relocations that reference symbols that belong to sections with a size of 0 are not properly resolved, as the address of those symbols won't be resolved in the first place.
Fix this by not ignoring sections with a size of 0, while still properly handling the detection of whether a livepatch can be reapplied after being reverted (patch 1).
Also detect whether any relocations reference unresolved symbols and error out in that case, as those relocations cannot be resolved (patch 2).
I wonder whether it's possible to have unresolved symbols if we only ignore non SHF_ALLOC sections, so we could maybe error out earlier if we found a symbols that belongs to a non SHF_ALLOC section in livepatch_elf_resolve_symbols. The current approach is more conservative as we would only report an error if we have unresolved symbols that are referenced in relocations.
Thanks, Roger.
Roger Pau Monne (2):
livepatch: do not ignore sections with 0 size
livepatch: avoid relocations referencing ignored section symbols
xen/arch/arm/arm32/livepatch.c | 7 +++++++ xen/arch/arm/arm64/livepatch.c | 7 +++++++ xen/arch/x86/livepatch.c | 7 +++++++ xen/common/livepatch.c | 16 +++++++++++----- xen/common/livepatch_elf.c | 6 ++++++ xen/include/xen/livepatch_elf.h | 3 ++- 6 files changed, 40 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 livepatch: do not ignore sections with 0 size
A side effect of ignoring such sections is that symbols belonging to them won't be resolved, and that could make relocations belonging to other sections that reference those symbols fail.
For example it's likely to have an empty .altinstr_replacement with symbols pointing to it, and marking the section as ignored will prevent the symbols from being resolved, which in turn will cause any relocations against them to fail.
In order to solve this do not ignore sections with 0 size, only ignore sections that don't have the SHF_ALLOC flag set.
Special case such empty sections in move_payload so they are not taken into account in order to decide whether a livepatch can be safely re-applied after a revert.
--- xen/common/livepatch.c | 16 +++++++++++----- xen/include/xen/livepatch_elf.h | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-)
xen/common/livepatch.c +11 -5
@@ -300,9 +300,6 @@ 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 (livepatch_elf_ignore_section (elf->sec[i].sec) ) offset[i] =UINT_MAX ;
@@ -361,8 +358,17 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)else if ( elf->sec[i].sec->sh_flags &SHF_WRITE ) { buf = rw_buf; - rw_buf_sec = i; - rw_buf_cnt++; +if ( elf->sec[i].sec->sh_size ) + { +/* +* Special handling of RW empty regions: do not account for +* them in order to decide whether a patch can safely be +* re-applied, but assign them a load address so symbol +* resolution and relocations work. +*/ + rw_buf_sec = i; + rw_buf_cnt++; + } }else buf = ro_buf;
xen/include/xen/livepatch_elf.h +1 -1
@@ -48,7 +48,7 @@ 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 ; +return !(sec->sh_flags &SHF_ALLOC ); }#endif /* __XEN_LIVEPATCH_ELF_H__ */
Signature · 1 line
2.34.1
Re: [PATCH 1/2] livepatch: do not ignore sections with 0 size
Quoted 8 lines · Roger Pau Monne
A side effect of ignoring such sections is that symbols belonging to them won't be resolved, and that could make relocations belonging to other sections that reference those symbols fail.
For example it's likely to have an empty .altinstr_replacement with symbols pointing to it, and marking the section as ignored will prevent the symbols from being resolved, which in turn will cause any relocations against them to fail.
I agree this is a bug in livepatch handling, but it's also an error in the generated livepatch. We should not have relocations to an empty altinstr_replacement section in the first place.
This will probably be from the lfences, where the replacement in a nop and takes no space. I think I know how to fix this case.
~Andrew
Re: [PATCH 1/2] livepatch: do not ignore sections with 0 size
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Roger Pau Monne
A side effect of ignoring such sections is that symbols belonging to them won't be resolved, and that could make relocations belonging to other sections that reference those symbols fail.
Quoted 8 lines
Quoted 4 lines
For example it's likely to have an empty .altinstr_replacement with symbols pointing to it, and marking the section as ignored will prevent the symbols from being resolved, which in turn will cause any relocations against them to fail.
I agree this is a bug in livepatch handling, but it's also an error in the generated livepatch. We should not have relocations to an empty
altinstr_replacementsection in the first place.
I think having such relocations is quite natural, but ...
Quoted 2 lines
This will probably be from the lfences, where the replacement in a nop and takes no space. I think I know how to fix this case.
... of course it's possible to eliminate them. Whether that's worthwhile to add logic for I'm not sure.
Jan
Re: [PATCH 1/2] livepatch: do not ignore sections with 0 size
In reply to Andrew CooperQuoted 5 lines · Andrew Cooper
Quoted 4 lines · Roger Pau Monne
A side effect of ignoring such sections is that symbols belonging to them won't be resolved, and that could make relocations belonging to other sections that reference those symbols fail.
Quoted 8 lines
Quoted 4 lines
For example it's likely to have an empty .altinstr_replacement with symbols pointing to it, and marking the section as ignored will prevent the symbols from being resolved, which in turn will cause any relocations against them to fail.
I agree this is a bug in livepatch handling, but it's also an error in the generated livepatch. We should not have relocations to an empty
altinstr_replacementsection in the first place.
Well, the relocation destination is in the .altinstructions section (which is not empty). It happens however to reference a symbol that points to the .altinstr_replacement section that's empty.
We could likely avoid generating the altinstr_replacement section in the first place, but I think it's more robust to handle those properly in the elf loading code.
Quoted 2 lines
This will probably be from the lfences, where the replacement in a nop and takes no space. I think I know how to fix this case.
Indeed, that's my understanding.
Thanks, Roger.
Re: [PATCH 1/2] livepatch: do not ignore sections with 0 size
Quoted 13 lines · Roger Pau Monné
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Roger Pau Monne
A side effect of ignoring such sections is that symbols belonging to them won't be resolved, and that could make relocations belonging to other sections that reference those symbols fail.
Quoted 7 lines
Quoted 4 lines
For example it's likely to have an empty .altinstr_replacement with symbols pointing to it, and marking the section as ignored will prevent the symbols from being resolved, which in turn will cause any relocations against them to fail.
I agree this is a bug in livepatch handling, but it's also an error in the generated livepatch. We should not have relocations to an empty
altinstr_replacementsection in the first place.
Quoted 7 lines
Well, the relocation destination is in the .altinstructions section (which is not empty). It happens however to reference a symbol that points to the .altinstr_replacement section that's empty.
We could likely avoid generating the
altinstr_replacementsection in the first place, but I think it's more robust to handle those properly in the elf loading code.
Actually, it turns out it's distinctly non-trivial to omit these references. We need to put the replacement somewhere, so we can subtract the start from the end, and figure out if it is 0.
~Andrew
Re: [PATCH 1/2] livepatch: do not ignore sections with 0 size
In reply to Roger Pau MonnePing?
There was some discussion on whether we need to handle such empty sections, but I think we settled that it's necessary.
Thanks, Roger.
Quoted 73 lines · Roger Pau Monne
A side effect of ignoring such sections is that symbols belonging to them won't be resolved, and that could make relocations belonging to other sections that reference those symbols fail.
For example it's likely to have an empty .altinstr_replacement with symbols pointing to it, and marking the section as ignored will prevent the symbols from being resolved, which in turn will cause any relocations against them to fail.
In order to solve this do not ignore sections with 0 size, only ignore sections that don't have the
SHF_ALLOCflag set.Special case such empty sections in
move_payloadso they are not taken into account in order to decide whether a livepatch can be safely re-applied after a revert.Fixes 98b728a7b2 ('livepatch: Disallow applying after an revert')Signed-off-by Roger Pau Monné <[email protected]>--- xen/common/livepatch.c | 16 +++++++++++----- xen/include/xen/livepatch_elf.h | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-)xen/common/livepatch.c +11
-5@@ -300,9 +300,6 @@ 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 (livepatch_elf_ignore_section (elf->sec[i].sec) ) offset[i] =UINT_MAX ;@@ -361,8 +358,17 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)else if ( elf->sec[i].sec->sh_flags &SHF_WRITE ) { buf = rw_buf; - rw_buf_sec = i; - rw_buf_cnt++; +if ( elf->sec[i].sec->sh_size ) + { +/* +* Special handling of RW empty regions: do not account for +* them in order to decide whether a patch can safely be +* re-applied, but assign them a load address so symbol +* resolution and relocations work. +*/ + rw_buf_sec = i; + rw_buf_cnt++; + } }else buf = ro_buf;xen/include/xen/livepatch_elf.h +1
-1@@ -48,7 +48,7 @@ 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 ; +return !(sec->sh_flags &SHF_ALLOC ); }#endif /* __XEN_LIVEPATCH_ELF_H__ */ Signature · 1 line
2.34.1
Re: [PATCH 1/2] livepatch: do not ignore sections with 0 size
In reply to Roger Pau MonneQuoted 18 lines
From: Roger Pau Monne <[email protected]>
Sent: Thursday, March 17, 2022 11:08 AM
To: [email protected] <[email protected]>
Cc: Roger Pau Monne <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Ross Lagerwall <[email protected]>
Subject: [PATCH 1/2] livepatch: do not ignore sections with 0 sizeA side effect of ignoring such sections is that symbols belonging to them won't be resolved, and that could make relocations belonging to other sections that reference those symbols fail.
For example it's likely to have an empty .altinstr_replacement with symbols pointing to it, and marking the section as ignored will prevent the symbols from being resolved, which in turn will cause any relocations against them to fail.
In order to solve this do not ignore sections with 0 size, only ignore sections that don't have the
SHF_ALLOCflag set.
Quoted 6 lines
Special case such empty sections in
move_payloadso they are not taken into account in order to decide whether a livepatch can be safely re-applied after a revert.Fixes 98b728a7b2 ('livepatch: Disallow applying after an revert')Signed-off-by Roger Pau Monné <[email protected]>
PATCH 2/2 livepatch: avoid relocations referencing ignored section symbols
In reply to Roger Pau MonneTrack whether symbols belong to ignored sections in order to avoid applying relocations referencing those symbols. The address of such symbols won't be resolved and thus the relocation will likely fail or write garbage to the destination.
Return an error in that case, as leaving unresolved relocations would lead to malfunctioning payload code.
--- xen/arch/arm/arm32/livepatch.c | 7 +++++++ xen/arch/arm/arm64/livepatch.c | 7 +++++++ xen/arch/x86/livepatch.c | 7 +++++++ xen/common/livepatch_elf.c | 6 ++++++ xen/include/xen/livepatch_elf.h | 1 + 5 files changed, 28 insertions(+)
xen/arch/arm/arm32/livepatch.c +7 -0
@@ -272,6 +272,13 @@ int arch_livepatch_perform(struct livepatch_elf *elf, elf->name, symndx);return -EINVAL ; } +else if ( elf->sym[symndx].ignored ) + { + printk(XENLOG_ERR LIVEPATCH +"%s: Relocation against ignored symbol %s cannot be resolved\n" , + elf->name, elf->sym[symndx].name); +return -EINVAL ; + } val = elf->sym[symndx].sym->st_value;/* S */
xen/arch/arm/arm64/livepatch.c +7 -0
@@ -270,6 +270,13 @@ int arch_livepatch_perform_rela(struct livepatch_elf *elf, elf->name, symndx);return -EINVAL ; } +else if ( elf->sym[symndx].ignored ) + { + printk(XENLOG_ERR LIVEPATCH +"%s: Relocation against ignored symbol %s cannot be resolved\n" , + elf->name, elf->sym[symndx].name); +return -EINVAL ; + } val = elf->sym[symndx].sym->st_value + r->r_addend;/* S+A */
xen/arch/x86/livepatch.c +7 -0
@@ -262,6 +262,13 @@ int arch_livepatch_perform_rela(struct livepatch_elf *elf, elf->name, symndx);return -EINVAL ; } +else if ( elf->sym[symndx].ignored ) + { + printk(XENLOG_ERR LIVEPATCH +"%s: Relocation against ignored symbol %s cannot be resolved\n" , + elf->name, elf->sym[symndx].name); +return -EINVAL ; + } val = r->r_addend + elf->sym[symndx].sym->st_value;
xen/common/livepatch_elf.c +6 -0
@@ -334,7 +334,13 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf) }if (livepatch_elf_ignore_section (elf->sec[idx].sec) ) + { +dprintk (XENLOG_DEBUG ,LIVEPATCH +"%s: Symbol %s from section %s ignored\n" , + elf->name, elf->sym[i].name, elf->sec[idx].name); + elf->sym[i].ignored = true;break ; + } st_value += (unsigned long )elf->sec[idx].load_addr;if ( elf->sym[i].name )
xen/include/xen/livepatch_elf.h +1 -0
@@ -22,6 +22,7 @@ struct livepatch_elf_sec {struct livepatch_elf_sym {const Elf_Sym *sym;const char *name; +bool ignored; };struct livepatch_elf {
Signature · 1 line
2.34.1
Re: [PATCH 2/2] livepatch: avoid relocations referencing ignored section symbols
Quoted 9 lines · Roger Pau Monne
Track whether symbols belong to ignored sections in order to avoid applying relocations referencing those symbols. The address of such symbols won't be resolved and thus the relocation will likely fail or write garbage to the destination.
Return an error in that case, as leaving unresolved relocations would lead to malfunctioning payload code.
Signed-off-by Roger Pau Monné <[email protected]>
with one nit (which can likely be addressed while committing):
xen/arch/arm/arm32/livepatch.c int arch_livepatch_perform(struct livepatch_elf *elf, · line 279
Quoted 11 lines
xen/arch/arm/arm32/livepatch.c +5
-0@@ -272,6 +272,13 @@ int arch_livepatch_perform(struct livepatch_elf *elf, elf->name, symndx);return -EINVAL ; } +else if ( elf->sym[symndx].ignored ) + { + printk(XENLOG_ERR LIVEPATCH +"%s: Relocation against ignored symbol %s cannot be resolved\n" , + elf->name, elf->sym[symndx].name);
Indentation here (and in the other two instances mirroring this) is off by one.
Jan
Re: [PATCH 2/2] livepatch: avoid relocations referencing ignored section symbols
Quoted 14 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne
Track whether symbols belong to ignored sections in order to avoid applying relocations referencing those symbols. The address of such symbols won't be resolved and thus the relocation will likely fail or write garbage to the destination.
Return an error in that case, as leaving unresolved relocations would lead to malfunctioning payload code.
Signed-off-by Roger Pau Monné <[email protected]>Reviewed-by Jan Beulich <[email protected]>with one nit (which can likely be addressed while committing):
Quoted 14 lines
xen/arch/arm/arm32/livepatch.c int arch_livepatch_perform(struct livepatch_elf *elf, · line 279
Quoted 11 lines
xen/arch/arm/arm32/livepatch.c +5
-0@@ -272,6 +272,13 @@ int arch_livepatch_perform(struct livepatch_elf *elf, elf->name, symndx);return -EINVAL ; } +else if ( elf->sym[symndx].ignored ) + { + printk(XENLOG_ERR LIVEPATCH +"%s: Relocation against ignored symbol %s cannot be resolved\n" , + elf->name, elf->sym[symndx].name);Indentation here (and in the other two instances mirroring this) is off by one.
Oh, thanks, sorry for not noticing.
Roger.
Re: [PATCH 2/2] livepatch: avoid relocations referencing ignored section symbols
In reply to Roger Pau MonneQuoted 11 lines
From: Roger Pau Monne <[email protected]>
Sent: Thursday, March 17, 2022 11:08 AM
To: [email protected] <[email protected]>
Cc: Roger Pau Monne <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Ross Lagerwall <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH 2/2] livepatch: avoid relocations referencing ignored section symbolsTrack whether symbols belong to ignored sections in order to avoid applying relocations referencing those symbols. The address of such symbols won't be resolved and thus the relocation will likely fail or write garbage to the destination.
Quoted 4 lines
Return an error in that case, as leaving unresolved relocations would lead to malfunctioning payload code.
Signed-off-by Roger Pau Monné <[email protected]>
Quoted 6 lines · Roger Pau Monne
I wonder whether it's possible to have unresolved symbols if we only ignore non
SHF_ALLOCsections, so we could maybe error out earlier if we found a symbols that belongs to a nonSHF_ALLOCsection inlivepatch_elf_resolve_symbols. The current approach is more conservative as we would only report an error if we have unresolved symbols that are referenced in relocations.
I think it's better to remain that way. Symbols appearing in non-alloc sections isn't wrong in any way, as long - as you say - there's no relocation using them.
Jan