Commit 9d7b7dd9 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Linus Torvalds

kasan: split out print_report from __kasan_report

Split out the part of __kasan_report() that prints things into
print_report().  One of the subsequent patches makes another error handler
use print_report() as well.

Includes lower-level changes:

 - Allow addr_has_metadata() accepting a tagged address.

 - Drop the const qualifier from the fields of kasan_access_info to
   avoid excessive type casts.

 - Change the type of the address argument of __kasan_report() and
   end_report() to void * to reduce the number of type casts.

Link: https://lkml.kernel.org/r/9be3ed99dd24b9c4e1c4a848b69a0c6ecefd845e.1646237226.git.andreyknvl@google.comSigned-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 0a6e8a07
...@@ -128,8 +128,8 @@ static inline bool kasan_sync_fault_possible(void) ...@@ -128,8 +128,8 @@ static inline bool kasan_sync_fault_possible(void)
#define META_ROWS_AROUND_ADDR 2 #define META_ROWS_AROUND_ADDR 2
struct kasan_access_info { struct kasan_access_info {
const void *access_addr; void *access_addr;
const void *first_bad_addr; void *first_bad_addr;
size_t access_size; size_t access_size;
bool is_write; bool is_write;
unsigned long ip; unsigned long ip;
...@@ -239,7 +239,8 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr) ...@@ -239,7 +239,8 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
static inline bool addr_has_metadata(const void *addr) static inline bool addr_has_metadata(const void *addr)
{ {
return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START)); return (kasan_reset_tag(addr) >=
kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
} }
/** /**
......
...@@ -139,10 +139,11 @@ static void start_report(unsigned long *flags, bool sync) ...@@ -139,10 +139,11 @@ static void start_report(unsigned long *flags, bool sync)
pr_err("==================================================================\n"); pr_err("==================================================================\n");
} }
static void end_report(unsigned long *flags, unsigned long addr) static void end_report(unsigned long *flags, void *addr)
{ {
if (addr) if (addr)
trace_error_report_end(ERROR_DETECTOR_KASAN, addr); trace_error_report_end(ERROR_DETECTOR_KASAN,
(unsigned long)addr);
pr_err("==================================================================\n"); pr_err("==================================================================\n");
add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
spin_unlock_irqrestore(&report_lock, *flags); spin_unlock_irqrestore(&report_lock, *flags);
...@@ -398,7 +399,7 @@ void kasan_report_invalid_free(void *object, unsigned long ip) ...@@ -398,7 +399,7 @@ void kasan_report_invalid_free(void *object, unsigned long ip)
pr_err("\n"); pr_err("\n");
print_address_description(object, tag); print_address_description(object, tag);
print_memory_metadata(object); print_memory_metadata(object);
end_report(&flags, (unsigned long)object); end_report(&flags, object);
} }
#ifdef CONFIG_KASAN_HW_TAGS #ifdef CONFIG_KASAN_HW_TAGS
...@@ -411,44 +412,47 @@ void kasan_report_async(void) ...@@ -411,44 +412,47 @@ void kasan_report_async(void)
pr_err("Asynchronous mode enabled: no access details available\n"); pr_err("Asynchronous mode enabled: no access details available\n");
pr_err("\n"); pr_err("\n");
dump_stack_lvl(KERN_ERR); dump_stack_lvl(KERN_ERR);
end_report(&flags, 0); end_report(&flags, NULL);
} }
#endif /* CONFIG_KASAN_HW_TAGS */ #endif /* CONFIG_KASAN_HW_TAGS */
static void __kasan_report(unsigned long addr, size_t size, bool is_write, static void print_report(struct kasan_access_info *info)
{
void *tagged_addr = info->access_addr;
void *untagged_addr = kasan_reset_tag(tagged_addr);
u8 tag = get_tag(tagged_addr);
print_error_description(info);
if (addr_has_metadata(untagged_addr))
kasan_print_tags(tag, info->first_bad_addr);
pr_err("\n");
if (addr_has_metadata(untagged_addr)) {
print_address_description(untagged_addr, tag);
print_memory_metadata(info->first_bad_addr);
} else {
dump_stack_lvl(KERN_ERR);
}
}
static void __kasan_report(void *addr, size_t size, bool is_write,
unsigned long ip) unsigned long ip)
{ {
struct kasan_access_info info; struct kasan_access_info info;
void *tagged_addr;
void *untagged_addr;
unsigned long flags; unsigned long flags;
start_report(&flags, true); start_report(&flags, true);
tagged_addr = (void *)addr; info.access_addr = addr;
untagged_addr = kasan_reset_tag(tagged_addr); if (addr_has_metadata(addr))
info.first_bad_addr = kasan_find_first_bad_addr(addr, size);
info.access_addr = tagged_addr;
if (addr_has_metadata(untagged_addr))
info.first_bad_addr =
kasan_find_first_bad_addr(tagged_addr, size);
else else
info.first_bad_addr = untagged_addr; info.first_bad_addr = addr;
info.access_size = size; info.access_size = size;
info.is_write = is_write; info.is_write = is_write;
info.ip = ip; info.ip = ip;
print_error_description(&info); print_report(&info);
if (addr_has_metadata(untagged_addr))
kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
pr_err("\n");
if (addr_has_metadata(untagged_addr)) {
print_address_description(untagged_addr, get_tag(tagged_addr));
print_memory_metadata(info.first_bad_addr);
} else {
dump_stack_lvl(KERN_ERR);
}
end_report(&flags, addr); end_report(&flags, addr);
} }
...@@ -460,7 +464,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, ...@@ -460,7 +464,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write,
bool ret = false; bool ret = false;
if (likely(report_enabled())) { if (likely(report_enabled())) {
__kasan_report(addr, size, is_write, ip); __kasan_report((void *)addr, size, is_write, ip);
ret = true; ret = true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment