From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A1354B0C86; Thu, 3 Sep 2026 13:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788443349; cv=none; b=nnee6bYIopAzamhwY7QWkZmlTjH9gy9+hLR1mbrid6XXNYP3ZxFsNgoQ/ZYggn3TsDsXld4Y9h8H44AV9HavzGsrs2WQCP828vju8ELcinuhVPAEhix4Z3dLYozKeNK41PHoYKQ7rau/8hT4O+dOhIm0n+5k24dZrxU/brRRRZo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788443349; c=relaxed/simple; bh=xwRswq87lvm5eihQOLOsUYUUFXoY/BZYZif+E60hD9g=; h=From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type:Date; b=pIPjXz+/7+sCTt2L2Mf3HWyRfb664qcegtY4dTSzxyEtVaG15bfH2jxwe8C45aaWdBsEMJIwm5JcguKj7AoCCptPrI2KOjTEUHkMs1tyWZg+K4IpoaYDoq6hmh2DieqIiicP/2zXXE591Uc0JNfAtu1+g+2JRPSaksMZFjgK8F8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Z9MeGmA2; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Z9MeGmA2" Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 26F671F00A3F; Thu, 3 Sep 2026 13:48:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788443335; bh=6To+8fe2xKabaq6LuBCZ/jPSqRixKgPLH1mPUnM4bHo=; h=From:To:Cc:Subject:Date; b=Z9MeGmA21BkOeddjLE5LR+XMcG1y6ECfJya73DU/Sz4bHhlqCXj9ITrKmN35Q9USh 9qcDv7PZgOTejj2ZGZ3GMj7N82R6dxusbEjROgDFwnjcGW7Dsc7h8dK8K8J1RZaQuG tzLfqGgTZqj1uHoEqyedjDD4zynzhcK8sDLVyERsZQuldPVq1iTS4+ae11s3Uba+QV wjizbqXtIE0xB4UAr3wRcV2gr/SDYnfcourzrSV2lJyTbclsxpztfBmiuZgU371VeJ jwiPw1HfSLolZiiF1UgH9xGXB40nQiASCwYCyKhlJMhYwNy7xK+w0FYhXTyA7qZ4ND nraERTUl1ykyA== From: "syzbot" To: syzkaller-bugs@googlegroups.com, Krystian Kaniewski , "Andrew Morton" , , , "Matthew Wilcox" Cc: linux-kernel@vger.kernel.org, syzbot@lists.linux.dev Subject: [PATCH] xarray: fix index jumping backwards in xas_find() Message-ID: <2992424b-2120-489e-9010-f45f46ed52c8@mail.kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: Thu, 3 Sep 2026 13:48:55 +0000 (UTC) From: Krystian Kaniewski A bug in the XArray iterator xas_find() causes the iterator's index (xas->xa_index) to jump backwards when iterating over a multi-index entry (like a THP) that resides in a non-leaf node and is concurrently split. When iterating over a multi-index entry in a non-leaf node, xas_load() sets xas->xa_offset to the base offset of the entry, but leaves xas->xa_index at the requested index. When the caller subsequently wants to advance to the next entry, xas_find() is called. xas_find() attempts to synchronize xas->xa_offset with xas->xa_index before advancing. However, the fixup logic was incorrectly restricted to leaf nodes (!xas->xa_node->shift). Because the THP resides in a non-leaf node, the fixup is skipped. As a result, xas_find() simply increments xas->xa_offset and recalculates xas->xa_index based on this new offset. This causes xas->xa_index to jump backwards. If the THP was concurrently split, the entry at the new offset is a node pointer, so xas_find() descends into it and returns the folio at the backwards index. The caller (filemap_map_pages()) then calculates the PTE pointer based on this backwards index, resulting in an invalid memory access such as an out-of-bounds read or use-after-free on a page-table page freed via tlb_remove_table_rcu(). To fix this, check if xas->xa_offset matches get_offset(xas->xa_index, xas->xa_node). If it does not and the node is a non-leaf node, set xas->xa_offset to get_offset(xas->xa_index, xas->xa_node) before advancing. Also add test cases in test_xarray to verify xas_find() behavior when iterating over and splitting multi-index entries. Fixes: b803b42823d0 ("xarray: Add XArray iterators") Assisted-by: Gemini:gemini-3.7-flash syzbot Reported-by: syzbot+b72767277f29b6407083@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=b72767277f29b6407083 Link: https://syzkaller.appspot.com/ai_job?id=a01c56bd-74d0-411c-afb4-ee6f0cb6cb61 Signed-off-by: Krystian Kaniewski --- diff --git a/lib/test_xarray.c b/lib/test_xarray.c index 5ca0aefee..cda7245e4 100644 --- a/lib/test_xarray.c +++ b/lib/test_xarray.c @@ -1247,6 +1247,67 @@ static noinline void check_multi_find_3(struct xarray *xa) } } +static noinline void check_multi_find_4(struct xarray *xa) +{ +#ifdef CONFIG_XARRAY_MULTI + XA_STATE(xas, xa, 100); + XA_STATE_ORDER(split, xa, 0, 0); + void *entry; + unsigned long i; + + /* (1) Order-7 entry (0-127) with adjacent entry at 128, starting at 100 */ + xa_store_order(xa, 0, 7, xa_mk_index(0), GFP_KERNEL); + XA_BUG_ON(xa, xa_store_index(xa, 128, GFP_KERNEL) != NULL); + + rcu_read_lock(); + entry = xas_find(&xas, ULONG_MAX); + XA_BUG_ON(xa, entry != xa_mk_index(0)); + XA_BUG_ON(xa, xas.xa_index != 100); + + entry = xas_find(&xas, ULONG_MAX); + XA_BUG_ON(xa, entry != xa_mk_index(128)); + XA_BUG_ON(xa, xas.xa_index != 128); + + entry = xas_find(&xas, ULONG_MAX); + XA_BUG_ON(xa, entry != NULL); + rcu_read_unlock(); + + xa_erase_index(xa, 128); + xa_erase_index(xa, 0); + XA_BUG_ON(xa, !xa_empty(xa)); + + /* (2) Splitting a multi-index entry after a lookup begins inside it */ + xa_store_order(xa, 0, 7, xa_mk_index(0), GFP_KERNEL); + XA_BUG_ON(xa, xa_store_index(xa, 128, GFP_KERNEL) != NULL); + + xas_set(&xas, 100); + rcu_read_lock(); + entry = xas_find(&xas, ULONG_MAX); + XA_BUG_ON(xa, entry != xa_mk_index(0)); + XA_BUG_ON(xa, xas.xa_index != 100); + rcu_read_unlock(); + + xas_split_alloc(&split, xa_mk_index(0), 7, GFP_KERNEL); + xas_lock(&split); + xas_split(&split, xa_mk_index(0), 7); + for (i = 0; i < 128; i++) + __xa_store(xa, i, xa_mk_index(i), 0); + xas_unlock(&split); + + rcu_read_lock(); + entry = xas_find(&xas, ULONG_MAX); + XA_BUG_ON(xa, entry != xa_mk_index(128)); + XA_BUG_ON(xa, xas.xa_index != 128); + + entry = xas_find(&xas, ULONG_MAX); + XA_BUG_ON(xa, entry != NULL); + rcu_read_unlock(); + + xa_destroy(xa); + XA_BUG_ON(xa, !xa_empty(xa)); +#endif +} + static noinline void check_find_1(struct xarray *xa) { unsigned long i, j, k; @@ -1370,6 +1431,7 @@ static noinline void check_find(struct xarray *xa) check_multi_find_1(xa, i); check_multi_find_2(xa); check_multi_find_3(xa); + check_multi_find_4(xa); } /* See find_swap_entry() in mm/shmem.c */ diff --git a/lib/xarray.c b/lib/xarray.c index 9a8b49165..980324d68 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1406,9 +1406,11 @@ void *xas_find(struct xa_state *xas, unsigned long max) entry = xas_load(xas); if (entry || xas_not_node(xas->xa_node)) return entry; - } else if (!xas->xa_node->shift && - xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) { - xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1; + } else if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) { + if (!xas->xa_node->shift) + xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1; + else + xas->xa_offset = get_offset(xas->xa_index, xas->xa_node); } xas_next_offset(xas); base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f -- See https://goo.gle/syzbot-ai-patches for information about AI-generated patches. The person who has signed off on the patch is responsible for addressing comments. syzbot engineers can be reached at syzkaller@googlegroups.com.