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 808A4C433F5 for ; Mon, 14 Mar 2022 07:01:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEFAD8D0002; Mon, 14 Mar 2022 03:01:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9E838D0001; Mon, 14 Mar 2022 03:01:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C66A78D0002; Mon, 14 Mar 2022 03:01:48 -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 B73948D0001 for ; Mon, 14 Mar 2022 03:01:48 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 83DEB12146E for ; Mon, 14 Mar 2022 07:01:48 +0000 (UTC) X-FDA: 79242096696.05.6B35280 Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) by imf23.hostedemail.com (Postfix) with ESMTP id F11D9140029 for ; Mon, 14 Mar 2022 07:01:47 +0000 (UTC) Received: by mail-yb1-f172.google.com with SMTP id l2so28815500ybe.8 for ; Mon, 14 Mar 2022 00:01:47 -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=srqCigb1vWPaMd3DOBZQqhVr8vvkskgf2mmVXi17bKI=; b=YVTqjtBDJ4WxcYfcPezyITAxXd48JqxPEwgMQyEGSzaVTVXoDmRLS+TkZwcU9Hv26x ueylOT3rjV6u/WF2Trf3UrC1cp5DZ3nvYED3Wi4baWQHdmCA5pgs4YbA1FOMxFmCLzeD M7VTEmifPzoiWme/HRy6qhNLZPZY6d7W//fyHqBLaFy8JsdWi1m9sxZQX9TJ2RIVD/0y OyPmw6Um4C7+dejS8GjBPBoBVSEcu8T/i2aWZ/92+nSbuofOAX3pJGzpqQLKlJMeJ8Na dN2nbqPUAtHSnYliwdD4kU8GbxMThMMyd7Hw1DzkSI6nh2tzXgTGOpsXlgTgXlBQszEE oWzA== 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=srqCigb1vWPaMd3DOBZQqhVr8vvkskgf2mmVXi17bKI=; b=AmcDsx+4Z4lr+BUL8BrHvs2eUUpOuifuu60vockCCfY2FFbkV7RxQF5aULv9KkL27F sx/ZHQmIgtiKpnglfH9aQo8mBlipG5NpSMHDAmV7HMYaNeLXR2u5xCbzs1+ncr4Njm3y 06qwL5gv7+457kOYGfVd1Z/ErjyQKVyXyOWqzattn8nTUSV2bclE7UjamG605yQnXqJ5 UGkWVufx/Z01Q1K+YTrLstO6ZENWkYR8D4jxW0u4gT42U3+QwSKEyu6WUisI10lMh6i3 qm5V8lUvY+GB/Qqfn8aJZLoOlD07/ZwS50lEuJtz8/I7HYXmDba6Gd8DHHRXo0C1Ywzw go9Q== X-Gm-Message-State: AOAM533NQ41GeWs4nj/vAg9LEq6uPr79zOvRuPFBRw6g1/YFw74LN9sY o2J25ipn+VdnyLp3wpeplSwgAh666+/vqhKrqsogXg== X-Google-Smtp-Source: ABdhPJzQ+CFHzpFTMac9G+rc/GRSCrnDKbAPdyk6SnRHXO0eMR1ouREWl48CurmtJzBA1/fJiSOAq0fhPWilSWzAlv4= X-Received: by 2002:a05:6902:24f:b0:62d:69d:c9fc with SMTP id k15-20020a056902024f00b0062d069dc9fcmr14878938ybs.87.1647241306986; Mon, 14 Mar 2022 00:01:46 -0700 (PDT) MIME-Version: 1.0 References: <57133fafc4d74377a4a08d98e276d58fe4a127dc.1647115974.git.andreyknvl@google.com> In-Reply-To: <57133fafc4d74377a4a08d98e276d58fe4a127dc.1647115974.git.andreyknvl@google.com> From: Marco Elver Date: Mon, 14 Mar 2022 08:00:00 +0100 Message-ID: Subject: Re: [PATCH] kasan, scs: collect stack traces from shadow stack To: andrey.konovalov@linux.dev Cc: Alexander Potapenko , Andrew Morton , Andrey Konovalov , Dmitry Vyukov , 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-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: F11D9140029 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=YVTqjtBD; spf=pass (imf23.hostedemail.com: domain of elver@google.com designates 209.85.219.172 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Stat-Signature: 8rqb9bd555b8aeg151yf37ryatrh3mnn X-HE-Tag: 1647241307-288006 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 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. This is impressive. > 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. Why is stack_trace_consume_fn required? This is an internal detail of arch_stack_walk(), but to implement stack_trace_save_shadow() that's not used at all. I think having stack_trace_save_shadow() as you have implemented in kernel/stacktrace.c or simply in kernel/scs.c itself would be appropriate. > 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. I think SCS should just work - and if it doesn't, can we fix it? It is unclear to me what would be a deciding factor to choose between stack trace collection modes, since it is hard to quantify when and if SCS doesn't work as intended. So I fear it'd just be an option that's never used because we don't understand when it's required to be used. > 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)) This should go into arch/arm64/include/asm/kasan.h, and here it should then just do #ifndef PAC_TAG_RESET #define ... > +#else > +#define PAC_TAG_RESET(x) (x) > +#endif But perhaps there's a better, more generic location for this macro? > +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); > } > > -- > 2.25.1 >