Subject: Re: [PATCH] powerpc/rtas: Prevent Spectre v1 gadget construction in sys_rtas()

From: Breno Leitao <[email-masked]>
Date: Fri, 31 May 2024 06:45:13 -0700

On Thu, May 30, 2024 at 07:44:12PM -0500, Nathan Lynch via B4 Relay wrote:
> From: Nathan Lynch <[email-masked]>
> 
> Smatch warns:
> 
>   arch/powerpc/kernel/rtas.c:1932 __do_sys_rtas() warn: potential
>   spectre issue 'args.args' [r] (local cap)
> 
> The 'nargs' and 'nret' locals come directly from a user-supplied
> buffer and are used as indexes into a small stack-based array and as
> inputs to copy_to_user() after they are subject to bounds checks.
> 
> Use array_index_nospec() after the bounds checks to clamp these values
> for speculative execution.
> 
> Signed-off-by: Nathan Lynch <[email-masked]>
> Reported-by: Breno Leitao <[email-masked]>

Thanks for working on it. 

Reviewed-by: Breno Leitao <[email-masked]>

> +	nargs = array_index_nospec(nargs, ARRAY_SIZE(args.args));
> +	nret = array_index_nospec(nret, ARRAY_SIZE(args.args) - nargs);

On an unrelated note, can nargs and nret are integers and could be
eventually negative. Is this a valid use case?

Thanks!