linux-brain/arch/arm64/kernel
Dave Martin a39224199e arm64: fpsimd: Prevent registers leaking from dead tasks
commit 071b6d4a5d upstream.

Currently, loading of a task's fpsimd state into the CPU registers
is skipped if that task's state is already present in the registers
of that CPU.

However, the code relies on the struct fpsimd_state * (and by
extension struct task_struct *) to unambiguously identify a task.

There is a particular case in which this doesn't work reliably:
when a task exits, its task_struct may be recycled to describe a
new task.

Consider the following scenario:

 1) Task P loads its fpsimd state onto cpu C.
        per_cpu(fpsimd_last_state, C) := P;
        P->thread.fpsimd_state.cpu := C;

 2) Task X is scheduled onto C and loads its fpsimd state on C.
        per_cpu(fpsimd_last_state, C) := X;
        X->thread.fpsimd_state.cpu := C;

 3) X exits, causing X's task_struct to be freed.

 4) P forks a new child T, which obtains X's recycled task_struct.
	T == X.
	T->thread.fpsimd_state.cpu == C (inherited from P).

 5) T is scheduled on C.
	T's fpsimd state is not loaded, because
	per_cpu(fpsimd_last_state, C) == T (== X) &&
	T->thread.fpsimd_state.cpu == C.

        (This is the check performed by fpsimd_thread_switch().)

So, T gets X's registers because the last registers loaded onto C
were those of X, in (2).

This patch fixes the problem by ensuring that the sched-in check
fails in (5): fpsimd_flush_task_state(T) is called when T is
forked, so that T->thread.fpsimd_state.cpu == C cannot be true.
This relies on the fact that T is not schedulable until after
copy_thread() completes.

Once T's fpsimd state has been loaded on some CPU C there may still
be other cpus D for which per_cpu(fpsimd_last_state, D) ==
&X->thread.fpsimd_state.  But D is necessarily != C in this case,
and the check in (5) must fail.

An alternative fix would be to do refcounting on task_struct.  This
would result in each CPU holding a reference to the last task whose
fpsimd state was loaded there.  It's not clear whether this is
preferable, and it involves higher overhead than the fix proposed
in this patch.  It would also move all the task_struct freeing
work into the context switch critical section, or otherwise some
deferred cleanup mechanism would need to be introduced, neither of
which seems obviously justified.

