Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp620001pxb; Tue, 1 Feb 2022 07:08:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJwKn0yb86W6ik/gg0hfkD2SIOZbDofwoOSxFCVVuCEI+N0QPe4rP8e8XCMughENp388f/o0 X-Received: by 2002:a05:6402:1e92:: with SMTP id f18mr25630204edf.128.1643728102487; Tue, 01 Feb 2022 07:08:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728102; cv=none; d=google.com; s=arc-20160816; b=olziu+nA7BOX+qQOBaUYoeegt7jf9ohfEJBkKxDmXaYqS70u8FQ9OdMiFh5B150c1A jbQyPuXh1YmHVBfePYyfdeszjVWwpdSlqw9PbIOocojLckbFA5boqbp5g3Gh2ArczDQd /7qZH3SBM51pjCF5Wv4QOc3HDlb/oQhUeed4Ara/rH06M3cLqglV9zpmPkZ1M8ytM1Wb n2oSUaDzkX3yMJ3mGcwmSH3sA5SzDKYK7IqlkZnND1F8vZHC31bVnorlybp4BLaEDrih aVHgjYgeeWDg2Gk3YdGcjNGgLGIrYgrVG/o2ZHeBr229ek8PoMdDEx6KeQaSwPk7G0lV 4NGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=dyQYjYh7dRDOyEWFSZNopEDBslszXUVFF9ptW3wl9h0=; b=YGHxjSczPZRu1jUFPqiC/4wlntDMNTKE3263E2D8SZqFCLGX65AAaMvtogCthw1gmE SVzyQKbsR/tOs/ho7NhutNZnlmDsoMLUDO1sJunAONh5XTD2rd2TK+OxDp8tr6JmN26S DjjLKXCyoMaIuhBW4sugSF3I3X5Och2hdgEPzWKT/gce6kASR9TRJr5chKcwISe7uYVY OR3LFQXR8nkmJbSWlIt2wYkNIsmz9YTAMkZ4BC1qMTGNaGqsUe/jG2+o1RxED/DqhgaH 6obZ1xgTs2jIoRK3thF+RuY/RT8lfvYOW6xI76WDkXLOchz3sRRTPuNRqNGhk8sQV5xa NRrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Z16Y+Sd5; 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=pass (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 d13si8761351ejo.704.2022.02.01.07.07.55; Tue, 01 Feb 2022 07:08:22 -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; dkim=pass header.i=@intel.com header.s=Intel header.b=Z16Y+Sd5; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356647AbiA3VYT (ORCPT + 99 others); Sun, 30 Jan 2022 16:24:19 -0500 Received: from mga06.intel.com ([134.134.136.31]:52048 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356536AbiA3VWB (ORCPT ); Sun, 30 Jan 2022 16:22:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577721; x=1675113721; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=JTPyM8ZAgyeb9WCq9+VH3FXgpAXnWn1PijDxlRo5/Us=; b=Z16Y+Sd5+dXMjzc3qsc/NGUN8jNBXJx1Uph7z6lKaFV3BvoQFrW+ZCl5 yOyXx45vmQzHVHAUjkATda6+Oequ09mOqPoXZpWXLDAYtnOlGGiXXon3/ 8H+Shnm2XjsSKhDfN5MxDIE/7ppqkb5I09oMZuirvLxrIc4EL/kJrYXpx bHgxcpUcHRAafF+MCp1pVVL9bfu9ZusrvyCniqbpHWqb3gSKpNnWOxwYx pnQLBiOUzVe9TJL2/dMIg6vsjSpxjEz6iGxLKotF4ufe9odt26iK9DbCw ya9CkKJaa7pH4dmngXs84Pndey8oAe7g4arM+NPxZVwB1Mht+2GKtiVwO Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104942" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104942" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:22:00 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856812" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:59 -0800 From: Rick Edgecombe 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" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH 18/35] mm: Add guard pages around a shadow stack. Date: Sun, 30 Jan 2022 13:18:21 -0800 Message-Id: <20220130211838.8382-19-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu-cheng Yu 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 Signed-off-by: Rick Edgecombe Cc: Kees Cook --- Yu-cheng v25: - Move SHADOW_STACK_GUARD_GAP to arch/x86/mm/mmap.c. Yu-cheng 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 b3cb3a17037b..e125358d7f75 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2797,6 +2797,7 @@ struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr) 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; @@ -2808,7 +2809,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; @@ -2820,6 +2823,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.17.1