From mboxrd@z Thu Jan 1 00:00:00 1970 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 362A4C4167B for ; Sat, 10 Dec 2022 23:01:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1DDF8E0006; Sat, 10 Dec 2022 18:01:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CCD8C8E0001; Sat, 10 Dec 2022 18:01:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B94418E0006; Sat, 10 Dec 2022 18:01:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AB3428E0001 for ; Sat, 10 Dec 2022 18:01:12 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 87DDF1207D7 for ; Sat, 10 Dec 2022 23:01:12 +0000 (UTC) X-FDA: 80227919184.08.06E9F7A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 09B72A0018 for ; Sat, 10 Dec 2022 23:01:10 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h2P4F5y1; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf25.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670713271; 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=BzoClKiMxtjbfNJAceE2aMmNjTXvJrAUZ+qD1O1iqNQ=; b=OfJR62hiQsOH+49el4rdCef1WtOK9VbHWqAWDMP5My9au7eoqr6usdbjpGUp3SQxd+aRH0 5g3e1I8adUpBqoTIx4Js1DNRPh0V8C8hmL3SjmzHQXX8m1zEJOex/COcHnGhsQDCFDzxny cebn2ZUX0MB1nnDMh+aNSujQxeL4Tvo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h2P4F5y1; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf25.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670713271; a=rsa-sha256; cv=none; b=cd7LOwJebkcVtSUdj0xdnD00BGo8oIfXr32MCvwbGFuIIVjxLNp0QoWdG+YlA0/GHqHmAa AsIVy/Mqr/tpJ9oh4Jl7qH3By/+4eRDP0/mR7SwzmUB4+y3vOXEmGmJSZC1NRzVHSaxvsc xdM0QMvVvGH5Y98IA30KvJXlr79EOc0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670713270; 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=BzoClKiMxtjbfNJAceE2aMmNjTXvJrAUZ+qD1O1iqNQ=; b=h2P4F5y1A5cAs497PMr/UXH2DvgviL4Tl0Ah7H79mxvtDou6I8blrCoe5YxBMptMTjcxRV s5/onK2UFoSqCJGjQyy9wLMVb+pTBthcSyWWMFRm9NGWAHnh95b2ApR4uP0AuWTYFVpKLX O0/jaZfscZx1iS5XW6aqF4us+EOR5cs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-280-16NgXCebOAiorSWx2Dnadw-1; Sat, 10 Dec 2022 18:01:05 -0500 X-MC-Unique: 16NgXCebOAiorSWx2Dnadw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9FFCE8032FB; Sat, 10 Dec 2022 23:01:04 +0000 (UTC) Received: from llong.com (unknown [10.22.8.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5D4BD40C6EC2; Sat, 10 Dec 2022 23:01:04 +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: [PATCH 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Date: Sat, 10 Dec 2022 18:00:48 -0500 Message-Id: <20221210230048.2841047-3-longman@redhat.com> In-Reply-To: <20221210230048.2841047-1-longman@redhat.com> References: <20221210230048.2841047-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspamd-Queue-Id: 09B72A0018 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: f41n5qwwof5rdq8o3pc5yycwi43pjng3 X-HE-Tag: 1670713270-143588 X-HE-Meta: U2FsdGVkX1+OYklDIoe8nHj+Z0TnyX7knM6Df1PuxSWceCMwPxf5/oN677SjYwWOZv1iFBbDvfaZ9+Zunc9QocQVWIEpte8nfI2uQgW+p2ORDyKGOKKJzULa6LwiAk9M7lPgu/L43zCX2EMeii3cmUkuSwbC0FrAJy0mW79m3DPl2hNoZHEB+z+9rvMDTpAI5WUOtszZfy+2rcbE8L9ZgZzTpvL5MVzLbSUnbq47WIlpQUOrjS8oBjdsTXkjlcbuRpR2/cKqECaJ7/xMLEw3btyufcCTIu5J3D8uZTHn4VDU4P9VXArtH7p+UhIAP+ZiNyFOLs6O6kc1rxLYSb9qHHOd1M/de47XyNK+tPasEzFYSN/V8LdUgOdOUaZa63OFkpuaEqzWVXHBemgOJgJq2K2QplqgOOYIwv365qa4K8OlKAzaWa0ujPN+u8dqL13JzX4ZorNHNpBIrLK8wEexy9IyezDcRbmVSlGYhVE9G+6OA8DNiJqeT+pTBo57UO/71mt3t1BOblfkvs59SRuLwIN+TGTAjwpa5AKWkevU9sCFI0+hSouKtz6Sg4Lj7OEe+ZbOodxN1kdxA8ggCKvM5Dn3xODFqahWKecEQdtHLO7qE1EFPQKNtBtVh5SG5iB1tGPGjgUDVhEoXb6N2B+KxbCkEhQXvl24jMcJsIM8+ReqqYrTAK8BX0rIHI804UhRu4tyaVs+/DHoTM2hkperXPyI88kbrmiiBSx+TXpB29p3Ib3rAu3pwFIoiWxz5Hdi7UoYIPFHbv9H55YqHKt9Y42r+vo1X8BHKbQRLw9pZfE+XBNESG0RqCfgOBwz2Ffx4UYJx9aj3tdpEZZU5/YV7V3sL3jwEo866OjWMMicgVAQtOQllX7vnXZacF6muOhN5rpo2K7my3SRektBSGinofcwrlp8Xfyf+O4TLh4L7JqTzZkQ3AEe1BxtNtgVT6+ZJiyTrpl0Sm0= 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 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 --- 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