[PATCH] kexec: return -ENOEXEC from image probe functions on mismatch

Pratyush Yadav pratyush at kernel.org
Wed Aug 19 02:10:50 PDT 2026


On Wed, Aug 19 2026, Mukesh Pilaniya wrote:

> Hi Pratyush,
>
> On 18/08/26 3:14 pm, Pratyush Yadav wrote:
>> On Fri, Aug 14 2026, Mukesh Pilaniya wrote:
[...]
>> 
>> So how about the below diff instead? (** only compile tested **)
>> 
>> --- 8< ---
>> diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
>> index 2bfbb2d144e6..cfb2b8cd5679 100644
>> --- a/kernel/kexec_file.c
>> +++ b/kernel/kexec_file.c
>> @@ -67,17 +67,16 @@ int kexec_image_probe_default(struct kimage *image, void *buf,
>>  			      unsigned long buf_len)
>>  {
>>  	const struct kexec_file_ops * const *fops;
>> -	int ret = -ENOEXEC;
>>  
>>  	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
>> -		ret = (*fops)->probe(buf, buf_len);
>> -		if (!ret) {
>> +		if (!(*fops)->probe(buf, buf_len)) {
>>  			image->fops = *fops;
>> -			return ret;
>> +			return 0;
>>  		}
>>  	}
>>  
>> -	return ret;
>> +	/* No loader found. */
>> +	return -ENOEXEC;
>>  }
> Nice catch, Pratyush but this discards all non-zero return values from
> probe functions, which means real errors get swallowed. For example,
> kexec_elf_probe() can return -ENOMEM when kzalloc() fails in
> elf_read_phdrs(). With this diff, that -ENOMEM becomes -ENOEXEC, which
> tells userspace "no loader found" when the actual problem was a memory
> allocation failure.

Right, good point.

>
>>  
>>  static void *kexec_image_load_default(struct kimage *image)
>> 
> How about the following instead? It keeps the probe return value and
> distinguishes -ENOEXEC (format not recognized, try next loader) from any
> other error (real failure, propagate immediately):
> 
> diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
> index 59fb9d71e9d8..ceb5c97cedac 100644
> --- a/kernel/kexec_file.c
> +++ b/kernel/kexec_file.c
> @@ -68,17 +68,19 @@ int kexec_image_probe_default(struct kimage *image,
> void *buf,
>  			      unsigned long buf_len)
>  {
>  	const struct kexec_file_ops * const *fops;
> -	int ret = -ENOEXEC;
> +	int ret;
>
>  	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
>  		ret = (*fops)->probe(buf, buf_len);
> -		if (!ret) {
> +		if (ret == 0) {
>  			image->fops = *fops;
> -			return ret;
> +			return 0;
>  		}
> +		if (ret != -ENOEXEC)
> +			return ret;
>  	}
>
> -	return ret;
> +	return -ENOEXEC;
>  }
>
>  static void *kexec_image_load_default(struct kimage *image)
>
>
> This requires probe functions to return -ENOEXEC from fixes in patch 1.
> I will add this as a separate patch on top in v2.
>
> Does this looks good to you ?

LGTM. The only thing I'd change is to perhaps move the declaration of
ret inside the loop so it can never be used outside it, since ret is
only useful in the loop in this function.

-- 
Regards,
Pratyush Yadav



More information about the linux-riscv mailing list