On 4/25/2021 11:52 PM, Kirill A. Shutemov wrote:
On Thu, Apr 15, 2021 at 03:14:19PM -0700, Yu-cheng Yu wrote:
There are three possible options to create a shadow stack allocation API:
an arch_prctl, a new syscall, or adding PROT_SHSTK to mmap()/mprotect().
Each has its advantages and compromises.
An arch_prctl() is the least intrusive. However, the existing x86
arch_prctl() takes only two parameters. Multiple parameters must be
passed in a memory buffer. There is a proposal to pass more parameters in
registers [1], but no active discussion on that.
A new syscall minimizes compatibility issues and offers an extensible frame
work to other architectures, but this will likely result in some overlap of
mmap()/mprotect().
The introduction of PROT_SHSTK to mmap()/mprotect() takes advantage of
Maybe PROT_SHADOW_STACK?
existing APIs. The x86-specific PROT_SHSTK is translated to
VM_SHADOW_STACK and a shadow stack mapping is created without reinventing
the wheel. There are potential pitfalls though. The most obvious one
would be using this as a bypass to shadow stack protection. However, the
attacker would have to get to the syscall first.
[1] https://lore.kernel.org/lkml/20200828121624.108243-1-hjl.tools@xxxxxxxxx/
Signed-off-by: Yu-cheng Yu <yu-cheng.yu@xxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
---
v24:
- Update arch_calc_vm_prot_bits(), leave PROT* checking to
arch_validate_prot().
- Update arch_validate_prot(), leave vma flags checking to
arch_validate_flags().
- Add arch_validate_flags().
arch/x86/include/asm/mman.h | 59 +++++++++++++++++++++++++++++++-
arch/x86/include/uapi/asm/mman.h | 1 +
include/linux/mm.h | 1 +
3 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/mman.h b/arch/x86/include/asm/mman.h
index 629f6c81263a..1821c179f35d 100644
--- a/arch/x86/include/asm/mman.h
+++ b/arch/x86/include/asm/mman.h
[...]
+
+#define arch_validate_prot arch_validate_prot
+
+static inline bool arch_validate_flags(unsigned long vm_flags, bool is_anon)
+{
+ if (vm_flags & VM_SHADOW_STACK) {
+ if ((vm_flags & VM_SHARED) || !is_anon)
VM_SHARED check is redundant. vma_is_anonymous() should be enough.
Anonymous shared mappings would fail vma_is_anonymous().
Thanks for looking into this. I will update and send another version.
Yu-cheng