Fixes: 005f78cd88 ("arm64: defer reloading a task's FPSIMD state to userland resume")
Signed-off-by: Dave Martin <Dave.Martin@arm.com>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
[will: word-smithed the comment so it makes more sense]
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-12-14 09:28:14 +01:00
..
probes arm64: migrate exception table users off module.h and onto extable.h 2016-09-20 09:36:21 +01:00
vdso arm64/vdso: Fix nsec handling for CLOCK_MONOTONIC_RAW 2017-06-29 13:00:30 +02:00
.gitignore arm64: Build infrastructure 2012-09-17 13:42:21 +01:00
Makefile arm64: Set UTS_MACHINE in the Makefile 2016-08-31 12:31:38 +01:00
acpi.c ARM64: ACPI: enable ACPI_SPCR_TABLE 2016-09-28 17:46:57 +02:00
acpi_numa.c arm64/numa: avoid inconsistent information to be printed 2016-09-09 14:59:08 +01:00
acpi_parking_protocol.c arm64: kernel: acpi: fix ioremap in ACPI parking protocol cpu_postboot 2016-02-26 15:39:52 +00:00
alternative.c arm64: alternative: Add support for patching adrp instructions 2016-09-09 15:03:28 +01:00
arm64ksyms.c arm: kernel: Add SMC structure parameter 2017-04-12 12:41:21 +02:00
armv8_deprecated.c arm64: armv8_deprecated: ensure extension of addr 2017-05-25 15:44:45 +02:00
asm-offsets.c arm: kernel: Add SMC structure parameter 2017-04-12 12:41:21 +02:00
cacheinfo.c arm64: simplify sysreg manipulation 2016-09-09 11:43:50 +01:00
cpu-reset.S arm64: Add back cpu reset routines 2016-06-27 16:31:25 +01:00
cpu-reset.h arm64: Add back cpu reset routines 2016-06-27 16:31:25 +01:00
cpu_errata.c arm64: cpufeature: Schedule enable() calls instead of calling them via IPI 2016-10-20 09:50:53 +01:00
cpu_ops.c arm64: apply __ro_after_init to some objects 2016-08-22 12:32:29 +01:00
cpufeature.c arm64: fix erroneous __raw_read_system_reg() cases 2017-03-12 06:41:48 +01:00
cpuidle.c arm64: add support for ACPI Low Power Idle(LPI) 2016-07-21 23:29:38 +02:00
cpuinfo.c arm64: Rearrange CPU errata workaround checks 2016-09-09 15:03:28 +01:00
debug-monitors.c arm64: simplify sysreg manipulation 2016-09-09 11:43:50 +01:00
efi-entry.S arm64: move early boot code to the .init segment 2016-04-14 18:11:30 +01:00
efi.c arm64: efi: avoid block mappings for unaligned UEFI memory regions 2016-07-01 11:56:26 +01:00
entry-fpsimd.S arm64: fpsimd: fix a typo in fpsimd_save_partial_state ENDPROC 2014-07-31 11:42:42 +01:00
entry-ftrace.S ftrace: Remove CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST from config 2016-08-24 12:15:13 +02:00
entry.S arm64: entry: improve data abort handling of tagged pointers 2017-06-14 15:06:05 +02:00
entry32.S arm64: entry32: remove pointless register assignment 2015-07-10 16:47:13 +01:00
fpsimd.c arm64: fpsimd: Prevent registers leaking across exec 2017-09-02 07:07:54 +02:00
ftrace.c ftrace: Add return address pointer to ftrace_ret_stack 2016-08-24 12:15:14 +02:00
head.S arm64: Make sure SPsel is always set 2017-10-05 09:44:02 +02:00
hibernate-asm.S arm64: Introduce raw_{d,i}cache_line_size 2016-09-09 15:03:29 +01:00
hibernate.c Revert "arm64: hibernate: Refuse to hibernate if the boot cpu is offline" 2016-08-26 11:21:25 +01:00
hw_breakpoint.c arm64: hw_breakpoint: fix watchpoint matching for tagged pointers 2017-06-14 15:06:05 +02:00
hyp-stub.S arm64: Add back cpu reset routines 2016-06-27 16:31:25 +01:00
image.h arm64 updates for 4.7: 2016-05-16 17:17:24 -07:00
insn.c arm64: insn: Add helpers for adrp offsets 2016-09-09 15:03:28 +01:00
io.c arm64: optimize memcpy_{from,to}io() and memset_io() 2014-11-06 17:25:27 +00:00
irq.c arm64: remove irq_count and do_softirq_own_stack() 2015-12-21 17:26:01 +00:00
jump_label.c jump_label: Rename JUMP_LABEL_{EN,DIS}ABLE to JUMP_LABEL_{JMP,NOP} 2015-08-03 11:34:12 +02:00
kaslr.c arm64: kaslr: Fix up the kernel image alignment 2017-03-30 09:41:26 +02:00
kgdb.c arm64: kgdb: handle read-only text / modules 2016-09-23 11:25:01 +01:00
kuser32.S arm64: Add __NR_* definitions for compat syscalls 2014-07-10 11:02:40 +01:00
machine_kexec.c arm64/kexec: Add pr_debug output 2016-06-27 16:31:26 +01:00
module-plts.c arm64: add support for module PLTs 2016-02-24 14:57:24 +00:00
module.c arm64: add support for kernel ASLR 2016-02-24 14:57:27 +00:00
module.lds arm64: add support for module PLTs 2016-02-24 14:57:24 +00:00
paravirt.c arm64: introduce CONFIG_PARAVIRT, PARAVIRT_TIME_ACCOUNTING and pv_time_ops 2015-12-21 14:40:54 +00:00
pci.c arm64: PCI: Add local struct device pointers 2017-04-12 12:41:17 +02:00
perf_callchain.c perf core: Add a 'nr' field to perf_event_callchain_context 2016-05-16 23:11:51 -03:00
perf_event.c KVM: arm64: Fix the issues when guest PMCCFILTR is configured 2016-11-18 09:06:58 +00:00
perf_regs.c perf: Move task_pt_regs sampling into arch code 2015-01-09 11:12:28 +01:00
process.c arm64: fpsimd: Prevent registers leaking from dead tasks 2017-12-14 09:28:14 +01:00
psci.c ARM: 8511/1: ARM64: kernel: PSCI: move PSCI idle management code to drivers/firmware 2016-02-11 15:33:38 +00:00
ptrace.c arm64/ptrace: Reject attempts to set incomplete hardware breakpoint fields 2017-01-26 08:24:41 +01:00
relocate_kernel.S arm64: Introduce raw_{d,i}cache_line_size 2016-09-09 15:03:29 +01:00
return_address.c arm64: ftrace: fix a stack tracer's output under function graph tracer 2015-12-21 17:26:02 +00:00
setup.c arm64: Set UTS_MACHINE in the Makefile 2016-08-31 12:31:38 +01:00
signal.c arm64: factor work_pending state machine to C 2016-08-22 10:00:48 +01:00
signal32.c arm64: Fix misspellings in comments. 2016-03-04 18:19:17 +00:00
sleep.S kprobes: Unpoison stack in jprobe_return() for KASAN 2016-10-16 11:02:31 +02:00
smccc-call.S firmware: qcom: scm: Fix interrupted SCM calls 2017-04-12 12:41:21 +02:00
smp.c arm64: fix NULL dereference in have_cpu_die() 2017-07-05 14:40:31 +02:00
smp_spin_table.c arm64: vmlinux.ld: Add mmuoff data sections and move mmuoff text into idmap 2016-08-25 18:00:30 +01:00
stacktrace.c arm64: fix dump_backtrace/unwind_frame with NULL tsk 2016-09-26 14:24:01 +01:00
suspend.c arm64: suspend: Reconfigure PSTATE after resume from idle 2016-10-20 09:50:54 +01:00
sys.c arm64: compat: Check for AArch32 state 2016-04-20 12:22:42 +01:00
sys32.c arm64: fix implementation of mmap2 compat syscall 2015-03-19 10:43:51 +00:00
sys_compat.c arm64: simplify sysreg manipulation 2016-09-09 11:43:50 +01:00
time.c arm64: ftrace: fix a stack tracer's output under function graph tracer 2015-12-21 17:26:02 +00:00
topology.c arm64: kernel: remove non-legit DT warnings when booting using ACPI 2015-07-27 11:08:41 +01:00
trace-events-emulation.h arm64: Trace emulation of AArch32 legacy instructions 2014-11-20 16:35:02 +00:00
traps.c arm64: ensure __dump_instr() checks addr_limit 2017-11-08 10:08:32 +01:00
vdso.c arm64/vdso: Fix nsec handling for CLOCK_MONOTONIC_RAW 2017-06-29 13:00:30 +02:00
vmlinux.lds.S nmi_backtrace: generate one-line reports for idle cpus 2016-10-07 18:46:30 -07:00