From patchwork Thu Jan 19 21:22:38 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: 13108776 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 C022EC004D4 for ; Thu, 19 Jan 2023 21:23:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C8F66B0071; Thu, 19 Jan 2023 16:23:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 352376B0073; Thu, 19 Jan 2023 16:23:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CB506B0078; Thu, 19 Jan 2023 16:23:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0A8F56B0071 for ; Thu, 19 Jan 2023 16:23:29 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CE38A120B89 for ; Thu, 19 Jan 2023 21:23:28 +0000 (UTC) X-FDA: 80372824896.23.F7AE00A Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf29.hostedemail.com (Postfix) with ESMTP id ACBF512000A for ; Thu, 19 Jan 2023 21:23:26 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bxVyM2XU; spf=pass (imf29.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1674163407; a=rsa-sha256; cv=none; b=iU/6pUwH8zXvLoMhz5pVFFl2c3hYcpyUMD5fMlI7O8YJnRr1JTulf2AsUFF5WRlriSe29u 2/yuJXHn5XbgxkDk25XaEec/40ncRhEDO+pvDlmpjljpkoFZd2QIsPt3fEnJCY29bZExav Qojv3nfR+etJ/5JV+F81funHH7XjSOQ= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bxVyM2XU; spf=pass (imf29.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1674163407; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=g+2PRwqZcp2dJEdWD5Hf1MbtN8bNDIL6rm89W+Rq+f0=; b=4qsL7KJg/9kYMo9vmUa4yXWllNpq+wCX1wP+DJLVU4EX30EW5PNFahTOrfBHYbBhDj5k73 ltz8edCE7AnJoEMMFjNw8A0tB6WXLth682b9TWJqD9y4dXuouecXlcZCS8olFyVLYBTaMT SaDEPDv+L7yp4kTcL7eNI4G7Vrh2ByA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163406; x=1705699406; h=from:to:cc:subject:date:message-id; bh=KeghPjHYUOp2vxEfBJ4+MNk/QB9fTnQp/kAL+jxFnkc=; b=bxVyM2XU2W9ZsAWmgPZE/vVMPaIPlbOLkzq0T9ws0YqJw4HjkFwCRpM3 vdR8JxqjZ2lBk7ziTaAqPbl7SZNz+zeeJa6NEu3b1A41/1g5WJ0ue8Kjj NtWyTmTdQ7Ox9htJrH72/9Z4lRWoJIOU9BMvz/gaG2BzxBVYXBvkhZRnI SrhaVvC4+gKSqPsCACCXRc4cb151DU1HKhzyg9Exm+lzGeFnTGT3OGFjV /DO3QK/mv2V+xUZE+7gPzkEJ+norg1ZO6DTCqLP1HymcaOs/xsm+eEu6K fd0ieL7w9xF9shfSB24hue0/YPQDnryw0+eSw0W3gb6XcesqD+xX0/mI7 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119141" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119141" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:24 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989138978" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989138978" Received: from hossain3-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.252.128.187]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:23 -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 Cc: rick.p.edgecombe@intel.com Subject: [PATCH v5 00/39] Shadow stacks for userspace Date: Thu, 19 Jan 2023 13:22:38 -0800 Message-Id: <20230119212317.8324-1-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 X-Rspam-User: X-Rspamd-Queue-Id: ACBF512000A X-Rspamd-Server: rspam01 X-Stat-Signature: y1nqmctc33qcey8a8oudux5zg7g4ze5c X-HE-Tag: 1674163406-921057 X-HE-Meta: U2FsdGVkX19RykSbNiy6Nj+yZ418lWTE3MbmwTPYG1CaVR3u2awWHlTqKLLlZlN3/9R/MmTWUNO1Ox03VNy5VnqJElPuY66jFUKzRiGhG51VeNTZ1eUx/Ow6ztmfeL0ja5IuvuNEtt/Zc3SI0SOIi1aCglCPRoIRauT9z45m7LsZ2vOTRSPcPiP/3jIAZUzt/HDGjVW2G6BAq0Chh+PAlHZvyrxXv/yZVE/nj8e/JM35acgzl8OjcY9hF3NtC1Sbv+nSVTSi1wEoeMzx5J15U/mVFMoNet42ZOuDjf346W9vel896/VIEXSthfaXcF/Cs+9QfO+SMlIDuIggimE3BuEEgF6eUt05cNniWaIq5I+87XxaYNDxZs5Bwi/hshfAGHZWr2ByBxNAoENLHVc5Y7BlKgJHFyEn9LlO5WqVpSoi+zT2s0P83rz3lc2MMfbDvL5yLj1wBJB9pNvL614MCJSlrkRDiO6wv0zeeULzpL/CEypjedrHF8Oi/5rdqZx/xsIBZTrOMl2/PBM9iz+8sGbYQnCRcnpTQPTE44ns038WeYDPQuukjTMZVTgOinIyZMpDwQ5S0MD/HKsBIgLloS+ODzCiNx4PODra2JE0sDebjPvAwW+lv20sf6QHkHrpcltBEr+fMsxn6zsH5e8wasYLAmQYEiNpM1Oz2tf3tnPWvc81moUqa2rdJiYtTjqHq4rxe6JO8ktijO6Z57UiykYcaY91tW6snczfoPAlg/b3K8Z46KiwJwkzDDyOnAEYtryksxMc6xejEaNISewJ1TYTrluaZY5vC+AYYy8zHlEXa94JJCVtkZSBiUTKH1P7hCQu5Xmz26DkVlI9R1HhbfzKrrkUCJa131LAgMiencowPK/w9mqW7prZh9xxcViYOT+eyJHSY6pp0xN/wPejBpdQdgFT5fMQPyG5XqV/GVSIRBw/bFpCDv0iuwFSmizI9amDUsU6HVxCGxjc+Oz ovbtCKrU WjdyYPBODNTAIlGawryq2Q8f6PR+zXtZmCXAS+/vL79x60hIp+N2bNYCQzmBTvvE0FsKmL0GKRRDmL+XoAkY0Zm2C9dLSFgeBViVucj6Bfhzl7wJmd2WA/cJv5jQXN4GTTSTm3YCAMGE0lMiex5RON/TrGUOYyV+Fv6RBEqbH4eQEECUJ0OfyvYa/Ln0xoRQovpeHSKItyqKAsn4QAvK9HK3l5lmkh9Xm8LrkpqG9x6uQXD/RgQ+7uo3Ug50r/Jf/WhC4HijpQrak808= 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 main change in this version is the removal of the attempt to prevent 32 bit signals from being registered with shadow stack enabled. Peterz originally raised the issue that shadow stack support in 32 bit signals was in a half working state. The reason for that was 32 bit signals are not easy to support for shadow stack, and also there is not a huge demand for shadow stack support in 32 bit apps using 32 bit emulation on 64 bit kernels. At that point the solution was to prevent shadow stack from being enabled on 32 bit processes. But Peterz pointed that 64 bit apps can transition to 32 bit outside of kernel interaction by making a far call to a 32 bit segment. So the next solution was to prevent 32 bit signals from being registered when shadow stack was enabled. This turned out to be hard to do, due to signals being per-process and shadow stack being per task. But it turns out this far call scenario was already mostly not possible due to the HW not supporting shadow stacks located outside of the 32 bit address space when in 32 bit mode. During the transition to 32 bit mode with an SSP pointing outside of the 32 bit address space, HW generates a #GP which in turn triggers a segfault. So basically there is already a barrier in place for this far call scenario for the most part. Creation of shadow stack memory is tightly controlled, so the solution in this version is just to *ensure* that shadow stacks can never be allocated in the 32 bit address space. For more information see the new patch: "x86/mm: Introduce MAP_ABOVE4G", and the documentation in patch 1. Additionally: - A smattering of small changes from Boris and Kees - Fixed my spellcheck setup and then fixed a bunch of spelling issues in the commit logs. - An update to the pte_modify() PAGE_COW solution I left tested-by tags in place per discussion with testers. Testers, please retest. Previous version [1]. [0] https://lore.kernel.org/lkml/20220130211838.8382-1-rick.p.edgecombe@intel.com/ [1] https://lore.kernel.org/lkml/20221203003606.6838-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 (14): x86/fpu: Add helper for modifying xstate x86/mm: Introduce _PAGE_COW x86/mm: Start actually marking _PAGE_COW mm: Handle faultless write upgrades for shstk 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/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 (23): 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: 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 pte_modify for _PAGE_COW x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW 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 x86/mm: Update maybe_mkwrite() for shadow stack mm: Fixup places that call pte_mkwrite() directly 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/x86/index.rst | 1 + Documentation/x86/shstk.rst | 176 +++++ arch/arm/kernel/signal.c | 2 +- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/signal32.c | 2 +- arch/sparc/kernel/signal32.c | 2 +- arch/sparc/kernel/signal_64.c | 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 | 338 ++++++++- arch/x86/include/asm/pgtable_types.h | 65 +- 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 | 87 +++ 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 | 492 +++++++++++++ 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 | 38 + arch/x86/mm/pat/set_memory.c | 2 +- arch/x86/mm/pgtable.c | 6 + arch/x86/xen/enlighten_pv.c | 2 +- arch/x86/xen/xen-asm.S | 2 +- fs/aio.c | 2 +- fs/proc/array.c | 6 + fs/proc/task_mmu.c | 3 + include/linux/mm.h | 59 +- include/linux/mman.h | 4 + include/linux/pgtable.h | 35 + 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/gup.c | 2 +- mm/huge_memory.c | 12 +- mm/memory.c | 7 +- mm/migrate_device.c | 4 +- mm/mmap.c | 12 +- mm/nommu.c | 4 +- mm/userfaultfd.c | 10 +- mm/util.c | 2 +- tools/testing/selftests/x86/Makefile | 4 +- .../testing/selftests/x86/test_shadow_stack.c | 667 ++++++++++++++++++ 78 files changed, 2578 insertions(+), 259 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 Tested-by: John Allen Acked-by: Mike Rapoport (IBM)