Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2452355pxp; Sun, 13 Mar 2022 19:01:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwROS8b/6+WeNJ4/WE6myFrXwat6bRyYgTvfVqOUcXMe/JpkuhT0/l8rbEI6LJpy6+szLPY X-Received: by 2002:a05:6402:50d0:b0:416:a2a6:5443 with SMTP id h16-20020a05640250d000b00416a2a65443mr18701035edb.410.1647223298669; Sun, 13 Mar 2022 19:01:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647223298; cv=none; d=google.com; s=arc-20160816; b=q4VCjDcKPf6i9yDBx0KYnWRhNwU7T555Oyby7i5TUEPssuqDDId8jBSamxkn6SR6A5 iGkXkQQy6TOZgKPgn+CTLTcKNW4eurGDluSvHhWzjMJzgk6JRBq4sIPVAjWKV5J+qwtK KfM0EDh+XmosPt7jmu8dv4TCWDInzLldCKxF4o0zQEI31VJZciPb91xk7OOx8fU0UxS0 okPpPekfS5sD6BZCmhH4FQhlqGO650QRdPhwj4keReI9ocpA5SwlkVz2sEBjZiWpAjnK CIbvqU1v8t4rFaEqg/JAwSYd6PIJEVvYsEzpt72716PuNec9NRzV4HFaALhmWYnvwyS/ jKwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=LmoxS08scyGyVZqa/3VyRO6NmjBbbNh8W/Hs8Ybg/AU=; b=CCVkXJ0o/qQFRsGrAun0uqAjpopB8Fa73dsbR0HUClSvgKFSHeJqYh1mLdgRtJTy/5 ED28upUJD6bcvgsgzjMKAfmtj0e/4qezfyfVYZOVZ1x12A2g2JcqsdKKpElH7AU3Fvix LlcOcAXx0y73aUwOuDh303lx7pWfS5NfKmWlwkk0HnVWBFBXjBaLEFnt7Pf18coMLqIP 1DFVP1jbP7ztvvbGC8nmItopBWGcw4CAAZk5kR2nWU+sBr/h5aCZxHf/CvAWAK2L1oEd M8hcxT2MSzf7dQiboUIUjGjz2vD+9SWsLq1/IVpppOn0F8xZTNhy3y3h6kwbJJT81nnF spcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=KWCmw9X7; 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=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b9-20020a056402278900b0041873a37fa4si2532306ede.202.2022.03.13.19.01.09; Sun, 13 Mar 2022 19:01:38 -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=@linux.dev header.s=key1 header.b=KWCmw9X7; 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=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232568AbiCLUPU (ORCPT + 99 others); Sat, 12 Mar 2022 15:15:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbiCLUPT (ORCPT ); Sat, 12 Mar 2022 15:15:19 -0500 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E4392C643 for ; Sat, 12 Mar 2022 12:14:13 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1647116050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=LmoxS08scyGyVZqa/3VyRO6NmjBbbNh8W/Hs8Ybg/AU=; b=KWCmw9X7waDLg1FZltp14KfIGyto7rGibooprLEDSWhgrgIdyyrss/jrmCWPML7qrv4h3m s0gyjJwZ6jZkTYilfgDgMqU7xbGhTb9UT4MrHm5Fnh6URXKOyMWzhaExVa9NgXSSccY2c9 CGZrl8lNiUsRHWHw2rTcoSdmyThnBrI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko , Andrew Morton Cc: 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 Subject: [PATCH] kasan, scs: collect stack traces from shadow stack Date: Sat, 12 Mar 2022 21:14:06 +0100 Message-Id: <57133fafc4d74377a4a08d98e276d58fe4a127dc.1647115974.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 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); } -- 2.25.1