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 828C3C001B8 for ; Thu, 1 Apr 2021 22:11:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0D4F961104 for ; Thu, 1 Apr 2021 22:11:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0D4F961104 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 9247D6B00F7; Thu, 1 Apr 2021 18:11:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FD916B00F5; Thu, 1 Apr 2021 18:11:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7780B6B00FA; Thu, 1 Apr 2021 18:11:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0254.hostedemail.com [216.40.44.254]) by kanga.kvack.org (Postfix) with ESMTP id 4965C6B00F7 for ; Thu, 1 Apr 2021 18:11:29 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 117421839A9F3 for ; Thu, 1 Apr 2021 22:11:29 +0000 (UTC) X-FDA: 77985195498.14.226C1A8 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf24.hostedemail.com (Postfix) with ESMTP id 8E79AA00038D for ; Thu, 1 Apr 2021 22:11:26 +0000 (UTC) IronPort-SDR: HDQu9z4f7/mgDWm+HOysT/9P2PomtNcgtHuK8HnktMhnAb3SFURIohOyTP/py7uzugd3V65cU7 7aMK7RNjkXBg== X-IronPort-AV: E=McAfee;i="6000,8403,9941"; a="192371374" X-IronPort-AV: E=Sophos;i="5.81,296,1610438400"; d="scan'208";a="192371374" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Apr 2021 15:11:24 -0700 IronPort-SDR: qvDUVgLNQTrVngNQgTB5rVJCECCArmO5+U62GuXCGyQXb4zI22BTpOZeJuMwvjbqFcyzdBaPKm F1w+csx0kq/Q== X-IronPort-AV: E=Sophos;i="5.81,296,1610438400"; d="scan'208";a="517513918" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Apr 2021 15:11:24 -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 , "Kirill A . Shutemov" Subject: [PATCH v24 17/30] mm: Add guard pages around a shadow stack. Date: Thu, 1 Apr 2021 15:10:51 -0700 Message-Id: <20210401221104.31584-18-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210401221104.31584-1-yu-cheng.yu@intel.com> References: <20210401221104.31584-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8E79AA00038D X-Stat-Signature: x7ttjk7w6bghp7fztdujyaj7fsif5zxq X-Rspamd-Server: rspam02 Received-SPF: none (intel.com>: No applicable sender policy available) receiver=imf24; identity=mailfrom; envelope-from=""; helo=mga03.intel.com; client-ip=134.134.136.65 X-HE-DKIM-Result: none/none X-HE-Tag: 1617315086-223368 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 Cc: Kees Cook Cc: Kirill A. Shutemov --- v24: - Instead changing vm_*_gap(), create x86-specific versions. arch/x86/include/asm/page_types.h | 17 +++++++++++++++ arch/x86/mm/mmap.c | 36 +++++++++++++++++++++++++++++++ include/linux/mm.h | 4 ++++ 3 files changed, 57 insertions(+) diff --git a/arch/x86/include/asm/page_types.h b/arch/x86/include/asm/pag= e_types.h index a506a411474d..3a5529bcfd76 100644 --- a/arch/x86/include/asm/page_types.h +++ b/arch/x86/include/asm/page_types.h @@ -73,6 +73,23 @@ bool pfn_range_is_mapped(unsigned long start_pfn, unsi= gned long end_pfn); =20 extern void initmem_init(void); =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 SHADOW_STACK_GUARD_GAP PAGE_SIZE + +#define vm_start_gap vm_start_gap +struct vm_area_struct; +extern unsigned long vm_start_gap(struct vm_area_struct *vma); + +#define vm_end_gap vm_end_gap +extern unsigned long vm_end_gap(struct vm_area_struct *vma); + #endif /* !__ASSEMBLY__ */ =20 #endif /* _ASM_X86_PAGE_DEFS_H */ diff --git a/arch/x86/mm/mmap.c b/arch/x86/mm/mmap.c index f3f52c5e2fd6..e714ddc8f3f7 100644 --- a/arch/x86/mm/mmap.c +++ b/arch/x86/mm/mmap.c @@ -250,3 +250,39 @@ bool pfn_modify_allowed(unsigned long pfn, pgprot_t = prot) return false; return true; } + +unsigned long vm_start_gap(struct vm_area_struct *vma) +{ + unsigned long vm_start =3D vma->vm_start; + unsigned long gap =3D 0; + + if (vma->vm_flags & VM_GROWSDOWN) + gap =3D stack_guard_gap; + else if (vma->vm_flags & VM_SHADOW_STACK) + gap =3D SHADOW_STACK_GUARD_GAP; + + if (gap !=3D 0) { + vm_start -=3D gap; + if (vm_start > vma->vm_start) + vm_start =3D 0; + } + return vm_start; +} + +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_SHADOW_STACK) + gap =3D SHADOW_STACK_GUARD_GAP; + + if (gap !=3D 0) { + vm_end +=3D gap; + if (vm_end < vma->vm_end) + vm_end =3D -PAGE_SIZE; + } + return vm_end; +} diff --git a/include/linux/mm.h b/include/linux/mm.h index 6ac9b3e9a865..3e9c84f21ef6 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2660,6 +2660,7 @@ static inline struct vm_area_struct * find_vma_inte= rsection(struct mm_struct * m return vma; } =20 +#ifndef vm_start_gap static inline unsigned long vm_start_gap(struct vm_area_struct *vma) { unsigned long vm_start =3D vma->vm_start; @@ -2671,7 +2672,9 @@ static inline unsigned long vm_start_gap(struct vm_= area_struct *vma) } return vm_start; } +#endif =20 +#ifndef vm_end_gap static inline unsigned long vm_end_gap(struct vm_area_struct *vma) { unsigned long vm_end =3D vma->vm_end; @@ -2683,6 +2686,7 @@ static inline unsigned long vm_end_gap(struct vm_ar= ea_struct *vma) } return vm_end; } +#endif =20 static inline unsigned long vma_pages(struct vm_area_struct *vma) { --=20 2.21.0