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 9A213C05027 for ; Mon, 23 Jan 2023 19:40:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A2976B0073; Mon, 23 Jan 2023 14:40:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 252AF6B0075; Mon, 23 Jan 2023 14:40:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F41B6B0078; Mon, 23 Jan 2023 14:40:36 -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 01A6A6B0073 for ; Mon, 23 Jan 2023 14:40:35 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BE34E1A3206 for ; Mon, 23 Jan 2023 19:40:35 +0000 (UTC) X-FDA: 80387080830.28.279E6BA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id C7EEE4002A for ; Mon, 23 Jan 2023 19:40:33 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WWTs609V; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of longman@redhat.com designates 170.10.129.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=1674502833; 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=NjaS7acAKguGX9J/sL7TrgKaT9d72gve0LYaOvny/RI=; b=jxy2kIA8c9cWfysGH3n/2uIFkR7BpXwNbs9WkBe0MkwlJbxjr9CdqxCKwU5GQ6b/qhQRdF HKPdRDrgkwjXokpVpKt6FXPMW1hxGKg/BPrV3shtwMpw+bJYC3aBLpa5o2PA8Kyt/tzRDM 9IWCUb/o1yDIBHvtOClHwpjS6qzlOG8= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WWTs609V; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf17.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674502833; a=rsa-sha256; cv=none; b=rlbTLzSmfSLc0Btg90SaNr/zzDXFLeZ0+cp+nkpWdtkeG5Gc7VKbypPIZ3p0FE2SKjzyAP uhamDd4aUurfNeKP+WJVxpN5Pr5No4oQicNm6riA99B9QLQG8VRcxKfGPw++RYryrsRHGs dc4sIVK8Xyb6q/GuvcmicUAbcEa6pE4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674502833; 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=NjaS7acAKguGX9J/sL7TrgKaT9d72gve0LYaOvny/RI=; b=WWTs609V+UdIjKJYadgmszA3UEa6iemGMU16yGJh/0o/27Z82mzkpOkG3MxHCeAUZCNSiL ESS9egReIy8q/17j+8jYXdsQ0Olduqm5liJp5txb+JUrfWGbaypapLcySxVWp7M+WToaNa wAzCepN4uAu4thkcUhAatSttxh2aLhk= 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-561-7X0GtWCYNv-CM1Sjn3LXtQ-1; Mon, 23 Jan 2023 14:40:27 -0500 X-MC-Unique: 7X0GtWCYNv-CM1Sjn3LXtQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 170BE3C38FE0; Mon, 23 Jan 2023 19:40:27 +0000 (UTC) Received: from [10.22.34.140] (unknown [10.22.34.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id C76092166B32; Mon, 23 Jan 2023 19:40:26 +0000 (UTC) Message-ID: Date: Mon, 23 Jan 2023 14:40:26 -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> <55978b11-5e7e-4b10-dff1-398275ec68b3@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.6 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C7EEE4002A X-Stat-Signature: smw7bsqnughrpmzqc9t8rwre3gtkziga X-HE-Tag: 1674502833-114372 X-HE-Meta: U2FsdGVkX1+wtyDqHrM8S+3SxghjfmBQMhqy7iD0NuTRFvdtZd3lZOcuAnktU1X5Yjo+diyxbx7Z/yoIwpLmYUkUDdvt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NujmB/tw8iakDwHpVe7acj7NRecpTL4ywhIobEaZ92UbBih9FaKsPf32YdjlPVeOxfwJTDHUTt4MV1INhcaQMjvajg== 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/23/23 14:24, Catalin Marinas wrote: > On Fri, Jan 20, 2023 at 05:54:28PM -0500, Waiman Long wrote: >> On 1/20/23 14:18, Catalin Marinas wrote: >>>> /* >>>> @@ -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(). > More like thinking out loud: > > The lockless RCU loop relies on object->next->next being valid within > the grace period (A not freed). Due to weak memory ordering, the looping > CPU may not observe the object->next update (removal of A) by another > CPU, so it continues to loop over it. But since we do an > rcu_read_unlock() in the middle of the loop, I don't think these > assumptions are still valid, so A may be freed. > > What we need is that object->next reading for the following iteration > either sees the updated object->next (B) or it sees A but the latter > still around. I think this holds with the proposed > kmemleak_cond_resched() since we now start a new grace period with > rcu_read_lock() followed by taking and releasing kmemleak_lock. The > latter would give us the memory ordering required since removing object > A from the list does take the lock. > > So yeah, you are probably right, I just find it hard to get my head > around ;). I still think it would be simpler with a single kmemleak_lock > (no object->lock) but that's more involved than a simple fix. > > Assuming your (and my) reasoning above is correct: > > Reviewed-by: Catalin Marinas I should have mentioned the fact that taking the kmemleak_lock will post some ordering guarantee since it is done after a new rcu_read_lock(). So yes, even if both A and B are removed from the object_list, they should still be around and not freed yet. Thanks for your review. Cheers, Longman