From: Waiman Long <longman@redhat.com>
To: Catalin Marinas <catalin.marinas@arm.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Muchun Song <songmuchun@bytedance.com>,
Waiman Long <longman@redhat.com>
Subject: [PATCH 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan()
Date: Sat, 10 Dec 2022 18:00:48 -0500 [thread overview]
Message-ID: <20221210230048.2841047-3-longman@redhat.com> (raw)
In-Reply-To: <20221210230048.2841047-1-longman@redhat.com>
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 restarting the object scan from the beginning of
the object_list in case the object has been de-allocated after returning
from cond_resched().
Fixes: 6edda04ccc7c ("mm/kmemleak: prevent soft lockup in first object iteration loop of kmemleak_scan()")
Signed-off-by: Waiman Long <longman@redhat.com>
---
mm/kmemleak.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 8c44f70ed457..d3a8fa4e3af3 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1465,15 +1465,26 @@ static void scan_gray_list(void)
* that the given object won't go away without RCU read lock by performing a
* get_object() if necessaary.
*/
-static void kmemleak_cond_resched(struct kmemleak_object *object)
+static void kmemleak_cond_resched(struct kmemleak_object **pobject)
{
- if (!get_object(object))
+ struct kmemleak_object *obj = *pobject;
+
+ if (!(obj->flags & OBJECT_ALLOCATED) || !get_object(obj))
return; /* Try next object */
rcu_read_unlock();
cond_resched();
rcu_read_lock();
- put_object(object);
+ put_object(obj);
+
+ /*
+ * In the unlikely event that the object had been de-allocated, we
+ * have to restart the scanning from the beginning of the object_list
+ * as the object pointed to by the next pointer may have been freed.
+ */
+ if (unlikely(!(obj->flags & OBJECT_ALLOCATED)))
+ *pobject = list_entry_rcu(object_list.next,
+ typeof(*obj), object_list);
}
/*
@@ -1524,7 +1535,7 @@ static void kmemleak_scan(void)
raw_spin_unlock_irq(&object->lock);
if (need_resched())
- kmemleak_cond_resched(object);
+ kmemleak_cond_resched(&object);
}
rcu_read_unlock();
@@ -1593,7 +1604,7 @@ static void kmemleak_scan(void)
rcu_read_lock();
list_for_each_entry_rcu(object, &object_list, object_list) {
if (need_resched())
- kmemleak_cond_resched(object);
+ kmemleak_cond_resched(&object);
/*
* This is racy but we can save the overhead of lock/unlock
@@ -1630,7 +1641,7 @@ static void kmemleak_scan(void)
rcu_read_lock();
list_for_each_entry_rcu(object, &object_list, object_list) {
if (need_resched())
- kmemleak_cond_resched(object);
+ kmemleak_cond_resched(&object);
/*
* This is racy but we can save the overhead of lock/unlock
--
2.31.1
next prev parent reply other threads:[~2022-12-10 23:01 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-10 23:00 [PATCH 0/2] mm/kmemleak: Simplify kmemleak_cond_resched() & fix UAF Waiman Long
2022-12-10 23:00 ` [PATCH 1/2] mm/kmemleak: Simplify kmemleak_cond_resched() usage Waiman Long
2022-12-14 10:43 ` Catalin Marinas
2022-12-10 23:00 ` Waiman Long [this message]
2022-12-14 11:16 ` [PATCH 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Catalin Marinas
2022-12-14 15:54 ` Waiman Long
2022-12-16 10:32 ` Catalin Marinas
2022-12-16 16:38 ` Waiman Long
2022-12-23 17:50 ` Catalin Marinas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221210230048.2841047-3-longman@redhat.com \
--to=longman@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=catalin.marinas@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=songmuchun@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox