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=-14.2 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,UNWANTED_LANGUAGE_BODY,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 E8762C64E7B for ; Mon, 30 Nov 2020 11:45:50 +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 51A89206C0 for ; Mon, 30 Nov 2020 11:45:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="0xDfu67f"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="0Z/mBgI9" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 51A89206C0 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=YHJ/o3N3SedQKKFfFPuCxG3uvZrt04zNmwmEkpibxHk=; b=0xDfu67ftHm7qIpYvGUopkgzH ncGux8dN/nHX9Apj77Mk2IClRO+xVBbPAgOq/5Ipk4Nts2dnAcY5rYvkn1G74flpWfsMtyRkdmJ5X l3YjDVYilLQniXn1k9ZURHsPIbO0pfWJqKPcD/QZ8ileptaIlswtbTPIkAR9biKHMEZh6B/nnouTA z5lXrUCNIMmUni1rYAP9sBdKVRyzc9PWh4MINSBIwaIBqQVelOvSbSu9pjg/etPXUWz4IUpr4TUR4 um4+vRws1rThaIx1aHU17RZLmiZ51Nj2AAszKHYSfIIVC6yNOI3EYkfMD71cQNUSJS5ZXeu1gKsKQ GUozxLXSA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kjhbX-0006fS-Q3; Mon, 30 Nov 2020 11:44:27 +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 1kjhbT-0006es-LJ for linux-arm-kernel@lists.infradead.org; Mon, 30 Nov 2020 11:44:25 +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 EA195206C0; Mon, 30 Nov 2020 11:44:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606736662; bh=q82LeZLj+xOvMtauyebaFwznOwFNc6Yx8wgfJmNh8SY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0Z/mBgI9D5ebSNshYuyVnBmQL9bcEj5x5NEnO8qrexx73of1xIse9RjSJSGMw+lJ8 l0jG0FIjeLqPKjOIcaY0LGYz2SI7UnHEdSaVnz3+XTLAEWWbz3IX3vvn5VFpqPDN32 fsmvTEcJMbKZ/czI5OUdOXrtKKb3V9uPspKppI7g= Date: Mon, 30 Nov 2020 11:44:17 +0000 From: Will Deacon To: Sami Tolvanen Subject: Re: [PATCH v2 1/2] scs: switch to vmapped shadow stacks Message-ID: <20201130114417.GA24563@willie-the-truck> References: <20201124195940.27061-1-samitolvanen@google.com> <20201124195940.27061-2-samitolvanen@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20201124195940.27061-2-samitolvanen@google.com> 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-20201130_064424_481845_1EB4D65C X-CRM114-Status: GOOD ( 27.23 ) 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 , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org 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 On Tue, Nov 24, 2020 at 11:59:39AM -0800, Sami Tolvanen wrote: > The kernel currently uses kmem_cache to allocate shadow call stacks, > which means an overflows 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 for > tasks, 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 | 12 ++++----- > kernel/scs.c | 66 +++++++++++++++++++++++++++++++++++++-------- > 2 files changed, 61 insertions(+), 17 deletions(-) > > diff --git a/include/linux/scs.h b/include/linux/scs.h > index 6dec390cf154..2a506c2a16f4 100644 > --- a/include/linux/scs.h > +++ b/include/linux/scs.h > @@ -15,12 +15,8 @@ > > #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_ORDER 0 > +#define SCS_SIZE (PAGE_SIZE << SCS_ORDER) > #define GFP_SCS (GFP_KERNEL | __GFP_ZERO) > > /* An illegal pointer value to mark the end of the shadow stack. */ > @@ -33,6 +29,8 @@ > #define task_scs(tsk) (task_thread_info(tsk)->scs_base) > #define task_scs_sp(tsk) (task_thread_info(tsk)->scs_sp) > > +void *scs_alloc(int node); > +void scs_free(void *s); > void scs_init(void); > int scs_prepare(struct task_struct *tsk, int node); > void scs_release(struct task_struct *tsk); > @@ -61,6 +59,8 @@ static inline bool task_scs_end_corrupted(struct task_struct *tsk) > > #else /* CONFIG_SHADOW_CALL_STACK */ > > +static inline void *scs_alloc(int node) { return NULL; } > +static inline void scs_free(void *s) {} > static inline void scs_init(void) {} > static inline void scs_task_reset(struct task_struct *tsk) {} > static inline int scs_prepare(struct task_struct *tsk, int node) { return 0; } > diff --git a/kernel/scs.c b/kernel/scs.c > index 4ff4a7ba0094..25b0dd5aa0e2 100644 > --- a/kernel/scs.c > +++ b/kernel/scs.c > @@ -5,50 +5,94 @@ > * 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)); > } > > -static void *scs_alloc(int node) > +/* Matches NR_CACHED_STACKS for VMAP_STACK */ > +#define NR_CACHED_SCS 2 > +static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]); > + > +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) { > + kasan_unpoison_vmalloc(s, SCS_SIZE); > + memset(s, 0, SCS_SIZE); > + goto out; > + } > + } > + > + s = __vmalloc_node_range(SCS_SIZE, 1, VMALLOC_START, VMALLOC_END, > + GFP_SCS, PAGE_KERNEL, 0, node, > + __builtin_return_address(0)); > > if (!s) > return NULL; Sorry I didn't spot this before, but if you put the xchg/vmalloc code into a new __scs_alloc() function then you can drop the label and this becomes: s = __scs_alloc(...); if (!s) return NULL; *__scs_maghic(s) = SCS_ENG_MAGIC; ... With that: Acked-by: Will Deacon Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel