From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B528AC388F7 for ; Tue, 10 Nov 2020 16:23:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4AA1120780 for ; Tue, 10 Nov 2020 16:23:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4AA1120780 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A26076B0080; Tue, 10 Nov 2020 11:22:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7411C6B0098; Tue, 10 Nov 2020 11:22:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4AB806B0093; Tue, 10 Nov 2020 11:22:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0083.hostedemail.com [216.40.44.83]) by kanga.kvack.org (Postfix) with ESMTP id D9A786B0085 for ; Tue, 10 Nov 2020 11:22:57 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 80AAD180AD830 for ; Tue, 10 Nov 2020 16:22:57 +0000 (UTC) X-FDA: 77469027594.23.week95_3f0f5dc272f6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id 8FB0E37608 for ; Tue, 10 Nov 2020 16:22:56 +0000 (UTC) X-HE-Tag: week95_3f0f5dc272f6 X-Filterd-Recvd-Size: 5976 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Nov 2020 16:22:55 +0000 (UTC) IronPort-SDR: QRusISvu26IpSpKo0cQpiMcZ0s13cLy6+PuVNx92OzL0cUcDhLUPPWl9PeyJAWrIFo874aygLE GWQ3JAUR0A6A== X-IronPort-AV: E=McAfee;i="6000,8403,9801"; a="170110960" X-IronPort-AV: E=Sophos;i="5.77,466,1596524400"; d="scan'208";a="170110960" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2020 08:22:53 -0800 IronPort-SDR: X1Xilg3tsvrfiVzhCGv1/87nT21DwI9nZ80X0r+y/t1qqzKIAdFKQtIQk9ovtE9kBvUACfTNny Z33ygY3x1iAA== X-IronPort-AV: E=Sophos;i="5.77,466,1596524400"; d="scan'208";a="365572878" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2020 08:22:52 -0800 From: Yu-cheng Yu 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 , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu Cc: Yu-cheng Yu Subject: [PATCH v15 16/26] mm: Add guard pages around a shadow stack. Date: Tue, 10 Nov 2020 08:22:01 -0800 Message-Id: <20201110162211.9207-17-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201110162211.9207-1-yu-cheng.yu@intel.com> References: <20201110162211.9207-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: INCSSP(Q/D) increments shadow stack pointer and 'pops and discards' the first and the last elements in the range, effectively touches those memor= y areas. The maximum moving distance by INCSSPQ is 255 * 8 =3D 2040 bytes and 255 * 4 =3D 1020 bytes by INCSSPD. Both ranges are far from PAGE_SIZE. Thus, putting a gap page on both ends of a shadow stack prevents INCSSP, CALL, and RET from going beyond. Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/page_64_types.h | 10 ++++++++++ include/linux/mm.h | 24 ++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/page_64_types.h b/arch/x86/include/asm/= page_64_types.h index 3f49dac03617..0fbee6dcd3ca 100644 --- a/arch/x86/include/asm/page_64_types.h +++ b/arch/x86/include/asm/page_64_types.h @@ -97,6 +97,16 @@ #define STACK_TOP TASK_SIZE_LOW #define STACK_TOP_MAX TASK_SIZE_MAX =20 +/* + * Shadow stack pointer is moved by CALL, JMP, and INCSSP(Q/D). INCSSPQ + * moves shadow stack pointer up to 255 * 8 =3D ~2 KB (~1KB for INCSSPD)= and + * touches the first and the last element in the range, which triggers a + * page fault if the range is not in a shadow stack. Because of this, + * creating 4-KB guard pages around a shadow stack prevents these + * instructions from going beyond. + */ +#define ARCH_SHADOW_STACK_GUARD_GAP PAGE_SIZE + /* * Maximum kernel image size is limited to 1 GiB, due to the fixmap livi= ng * in the next 1 GiB (see level2_kernel_pgt in arch/x86/kernel/head_64.S= ). diff --git a/include/linux/mm.h b/include/linux/mm.h index 09f07d07a8ff..80cda65bb1ae 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2639,6 +2639,10 @@ extern vm_fault_t filemap_page_mkwrite(struct vm_f= ault *vmf); int __must_check write_one_page(struct page *page); void task_dirty_inc(struct task_struct *tsk); =20 +#ifndef ARCH_SHADOW_STACK_GUARD_GAP +#define ARCH_SHADOW_STACK_GUARD_GAP 0 +#endif + extern unsigned long stack_guard_gap; /* Generic expand stack which grows the stack according to GROWS{UP,DOWN= } */ extern int expand_stack(struct vm_area_struct *vma, unsigned long addres= s); @@ -2671,9 +2675,15 @@ static inline struct vm_area_struct * find_vma_int= ersection(struct mm_struct * m static inline unsigned long vm_start_gap(struct vm_area_struct *vma) { unsigned long vm_start =3D vma->vm_start; + unsigned long gap =3D 0; =20 - if (vma->vm_flags & VM_GROWSDOWN) { - vm_start -=3D stack_guard_gap; + if (vma->vm_flags & VM_GROWSDOWN) + gap =3D stack_guard_gap; + else if (vma->vm_flags & VM_SHSTK) + gap =3D ARCH_SHADOW_STACK_GUARD_GAP; + + if (gap !=3D 0) { + vm_start -=3D gap; if (vm_start > vma->vm_start) vm_start =3D 0; } @@ -2683,9 +2693,15 @@ static inline unsigned long vm_start_gap(struct vm= _area_struct *vma) static inline unsigned long vm_end_gap(struct vm_area_struct *vma) { unsigned long vm_end =3D vma->vm_end; + unsigned long gap =3D 0; + + if (vma->vm_flags & VM_GROWSUP) + gap =3D stack_guard_gap; + else if (vma->vm_flags & VM_SHSTK) + gap =3D ARCH_SHADOW_STACK_GUARD_GAP; =20 - if (vma->vm_flags & VM_GROWSUP) { - vm_end +=3D stack_guard_gap; + if (gap !=3D 0) { + vm_end +=3D gap; if (vm_end < vma->vm_end) vm_end =3D -PAGE_SIZE; } --=20 2.21.0