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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3813EC433F5 for ; Mon, 14 Mar 2022 08:57:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 667648D0003; Mon, 14 Mar 2022 04:57:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 617958D0001; Mon, 14 Mar 2022 04:57:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 506088D0003; Mon, 14 Mar 2022 04:57:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 3D2E88D0001 for ; Mon, 14 Mar 2022 04:57:53 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 02A58228E0 for ; Mon, 14 Mar 2022 08:57:52 +0000 (UTC) X-FDA: 79242389226.03.444C314 Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) by imf22.hostedemail.com (Postfix) with ESMTP id 7CF25C001C for ; Mon, 14 Mar 2022 08:57:52 +0000 (UTC) Received: by mail-yb1-f176.google.com with SMTP id e186so29307126ybc.7 for ; Mon, 14 Mar 2022 01:57:52 -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=dnStb81zNfCbHNpdfKDavCPptqLqSaSsDRJFANgLx8A=; b=dq218i3qZDV4+B76aTfwkaH9pAlGjbYICkbByaYC8TXGnhwN6Ehv1mPsMhNyM9O8++ qvfNx5pKKzlTMNvwYIN55VhYlrepET0w3qSroHDLgagYw6yJZ/YakR4CykLuIhL3LrSF JrJEVzlId77OaIhjTUBslVlKJv4S7oqc6sySieHKCG34OCikFruQ9/H+HO0yqnIDvktS 5URQXk75ftEsml2eptXJ3rPy3obpqRDDEgcaawW2q/dqhVA1yepsxYYVhjH/szn2ORPb lzlKShszva657FhgQJR7T0zHCZPKAsh+gpEeYnp2R6tP4uoQM8OSWEB76QULmsZqMkMI 2Yfg== 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=dnStb81zNfCbHNpdfKDavCPptqLqSaSsDRJFANgLx8A=; b=v9O7qvYM4RbfZGQXgXp9A33JqbaTdrrrbG3VEzYN/Bzj6nqlQhI6NIz0n8XLZ6Fxnw mBIqouGJcGNzA03je9CvZX53Ycnr2yYrkZiM15djOqaMq80w9dr0TLzYX7oMGo0+O+Q6 7ZG+udGGtEv55e0oTjh6dSiAe5Wwi5nNLgad2HT8gQHgoEGDBUL9MOj7F4ce+9aBW1Vi KjisxEPY5plOaMW2vXqNo2U5gzs7mOtqzWgiPAQFBmMSZ83rG8Q5YEZXdWSSFDnXHRhH /J0YRipnUtpa5n+pdmC2QzcWE4j/XIpT6ZcSCbqeyQ2sAtGVh/Gi5K/E8vKBJU0j/HP8 C6lw== X-Gm-Message-State: AOAM530I8xcuCefVI/xmHY7CbhQ2b5CE3nYyzaQ5uAKEOwOokqMiY7aO 3V/0+Wsg5M+srJEjsjXhs7EGlisw6/2w3aWFEFpCfQ== X-Google-Smtp-Source: ABdhPJxuxOY6aZUyjBnh7cuSqx9wWpYORShFAkS3wOwBe4x0wT3pJ/53NQryS1a+JqhW0WBAjH/EmcMgDm7r/a2nfls= X-Received: by 2002:a25:6994:0:b0:629:1e05:b110 with SMTP id e142-20020a256994000000b006291e05b110mr17250131ybc.425.1647248271489; Mon, 14 Mar 2022 01:57:51 -0700 (PDT) MIME-Version: 1.0 References: <57133fafc4d74377a4a08d98e276d58fe4a127dc.1647115974.git.andreyknvl@google.com> In-Reply-To: From: Marco Elver Date: Mon, 14 Mar 2022 09:57:15 +0100 Message-ID: Subject: Re: [PATCH] kasan, scs: collect stack traces from shadow stack To: Andrey Konovalov Cc: andrey.konovalov@linux.dev, Alexander Potapenko , Andrew Morton , Dmitry Vyukov , Andrey Ryabinin , kasan-dev , Vincenzo Frascino , Catalin Marinas , Will Deacon , Mark Rutland , Sami Tolvanen , Peter Collingbourne , Evgenii Stepanov , Linux Memory Management List , LKML , Andrey Konovalov , Florian Mayer Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 7CF25C001C X-Stat-Signature: typzphehqd8bbamdmcr3tey3mijux9o9 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dq218i3q; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of elver@google.com designates 209.85.219.176 as permitted sender) smtp.mailfrom=elver@google.com X-HE-Tag: 1647248272-94883 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, 14 Mar 2022 at 00:44, Andrey Konovalov wrote: > > On Sat, Mar 12, 2022 at 9:14 PM 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); > > + 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); > > Another option here is to instruct stack depot to get the stack from > the Shadow Call Stack. This would avoid copying the frames twice. Yes, I think a stack_depot_save_shadow() would be appropriate if it saves a copy.