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 7397DC05027 for ; Mon, 23 Jan 2023 19:24:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE2146B0074; Mon, 23 Jan 2023 14:24:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E924E6B0075; Mon, 23 Jan 2023 14:24:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5A286B0078; Mon, 23 Jan 2023 14:24:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C4C826B0074 for ; Mon, 23 Jan 2023 14:24:56 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 95A26142265 for ; Mon, 23 Jan 2023 19:24:56 +0000 (UTC) X-FDA: 80387041392.08.4836CD2 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id DEB1B40025 for ; Mon, 23 Jan 2023 19:24:53 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf11.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674501894; 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: in-reply-to:in-reply-to:references:references; bh=7bpkctU+5ZmQPcWgMQGluPz0LqprahVXJ0BjeR887vQ=; b=gezDMJ18K8tfcSL22Yk4SiLwDCyftFgXCjcdtx2ltmDziBAZ+ZjKj8qftzTWk+Wayre8Jt YdUqEgSVQcQ/K5NpNnlkc2f1AH0zCYQCWYwDUZhCP7jEEPwWGT/akRYybMr10mLFD9+Blh 8digLPuD5ED0o4I25Lj4ieXJ0S/j7WY= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf11.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674501894; a=rsa-sha256; cv=none; b=MkRKtjQE0DNfrOe2rk8mb4lXkNN57x0CZxc0Sy0dByzqb8IVJaQHy3PLh8wUItjHxxH9VA Geif6/EzWOlRAFyFHcL2YIF81JoxbcErwBU2gvMZyRXp738b4u8TYIrFhCowmn5qIZjALG b122CbsUq7uxTgcHEIlP1IGwbDd3kV8= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C1FF461003; Mon, 23 Jan 2023 19:24:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05B45C433D2; Mon, 23 Jan 2023 19:24:50 +0000 (UTC) Date: Mon, 23 Jan 2023 19:24:48 +0000 From: Catalin Marinas To: Waiman Long Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Subject: Re: [RESEND PATCH v2 2/2] mm/kmemleak: Fix UAF bug in kmemleak_scan() Message-ID: References: <20230119040111.350923-1-longman@redhat.com> <20230119040111.350923-3-longman@redhat.com> <55978b11-5e7e-4b10-dff1-398275ec68b3@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <55978b11-5e7e-4b10-dff1-398275ec68b3@redhat.com> X-Rspamd-Queue-Id: DEB1B40025 X-Stat-Signature: 95ibyw4co3jswxok3t57eji4o9x9tmxo X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674501893-383255 X-HE-Meta: U2FsdGVkX1/VeO325AvxD3rcrH2BPE6s0VAPra8xRvIDW4JckQNnSTGadlydkd/b2vCCIRrlbdl40Gs+Pb1SANeBevs5X1cOf833coFyPbH3fJPkzUFotMr6r6cQHvc7PRTIATZ+wQDjL3rBQ1+LzAW8KFSk1EWJhJHiM2lqa8WtTR4IAX6bY76Gs8naycgUZOwE8qCUfwd1Rzrznpq7VK/V5a7Tk1wWfDngczwiV50Fqwjraq+8oNagZHp8zGDfKeDIS0EvKEGu5xj/VSbviT2tuC2HKcIDM0Ogums+NyxhpBl/tF+1GhyfTSGjF73XhlAiKxCtWC2goE8rEyTC9SalIxieKwaR/x4fNyhNmcTljrQKFw/1NedSaF9Ez+nyrwzHu6Twl91WQg5r9/9N7YF97wEIP9A4SmSPTXYTbQglg82PB6bDvQJynky5lR3jR3Ey5VRlYCGutThr6gFHcdmuq3bMAKBQv+U8bVN+AaMRACzWojUoqxfjzy47+/syjub0Wzvy3oEbs3H981qZbhnMnQDazlebRPstOU+n/XtVMiVsdb50r4oh7OOSTOiuklRx5mMi3jgJnxkEBhDsZOeRMNunv0rpt7iGR7NK5qjfZxh2Hf0hv0cyZR9pYb7LGW7eI1H08cLnkS1p70L6mJ0ZuqG0eFYYgvvqrgY2I9iODxDbbwE1onOEBWIK/6w+zRIc8zJxvDx04cOBww7gEqijXPklWKLT9YNObu60+43MrN22LkA7Eu0zN9CaqyQIbg5Ke9+PaqnfPy1ILsSNuTW6voSeim7mKvyheqlxkylSHb9GcaVpa7ra0QEE0OhYB5pfwSx/ryAXGvVyLsQxt2tNAGeFkMJzey0c60cGE+s2FJ1xZeSMV6Hri+ovMuxr7G+kiuaWPqC455GKVM1tplmLykps7cdDhNEpjxw7WKzs1iz64uoF6tNImthyTVSYVvYaEfWDA+3sbIb73Qb 0vI/XE57 uCBepR9jxuMwServKligoGZJer1H/cQY3Q9urO7xKH9PQ5xlBZpBvUpdyyusOKwlibIrjN/C1pUcdqiL37KEKE54eiCNaVLnrrYkpUptD9O9XiaL6Y5TIQfvQ+MyaCzZJURd1Nw4zXQCzr7/OqazReRlXWYnWrMAc3mhQr426rNNZuUbErlGgOBcF5buJZRRI5ssYe3lTkN+7X+Pq47SNdkSZlbFBpH4LspMFjeT8YU9kp1WdQUA193OVdYc0TzpULCPPUJUDjphY0+s93BDliDnXar76UftXfj2XX4cwZnU1drtD+Kfhu/eaZprhvnSikJxUE99zB8izrP43yGFBNQ8j0fhExzHuyxi292l0rKAJGa3S1Slc3x62augEWh701pcRq5nxa4JrBFV3C+XoxIDx+Asx6TEJTUzt8PoVyvOxqrqcC/+NnZwQVfjBKzMGhXN52l/ZMpIjIzPeOrmqaJwY5rtpNUQd3NDwuy99h0Z5XRPBDiTTV6bM4qnDGKsPSZSfSfwX7p1uNUcJnW/5tfQsqkHrw9T1vbdP 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 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