kexec: return -ENOEXEC from image probe functions on mismatch

Several kexec_file_load() image probe functions return -EINVAL when
they do not recognize the image format.  A probe function that rejects
an image should return -ENOEXEC to indicate that the image is not a
recognized executable format.  -EINVAL implies a problem with the
syscall parameters, not with image recognition.

kexec_image_probe_default() iterates through registered loaders and
returns the last probe's error code to the caller.  That error
propagates as the kexec_file_load() return value to userspace.
Returning -EINVAL from a probe when no loader matches is semantically
incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the
image format is not recognized.

Signed-off-by: Mukesh Pilaniya <mpilaniy@redhat.com>
Reviewed-by: Philipp Rudo <prudo@redhat.com>
Reviewed-by: Pratyush Yadav <pratyush@kernel.org>
Reviewed-by: Bradley Morgan <include@grrlz.net>
Link: https://patch.msgid.link/20260821-mpilaniy-v3-1-76b355399d2e@redhat.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
This commit is contained in:
Mukesh Pilaniya 2026-08-21 19:19:57 +05:30 committed by Mike Rapoport (Microsoft)
parent 818bebeb63
commit 2792b4b15f
4 changed files with 8 additions and 8 deletions

View File

@ -25,10 +25,10 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
(const struct arm64_image_header *)(kernel_buf);
if (!h || (kernel_len < sizeof(*h)))
return -EINVAL;
return -ENOEXEC;
if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
return -EINVAL;
return -ENOEXEC;
return 0;
}

View File

@ -24,12 +24,12 @@ static int efi_kexec_probe(const char *kernel_buf, unsigned long kernel_len)
if (!h || (kernel_len < sizeof(*h))) {
kexec_dprintk("No LoongArch image header.\n");
return -EINVAL;
return -ENOEXEC;
}
if (!loongarch_header_check_dos_sig(h)) {
kexec_dprintk("No LoongArch PE image header.\n");
return -EINVAL;
return -ENOEXEC;
}
return 0;

View File

@ -20,7 +20,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
const struct riscv_image_header *h = (const struct riscv_image_header *)kernel_buf;
if (!h || kernel_len < sizeof(*h))
return -EINVAL;
return -ENOEXEC;
/* According to Documentation/arch/riscv/boot-image-header.rst,
* use "magic2" field to check when version >= 0.2.
@ -28,7 +28,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
if (h->version >= RISCV_HEADER_VERSION &&
memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2)))
return -EINVAL;
return -ENOEXEC;
return 0;
}

View File

@ -172,7 +172,7 @@ static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
default:
pr_debug("Unknown ELF class.\n");
return -EINVAL;
return -ENOEXEC;
}
return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
@ -236,7 +236,7 @@ static int elf_read_phdr(const char *buf, size_t len,
default:
pr_debug("Unknown ELF class.\n");
return -EINVAL;
return -ENOEXEC;
}
return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;