Re: [PATCH v2 1/4] x86/signal: Introduce helpers to get the maximum signal frame size

From: Borislav Petkov
Date: Wed Nov 25 2020 - 06:17:37 EST


On Thu, Nov 19, 2020 at 11:02:34AM -0800, Chang S. Bae wrote:
> Signal frames do not have a fixed format and can vary in size when a number
> of things change: support XSAVE features, 32 vs. 64-bit apps. Add the code
> to support a runtime method for userspace to dynamically discover how large
> a signal stack needs to be.
>
> Introduce a new variable, max_frame_size, and helper functions for the
> calculation to be used in a new user interface. Set max_frame_size to a
> system-wide worst-case value, instead of storing multiple app-specific
> values.
>
> Locate the body of the helper function -- fpu__get_fpstate_sigframe_size()
> in fpu/signal.c for its relevance.

This sentence is strange and not needed.

> diff --git a/arch/x86/include/asm/sigframe.h b/arch/x86/include/asm/sigframe.h
> index 84eab2724875..ac77f3f90bc9 100644
> --- a/arch/x86/include/asm/sigframe.h
> +++ b/arch/x86/include/asm/sigframe.h
> @@ -52,6 +52,15 @@ struct rt_sigframe_ia32 {
> char retcode[8];
> /* fp state follows here */
> };
> +
> +#define SIZEOF_sigframe_ia32 sizeof(struct sigframe_ia32)
> +#define SIZEOF_rt_sigframe_ia32 sizeof(struct rt_sigframe_ia32)
> +
> +#else
> +
> +#define SIZEOF_sigframe_ia32 0
> +#define SIZEOF_rt_sigframe_ia32 0
> +
> #endif /* defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION) */
>
> #ifdef CONFIG_X86_64
> @@ -81,8 +90,22 @@ struct rt_sigframe_x32 {
> /* fp state follows here */
> };
>
> +#define SIZEOF_rt_sigframe_x32 sizeof(struct rt_sigframe_x32)
> +
> #endif /* CONFIG_X86_X32_ABI */
>
> +#define SIZEOF_rt_sigframe sizeof(struct rt_sigframe)
> +
> +#else
> +
> +#define SIZEOF_rt_sigframe 0
> +
> #endif /* CONFIG_X86_64 */
>
> +#ifndef SIZEOF_rt_sigframe_x32
> +#define SIZEOF_rt_sigframe_x32 0
> +#endif

Those are defined here to be used in only one place -
init_sigframe_size() - where there already is ifdeffery. Just use the
normal sizeof() operator there instead of adding more gunk here.

> diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
> index a4ec65317a7f..9f009525f551 100644
> --- a/arch/x86/kernel/fpu/signal.c
> +++ b/arch/x86/kernel/fpu/signal.c
> @@ -507,6 +507,26 @@ fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
>
> return sp;
> }
> +
> +unsigned long fpu__get_fpstate_sigframe_size(void)
> +{
> + unsigned long xstate_size = xstate_sigframe_size();
> + unsigned long fsave_header_size = 0;
> +
> + /*
> + * This space is needed on (most) 32-bit kernels, or when a 32-bit
> + * app is running on a 64-bit kernel. To keep things simple, just
> + * assume the worst case and always include space for 'freg_state',
> + * even for 64-bit apps on 64-bit kernels. This wastes a bit of
> + * space, but keeps the code simple.
> + */
> + if ((IS_ENABLED(CONFIG_IA32_EMULATION) ||
> + IS_ENABLED(CONFIG_X86_32)) && use_fxsr())
> + fsave_header_size = sizeof(struct fregs_state);
> +
> + return fsave_header_size + xstate_size;
> +}

I guess this can be simplified to:

unsigned long fpu__get_fpstate_sigframe_size(void)
{
unsigned long ret = xstate_sigframe_size();

/*
* This space is needed on (most) 32-bit kernels, or when a 32-bit
* app is running on a 64-bit kernel. To keep things simple, just
* assume the worst case and always include space for 'freg_state',
* even for 64-bit apps on 64-bit kernels. This wastes a bit of
* space, but keeps the code simple.
*/
if ((IS_ENABLED(CONFIG_IA32_EMULATION) ||
IS_ENABLED(CONFIG_X86_32)) && use_fxsr())
ret += sizeof(struct fregs_state);

return ret;
}

Also, this function simply gives you the xstate size, there's no need
for "sigframe" in the name.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette