From patchwork Mon Feb 27 22:29:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13154231 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 941BAC7EE30 for ; Mon, 27 Feb 2023 22:31:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B535D6B0071; Mon, 27 Feb 2023 17:31:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B01CE6B0072; Mon, 27 Feb 2023 17:31:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A2826B0074; Mon, 27 Feb 2023 17:31:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8A06B6B0071 for ; Mon, 27 Feb 2023 17:31:30 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 59EC21A0905 for ; Mon, 27 Feb 2023 22:31:30 +0000 (UTC) X-FDA: 80514519540.29.4B9CCA1 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf07.hostedemail.com (Postfix) with ESMTP id D5F3140018 for ; Mon, 27 Feb 2023 22:31:27 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kfIZaLGD; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677537088; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=5HdbklBkPekiXe8PnrG3ZCQoVaTjjLytNWI9dALaCUQ=; b=DOsHfNiEpRnfKbG5FYdLHVkxo6lmT+L+O0WwaLxAnvI16v6NqRUht1KeMMjhRJQuE6LoyO mIpFJBU9hYVx+SwgCxCWohBPL/j8btUtFPdplzVOB6B67CXdvWOTJ+bwoUDaCjWiYtIkRd 9Qf/9BUTv5klKmhLWSxDMPz5nxs8zUg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kfIZaLGD; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677537088; a=rsa-sha256; cv=none; b=iB/mf0TzZr9HZMEyroZx0yjeVIioMFXKAj73cwPoIc0pDR7JUL/MMJxMnN9Gruu4SaITvv cxIViX+npdpx8CYIOicp0SSps4Iy9OFJ1AxuGFBpF6r+ZsUTYpbWk7b46KBtG9k7RjXW4e 54lhq6o1c8zI46P52+9ZeJ1DDZ+wbxc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537087; x=1709073087; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=wgxjh9QgD3JL/mbNIdl6volkZkIB/jWnSXcuFGCrqwg=; b=kfIZaLGD8D6IB+oRPU+dJKqGPfo9C+wN+I3otwasv1OGvMDJq7oiysKB b28w7seqhDOfR5457R+Y1i4P39PlSSWlnBZrpAp+Lcfpo0YFdXN8cNzXw 1SxuFPeeZE7ELvSze+axqc3bXhCt6KAF5LT7X2dvKdSTLi/3pOc2Tdc+Z mBbN2yyChhXtUTgY9YZWiZ+qUiO5qGkotWE/yOh28DTB1S7sYRHL0w1bx REJKZqTlR9T85jspYE5adcrr+PDXHwvG1CC0xMYzA6PwRaPkSSy8NFo42 5ueJSgnxTGQRvQZ9K7NxY3vMNSFOdVXvzMGc7oEvlVRYbkkM8fy47wjRg A==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313656986" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313656986" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:07 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024329" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024329" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:06 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v7 00/41] Shadow stacks for userspace Date: Mon, 27 Feb 2023 14:29:16 -0800 Message-Id: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D5F3140018 X-Stat-Signature: 374jw9kubfsdnjrgqgagwycdaroi66xt X-Rspam-User: X-HE-Tag: 1677537087-821974 X-HE-Meta: U2FsdGVkX1+PnzYUyPBktipTZ7bnTUMlbpnnzIZIAOSnsCNp9YELVy8vZgQt/nsBJ6F9bJ7VNXkU4RnRK0WPSD+is0QFdhADAJREKLc2sig+otpDq+ge1GtEFtoGVwE13og3hzC+956+09HIvO4fgXSnBLGGpBcwfZ+Z3cUsVnq4l2bAdDk+nrXG3Btu5VvufE8pNPZUCroHB8w6JftvAZ7E4JwgpA6VWTFJKN8UqQUlN3n31wNA7wWW7OKKOQ+Suzp3ypagcNchEpDeogTxAwwYvkUmq/Sba3oZGzEeHcwSoUxCzSO+sGcEU3UYSJ3OP5THC+2VjPe/yy70P12ZECMg8hsow4LT6RvICuY2549a2INmllmIehTBuv+RxKA/HKVizgEq3WEtTHf/LQ2bpM/NnFYDRTwEewgdFof/FAr0TsIwN7kxeVCk8xEpSHMpQIhxNoUoSfyZ3tOr/1mpQcha2FIjAbLR8U3utU95ZNUV4V8mmr/sV5gR0aP3wagdH/T7KAAPar0u0ubK5CeQ0qhiJdyrf5k28liBekyq/t/ZNLhsJ61oTd1v8mQ9kK3G02wfcgqzS50EC2DTyma2k/KjTcxaOiJEqi7koUv8G0Non3x2y2bL5ZvKyTJadDf6GQCEgmtwKiUzVXCfZkIoPNX1kVEZNlRH4l32qa35WQToZptSPxXEm25snazE4sAv7Fwl42t7DhQj1UeVrnZl7xyElAzJniCclA0cco4N4M8dzzV7xDe8Dff0j5HgtIhY/ck8LnMf8vg9hf/ne5rWZs8/bk7iBr2dsSOlhqRNRzOVZjdQQFJPs9jKEr18QFYs3g3zTGNZsS23X9O52AqHIGU/hpEYsgFZW3WoBTDwoZYHgRqBUZxwfpfjtnU/Rg+D570re/eM2dPbE9WOZLS/UkE0s0L1heNPEWb46/PN7oawr4IcENyQmDW9ZRolIm8L7F4S45laV9vSYGQYmv5 OPgAeJ8Y i4n5i9V6kKkgvqEJ+uU0kKRskV2FdJRjtMCwZt9kjsjp5aic+AnAcMOwuGX8EO1Uzgc6P0MZ0oHpDBt1hhwEaZU6SMSBwGhOkQaqK1fQ3tRtRXp2+7tg/mtd8PTf9VI6pRpRIA04+osa+ozRxF8vrCiUbb5nJKealgwxtZHLz0A6x0YgjoUelSbYzSrDMHsopwoJbw2iauNqWP6mLxoxORnb0daJo/Ss12pk8p/F4BiQdLi6qAJxPM4JCfsD6JXPApvYF0/J2zDN/UFWgyKopF4Oty/4qY3SmmmeRqBh07II15K7jVzDPZDxoSw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hi, This series implements Shadow Stacks for userspace using x86's Control-flow Enforcement Technology (CET). CET consists of two related security features: shadow stacks and indirect branch tracking. This series implements just the shadow stack part of this feature, and just for userspace. The main use case for shadow stack is providing protection against return oriented programming attacks. It works by maintaining a secondary (shadow) stack using a special memory type that has protections against modification. When executing a CALL instruction, the processor pushes the return address to both the normal stack and to the special permission shadow stack. Upon RET, the processor pops the shadow stack copy and compares it to the normal stack copy. For more details, see the coverletter from v1 [0]. The changes for this version are some more cleanup of comment and commit log verbiage, and small refactor in the memory accounting patch. There was also some feedback from David Hildenbrand about adding GUP tests for the !FOLL_FORCE case. This is currently planned for a fast follow on patch. Previous version [1]. Thanks, Rick [0] https://lore.kernel.org/lkml/20220130211838.8382-1-rick.p.edgecombe@intel.com/ [1] https://lore.kernel.org/lkml/20230218211433.26859-1-rick.p.edgecombe@intel.com/ Kirill A. Shutemov (1): x86: Introduce userspace API for shadow stack Mike Rapoport (1): x86/shstk: Add ARCH_SHSTK_UNLOCK Rick Edgecombe (19): x86/fpu: Add helper for modifying xstate x86: Move control protection handler to separate file mm: Introduce pte_mkwrite_kernel() s390/mm: Introduce pmd_mkwrite_kernel() mm: Make pte_mkwrite() take a VMA x86/mm: Introduce _PAGE_SAVED_DIRTY x86/mm: Start actually marking _PAGE_SAVED_DIRTY x86/mm: Teach pte_mkwrite() about stack memory mm: Don't allow write GUPs to shadow stack memory x86/mm: Introduce MAP_ABOVE4G mm: Warn on shadow stack memory in wrong vma x86/mm: Warn if create Write=0,Dirty=1 with raw prot x86/shstk: Introduce map_shadow_stack syscall x86/shstk: Support WRSS for userspace x86: Expose thread features in /proc/$PID/status x86/shstk: Wire in shadow stack interface selftests/x86: Add shadow stack test x86/fpu: Add helper for initing features x86/shstk: Add ARCH_SHSTK_STATUS Yu-cheng Yu (20): Documentation/x86: Add CET shadow stack description x86/shstk: Add Kconfig option for shadow stack x86/cpufeatures: Add CPU feature flags for shadow stacks x86/cpufeatures: Enable CET CR4 bit for shadow stack x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states x86/shstk: Add user control-protection fault handler x86/mm: Remove _PAGE_DIRTY from kernel RO pages x86/mm: Move pmd_write(), pud_write() up in the file x86/mm: Update ptep/pmdp_set_wrprotect() for _PAGE_SAVED_DIRTY mm: Move VM_UFFD_MINOR_BIT from 37 to 38 mm: Introduce VM_SHADOW_STACK for shadow stack memory x86/mm: Check shadow stack page fault errors mm: Add guard pages around a shadow stack. mm/mmap: Add shadow stack pages to memory accounting mm: Re-introduce vm_flags to do_mmap() x86/shstk: Add user-mode shadow stack support x86/shstk: Handle thread shadow stack x86/shstk: Introduce routines modifying shstk x86/shstk: Handle signals for shadow stack x86: Add PTRACE interface for shadow stack Documentation/filesystems/proc.rst | 1 + Documentation/mm/arch_pgtable_helpers.rst | 9 +- Documentation/x86/index.rst | 1 + Documentation/x86/shstk.rst | 176 +++++ arch/alpha/include/asm/pgtable.h | 6 +- arch/arc/include/asm/hugepage.h | 2 +- arch/arc/include/asm/pgtable-bits-arcv2.h | 7 +- arch/arm/include/asm/pgtable-3level.h | 7 +- arch/arm/include/asm/pgtable.h | 2 +- arch/arm/kernel/signal.c | 2 +- arch/arm64/include/asm/pgtable.h | 9 +- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/signal32.c | 2 +- arch/arm64/mm/trans_pgd.c | 4 +- arch/csky/include/asm/pgtable.h | 2 +- arch/hexagon/include/asm/pgtable.h | 2 +- arch/ia64/include/asm/pgtable.h | 2 +- arch/loongarch/include/asm/pgtable.h | 4 +- arch/m68k/include/asm/mcf_pgtable.h | 2 +- arch/m68k/include/asm/motorola_pgtable.h | 6 +- arch/m68k/include/asm/sun3_pgtable.h | 6 +- arch/microblaze/include/asm/pgtable.h | 2 +- arch/mips/include/asm/pgtable.h | 6 +- arch/nios2/include/asm/pgtable.h | 2 +- arch/openrisc/include/asm/pgtable.h | 2 +- arch/parisc/include/asm/pgtable.h | 6 +- arch/powerpc/include/asm/book3s/32/pgtable.h | 2 +- arch/powerpc/include/asm/book3s/64/pgtable.h | 4 +- arch/powerpc/include/asm/nohash/32/pgtable.h | 2 +- arch/powerpc/include/asm/nohash/32/pte-8xx.h | 2 +- arch/powerpc/include/asm/nohash/64/pgtable.h | 2 +- arch/riscv/include/asm/pgtable.h | 6 +- arch/s390/include/asm/hugetlb.h | 4 +- arch/s390/include/asm/pgtable.h | 14 +- arch/s390/mm/pageattr.c | 4 +- arch/sh/include/asm/pgtable_32.h | 10 +- arch/sparc/include/asm/pgtable_32.h | 2 +- arch/sparc/include/asm/pgtable_64.h | 6 +- arch/sparc/kernel/signal32.c | 2 +- arch/sparc/kernel/signal_64.c | 2 +- arch/um/include/asm/pgtable.h | 2 +- arch/x86/Kconfig | 24 + arch/x86/Kconfig.assembler | 5 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/x86/include/asm/cpufeatures.h | 2 + arch/x86/include/asm/disabled-features.h | 16 +- arch/x86/include/asm/fpu/api.h | 9 + arch/x86/include/asm/fpu/regset.h | 7 +- arch/x86/include/asm/fpu/sched.h | 3 +- arch/x86/include/asm/fpu/types.h | 16 +- arch/x86/include/asm/fpu/xstate.h | 6 +- arch/x86/include/asm/idtentry.h | 2 +- arch/x86/include/asm/mmu_context.h | 2 + arch/x86/include/asm/msr.h | 11 + arch/x86/include/asm/pgtable.h | 322 +++++++- arch/x86/include/asm/pgtable_types.h | 56 +- arch/x86/include/asm/processor.h | 8 + arch/x86/include/asm/shstk.h | 40 + arch/x86/include/asm/special_insns.h | 13 + arch/x86/include/asm/tlbflush.h | 3 +- arch/x86/include/asm/trap_pf.h | 2 + arch/x86/include/asm/traps.h | 12 + arch/x86/include/uapi/asm/mman.h | 4 + arch/x86/include/uapi/asm/prctl.h | 12 + arch/x86/kernel/Makefile | 4 + arch/x86/kernel/cet.c | 152 ++++ arch/x86/kernel/cpu/common.c | 35 +- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/cpu/proc.c | 23 + arch/x86/kernel/fpu/core.c | 59 +- arch/x86/kernel/fpu/regset.c | 86 +++ arch/x86/kernel/fpu/xstate.c | 148 ++-- arch/x86/kernel/fpu/xstate.h | 6 + arch/x86/kernel/idt.c | 2 +- arch/x86/kernel/process.c | 18 +- arch/x86/kernel/process_64.c | 9 +- arch/x86/kernel/ptrace.c | 12 + arch/x86/kernel/shstk.c | 491 +++++++++++++ arch/x86/kernel/signal.c | 1 + arch/x86/kernel/signal_32.c | 2 +- arch/x86/kernel/signal_64.c | 8 +- arch/x86/kernel/sys_x86_64.c | 6 +- arch/x86/kernel/traps.c | 87 --- arch/x86/mm/fault.c | 31 + arch/x86/mm/pat/set_memory.c | 4 +- arch/x86/mm/pgtable.c | 38 + arch/x86/xen/enlighten_pv.c | 2 +- arch/x86/xen/mmu_pv.c | 2 +- arch/x86/xen/xen-asm.S | 2 +- arch/xtensa/include/asm/pgtable.h | 2 +- fs/aio.c | 2 +- fs/proc/array.c | 6 + fs/proc/task_mmu.c | 3 + include/asm-generic/hugetlb.h | 4 +- include/linux/mm.h | 46 +- include/linux/mman.h | 4 + include/linux/pgtable.h | 14 + include/linux/proc_fs.h | 2 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/siginfo.h | 3 +- include/uapi/asm-generic/unistd.h | 2 +- include/uapi/linux/elf.h | 2 + ipc/shm.c | 2 +- kernel/sys_ni.c | 1 + mm/debug_vm_pgtable.c | 16 +- mm/gup.c | 2 +- mm/huge_memory.c | 7 +- mm/hugetlb.c | 4 +- mm/internal.h | 8 +- mm/memory.c | 5 +- mm/migrate_device.c | 2 +- mm/mmap.c | 10 +- mm/mprotect.c | 2 +- mm/nommu.c | 4 +- mm/userfaultfd.c | 2 +- mm/util.c | 2 +- tools/testing/selftests/x86/Makefile | 2 +- .../testing/selftests/x86/test_shadow_stack.c | 695 ++++++++++++++++++ 118 files changed, 2669 insertions(+), 327 deletions(-) create mode 100644 Documentation/x86/shstk.rst create mode 100644 arch/x86/include/asm/shstk.h create mode 100644 arch/x86/kernel/cet.c create mode 100644 arch/x86/kernel/shstk.c create mode 100644 tools/testing/selftests/x86/test_shadow_stack.c