From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64668C433E5 for ; Mon, 22 Mar 2021 10:56:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 321F461931 for ; Mon, 22 Mar 2021 10:56:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230397AbhCVKz6 (ORCPT ); Mon, 22 Mar 2021 06:55:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230381AbhCVKzZ (ORCPT ); Mon, 22 Mar 2021 06:55:25 -0400 Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D745C061574 for ; Mon, 22 Mar 2021 03:55:24 -0700 (PDT) Received: by mail-lj1-x22c.google.com with SMTP id 16so20395476ljc.11 for ; Mon, 22 Mar 2021 03:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=D+PcY8fizKyI1pa8vyAaI0Fu5ahvlZoOnbR8AqNaIGM=; b=cNdFZe9aFiClr44vhnMkdDn4A4PRwCmXoJ8KEm1xluKWbN8IGn8mR3MlZg3J6rbUh5 pw68llA/oheOVpyvb+bbJ0sh4EVoe/nmWmGnzNtopCABnUJ7EQgOPtQSFTwM0Kzx8k+M +1mtp/2DwiZq5luOFZ3V/g01ge3sXb9SouwkCD0JH2lHd7gq1I7t86xU+J19KJyHmhmH LYNjOzP/9DVag/uyi8sMESQt8u8P6n+9arqOVmypRXuc6uC+TA53JscZSw5Cedl0Skih Uj5uIS/3Q3i8PmxUzCy71C2yDXfONCcAgKBAsNjfZlR3dTNNB6tHQ8+1nQKyAoEnlb/S 6OWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=D+PcY8fizKyI1pa8vyAaI0Fu5ahvlZoOnbR8AqNaIGM=; b=uiAvSn2U8YBUo9zmNutneZGAAZmer8F6SvOuGDoQEhSkux6tYGx58dOsovZ74w8b/l yWsvAS40Qm3KKp7TUFhVdjkxrJaw1D1pWkTecICy7si0QUuJogf3/k2Dw2Wluyj8Lok1 EUwNWYA6VzifiHk9tJCKAa5FGvIkdvgfAH3/1VKp7nj5b1/zztDAYgtaEC1/vr5imQXB Y0tnvGI946sVzvNFrNodiGXdJEipLXNNWNyAOy3cobylrI7qy4mtV9IrZgVD9c6O+/1K FHJRoibofDnp5h1XE7jqUgD7mRI3fsvA0eZJfqM2PP1sqpkYkHzMmHgTwi1spIZmWc93 PqQg== X-Gm-Message-State: AOAM531JdNMQoIin+1azIscjg1FzY6QrCfId7Rq80R+iK7nui+BwSp2/ 3XsjJhWtoj1ZMzyzAla3js7K3A== X-Google-Smtp-Source: ABdhPJxf/OAQj9ADevnB8ESViQBvohbq91TGiPa/7o4CoF/N6TtVkgXQQlUwXU07vQ7ubVRc+vrblQ== X-Received: by 2002:a2e:2a44:: with SMTP id q65mr9601149ljq.238.1616410523121; Mon, 22 Mar 2021 03:55:23 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id e9sm1905215ljj.52.2021.03.22.03.55.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 03:55:22 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id 36E96101DEB; Mon, 22 Mar 2021 13:55:30 +0300 (+03) Date: Mon, 22 Mar 2021 13:55:30 +0300 From: "Kirill A. Shutemov" To: Yu-cheng Yu Cc: 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 Subject: Re: [PATCH v23 17/28] mm: Add guard pages around a shadow stack. Message-ID: <20210322105530.pbmfrg6rhywct5ft@box> References: <20210316151054.5405-1-yu-cheng.yu@intel.com> <20210316151054.5405-18-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210316151054.5405-18-yu-cheng.yu@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 16, 2021 at 08:10:43AM -0700, Yu-cheng Yu wrote: > 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: Kees Cook > --- > 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 64297eabad63..23e3d880ce6c 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 af805ffde48e..9890e9f5a5e0 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -2619,6 +2619,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); > @@ -2651,9 +2655,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; Looks too x86-centric for generic code. Maybe we can have a helper that would return gap for the given VMA? The generic version of the helper would only return stack_guard_gap for VM_GROWSDOWN. Arch code would override it to handle VM_SHSTK case too. Similar can be done in vm_end_gap(). > + > + if (gap != 0) { > + vm_start -= gap; > if (vm_start > vma->vm_start) > vm_start = 0; > } > @@ -2663,9 +2673,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 > -- Kirill A. Shutemov