Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp837243pxf; Thu, 1 Apr 2021 15:13:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqXkkNxSc5F+UjCiCHArpLqqj0LnL6zyi3o9aYy7f528RGh9hxDOVaEbURrLv3UeqYWwQH X-Received: by 2002:a02:caa9:: with SMTP id e9mr9828398jap.59.1617315200419; Thu, 01 Apr 2021 15:13:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617315200; cv=none; d=google.com; s=arc-20160816; b=p+q6hlUFXrgbpf5WpCjgr8or4Pz0MEm8cHrhEgGKJoEWYSrhu6kxOQDRF+DeVimAlH iR4RIN9URq0nYcdlNu729wBXMCWDJ9sjWCCS+HbkLAinueKuiGOlhNp+QgsQuw7sb+Sj Tv6f7EfLpgMHEKgQJb5kHus6Lko5vuZphIQLkXRKtdC4CnHsDOyz66YzY8+KowgrdDes LCsDJrqx937ilQ9O7NS8dMCiU05ykX5VeLtYvVzQ6iFRw6RPmWM6k73qd94K9MMcHw35 XROLceb0iV1vLJCqREIooDtXgyK1b8LUqxYd+2kSHC47OAKU5EkShyI77fwbEVGboWFH Ux2g== 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=v7g2VmvQsBIOsbDisOxOYXeWz8dFgzlnzXfiSeopbpA=; b=yET10xJL4hBkSRHvvq89NNhn5TB3QiCywxBVz9aJ7xcEioo4hJG+BRSewektd+ROYt I6j5pF663rjVSJsAMD+o/fjAYyhVK9q2+QO/wqjZs1WFOO899RcVUfb5xdf07AUbKSdd Fec69FNXaZzbP8Tnm29pFy9qvHoZwhOAyM/K2Ixk7ujNbfc+So2smTzi+tTQNunYtPqU i5qwKrNXT8eGEWR2+skJiSYeDp0d/0CLzwfiSoJQoJiNXytq/6oB7y6X31bDnp1/O3X+ Z90pSKul8hsTUKc4ITcQFGGW8HljSM4eCN9eAun8ZnxB7CWBc6BCRuqw+vIC7Tz5FzEc wpNQ== 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 l11si6630063ils.61.2021.04.01.15.13.06; Thu, 01 Apr 2021 15:13:20 -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 S235928AbhDAWMV (ORCPT + 99 others); Thu, 1 Apr 2021 18:12:21 -0400 Received: from mga11.intel.com ([192.55.52.93]:34674 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235069AbhDAWLh (ORCPT ); Thu, 1 Apr 2021 18:11:37 -0400 IronPort-SDR: zgkDeCHnpzIquYTklwezMm+ENQdxG4zmLC4q/nouvLUJMOK1l712JQcoiT7n5AigtsFSyuWkxQ oGMwE/6LuU9g== X-IronPort-AV: E=McAfee;i="6000,8403,9941"; a="189084585" X-IronPort-AV: E=Sophos;i="5.81,296,1610438400"; d="scan'208";a="189084585" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.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 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 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/page_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, unsigned long end_pfn); extern void initmem_init(void); +/* + * 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 + +#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..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 = 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