Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4522659pxu; Mon, 12 Oct 2020 23:45:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywiWppjAeBLeyQoHvlieiu9eboPO4FkUE/vuZOAdu+bUxpGJolrobJe+zRrRrbnVcJdLeU X-Received: by 2002:aa7:c7d7:: with SMTP id o23mr18134254eds.44.1602571550910; Mon, 12 Oct 2020 23:45:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602571550; cv=none; d=google.com; s=arc-20160816; b=BTbxEBkwoYnhUaur4bS+MWcSGiP2svGR4CFpDY+LvzHMbt1mJucJMzDk1Yd0Qc2FBz sWX9MvDT1jS2wNnaj95mGmlv4OpA12Xa3Hap8Yzt1OPaIGjK4XmrdrTRjYHSLyZBsvPf sXRCuG2aJtJF40ZvkidJlURUbXjSNMQZJI3Dub0Ylzfqg1r3m24REQ05rDXFt3hsVde7 vxNVnym8qrXOuTAxhOvHopoBJXM2358SUHZLn2l9gpfleAsVJ2kDRjg3lVzZcgwx/LTC QyB/t0mKvqmT6XUpuo/zpJTAXLmOJRAsh1R0Qx0/9HiqkLffT0F1qhoePDB38XzFAf7C vgUw== 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=cfOzqAh6Y3PtdtdHyddS/jTN2n+H9q/pePsCAOj2kfE=; b=NqzzGJJ7Mmsm+IUmxrBD3sYo4CyXQ/JDQ0nmmlIHjqVXaTbrDBwyZyDFNEYwDQ1jK9 qz/LvTtxLEBaYFgEsC4nPsn/KpMVC0N2KGhfOe72puE/uYix9qQQXbt5cEAp+xr2Z2bc IV3JmJ+hcgP0KjF/pyxA377yZ6l21HsdZP/CcHtkO4QayoiX96pqR2O05y7yp8f05g56 uYFSjupTGW0rPeTmnfgb1dZ2H+GXfPJ+dMZFqTlLcWl/vBJGyTQYVV1Jt6pcxiU8bMSl 0WHPQpMtDrGJOLe1Q3FYs9uiJ/pXiVeFdw2aGND0VUZ+4WG5OP6GT6Hgiwz5cfaEMS97 q/wQ== 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 y2si13089025ede.379.2020.10.12.23.45.28; Mon, 12 Oct 2020 23:45:50 -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 S2390716AbgJLPlE (ORCPT + 99 others); Mon, 12 Oct 2020 11:41:04 -0400 Received: from mga05.intel.com ([192.55.52.43]:1323 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390341AbgJLPkH (ORCPT ); Mon, 12 Oct 2020 11:40:07 -0400 IronPort-SDR: HuWYj6Jo+bJIwHqdDXGlZPMhs+stWPI+sYckG9qM06G5EeFJt5BuCjA9btS5ZgwBBw7GBDNxEj q9g60PsNVGlA== X-IronPort-AV: E=McAfee;i="6000,8403,9772"; a="250452694" X-IronPort-AV: E=Sophos;i="5.77,367,1596524400"; d="scan'208";a="250452694" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2020 08:39:54 -0700 IronPort-SDR: uWaSeHh1uSCgF6U+F52fJpCp8vsAHiDtGJSNbdZ0NxIo+NYLri10Ld2Uzl+KfWsg0yDrmE22I6 08cRUO3IJegQ== X-IronPort-AV: E=Sophos;i="5.77,367,1596524400"; d="scan'208";a="530010883" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2020 08:39:54 -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 Cc: Yu-cheng Yu Subject: [PATCH v14 16/26] mm: Add guard pages around a shadow stack. Date: Mon, 12 Oct 2020 08:38:40 -0700 Message-Id: <20201012153850.26996-17-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201012153850.26996-1-yu-cheng.yu@intel.com> References: <20201012153850.26996-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/processor.h | 10 ++++++++++ include/linux/mm.h | 24 ++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 97143d87994c..01acbd63cad8 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -840,6 +840,16 @@ static inline void spin_lock_prefetch(const void *x) #define STACK_TOP TASK_SIZE_LOW #define STACK_TOP_MAX TASK_SIZE_MAX +/* + * Shadow stack pointer is moved by CALL, JMP, 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 + #define INIT_THREAD { \ .addr_limit = KERNEL_DS, \ } diff --git a/include/linux/mm.h b/include/linux/mm.h index 4f6305106feb..ce461795fd8b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2615,6 +2615,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); @@ -2647,9 +2651,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; } @@ -2659,9 +2669,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