From patchwork Sat Dec 3 00:35:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13063341 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 C8D32C4708E for ; Sat, 3 Dec 2022 00:36:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 452C46B0072; Fri, 2 Dec 2022 19:36:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DBD66B0073; Fri, 2 Dec 2022 19:36:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2574F6B0074; Fri, 2 Dec 2022 19:36:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 148196B0072 for ; Fri, 2 Dec 2022 19:36:32 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CD4841A046E for ; Sat, 3 Dec 2022 00:36:31 +0000 (UTC) X-FDA: 80199128982.10.2EC4E68 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf22.hostedemail.com (Postfix) with ESMTP id 22887C0004 for ; Sat, 3 Dec 2022 00:36:29 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YLSIcfY3; spf=pass (imf22.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=1670027791; 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=Rslwj3ojwa5QEXhYUPXlaqBu/QfxYUxdTvpSLGLVH7k=; b=n/hNQlOJHnUc2RjTjOO8crGlZLFi1m2TElReYeaLukaALJYsKvEmGMZuKwEFzaGg8N7Yc1 HT3vQEJry/NulPRlEFJHlPH4sBDCjhuLabJSti6841nN89p2V+KZ7DiGxNNM/Wo9fbm+lk qqjoBPu3Aoae4kBGfxLkOkuDIdrxtxU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=YLSIcfY3; spf=pass (imf22.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=1670027791; a=rsa-sha256; cv=none; b=E8SxWVEG6CroQxbpPEtw580nSnH99WhHjmOS5mCFquK8Dq1zfMHrXA91GnI6hn3ZXVH7We edTGK/5o6HGmOL+6N2pWYj5PfdYOG30lV730o0YLBofSF6iy5ZhVe2/yFg/A3M5oe4jK9m mNnR0dnSMSYMSl1nJBtTNF9mr9QiVN8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670027790; x=1701563790; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=UExb0P455rgshLHt6GYI1t6rsa+B8lT8eGARn4e7l/I=; b=YLSIcfY3JrKSCwq5Ib5ivkwv8wzhpmaiu1mfSi88vV64KBYO4kkNqCtQ HUSjMYXTrLmP1kmNPIoqo3aCQVQVMUum+xJaVss1FVPh7XFCL1k9ptW5W BFzjOeQRmI+uh3vRryguKyhpkhWntod8QOR/SwQpSnTRRrM26dGb1A1/1 dPq5GULsAtq87RXKOU5fxO5ma2He6vJYvn9+JgI1/0UprOc9onZApccEV wfOvsTJJYKrxcJjHzaozdPXB5lqBiNB6lG5Wby2ylKVwO3PYRbeiGafi0 VWbazfMdB81juiYMUwBo0vzDMfPBEEk27EhK9R5fYrfyg+5SWGUPOLVEk w==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="313710619" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="313710619" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 16:36:28 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="787479735" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="787479735" Received: from bgordon1-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.212.211.211]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 16:36:26 -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 v4 00/39] Shadow stacks for userspace Date: Fri, 2 Dec 2022 16:35:27 -0800 Message-Id: <20221203003606.6838-1-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 22887C0004 X-Stat-Signature: sry9chu5cyu1j38xr5dyynhno499ut4n X-Rspam-User: X-Spamd-Result: default: False [-4.40 / 9.00]; BAYES_HAM(-6.00)[100.00%]; SUSPICIOUS_RECIPS(1.50)[]; MID_CONTAINS_FROM(1.00)[]; DMARC_POLICY_ALLOW(-0.50)[intel.com,none]; R_SPF_ALLOW(-0.20)[+ip4:192.55.52.93/32]; R_DKIM_ALLOW(-0.20)[intel.com:s=Intel]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_TWELVE(0.00)[39]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; FROM_HAS_DN(0.00)[]; DKIM_TRACE(0.00)[intel.com:+]; TO_DN_SOME(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; TAGGED_RCPT(0.00)[]; ARC_NA(0.00)[] X-Rspamd-Server: rspam08 X-HE-Tag: 1670027789-670023 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 permissioned 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]. I humbly think this is looking decent at this point, please consider applying. Again there were some smaller changes (logged in the patches). The more noteworthy changes were: - Separate Shadow stack from IBT in kernel APIs. This involves renaming the arch_prctl()s. And changing the ptrace cet regset interface to only expose the SSP, and not any IBT bits. - Handle 32 bit case more completely. (see commit log of “x86: Prevent 32 bit operations for 64 bit shstk tasks” for the details) - Drop elf header bit filtering compatibility patch for now, per Linus. [1] - Break apart _PAGE_COW patch for bisectability reasons. I left the Tested-by tags that were already in place, testers please re-test. Previous version [2]. Thanks, Rick [0] https://lore.kernel.org/lkml/20220130211838.8382-1-rick.p.edgecombe@intel.com/ [1] https://lore.kernel.org/lkml/CAHk-=wgP5mk3poVeejw16Asbid0ghDt4okHnWaWKLBkRhQntRA@mail.gmail.com/ [2] https://lore.kernel.org/lkml/20221104223604.29615-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 (13): x86/fpu: Add helper for modifying xstate x86/mm: Introduce _PAGE_COW x86/mm: Start actually marking _PAGE_COW mm: Don't allow write GUPs to shadow stack memory 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: Prevent 32 bit operations for 64 bit shstk tasks 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 (24): 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/mprotect: Exclude shadow stack from preserve_write 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 | 172 +++++ 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 | 14 +- 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 | 320 +++++++- arch/x86/include/asm/pgtable_types.h | 65 +- arch/x86/include/asm/processor.h | 8 + arch/x86/include/asm/shstk.h | 52 ++ arch/x86/include/asm/sighandling.h | 1 + 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/uapi/asm/mman.h | 3 + arch/x86/include/uapi/asm/prctl.h | 12 + arch/x86/kernel/Makefile | 2 + arch/x86/kernel/cpu/common.c | 37 +- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/cpu/proc.c | 23 + arch/x86/kernel/fpu/core.c | 60 +- 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 | 8 + arch/x86/kernel/ptrace.c | 12 + arch/x86/kernel/shstk.c | 495 +++++++++++++ arch/x86/kernel/signal.c | 21 + arch/x86/kernel/signal_64.c | 6 + arch/x86/kernel/signal_compat.c | 7 +- arch/x86/kernel/traps.c | 107 ++- 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 | 57 +- include/linux/pgtable.h | 35 + include/linux/proc_fs.h | 2 + include/linux/ptrace.h | 1 + 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/signal.c | 8 + kernel/sys_ni.c | 1 + mm/gup.c | 2 +- mm/huge_memory.c | 20 +- mm/memory.c | 7 +- mm/migrate_device.c | 4 +- mm/mmap.c | 12 +- mm/mprotect.c | 8 + 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 | 685 ++++++++++++++++++ 78 files changed, 2560 insertions(+), 178 deletions(-) create mode 100644 Documentation/x86/shstk.rst create mode 100644 arch/x86/include/asm/shstk.h create mode 100644 arch/x86/kernel/shstk.c create mode 100644 tools/testing/selftests/x86/test_shadow_stack.c