Message ID | 20230227222957.24501-20-rick.p.edgecombe@intel.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | Shadow stacks for userspace | expand |
On Mon, Feb 27, 2023 at 02:29:35PM -0800, Rick Edgecombe wrote: > @@ -1310,6 +1324,23 @@ void do_user_addr_fault(struct pt_regs *regs, > > perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); > > + /* > + * For conventionally writable pages, a read can be serviced with a > + * read only PTE. But for shadow stack, there isn't a concept of > + * read-only shadow stack memory. If it a PTE has the shadow stack s/it // > + * permission, it can be modified via CALL and RET instructions. So > + * core MM needs to fault in a writable PTE and do things it already > + * does for write faults. > + * > + * Shadow stack accesses (read or write) need to be serviced with > + * shadow stack permission memory, which always include write > + * permissions. So in the case of a shadow stack read access, treat it > + * as a WRITE fault. This will make sure that MM will prepare > + * everything (e.g., break COW) such that maybe_mkwrite() can create a > + * proper shadow stack PTE. > + */ > + if (error_code & X86_PF_SHSTK) > + flags |= FAULT_FLAG_WRITE; > if (error_code & X86_PF_WRITE) > flags |= FAULT_FLAG_WRITE; > if (error_code & X86_PF_INSTR) > -- > 2.17.1 >
On 3/3/23 06:00, Borislav Petkov wrote: > On Mon, Feb 27, 2023 at 02:29:35PM -0800, Rick Edgecombe wrote: >> @@ -1310,6 +1324,23 @@ void do_user_addr_fault(struct pt_regs *regs, >> >> perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); >> >> + /* >> + * For conventionally writable pages, a read can be serviced with a >> + * read only PTE. But for shadow stack, there isn't a concept of >> + * read-only shadow stack memory. If it a PTE has the shadow stack > s/it // > >> + * permission, it can be modified via CALL and RET instructions. So >> + * core MM needs to fault in a writable PTE and do things it already >> + * does for write faults. >> + * >> + * Shadow stack accesses (read or write) need to be serviced with >> + * shadow stack permission memory, which always include write >> + * permissions. So in the case of a shadow stack read access, treat it >> + * as a WRITE fault. This will make sure that MM will prepare >> + * everything (e.g., break COW) such that maybe_mkwrite() can create a >> + * proper shadow stack PTE. I ended up just chopping that top paragraph out and rewording it a bit. I think this still expresses the intent in a lot less space: /* * Read-only permissions can not be expressed in shadow stack PTEs. * Treat all shadow stack accesses as WRITE faults. This ensures * that the MM will prepare everything (e.g., break COW) such that * maybe_mkwrite() can create a proper shadow stack PTE. */
diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..afa524325e55 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -11,6 +11,7 @@ * bit 3 == 1: use of reserved bit detected * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access + * bit 6 == 1: shadow stack access fault * bit 15 == 1: SGX MMU page-fault */ enum x86_pf_error_code { @@ -20,6 +21,7 @@ enum x86_pf_error_code { X86_PF_RSVD = 1 << 3, X86_PF_INSTR = 1 << 4, X86_PF_PK = 1 << 5, + X86_PF_SHSTK = 1 << 6, X86_PF_SGX = 1 << 15, }; diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index a498ae1fbe66..776b92339cfe 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1117,8 +1117,22 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) (error_code & X86_PF_INSTR), foreign)) return 1; + /* + * Shadow stack accesses (PF_SHSTK=1) are only permitted to + * shadow stack VMAs. All other accesses result in an error. + */ + if (error_code & X86_PF_SHSTK) { + if (unlikely(!(vma->vm_flags & VM_SHADOW_STACK))) + return 1; + if (unlikely(!(vma->vm_flags & VM_WRITE))) + return 1; + return 0; + } + if (error_code & X86_PF_WRITE) { /* write, present and write, not present: */ + if (unlikely(vma->vm_flags & VM_SHADOW_STACK)) + return 1; if (unlikely(!(vma->vm_flags & VM_WRITE))) return 1; return 0; @@ -1310,6 +1324,23 @@ void do_user_addr_fault(struct pt_regs *regs, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); + /* + * For conventionally writable pages, a read can be serviced with a + * read only PTE. But for shadow stack, there isn't a concept of + * read-only shadow stack memory. If it a PTE has the shadow stack + * permission, it can be modified via CALL and RET instructions. So + * core MM needs to fault in a writable PTE and do things it already + * does for write faults. + * + * Shadow stack accesses (read or write) need to be serviced with + * shadow stack permission memory, which always include write + * permissions. So in the case of a shadow stack read access, treat it + * as a WRITE fault. This will make sure that MM will prepare + * everything (e.g., break COW) such that maybe_mkwrite() can create a + * proper shadow stack PTE. + */ + if (error_code & X86_PF_SHSTK) + flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_WRITE) flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_INSTR)