linux-kernelorg-stable/arch/x86/kernel/cpu
Jarkko Sakkinen 2ade0d6093 x86/sgx: Maintain encl->refcount for each encl->mm_list entry
This has been shown in tests:

[  +0.000008] WARNING: CPU: 3 PID: 7620 at kernel/rcu/srcutree.c:374 cleanup_srcu_struct+0xed/0x100

This is essentially a use-after free, although SRCU notices it as
an SRCU cleanup in an invalid context.

== Background ==

SGX has a data structure (struct sgx_encl_mm) which keeps per-mm SGX
metadata.  This is separate from struct sgx_encl because, in theory,
an enclave can be mapped from more than one mm.  sgx_encl_mm includes
a pointer back to the sgx_encl.

This means that sgx_encl must have a longer lifetime than all of the
sgx_encl_mm's that point to it.  That's usually the case: sgx_encl_mm
is freed only after the mmu_notifier is unregistered in sgx_release().

However, there's a race.  If the process is exiting,
sgx_mmu_notifier_release() can be called in parallel with sgx_release()
instead of being called *by* it.  The mmu_notifier path keeps encl_mm
alive past when sgx_encl can be freed.  This inverts the lifetime rules
and means that sgx_mmu_notifier_release() can access a freed sgx_encl.

== Fix ==

Increase encl->refcount when encl_mm->encl is established. Release
this reference when encl_mm is freed. This ensures that encl outlives
encl_mm.

 [ bp: Massage commit message. ]

Fixes: 1728ab54b4 ("x86/sgx: Add a page reclaimer")
Reported-by: Haitao Huang <haitao.huang@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Link: https://lkml.kernel.org/r/20210207221401.29933-1-jarkko@kernel.org
2021-02-08 19:11:30 +01:00
..
mce x86/mce: Remove explicit/superfluous tracing 2021-01-12 21:10:59 +01:00
microcode - A single cleanup removing "break" after a return statement (Tom Rix) 2020-12-14 13:13:15 -08:00
mtrr x86/mtrr: Correct the range check before performing MTRR type lookups 2021-01-06 13:01:13 +01:00
resctrl x86/resctrl: Don't move a task to the same resource group 2021-01-08 09:08:03 +01:00
sgx x86/sgx: Maintain encl->refcount for each encl->mm_list entry 2021-02-08 19:11:30 +01:00
.gitignore
Makefile
acrn.c
amd.c x86/cpu/amd: Set __max_die_per_package on AMD 2021-01-12 12:21:01 +01:00
aperfmperf.c
bugs.c x86/speculation: Fix prctl() when spectre_v2_user={seccomp,prctl},ibpb 2020-11-25 20:17:09 +01:00
cacheinfo.c x86/CPU/AMD: Remove amd_get_nb_id() 2020-11-19 11:43:17 +01:00
centaur.c
common.c
cpu.h
cpuid-deps.c x86: Enumerate AVX512 FP16 CPUID feature flag 2020-12-11 19:00:58 -05:00
cyrix.c
feat_ctl.c
hygon.c x86/cpu/amd: Remove dead code for TSEG region remapping 2020-12-08 18:45:21 +01:00
hypervisor.c
intel.c x86/split_lock: Enable the split lock feature on another Alder Lake CPU 2021-02-01 21:34:51 +01:00
intel_epb.c
intel_pconfig.c
match.c
mkcapflags.sh
mshyperv.c x86/hyperv: Fix kexec panic/hang issues 2021-01-05 17:52:04 +00:00
perfctr-watchdog.c
powerflags.c
proc.c
rdrand.c
scattered.c x86/cpu: Add VM page flush MSR availablility as a CPUID feature 2020-12-14 11:09:30 -05:00
topology.c x86/topology: Make __max_die_per_package available unconditionally 2021-01-14 12:18:36 +01:00
transmeta.c
tsx.c
umc.c
umwait.c
vmware.c KVM: SVM: Add GHCB accessor functions for retrieving fields 2020-12-14 11:09:32 -05:00
zhaoxin.c