2022-09-10 23:37:26

by andrey.konovalov

[permalink] [raw]
Subject: [PATCH] kasan: better invalid/double-free report header

From: Andrey Konovalov <[email protected]>

Update the report header for invalid- and double-free bugs to contain
the address being freed:

BUG: KASAN: invalid-free in kfree+0x280/0x2a8
Free of addr ffff00000beac001 by task kunit_try_catch/99

Signed-off-by: Andrey Konovalov <[email protected]>
---
mm/kasan/report.c | 23 ++++++++++++++++-------
mm/kasan/report_generic.c | 3 ++-
mm/kasan/report_tags.c | 2 +-
3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index 39e8e5a80b82..df3602062bfd 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -175,17 +175,14 @@ static void end_report(unsigned long *flags, void *addr)

static void print_error_description(struct kasan_report_info *info)
{
- if (info->type == KASAN_REPORT_INVALID_FREE) {
- pr_err("BUG: KASAN: invalid-free in %pS\n", (void *)info->ip);
- return;
- }
+ pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);

- if (info->type == KASAN_REPORT_DOUBLE_FREE) {
- pr_err("BUG: KASAN: double-free in %pS\n", (void *)info->ip);
+ if (info->type != KASAN_REPORT_ACCESS) {
+ pr_err("Free of addr %px by task %s/%d\n",
+ info->access_addr, current->comm, task_pid_nr(current));
return;
}

- pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
if (info->access_size)
pr_err("%s of size %zu at addr %px by task %s/%d\n",
info->is_write ? "Write" : "Read", info->access_size,
@@ -420,6 +417,18 @@ static void complete_report_info(struct kasan_report_info *info)
} else
info->cache = info->object = NULL;

+ switch (info->type) {
+ case KASAN_REPORT_INVALID_FREE:
+ info->bug_type = "invalid-free";
+ break;
+ case KASAN_REPORT_DOUBLE_FREE:
+ info->bug_type = "double-free";
+ break;
+ default:
+ /* bug_type filled in by kasan_complete_mode_report_info. */
+ break;
+ }
+
/* Fill in mode-specific report info fields. */
kasan_complete_mode_report_info(info);
}
diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c
index 087c1d8c8145..043c94b04605 100644
--- a/mm/kasan/report_generic.c
+++ b/mm/kasan/report_generic.c
@@ -132,7 +132,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info)
struct kasan_alloc_meta *alloc_meta;
struct kasan_free_meta *free_meta;

- info->bug_type = get_bug_type(info);
+ if (!info->bug_type)
+ info->bug_type = get_bug_type(info);

if (!info->cache || !info->object)
return;
diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c
index d3510424d29b..ecede06ef374 100644
--- a/mm/kasan/report_tags.c
+++ b/mm/kasan/report_tags.c
@@ -37,7 +37,7 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info)
bool is_free;
bool alloc_found = false, free_found = false;

- if (!info->cache || !info->object) {
+ if ((!info->cache || !info->object) && !info->bug_type) {
info->bug_type = get_common_bug_type(info);
return;
}
--
2.25.1


2022-09-11 11:58:45

by Andrey Konovalov

[permalink] [raw]
Subject: Re: [PATCH] kasan: better invalid/double-free report header

On Sun, Sep 11, 2022 at 1:25 AM <[email protected]> wrote:
>
> From: Andrey Konovalov <[email protected]>
>
> Update the report header for invalid- and double-free bugs to contain
> the address being freed:
>
> BUG: KASAN: invalid-free in kfree+0x280/0x2a8
> Free of addr ffff00000beac001 by task kunit_try_catch/99
>
> Signed-off-by: Andrey Konovalov <[email protected]>

Forgot to mention: this goes on top of the "kasan: switch tag-based
modes to stack ring from per-object metadata" series.

> ---
> mm/kasan/report.c | 23 ++++++++++++++++-------
> mm/kasan/report_generic.c | 3 ++-
> mm/kasan/report_tags.c | 2 +-
> 3 files changed, 19 insertions(+), 9 deletions(-)
>
> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> index 39e8e5a80b82..df3602062bfd 100644
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -175,17 +175,14 @@ static void end_report(unsigned long *flags, void *addr)
>
> static void print_error_description(struct kasan_report_info *info)
> {
> - if (info->type == KASAN_REPORT_INVALID_FREE) {
> - pr_err("BUG: KASAN: invalid-free in %pS\n", (void *)info->ip);
> - return;
> - }
> + pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
>
> - if (info->type == KASAN_REPORT_DOUBLE_FREE) {
> - pr_err("BUG: KASAN: double-free in %pS\n", (void *)info->ip);
> + if (info->type != KASAN_REPORT_ACCESS) {
> + pr_err("Free of addr %px by task %s/%d\n",
> + info->access_addr, current->comm, task_pid_nr(current));
> return;
> }
>
> - pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
> if (info->access_size)
> pr_err("%s of size %zu at addr %px by task %s/%d\n",
> info->is_write ? "Write" : "Read", info->access_size,
> @@ -420,6 +417,18 @@ static void complete_report_info(struct kasan_report_info *info)
> } else
> info->cache = info->object = NULL;
>
> + switch (info->type) {
> + case KASAN_REPORT_INVALID_FREE:
> + info->bug_type = "invalid-free";
> + break;
> + case KASAN_REPORT_DOUBLE_FREE:
> + info->bug_type = "double-free";
> + break;
> + default:
> + /* bug_type filled in by kasan_complete_mode_report_info. */
> + break;
> + }
> +
> /* Fill in mode-specific report info fields. */
> kasan_complete_mode_report_info(info);
> }
> diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c
> index 087c1d8c8145..043c94b04605 100644
> --- a/mm/kasan/report_generic.c
> +++ b/mm/kasan/report_generic.c
> @@ -132,7 +132,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info)
> struct kasan_alloc_meta *alloc_meta;
> struct kasan_free_meta *free_meta;
>
> - info->bug_type = get_bug_type(info);
> + if (!info->bug_type)
> + info->bug_type = get_bug_type(info);
>
> if (!info->cache || !info->object)
> return;
> diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c
> index d3510424d29b..ecede06ef374 100644
> --- a/mm/kasan/report_tags.c
> +++ b/mm/kasan/report_tags.c
> @@ -37,7 +37,7 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info)
> bool is_free;
> bool alloc_found = false, free_found = false;
>
> - if (!info->cache || !info->object) {
> + if ((!info->cache || !info->object) && !info->bug_type) {
> info->bug_type = get_common_bug_type(info);
> return;
> }
> --
> 2.25.1
>

2022-09-12 17:25:59

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH] kasan: better invalid/double-free report header

