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 CDF49ECAAD5 for ; Sat, 10 Sep 2022 05:25:42 +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:Cc:To:From:Subject:Mime-Version: Message-Id:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=vIZstZK9hsSCVApuhNrXZTG3e42Pp8CNdbpgDJ1DFWU=; b=ODR rrkPC2E1QirwD8jAbEmUXVNIlNECld/5Z3THGbVoI8aTlfOV1rl6YuLAf2rpXGTP2akwmSV/iC8sX 6bn1GnJhr/P7nKj9Ette0kHMCj3R1lMGy5dBz6B01FRcnNnEHSKnEVfPcE8xfvWZxNDki9ma8oN6R E3Np/i6kbXpM2uUn1dhhc40m7nzQxuRZdbfAI1Wsx7VRaE3gHZBf5iz1ya+lsRwajpf4tOpKfpjV8 9ozESApVzCX90LndUL2uhjr8dzb84+y/6WLnmJ5vaMML3wb38SIYth/J/6MlKBq5kpyC7v8bX9ibL 6P+PTUIUWA8BrtjiAQg65t6D4larZ2Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oWsyv-006PLd-3U; Sat, 10 Sep 2022 05:24:41 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oWsys-006PIf-3l for linux-arm-kernel@lists.infradead.org; Sat, 10 Sep 2022 05:24:39 +0000 Received: by mail-yb1-xb49.google.com with SMTP id v5-20020a2583c5000000b006964324be8cso3309239ybm.14 for ; Fri, 09 Sep 2022 22:24:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:mime-version:message-id:date:from:to:cc:subject :date; bh=JqNTfVNEPEGXKtb8DKwwJsm47BWKuJspxKDX7cLnwDc=; b=Ig0ItrD1iczS7H38nUKbOU606qXdEQtNSM96un0caH//+ONB9TQpdDpE4eu82EJw7y WoAqBkZSgql1AIhXAHRL7nAVuXoNr/D/WzArKgxqAYDCA21hcIcs7v007A/nlNSwAIug 43TD4bxRN5G8PJU4kuGlp5EUqagVzBq+cFqCHZcMt8xxunmlJoHGKE/Gd+jKK2lrEWOe PcEVcU6aMmioTQDYc7UQl2k7aNfBJE6XGxsrV+z2GZ2E866ESFZMuHmDNB2z3OxagXPN NLgt2sDvVJ+9hmtGKDFuQWVRLh+pL9yV3ZmTl7VfAXmVLhNCp9Crm3doadgGMAqEyl27 Tezw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date; bh=JqNTfVNEPEGXKtb8DKwwJsm47BWKuJspxKDX7cLnwDc=; b=Ea7UYSR9eZRlmM+yXWExfSTJ2S5zqQHakHIouG1naUaX56mvM8mIATKmFJydBC3O+h ue/IdGuqLcoQQgkSXVWPb5XknvuuQmwgadaDYXa/3UgQuCjNECgYqtkGQ1QFVMqikfxH zPPxGk4VrJsPRnMHdfh3b2UwdFjkQDS/m4Se211bBmog3V65JrJgEs5PkkRE06/LC3jB q2AGCQwd37XrU3sL1smjJWusTnLHeiDz1hLseXLelkh7pdg4h6GbCkUejsbKimZgaPR1 V4IVIZ/UgEO61wzEi0ttCwDKXmk6Ep0nOphn1tvBghD9NX5i4LkFVUEhS4QQKbibxS/d dhTg== X-Gm-Message-State: ACgBeo3IzDZA44sqY8GYOOnuGY4EwWyr0ZcuRhm1oWI2XRPWBPCU+rr+ buTfRbnBUxg8JT7lanhiND1RJSY= X-Google-Smtp-Source: AA6agR7J+MJ4CbmGf5il6MSSbTeMp6GnYimb+kijqjPdd5RmvTmVZMOjZnDxlbd2gvC/bBmap4i7erI= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:853c:a4e2:939a:fb56]) (user=pcc job=sendgmr) by 2002:a25:bb82:0:b0:696:4351:8f5f with SMTP id y2-20020a25bb82000000b0069643518f5fmr14192400ybg.90.1662787475684; Fri, 09 Sep 2022 22:24:35 -0700 (PDT) Date: Fri, 9 Sep 2022 22:24:26 -0700 Message-Id: <20220910052426.943376-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Subject: [PATCH] kasan: also display registers for reports from HW exceptions From: Peter Collingbourne To: Catalin Marinas , Vincenzo Frascino , Andrew Morton , Andrey Konovalov Cc: Peter Collingbourne , linux-arm-kernel@lists.infradead.org, kasan-dev@googlegroups.com, linux-mm@kvack.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220909_222438_177365_593179B2 X-CRM114-Status: GOOD ( 22.35 ) 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 It is sometimes useful to know the values of the registers when a KASAN report is generated. We can do this easily for reports that resulted from a hardware exception by passing the struct pt_regs from the exception into the report function; do so. Signed-off-by: Peter Collingbourne --- Applies to -next. arch/arm64/kernel/traps.c | 3 +-- arch/arm64/mm/fault.c | 2 +- include/linux/kasan.h | 10 ++++++++++ mm/kasan/kasan.h | 1 + mm/kasan/report.c | 27 ++++++++++++++++++++++----- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index b7fed33981f7..42f05f38c90a 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -1019,9 +1019,8 @@ static int kasan_handler(struct pt_regs *regs, unsigned long esr) bool write = esr & KASAN_ESR_WRITE; size_t size = KASAN_ESR_SIZE(esr); u64 addr = regs->regs[0]; - u64 pc = regs->pc; - kasan_report(addr, size, write, pc); + kasan_report_regs(addr, size, write, regs); /* * The instrumentation allows to control whether we can proceed after diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 5b391490e045..c4b91f5d8cc8 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -316,7 +316,7 @@ static void report_tag_fault(unsigned long addr, unsigned long esr, * find out access size. */ bool is_write = !!(esr & ESR_ELx_WNR); - kasan_report(addr, 0, is_write, regs->pc); + kasan_report_regs(addr, 0, is_write, regs); } #else /* Tag faults aren't enabled without CONFIG_KASAN_HW_TAGS. */ diff --git a/include/linux/kasan.h b/include/linux/kasan.h index d811b3d7d2a1..381aea149353 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -353,6 +353,16 @@ static inline void *kasan_reset_tag(const void *addr) bool kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip); +/** + * kasan_report_regs - print a report about a bad memory access detected by KASAN + * @addr: address of the bad access + * @size: size of the bad access + * @is_write: whether the bad access is a write or a read + * @regs: register values at the point of the bad memory access + */ +bool kasan_report_regs(unsigned long addr, size_t size, bool is_write, + struct pt_regs *regs); + #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ static inline void *kasan_reset_tag(const void *addr) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index abbcc1b0eec5..39772c21a8ae 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -175,6 +175,7 @@ struct kasan_report_info { size_t access_size; bool is_write; unsigned long ip; + struct pt_regs *regs; /* Filled in by the common reporting code. */ void *first_bad_addr; diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 39e8e5a80b82..eac9cd45b4a1 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -284,7 +285,6 @@ static void print_address_description(void *addr, u8 tag, { struct page *page = addr_to_page(addr); - dump_stack_lvl(KERN_ERR); pr_err("\n"); if (info->cache && info->object) { @@ -394,11 +394,14 @@ static void print_report(struct kasan_report_info *info) kasan_print_tags(tag, info->first_bad_addr); pr_err("\n"); + if (info->regs) + show_regs(info->regs); + else + dump_stack_lvl(KERN_ERR); + if (addr_has_metadata(addr)) { print_address_description(addr, tag, info); print_memory_metadata(info->first_bad_addr); - } else { - dump_stack_lvl(KERN_ERR); } } @@ -458,8 +461,8 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_ty * user_access_save/restore(): kasan_report_invalid_free() cannot be called * from a UACCESS region, and kasan_report_async() is not used on x86. */ -bool kasan_report(unsigned long addr, size_t size, bool is_write, - unsigned long ip) +static bool __kasan_report(unsigned long addr, size_t size, bool is_write, + unsigned long ip, struct pt_regs *regs) { bool ret = true; void *ptr = (void *)addr; @@ -480,6 +483,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, info.access_size = size; info.is_write = is_write; info.ip = ip; + info.regs = regs; complete_report_info(&info); @@ -493,6 +497,19 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, return ret; } +bool kasan_report(unsigned long addr, size_t size, bool is_write, + unsigned long ip) +{ + return __kasan_report(addr, size, is_write, ip, NULL); +} + +bool kasan_report_regs(unsigned long addr, size_t size, bool is_write, + struct pt_regs *regs) +{ + return __kasan_report(addr, size, is_write, instruction_pointer(regs), + regs); +} + #ifdef CONFIG_KASAN_HW_TAGS void kasan_report_async(void) { -- 2.37.2.789.g6183377224-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel