Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3274314rwb; Mon, 3 Oct 2022 12:16:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6ocuKOx/TbWdLKuXtdscGXCO4OpJsMj5HKDmJKXuKdD3JjotD9ZtVlrEbvto19dup4iMVg X-Received: by 2002:a17:902:ccc4:b0:17c:7cc1:a401 with SMTP id z4-20020a170902ccc400b0017c7cc1a401mr17744616ple.58.1664824593472; Mon, 03 Oct 2022 12:16:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664824593; cv=none; d=google.com; s=arc-20160816; b=u9Wm1eXlAPvNkg9vx6lbeiy//qXapKJkOpRYRLUOD3FGI62t15FINKBEiXdloRJNNv iYA+5XkfAN4MrtYS12Af2uw/cmuTn9izsm1OSbaBFE8HeDQ2KzZT3HePpe1eBtClS+Mi T7N5FDJ6Qlkpo7XnHhQvUtKFlGGhJVj6Q37QjmLmoXr0L9kHs+XDvzE5wLxIQbEcWvgy adxPwyrnrs2UCmo+uLiB5Ycb6h39wanzlA7nabaFt3C/MpmxKHMxKe6J53/qvnTwWfKW Q40eyhMN27owOuWxBUFkFEfIfu/xlq9UVwHyQrqZ3dugBTelUFImqP/i8xe80jLXKrY5 SLOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=yWFb7RgESUpR05JgfY3hGQPjFfcU1x/EUhBNi7FjnRo=; b=AALQnmgSjyNM+uoNs0AHvYk0MpW6ho2qW1Nc3aLjnpm0bezmcKt8BNUYyyHbAkH6LV MLEQh417KE6EHPKU8fZ4qq6xGuuK9DzS4lH9U/D2MnxPI9vh5cMN3QDZfIJSwx96M5nz hir9c96p+A0/BPxEtGwhKEjxq6FmSSe8I05gh5KAIbcXa1Ce8j0cHOK85zUWvQVCp/UZ /iKQnTbQLVp/a6YVhyE5ocpockor0BtR06n3zb4Lcu2zlwginG7iV2iWBxlRJ8lDvpFQ UqAom6WrYotZqmQJPgsJR3JxL0u+/qh4nNMW2rwj2/B+xs+5XUYH//2SkYzwpoMn3OsE /HvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PheLfVQN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a8-20020a056a000c8800b00552637c1283si11908513pfv.20.2022.10.03.12.16.16; Mon, 03 Oct 2022 12:16:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PheLfVQN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229774AbiJCSbG (ORCPT + 99 others); Mon, 3 Oct 2022 14:31:06 -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-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> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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