From patchwork Thu Jan 19 04:01:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13107346 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 35DF4C38142 for ; Thu, 19 Jan 2023 04:01:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 769056B0072; Wed, 18 Jan 2023 23:01:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 718BD6B0073; Wed, 18 Jan 2023 23:01:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E0836B0074; Wed, 18 Jan 2023 23:01:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4F56B6B0072 for ; Wed, 18 Jan 2023 23:01:26 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 21D82AB221 for ; Thu, 19 Jan 2023 04:01:26 +0000 (UTC) X-FDA: 80370198972.06.1759FE5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 527FB40005 for ; Thu, 19 Jan 2023 04:01:24 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WssVKNz8; spf=pass (imf12.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674100884; 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=Eek5YPddR+rdvDYmWL3WCwvTt/kh6fJNelSHfFXusYg=; b=xjNzvJYGJtUTSlCOjA1JRqtfSbbxgsPss76LmOs7KXfn461oNm6BuaOiJsKVaeuy2NgIq3 sDenvRXy1ndT8F4Lc2bojoW4/Pz0uW2ZVC7GsMYooW53CxXlJ4g+GZ5+E9TS7kLMs/RlHw WdY+qnOVeFspLwyH81uQjvP03M7Vf/c= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WssVKNz8; spf=pass (imf12.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674100884; a=rsa-sha256; cv=none; b=reVHa9hjlFWr75w1Q9yuKEPIjKlEI6VgE1jFW2vhCtIfEll9HiaDlv0MR3ZUw/6CAZfIch 3C/rTbYM5/cpyy2dP8xkLfppPTDWvqZfEq6yAhUoBzMyfjKWF9kgq8gEtwffKA1vQzGsAc C+BEGwoPiDl2jD+Pu6DGZtQKHuJ8/yw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674100883; 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=Eek5YPddR+rdvDYmWL3WCwvTt/kh6fJNelSHfFXusYg=; b=WssVKNz8gmd16G6s+XUoT2PmCw5rr7VzwPhUOJEJFUh7I7q7DzFUZ2fQlqzL5mhpkBlBZ8 9ONpzMgxyjFlzjBsoqST50L2wtmo69N7prymQ/nToYmpgBdti4efq56rxYnOML3f+XNB+5 AV8kFxl/kNN5eVUi5tTq8cPxDyQeS60= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-550-JseQALo8MkyN83fBd0phlQ-1; Wed, 18 Jan 2023 23:01:21 -0500 X-MC-Unique: JseQALo8MkyN83fBd0phlQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 657E52A5956F; Thu, 19 Jan 2023 04:01:21 +0000 (UTC) Received: from llong.com (unknown [10.22.8.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C65751E5; Thu, 19 Jan 2023 04:01:21 +0000 (UTC) From: Waiman Long To: Catalin Marinas , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Waiman Long Subject: [RESEND PATCH v2 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Date: Wed, 18 Jan 2023 23:01:11 -0500 Message-Id: <20230119040111.350923-3-longman@redhat.com> In-Reply-To: <20230119040111.350923-1-longman@redhat.com> References: <20230119040111.350923-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 527FB40005 X-Stat-Signature: 111e5pk9hruykffjqdctrm3k9swd59fo X-Rspam-User: X-HE-Tag: 1674100884-159746 X-HE-Meta: U2FsdGVkX19wF68UHzadCRVN2TtaO8HZ1q3pv0BH70DnxU5KNocau2ADVl0zgXlZECnFINooVJBuUUb55nfi+/JWC1EmAdKzQn1zbHAMJFs/JukYgliHueqHD9DPTtEMI4ozkr6JYxP5W4/wjnGfzRRCaj3BGZ3KTUf4Aaz78ZN8hRdBWzKCIqs97CYhkID4ezZcxPF2bQUIgvECHWJUFepS2MQSR3CHdaH6c7gU/dArXKVRlp4czTjmy/pvYmXEJpy4IpBNrmyNJvdQ4mBCdk/fXqEMgE8iZ+mNfz3QZ4U+HAUFcDIs+g5LpMpnSq+fTznUiVSp896xb2R8tVCXUMuETIyyFvWLQNb3qf4ZQV7p7aERiWJvnL5aXX+VNDAVBYN4aUeCPJGsk3UENxzqOmXU7SKIRr30MFOTwptjkIsvPqgE7AgmMEylHRcP+xdd5/0R7aH1Yxb65Y5JfdlT88dw1ujINwN8oKcQ1VtskikIZUEOju6SiYeOMFD3TqdLV5hWatr7iYVZUXAcGqHtMVl8KZnGTpBbVbgCqEe81sna2M3KqjafcUjD5BTs6DpZxv1iEAO5D8FxlF3hWy7Fj6XiMg9deHTjEsoRfvpW229AH30Y+EdQEexnSHFoV83q26e+0Ky6aodoyVv2Gz08yegzwEELur+xwJOX5S+g5b+9Ko6L35Q/Vnx+gaNnJ84KLBgdpY/sMLHokBlGXGqXij/7yD8GWFWzHAew+6m3L0J87PFWQuLIo84K59TG3Sls300qrY6RTp5wmGy+JMMU/Aq8/m6RbjSKHohK62W0iRh4d4eK/cdG+cOi6ERYXWSGZRAiJ3hjHz0tD3LjlNir9+sO6MccbGr+bQWi1p42kxtWXdfe8n434heJeLX8AQZQYx18KmIzQzOHXeMMD7QF0EfNx78koil7PGeAlU5Kr1oFcYJRyPQrhG/khJOvJ+1JJ92NyTuzHhI= 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: Commit 6edda04ccc7c ("mm/kmemleak: prevent soft lockup in first object iteration loop of kmemleak_scan()") fixes soft lockup problem in kmemleak_scan() by periodically doing a cond_resched(). It does take a reference of the current object before doing it. Unfortunately, if the object has been deleted from the object_list, the next object pointed to by its next pointer may no longer be valid after coming back from cond_resched(). This can result in use-after-free and other nasty problem. Fix this problem by adding a del_state flag into kmemleak_object structure to synchronize the object deletion process between kmemleak_cond_resched() and __remove_object() to make sure that the object remained in the object_list in the duration of the cond_resched() call. Fixes: 6edda04ccc7c ("mm/kmemleak: prevent soft lockup in first object iteration loop of kmemleak_scan()") Signed-off-by: Waiman Long Reviewed-by: Catalin Marinas --- mm/kmemleak.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index e7cb521236bf..0ece170fc9ef 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -13,11 +13,12 @@ * * The following locks and mutexes are used by kmemleak: * - * - kmemleak_lock (raw_spinlock_t): protects the object_list modifications and - * accesses to the object_tree_root (or object_phys_tree_root). The - * object_list is the main list holding the metadata (struct kmemleak_object) - * for the allocated memory blocks. The object_tree_root and object_phys_tree_root - * are red black trees used to look-up metadata based on a pointer to the + * - kmemleak_lock (raw_spinlock_t): protects the object_list as well as + * del_state modifications and accesses to the object_tree_root (or + * object_phys_tree_root). The object_list is the main list holding the + * metadata (struct kmemleak_object) for the allocated memory blocks. + * The object_tree_root and object_phys_tree_root are red + * black trees used to look-up metadata based on a pointer to the * corresponding memory block. The object_phys_tree_root is for objects * allocated with physical address. The kmemleak_object structures are * added to the object_list and object_tree_root (or object_phys_tree_root) @@ -147,6 +148,7 @@ struct kmemleak_object { struct rcu_head rcu; /* object_list lockless traversal */ /* object usage count; object freed when use_count == 0 */ atomic_t use_count; + unsigned int del_state; /* deletion state */ unsigned long pointer; size_t size; /* pass surplus references to this pointer */ @@ -177,6 +179,11 @@ struct kmemleak_object { /* flag set for object allocated with physical address */ #define OBJECT_PHYS (1 << 4) +/* set when __remove_object() called */ +#define DELSTATE_REMOVED (1 << 0) +/* set to temporarily prevent deletion from object_list */ +#define DELSTATE_NO_DELETE (1 << 1) + #define HEX_PREFIX " " /* number of bytes to print per line; must be 16 or 32 */ #define HEX_ROW_SIZE 16 @@ -567,7 +574,9 @@ static void __remove_object(struct kmemleak_object *object) rb_erase(&object->rb_node, object->flags & OBJECT_PHYS ? &object_phys_tree_root : &object_tree_root); - list_del_rcu(&object->object_list); + if (!(object->del_state & DELSTATE_NO_DELETE)) + list_del_rcu(&object->object_list); + object->del_state |= DELSTATE_REMOVED; } /* @@ -633,6 +642,7 @@ static void __create_object(unsigned long ptr, size_t size, object->count = 0; /* white color initially */ object->jiffies = jiffies; object->checksum = 0; + object->del_state = 0; /* task information */ if (in_hardirq()) { @@ -1470,9 +1480,22 @@ static void kmemleak_cond_resched(struct kmemleak_object *object) if (!get_object(object)) return; /* Try next object */ + raw_spin_lock_irq(&kmemleak_lock); + if (object->del_state & DELSTATE_REMOVED) + goto unlock_put; /* Object removed */ + object->del_state |= DELSTATE_NO_DELETE; + raw_spin_unlock_irq(&kmemleak_lock); + rcu_read_unlock(); cond_resched(); rcu_read_lock(); + + raw_spin_lock_irq(&kmemleak_lock); + if (object->del_state & DELSTATE_REMOVED) + list_del_rcu(&object->object_list); + object->del_state &= ~DELSTATE_NO_DELETE; +unlock_put: + raw_spin_unlock_irq(&kmemleak_lock); put_object(object); }