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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6345AC433EF for ; Wed, 13 Apr 2022 19:28:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238353AbiDMTao (ORCPT ); Wed, 13 Apr 2022 15:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238349AbiDMTal (ORCPT ); Wed, 13 Apr 2022 15:30:41 -0400 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50B5F73071 for ; Wed, 13 Apr 2022 12:28:19 -0700 (PDT) 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=1649878096; 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: in-reply-to:in-reply-to:references:references; bh=1W/iPGxZZe0dTxtRIT/L4inWkaCNOxDFyHeQ0vhNfa0=; b=gnwkJMKPPygIVkZ/9Wh1mh3OT1cMRRjuS5qS8IK6a6j48F8Mu3vVEr6wfetKEVn+G0kwVH VpnSFOGe6lHZ16nJIfVYj83+P9+IFGSaJ9oP5g4LZ79gI0ZQ4hAcFfPGXw47pP2GkGazjs X7uQvydkAJIrVzsEfjwJGtTH9md0qaI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko , Mark Rutland Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Catalin Marinas , Will Deacon , Vincenzo Frascino , Sami Tolvanen , linux-arm-kernel@lists.infradead.org, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v3 2/3] kasan, arm64: implement stack_trace_save_shadow Date: Wed, 13 Apr 2022 21:26:45 +0200 Message-Id: <78cd352296ceb14da1d0136ff7d0a6818e594ab7.1649877511.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrey Konovalov Implement stack_trace_save_shadow() that collects stack traces based on the Shadow Call Stack (SCS) for arm64 by copiing the frames from SCS. The implementation is best-effort and thus has limitations. stack_trace_save_shadow() fully handles task and softirq contexts, which are both processed on the per-task SCS. For hardirqs, the support is limited: stack_trace_save_shadow() does not collect the task part of the stack trace. For KASAN, this is not a problem, as stack depot only saves the interrupt part of the stack anyway. Otherwise, stack_trace_save_shadow() also takes a best-effort approach with a focus on performance. Thus, it: - Does not try to collect stack traces from other exceptions like SDEI. - Does not try to recover frames modified by KRETPROBES or by FTRACE. However, stack_trace_save_shadow() does strip PTR_AUTH tags to avoid leaking them in stack traces. The -ENOSYS return value is deliberatly used to match stack_trace_save_tsk_reliable(). Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index d9079ec11f31..23b30fa6e270 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -30,6 +30,68 @@ #include "kasan.h" #include "../slab.h" +#ifdef CONFIG_SHADOW_CALL_STACK +#include +#include + +/* + * Collect the stack trace from the Shadow Call Stack in a best-effort manner: + * + * - Do not collect the task part of the stack trace when in a hardirq. + * - Do not collect stack traces from other exception levels like SDEI. + * - Do not recover frames modified by KRETPROBES or by FTRACE. + * + * Note that marking the function with __noscs leads to unnacceptable + * performance impact, as helper functions stop being inlined. + */ +static inline int stack_trace_save_shadow(unsigned long *store, + unsigned int size) +{ + unsigned long *scs_top, *scs_base, *frame; + unsigned int len = 0; + + /* Get the SCS base. */ + if (in_task() || in_serving_softirq()) { + /* Softirqs reuse the task SCS area. */ + scs_base = task_scs(current); + } else if (in_hardirq()) { + /* Hardirqs use a per-CPU SCS area. */ + scs_base = *this_cpu_ptr(&irq_shadow_call_stack_ptr); + } else { + /* Ignore other exception levels. */ + return 0; + } + + /* + * Get the SCS pointer. + * + * Note that this assembly might be placed before the function's + * prologue. In this case, the last stack frame will be lost. This is + * acceptable: the lost frame will correspond to an internal KASAN + * function, which is not relevant to identify the external call site. + */ + asm volatile("mov %0, x18" : "=&r" (scs_top)); + + /* The top SCS slot is empty. */ + scs_top -= 1; + + for (frame = scs_top; frame >= scs_base; frame--) { + if (len >= size) + break; + /* Do not leak PTR_AUTH tags in stack traces. */ + store[len++] = ptrauth_strip_insn_pac(*frame); + } + + return len; +} +#else /* CONFIG_SHADOW_CALL_STACK */ +static inline int stack_trace_save_shadow(unsigned long *store, + unsigned int size) +{ + return -ENOSYS; +} +#endif /* CONFIG_SHADOW_CALL_STACK */ + depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) { unsigned long entries[KASAN_STACK_DEPTH]; -- 2.25.1 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7DA9BC433EF for ; Wed, 13 Apr 2022 19:45:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=POC/SDjLdNT+NossS15irMFPLFE0DObUepXV4jA9Ro4=; b=W3YtUppCFme6ME hrgR+O7F1D0S6C7ZSu7LXzNwjwJkPS4pq7aDUv3Bsz62/jZ1d5avLPlcEgsEoAcReIkhFcOmrioYf cN4tVNOeq65Qf9qSln6O0wI58KCTmj0JTS/DB0SGZ4E9QXbgWFp/cql/u3e7lvXE2Jn6YkJY2Bw3O mEMSIKYkdCa0rRkdyRyuZy69gc/IPnTGRjXYGjTX6OK/oQze/jlesTEm0lLgWF7lCj8Y5ueirGpAL BMXa+wv1FUYk2JEXM6j4r/8rX38NWNTv1cWFPEbzu2+7PcPk1JtJd8Ksfo/9qhS5KKT0SQddvG0Pk YEZAtRTVaHQMeQhIzkNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neiu3-002Y1M-8d; Wed, 13 Apr 2022 19:43:47 +0000 Received: from out2.migadu.com ([2001:41d0:2:aacc::]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1neif7-002ROj-70 for linux-arm-kernel@lists.infradead.org; Wed, 13 Apr 2022 19:28:23 +0000 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=1649878096; 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: in-reply-to:in-reply-to:references:references; bh=1W/iPGxZZe0dTxtRIT/L4inWkaCNOxDFyHeQ0vhNfa0=; b=gnwkJMKPPygIVkZ/9Wh1mh3OT1cMRRjuS5qS8IK6a6j48F8Mu3vVEr6wfetKEVn+G0kwVH VpnSFOGe6lHZ16nJIfVYj83+P9+IFGSaJ9oP5g4LZ79gI0ZQ4hAcFfPGXw47pP2GkGazjs X7uQvydkAJIrVzsEfjwJGtTH9md0qaI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko , Mark Rutland Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Catalin Marinas , Will Deacon , Vincenzo Frascino , Sami Tolvanen , linux-arm-kernel@lists.infradead.org, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v3 2/3] kasan, arm64: implement stack_trace_save_shadow Date: Wed, 13 Apr 2022 21:26:45 +0200 Message-Id: <78cd352296ceb14da1d0136ff7d0a6818e594ab7.1649877511.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220413_122821_606697_5B6C955D X-CRM114-Status: GOOD ( 17.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 From: Andrey Konovalov Implement stack_trace_save_shadow() that collects stack traces based on the Shadow Call Stack (SCS) for arm64 by copiing the frames from SCS. The implementation is best-effort and thus has limitations. stack_trace_save_shadow() fully handles task and softirq contexts, which are both processed on the per-task SCS. For hardirqs, the support is limited: stack_trace_save_shadow() does not collect the task part of the stack trace. For KASAN, this is not a problem, as stack depot only saves the interrupt part of the stack anyway. Otherwise, stack_trace_save_shadow() also takes a best-effort approach with a focus on performance. Thus, it: - Does not try to collect stack traces from other exceptions like SDEI. - Does not try to recover frames modified by KRETPROBES or by FTRACE. However, stack_trace_save_shadow() does strip PTR_AUTH tags to avoid leaking them in stack traces. The -ENOSYS return value is deliberatly used to match stack_trace_save_tsk_reliable(). Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index d9079ec11f31..23b30fa6e270 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -30,6 +30,68 @@ #include "kasan.h" #include "../slab.h" +#ifdef CONFIG_SHADOW_CALL_STACK +#include +#include + +/* + * Collect the stack trace from the Shadow Call Stack in a best-effort manner: + * + * - Do not collect the task part of the stack trace when in a hardirq. + * - Do not collect stack traces from other exception levels like SDEI. + * - Do not recover frames modified by KRETPROBES or by FTRACE. + * + * Note that marking the function with __noscs leads to unnacceptable + * performance impact, as helper functions stop being inlined. + */ +static inline int stack_trace_save_shadow(unsigned long *store, + unsigned int size) +{ + unsigned long *scs_top, *scs_base, *frame; + unsigned int len = 0; + + /* Get the SCS base. */ + if (in_task() || in_serving_softirq()) { + /* Softirqs reuse the task SCS area. */ + scs_base = task_scs(current); + } else if (in_hardirq()) { + /* Hardirqs use a per-CPU SCS area. */ + scs_base = *this_cpu_ptr(&irq_shadow_call_stack_ptr); + } else { + /* Ignore other exception levels. */ + return 0; + } + + /* + * Get the SCS pointer. + * + * Note that this assembly might be placed before the function's + * prologue. In this case, the last stack frame will be lost. This is + * acceptable: the lost frame will correspond to an internal KASAN + * function, which is not relevant to identify the external call site. + */ + asm volatile("mov %0, x18" : "=&r" (scs_top)); + + /* The top SCS slot is empty. */ + scs_top -= 1; + + for (frame = scs_top; frame >= scs_base; frame--) { + if (len >= size) + break; + /* Do not leak PTR_AUTH tags in stack traces. */ + store[len++] = ptrauth_strip_insn_pac(*frame); + } + + return len; +} +#else /* CONFIG_SHADOW_CALL_STACK */ +static inline int stack_trace_save_shadow(unsigned long *store, + unsigned int size) +{ + return -ENOSYS; +} +#endif /* CONFIG_SHADOW_CALL_STACK */ + depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) { unsigned long entries[KASAN_STACK_DEPTH]; -- 2.25.1 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel