]> git.itanic.dy.fi Git - linux-stable/commit
mips: Call lose_fpu(0) before initializing fcr31 in mips_set_personality_nan
authorXi Ruoyao <xry111@xry111.site>
Fri, 26 Jan 2024 21:05:57 +0000 (05:05 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 1 Feb 2024 00:17:12 +0000 (16:17 -0800)
commite333bbb557dc3af79ee3f9f710ef03097bb91435
tree9fe1c790b6bda63b8b431b814f5bae83eca2b4a4
parentec745eeff4d2824e983f6f00bbff2a8a8003972b
mips: Call lose_fpu(0) before initializing fcr31 in mips_set_personality_nan

commit 59be5c35850171e307ca5d3d703ee9ff4096b948 upstream.

If we still own the FPU after initializing fcr31, when we are preempted
the dirty value in the FPU will be read out and stored into fcr31,
clobbering our setting.  This can cause an improper floating-point
environment after execve().  For example:

    zsh% cat measure.c
    #include <fenv.h>
    int main() { return fetestexcept(FE_INEXACT); }
    zsh% cc measure.c -o measure -lm
    zsh% echo $((1.0/3)) # raising FE_INEXACT
    0.33333333333333331
    zsh% while ./measure; do ; done
    (stopped in seconds)

Call lose_fpu(0) before setting fcr31 to prevent this.

Closes: https://lore.kernel.org/linux-mips/7a6aa1bbdbbe2e63ae96ff163fab0349f58f1b9e.camel@xry111.site/
Fixes: 9b26616c8d9d ("MIPS: Respect the ISA level in FCSR handling")
Cc: stable@vger.kernel.org
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/mips/kernel/elf.c