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 7836CC4332F for ; Thu, 15 Dec 2022 16:03:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30CC98E0006; Thu, 15 Dec 2022 11:03:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2BB568E0003; Thu, 15 Dec 2022 11:03:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A9AD8E0006; Thu, 15 Dec 2022 11:03:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id F40DD8E0003 for ; Thu, 15 Dec 2022 11:03:20 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CACB21A0FFC for ; Thu, 15 Dec 2022 16:03:20 +0000 (UTC) X-FDA: 80245010160.17.752ADF1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf26.hostedemail.com (Postfix) with ESMTP id DB99C14003E for ; Thu, 15 Dec 2022 16:03:15 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GXWX9rFK; spf=pass (imf26.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=1671120196; 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=hU8XPnEOQIrXJm1f8LjuXWxGasqPI3FiV6KWdOox3kBmklV24aXA0Ywcj40+EicWsQ6RlW VxRBWIcfRof/lM7Un+gMcrw91y98IyZMnDomu2Tj2kX5SVtZIVFcZULohOsVcNu84Yrx7D x7E714V6j2gXbc8x6wzLrsHgWiNQ1as= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GXWX9rFK; spf=pass (imf26.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=1671120196; a=rsa-sha256; cv=none; b=57GM9ggP/ObH7IrVLJNaT/CH6W6KqMWTJYfGhiyiJ9ooECu0Do2ZUPu/EdDho96ToUjnnQ A2CdY5/bnoCX/phs++AE8Tm7odnb8wvDFwGPSgikrCI+E5LfK5260ai6aTE0FWl4sIJhaV I6uhzeRaKu1+z+aAeos1yWSiQYtsbns= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671120195; 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=GXWX9rFKJx+pkcgQjak20S8RHHN/1Ooe4v4S+NHGJnAKeGdfpZvZ2ifAz35h/hR0QrPxBr r3Ef0mLuymf3O4NM96On/NVUv75hYDoHnMMn0pxBp34smR4Dem6hjRm+4knkgAXFSe1z6L 4q/igSlYdYWo96hAf+uVtkJXLw71xuU= 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-74--JlN4jZHNsufKEb23_Ljsg-1; Thu, 15 Dec 2022 11:03:09 -0500 X-MC-Unique: -JlN4jZHNsufKEb23_Ljsg-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 459BA857F82; Thu, 15 Dec 2022 16:03:09 +0000 (UTC) Received: from llong.com (unknown [10.22.9.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0123C4085720; Thu, 15 Dec 2022 16:03:08 +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 v2 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Date: Thu, 15 Dec 2022 11:02:59 -0500 Message-Id: <20221215160259.261136-3-longman@redhat.com> In-Reply-To: <20221215160259.261136-1-longman@redhat.com> References: <20221215160259.261136-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DB99C14003E X-Stat-Signature: sexhrbb4okp665hbjn969zh5cfmyjh5b X-HE-Tag: 1671120195-774361 X-HE-Meta: U2FsdGVkX1/JVSNxvVdoSBHw1dS/RD2ekZw9xHEefZ1lAM0LGtVhDHU50Eg748j4r8LNnWpLb4RL+nHeDSc1ozexVvg6fQF0r0WjUfaMds8/fdp/FaS84R03zI6kUEyW+NIKEh9tB9PZZGOwOMOT3G+kpVMRnvEoqJVL+ErYfKuRFP5GJg1VfgBgKnM4Ohzx1nJ83XNIH7X/1/QYPxWFID/HKZptkNsgIaEva0pK6kcIYi0JuUMk/Us3o1T8BJGENyJRcSOa08oEb8/3tYgDd/tI9cTYDH6wDls5Nl2XDH8c8o9bD08c6msfG1e4T1eY8QQcVxwJ1GFOmQD4wueze/LA8NxTVpjPjKe0b31wb2fVdJg/u/4nCjo/VBpST/mZyeUqqPOm8NOmgXGeGX1oz1chXNU6aS3EHNoiEdcvgre/jjEFStCwezjUkPgvAI6IlHROjtpa8qemkr2UNhoYoOKfREy+kXLp5AXLqdGTtOJQ8zE3SOg5ruHFTmLKj0RNCrZ0J+w0hBXV7MSwbDdW3pJMZ2GEXad1pzh1K0YNvtc77rh6rAyqEwyhW8hYP9NhqDQprReKHAb2dGLoaf1ppldmmbAJuXaib1RS/waDbnJDCfBxql8bnbpuuN23qhVpqqajKo1RLqme4bhkdvgNUgSSnqsG/o+7yUp91KJd3G8qdRdjNg6i4EgvDje59eOo3SLfAvSHQHwRdnz1Z3XEfYH/RSG9UyoaMaX4ud3rDF6qIGyXL5WekiyHIoYKtdNWmIzJ38C7q7BcS7sXXyIbHNqPGXRIz/TOL4DUWFXCaxCO7Bhp9I3H/JQFtm+kKV1QmUu6mtHHDdVH+V2QheeDUfD47oL/XbSudovKyi+ba7DbE6hwB6IEWXF2Sbc0GZvLK25Botyr2NaXPERyeyufVfTxEuRak8vWD8oDsAokWG/Z/GebxUfCKAmS8C1IxVOA44c0W52j+zQ= 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 --- 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); } -- 2.31.1