Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2794877pxp; Mon, 14 Mar 2022 05:14:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyoahunNxJOAzdCgswzsIZCMDuDmZf98AQob+UavbSuV43LtzPmwrCZzoq3nXMufgzHaInB X-Received: by 2002:a17:907:86a3:b0:6da:870c:af44 with SMTP id qa35-20020a17090786a300b006da870caf44mr18191477ejc.445.1647260071296; Mon, 14 Mar 2022 05:14:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647260071; cv=none; d=google.com; s=arc-20160816; b=DY4pozpVpB/Squhu0VM/kQHKypQS0SH9gw/stuRRZqlcXbzJQdl7DR4/LZDuu94dog H/iCLTAmMpQPkdMIc8xehuXvXhDuMzaScejqAfZnqJetPaeqNs/HMEGXhwvvfJha++ic yHwjMmaKvPlxbcpxHBEMvXui3u9sPLNaQBtqxRLtOu92oecKVNnV47BOYhkNqgyRfUvo gilkjHEU+N3j5qeYcdQKIdwxH1RpweevZRlb42nhnllymHOaqmZ7mEe/Xzs0mQpU2a4F XHkivmdcQma1jJY3d2x4eA/2m4vHCnG6pq/LL2G63z97bI5Z6kxshKkKY+sC5okvfD31 rW6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=jHOQpg8FkWZ2D9kBQlZqUsUw9/chXLkk2DroGgT0P6g=; b=0IhhQljjWf46kVaePFWW8LDbswGl35rVF/kcQOaZ6sIsqSxvTa3oRo5KEJh+nUOd6v n6etmfaNp6SN1WsiioWmQ4BtRe/NfrAZY4qHMvI6R8IjfiU1zG1pGodLASjAzpiJLyg7 VzbwVqZTj+SQE8/pR1WPmPe8HEwDhN8/zX2P2K8rKKd8XZmAq3PsPYzQf460w6LGm8kO QbIlnFd9Xi6cSwTDc+p6+Jv+xvJNMP3zyy28Rew+eSoJNoMyAkmqd/ySXdneNXYTcoLc 6gcCHLj9HC56ZOcEylyQOHAfqKHBm0yJgSz0VnTj9Zs+U80UYmpHosiltZ678DlZbF1x s2mA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=D8uyMk8E; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b9-20020a50e389000000b0041606325b11si11127452edm.541.2022.03.14.05.14.04; Mon, 14 Mar 2022 05:14:31 -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=@google.com header.s=20210112 header.b=D8uyMk8E; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236571AbiCNHTJ (ORCPT + 99 others); Mon, 14 Mar 2022 03:19:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235773AbiCNHTH (ORCPT ); Mon, 14 Mar 2022 03:19:07 -0400 Received: from mail-oo1-xc33.google.com (mail-oo1-xc33.google.com [IPv6:2607:f8b0:4864:20::c33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46F8F12605 for ; Mon, 14 Mar 2022 00:17:57 -0700 (PDT) Received: by mail-oo1-xc33.google.com with SMTP id h16-20020a4a6f10000000b00320507b9ccfso19055615ooc.7 for ; Mon, 14 Mar 2022 00:17:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jHOQpg8FkWZ2D9kBQlZqUsUw9/chXLkk2DroGgT0P6g=; b=D8uyMk8EnZyrQebikWLVsUrSAQ3/m4z2B+4pBphbGlSeumCFVFgChQOnndtr+hsxbs glD2lvbM8w/3KYaxR4/C6UY0oPJkjJMjjZ6+JOcTmE9DFfQ0OmC9vYHb/mfDdSn7g0pA 7ZRHdmeFlBoEKnuzQ1RujYEop8rvsU5L/lUdlzq50s49dKp7iL1tfGl9bHfjEjftI2Ea Hbd1fo1LqSgTkO8bIWg+Ldfl3m0Hs9Sm0v+iFY3CKi754l2YSKRJU3j88E422TsRYu6D euIIE/UdykRfsOsp4T7Ev9LSQhzfnIchrMKzYeCg635pg5kOtMHrkurw0DIf5cQLp9J6 cUEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jHOQpg8FkWZ2D9kBQlZqUsUw9/chXLkk2DroGgT0P6g=; b=CCxDSZf0a4HCibQxsQ//Hf/f18VhCriMsNh6ppM3DvIxxWOQDOk7RDjlFjLHAKDq00 0ebIDwhdia1zyb11QmbrgArbSCfQcEjqUHyaTXJ1/qZixv8tMud2LC2aFQHX3hKXMhkB LCTgNvNTot/iwOdKcsuDwl+sbJSZrlcN0J4gmYq+1Zoi7wAUbHRyokL+cuFmrMW0pOgX YBIJ3sAZ7j2yHoaFwkvGwj5O+0GGGVsuLcmd1jLWcV9pQ5yIFhUnwdek/Tszy3X6RINt zh6SCDMTFCZWZmd3vOqyWvy+nqrMLLii3NKbGOnYuru/Gz3N3SRL3lYzrMPW95x/o1dd b57A== X-Gm-Message-State: AOAM531n92chH0IemyvF/X0l9JifmnZ+yVOl+u1RGWzTWtbpIZJw2Eaq BwYA9lwSDNoPychB7KfXZeFA5o300G2QUgrOBMv3dg== X-Received: by 2002:a05:6870:9619:b0:d9:a25e:ed55 with SMTP id d25-20020a056870961900b000d9a25eed55mr10310764oaq.163.1647242276344; Mon, 14 Mar 2022 00:17:56 -0700 (PDT) MIME-Version: 1.0 References: <57133fafc4d74377a4a08d98e276d58fe4a127dc.1647115974.git.andreyknvl@google.com> In-Reply-To: <57133fafc4d74377a4a08d98e276d58fe4a127dc.1647115974.git.andreyknvl@google.com> From: Dmitry Vyukov Date: Mon, 14 Mar 2022 08:17:45 +0100 Message-ID: Subject: Re: [PATCH] kasan, scs: collect stack traces from shadow stack To: andrey.konovalov@linux.dev Cc: Marco Elver , Alexander Potapenko , Andrew Morton , Andrey Konovalov , Andrey Ryabinin , kasan-dev@googlegroups.com, Vincenzo Frascino , Catalin Marinas , Will Deacon , Mark Rutland , Sami Tolvanen , Peter Collingbourne , Evgenii Stepanov , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Sat, 12 Mar 2022 at 21:14, wrote: > > From: Andrey Konovalov > > Currently, KASAN always uses the normal stack trace collection routines, > which rely on the unwinder, when saving alloc and free stack traces. > > Instead of invoking the unwinder, collect the stack trace by copying > frames from the Shadow Call Stack whenever it is enabled. This reduces > boot time by 30% for all KASAN modes when Shadow Call Stack is enabled. > > To avoid potentially leaking PAC pointer tags, strip them when saving > the stack trace. > > Signed-off-by: Andrey Konovalov > > --- > > Things to consider: > > We could integrate shadow stack trace collection into kernel/stacktrace.c > as e.g. stack_trace_save_shadow(). However, using stack_trace_consume_fn > leads to invoking a callback on each saved from, which is undesirable. > The plain copy loop is faster. > > We could add a command line flag to switch between stack trace collection > modes. I noticed that Shadow Call Stack might be missing certain frames > in stacks originating from a fault that happens in the middle of a > function. I am not sure if this case is important to handle though. > > Looking forward to thoughts and comments. > > Thanks! > > --- > mm/kasan/common.c | 36 +++++++++++++++++++++++++++++++++++- > 1 file changed, 35 insertions(+), 1 deletion(-) > > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index d9079ec11f31..65a0723370c7 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -9,6 +9,7 @@ > * Andrey Konovalov > */ > > +#include > #include > #include > #include > @@ -21,6 +22,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -30,12 +32,44 @@ > #include "kasan.h" > #include "../slab.h" > > +#ifdef CONFIG_SHADOW_CALL_STACK > + > +#ifdef CONFIG_ARM64_PTR_AUTH > +#define PAC_TAG_RESET(x) (x | GENMASK(63, CONFIG_ARM64_VA_BITS)) > +#else > +#define PAC_TAG_RESET(x) (x) > +#endif > + > +static unsigned int save_shadow_stack(unsigned long *entries, > + unsigned int nr_entries) > +{ > + unsigned long *scs_sp = task_scs_sp(current); > + unsigned long *scs_base = task_scs(current); Just to double-check: interrupt frames are also appended to the the current task buffer, right? > + unsigned long *frame; > + unsigned int i = 0; > + > + for (frame = scs_sp - 1; frame >= scs_base; frame--) { > + entries[i++] = PAC_TAG_RESET(*frame); > + if (i >= nr_entries) > + break; > + } > + > + return i; > +} > +#else /* CONFIG_SHADOW_CALL_STACK */ > +static inline unsigned int save_shadow_stack(unsigned long *entries, > + unsigned int nr_entries) { return 0; } > +#endif /* CONFIG_SHADOW_CALL_STACK */ > + > depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) > { > unsigned long entries[KASAN_STACK_DEPTH]; > unsigned int nr_entries; > > - nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); > + if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) > + nr_entries = save_shadow_stack(entries, ARRAY_SIZE(entries)); > + else > + nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); > return __stack_depot_save(entries, nr_entries, flags, can_alloc); > } > > -- > 2.25.1 >