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 9B212C6FA82 for ; Sat, 10 Sep 2022 21:40:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5AE96B0071; Sat, 10 Sep 2022 17:40:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C0A3F6B0072; Sat, 10 Sep 2022 17:40:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AADDB8D0002; Sat, 10 Sep 2022 17:40:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9CD9A6B0071 for ; Sat, 10 Sep 2022 17:40:37 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5AECF1403D6 for ; Sat, 10 Sep 2022 21:40:37 +0000 (UTC) X-FDA: 79897495314.13.5357083 Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) by imf03.hostedemail.com (Postfix) with ESMTP id 0FD1E2007A for ; Sat, 10 Sep 2022 21:40:36 +0000 (UTC) Received: by mail-qk1-f181.google.com with SMTP id c9so3642493qkk.6 for ; Sat, 10 Sep 2022 14:40:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=1rkaXGzitQ8uWDzj/RJ0ZVLihQPs/LjSy+ViYByz17c=; b=IRjnD3bPK8CiwwVT1yaoDgVCStlU5FcKyvb8NNUFL1sGVeFoB5aS7TITUIZcEi/yu0 nuf8utR/TETtBKHQmnlmv6USrjJVamM83hTmmizSU9QqG9+YdP0IlDg2GKTy4FB33Rdr irilfSbO1hoRYg5kaYh3jk/ICr11SJ8cD2pK/wOv4gXDAYEMc18Lf1oZGhPaMtvwT8h6 avf+RfLQm3izhy7Qd4uRJhPJb/eJewnBtONfuBlZwOSuyC0zpiGc6IuhvkOLoCYhhlS1 M2Z+NXzyfp4W1FhpsSMgRg42x0qxkoA7pwh6McNhatGx5MoPFiHtw8T3lnkjU0pIFxAc KGew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=1rkaXGzitQ8uWDzj/RJ0ZVLihQPs/LjSy+ViYByz17c=; b=cCLR0YF51MLjJNKIuD3fq99DW03ea6TLRrs09fItWGwRAqyezTYPLSF0IQsvdx4FGd h7NtjJu3JO8DN9XmVkptROLzwZ5Vcx2vhcvcc5A+qDxN2i3pibYwDD81SrsCUlsbG+1E WGf6Ml32O4qB7yiVaf4Bogzn9XpdsuAbkgqyzaqVBp57qQCIkDFALeA42HcllPZg8efQ //wC4CyMX6plV9JMwdjp39Uusy9hE9sM2lqeQeZPdWX/9vzBqfSxz41yLPCR41m3+jJE juM8AgSEaa9cnstHp1S1RqFmeLEQ1rS9ALxU07Dpkx7AM25T7811ikwiopmsXMzJhson fTwg== X-Gm-Message-State: ACgBeo2kPI4IrIpe+Hhn32ylrYIACzO9bvHTWnCHfjbZ5yUNCHqmH+HP 380iZs5qwP29+T+ttFwpkU/X98BugndRj/sVc8k= X-Google-Smtp-Source: AA6agR7n7x8ZllUWJWdAO/53wDzlOM1rjM6lvxQ6mZHcrA2CbUKC1wpEhA7pd9HFN5VnTlthQNfRPG2oZIKy0wPRbJY= X-Received: by 2002:a37:aac4:0:b0:6cb:d070:7842 with SMTP id t187-20020a37aac4000000b006cbd0707842mr9667946qke.386.1662846036279; Sat, 10 Sep 2022 14:40:36 -0700 (PDT) MIME-Version: 1.0 References: <20220910052426.943376-1-pcc@google.com> In-Reply-To: <20220910052426.943376-1-pcc@google.com> From: Andrey Konovalov Date: Sat, 10 Sep 2022 23:40:25 +0200 Message-ID: Subject: Re: [PATCH] kasan: also display registers for reports from HW exceptions To: Peter Collingbourne Cc: Catalin Marinas , Vincenzo Frascino , Andrew Morton , Linux ARM , kasan-dev , Linux Memory Management List Content-Type: text/plain; charset="UTF-8" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662846037; a=rsa-sha256; cv=none; b=eT8eLQsoZEUtF66ti4VtkKIo6dpDjtcMiHjst791Ltq7VBTWU4K3B6HhmmtwOlRZsvIsOZ 6fgXLXkgQ1ZEY13LKIefxDuIkPT77hv81Er9mnehkNROoVGQbejYldYkQn+GqqrVmlgDEn LZ/0H5Zigk0x2rqejaDWZHqo+EPGpaY= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IRjnD3bP; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.222.181 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662846037; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1rkaXGzitQ8uWDzj/RJ0ZVLihQPs/LjSy+ViYByz17c=; b=JVTbvQfgfcoAXAgVUjxC5fbkt2yrWL2HoGnGWlMjSgxmSbHeZBcAyTml5WXWu45wwvtahp QeX5mVjoQWMF4OsmE/H2z5HCEco2boz7oJ05rVQy2Kg/jVl5toseMup5+xLM3t78B+Zl0Y Pw7hQ2IYCwwiJejvmwOSWvRcntjk8bw= Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IRjnD3bP; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.222.181 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com X-Rspam-User: X-Rspamd-Queue-Id: 0FD1E2007A X-Rspamd-Server: rspam11 X-Stat-Signature: z4akjbpxjuhqqotckmj37wos1chettdw X-HE-Tag: 1662846036-363859 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, Sep 10, 2022 at 7:24 AM Peter Collingbourne wrote: > > It is sometimes useful to know the values of the registers when a KASAN > report is generated. Hi Peter, What are the cases when the register values are useful? They are "corrupted" by KASAN runtime anyway and thus are not relevant to the place in code where the bad access happened. Thanks! > 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 >