From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382465; b=Iz0aTrSyZq/x0klO+Jxd4R093wekZFlU2hd98zdzitU3MBnZ1brTLkTMfoXJWZAzbTOG dNJRXwHReKxn739wEwYYltly713G/K0J7qZPTjT4gx4hdUAbjmG96FENVmsu5YU/nJAg9 YpSZMUtMxvQWZ5TbKGDNub0M8VwguKOoBlm7+9EM8HRep7c9a7ThsS6OEFhmL8xw37364 NO9UD42XuBP3rE1Vh61ptq63ZrGwDh3SzxdmUkMVNInrj74RXLlZbxO66ymBrnZ/GM/fS 0hLzyt5StajmdG/rQ94yqjrxSbpQ7O4u/e1q++xn0UpOSoAMWWyFDbEaIeHAZZH5RUg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382465; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=cUVHtBhtX2wxLse5GeJKvVIMiYu+IofyLt6c4R2kT60=; b=t/hPOIQHiN1FmnSbwTj2r3K86rHteP4ktkbJMaql1lpPiyI3DvEeJu1jkcZ10X8UwhuX qyN2YMqrPlfHxo0QbgkDgLefpeTzdGjVuAuSMh/T12BcpgxW9j40NWQWqtrYT5y2+AJu5 9xNaMoLwb0tYDCZwYA+2ZhDs6AU6ncJ0lQd9wlr0T5VBGWJ1V7U8cZ6Lwv3ffOQ4WinQS UiUB0WlFUY7ONtkqEhHdch9vmGb9Vp1p857Ny4uCAmEBS5mMmPrUXxhscR4CX1mZM6ujs +EMRFKLzZUEXsHse/AmSRwYHRhGG1RHZdUSRvC6ZJvHFTHy5zMuYTUTcbEfZw4nYCew== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=vkWwzrVO; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAA55606A335; Fri, 4 Dec 2026 11:07:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=vkWwzrVO Received: by smtp.kernel.org (Postfix) with ESMTPSA id 497D01F000C2; Fri, 4 Dec 2026 11:07:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382465; bh=cUVHtBhtX2wxLse5GeJKvVIMiYu+IofyLt6c4R2kT60=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=vkWwzrVOQgtPTtoxvt0HWCzVY21RiG31yU0KfscveLATnNt6nFWiaZQAGEb4o126T Nt4suEtr3qHgZytsAbeHvGen9CYuYC1/19HYOxBdWmdFb1NUxxSr1kLdBQgqe+caqX OHI5ZEYPkFi4TNdSokcNdGl/vVdinmMGhP4PF7zk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Liam R. Howlett (Google)" , Andrew Morton , Chris Mason , Lorenzo Stoakes , Alice Ryhl , Andrew Ballance Subject: [PATCH 6.18 467/703] maple_tree: fix rcu reader NULL dereference after the tree is emptied Date: Fri, 4 Dec 2026 19:06:06 +0900 Message-ID: <20261204100640.201945959@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Liam R. Howlett (Google) commit e8a71f284ba7eeddd9475bb0b65200e95428b497 upstream. Patch series "maple_tree fixes for 7.3", v2. Two bugs found by an LLM review, and one problem Lorenzo Stoakes reported off list. An RCU reader that restarts after its node dies can dereference NULL if the tree was emptied, or holds a single entry in the root pointer. Patch 1 fixes it and patch 2 adds a test. With CONFIG_DEBUG_MAPLE_TREE, MT_WARN_ON() and the other WARN macros print the tree and call dump_stack() rather than WARN(), so syzbot never sees them. Patch 3 makes them WARN once per call site, after the dump, and makes the BUG macros BUG() after theirs. The userspace tests make WARN() fatal to keep failing as they did on dump_stack(). This patch (of 3): RCU readers may hit a NULL dereference if the tree is emptied while the walker is restarted from a dead node. Avoid the NULL dereference by checking for an emptied/pointer tree (0->ptr, 1-ULONG_MAX -> NULL) and set the maple state limits correctly. A reader of /proc//maps racing with an munmap() of the whole address space can hit this. The first implementation has the same problem in both directions. Initial fix was generated by LLM and sent by Chris Mason in private. [akpm@linux-foundation.org: fix reverse iterator state when the root disappears] Link: https://lore.kernel.org/20261104104137.2058705ac29734a7ab5666c1@linux-foundation.org Link: https://lore.kernel.org/20261103020724.2614498-1-liam@infradead.org Link: https://lore.kernel.org/20261103020724.2614498-2-liam@infradead.org Fixes: 54a611b60590 ("Maple Tree: add new data structure") Signed-off-by: Liam R. Howlett (Google) Signed-off-by: Andrew Morton Reported-by: Chris Mason Cc: Lorenzo Stoakes Cc: Alice Ryhl Cc: Andrew Ballance Cc: Signed-off-by: Greg Kroah-Hartman --- lib/maple_tree.c | 120 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 93 insertions(+), 27 deletions(-) --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -4037,14 +4037,64 @@ retry: goto retry; } -static __always_inline bool mas_rewalk_if_dead(struct ma_state *mas, - struct maple_node *node, const unsigned long index) +/* + * mas_next_no_node() - Finish a next walk that restarted into a tree without + * nodes. + * @mas: The inactive maple state (ma_none or ma_root) + * + * The arrival here is either because there is a root value or nothing. Either + * way, the next entry is nothing. So set the limits and overwrite the status. + * + * Return: NULL + */ +static inline void *mas_next_no_node(struct ma_state *mas) { - if (unlikely(ma_dead_node(node))) { - mas_rewalk(mas, index); - return true; + if (mas_is_ptr(mas)) + mas->index = 1; + else + mas->index = 0; + + mas->last = ULONG_MAX; + mas->status = ma_none; + return NULL; +} + +/* + * mas_prev_no_node() - Finish a prev walk that restarted into a tree without + * nodes. + * @mas: The inactive maple state (ma_none or ma_root) + * @index: The start of the range the walk left + * @min: The lowest index to return + * + * The arrival here is either because there is a root value or nothing. With + * nothing, leave the limits mas_walk() sets for an empty tree. A root value + * sits at 0: stop on it as mas_prev_setup() does, unless the walk was at 0. + * + * Return: The root entry if @index and @min allow it, NULL otherwise. A node + * if the root changed again since the walk; the caller restarts. + */ +static inline void *mas_prev_no_node(struct ma_state *mas, unsigned long index, + unsigned long min) +{ + void *entry; + + mas->index = 0; + if (mas_is_none(mas)) { + mas->last = ULONG_MAX; + return NULL; } - return false; + + mas->last = 0; + if (!index || min) + return NULL; + + entry = mas_root(mas); + if (!entry) { + mas->status = ma_none; + mas->last = ULONG_MAX; + } + + return entry; } /* @@ -4152,14 +4202,14 @@ retry: node = mas_mn(mas); type = mte_node_type(mas->node); pivots = ma_pivots(node, type); - if (unlikely(mas_rewalk_if_dead(mas, node, save_point))) - goto retry; + if (unlikely(ma_dead_node(node))) + goto dead; if (mas->min <= min) { pivot = mas_safe_min(mas, pivots, mas->offset); - if (unlikely(mas_rewalk_if_dead(mas, node, save_point))) - goto retry; + if (unlikely(ma_dead_node(node))) + goto dead; if (pivot <= min) goto underflow; @@ -4174,10 +4224,8 @@ again: if (mas->index <= min) goto underflow; - if (mas_prev_node(mas, min)) { - mas_rewalk(mas, save_point); - goto retry; - } + if (mas_prev_node(mas, min)) + goto dead; if (WARN_ON_ONCE(mas_is_underflow(mas))) return NULL; @@ -4191,8 +4239,8 @@ again: slots = ma_slots(node, type); entry = mas_slot(mas, slots, mas->offset); - if (unlikely(mas_rewalk_if_dead(mas, node, save_point))) - goto retry; + if (unlikely(ma_dead_node(node))) + goto dead; if (likely(entry)) return entry; @@ -4209,6 +4257,17 @@ again: underflow: mas->status = ma_underflow; return NULL; + +dead: + mas_rewalk(mas, save_point); + if (likely(mas_is_active(mas))) + goto retry; + + entry = mas_prev_no_node(mas, save_point, min); + if (unlikely(xa_is_node(entry))) /* The root changed again */ + goto dead; + + return entry; } /* @@ -4322,8 +4381,8 @@ retry: node = mas_mn(mas); type = mte_node_type(mas->node); pivots = ma_pivots(node, type); - if (unlikely(mas_rewalk_if_dead(mas, node, save_point))) - goto retry; + if (unlikely(ma_dead_node(node))) + goto dead; if (mas->max >= max) { if (likely(mas->offset < mas->end)) @@ -4331,8 +4390,8 @@ retry: else pivot = mas->max; - if (unlikely(mas_rewalk_if_dead(mas, node, save_point))) - goto retry; + if (unlikely(ma_dead_node(node))) + goto dead; if (pivot >= max) { /* Was at the limit, next will extend beyond */ mas->status = ma_overflow; @@ -4354,10 +4413,8 @@ again: return NULL; } - if (mas_next_node(mas, node, max)) { - mas_rewalk(mas, save_point); - goto retry; - } + if (mas_next_node(mas, node, max)) + goto dead; if (WARN_ON_ONCE(mas_is_overflow(mas))) return NULL; @@ -4372,8 +4429,8 @@ again: slots = ma_slots(node, type); entry = mt_slot(mas->tree, slots, mas->offset); - if (unlikely(mas_rewalk_if_dead(mas, node, save_point))) - goto retry; + if (unlikely(ma_dead_node(node))) + goto dead; if (entry) return entry; @@ -4390,6 +4447,13 @@ again: } return entry; + +dead: + mas_rewalk(mas, save_point); + if (likely(mas_is_active(mas))) + goto retry; + + return mas_next_no_node(mas); } /* @@ -5585,7 +5649,9 @@ void *mas_find(struct ma_state *mas, uns /* Retries on dead nodes handled by mas_next_slot */ entry = mas_next_slot(mas, max, false); /* Ignore overflow */ - mas->status = ma_active; + if (mas_is_overflow(mas)) + mas->status = ma_active; + return entry; } EXPORT_SYMBOL_GPL(mas_find);