On Sun, 11 Sept 2022 at 01:25, <[email protected]> wrote:
>
> From: Andrey Konovalov <[email protected]>
>
> Update the report header for invalid- and double-free bugs to contain
> the address being freed:
>
> BUG: KASAN: invalid-free in kfree+0x280/0x2a8
> Free of addr ffff00000beac001 by task kunit_try_catch/99

It wouldn't hurt showing a full before vs. after report here in the
commit message for ease of reviewing.

> Signed-off-by: Andrey Konovalov <[email protected]>

Reviewed-by: Marco Elver <[email protected]>

> ---
> mm/kasan/report.c | 23 ++++++++++++++++-------
> mm/kasan/report_generic.c | 3 ++-
> mm/kasan/report_tags.c | 2 +-
> 3 files changed, 19 insertions(+), 9 deletions(-)
>
> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> index 39e8e5a80b82..df3602062bfd 100644
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -175,17 +175,14 @@ static void end_report(unsigned long *flags, void *addr)
>
> static void print_error_description(struct kasan_report_info *info)
> {
> - if (info->type == KASAN_REPORT_INVALID_FREE) {
> - pr_err("BUG: KASAN: invalid-free in %pS\n", (void *)info->ip);
> - return;
> - }
> + pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
>
> - if (info->type == KASAN_REPORT_DOUBLE_FREE) {
> - pr_err("BUG: KASAN: double-free in %pS\n", (void *)info->ip);
> + if (info->type != KASAN_REPORT_ACCESS) {
> + pr_err("Free of addr %px by task %s/%d\n",
> + info->access_addr, current->comm, task_pid_nr(current));
> return;
> }
>
> - pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
> if (info->access_size)
> pr_err("%s of size %zu at addr %px by task %s/%d\n",
> info->is_write ? "Write" : "Read", info->access_size,
> @@ -420,6 +417,18 @@ static void complete_report_info(struct kasan_report_info *info)
> } else
> info->cache = info->object = NULL;
>
> + switch (info->type) {
> + case KASAN_REPORT_INVALID_FREE:
> + info->bug_type = "invalid-free";
> + break;
> + case KASAN_REPORT_DOUBLE_FREE:
> + info->bug_type = "double-free";
> + break;
> + default:
> + /* bug_type filled in by kasan_complete_mode_report_info. */
> + break;
> + }
> +
> /* Fill in mode-specific report info fields. */
> kasan_complete_mode_report_info(info);
> }
> diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c
> index 087c1d8c8145..043c94b04605 100644
> --- a/mm/kasan/report_generic.c
> +++ b/mm/kasan/report_generic.c
> @@ -132,7 +132,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info)
> struct kasan_alloc_meta *alloc_meta;
> struct kasan_free_meta *free_meta;
>
> - info->bug_type = get_bug_type(info);
> + if (!info->bug_type)
> + info->bug_type = get_bug_type(info);
>
> if (!info->cache || !info->object)
> return;
> diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c
> index d3510424d29b..ecede06ef374 100644
> --- a/mm/kasan/report_tags.c
> +++ b/mm/kasan/report_tags.c
> @@ -37,7 +37,7 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info)
> bool is_free;
> bool alloc_found = false, free_found = false;
>
> - if (!info->cache || !info->object) {
> + if ((!info->cache || !info->object) && !info->bug_type) {
> info->bug_type = get_common_bug_type(info);
> return;
> }
> --
> 2.25.1
>