Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp684320pxb; Wed, 3 Feb 2021 15:19:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJwAWJVsAa9MeeXebyMHBzwr7R3Jbfl1eOKXCi+4rOQvhtoVDZZxnpCDvFtNLSwodGuxbXhA X-Received: by 2002:aa7:dd4c:: with SMTP id o12mr5379840edw.180.1612394380999; Wed, 03 Feb 2021 15:19:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612394380; cv=none; d=google.com; s=arc-20160816; b=0ix++KSggaTY+0lPmWIPMz57EcPO6Mz0C6AKlvwKMrt8xtJHfgKyrREL3NH0N04764 wFlZpwxL7ofuldBLwlnKurKgKGZvYwIdJLGr5YvXThM5CmU9kWv5a8+xb//4tHSmFTab ao3RHbKIAcpfHW8s7CwzB2omDiKMxF2dRc8iwr0dTJPWvPX2EZVcKYDtgmmVxv6PPiQL PQjvaNXjRzQ/foQTUTCOwmPLkYNBENgpTqk3hikGeoZTmzulWjaxYi/HfNjjTHC3llyl 6jLjrR8Osq61EaqrQYAoDhcV+lnJqv9DOo5zVEc+Sz4OHPjiLifBOn7XgoiU3tVEQWHy S8dw== 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=2sebWxpezThjyEpQwuahOyxOujyGP+YMCqOcOmAfOBA=; b=Wc1AjeTrHmzOipwQW0llctOGjhjwl7ZpAAeKPMEttOvRmEJU/ROSpZy+91cuBzmzwv GrP3xDO4G+Bp65HoOdNape11sF0x+kGltu7O9hvS/l148xkZgfOO6VYQ8gt77TsnKc5Q RI3qn1tGiv1q5EH2Gfm/+7xAYrlizkjVKnJqB77HjNHSDHbx6oCJjoR+UFY3jCWY8t1O O+FDQooleOHAWSov1BouFOB2+nnaegojhkIJI74eIUAqWBcebxgPaK9dF2TtnDetCIl9 IF/9pNkLGeMU+PmUYx60BeTIrsL7fIeQRnG2Yo6al9cO0NSSZOw65yb0k+7Bta+ln/T+ QJzA== 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 eb13si2878162edb.8.2021.02.03.15.19.16; Wed, 03 Feb 2021 15:19:40 -0800 (PST) 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 S233195AbhBCXQG (ORCPT + 99 others); Wed, 3 Feb 2021 18:16:06 -0500 Received: from mga06.intel.com ([134.134.136.31]:30214 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233283AbhBCW6X (ORCPT ); Wed, 3 Feb 2021 17:58:23 -0500 IronPort-SDR: 9i7GTc+QTW8QwIP682GOTXxowKjcJF5dCuMnchgAa59Aw8GWl6hzDCzSgj6Rk5ZK4SJmtScKGy GA9JvVUa1IMg== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="242642367" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="242642367" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:29 -0800 IronPort-SDR: pndA0FkcAg9V8kZ2Myj45z9tmhefWkilfH6mpLfCOd6idreHpNDT3tFsGDnIOUeoE9GAHurNPX Aj9gF/HqPg3Q== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="507921131" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:28 -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 v19 16/25] mm: Add guard pages around a shadow stack. Date: Wed, 3 Feb 2021 14:55:38 -0800 Message-Id: <20210203225547.32221-17-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210203225547.32221-1-yu-cheng.yu@intel.com> References: <20210203225547.32221-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 --- 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 645bd1d0ee07..fda4c023e009 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 +/* + * 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 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 abd756e426fc..c12b3d36dbd3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2640,6 +2640,10 @@ extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf); int __must_check write_one_page(struct page *page); void task_dirty_inc(struct task_struct *tsk); +#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 address); @@ -2672,9 +2676,15 @@ static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * m static inline 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) { - vm_start -= stack_guard_gap; + if (vma->vm_flags & VM_GROWSDOWN) + gap = stack_guard_gap; + else if (vma->vm_flags & VM_SHSTK) + gap = ARCH_SHADOW_STACK_GUARD_GAP; + + if (gap != 0) { + vm_start -= gap; if (vm_start > vma->vm_start) vm_start = 0; } @@ -2684,9 +2694,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 = vma->vm_end; + unsigned long gap = 0; + + if (vma->vm_flags & VM_GROWSUP) + gap = stack_guard_gap; + else if (vma->vm_flags & VM_SHSTK) + gap = ARCH_SHADOW_STACK_GUARD_GAP; - if (vma->vm_flags & VM_GROWSUP) { - vm_end += stack_guard_gap; + if (gap != 0) { + vm_end += gap; if (vm_end < vma->vm_end) vm_end = -PAGE_SIZE; } -- 2.21.0