Subject: Re: powerpc/ptrace: Simplify vr_get/set() to avoid GCC warning

From: Michael Ellerman <[email-masked]>
Date: Fri, 22 Feb 2019 20:47:59 +1100 (AEDT)

On Fri, 2019-02-15 at 06:14:00 UTC, Michael Ellerman wrote:
> GCC 8 warns about the logic in vr_get/set(), which with -Werror breaks
> the build:
> 
>   In function âuser_regset_copyinâ,
>       inlined from âvr_setâ at arch/powerpc/kernel/ptrace.c:628:9:
>   include/linux/regset.h:295:4: error: âmemcpyâ offset [-527, -529] is
>   out of the bounds [0, 16] of object âvrsaveâ with type âunion
>   <anonymous>â [-Werror=array-bounds]
>   arch/powerpc/kernel/ptrace.c: In function âvr_setâ:
>   arch/powerpc/kernel/ptrace.c:623:5: note: âvrsaveâ declared here
>      } vrsave;
> 
> This has been identified as a regression in GCC, see GCC bug 88273.
> 
> However we can avoid the warning and also simplify the logic and make
> it more robust.
> 
> Currently we pass -1 as end_pos to user_regset_copyout(). This says
> "copy up to the end of the regset".
> 
> The definition of the regset is:
> 	[REGSET_VMX] = {
> 		.core_note_type = NT_PPC_VMX, .n = 34,
> 		.size = sizeof(vector128), .align = sizeof(vector128),
> 		.active = vr_active, .get = vr_get, .set = vr_set
> 	},
> 
> The end is calculated as (n * size), ie. 34 * sizeof(vector128).
> 
> In vr_get/set() we pass start_pos as 33 * sizeof(vector128), meaning
> we can copy up to sizeof(vector128) into/out-of vrsave.
> 
> The on-stack vrsave is defined as:
>   union {
> 	  elf_vrreg_t reg;
> 	  u32 word;
>   } vrsave;
> 
> And elf_vrreg_t is:
>   typedef __vector128 elf_vrreg_t;
> 
> So there is no bug, but we rely on all those sizes lining up,
> otherwise we would have a kernel stack exposure/overwrite on our
> hands.
> 
> Rather than relying on that we can pass an explict end_pos based on
> the sizeof(vrsave). The result should be exactly the same but it's
> more obviously not over-reading/writing the stack and it avoids the
> compiler warning.
> 
> Reported-by: Meelis Roos <[email-masked]>
> Reported-by: Mathieu Malaterre <[email-masked]>
> Cc: [email-masked]
> Signed-off-by: Michael Ellerman <[email-masked]>
> Tested-by: Mathieu Malaterre <[email-masked]>

Applied to powerpc next.

https://git.kernel.org/powerpc/c/ca6d5149d2ad0a8d2f9c28cbe3798022

cheers