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 F27F1C25B50 for ; Fri, 20 Jan 2023 22:54:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 062106B0071; Fri, 20 Jan 2023 17:54:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 011546B0073; Fri, 20 Jan 2023 17:54:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1AD56B0074; Fri, 20 Jan 2023 17:54:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CF4D86B0071 for ; Fri, 20 Jan 2023 17:54:36 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A75EF1C62E4 for ; Fri, 20 Jan 2023 22:54:36 +0000 (UTC) X-FDA: 80376683352.02.FF546DD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id C3E81A000F for ; Fri, 20 Jan 2023 22:54:34 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=b232TNtj; spf=pass (imf25.hostedemail.com: domain of longman@redhat.com designates 170.10.129.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=1674255274; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KSMfGcmcVhk0Uo6IE79LVhhz/BJL/6En61IwKDiYOrM=; b=TSelyWi0aCSlFzOBtZE56083iqz30JazdwJPQ/LIaO4H77/Zs3oDHtpptGLDZkx5a1VdS9 9ajsBl/hS6xDTkcJd5hw9wLTQteuKheFQVJ1xt5xqWRnWiE+xAPOWkMSzUg7P+Alxv2WS+ 12D8MFQHv2oMypl7eTgjAcsMYnyQiwo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=b232TNtj; spf=pass (imf25.hostedemail.com: domain of longman@redhat.com designates 170.10.129.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=1674255274; a=rsa-sha256; cv=none; b=Zkpy4d7/r6VIIwjQq7Dm2vwJ+/CmIUlFDWPgu7AsGV3CtA4cw4v6a/KOtK+0gHLXmyDQ7G LG05QHdCrR9UoAQWsOFz2amchani4A/XmNCvwYxNYgFranerhVdRuC4WtsADhQ3w110riQ /zBeM4v/gVeo4LiBqhuHFOAn+Pq9ylc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674255274; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KSMfGcmcVhk0Uo6IE79LVhhz/BJL/6En61IwKDiYOrM=; b=b232TNtjK4O+2NW8A9DpDvqexB7CFY6P4r5Aq2Gr3kvSIYDSPwEV6NJlHkVj8HDQBMPsB3 IvqZHN+M+Jn/otuaOjb/eTkIdazPI00Z8JBiRL/08CgmgiZoKYIwlPCxOZSGd4v1n5MmqI BjoCE/GYQR0QUvxNyAyvdERPL0vhaDM= 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-313-fbEc8iaaObGToB-glIOzXg-1; Fri, 20 Jan 2023 17:54:29 -0500 X-MC-Unique: fbEc8iaaObGToB-glIOzXg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 34B95101A521; Fri, 20 Jan 2023 22:54:29 +0000 (UTC) Received: from [10.22.17.220] (unknown [10.22.17.220]) by smtp.corp.redhat.com (Postfix) with ESMTP id EEF961121318; Fri, 20 Jan 2023 22:54:28 +0000 (UTC) Message-ID: <55978b11-5e7e-4b10-dff1-398275ec68b3@redhat.com> Date: Fri, 20 Jan 2023 17:54:28 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: [RESEND PATCH v2 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Content-Language: en-US To: Catalin Marinas Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song References: <20230119040111.350923-1-longman@redhat.com> <20230119040111.350923-3-longman@redhat.com> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C3E81A000F X-Rspam-User: X-Stat-Signature: pk374oudpe3bks64uhwkj1iyqae77cty X-HE-Tag: 1674255274-378268 X-HE-Meta: U2FsdGVkX1/j6isk2+1XQVril7FgC/h1L1CZD5Id8vmWlh946r1oBkBRWk8OtnCXzGLijpHmBqWY841Z3qkk/A5vVcWvrBEQhYFMT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a2Po3+aL9X1xzSaxbrFZMaViGTtCVW0n5 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: On 1/20/23 14:18, Catalin Marinas wrote: > Hi Waiman, > > Thanks for your effort on trying to fix this. > > On Wed, Jan 18, 2023 at 11:01:11PM -0500, Waiman Long wrote: >> @@ -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; >> } > So IIUC, this prevents the current object being scanned from being > removed from the list during the kmemleak_cond_resched() call. Yes, that is the point. > >> /* >> @@ -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); >> } > I'm not sure this was the only problem. We do have the problem that the > current object may be removed from the list, solved above, but another > scenario I had in mind is the next object being released during this > brief resched period. The RCU relies on object->next->next being valid > but, with a brief rcu_read_unlock(), the object->next could be freed, > reallocated, so object->next->next invalid. Looking at the following scenario, object->next => A (removed) A->next => B (removed) As object->next is pointing to A, A must still be allocated and not freed yet. Now if B is also removed, there are 2 possible case. 1) B is removed from the list after the removal of A. In that case, it is not possible that A is allocated, but B is freed. 2) B is removed before A. A->next can't pointed to B when it is being removed. Due to weak memory ordering, it is possible that another cpu can see A->next still pointing to B. In that case, I believe that it is still within the grace period where neither A or B is freed. In fact, it is no different from a regular scanning of the object list without ever called cond_resched(). Cheers, Longman