From patchwork Tue Jul 19 00:10:13 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: 12921928 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 28DF0C43334 for ; Tue, 19 Jul 2022 00:15:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D98166B0071; Mon, 18 Jul 2022 20:15:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCC95940009; Mon, 18 Jul 2022 20:15:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFAC66B0074; Mon, 18 Jul 2022 20:15:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 95B086B0071 for ; Mon, 18 Jul 2022 20:15:47 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 67AE335B60 for ; Tue, 19 Jul 2022 00:15:47 +0000 (UTC) X-FDA: 79701931134.26.680FC01 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf20.hostedemail.com (Postfix) with ESMTP id E67061C008C for ; Tue, 19 Jul 2022 00:15:46 +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=1658189745; 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=QKjK8ZCYDbi7wJ5mRNldR0JmUEuXPl5aWvMQPqK0SzM=; b=NNYTqznYpLLilH6ADHz0XOEI/TSKbuz5CFblIRw1Zi+58+ebbNbin+GL4BY3SVKAONjWwn NGYKTUzzHGr3BJjYaa2hulM9wY5VbQ0zWFYkEsyvidYkxNfAInAxsW6zOEwZaNwV6Iti0J HDjcJ69PTDs68eHarA3zEtEFWuyV6PM= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm v2 33/33] kasan: better identify bug types for tag-based modes Date: Tue, 19 Jul 2022 02:10:13 +0200 Message-Id: <355b5734d7f70a70c87ded21b3c4267e1c401b10.1658189199.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NNYTqznY; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658189747; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QKjK8ZCYDbi7wJ5mRNldR0JmUEuXPl5aWvMQPqK0SzM=; b=uKPs23ta/ONntrRDjlH5UqqylujM46rXT8Jrezq1J1IIjycrYLfwQa6bh9puDKrRCfqATQ 9JLGDpwXtzMwCfVUFPvwLxlQNpuwZcPdme/oDIfCx2bDsj8cK/LEdPjm4TWRFH76LI8E/B r7IJ1UQY6bG1DQgGLCgrEl4osDuFYXM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658189747; a=rsa-sha256; cv=none; b=0Gkc1cy9USPVwaEAr087j+rauYcSeJ6OAdEaKs04X46h7HdiEBUUxec/0/bWqm52ZYtTtj 2TfJD2rnvslZwwYS6YNAsGGE3oFu2MUtXpK3Kt6XPMUPsj+Bw2Oe3klynXeiGvqCjsG+TN tD51UWNsRdnR13d6vA7r9eGqKBKqEnI= X-Rspamd-Queue-Id: E67061C008C Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NNYTqznY; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 4u6zgchi8s47e5jyjz7biwaymcpdzexq X-HE-Tag: 1658189746-724277 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 Identify the bug type for the tag-based modes based on the stack trace entries found in the stack ring. If a free entry is found first (meaning that it was added last), mark the bug as use-after-free. If an alloc entry is found first, mark the bug as slab-out-of-bounds. Otherwise, assign the common bug type. This change returns the functionalify of the previously dropped CONFIG_KASAN_TAGS_IDENTIFY. Signed-off-by: Andrey Konovalov --- mm/kasan/report_tags.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 7e267e69ce19..cedcdc5890bc 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -10,7 +10,7 @@ extern struct kasan_stack_ring stack_ring; -static const char *get_bug_type(struct kasan_report_info *info) +static const char *get_common_bug_type(struct kasan_report_info *info) { /* * If access_size is a negative number, then it has reason to be @@ -37,9 +37,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) bool is_free; bool alloc_found = false, free_found = false; - info->bug_type = get_bug_type(info); - - if (!info->cache || !info->object) + if (!info->cache || !info->object) { + info->bug_type = get_common_bug_type(info); return; } @@ -89,6 +88,13 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) info->free_track.pid = pid; info->free_track.stack = stack; free_found = true; + + /* + * If a free entry is found first, the bug is likely + * a use-after-free. + */ + if (!info->bug_type) + info->bug_type = "use-after-free"; } else { /* Second alloc of the same object. Give up. */ if (alloc_found) @@ -97,8 +103,19 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) info->alloc_track.pid = pid; info->alloc_track.stack = stack; alloc_found = true; + + /* + * If an alloc entry is found first, the bug is likely + * an out-of-bounds. + */ + if (!info->bug_type) + info->bug_type = "slab-out-of-bounds"; } } write_unlock_irqrestore(&stack_ring.lock, flags); + + /* Assign the common bug type if no entries were found. */ + if (!info->bug_type) + info->bug_type = get_common_bug_type(info); }