Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1107684ybz; Wed, 29 Apr 2020 15:12:18 -0700 (PDT) X-Google-Smtp-Source: APiQypKdRgJF6QcFq9MGqHroBBl1t7FJbkFq0zVXtQVxHcaztj1pPaFLkuaN9xzH6W9kj8GM1o04 X-Received: by 2002:aa7:cdcb:: with SMTP id h11mr127097edw.264.1588198338363; Wed, 29 Apr 2020 15:12:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588198338; cv=none; d=google.com; s=arc-20160816; b=Ui19s5nV0YMVKwZ7yObTv10vETgZW9S/7EjuUNaDVPSUut3F9Uva1goartTturv313 uP9t/WA3xos9K2wmfjVHfEVNW6SNzahkzydoAUucj/hPL2rDElWJL7AKJ3QB/SYq1PJh gpBM7sGxXwvBmlX/U3OoYCHRdexMDknnbdQc2hu95fZvLoc9ClGdJDNIwgk8VQbFEyFI dJriUvjix5G+sxhHYsgF5e6VfVvc+fBGk10xCQU1/IuypX/fkIH7H3W/um4huPTW33Pp 9f8yAdGJ2iE1ximXGrmsEy6f6fSb+F0U2D99HKWQUI6KlzJ7s2M74GTxTJ6tvFMDZxI2 9FAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=IV504k4REnSwBk1L6dr6Yx1po4Is2PKoQQBI353MsyY=; b=hYksDZvDdPMDdblGn6Kz/rTwEKqXEaClvq8i0GN4jHvEHf/FhpO43MMlx3jZmDOKQX LZRrZnZifA1mbnTqUQC3JX895dLejFBaR5wS/R+cpQEYuJRYsuQITJMOUxrAZGnyOGyL DI+EKJFTzKz7u7NezB+jw+uzb4KrGXVP/xSQWm2CP/4uzAPglsCe6lljbdbnVARzz/xY 3W4EKvruYkgCxkxecmegMvP+Xx2UZQIZpIo4C1Uw1CZYhRPuqm9WHH+MyBaiG+uaxUOd KYoH0ifDnTeStbsVpjkWRd4FEz/SPkquw+xOxDU8MznMbfKfbwtIP08AFxATj/RKi2QO wWrA== 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 d6si4130981edy.66.2020.04.29.15.11.55; Wed, 29 Apr 2020 15:12:18 -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 S1728289AbgD2WJ4 (ORCPT + 99 others); Wed, 29 Apr 2020 18:09:56 -0400 Received: from mga09.intel.com ([134.134.136.24]:61306 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727933AbgD2WIt (ORCPT ); Wed, 29 Apr 2020 18:08:49 -0400 IronPort-SDR: QAZBZQ+Nojq1cmQyD28yxiVHeb5E3jIv+PHy9CTGyVEjR7yInj26Aw4F1Ao2BDn/NkOBPBq3pF uxqrfFQ6QSNA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2020 15:08:48 -0700 IronPort-SDR: wUPVS6TcPPbWAbooMO8NjtL8Z4wUCmoBJDKvJwh8pI0Ugdlvf7Cmbj8k5QRjSmB+Y+2bKrQAYs h3mXof4ZmPLw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,333,1583222400"; d="scan'208";a="276308906" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga002.jf.intel.com with ESMTP; 29 Apr 2020 15:08:48 -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 Cc: Yu-cheng Yu Subject: [PATCH v10 16/26] mm: Add guard pages around a shadow stack. Date: Wed, 29 Apr 2020 15:07:22 -0700 Message-Id: <20200429220732.31602-17-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200429220732.31602-1-yu-cheng.yu@intel.com> References: <20200429220732.31602-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org 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 --- v10: - Define ARCH_SHADOW_STACK_GUARD_GAP. 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 3bcf27caf6c9..eb9536f803f9 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -907,6 +907,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 f0669e3cdd37..68eadf2c466d 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2630,6 +2630,10 @@ void page_cache_async_readahead(struct address_space *mapping, pgoff_t offset, unsigned long size); +#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); @@ -2662,9 +2666,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; } @@ -2674,9 +2684,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