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=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 B8EF5C2BA1A for ; Tue, 16 Mar 2021 15:13:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 44F3F650AE for ; Tue, 16 Mar 2021 15:13:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 44F3F650AE 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 D56B76B0074; Tue, 16 Mar 2021 11:13:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D2E036B0085; Tue, 16 Mar 2021 11:13:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7F966B0087; Tue, 16 Mar 2021 11:13:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0224.hostedemail.com [216.40.44.224]) by kanga.kvack.org (Postfix) with ESMTP id 9B5146B0074 for ; Tue, 16 Mar 2021 11:13:11 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 5BB5E8249980 for ; Tue, 16 Mar 2021 15:13:11 +0000 (UTC) X-FDA: 77926080582.25.A992AED Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by imf17.hostedemail.com (Postfix) with ESMTP id 280914080F59 for ; Tue, 16 Mar 2021 15:13:06 +0000 (UTC) IronPort-SDR: xc86Dlg/7cxDWKVZC5ys1dmeWJ+qQLbmp9JlnPgeyywnQJyYBz3NDctSoTCWyyaRoWLL6RogmZ h9OpMGymdgGg== X-IronPort-AV: E=McAfee;i="6000,8403,9924"; a="169190311" X-IronPort-AV: E=Sophos;i="5.81,251,1610438400"; d="scan'208";a="169190311" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2021 08:11:31 -0700 IronPort-SDR: XtKx3L4HKogssycBM6GJgmQYYAihzpvpuzzhXpP39M8bNmn5hP8m/nVuB7aPR0ENVeyZaRGAgA rBqW0DRk7IFQ== X-IronPort-AV: E=Sophos;i="5.81,251,1610438400"; d="scan'208";a="405570301" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2021 08:11:30 -0700 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 , Haitao Huang Cc: Yu-cheng Yu Subject: [PATCH v23 17/28] mm: Add guard pages around a shadow stack. Date: Tue, 16 Mar 2021 08:10:43 -0700 Message-Id: <20210316151054.5405-18-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210316151054.5405-1-yu-cheng.yu@intel.com> References: <20210316151054.5405-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 280914080F59 X-Stat-Signature: o9e99f36jbaex8wtqc6eegzxq4ktt3zb Received-SPF: none (intel.com>: No applicable sender policy available) receiver=imf17; identity=mailfrom; envelope-from=""; helo=mga17.intel.com; client-ip=192.55.52.151 X-HE-DKIM-Result: none/none X-HE-Tag: 1615907586-311139 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 Reviewed-by: Kees Cook --- 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 64297eabad63..23e3d880ce6c 100644 --- a/arch/x86/include/asm/page_64_types.h +++ b/arch/x86/include/asm/page_64_types.h @@ -115,4 +115,14 @@ #define KERNEL_IMAGE_SIZE (512 * 1024 * 1024) #endif =20 +/* + * Shadow stack pointer is moved by CALL, RET, 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 + #endif /* _ASM_X86_PAGE_64_DEFS_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index af805ffde48e..9890e9f5a5e0 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2619,6 +2619,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); @@ -2651,9 +2655,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; } @@ -2663,9 +2673,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