From patchwork Wed Mar 2 16:36:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766291 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 66A6BC433F5 for ; Wed, 2 Mar 2022 16:36:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2BE48D0007; Wed, 2 Mar 2022 11:36:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D3F318D0001; Wed, 2 Mar 2022 11:36:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B91DC8D0007; Wed, 2 Mar 2022 11:36:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id A34BB8D0001 for ; Wed, 2 Mar 2022 11:36:52 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 57C23250AD for ; Wed, 2 Mar 2022 16:36:52 +0000 (UTC) X-FDA: 79200000264.04.FF7F6A3 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf26.hostedemail.com (Postfix) with ESMTP id E66A3140013 for ; Wed, 2 Mar 2022 16:36:48 +0000 (UTC) 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=1646239006; 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=yCGVn7r00QMuSI8V6gqxIK+E2Lf9e3kHr+j6afAS6Vw=; b=Pw56LxQwUnnzUUyfCn+zD5iB/Yufs+PrCWRQWBChTqj0u6vbtBk7Wc9jO7UAE4YBh8z0eS HfHWdWM4GHiM0xaVGb4TNNniiNsPmCmhkLMtubL6WsogGoHsPqlpRc8dVNQ+Cts8WtGSAZ hOte/7TwHHWIjycI/tHU9kP6pOdlcPA= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 01/22] kasan: drop addr check from describe_object_addr Date: Wed, 2 Mar 2022 17:36:21 +0100 Message-Id: <761f8e5a6ee040d665934d916a90afe9f322f745.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E66A3140013 X-Stat-Signature: 8az3t8j61twweiq8qwurtos4io3bnccb Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Pw56LxQw; spf=pass (imf26.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1646239008-188395 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: From: Andrey Konovalov describe_object_addr() used to be called with NULL addr in the early days of KASAN. This no longer happens, so drop the check. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/report.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index f64352008bb8..607a8c2e4674 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -162,9 +162,6 @@ static void describe_object_addr(struct kmem_cache *cache, void *object, " which belongs to the cache %s of size %d\n", object, cache->name, cache->object_size); - if (!addr) - return; - if (access_addr < object_addr) { rel_type = "to the left"; rel_bytes = object_addr - access_addr; From patchwork Wed Mar 2 16:36:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766292 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 4543EC433F5 for ; Wed, 2 Mar 2022 16:37:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2EE98D0008; Wed, 2 Mar 2022 11:37:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BDD468D0001; Wed, 2 Mar 2022 11:37:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACDD58D0008; Wed, 2 Mar 2022 11:37:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0008.hostedemail.com [216.40.44.8]) by kanga.kvack.org (Postfix) with ESMTP id 9CA068D0001 for ; Wed, 2 Mar 2022 11:37:28 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4BC879CD48 for ; Wed, 2 Mar 2022 16:37:28 +0000 (UTC) X-FDA: 79200001776.29.BB78601 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf16.hostedemail.com (Postfix) with ESMTP id EF577180019 for ; Wed, 2 Mar 2022 16:36:48 +0000 (UTC) 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=1646239007; 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=ReVQg90bpO6aQ9p1E17mGNBQrTUFi/5PZoBeBo27NeE=; b=CkSxgq2C4AQd4Hc/5oIgvZVdzS8i0G/red+aOhMvBK+qP+KWRBe1leENjyQER0Scn8x8s+ +JQ5jqkF9/rAvHLfhxTpE1MaNTAH3+5lHi+ABoT3biafZUWNz1r9qlaCCnYaOH+mR/AO56 glUtZxXMxV9DzLCbZno4jG0eTH+Qnxc= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 02/22] kasan: more line breaks in reports Date: Wed, 2 Mar 2022 17:36:22 +0100 Message-Id: <8682c4558e533cd0f99bdb964ce2fe741f2a9212.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: EF577180019 X-Stat-Signature: eafs3z4a9uafsf5f1h5be9g89xb98akk Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CkSxgq2C; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-HE-Tag: 1646239008-801368 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: From: Andrey Konovalov Add a line break after each part that describes the buggy address. Improves readability of reports. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/report.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 607a8c2e4674..ded648c0a0e4 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -250,11 +250,13 @@ static void print_address_description(void *addr, u8 tag) void *object = nearest_obj(cache, slab, addr); describe_object(cache, object, addr, tag); + pr_err("\n"); } if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) { pr_err("The buggy address belongs to the variable:\n"); pr_err(" %pS\n", addr); + pr_err("\n"); } if (is_vmalloc_addr(addr)) { @@ -265,6 +267,7 @@ static void print_address_description(void *addr, u8 tag) " [%px, %px) created by:\n" " %pS\n", va->addr, va->addr + va->size, va->caller); + pr_err("\n"); page = vmalloc_to_page(page); } @@ -273,9 +276,11 @@ static void print_address_description(void *addr, u8 tag) if (page) { pr_err("The buggy address belongs to the physical page:\n"); dump_page(page, "kasan: bad access detected"); + pr_err("\n"); } kasan_print_address_stack_frame(addr); + pr_err("\n"); } static bool meta_row_is_guilty(const void *row, const void *addr) @@ -382,7 +387,6 @@ void kasan_report_invalid_free(void *object, unsigned long ip) kasan_print_tags(tag, object); pr_err("\n"); print_address_description(object, tag); - pr_err("\n"); print_memory_metadata(object); end_report(&flags, (unsigned long)object); } @@ -443,7 +447,6 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write, if (addr_has_metadata(untagged_addr)) { print_address_description(untagged_addr, get_tag(tagged_addr)); - pr_err("\n"); print_memory_metadata(info.first_bad_addr); } else { dump_stack_lvl(KERN_ERR); From patchwork Wed Mar 2 16:36:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766288 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 ECB32C433EF for ; Wed, 2 Mar 2022 16:36:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 725F68D0003; Wed, 2 Mar 2022 11:36:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AF418D0001; Wed, 2 Mar 2022 11:36:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C9C58D0003; Wed, 2 Mar 2022 11:36:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 2AC248D0001 for ; Wed, 2 Mar 2022 11:36:50 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F040820341 for ; Wed, 2 Mar 2022 16:36:49 +0000 (UTC) X-FDA: 79200000138.09.46EF2A2 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf09.hostedemail.com (Postfix) with ESMTP id 45535140005 for ; Wed, 2 Mar 2022 16:36:49 +0000 (UTC) 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=1646239008; 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=/PNbeFwa0AFDpaFm8soBx+srw+6LkE/5medj0Pnuzw0=; b=dv6ARQELb9KrBwGlPNLJRXzwHGIL9vGau4uJBJnlfxJAURfUnesxoerB2uQ1mLFWWJpXCY eMBLw+rI7xjWi+Uqs6C7OYC65eMQkQblcKlU2uEt4dLkyYDGPGIakZh4NzoflRtxJBQp1E A9UTsjjFXAN48UAJzg4uhu0IIVPV10k= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 03/22] kasan: rearrange stack frame info in reports Date: Wed, 2 Mar 2022 17:36:23 +0100 Message-Id: <1ee113a4c111df97d168c820b527cda77a3cac40.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 45535140005 X-Stat-Signature: w7x8ju7abqfh5enm4fsywxo3k1ufjsqi Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dv6ARQEL; spf=pass (imf09.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1646239009-428011 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: From: Andrey Konovalov - Move printing stack frame info before printing page info. - Add object_is_on_stack() check to print_address_description() and add a corresponding WARNING to kasan_print_address_stack_frame(). This looks more in line with the rest of the checks in this function and also allows to avoid complicating code logic wrt line breaks. - Clean up comments related to get_address_stack_frame_info(). Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/report.c | 12 +++++++++--- mm/kasan/report_generic.c | 15 ++++----------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index ded648c0a0e4..d60ee8b81e2b 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -259,6 +259,15 @@ static void print_address_description(void *addr, u8 tag) pr_err("\n"); } + if (object_is_on_stack(addr)) { + /* + * Currently, KASAN supports printing frame information only + * for accesses to the task's own stack. + */ + kasan_print_address_stack_frame(addr); + pr_err("\n"); + } + if (is_vmalloc_addr(addr)) { struct vm_struct *va = find_vm_area(addr); @@ -278,9 +287,6 @@ static void print_address_description(void *addr, u8 tag) dump_page(page, "kasan: bad access detected"); pr_err("\n"); } - - kasan_print_address_stack_frame(addr); - pr_err("\n"); } static bool meta_row_is_guilty(const void *row, const void *addr) diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 139615ef326b..3751391ff11a 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -211,6 +211,7 @@ static void print_decoded_frame_descr(const char *frame_descr) } } +/* Returns true only if the address is on the current task's stack. */ static bool __must_check get_address_stack_frame_info(const void *addr, unsigned long *offset, const char **frame_descr, @@ -224,13 +225,6 @@ static bool __must_check get_address_stack_frame_info(const void *addr, BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP)); - /* - * NOTE: We currently only support printing frame information for - * accesses to the task's own stack. - */ - if (!object_is_on_stack(addr)) - return false; - aligned_addr = round_down((unsigned long)addr, sizeof(long)); mem_ptr = round_down(aligned_addr, KASAN_GRANULE_SIZE); shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr); @@ -269,14 +263,13 @@ void kasan_print_address_stack_frame(const void *addr) const char *frame_descr; const void *frame_pc; + if (WARN_ON(!object_is_on_stack(addr))) + return; + if (!get_address_stack_frame_info(addr, &offset, &frame_descr, &frame_pc)) return; - /* - * get_address_stack_frame_info only returns true if the given addr is - * on the current task's stack. - */ pr_err("\n"); pr_err("addr %px is located in stack of task %s/%d at offset %lu in frame:\n", addr, current->comm, task_pid_nr(current), offset); From patchwork Wed Mar 2 16:36:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766289 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 C07CEC433FE for ; Wed, 2 Mar 2022 16:36:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD3778D0006; Wed, 2 Mar 2022 11:36:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A59828D0001; Wed, 2 Mar 2022 11:36:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A8988D0006; Wed, 2 Mar 2022 11:36:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0088.hostedemail.com [216.40.44.88]) by kanga.kvack.org (Postfix) with ESMTP id 080998D0001 for ; Wed, 2 Mar 2022 11:36:51 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id C12369BBA1 for ; Wed, 2 Mar 2022 16:36:50 +0000 (UTC) X-FDA: 79200000180.17.67D1B55 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf31.hostedemail.com (Postfix) with ESMTP id F1AD02000E for ; Wed, 2 Mar 2022 16:36:49 +0000 (UTC) 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=1646239008; 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=uo3r1XZ2YxXX2Y0KQk7NP/LkQS2+d42pEDKbCuKVO2A=; b=dqtbv/8FM/BpZ36aN0SoEvBbrDqGpQNujrrTiFrcZ/GGK1iACrmtY3v9EpgsqMoF5kVf+V T77/+r0QQUpdIhNtrvEvaDV4gqjloM1z7E8wkpf36GllOgJ+Wk5zA4ChirBxB4zHXCD8Ai ZlMMu+FEdx2HixPTDXf2hhJwnz8Ki+I= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 04/22] kasan: improve stack frame info in reports Date: Wed, 2 Mar 2022 17:36:24 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: F1AD02000E X-Rspam-User: Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="dqtbv/8F"; spf=pass (imf31.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Stat-Signature: 3gercyocghtog7pyqjj4a5jrbcmtbzr6 X-HE-Tag: 1646239009-512360 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: From: Andrey Konovalov - Print at least task name and id for reports affecting allocas (get_address_stack_frame_info() does not support them). - Capitalize first letter of each sentence. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/report_generic.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 3751391ff11a..7e03cca569a7 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -180,7 +180,7 @@ static void print_decoded_frame_descr(const char *frame_descr) return; pr_err("\n"); - pr_err("this frame has %lu %s:\n", num_objects, + pr_err("This frame has %lu %s:\n", num_objects, num_objects == 1 ? "object" : "objects"); while (num_objects--) { @@ -266,13 +266,14 @@ void kasan_print_address_stack_frame(const void *addr) if (WARN_ON(!object_is_on_stack(addr))) return; + pr_err("The buggy address belongs to stack of task %s/%d\n", + current->comm, task_pid_nr(current)); + if (!get_address_stack_frame_info(addr, &offset, &frame_descr, &frame_pc)) return; - pr_err("\n"); - pr_err("addr %px is located in stack of task %s/%d at offset %lu in frame:\n", - addr, current->comm, task_pid_nr(current), offset); + pr_err(" and is located at offset %lu in frame:\n", offset); pr_err(" %pS\n", frame_pc); if (!frame_descr) From patchwork Wed Mar 2 16:36:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766290 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 8B829C433EF for ; Wed, 2 Mar 2022 16:36:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ECC528D0005; Wed, 2 Mar 2022 11:36:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E27FB8D0001; Wed, 2 Mar 2022 11:36:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACE6E8D0005; Wed, 2 Mar 2022 11:36:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0019.hostedemail.com [216.40.44.19]) by kanga.kvack.org (Postfix) with ESMTP id 8404C8D0005 for ; Wed, 2 Mar 2022 11:36:51 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 47C13181CA327 for ; Wed, 2 Mar 2022 16:36:51 +0000 (UTC) X-FDA: 79200000222.29.58E59AC Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf27.hostedemail.com (Postfix) with ESMTP id A6EED40015 for ; Wed, 2 Mar 2022 16:36:50 +0000 (UTC) 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=1646239009; 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=Y1tVVcjXP0EERa5LTGRHGhA3byiWB4oonSnJ546jpQY=; b=Lt4GrBE4AcuD8i0t9r97+W9VG/37yQMTx3JTaWtBhqz73Ts6O4gHTwfWe/kSPuGcx5X2Mo 5T5ocOiZw0whxQHDZxGV14Ovfb18EmMLsBiOoSZhPraZ349MCqlDwi5bfAklOErZc2h5/5 5aV2NFs1OglbiLieI37hZm7xGihH/Vs= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 05/22] kasan: print basic stack frame info for SW_TAGS Date: Wed, 2 Mar 2022 17:36:25 +0100 Message-Id: <029aaa87ceadde0702f3312a34697c9139c9fb53.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam10 X-Rspam-User: X-Stat-Signature: jf6est9wsqcj6nz8nxuigrrnr64k9x9z Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Lt4GrBE4; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspamd-Queue-Id: A6EED40015 X-HE-Tag: 1646239010-941479 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: From: Andrey Konovalov Software Tag-Based mode tags stack allocations when CONFIG_KASAN_STACK is enabled. Print task name and id in reports for stack-related bugs. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/kasan.h | 2 +- mm/kasan/report_sw_tags.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index d1e111b7d5d8..4447df0d7343 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -274,7 +274,7 @@ void *kasan_find_first_bad_addr(void *addr, size_t size); const char *kasan_get_bug_type(struct kasan_access_info *info); void kasan_metadata_fetch_row(char *buffer, void *row); -#if defined(CONFIG_KASAN_GENERIC) && defined(CONFIG_KASAN_STACK) +#if defined(CONFIG_KASAN_STACK) void kasan_print_address_stack_frame(const void *addr); #else static inline void kasan_print_address_stack_frame(const void *addr) { } diff --git a/mm/kasan/report_sw_tags.c b/mm/kasan/report_sw_tags.c index d2298c357834..44577b8d47a7 100644 --- a/mm/kasan/report_sw_tags.c +++ b/mm/kasan/report_sw_tags.c @@ -51,3 +51,14 @@ void kasan_print_tags(u8 addr_tag, const void *addr) pr_err("Pointer tag: [%02x], memory tag: [%02x]\n", addr_tag, *shadow); } + +#ifdef CONFIG_KASAN_STACK +void kasan_print_address_stack_frame(const void *addr) +{ + if (WARN_ON(!object_is_on_stack(addr))) + return; + + pr_err("The buggy address belongs to stack of task %s/%d\n", + current->comm, task_pid_nr(current)); +} +#endif From patchwork Wed Mar 2 16:36:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766293 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 77C30C433EF for ; Wed, 2 Mar 2022 16:37:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E3428D0002; Wed, 2 Mar 2022 11:37:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 092D08D0001; Wed, 2 Mar 2022 11:37:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB608D0002; Wed, 2 Mar 2022 11:37:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0088.hostedemail.com [216.40.44.88]) by kanga.kvack.org (Postfix) with ESMTP id DBF428D0001 for ; Wed, 2 Mar 2022 11:37:53 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id ACB00181CB2B3 for ; Wed, 2 Mar 2022 16:37:53 +0000 (UTC) X-FDA: 79200002826.23.2B55474 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf27.hostedemail.com (Postfix) with ESMTP id 1ED3F40006 for ; Wed, 2 Mar 2022 16:37:52 +0000 (UTC) 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=1646239072; 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=I05J+o+7HT6Q0jFo6xpWdgjFaYldkohtghm32sfnKb0=; b=EoZSAVxP43kd9ROACbNMePuzm1dcn+XXaWMyw5ByZ/ttIEznjWnvjbWO0dH1GvTwB4McX2 /2JSo4+MY3aZM3Dozy0YXIjTDH8PVgk+iIZXPfy/fkQnDliZIfQGZae3tuP+/dvC8GtBfz Mqgr+ukf08BswObNocR5H/yHe/qOgnw= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 06/22] kasan: simplify async check in end_report Date: Wed, 2 Mar 2022 17:36:26 +0100 Message-Id: <1c8ce43f97300300e62c941181afa2eb738965c5.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 1ED3F40006 X-Stat-Signature: sua593qg9tcdy4cfgx7f4kukp7uoxr1z Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=EoZSAVxP; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-HE-Tag: 1646239072-104851 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: From: Andrey Konovalov Currently, end_report() does not call trace_error_report_end() for bugs detected in either async or asymm mode (when kasan_async_fault_possible() returns true), as the address of the bad access might be unknown. However, for asymm mode, the address is known for faults triggered by read operations. Instead of using kasan_async_fault_possible(), simply check that the addr is not NULL when calling trace_error_report_end(). Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index d60ee8b81e2b..2d892ec050be 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -112,7 +112,7 @@ static void start_report(unsigned long *flags) static void end_report(unsigned long *flags, unsigned long addr) { - if (!kasan_async_fault_possible()) + if (addr) trace_error_report_end(ERROR_DETECTOR_KASAN, addr); pr_err("==================================================================\n"); add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); From patchwork Wed Mar 2 16:36:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766294 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 64A2AC433F5 for ; Wed, 2 Mar 2022 16:37:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E02668D0003; Wed, 2 Mar 2022 11:37:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DB1A08D0001; Wed, 2 Mar 2022 11:37:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDE5A8D0003; Wed, 2 Mar 2022 11:37:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id AB2C28D0001 for ; Wed, 2 Mar 2022 11:37:54 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 7F827121F4B for ; Wed, 2 Mar 2022 16:37:54 +0000 (UTC) X-FDA: 79200002868.07.DC31467 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf14.hostedemail.com (Postfix) with ESMTP id B85C1100010 for ; Wed, 2 Mar 2022 16:37:53 +0000 (UTC) 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=1646239072; 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=RoJ6KhPXk7RwGdhLlSmay7OZIFNg6mxUpxpaIs64SPw=; b=He6aO0JgCTzTNVfaICfLABWtZP0A3y0+aj6FYlfvf+mgRQAkTl3GWU/Tn7dKzAUWBQXWBe +5zV/8vXVNGiqruB3+klAWFioEYzmvGtBvAdTQqj8mMmuKz+GYYFtA2GylRDOYdKUN8grK ylTRltogUO+0CElKwAb2CS42S8905Es= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 07/22] kasan: simplify kasan_update_kunit_status and call sites Date: Wed, 2 Mar 2022 17:36:27 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Queue-Id: B85C1100010 X-Stat-Signature: kbo98hcudbg3x4z67jxdk9yrgh5kfe5z X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=He6aO0Jg; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspamd-Server: rspam07 X-HE-Tag: 1646239073-626912 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: From: Andrey Konovalov - Rename kasan_update_kunit_status() to update_kunit_status() (the function is static). - Move the IS_ENABLED(CONFIG_KUNIT) to the function's definition instead of duplicating it at call sites. - Obtain and check current->kunit_test within the function. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/report.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 2d892ec050be..59db81211b8a 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -357,24 +357,31 @@ static bool report_enabled(void) } #if IS_ENABLED(CONFIG_KUNIT) -static void kasan_update_kunit_status(struct kunit *cur_test, bool sync) +static void update_kunit_status(bool sync) { + struct kunit *test; struct kunit_resource *resource; struct kunit_kasan_status *status; - resource = kunit_find_named_resource(cur_test, "kasan_status"); + test = current->kunit_test; + if (!test) + return; + resource = kunit_find_named_resource(test, "kasan_status"); if (!resource) { - kunit_set_failure(cur_test); + kunit_set_failure(test); return; } status = (struct kunit_kasan_status *)resource->data; WRITE_ONCE(status->report_found, true); WRITE_ONCE(status->sync_fault, sync); + kunit_put_resource(resource); } -#endif /* IS_ENABLED(CONFIG_KUNIT) */ +#else +static void update_kunit_status(bool sync) { } +#endif void kasan_report_invalid_free(void *object, unsigned long ip) { @@ -383,10 +390,7 @@ void kasan_report_invalid_free(void *object, unsigned long ip) object = kasan_reset_tag(object); -#if IS_ENABLED(CONFIG_KUNIT) - if (current->kunit_test) - kasan_update_kunit_status(current->kunit_test, true); -#endif /* IS_ENABLED(CONFIG_KUNIT) */ + update_kunit_status(true); start_report(&flags); pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); @@ -402,10 +406,7 @@ void kasan_report_async(void) { unsigned long flags; -#if IS_ENABLED(CONFIG_KUNIT) - if (current->kunit_test) - kasan_update_kunit_status(current->kunit_test, false); -#endif /* IS_ENABLED(CONFIG_KUNIT) */ + update_kunit_status(false); start_report(&flags); pr_err("BUG: KASAN: invalid-access\n"); @@ -424,10 +425,7 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write, void *untagged_addr; unsigned long flags; -#if IS_ENABLED(CONFIG_KUNIT) - if (current->kunit_test) - kasan_update_kunit_status(current->kunit_test, true); -#endif /* IS_ENABLED(CONFIG_KUNIT) */ + update_kunit_status(true); disable_trace_on_warning(); From patchwork Wed Mar 2 16:36:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766295 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 DDD38C433EF for ; Wed, 2 Mar 2022 16:37:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5ED008D0001; Wed, 2 Mar 2022 11:37:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 59AFD8D0009; Wed, 2 Mar 2022 11:37:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3EE748D0001; Wed, 2 Mar 2022 11:37:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 139408D0009 for ; Wed, 2 Mar 2022 11:37:55 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id D7E7661B3F for ; Wed, 2 Mar 2022 16:37:54 +0000 (UTC) X-FDA: 79200002868.12.757DEC9 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf12.hostedemail.com (Postfix) with ESMTP id 4EF734000C for ; Wed, 2 Mar 2022 16:37:54 +0000 (UTC) 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=1646239073; 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=L6CSCTWOxlFTjCUaDQCg92iZqD/oJAA5cp+h5CudQM0=; b=IEetNTQJKp8rSFcLOgVkVj+JLPu8/d9JDYmmx9tBOAw5O8yADVRymYyXX7dCZVZTfdDojh t7OQUZtYSynSIzMGT6pcoEY7lpO5HnGdUxrvS6Sc4MVKQ4lko8XLsusJLla0BXr/MKdyAj KML1RT69q8S4HTyr7ZbSuaFLKu19EZ0= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 08/22] kasan: check CONFIG_KASAN_KUNIT_TEST instead of CONFIG_KUNIT Date: Wed, 2 Mar 2022 17:36:28 +0100 Message-Id: <223592d38d2a601a160a3b2b3d5a9f9090350e62.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 4EF734000C X-Stat-Signature: m5m5ttxm1qw8bkspr1f93g5mwc1pqmzp Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IEetNTQJ; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-HE-Tag: 1646239074-762219 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: From: Andrey Konovalov Check the more specific CONFIG_KASAN_KUNIT_TEST config option when defining things related to KUnit-compatible KASAN tests instead of CONFIG_KUNIT. Also put the kunit_kasan_status definition next to the definitons of other KASAN-related structs. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/kasan.h | 18 ++++++++---------- mm/kasan/report.c | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 4447df0d7343..cc7162a9f304 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -7,16 +7,6 @@ #include #include -#if IS_ENABLED(CONFIG_KUNIT) - -/* Used in KUnit-compatible KASAN tests. */ -struct kunit_kasan_status { - bool report_found; - bool sync_fault; -}; - -#endif - #ifdef CONFIG_KASAN_HW_TAGS #include @@ -224,6 +214,14 @@ struct kasan_free_meta { #endif }; +#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) +/* Used in KUnit-compatible KASAN tests. */ +struct kunit_kasan_status { + bool report_found; + bool sync_fault; +}; +#endif + struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache, const void *object); #ifdef CONFIG_KASAN_GENERIC diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 59db81211b8a..93543157d3e1 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -356,7 +356,7 @@ static bool report_enabled(void) return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); } -#if IS_ENABLED(CONFIG_KUNIT) +#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) static void update_kunit_status(bool sync) { struct kunit *test; From patchwork Wed Mar 2 16:36:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766296 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 62C54C433F5 for ; Wed, 2 Mar 2022 16:37:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B5BE8D0009; Wed, 2 Mar 2022 11:37:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4164C8D0006; Wed, 2 Mar 2022 11:37:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 242958D0009; Wed, 2 Mar 2022 11:37:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id F10618D0006 for ; Wed, 2 Mar 2022 11:37:55 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BB0D31D54 for ; Wed, 2 Mar 2022 16:37:55 +0000 (UTC) X-FDA: 79200002910.05.EB7EAB2 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf24.hostedemail.com (Postfix) with ESMTP id 1A515180015 for ; Wed, 2 Mar 2022 16:37:54 +0000 (UTC) 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=1646239073; 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=swbTnyAsWMCMrpYXsiQkz6QMlKhIbhxptnYL52Z/im0=; b=ZVIurZ/9fvOAIOeiHUu/51xa78T1S/EHbzaNuZFvr3Srvw8s0IvbIDUTTKHU5Gu1lJOxjk ErkFaSBxK5IA9zzvUzW0UfjElafHDIE7JrnZlrAYQnAs4Dz/71sspxvUR5aLoZkM4KKxR4 ikwGE2jqH2t6ZZKqmGUUwGZ7eIfyuOA= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 09/22] kasan: move update_kunit_status to start_report Date: Wed, 2 Mar 2022 17:36:29 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 1A515180015 X-Rspam-User: Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="ZVIurZ/9"; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Stat-Signature: 36tr6c3gb6wsouz5ggz3tm1q3db4z6fr X-HE-Tag: 1646239074-634809 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: From: Andrey Konovalov Instead of duplicating calls to update_kunit_status() in every error report routine, call it once in start_report(). Pass the sync flag as an additional argument to start_report(). Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 75 +++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 93543157d3e1..0b6c8a14f0ea 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -98,13 +98,40 @@ static void print_error_description(struct kasan_access_info *info) info->access_addr, current->comm, task_pid_nr(current)); } +#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) +static void update_kunit_status(bool sync) +{ + struct kunit *test; + struct kunit_resource *resource; + struct kunit_kasan_status *status; + + test = current->kunit_test; + if (!test) + return; + + resource = kunit_find_named_resource(test, "kasan_status"); + if (!resource) { + kunit_set_failure(test); + return; + } + + status = (struct kunit_kasan_status *)resource->data; + WRITE_ONCE(status->report_found, true); + WRITE_ONCE(status->sync_fault, sync); + + kunit_put_resource(resource); +} +#else +static void update_kunit_status(bool sync) { } +#endif + static DEFINE_SPINLOCK(report_lock); -static void start_report(unsigned long *flags) +static void start_report(unsigned long *flags, bool sync) { - /* - * Make sure we don't end up in loop. - */ + /* Update status of the currently running KASAN test. */ + update_kunit_status(sync); + /* Make sure we don't end up in loop. */ kasan_disable_current(); spin_lock_irqsave(&report_lock, *flags); pr_err("==================================================================\n"); @@ -356,33 +383,6 @@ static bool report_enabled(void) return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); } -#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) -static void update_kunit_status(bool sync) -{ - struct kunit *test; - struct kunit_resource *resource; - struct kunit_kasan_status *status; - - test = current->kunit_test; - if (!test) - return; - - resource = kunit_find_named_resource(test, "kasan_status"); - if (!resource) { - kunit_set_failure(test); - return; - } - - status = (struct kunit_kasan_status *)resource->data; - WRITE_ONCE(status->report_found, true); - WRITE_ONCE(status->sync_fault, sync); - - kunit_put_resource(resource); -} -#else -static void update_kunit_status(bool sync) { } -#endif - void kasan_report_invalid_free(void *object, unsigned long ip) { unsigned long flags; @@ -390,9 +390,7 @@ void kasan_report_invalid_free(void *object, unsigned long ip) object = kasan_reset_tag(object); - update_kunit_status(true); - - start_report(&flags); + start_report(&flags, true); pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); kasan_print_tags(tag, object); pr_err("\n"); @@ -406,9 +404,7 @@ void kasan_report_async(void) { unsigned long flags; - update_kunit_status(false); - - start_report(&flags); + start_report(&flags, false); pr_err("BUG: KASAN: invalid-access\n"); pr_err("Asynchronous mode enabled: no access details available\n"); pr_err("\n"); @@ -425,9 +421,8 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write, void *untagged_addr; unsigned long flags; - update_kunit_status(true); - disable_trace_on_warning(); + start_report(&flags, true); tagged_addr = (void *)addr; untagged_addr = kasan_reset_tag(tagged_addr); @@ -442,8 +437,6 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write, info.is_write = is_write; info.ip = ip; - start_report(&flags); - print_error_description(&info); if (addr_has_metadata(untagged_addr)) kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr); From patchwork Wed Mar 2 16:36:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766298 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 B1A88C433EF for ; Wed, 2 Mar 2022 16:38:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 095418D0005; Wed, 2 Mar 2022 11:38:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01E958D0007; Wed, 2 Mar 2022 11:37:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E50328D0005; Wed, 2 Mar 2022 11:37:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id B79738D0007 for ; Wed, 2 Mar 2022 11:37:59 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8F39B2119E for ; Wed, 2 Mar 2022 16:37:59 +0000 (UTC) X-FDA: 79200003078.04.219A216 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf19.hostedemail.com (Postfix) with ESMTP id 902651A001A for ; Wed, 2 Mar 2022 16:37:56 +0000 (UTC) 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=1646239074; 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=4cKKYZ6rFTNdllIAOfnpLukoRzeAYo94WNiwHmUVtvE=; b=HVbToheIxR10VUP8ZlBKwBpe96+cP7ZagHwjrCt0/PJTSNJAVAmgv4JjI3toxGepTU/P+x kxBUHAO6r9T8Ca7DmIdW5ZyIoqecbSNt8eWrMkNdaBe81yZJigAWXY2dR1/2QfOthDk1I+ EZlcs8NWhHuUPzVWc6JhOYmxDyHVn4U= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 10/22] kasan: move disable_trace_on_warning to start_report Date: Wed, 2 Mar 2022 17:36:30 +0100 Message-Id: <7c066c5de26234ad2cebdd931adfe437f8a95d58.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 902651A001A X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=HVbToheI; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Stat-Signature: 5ibswddng1gsq8qdim8x95kpk1qefbyz X-HE-Tag: 1646239076-158113 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: From: Andrey Konovalov Move the disable_trace_on_warning() call, which enables the /proc/sys/kernel/traceoff_on_warning interface for KASAN bugs, to start_report(), so that it functions for all types of KASAN reports. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- mm/kasan/report.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 0b6c8a14f0ea..9286ff6ae1a7 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -129,6 +129,8 @@ static DEFINE_SPINLOCK(report_lock); static void start_report(unsigned long *flags, bool sync) { + /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */ + disable_trace_on_warning(); /* Update status of the currently running KASAN test. */ update_kunit_status(sync); /* Make sure we don't end up in loop. */ @@ -421,7 +423,6 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write, void *untagged_addr; unsigned long flags; - disable_trace_on_warning(); start_report(&flags, true); tagged_addr = (void *)addr; From patchwork Wed Mar 2 16:36:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766297 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 5F599C433EF for ; Wed, 2 Mar 2022 16:38:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0ADC8D0006; Wed, 2 Mar 2022 11:37:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BA8C8D0005; Wed, 2 Mar 2022 11:37:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 834648D0006; Wed, 2 Mar 2022 11:37:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0151.hostedemail.com [216.40.44.151]) by kanga.kvack.org (Postfix) with ESMTP id 667B98D0005 for ; Wed, 2 Mar 2022 11:37:59 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 2895D181CA77A for ; Wed, 2 Mar 2022 16:37:59 +0000 (UTC) X-FDA: 79200003078.30.06E0615 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf05.hostedemail.com (Postfix) with ESMTP id B0578100004 for ; Wed, 2 Mar 2022 16:37:56 +0000 (UTC) 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=1646239075; 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=IRHB3LUc8++0z9iu+FYKeD7NJQddTec6mZHPvLpKAps=; b=NANHGuVo79ac5h1H9z/xdbN0S2ODebC1PPyX7v+0niwvNGDbza3rCrxqcl3DL4YRtR9DD1 wSLvWbTLE+wqDALlXZXFka7ou//FdTOhlj58dZ8IqPanORtbIJqtFMOpQxJnjpdBkNT/OP KjF5N6KaUSsac5BDFSM8aLHfBo4gFL0= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 11/22] kasan: split out print_report from __kasan_report Date: Wed, 2 Mar 2022 17:36:31 +0100 Message-Id: <9be3ed99dd24b9c4e1c4a848b69a0c6ecefd845e.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B0578100004 X-Stat-Signature: ya5ozu4ab1cuetgxkopmqy3w3nfnr3ce Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NANHGuVo; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1646239076-875280 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: From: Andrey Konovalov 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. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 7 +++--- mm/kasan/report.c | 58 +++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index cc7162a9f304..40b863e289ec 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -128,8 +128,8 @@ static inline bool kasan_sync_fault_possible(void) #define META_ROWS_AROUND_ADDR 2 struct kasan_access_info { - const void *access_addr; - const void *first_bad_addr; + void *access_addr; + void *first_bad_addr; size_t access_size; bool is_write; unsigned long ip; @@ -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) { - return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START)); + return (kasan_reset_tag(addr) >= + kasan_shadow_to_mem((void *)KASAN_SHADOW_START)); } /** diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 9286ff6ae1a7..bb4c29b439b1 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -139,10 +139,11 @@ static void start_report(unsigned long *flags, bool sync) pr_err("==================================================================\n"); } -static void end_report(unsigned long *flags, unsigned long addr) +static void end_report(unsigned long *flags, void *addr) { if (addr) - trace_error_report_end(ERROR_DETECTOR_KASAN, addr); + trace_error_report_end(ERROR_DETECTOR_KASAN, + (unsigned long)addr); pr_err("==================================================================\n"); add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); spin_unlock_irqrestore(&report_lock, *flags); @@ -398,7 +399,7 @@ void kasan_report_invalid_free(void *object, unsigned long ip) pr_err("\n"); print_address_description(object, tag); print_memory_metadata(object); - end_report(&flags, (unsigned long)object); + end_report(&flags, object); } #ifdef CONFIG_KASAN_HW_TAGS @@ -411,44 +412,47 @@ void kasan_report_async(void) pr_err("Asynchronous mode enabled: no access details available\n"); pr_err("\n"); dump_stack_lvl(KERN_ERR); - end_report(&flags, 0); + end_report(&flags, NULL); } #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) { struct kasan_access_info info; - void *tagged_addr; - void *untagged_addr; unsigned long flags; start_report(&flags, true); - tagged_addr = (void *)addr; - untagged_addr = kasan_reset_tag(tagged_addr); - - info.access_addr = tagged_addr; - if (addr_has_metadata(untagged_addr)) - info.first_bad_addr = - kasan_find_first_bad_addr(tagged_addr, size); + info.access_addr = addr; + if (addr_has_metadata(addr)) + info.first_bad_addr = kasan_find_first_bad_addr(addr, size); else - info.first_bad_addr = untagged_addr; + info.first_bad_addr = addr; info.access_size = size; info.is_write = is_write; info.ip = ip; - print_error_description(&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); - } + print_report(&info); end_report(&flags, addr); } @@ -460,7 +464,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, bool ret = false; if (likely(report_enabled())) { - __kasan_report(addr, size, is_write, ip); + __kasan_report((void *)addr, size, is_write, ip); ret = true; } From patchwork Wed Mar 2 16:36:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766299 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 A7E68C433F5 for ; Wed, 2 Mar 2022 16:38:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C8648D0003; Wed, 2 Mar 2022 11:38:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 377258D0002; Wed, 2 Mar 2022 11:38:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 28E7D8D0003; Wed, 2 Mar 2022 11:38:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id 184AF8D0002 for ; Wed, 2 Mar 2022 11:38:59 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id CC6A1181CAC6D for ; Wed, 2 Mar 2022 16:38:58 +0000 (UTC) X-FDA: 79200005556.20.901CDD3 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf05.hostedemail.com (Postfix) with ESMTP id 4DED3100002 for ; Wed, 2 Mar 2022 16:38:58 +0000 (UTC) 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=1646239137; 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=JGZE0bvysohYUpkupChxa3EWr4j93F81DU4moq+5lLk=; b=pFiF5ZBBKwtr5haHzZ+mlJbBIPwPTTGaaQU/estweYKXFq/g+JBe8ekDyrppDB98msQPnO MZPXUBggNulqGy/S/jMmMd7r0Mufkp/3WPqcCmDVpa+EQXPQm/NWEjD/X8xNgK3Chg+g85 0qMysA0AKdryYWdV+sdDRlnH0dqADD4= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 12/22] kasan: simplify kasan_find_first_bad_addr call sites Date: Wed, 2 Mar 2022 17:36:32 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam10 X-Rspam-User: X-Stat-Signature: 4aqrzoxnyqqpyzhkk3gscgc9czkmozag Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pFiF5ZBB; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspamd-Queue-Id: 4DED3100002 X-HE-Tag: 1646239138-103245 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000167, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Move the addr_has_metadata() check into kasan_find_first_bad_addr(). Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 5 +---- mm/kasan/report_generic.c | 4 ++++ mm/kasan/report_hw_tags.c | 1 + mm/kasan/report_sw_tags.c | 4 ++++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index bb4c29b439b1..a0d4a9d3f933 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -444,10 +444,7 @@ static void __kasan_report(void *addr, size_t size, bool is_write, start_report(&flags, true); info.access_addr = addr; - if (addr_has_metadata(addr)) - info.first_bad_addr = kasan_find_first_bad_addr(addr, size); - else - info.first_bad_addr = addr; + info.first_bad_addr = kasan_find_first_bad_addr(addr, size); info.access_size = size; info.is_write = is_write; info.ip = ip; diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 7e03cca569a7..182239ca184c 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -34,8 +34,12 @@ void *kasan_find_first_bad_addr(void *addr, size_t size) { void *p = addr; + if (!addr_has_metadata(p)) + return p; + while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p))) p += KASAN_GRANULE_SIZE; + return p; } diff --git a/mm/kasan/report_hw_tags.c b/mm/kasan/report_hw_tags.c index 5dbbbb930e7a..f3d3be614e4b 100644 --- a/mm/kasan/report_hw_tags.c +++ b/mm/kasan/report_hw_tags.c @@ -17,6 +17,7 @@ void *kasan_find_first_bad_addr(void *addr, size_t size) { + /* Return the same value regardless of whether addr_has_metadata(). */ return kasan_reset_tag(addr); } diff --git a/mm/kasan/report_sw_tags.c b/mm/kasan/report_sw_tags.c index 44577b8d47a7..68724ba3d814 100644 --- a/mm/kasan/report_sw_tags.c +++ b/mm/kasan/report_sw_tags.c @@ -35,8 +35,12 @@ void *kasan_find_first_bad_addr(void *addr, size_t size) void *p = kasan_reset_tag(addr); void *end = p + size; + if (!addr_has_metadata(p)) + return p; + while (p < end && tag == *(u8 *)kasan_mem_to_shadow(p)) p += KASAN_GRANULE_SIZE; + return p; } From patchwork Wed Mar 2 16:36:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766300 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 A9E6BC433EF for ; Wed, 2 Mar 2022 16:39:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 379268D0002; Wed, 2 Mar 2022 11:39:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 32D418D0001; Wed, 2 Mar 2022 11:39:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CBBA8D0002; Wed, 2 Mar 2022 11:39:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0192.hostedemail.com [216.40.44.192]) by kanga.kvack.org (Postfix) with ESMTP id ECDD48D0001 for ; Wed, 2 Mar 2022 11:38:59 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id A2A9D9ACAE for ; Wed, 2 Mar 2022 16:38:59 +0000 (UTC) X-FDA: 79200005598.17.67FA0F3 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf28.hostedemail.com (Postfix) with ESMTP id 002D0C0017 for ; Wed, 2 Mar 2022 16:38:58 +0000 (UTC) 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=1646239137; 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=GA4CzC5kQCyt9Q46WjPbGlANWaRE9EWTbe6SrvYEjAk=; b=aaBHXo5y2T1+Vm+RNaREb41iINBlTfgGfciO9VaNYAxyxUoq7gYiXaDkPdMoxsduX3sA9g TzAIUkekbB8xhUA3Qo3mXL+z0ndkfEB2sBoCKWQJ2d3hO4mxiEgs5KkR9zFLzvL95BCF95 U/IBKTxSoTLvCJszTBkqfdNgKa1YG4o= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 13/22] kasan: restructure kasan_report Date: Wed, 2 Mar 2022 17:36:33 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 002D0C0017 X-Stat-Signature: baf3ixg4mnh8658uac8a9uf4sof1gg9k Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=aaBHXo5y; spf=pass (imf28.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-HE-Tag: 1646239138-76510 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: From: Andrey Konovalov Restructure kasan_report() to make reviewing the subsequent patches easier. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index a0d4a9d3f933..41c7966451e3 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -457,15 +457,18 @@ static void __kasan_report(void *addr, size_t size, bool is_write, bool kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip) { - unsigned long flags = user_access_save(); - bool ret = false; + unsigned long ua_flags = user_access_save(); + bool ret = true; - if (likely(report_enabled())) { - __kasan_report((void *)addr, size, is_write, ip); - ret = true; + if (unlikely(!report_enabled())) { + ret = false; + goto out; } - user_access_restore(flags); + __kasan_report((void *)addr, size, is_write, ip); + +out: + user_access_restore(ua_flags); return ret; } From patchwork Wed Mar 2 16:36:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766304 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 71935C4332F for ; Wed, 2 Mar 2022 16:39:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8ED08D0006; Wed, 2 Mar 2022 11:39:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D3D248D0001; Wed, 2 Mar 2022 11:39:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB7568D0006; Wed, 2 Mar 2022 11:39:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id AC5168D0001 for ; Wed, 2 Mar 2022 11:39:03 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7F3A1222D9 for ; Wed, 2 Mar 2022 16:39:03 +0000 (UTC) X-FDA: 79200005766.05.11AF40E Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf21.hostedemail.com (Postfix) with ESMTP id 3D7D11C001C for ; Wed, 2 Mar 2022 16:39:01 +0000 (UTC) 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=1646239138; 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=M062mhOVYVf9bJIp01ZOeX3qpFJNVUV/wAEZA8I272E=; b=lH9wtAh7UDSWF/e9AczfTV9enN4uq/JJvUnkvS3JD5lO39CTU8KgFlOa8XuaMRO5OBz/Rk +Gte5syiG2TrYhFZ5NuL2BGI/f9VgTmWVR+r0hvmFWvNp9TqUusWNQXJrSNdKo9w9VVizH pDmUp3Zb0WD+KB7MoptfkaUEEtwc450= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 14/22] kasan: merge __kasan_report into kasan_report Date: Wed, 2 Mar 2022 17:36:34 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3D7D11C001C X-Stat-Signature: y1it9gj19uhwxwupa3acxp8qtogcxphm Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=lH9wtAh7; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-HE-Tag: 1646239141-180596 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: From: Andrey Konovalov Merge __kasan_report() into kasan_report(). The code is simple enough to be readable without the __kasan_report() helper. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 41c7966451e3..56d5ba235542 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -435,37 +435,31 @@ static void print_report(struct kasan_access_info *info) } } -static void __kasan_report(void *addr, size_t size, bool is_write, - unsigned long ip) -{ - struct kasan_access_info info; - unsigned long flags; - - start_report(&flags, true); - - info.access_addr = addr; - info.first_bad_addr = kasan_find_first_bad_addr(addr, size); - info.access_size = size; - info.is_write = is_write; - info.ip = ip; - - print_report(&info); - - end_report(&flags, addr); -} - bool kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip) { - unsigned long ua_flags = user_access_save(); bool ret = true; + void *ptr = (void *)addr; + unsigned long ua_flags = user_access_save(); + unsigned long irq_flags; + struct kasan_access_info info; if (unlikely(!report_enabled())) { ret = false; goto out; } - __kasan_report((void *)addr, size, is_write, ip); + start_report(&irq_flags, true); + + info.access_addr = ptr; + info.first_bad_addr = kasan_find_first_bad_addr(ptr, size); + info.access_size = size; + info.is_write = is_write; + info.ip = ip; + + print_report(&info); + + end_report(&irq_flags, ptr); out: user_access_restore(ua_flags); From patchwork Wed Mar 2 16:36:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766301 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 1E8AFC433FE for ; Wed, 2 Mar 2022 16:39:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54F798D0007; Wed, 2 Mar 2022 11:39:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5011C8D0001; Wed, 2 Mar 2022 11:39:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DD408D0007; Wed, 2 Mar 2022 11:39:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 1DFB48D0001 for ; Wed, 2 Mar 2022 11:39:01 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DE11E250CE for ; Wed, 2 Mar 2022 16:39:00 +0000 (UTC) X-FDA: 79200005640.03.DAD3930 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf29.hostedemail.com (Postfix) with ESMTP id 2F910120014 for ; Wed, 2 Mar 2022 16:39:00 +0000 (UTC) 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=1646239139; 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=QsCdzSP/k33pOndnGnQGapEj7FynIvIpYn3S3i9tIEc=; b=bWJzl9fxs9+Hc43oKBCqXMPfpYIMGnCyPHr9sDfvUzPlmnCwrFeFx7NDs4btAWRHiFZHKj K3vqwcR91Lf9ARkARw3MfapJCPFw3OiKFT1v+6IGHBoUr7f9MTNW0FA6iat+mHcLbsIJG/ NlqxwlafTq+F9XbykzoviXQXCxoZN1A= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 15/22] kasan: call print_report from kasan_report_invalid_free Date: Wed, 2 Mar 2022 17:36:35 +0100 Message-Id: <9ea6f0604c5d2e1fb28d93dc6c44232c1f8017fe.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2F910120014 X-Stat-Signature: ij49nw35yti8wccdibdwk9mzfumf9i7j Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=bWJzl9fx; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-HE-Tag: 1646239140-115888 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: From: Andrey Konovalov Call print_report() in kasan_report_invalid_free() instead of calling printing functions directly. Compared to the existing implementation of kasan_report_invalid_free(), print_report() makes sure that the buggy address has metadata before printing it. The change requires adding a report type field into kasan_access_info and using it accordingly. kasan_report_async() is left as is, as using print_report() will only complicate the code. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 6 ++++++ mm/kasan/report.c | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 40b863e289ec..8c9a855152c2 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -127,7 +127,13 @@ static inline bool kasan_sync_fault_possible(void) #define META_MEM_BYTES_PER_ROW (META_BYTES_PER_ROW * KASAN_GRANULE_SIZE) #define META_ROWS_AROUND_ADDR 2 +enum kasan_report_type { + KASAN_REPORT_ACCESS, + KASAN_REPORT_INVALID_FREE, +}; + struct kasan_access_info { + enum kasan_report_type type; void *access_addr; void *first_bad_addr; size_t access_size; diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 56d5ba235542..73348f83b813 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -86,6 +86,12 @@ __setup("kasan_multi_shot", kasan_set_multi_shot); static void print_error_description(struct kasan_access_info *info) { + if (info->type == KASAN_REPORT_INVALID_FREE) { + pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", + (void *)info->ip); + return; + } + pr_err("BUG: KASAN: %s in %pS\n", kasan_get_bug_type(info), (void *)info->ip); if (info->access_size) @@ -386,22 +392,6 @@ static bool report_enabled(void) return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); } -void kasan_report_invalid_free(void *object, unsigned long ip) -{ - unsigned long flags; - u8 tag = get_tag(object); - - object = kasan_reset_tag(object); - - start_report(&flags, true); - pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); - kasan_print_tags(tag, object); - pr_err("\n"); - print_address_description(object, tag); - print_memory_metadata(object); - end_report(&flags, object); -} - #ifdef CONFIG_KASAN_HW_TAGS void kasan_report_async(void) { @@ -435,6 +425,25 @@ static void print_report(struct kasan_access_info *info) } } +void kasan_report_invalid_free(void *ptr, unsigned long ip) +{ + unsigned long flags; + struct kasan_access_info info; + + start_report(&flags, true); + + info.type = KASAN_REPORT_INVALID_FREE; + info.access_addr = ptr; + info.first_bad_addr = kasan_reset_tag(ptr); + info.access_size = 0; + info.is_write = false; + info.ip = ip; + + print_report(&info); + + end_report(&flags, ptr); +} + bool kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip) { @@ -451,6 +460,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, start_report(&irq_flags, true); + info.type = KASAN_REPORT_ACCESS; info.access_addr = ptr; info.first_bad_addr = kasan_find_first_bad_addr(ptr, size); info.access_size = size; From patchwork Wed Mar 2 16:36:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766302 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 A9CF4C4332F for ; Wed, 2 Mar 2022 16:39:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D67DC8D0008; Wed, 2 Mar 2022 11:39:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC6438D0001; Wed, 2 Mar 2022 11:39:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3F418D0008; Wed, 2 Mar 2022 11:39:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 9BE728D0001 for ; Wed, 2 Mar 2022 11:39:01 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 69CC424F29 for ; Wed, 2 Mar 2022 16:39:01 +0000 (UTC) X-FDA: 79200005682.12.6854D89 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf13.hostedemail.com (Postfix) with ESMTP id C67F620008 for ; Wed, 2 Mar 2022 16:39:00 +0000 (UTC) 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=1646239139; 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=9LoFBOx+41NuRW+TN1sbV0xBqJ4TCv0zuyZEci6w9hM=; b=akh6ZCXAq6FQiLjzz0h/T1o90vGgV7kP6AJdUAegzuJcUmFmx91g40h8iMe6ExZqRMVg8R 8vORXjnqmTNanqHasHPqrgDKVLA2b5UP/6An2XkkoipMm/2RQn3mf4ibXNC/PyVtnaQaIe NfFWVZDuiLbt7SARW7bRtOptFFXS3RE= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 16/22] kasan: move and simplify kasan_report_async Date: Wed, 2 Mar 2022 17:36:36 +0100 Message-Id: <52d942ef3ffd29bdfa225bbe8e327bc5bda7ab09.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: C67F620008 X-Rspam-User: Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=akh6ZCXA; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Stat-Signature: ntuhkexs4h8zsxybg8ycqjs94538pbgb X-HE-Tag: 1646239140-112530 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: From: Andrey Konovalov Place kasan_report_async() next to the other main reporting routines. Also simplify printed information. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 73348f83b813..162fd2d6209e 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -392,20 +392,6 @@ static bool report_enabled(void) return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); } -#ifdef CONFIG_KASAN_HW_TAGS -void kasan_report_async(void) -{ - unsigned long flags; - - start_report(&flags, false); - pr_err("BUG: KASAN: invalid-access\n"); - pr_err("Asynchronous mode enabled: no access details available\n"); - pr_err("\n"); - dump_stack_lvl(KERN_ERR); - end_report(&flags, NULL); -} -#endif /* CONFIG_KASAN_HW_TAGS */ - static void print_report(struct kasan_access_info *info) { void *tagged_addr = info->access_addr; @@ -477,6 +463,20 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, return ret; } +#ifdef CONFIG_KASAN_HW_TAGS +void kasan_report_async(void) +{ + unsigned long flags; + + start_report(&flags, false); + pr_err("BUG: KASAN: invalid-access\n"); + pr_err("Asynchronous fault: no details available\n"); + pr_err("\n"); + dump_stack_lvl(KERN_ERR); + end_report(&flags, NULL); +} +#endif /* CONFIG_KASAN_HW_TAGS */ + #ifdef CONFIG_KASAN_INLINE /* * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high From patchwork Wed Mar 2 16:36:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766303 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 11506C433FE for ; Wed, 2 Mar 2022 16:39:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BCAE8D0009; Wed, 2 Mar 2022 11:39:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01E9E8D0001; Wed, 2 Mar 2022 11:39:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8A918D0009; Wed, 2 Mar 2022 11:39:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id C46558D0001 for ; Wed, 2 Mar 2022 11:39:02 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 93B62121FAB for ; Wed, 2 Mar 2022 16:39:02 +0000 (UTC) X-FDA: 79200005724.03.95FB64E Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf04.hostedemail.com (Postfix) with ESMTP id C956340021 for ; Wed, 2 Mar 2022 16:39:01 +0000 (UTC) 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=1646239140; 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=/qOrM9Ost/e3M5mYf3qg7gFNISCBGPyiXg0v046DQbk=; b=G2aWvWA0mXOV8Xfi1OsPPKcpGyOMuyqdpfpc/ucbBO25DS/utA2Z2XM7mJt5knqzUBT7i8 6Bw1qS92F6mW03fxfSIR6tbK/zZEfxoPo4BidFJ5RN/h2sYWFEr2QWusrzSrX/r64pX265 gEZFrQQfP+rEVwS9SWLMNRQPgO+93WM= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 17/22] kasan: rename kasan_access_info to kasan_report_info Date: Wed, 2 Mar 2022 17:36:37 +0100 Message-Id: <158a4219a5d356901d017352558c989533a0782c.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C956340021 X-Stat-Signature: hga5gfko4zsugwjn5ukmy6und1gwd93n Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=G2aWvWA0; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1646239141-261980 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: From: Andrey Konovalov Rename kasan_access_info to kasan_report_info, as the latter name better reflects the struct's purpose. Signed-off-by: Andrey Konovalov --- mm/kasan/kasan.h | 4 ++-- mm/kasan/report.c | 8 ++++---- mm/kasan/report_generic.c | 6 +++--- mm/kasan/report_tags.c | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 8c9a855152c2..9d2e128eb623 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -132,7 +132,7 @@ enum kasan_report_type { KASAN_REPORT_INVALID_FREE, }; -struct kasan_access_info { +struct kasan_report_info { enum kasan_report_type type; void *access_addr; void *first_bad_addr; @@ -276,7 +276,7 @@ static inline void kasan_print_tags(u8 addr_tag, const void *addr) { } #endif void *kasan_find_first_bad_addr(void *addr, size_t size); -const char *kasan_get_bug_type(struct kasan_access_info *info); +const char *kasan_get_bug_type(struct kasan_report_info *info); void kasan_metadata_fetch_row(char *buffer, void *row); #if defined(CONFIG_KASAN_STACK) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 162fd2d6209e..7915af810815 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -84,7 +84,7 @@ static int __init kasan_set_multi_shot(char *str) } __setup("kasan_multi_shot", kasan_set_multi_shot); -static void print_error_description(struct kasan_access_info *info) +static void print_error_description(struct kasan_report_info *info) { if (info->type == KASAN_REPORT_INVALID_FREE) { pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", @@ -392,7 +392,7 @@ static bool report_enabled(void) return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); } -static void print_report(struct kasan_access_info *info) +static void print_report(struct kasan_report_info *info) { void *tagged_addr = info->access_addr; void *untagged_addr = kasan_reset_tag(tagged_addr); @@ -414,7 +414,7 @@ static void print_report(struct kasan_access_info *info) void kasan_report_invalid_free(void *ptr, unsigned long ip) { unsigned long flags; - struct kasan_access_info info; + struct kasan_report_info info; start_report(&flags, true); @@ -437,7 +437,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, void *ptr = (void *)addr; unsigned long ua_flags = user_access_save(); unsigned long irq_flags; - struct kasan_access_info info; + struct kasan_report_info info; if (unlikely(!report_enabled())) { ret = false; diff --git a/mm/kasan/report_generic.c b/mm/kasan/report_generic.c index 182239ca184c..efc5e79a103f 100644 --- a/mm/kasan/report_generic.c +++ b/mm/kasan/report_generic.c @@ -43,7 +43,7 @@ void *kasan_find_first_bad_addr(void *addr, size_t size) return p; } -static const char *get_shadow_bug_type(struct kasan_access_info *info) +static const char *get_shadow_bug_type(struct kasan_report_info *info) { const char *bug_type = "unknown-crash"; u8 *shadow_addr; @@ -95,7 +95,7 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info) return bug_type; } -static const char *get_wild_bug_type(struct kasan_access_info *info) +static const char *get_wild_bug_type(struct kasan_report_info *info) { const char *bug_type = "unknown-crash"; @@ -109,7 +109,7 @@ static const char *get_wild_bug_type(struct kasan_access_info *info) return bug_type; } -const char *kasan_get_bug_type(struct kasan_access_info *info) +const char *kasan_get_bug_type(struct kasan_report_info *info) { /* * If access_size is a negative number, then it has reason to be diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 1b41de88c53e..e25d2166e813 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -7,7 +7,7 @@ #include "kasan.h" #include "../slab.h" -const char *kasan_get_bug_type(struct kasan_access_info *info) +const char *kasan_get_bug_type(struct kasan_report_info *info) { #ifdef CONFIG_KASAN_TAGS_IDENTIFY struct kasan_alloc_meta *alloc_meta; From patchwork Wed Mar 2 16:36:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766311 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 821BAC433FE for ; Wed, 2 Mar 2022 16:40:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94D728D0001; Wed, 2 Mar 2022 11:40:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AE148D0007; Wed, 2 Mar 2022 11:40:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 684118D0003; Wed, 2 Mar 2022 11:40:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0230.hostedemail.com [216.40.44.230]) by kanga.kvack.org (Postfix) with ESMTP id 372318D0003 for ; Wed, 2 Mar 2022 11:40:05 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id E4844972FA for ; Wed, 2 Mar 2022 16:40:04 +0000 (UTC) X-FDA: 79200008328.16.9F1BF49 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf13.hostedemail.com (Postfix) with ESMTP id 7790420008 for ; Wed, 2 Mar 2022 16:40:03 +0000 (UTC) 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=1646239201; 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=ujXTYxMun+rvpXDm65LBC0iIPgbPBoZhDfYPsPYRrpE=; b=Vvu6o+ZYKZCdbhIWypkrGDaSmrcli8fMm/9H1fIRIwfZo46g18kv3aPNPhnQ4oiv4BpRCx a0CscnTby2H381xSHN6RzxF7C0Z/76Ck5MlwjXipdTqKzc+JfIVrw2/AmZ8ioX0ISlJyut yhcafTGxtI2YEEn5HUWq+BE1JX9xke0= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 18/22] kasan: add comment about UACCESS regions to kasan_report Date: Wed, 2 Mar 2022 17:36:38 +0100 Message-Id: <1201ca3c2be42c7bd077c53d2e46f4a51dd1476a.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7790420008 X-Stat-Signature: uh6re6xs8gmet9ntd89aoum6b8ac5iqe Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Vvu6o+ZY; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1646239203-996846 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: From: Andrey Konovalov Add a comment explaining why kasan_report() is the only reporting function that uses user_access_save/restore(). Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 7915af810815..08631d873204 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -430,6 +430,11 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip) end_report(&flags, ptr); } +/* + * kasan_report() is the only reporting function that uses + * 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) { From patchwork Wed Mar 2 16:36:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766313 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 62453C43217 for ; Wed, 2 Mar 2022 16:40:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF59A8D0007; Wed, 2 Mar 2022 11:40:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9DE448D0006; Wed, 2 Mar 2022 11:40:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CF298D0007; Wed, 2 Mar 2022 11:40:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0162.hostedemail.com [216.40.44.162]) by kanga.kvack.org (Postfix) with ESMTP id 7AD5D8D0006 for ; Wed, 2 Mar 2022 11:40:07 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 39AA81808E684 for ; Wed, 2 Mar 2022 16:40:07 +0000 (UTC) X-FDA: 79200008454.18.61445B7 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf03.hostedemail.com (Postfix) with ESMTP id 4EBD820003 for ; Wed, 2 Mar 2022 16:40:05 +0000 (UTC) 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=1646239202; 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=87o7sANrY5Obyuk6ZGRgPFc8t4HBbQ7LJJT+IQadP5M=; b=d1ASAWaCPabwRxpdrH/NY/MM1LnLKYv4/mm2/x3zmCQ0TPJe6nYt74oEJOUX6GzlRxJ1tX TMMHjPq1X51ecK0RfQA/Qgnn3XG4U8oSuGMKorU5yQucdp5EYczhmMIfFyRl1BKwohpHYn vzDLH7gVTyuSLZIWDIsy97pABMKQdpg= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 19/22] kasan: respect KASAN_BIT_REPORTED in all reporting routines Date: Wed, 2 Mar 2022 17:36:39 +0100 Message-Id: <715e346b10b398e29ba1b425299dcd79e29d58ce.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 4EBD820003 X-Stat-Signature: cwsoup31nnkb1cw3nayng5jcg968xge7 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=d1ASAWaC; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-HE-Tag: 1646239205-208061 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: From: Andrey Konovalov Currently, only kasan_report() checks the KASAN_BIT_REPORTED and KASAN_BIT_MULTI_SHOT flags. Make other reporting routines check these flags as well. Also add explanatory comments. Note that the current->kasan_depth check is split out into report_suppressed() and only called for kasan_report(). Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 08631d873204..ef649f5cee29 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -381,12 +381,26 @@ static void print_memory_metadata(const void *addr) } } -static bool report_enabled(void) +/* + * Used to suppress reports within kasan_disable/enable_current() critical + * sections, which are used for marking accesses to slab metadata. + */ +static bool report_suppressed(void) { #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) if (current->kasan_depth) - return false; + return true; #endif + return false; +} + +/* + * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot + * is enabled. Note that KASAN tests effectively enable kasan_multi_shot + * for their duration. + */ +static bool report_enabled(void) +{ if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) return true; return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); @@ -416,6 +430,14 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip) unsigned long flags; struct kasan_report_info info; + /* + * Do not check report_suppressed(), as an invalid-free cannot be + * caused by accessing slab metadata and thus should not be + * suppressed by kasan_disable/enable_current() critical sections. + */ + if (unlikely(!report_enabled())) + return; + start_report(&flags, true); info.type = KASAN_REPORT_INVALID_FREE; @@ -444,7 +466,7 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long irq_flags; struct kasan_report_info info; - if (unlikely(!report_enabled())) { + if (unlikely(report_suppressed()) || unlikely(!report_enabled())) { ret = false; goto out; } @@ -473,6 +495,13 @@ void kasan_report_async(void) { unsigned long flags; + /* + * Do not check report_suppressed(), as kasan_disable/enable_current() + * critical sections do not affect Hardware Tag-Based KASAN. + */ + if (unlikely(!report_enabled())) + return; + start_report(&flags, false); pr_err("BUG: KASAN: invalid-access\n"); pr_err("Asynchronous fault: no details available\n"); From patchwork Wed Mar 2 16:36:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766309 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 2ED5BC433EF for ; Wed, 2 Mar 2022 16:40:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB6248D0002; Wed, 2 Mar 2022 11:40:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B64A58D0001; Wed, 2 Mar 2022 11:40:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A54878D0002; Wed, 2 Mar 2022 11:40:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0238.hostedemail.com [216.40.44.238]) by kanga.kvack.org (Postfix) with ESMTP id 967A68D0001 for ; Wed, 2 Mar 2022 11:40:04 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 5506295AE6 for ; Wed, 2 Mar 2022 16:40:04 +0000 (UTC) X-FDA: 79200008328.28.8CCC603 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf16.hostedemail.com (Postfix) with ESMTP id A76DF180002 for ; Wed, 2 Mar 2022 16:40:03 +0000 (UTC) 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=1646239202; 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=djMH4j9HV8SXlCfl9Af/XXd5VDpDAiry7CNPlQpwESU=; b=PnZ2t/ANIdtINzZYFaDix7C8GIFWc+Tcr3lo2iGiNpriUbTepgZwinlIDD+IUj0vIZXPAT PUWWJBTw6hHckQ1Q+E98GyyjPVqju9Ydl66WDlbG/CQjQkd9tMJr4XtT5MULXn29AcSCz2 GJxDfbMqH3fNp2GkTIiuEGRW74+fR8I= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 20/22] kasan: reorder reporting functions Date: Wed, 2 Mar 2022 17:36:40 +0100 Message-Id: <82aa926c411e00e76e97e645a551ede9ed0c5e79.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A76DF180002 X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="PnZ2t/AN"; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Stat-Signature: 7zimyuf4nuuhrwqjueqpyk4j4f34kz5d X-HE-Tag: 1646239203-172177 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: From: Andrey Konovalov Move print_error_description()'s, report_suppressed()'s, and report_enabled()'s definitions to improve the logical order of function definitions in report.c. No functional changes. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 82 +++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index ef649f5cee29..7ef3b0455603 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -84,24 +84,29 @@ static int __init kasan_set_multi_shot(char *str) } __setup("kasan_multi_shot", kasan_set_multi_shot); -static void print_error_description(struct kasan_report_info *info) +/* + * Used to suppress reports within kasan_disable/enable_current() critical + * sections, which are used for marking accesses to slab metadata. + */ +static bool report_suppressed(void) { - if (info->type == KASAN_REPORT_INVALID_FREE) { - pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", - (void *)info->ip); - return; - } +#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) + if (current->kasan_depth) + return true; +#endif + return false; +} - pr_err("BUG: KASAN: %s in %pS\n", - kasan_get_bug_type(info), (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, - info->access_addr, current->comm, task_pid_nr(current)); - else - pr_err("%s at addr %px by task %s/%d\n", - info->is_write ? "Write" : "Read", - info->access_addr, current->comm, task_pid_nr(current)); +/* + * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot + * is enabled. Note that KASAN tests effectively enable kasan_multi_shot + * for their duration. + */ +static bool report_enabled(void) +{ + if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) + return true; + return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); } #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) @@ -160,6 +165,26 @@ static void end_report(unsigned long *flags, void *addr) kasan_enable_current(); } +static void print_error_description(struct kasan_report_info *info) +{ + if (info->type == KASAN_REPORT_INVALID_FREE) { + pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", + (void *)info->ip); + return; + } + + pr_err("BUG: KASAN: %s in %pS\n", + kasan_get_bug_type(info), (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, + info->access_addr, current->comm, task_pid_nr(current)); + else + pr_err("%s at addr %px by task %s/%d\n", + info->is_write ? "Write" : "Read", + info->access_addr, current->comm, task_pid_nr(current)); +} + static void print_track(struct kasan_track *track, const char *prefix) { pr_err("%s by task %u:\n", prefix, track->pid); @@ -381,31 +406,6 @@ static void print_memory_metadata(const void *addr) } } -/* - * Used to suppress reports within kasan_disable/enable_current() critical - * sections, which are used for marking accesses to slab metadata. - */ -static bool report_suppressed(void) -{ -#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) - if (current->kasan_depth) - return true; -#endif - return false; -} - -/* - * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot - * is enabled. Note that KASAN tests effectively enable kasan_multi_shot - * for their duration. - */ -static bool report_enabled(void) -{ - if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) - return true; - return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); -} - static void print_report(struct kasan_report_info *info) { void *tagged_addr = info->access_addr; From patchwork Wed Mar 2 16:36:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766310 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 ED491C433F5 for ; Wed, 2 Mar 2022 16:40:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 68C7B8D0005; Wed, 2 Mar 2022 11:40:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 524038D0001; Wed, 2 Mar 2022 11:40:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C4908D0005; Wed, 2 Mar 2022 11:40:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0155.hostedemail.com [216.40.44.155]) by kanga.kvack.org (Postfix) with ESMTP id 248128D0001 for ; Wed, 2 Mar 2022 11:40:05 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D69E8972E6 for ; Wed, 2 Mar 2022 16:40:04 +0000 (UTC) X-FDA: 79200008328.22.157C83B Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf12.hostedemail.com (Postfix) with ESMTP id 2752A40016 for ; Wed, 2 Mar 2022 16:40:04 +0000 (UTC) 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=1646239203; 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=xoA2paey3z+FS2agG9DA8SjKlQXnu1fJwhGoHYkB4f8=; b=eBQyizDuePbrr86tYNcUx/vFZwzzE0ijJKaaqpwjJdXWroQKed3pkHWU/poH1HtMAoNDgZ cb82oWToQ1vY0PyoZz0Jrj7+JifUkj6liyEZFlH4Lofk4SwLHmtre/inkPuJwII6Tg4DH8 yuNwZUCiYKxlOKZ4I+P7bnliTUPkhVc= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 21/22] kasan: move and hide kasan_save_enable/restore_multi_shot Date: Wed, 2 Mar 2022 17:36:41 +0100 Message-Id: <6ba637333b78447f027d775f2d55ab1a40f63c99.1646237226.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2752A40016 X-Stat-Signature: tp5f461xxgbnzdqtdkuzao9k1y49qqio Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=eBQyizDu; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-HE-Tag: 1646239203-999389 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: From: Andrey Konovalov - Move kasan_save_enable/restore_multi_shot() declarations to mm/kasan/kasan.h, as there is no need for them to be visible outside of KASAN implementation. - Only define and export these functions when KASAN tests are enabled. - Move their definitions closer to other test-related code in report.c. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 4 ---- mm/kasan/kasan.h | 7 +++++++ mm/kasan/report.c | 30 +++++++++++++++++------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index fe36215807f7..ceebcb9de7bf 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -267,10 +267,6 @@ static __always_inline bool kasan_check_byte(const void *addr) return true; } - -bool kasan_save_enable_multi_shot(void); -void kasan_restore_multi_shot(bool enabled); - #else /* CONFIG_KASAN */ static inline slab_flags_t kasan_never_merge(void) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 9d2e128eb623..d79b83d673b1 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -492,6 +492,13 @@ static inline bool kasan_arch_is_ready(void) { return true; } #error kasan_arch_is_ready only works in KASAN generic outline mode! #endif +#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST) + +bool kasan_save_enable_multi_shot(void); +void kasan_restore_multi_shot(bool enabled); + +#endif + /* * Exported functions for interfaces called from assembly or from generated * code. Declarations here to avoid warning about missing declarations. diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 7ef3b0455603..c9bfffe931b4 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -64,19 +64,6 @@ static int __init early_kasan_fault(char *arg) } early_param("kasan.fault", early_kasan_fault); -bool kasan_save_enable_multi_shot(void) -{ - return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); -} -EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot); - -void kasan_restore_multi_shot(bool enabled) -{ - if (!enabled) - clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); -} -EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); - static int __init kasan_set_multi_shot(char *str) { set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); @@ -109,6 +96,23 @@ static bool report_enabled(void) return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); } +#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST) + +bool kasan_save_enable_multi_shot(void) +{ + return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); +} +EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot); + +void kasan_restore_multi_shot(bool enabled) +{ + if (!enabled) + clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); +} +EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); + +#endif + #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) static void update_kunit_status(bool sync) { From patchwork Wed Mar 2 16:36:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12766312 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 DB830C4332F for ; Wed, 2 Mar 2022 16:40:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C613D8D0003; Wed, 2 Mar 2022 11:40:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB9868D0006; Wed, 2 Mar 2022 11:40:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A56338D0003; Wed, 2 Mar 2022 11:40:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0111.hostedemail.com [216.40.44.111]) by kanga.kvack.org (Postfix) with ESMTP id 6A0BA8D0006 for ; Wed, 2 Mar 2022 11:40:05 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 2F27E181C9D2C for ; Wed, 2 Mar 2022 16:40:05 +0000 (UTC) X-FDA: 79200008370.28.41ADC8E Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf31.hostedemail.com (Postfix) with ESMTP id 9A5B12000B for ; Wed, 2 Mar 2022 16:40:04 +0000 (UTC) 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=1646239203; 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=EfvBgrtiG9LxjqkuYus7pXkmugj62NhRZroC7mYbdoA=; b=THIJtJGDx8c6x+4JQpn2g1iQhzIDsWS/5VO7QzZGjLDo01uIBOCrChB4ZwgGPBwTPlHGev xXHLCbncZrgFyeUIjdf3mTUK10EXxvJLRLJyvp8BnnakIwuMiW8z8u0wXBHT1GzaCglHU6 pbbL0cwlGXse4K05c9u6n5xLIKgmCZI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 22/22] kasan: disable LOCKDEP when printing reports Date: Wed, 2 Mar 2022 17:36:42 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9A5B12000B X-Stat-Signature: xwanbantbfhjz8fyg4z5t9hp6j4cqmxt Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=THIJtJGD; spf=pass (imf31.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-HE-Tag: 1646239204-958061 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: From: Andrey Konovalov If LOCKDEP detects a bug while KASAN is printing a report and if panic_on_warn is set, KASAN will not be able to finish. Disable LOCKDEP while KASAN is printing a report. See https://bugzilla.kernel.org/show_bug.cgi?id=202115 for an example of the issue. Signed-off-by: Andrey Konovalov --- mm/kasan/report.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index c9bfffe931b4..199d77cce21a 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -148,6 +149,8 @@ static void start_report(unsigned long *flags, bool sync) disable_trace_on_warning(); /* Update status of the currently running KASAN test. */ update_kunit_status(sync); + /* Do not allow LOCKDEP mangling KASAN reports. */ + lockdep_off(); /* Make sure we don't end up in loop. */ kasan_disable_current(); spin_lock_irqsave(&report_lock, *flags); @@ -160,12 +163,13 @@ static void end_report(unsigned long *flags, void *addr) trace_error_report_end(ERROR_DETECTOR_KASAN, (unsigned long)addr); pr_err("==================================================================\n"); - add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); spin_unlock_irqrestore(&report_lock, *flags); if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) panic("panic_on_warn set ...\n"); if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC) panic("kasan.fault=panic set ...\n"); + add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); + lockdep_on(); kasan_enable_current(); }