kexec: fix kexec_file_load fallback error handling
Subsystems (2)
PATCH 1/1 kexec: fix kexec_file_load fallback error handling
With the default -a (--kexec-syscall-auto) option, kexec-tools first tries kexec_file_load() and falls back to kexec_load() when the syscall is not implemented (ENOSYS) or the kernel does not have a loader for the image format (ENOEXEC/ENOTSUP). With -s (--kexec-file-syscall), kexec-tools uses only kexec_file_load() and does not fall back. Falling back on EINVAL hides the real error, because EINVAL means something went wrong during loading.
Remove EINVAL from the fallback path. Print the actual errno for ENOEXEC/ENOTSUP before falling back and track it so the final message distinguishes a missing syscall from other failures.
Several kexec_file_load() image probe functions incorrectly return -EINVAL rather than -ENOEXEC when encountering an unrecognized image format. This kernel patch fixes the issue:
https://lore.kernel.org/all/[email protected]/
---
kernel patch:
https://lore.kernel.org/all/[email protected]/
--- kexec/kexec.c | 63 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 18 deletions(-)
kexec/kexec.c +45 -18
@@ -68,6 +68,8 @@ int do_hotplug = 0;static unsigned long kexec_flags =0 ;/* Flags for kexec file (fd) based syscall */ static unsigned long kexec_file_flags =0 ; +/* errno from kexec_file_load when it returned EFALLBACK */ +static int kexec_file_load_errno;/* initrd detected in probe phase */ int implicit_initrd_fd =-1 ;int kexec_debug =0 ;
@@ -869,12 +871,16 @@ static int my_load(const char *type, int fileind, int argc, char **argv, info.nr_segments, info.segment, info.kexec_flags);if (result !=0 ) { -/* The load failed, print some debugging information */ -fprintf (stderr,"kexec_load failed: %s\n" , -strerror (errno)); -fprintf (stderr,"entry = %p flags = 0x%lx\n" , - info.entry, info.kexec_flags); -print_segments (stderr, &info); +if (errno ==ENOSYS ) { +fprintf (stderr, +"syscall kexec_load is not available on this system.\n" ); + }else { +fprintf (stderr,"kexec_load failed: %s\n" , +strerror (errno)); +fprintf (stderr,"entry = %p flags = 0x%lx\n" , + info.entry, info.kexec_flags); +print_segments (stderr, &info); + } }return result; }
@@ -883,12 +889,15 @@ static int kexec_file_unload(unsigned long kexec_file_flags) {int ret =0 ; -if (!is_kexec_file_load_implemented ()) +if (!is_kexec_file_load_implemented ()) { + kexec_file_load_errno =ENOSYS ;return EFALLBACK ; + } ret =kexec_file_load (-1 ,-1 ,0 ,NULL , kexec_file_flags);if (ret !=0 ) {if (errno ==ENOSYS ) { + kexec_file_load_errno =ENOSYS ; ret =EFALLBACK ; }else {/*
@@ -919,9 +928,12 @@ static int k_unload (unsigned long kexec_flags)else result =kexec_load (NULL ,0 ,NULL , kexec_flags);if (result !=0 ) { -/* The unload failed, print some debugging information */ -fprintf (stderr,"kexec unload failed: %s\n" , -strerror (errno)); +if (errno ==ENOSYS ) +fprintf (stderr, +"syscall kexec_load is not available on this system.\n" ); +else +fprintf (stderr,"kexec unload failed: %s\n" , +strerror (errno)); }return result; }
@@ -1336,8 +1348,10 @@ static int do_kexec_file_load(int fileind, int argc, char **argv, info.kernel_fd =-1 ; info.initrd_fd =-1 ; -if (!is_kexec_file_load_implemented ()) +if (!is_kexec_file_load_implemented ()) { + kexec_file_load_errno =ENOSYS ;return EFALLBACK ; + }if (argc - fileind <=0 ) {fprintf (stderr,"No kernel specified\n" );
@@ -1441,13 +1455,14 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,/* Not implemented. */ case ENOSYS : + kexec_file_load_errno =ENOSYS ; + ret =EFALLBACK ; +break ; +/* -* Parsing image or other options failed -* The image may be invalid or image -* type may not supported by kernel so -* retry parsing in kexec-tools. +* The kernel does not have a loader for this +* image format, retry parsing in kexec-tools. */ -case EINVAL :case ENOEXEC :/* * ENOTSUP can be unsupported image
@@ -1455,6 +1470,9 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,* wrapper type, duh. */ case ENOTSUP : + kexec_file_load_errno = errno; +fprintf (stderr,"kexec_file_load failed: %s\n" , +strerror (errno)); ret =EFALLBACK ;break ; }
@@ -1823,8 +1841,17 @@ int main(int argc, char *argv[])if ((result ==0 ) && do_load_jump_back_helper) { result =my_load_jump_back_helper (kexec_flags, entry); } -if (result ==EFALLBACK ) -fputs ("syscall kexec_file_load not available.\n" , stderr); +/* +* When fallback to kexec_load was attempted, my_load() +* already reported the kexec_load error and overwrote +* result with its own return value, so this condition +* is only true when no fallback was attempted and +* kexec_file_load does not exist. +*/ +if (result ==EFALLBACK && kexec_file_load_errno ==ENOSYS ) { +fputs ("syscall kexec_file_load is not available on this system.\n" , + stderr); + }fflush (stdout);fflush (stderr);
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 7 lines · Mukesh Pilaniya
With the default -a (--kexec-syscall-auto) option, kexec-tools first tries
kexec_file_load()and falls back tokexec_load()when the syscall is not implemented (ENOSYS) or the kernel does not have a loader for the image format (ENOEXEC/ENOTSUP). [...] Falling back on EINVAL hides the real error, because EINVAL means something went wrong during loading.Remove EINVAL from the fallback path.
Hiding the real error behind a fallback is exactly what makes this one hurt to debug, so good direction.
Quoted 4 lines
Several
kexec_file_load()image probe functions incorrectly return-EINVALrather than-ENOEXECwhen encountering an unrecognized image format. This kernel patch fixes the issue:
https://lore.kernel.org/all/[email protected]/
That one is still being reworked, Pratyush just LGTM'd the probe_default change a couple days ago and the fix isn't in any tree yet. And until it is, the arm64 probe and the riscv image probe still return -EINVAL, and that last errno is what kexec_image_probe_default() leaks to userspace (I checked the loader lists in the tree). So if this lands on tools before the kernel fix lands, an image that used to silently fall back to kexec_load() on those arches now just fails with EINVAL.
Is the plan to hold this one until the kernel side is in? Because right now it looks like it can race ahead of its dependency.
Quoted 1 line
Print the actual errno for ENOEXEC/ENOTSUP before falling back
Good, that is the part that makes the s390 command line case visible.
The my_load/k_unload ENOSYS message hunks are unrelated cosmetics, fine but could be their own patch if you feel like splitting.
Its a good patch, I just want the ordering question answered first.
Thanks!
Hi Bradley, thanks for review.
Quoted 27 lines · Bradley Morgan
Quoted 7 lines · Mukesh Pilaniya
With the default -a (--kexec-syscall-auto) option, kexec-tools first tries
kexec_file_load()and falls back tokexec_load()when the syscall is not implemented (ENOSYS) or the kernel does not have a loader for the image format (ENOEXEC/ENOTSUP). [...] Falling back on EINVAL hides the real error, because EINVAL means something went wrong during loading.Remove EINVAL from the fallback path.
Hiding the real error behind a fallback is exactly what makes this one hurt to debug, so good direction.
Quoted 4 lines
Several
kexec_file_load()image probe functions incorrectly return-EINVALrather than-ENOEXECwhen encountering an unrecognized image format. This kernel patch fixes the issue:
https://lore.kernel.org/all/[email protected]/That one is still being reworked, Pratyush just LGTM'd the
probe_defaultchange a couple days ago and the fix isn't in any tree yet. And until it is, the arm64 probe and the riscv image probe still return-EINVAL, and that last errno is whatkexec_image_probe_default()leaks to userspace (I checked the loader lists in the tree). So if this lands on tools before the kernel fix lands, an image that used to silently fall back tokexec_load()on those arches now just fails with EINVAL.
Quoted 3 lines
Is the plan to hold this one until the kernel side is in? Because right now it looks like it can race ahead of its dependency.
Yes, the kernel patch will merge first, then this one. I will follow up on the same thread once the kernel fix is merged.
Quoted 7 lines
Quoted 1 line
Print the actual errno for ENOEXEC/ENOTSUP before falling back
Good, that is the part that makes the s390 command line case visible.
The my_load/k_unload ENOSYS message hunks are unrelated cosmetics, fine but could be their own patch if you feel like splitting.
The my_load/k_unload ENOSYS hunks are not just cosmetic, they address a misleading error output. Today, when kexec_load is absent, my_load dumps the entry pointer, flags, and all segments, which is debug noise for what is simply an unsupported syscall. Similar, k_unload prints "kexec unload failed: Function not implemented", which reads like something went wrong rather than the syscall not being available.
I'm happy to split this if you'd prefer, but since they're small and directly related to the error-path cleanup, I kept them together.
Quoted 3 lines
Its a good patch, I just want the ordering question answered first.
Thanks!
Signature · 2 lines
Regards, Mukesh Pilaniya