Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4832012pxy; Tue, 27 Apr 2021 13:46:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwR7T7NC60W9xwV4RltcpQBl1sKKITiIe6Z6BXSx/9V+mWgSCqbv88/m1FCe1wUVm4wiYG7 X-Received: by 2002:a17:902:74c5:b029:ed:d41:1ca0 with SMTP id f5-20020a17090274c5b02900ed0d411ca0mr17323906plt.66.1619556416690; Tue, 27 Apr 2021 13:46:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619556416; cv=none; d=google.com; s=arc-20160816; b=Us/gDiPDMaL/IH3+13t7cbeZyUxWLp+C+HI4FvCVFsdz8vVdSmt+XjArC8nWu2CK2w lwUd7W5TeRA+zs1pnw4MsZ92Ya/18oVf9FN6jXDPfmISVvIkLbz6p1zo+vDD18AYT4IO Sj4o0tv3WY4e7WxwJ8NoCfC/SUVx/NF1WkM5zFxcRzxQRe1GkmBXJuGVQieFNwId7ag6 jhRNKU8vHeg0Ncs5wQ/mKarBlqLNJOqy+zFPQytO5PWLsmuKlVFQONgG7Yrl+SYeMOHm k7XmwW7S0VaMFQ1+gE8Upt0RpIdpWqfWwh4cRRpuiDok+RKp21dGLDCQ4+axtGRw1eus S8CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=lr9cRqdhjj/Qmnw3e9TPgLL+UmyeP9JaRCZEa4FYK04=; b=LfZ7appmRyEagss+SCtV5H0Hr3+p0f02/Z+ZGj82blsB5FBFUPCno7+7iV1TzBPD3e Q1AWz6QrNKjkDOIvnFK0axqqOPsZqg8qTlRDCPOZ2zZD4u+oIx++y0z5Q8ftorjNaE/8 VAOpp6SHpZexb1Nn4JoRsDyICKnGxlmLTL4cMS6JwCKgl8BDMREv1eMNzVqyh4HpLgEs vOnwsyStnl3x0+jPRd5WD+fMcHd738e9jtQ0GIdq627YyI5wpvlJKmA8KWuI/qxVMAwT dp4IajG/VhElasHfvpKhu9TUpOFUJ2y7hzMOyE0AiDzWfZCORG33d3lvml1C6UjRJhmS TeIw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a7si1048072pls.73.2021.04.27.13.46.44; Tue, 27 Apr 2021 13:46:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239160AbhD0UqI (ORCPT + 99 others); Tue, 27 Apr 2021 16:46:08 -0400 Received: from mga05.intel.com ([192.55.52.43]:31798 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237060AbhD0Upm (ORCPT ); Tue, 27 Apr 2021 16:45:42 -0400 IronPort-SDR: fgu8+1xVy8aNfHZx5FLjsukfQKwVKMo9FjfUPewOR6snP+WnV0KhWMcvoho8jP9nDdydPPCuWi xKcgCmSeRdBQ== X-IronPort-AV: E=McAfee;i="6200,9189,9967"; a="281922484" X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="281922484" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:18 -0700 IronPort-SDR: X4XEh26uRW5SCsYJRhadYcziflBR+F11pNRCZDRYRlgwXTEVWFoSxTpPvuUGbuSOsrdopsYXxB YvOdSAUtODww== X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="465623524" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:16 -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 v26 17/30] mm: Add guard pages around a shadow stack. Date: Tue, 27 Apr 2021 13:43:02 -0700 Message-Id: <20210427204315.24153-18-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210427204315.24153-1-yu-cheng.yu@intel.com> References: <20210427204315.24153-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org INCSSP(Q/D) increments shadow stack pointer and 'pops and discards' the first and the last elements in the range, effectively touches those memory areas. The maximum moving distance by INCSSPQ is 255 * 8 = 2040 bytes and 255 * 4 = 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: Kirill A. Shutemov Cc: Kees Cook --- v25: - Move SHADOW_STACK_GUARD_GAP to arch/x86/mm/mmap.c. v24: - Instead changing vm_*_gap(), create x86-specific versions. arch/x86/include/asm/page_types.h | 7 +++++ arch/x86/mm/mmap.c | 46 +++++++++++++++++++++++++++++++ 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/page_types.h index a506a411474d..e1533fdc08b4 100644 --- a/arch/x86/include/asm/page_types.h +++ b/arch/x86/include/asm/page_types.h @@ -73,6 +73,13 @@ bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn); extern void initmem_init(void); +#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__ */ #endif /* _ASM_X86_PAGE_DEFS_H */ diff --git a/arch/x86/mm/mmap.c b/arch/x86/mm/mmap.c index f3f52c5e2fd6..81f9325084d3 100644 --- a/arch/x86/mm/mmap.c +++ b/arch/x86/mm/mmap.c @@ -250,3 +250,49 @@ bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot) return false; return true; } + +/* + * Shadow stack pointer is moved by CALL, RET, and INCSSP(Q/D). INCSSPQ + * moves shadow stack pointer up to 255 * 8 = ~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 + +unsigned long vm_start_gap(struct vm_area_struct *vma) +{ + unsigned long vm_start = vma->vm_start; + unsigned long gap = 0; + + if (vma->vm_flags & VM_GROWSDOWN) + gap = stack_guard_gap; + else if (vma->vm_flags & VM_SHADOW_STACK) + gap = SHADOW_STACK_GUARD_GAP; + + if (gap != 0) { + vm_start -= gap; + if (vm_start > vma->vm_start) + vm_start = 0; + } + return vm_start; +} + +unsigned long vm_end_gap(struct vm_area_struct *vma) +{ + unsigned long vm_end = vma->vm_end; + unsigned long gap = 0; + + if (vma->vm_flags & VM_GROWSUP) + gap = stack_guard_gap; + else if (vma->vm_flags & VM_SHADOW_STACK) + gap = SHADOW_STACK_GUARD_GAP; + + if (gap != 0) { + vm_end += gap; + if (vm_end < vma->vm_end) + vm_end = -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_intersection(struct mm_struct * m return vma; } +#ifndef vm_start_gap static inline unsigned long vm_start_gap(struct vm_area_struct *vma) { unsigned long vm_start = vma->vm_start; @@ -2671,7 +2672,9 @@ static inline unsigned long vm_start_gap(struct vm_area_struct *vma) } return vm_start; } +#endif +#ifndef vm_end_gap static inline unsigned long vm_end_gap(struct vm_area_struct *vma) { unsigned long vm_end = vma->vm_end; @@ -2683,6 +2686,7 @@ static inline unsigned long vm_end_gap(struct vm_area_struct *vma) } return vm_end; } +#endif static inline unsigned long vma_pages(struct vm_area_struct *vma) { -- 2.21.0