livepatch: minor bug fixes and improvements
v4 cover livepatch: minor bug fixes and improvements
Hello,
Following series contain some minor bugfixes and improvements for livepatch logic, both inside the hypervisor and on the command line tool.
Thanks, Roger.
Roger Pau Monne (4):
xen-livepatch: fix parameter name parsing
livepatch: introduce --force option
livepatch: refuse to resolve symbols that belong to init sections x86/livepatch: perform sanity checks on the payload exception table
contents
tools/include/xenctrl.h | 3 ++- tools/libs/ctrl/xc_misc.c | 7 +++---- tools/misc/xen-livepatch.c | 25 +++++++++++++++++++++---- xen/arch/x86/extable.c | 18 ++++++++++++++++++ xen/arch/x86/include/asm/uaccess.h | 3 +++ xen/common/livepatch.c | 25 +++++++++++++++++++------ xen/common/livepatch_elf.c | 22 +++++++++++++++++++++- xen/include/public/sysctl.h | 4 +++- xen/include/xen/livepatch_elf.h | 2 +- 9 files changed, 91 insertions(+), 18 deletions(-)
Signature · 1 line
2.44.0
PATCH v4 1/4 xen-livepatch: fix parameter name parsing
It's incorrect to restrict strncmp to the length of the command line input parameter, as then a user passing a rune like:
% xen-livepatch up foo.livepatch
Would match against the "upload" command, because the string comparison has been truncated to the length of the input argument. Use strcmp instead which doesn't truncate. Otherwise in order to keep using strncmp we would need to also check strings are of the same length before doing the comparison.
---
Changes since v3:
- Use strcmp.
Changes since v2:
- New in this version.
--- tools/misc/xen-livepatch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/misc/xen-livepatch.c +2 -2
@@ -572,13 +572,13 @@ int main(int argc, char *argv[])return 0 ; }for ( i =0 ; i <ARRAY_SIZE (main_options); i++ ) -if (!strncmp (main_options[i].name, argv[1 ],strlen (argv[1 ]))) +if (!strcmp (main_options[i].name, argv[1 ]))break ;if ( i ==ARRAY_SIZE (main_options) ) {for ( j =0 ; j <ARRAY_SIZE (action_options); j++ ) -if (!strncmp (action_options[j].name, argv[1 ],strlen (argv[1 ]))) +if (!strcmp (action_options[j].name, argv[1 ]))break ;if ( j ==ARRAY_SIZE (action_options) )
Signature · 1 line
2.44.0
Re: [PATCH v4 1/4] xen-livepatch: fix parameter name parsing
Quoted 5 lines · Roger Pau Monne
It's incorrect to restrict strncmp to the length of the command line input parameter, as then a user passing a rune like:
% xen-livepatch up foo.livepatch
Quoted 7 lines
Would match against the "upload" command, because the string comparison has been truncated to the length of the input argument. Use strcmp instead which doesn't truncate. Otherwise in order to keep using strncmp we would need to also check strings are of the same length before doing the comparison.
Fixes 05bb8afedede ('xen-xsplice: Tool to manipulate xsplice payloads')Signed-off-by Roger Pau Monné <[email protected]>
Thanks,
Signature · 1 line
Anthony PERARD
Re: [PATCH v4 1/4] xen-livepatch: fix parameter name parsing
In reply to Roger Pau MonneQuoted 11 lines · Roger Pau Monne
It's incorrect to restrict strncmp to the length of the command line input parameter, as then a user passing a rune like:
% xen-livepatch up foo.livepatch
Would match against the "upload" command, because the string comparison has been truncated to the length of the input argument. Use strcmp instead which doesn't truncate. Otherwise in order to keep using strncmp we would need to also check strings are of the same length before doing the comparison.
I had previously assumed that this was intentional as a way of allowing abbreviated syntax. Regardless of the original intent, I'm OK with this change.
PATCH v4 2/4 livepatch: introduce --force option
In reply to Roger Pau MonneIntroduce a xen-livepatch tool --force option, that's propagated into the hyerpvisor for livepatch operations. The intention is for the option to be used to bypass some checks that would otherwise prevent the patch from being loaded.
Re purpose the pad field in xen_sysctl_livepatch_op to be a flags field that applies to all livepatch operations. The flag is currently only set by the hypercall wrappers for the XEN_SYSCTL_LIVEPATCH_UPLOAD operation, as that's so far the only one where it will be used initially. Other uses can be added as required.
Note that helpers would set the .pad field to 0, that's been removed since the structure is already zero initialized at definition.
No functional usages of the new flag introduced in this patch.
---
Changes since v3:
- Use strcmp instead of strncmp.
Changes since v2:
- New in this version.
--- tools/include/xenctrl.h | 3 ++- tools/libs/ctrl/xc_misc.c | 7 +++---- tools/misc/xen-livepatch.c | 21 +++++++++++++++++++-- xen/common/livepatch.c | 3 ++- xen/include/public/sysctl.h | 4 +++- 5 files changed, 29 insertions(+), 9 deletions(-)
tools/include/xenctrl.h +2 -1
@@ -2555,7 +2555,8 @@ int xc_psr_get_hw_info(xc_interface *xch, uint32_t socket,#endif int xc_livepatch_upload (xc_interface *xch, -char *name,unsigned char *payload,uint32_t size); +char *name,unsigned char *payload,uint32_t size, +bool force); int xc_livepatch_get(xc_interface *xch, char *name,
tools/libs/ctrl/xc_misc.c +3 -4
@@ -576,7 +576,8 @@ int xc_getcpuinfo(xc_interface *xch, int max_cpus,int xc_livepatch_upload (xc_interface *xch,char *name,unsigned char *payload, -uint32_t size) +uint32_t size, +bool force) {int rc;struct xen_sysctl sysctl = {};
@@ -612,7 +613,7 @@ int xc_livepatch_upload(xc_interface *xch, sysctl.cmd = XEN_SYSCTL_livepatch_op; sysctl.u.livepatch.cmd =XEN_SYSCTL_LIVEPATCH_UPLOAD ; - sysctl.u.livepatch.pad =0 ; + sysctl.u.livepatch.flags = force ?LIVEPATCH_FLAG_FORCE :0 ; sysctl.u.livepatch.u.upload.size = size;set_xen_guest_handle (sysctl.u.livepatch.u.upload.payload, local);
@@ -656,7 +657,6 @@ int xc_livepatch_get(xc_interface *xch, sysctl.cmd = XEN_SYSCTL_livepatch_op; sysctl.u.livepatch.cmd =XEN_SYSCTL_LIVEPATCH_GET ; - sysctl.u.livepatch.pad =0 ; sysctl.u.livepatch.u.get.status.state =0 ; sysctl.u.livepatch.u.get.status.rc =0 ;
@@ -985,7 +985,6 @@ static int _xc_livepatch_action(xc_interface *xch, sysctl.cmd = XEN_SYSCTL_livepatch_op; sysctl.u.livepatch.cmd =XEN_SYSCTL_LIVEPATCH_ACTION ; - sysctl.u.livepatch.pad =0 ; sysctl.u.livepatch.u.action.cmd = action; sysctl.u.livepatch.u.action.timeout = timeout; sysctl.u.livepatch.u.action.flags = flags;
tools/misc/xen-livepatch.c +19 -2
@@ -19,11 +19,15 @@static xc_interface *xch; +/* Global option to disable checks. */ +static bool force; +void show_help (void ) {fprintf (stderr,"xen-livepatch: live patching tool\n" -"Usage: xen-livepatch <command> [args] [command-flags]\n" +"Usage: xen-livepatch [--force] <command> [args] [command-flags]\n" +" Use --force option to bypass some checks.\n" " <name> An unique name of payload. Up to %d characters.\n" "Commands:\n" " help display this help\n"
@@ -240,7 +244,7 @@ static int upload_func(int argc, char *argv[])return saved_errno; }printf ("Uploading %s... " , filename); - rc =xc_livepatch_upload (xch, name, fbuf, len); + rc =xc_livepatch_upload (xch, name, fbuf, len, force);if ( rc ) { rc = errno;
@@ -571,6 +575,19 @@ int main(int argc, char *argv[])show_help ();return 0 ; } + +if (strcmp ("--force" , argv[1 ]) ) + { +if ( argc <=2 ) + { +show_help (); +return EXIT_FAILURE ; + } + force = true; + argv++; + argc--; + } +for ( i =0 ; i <ARRAY_SIZE (main_options); i++ )if (!strcmp (main_options[i].name, argv[1 ]))break ;
xen/common/livepatch.c +2 -1
@@ -2125,7 +2125,8 @@ int livepatch_op(struct xen_sysctl_livepatch_op *livepatch) {int rc; -if ( livepatch->pad ) +if ( (livepatch->flags & ~LIVEPATCH_FLAGS_MASK ) && + !(livepatch->flags &LIVEPATCH_FLAG_FORCE ) )return -EINVAL ; switch ( livepatch->cmd )
xen/include/public/sysctl.h +3 -1
@@ -1139,7 +1139,9 @@ struct xen_sysctl_livepatch_action {struct xen_sysctl_livepatch_op {uint32_t cmd;/* IN: XEN_SYSCTL_LIVEPATCH_*. */ -uint32_t pad;/* IN: Always zero. */ +uint32_t flags;/* IN, flags. */ +#define LIVEPATCH_FLAG_FORCE (1u << 0)/* Skip some checks. */ +#define LIVEPATCH_FLAGS_MASK LIVEPATCH_FLAG_FORCEunion {struct xen_sysctl_livepatch_upload upload;struct xen_sysctl_livepatch_list list;
Signature · 1 line
2.44.0
Re: [PATCH v4 2/4] livepatch: introduce --force option
tools/misc/xen-livepatch.c int main(int argc, char *argv[]) · line 579
Quoted 6 lines · Roger Pau Monne
tools/misc/xen-livepatch.c +2
-0@@ -571,6 +575,19 @@ int main(int argc, char *argv[])show_help ();return 0 ; } + +if (strcmp ("--force" , argv[1 ]) )
I guess this missing ! or "== 0" is the reason for osstest reporting a livepatch-run failure.
Jan
Quoted 14 lines
tools/misc/xen-livepatch.c +11
-0+ { +if ( argc <=2 ) + { +show_help (); +return EXIT_FAILURE ; + } + force = true; + argv++; + argc--; + } +for ( i =0 ; i <ARRAY_SIZE (main_options); i++ )if (!strcmp (main_options[i].name, argv[1 ]))break ;
Re: [PATCH v4 2/4] livepatch: introduce --force option
Quoted 10 lines · Jan Beulich
tools/misc/xen-livepatch.c int main(int argc, char *argv[]) · line 579
Quoted 6 lines · Roger Pau Monne
tools/misc/xen-livepatch.c +2
-0@@ -571,6 +575,19 @@ int main(int argc, char *argv[])show_help ();return 0 ; } + +if (strcmp ("--force" , argv[1 ]) )I guess this missing ! or "== 0" is the reason for osstest reporting a livepatch-run failure.
Bah, seems like I dropped it when changing from strncmp to strcmp, as it's present in v3.
Will send a fix ASAP.
Thanks, Roger.
PATCH v4 3/4 livepatch: refuse to resolve symbols that belong to init sections
In reply to Roger Pau MonneLivepatch payloads containing symbols that belong to init sections can only lead to page faults later on, as by the time the livepatch is loaded init sections have already been freed.
Refuse to resolve such symbols and return an error instead.
Note such resolutions are only relevant for symbols that point to undefined sections (SHN_UNDEF), as that implies the symbol is not in the current payload and hence must either be a Xen or a different livepatch payload symbol.
Do not allow to resolve symbols that point to __init_begin, as that address is also unmapped. On the other hand, __init_end is not unmapped, and hence allow resolutions against it.
Since __init_begin can alias other symbols (like _erodata for example) allow the force flag to override the check and resolve the symbol anyway.
---
Changes since v3:
- Print warning message even when using the force option.
Changes since v2:
- Allow bypassing added check with the force flag.
Changes since v1:
- Fix off-by-one in range checking.
--- xen/common/livepatch.c | 13 ++++++++----- xen/common/livepatch_elf.c | 22 +++++++++++++++++++++- xen/include/xen/livepatch_elf.h | 2 +- 3 files changed, 30 insertions(+), 7 deletions(-)
xen/common/livepatch.c +8 -5
@@ -1080,7 +1080,8 @@ static void free_payload(struct payload *data)xfree (data); } -static int load_payload_data (struct payload *payload,void *raw,size_t len) +static int load_payload_data (struct payload *payload,void *raw,size_t len, +bool force) {struct livepatch_elf elf = { .name = payload->name, .len = len };int rc =0 ;
@@ -1093,7 +1094,7 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)if ( rc ) goto out; - rc =livepatch_elf_resolve_symbols (&elf); + rc =livepatch_elf_resolve_symbols (&elf, force);if ( rc ) goto out;
@@ -1133,7 +1134,8 @@ static int load_payload_data(struct payload *payload, void *raw, size_t len)return rc; } -static int livepatch_upload (struct xen_sysctl_livepatch_upload *upload) +static int livepatch_upload (struct xen_sysctl_livepatch_upload *upload, +bool force) {struct payload *data, *found;char n[XEN_LIVEPATCH_NAME_SIZE ];
@@ -1162,7 +1164,7 @@ static int livepatch_upload(struct xen_sysctl_livepatch_upload *upload) {memcpy (data->name, n,strlen (n)); - rc =load_payload_data (data, raw_data, upload->size); + rc =load_payload_data (data, raw_data, upload->size, force);if ( rc ) goto out;
@@ -2132,7 +2134,8 @@ int livepatch_op(struct xen_sysctl_livepatch_op *livepatch)switch ( livepatch->cmd ) {case XEN_SYSCTL_LIVEPATCH_UPLOAD : - rc =livepatch_upload (&livepatch->u.upload); + rc =livepatch_upload (&livepatch->u.upload, + livepatch->flags &LIVEPATCH_FLAG_FORCE );break ;case XEN_SYSCTL_LIVEPATCH_GET :
xen/common/livepatch_elf.c +21 -1
@@ -4,6 +4,7 @@#include <xen/errno.h> #include <xen/lib.h> +#include <xen/sections.h> #include <xen/symbols.h> #include <xen/livepatch_elf.h> #include <xen/livepatch.h>
@@ -276,7 +277,7 @@ static int elf_get_sym(struct livepatch_elf *elf, const void *data)return 0 ; } -int livepatch_elf_resolve_symbols (struct livepatch_elf *elf) +int livepatch_elf_resolve_symbols (struct livepatch_elf *elf,bool force) {unsigned int i;int rc =0 ;
@@ -310,6 +311,25 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)break ; } } + +/* +* Ensure not an init symbol. Only applicable to Xen symbols, as +* livepatch payloads don't have init sections or equivalent. +*/ +else if ( st_value >= (uintptr_t )&__init_begin && + st_value < (uintptr_t )&__init_end ) + { +printk ("%s" LIVEPATCH "%s: symbol %s is in init section%s\n" , + force ?XENLOG_WARNING :XENLOG_ERR , + elf->name, elf->sym[i].name, + force ?"" :", not resolving" ); +if ( !force ) + { + rc = -ENXIO ; +break ; + } + } +dprintk (XENLOG_DEBUG ,LIVEPATCH "%s: Undefined symbol resolved: %s => %#" PRIxElfAddr"\n" , elf->name, elf->sym[i].name, st_value);break ;
xen/include/xen/livepatch_elf.h +1 -1
@@ -44,7 +44,7 @@ livepatch_elf_sec_by_name(const struct livepatch_elf *elf,int livepatch_elf_load (struct livepatch_elf *elf,const void *data);void livepatch_elf_free (struct livepatch_elf *elf); -int livepatch_elf_resolve_symbols (struct livepatch_elf *elf); +int livepatch_elf_resolve_symbols (struct livepatch_elf *elf,bool force);int livepatch_elf_perform_relocs (struct livepatch_elf *elf);static inline bool livepatch_elf_ignore_section (const Elf_Shdr *sec)
Signature · 1 line
2.44.0
Re: [PATCH v4 3/4] livepatch: refuse to resolve symbols that belong to init sections
Quoted 10 lines · Roger Pau Monne
Livepatch payloads containing symbols that belong to init sections can only lead to page faults later on, as by the time the livepatch is loaded init sections have already been freed.
Refuse to resolve such symbols and return an error instead.
Note such resolutions are only relevant for symbols that point to undefined sections (
SHN_UNDEF), as that implies the symbol is not in the current payload and hence must either be a Xen or a different livepatch payload symbol.
Quoted 6 lines
Do not allow to resolve symbols that point to
__init_begin, as that address is also unmapped. On the other hand,__init_endis not unmapped, and hence allow resolutions against it.Since
__init_begincan alias other symbols (like_erodatafor example) allow the force flag to override the check and resolve the symbol anyway.
I've been thinking more about this, and it has further corner cases, for example with this patch applied attempting to livepatch a function that contains a rune like:
if ( system_state < SYS_STATE_active )
call_init_function();
Will now fail as Xen will refuse to resolve the call_init_function symbol.
I however don't have any better ideas about how to solve this. Maybe it's fine to require such patches to use the --force option, as refusing to resolve init symbols adds a bit more safety to livepatches.
Thanks, Roger.
PATCH v4 4/4 x86/livepatch: perform sanity checks on the payload exception table contents
In reply to Roger Pau MonneEnsure the entries of a payload exception table only apply to text regions in the payload itself. Since the payload exception table needs to be loaded and active even before a patch is applied (because hooks might already rely on it), make sure the exception table (if any) only contains fixups for the payload text section.
---
Changes since v3:
- Rename to extable_in_bounds().
- Use _p() instead of casting to void *.
Changes since v2:
- New in this version.
--- xen/arch/x86/extable.c | 18 ++++++++++++++++++ xen/arch/x86/include/asm/uaccess.h | 3 +++ xen/common/livepatch.c | 9 +++++++++ 3 files changed, 30 insertions(+)
xen/arch/x86/extable.c +18 -0
@@ -228,3 +228,21 @@ unsigned long asmlinkage search_pre_exception_table(struct cpu_user_regs *regs) }return fixup; } + +#ifdef CONFIG_LIVEPATCH +bool extable_in_bounds (const struct exception_table_entry *ex_start, +const struct exception_table_entry *ex_end, +const void *start,const void *end) +{ +for ( ; ex_start < ex_end; ex_start++ ) + { +const void *addr =_p (ex_addr (ex_start)); +const void *cont =_p (ex_cont (ex_start)); + +if ( addr < start || addr >= end || cont < start || cont >= end ) +return false; + } + +return true; +} +#endif
xen/arch/x86/include/asm/uaccess.h +3 -0
@@ -426,5 +426,8 @@ extern unsigned long search_exception_table(const struct cpu_user_regs *regs,extern void sort_exception_tables (void );extern void sort_exception_table (struct exception_table_entry *start,const struct exception_table_entry *stop); +extern bool extable_in_bounds (const struct exception_table_entry *ex_start, +const struct exception_table_entry *ex_end, +const void *start,const void *end);#endif /* __X86_UACCESS_H__ */
xen/common/livepatch.c +9 -0
@@ -912,6 +912,15 @@ static int prepare_payload(struct payload *payload, s = sec->load_addr; e = sec->load_addr + sec->sec->sh_size; +if ( !extable_in_bounds (s, e, payload->text_addr, + payload->text_addr + payload->text_size) ) + { + printk(XENLOG_ERR LIVEPATCH +"%s: Invalid exception table with out of bounds entries\n" , + elf->name); +return -EINVAL ; + } +sort_exception_table (s ,e); region->ex = s;
Signature · 1 line
2.44.0