Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp286103pxm; Wed, 2 Mar 2022 15:27:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJzAMdMKsCiya+Psm6FWt7wG/NPY+TndUkJSswaDtLjhV6ME1TMFJ5IjvCcianrPe1ms9i1m X-Received: by 2002:a63:68c7:0:b0:37c:4e5b:8a93 with SMTP id d190-20020a6368c7000000b0037c4e5b8a93mr1461887pgc.527.1646263650134; Wed, 02 Mar 2022 15:27:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646263650; cv=none; d=google.com; s=arc-20160816; b=kBCXcvEnl9ID6o3w3aNKAj8vW+RSe3tX9TqRsgHqq/k8uTB+dgoGY7eC2+iSFcZkNZ dsORouAgdZ8tbktRR6xku4gLUNurPkZDQZxVTuWCtffNgyAJ2nureuCjFY1iQN4htexY kjJI3pAgO/QZyTZy/YIpVEhiNYpCjRxZc9EXoReHivjbwUdzaZjbH4NswDx6FUWew+JL mSm3RtHdR99jaN6cALJX8XrwNuU6sbQb4GIBY0z9i2mCY+lZmUr+vCwDmJxynP+rYOLl zmF7Uhp/bbHuRPh3dNdIeVUSCj26mK89k+fZ5XA7XX2Loi23BlDD8BQvCDmzzxs3NRNY 4M6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QsCdzSP/k33pOndnGnQGapEj7FynIvIpYn3S3i9tIEc=; b=zxHPhPvjD3gobvWARzwn7SnUYFunjqDSOrGL7pX1xmoPcXxkbwwK2GH1X8yOPX0MHJ S6WNK4EjBBhAF67O0KuIIaXWWL8cvmlKYZXS19z96jBCinxU4J4fd5uwIiRgwSBUS2n5 xUzXsaabNeJja8ARwMJGNfJi8WreJdoQIXoOmrim0wyrs4n2cn6JaLioHkfyNIL1pO12 o9RXZpnfrr+NxyhU+iY7Oqkt2AHYtRnCJ60XW3i4v6j8DMZxeHoiopA/wFXGgI26L8Cy mdl+quXNTAfOEwFMbqSRSNbRAD+u9sFGpbOHSxBYy+Uv+pwhlEtM2fje/U8Kkfd79Uvo Oa7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=bWJzl9fx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id f4-20020a63de04000000b0037313fd9a5csi380630pgg.808.2022.03.02.15.27.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 15:27:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=bWJzl9fx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E2A48CC514; Wed, 2 Mar 2022 14:59:37 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243132AbiCBQjw (ORCPT + 99 others); Wed, 2 Mar 2022 11:39:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238870AbiCBQjp (ORCPT ); Wed, 2 Mar 2022 11:39:45 -0500 Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E0E531369 for ; Wed, 2 Mar 2022 08:39:00 -0800 (PST) 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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.25.1