Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/mm: Signal SIGSEGV with PF_SGX

The x86 architecture has a set of page fault error codes. These indicate
things like whether the fault occurred from a write, or whether it
originated in userspace.

The SGX hardware architecture has its own per-page memory management
metadata (EPCM) [*] and hardware which is separate from the normal x86 MMU.
The architecture has a new page fault error code: PF_SGX. This new error
code bit is set whenever a page fault occurs as the result of the SGX MMU.

These faults occur for a variety of reasons. For instance, an access
attempt to enclave memory from outside the enclave causes a PF_SGX fault.
PF_SGX would also be set for permission conflicts, such as if a write to an
enclave page occurs and the page is marked read-write in the x86 page
tables but is read-only in the EPCM.

These faults do not always indicate errors, though. SGX pages are
encrypted with a key that is destroyed at hardware reset, including
suspend. Throwing a SIGSEGV allows user space software to react and recover
when these events occur.

Include PF_SGX in the PF error codes list and throw SIGSEGV when it is
encountered.

[*] Intel SDM: 36.5.1 Enclave Page Cache Map (EPCM)

[ bp: Add bit 15 to the comment above enum x86_pf_error_code too. ]

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Jethro Beekman <jethro@fortanix.com>
Link: https://lkml.kernel.org/r/20201112220135.165028-7-jarkko@kernel.org

authored by

Sean Christopherson and committed by
Borislav Petkov
74faeee0 e7e05452

+14
+2
arch/x86/include/asm/trap_pf.h
··· 11 11 * bit 3 == 1: use of reserved bit detected 12 12 * bit 4 == 1: fault was an instruction fetch 13 13 * bit 5 == 1: protection keys block access 14 + * bit 15 == 1: SGX MMU page-fault 14 15 */ 15 16 enum x86_pf_error_code { 16 17 X86_PF_PROT = 1 << 0, ··· 20 19 X86_PF_RSVD = 1 << 3, 21 20 X86_PF_INSTR = 1 << 4, 22 21 X86_PF_PK = 1 << 5, 22 + X86_PF_SGX = 1 << 15, 23 23 }; 24 24 25 25 #endif /* _ASM_X86_TRAP_PF_H */
+12
arch/x86/mm/fault.c
··· 1102 1102 return 1; 1103 1103 1104 1104 /* 1105 + * SGX hardware blocked the access. This usually happens 1106 + * when the enclave memory contents have been destroyed, like 1107 + * after a suspend/resume cycle. In any case, the kernel can't 1108 + * fix the cause of the fault. Handle the fault as an access 1109 + * error even in cases where no actual access violation 1110 + * occurred. This allows userspace to rebuild the enclave in 1111 + * response to the signal. 1112 + */ 1113 + if (unlikely(error_code & X86_PF_SGX)) 1114 + return 1; 1115 + 1116 + /* 1105 1117 * Make sure to check the VMA so that we do not perform 1106 1118 * faults just to hit a X86_PF_PK as soon as we fill in a 1107 1119 * page.