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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A580BC433FE for ; Mon, 3 Oct 2022 18:31:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229946AbiJCSbI (ORCPT ); Mon, 3 Oct 2022 14:31:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229787AbiJCSbD (ORCPT ); Mon, 3 Oct 2022 14:31:03 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61D8930F69 for ; Mon, 3 Oct 2022 11:31:02 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id u24so3441948plq.12 for ; Mon, 03 Oct 2022 11:31:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=yWFb7RgESUpR05JgfY3hGQPjFfcU1x/EUhBNi7FjnRo=; b=PheLfVQNZdvrd76w9fTEkeWjqZZ7/2eEa6fQ0EZZSMfVfQPgF+/CqIcflCzBBHUFpw itUFINwrcHRurA8ZI8pf9m7E8cFXP7Qi00SMBeUYc0qkUTk+BhxAbx/wB1yEkS+j8nGw IzJhH5y7YxjK1FQ3v2cmRb2f3dqi1djliUovc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=yWFb7RgESUpR05JgfY3hGQPjFfcU1x/EUhBNi7FjnRo=; b=paLQ5XOqibRMQNovRGzMRt8VZcu3L/7p1a5okj8izBoD3eWZU3vu7uBfDd1SbyKUAM cKQGy6tDbw4gdMy44arF+TPk2XI6F3PxwNvoSzPNIq5xaZfzsUdshpQKzI91zFSyQIgc wa8vNS+rSuG2r9EiSAlFbx2NHNQV0M1gngxVGZdhbQRlK/mWsGt9au6xbSmtosB8InhV Bx0gbOxLxIQC3OF0zTCVT7AcVsn4rcQriBDtWS4V/ycVTO5LYhaP1hu071iU+DHkjcFL 7Hw19b1QrBnEk1NpaNMDD1FjTrx7WkI+KUTXOKs233keTpXMTJwocyswyq6cfEmgIri9 QyHA== X-Gm-Message-State: ACrzQf25jaFokU+9pv17qQOXHwIbZtldN2g6xfy0Nx6EksWoqb6I7wZf TDpuWsPrIejS4rzLS0bkSRXFOg== X-Google-Smtp-Source: AMsMyM7uo5Tzw/2Wd31Jjw44SLZYLBYBBrjxbdxXYvJpm9EoFdqt2quPyfPmA35slsvVMZOnqpgYmA== X-Received: by 2002:a17:90b:4ad1:b0:20a:b974:19a0 with SMTP id mh17-20020a17090b4ad100b0020ab97419a0mr3479373pjb.178.1664821861874; Mon, 03 Oct 2022 11:31:01 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id m7-20020a170902bb8700b0017bdfbfcf8dsm7500560pls.63.2022.10.03.11.31.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 11:31:00 -0700 (PDT) Date: Mon, 3 Oct 2022 11:30:59 -0700 From: Kees Cook To: Rick Edgecombe 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 , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, Yu-cheng Yu Subject: Re: [PATCH v2 18/39] mm: Add guard pages around a shadow stack. Message-ID: <202210031127.C6CF796@keescook> References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-19-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220929222936.14584-19-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 29, 2022 at 03:29:15PM -0700, Rick Edgecombe wrote: > [...] > +unsigned long stack_guard_start_gap(struct vm_area_struct *vma) > +{ > + if (vma->vm_flags & VM_GROWSDOWN) > + return stack_guard_gap; > + > + /* > + * 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. > + * > + * Creation of VM_SHADOW_STACK is tightly controlled, so a vma > + * can't be both VM_GROWSDOWN and VM_SHADOW_STACK > + */ Thank you for the details on how the size choice is made here! :) > diff --git a/include/linux/mm.h b/include/linux/mm.h > index fef14ab3abcb..09458e77bf52 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -2775,15 +2775,16 @@ struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr) > return vma; > } > > +unsigned long stack_guard_start_gap(struct vm_area_struct *vma); > + > static inline unsigned long vm_start_gap(struct vm_area_struct *vma) > { > + unsigned long gap = stack_guard_start_gap(vma); > unsigned long vm_start = vma->vm_start; > > - if (vma->vm_flags & VM_GROWSDOWN) { > - vm_start -= stack_guard_gap; > - if (vm_start > vma->vm_start) > - vm_start = 0; > - } > + vm_start -= gap; > + if (vm_start > vma->vm_start) > + vm_start = 0; > return vm_start; > } > > diff --git a/mm/mmap.c b/mm/mmap.c > index 9d780f415be3..f0d2e9143bd0 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -247,6 +247,13 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) > return origbrk; > } > I feel like something could be done with this definitions to make them inline, instead of __weak: #ifndef stack_guard_start_gap > +unsigned long __weak stack_guard_start_gap(struct vm_area_struct *vma) > +{ > + if (vma->vm_flags & VM_GROWSDOWN) > + return stack_guard_gap; > + return 0; > +} #endif And then move the x86 stack_guard_start_gap to a header? It's not exactly fast-path, but it feels a little weird. Regardlesss: Reviewed-by: Kees Cook -- Kees Cook