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=-15.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 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 E9BCFC388F9 for ; Mon, 23 Nov 2020 11:08:57 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4443F20729 for ; Mon, 23 Nov 2020 11:08:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="d98Jzsx7"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="rhXxGAEb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4443F20729 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6np5viZmXF5BB+Byr0R+wd5WaMIvb2OVwOeT/vOInlU=; b=d98Jzsx79zDnV7GvEgqZJAXdz lzx443bd3vc24hcR9T8UbDal/qoM+qSniIOcufLXtZrOs8ME7U/iKX3YQA4bUjGPMTlANUfwNLCzc 9mW2I9hb4Zi1uvXp7dXSfEcwNl9NSaZvnjCy39FWUrSPnVqW3c7Dq/gv6sKApfsSIpjGF0xPRsoLJ L2g/tYR/ooY+ml/6x6MnGnDqZgxc88hj8tajMzB27C9AGaOKhOPPGhLTHhC+eBG7+sJvzEeMB3mIE 3KRRDqlZYQ3LVc8NUz21n1ouaWPCp3avPv2e9L9reHVSXM4IQCEYL+TLmf4Lgq7/RSq1cCUNUybnX nda24CTdQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kh9hl-00088b-TW; Mon, 23 Nov 2020 11:08:21 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kh9hj-00087t-93 for linux-arm-kernel@lists.infradead.org; Mon, 23 Nov 2020 11:08:20 +0000 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DC8A720729; Mon, 23 Nov 2020 11:08:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606129697; bh=gS/0Cekh6Y/5gZIeZ9vh/sMPzjZWtkJBhwJGULSFSMA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=rhXxGAEbbaL7W9J6ud28rQGntfJejURY7o3vGGegUMcg09E8b04Mg1x4Yb+Z1NZPQ 1C71E6E8cP8O0tWhEFhOFI1Gsorkkq7xbVqLqS66mFHhq8eDIMRGMZvidZNT9JEtx3 UVbnaKVCArWVQXJL8AP4mucjBRjLPdB8PJBfxrWk= Date: Mon, 23 Nov 2020 11:08:12 +0000 From: Will Deacon To: Sami Tolvanen Subject: Re: [PATCH 1/2] scs: switch to vmapped shadow stacks Message-ID: <20201123110811.GA9957@willie-the-truck> References: <20201022202355.3529836-1-samitolvanen@google.com> <20201022202355.3529836-2-samitolvanen@google.com> <20201119130036.GA4331@willie-the-truck> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201123_060819_480616_BCF8822F X-CRM114-Status: GOOD ( 36.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Kees Cook , Ard Biesheuvel , Catalin Marinas , LKML , James Morse , linux-arm-kernel Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Sami, On Fri, Nov 20, 2020 at 09:00:17AM -0800, Sami Tolvanen wrote: > On Thu, Nov 19, 2020 at 5:00 AM Will Deacon wrote: > > On Thu, Oct 22, 2020 at 01:23:54PM -0700, Sami Tolvanen wrote: > > > The kernel currently uses kmem_cache to allocate shadow call stacks, > > > which means an overflow may not be immediately detected and can > > > potentially result in another task's shadow stack to be overwritten. > > > > > > This change switches SCS to use virtually mapped shadow stacks, > > > which increases shadow stack size to a full page and provides more > > > robust overflow detection similarly to VMAP_STACK. > > > > > > Signed-off-by: Sami Tolvanen > > > --- > > > include/linux/scs.h | 7 +---- > > > kernel/scs.c | 63 ++++++++++++++++++++++++++++++++++++++------- > > > 2 files changed, 55 insertions(+), 15 deletions(-) > > > > Cheers for posting this. I _much_ prefer handling the SCS this way, but I > > have some comments on the implementation below. > > > > > diff --git a/include/linux/scs.h b/include/linux/scs.h > > > index 6dec390cf154..86e3c4b7b714 100644 > > > --- a/include/linux/scs.h > > > +++ b/include/linux/scs.h > > > @@ -15,12 +15,7 @@ > > > > > > #ifdef CONFIG_SHADOW_CALL_STACK > > > > > > -/* > > > - * In testing, 1 KiB shadow stack size (i.e. 128 stack frames on a 64-bit > > > - * architecture) provided ~40% safety margin on stack usage while keeping > > > - * memory allocation overhead reasonable. > > > - */ > > > -#define SCS_SIZE SZ_1K > > > +#define SCS_SIZE PAGE_SIZE > > > > We could make this SCS_ORDER and then forget about alignment etc. > > It's still convenient to have SCS_SIZE defined, I think. I can > certainly define SCS_ORDER and use that to define SCS_SIZE, but do you > think we'll need an order >0 here at some point in future? I'm not daft enough to comment on SCS size again ;) Let's define SCS_ORDER 0 and then SCS_SIZE in terms of that. > > > > #define GFP_SCS (GFP_KERNEL | __GFP_ZERO) > > > > > > /* An illegal pointer value to mark the end of the shadow stack. */ > > > diff --git a/kernel/scs.c b/kernel/scs.c > > > index 4ff4a7ba0094..2136edba548d 100644 > > > --- a/kernel/scs.c > > > +++ b/kernel/scs.c > > > @@ -5,50 +5,95 @@ > > > * Copyright (C) 2019 Google LLC > > > */ > > > > > > +#include > > > #include > > > #include > > > #include > > > -#include > > > +#include > > > #include > > > > > > -static struct kmem_cache *scs_cache; > > > - > > > static void __scs_account(void *s, int account) > > > { > > > - struct page *scs_page = virt_to_page(s); > > > + struct page *scs_page = vmalloc_to_page(s); > > > > > > mod_node_page_state(page_pgdat(scs_page), NR_KERNEL_SCS_KB, > > > account * (SCS_SIZE / SZ_1K)); > > > } > > > > > > +/* Matches NR_CACHED_STACKS for VMAP_STACK */ > > > +#define NR_CACHED_SCS 2 > > > +static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); > > > + > > > static void *scs_alloc(int node) > > > { > > > - void *s = kmem_cache_alloc_node(scs_cache, GFP_SCS, node); > > > + int i; > > > + void *s; > > > + > > > + for (i = 0; i < NR_CACHED_SCS; i++) { > > > + s = this_cpu_xchg(scs_cache[i], NULL); > > > + if (s) { > > > + memset(s, 0, SCS_SIZE); > > > + goto out; > > > + } > > > + } > > > + > > > + /* > > > + * We allocate a full page for the shadow stack, which should be > > > + * more than we need. Check the assumption nevertheless. > > > + */ > > > + BUILD_BUG_ON(SCS_SIZE > PAGE_SIZE);i > > > > With SCS_ORDER, you can drop this. > > > > > + > > > + s = __vmalloc_node_range(PAGE_SIZE, SCS_SIZE, > > > + VMALLOC_START, VMALLOC_END, > > > + GFP_SCS, PAGE_KERNEL, 0, > > > + node, __builtin_return_address(0)); > > > > Do we actually need vmalloc here? If we used alloc_pages() + vmap() > > Does it matter that vmap() always uses NUMA_NO_NODE? We'll also lose > the ability to use vfree_atomic() in scs_release() unless we use > VM_MAP_PUT_PAGES and allocate the page array passed to vmap() with > kvmalloc(), which I think we need to do to avoid sleeping in > scs_free(). Huh, I didn't realise we didn't have vunmap_atomic(). In which case, I take that back -- let's stick with vmalloc() for now. Cheers, Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel