xarray: fix index jumping backwards in xas_find()
Subsystems (5)
syzbot
syzbotFrom: Krystian Kaniewski <[email protected]>
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.
---
lib/test_xarray.c +62 -0
@@ -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 */
lib/xarray.c +5 -3
@@ -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
Signature · 4 lines
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 [email protected].
Andrew Morton
maintainerQuoted 23 lines · On Thu, 3 Sep 2026 13:48:55 +0000 (UTC) "syzbot" <[email protected]> wrote:
From: Krystian Kaniewski <[email protected]>
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()setsxas->xa_offsetto the base offset of the entry, but leavesxas->xa_indexat the requested index. When the caller subsequently wants to advance to the next entry,xas_find()is called.xas_find()attempts to synchronizexas->xa_offsetwithxas->xa_indexbefore 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 incrementsxas->xa_offsetand recalculatesxas->xa_indexbased on this new offset. This causesxas->xa_indexto jump backwards. If the THP was concurrently split, the entry at the new offset is a node pointer, soxas_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 viatlb_remove_table_rcu().
Quoted 5 lines
To fix this, check if
xas->xa_offsetmatchesget_offset(xas->xa_index, xas->xa_node). If it does not and the node is a non-leaf node, setxas->xa_offsettoget_offset(xas->xa_index, xas->xa_node)before advancing. Also add test cases intest_xarrayto verifyxas_find()behavior when iterating over and splitting multi-index entries.
Thanks. But the changelog omits vital information: a description of the userspace-visible runtime effects of the bug.
Quoted 4 lines
Fixes b803b42823d0 ("xarray: Add XArray iterators")Assisted-by Gemini:gemini-3.7-flash syzbotReported-by [email protected]
I see from there that it was a use-after-free, detected by KASAN. I'll update the changelog and I'll add cc:stable.
I'll queue the patch for testing while awaiting review input.
Quoted 39 lines · Andrew Morton
Quoted 23 lines · On Thu, 3 Sep 2026 13:48:55 +0000 (UTC) "syzbot" <[email protected]> wrote:
From: Krystian Kaniewski <[email protected]>
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()setsxas->xa_offsetto the base offset of the entry, but leavesxas->xa_indexat the requested index. When the caller subsequently wants to advance to the next entry,xas_find()is called.xas_find()attempts to synchronizexas->xa_offsetwithxas->xa_indexbefore 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 incrementsxas->xa_offsetand recalculatesxas->xa_indexbased on this new offset. This causesxas->xa_indexto jump backwards. If the THP was concurrently split, the entry at the new offset is a node pointer, soxas_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 viatlb_remove_table_rcu().Quoted 5 lines
To fix this, check if
xas->xa_offsetmatchesget_offset(xas->xa_index, xas->xa_node). If it does not and the node is a non-leaf node, setxas->xa_offsettoget_offset(xas->xa_index, xas->xa_node)before advancing. Also add test cases intest_xarrayto verifyxas_find()behavior when iterating over and splitting multi-index entries.Thanks. But the changelog omits vital information: a description of the userspace-visible runtime effects of the bug.
Quoted 4 lines
Fixes b803b42823d0 ("xarray: Add XArray iterators")Assisted-by Gemini:gemini-3.7-flash syzbotReported-by [email protected]
Quoted 4 lines
I see from there that it was a use-after-free, detected by KASAN. I'll update the changelog and I'll add cc:stable.
I'll queue the patch for testing while awaiting review input.
The testing shows there are issues with this patch:
https://github.com/linux-mm/linux-mm/actions/runs/33815757662/job/100847425973
And since it's in mm-unstable now it breaks all CI jobs :(
Quoted 3 lines
Signature · 2 lines
Sincerely yours, Mike.
Hello,
Thanks for running tests on my patch. I've quickly looked into the test result and the problem seems to be related to check_multi_find_4() test and not the fix itself. I'll send a v2 patch once I get the test fixed.
pt., 4 wrz 2026 o 10:22 Mike Rapoport <[email protected]> napisał(a):
Quoted 74 lines · Andrew Morton
Quoted 1 line
On Thu, 3 Sep 2026 13:48:55 +0000 (UTC) "syzbot" <[email protected]>
Quoted 5 lines · wrote:
Quoted 5 lines
From: Krystian Kaniewski <[email protected]>
A bug in the XArray iterator
xas_find()causes the iterator's index (xas->xa_index) to jump backwards when iterating over a multi-indexentry
Quoted 3 lines
Quoted 3 lines
(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
Quoted 1 line
Quoted 1 line
xas->xa_offsetto the base offset of the entry, but leaves
xas->xa_indexatQuoted 1 line
Quoted 1 line
the requested index. When the caller subsequently wants to advance to
the
Quoted 6 lines
Quoted 6 lines
next entry,
xas_find()is called.xas_find()attempts to synchronizexas->xa_offsetwithxas->xa_indexbefore 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 incrementsxas->xa_offsetandrecalculates
Quoted 1 line
Quoted 1 line
xas->xa_indexbased on this new offset. This causesxas->xa_indextojump
Quoted 1 line
Quoted 1 line
backwards. If the THP was concurrently split, the entry at the new
offset
Quoted 1 line
Quoted 1 line
is a node pointer, so
xas_find()descends into it and returns thefolio at
Quoted 1 line
Quoted 1 line
the backwards index. The caller (
filemap_map_pages()) then calculatesthe
Quoted 1 line
Quoted 1 line
PTE pointer based on this backwards index, resulting in an invalid
memory
Quoted 1 line
Quoted 1 line
access such as an out-of-bounds read or use-after-free on a page-table
page
Quoted 5 lines
Quoted 5 lines
freed via
tlb_remove_table_rcu().To fix this, check if
xas->xa_offsetmatchesget_offset(xas->xa_index, xas->xa_node). If it does not and the node is a non-leaf node, setxas->xa_offsettoget_offset(xas->xa_index, xas->xa_node)beforeadvancing.
Quoted 11 lines
Quoted 2 lines
Also add test cases in
test_xarrayto verifyxas_find()behavior when iterating over and splitting multi-index entries.Thanks. But the changelog omits vital information: a description of the userspace-visible runtime effects of the bug.
Quoted 4 lines
Fixes b803b42823d0 ("xarray: Add XArray iterators")Assisted-by Gemini:gemini-3.7-flash syzbotReported-by [email protected]Quoted 4 lines
I see from there that it was a use-after-free, detected by KASAN. I'll update the changelog and I'll add cc:stable.
I'll queue the patch for testing while awaiting review input.
The testing shows there are issues with this patch:
https://github.com/linux-mm/linux-mm/actions/runs/33815757662/job/100847425973
And since it's in mm-unstable now it breaks all CI jobs :(
Quoted 1 line
Quoted 1 line
Link:
https://syzkaller.appspot.com/ai_job?id=a01c56bd-74d0-411c-afb4-ee6f0cb6cb61
Quoted 2 lines
--
Sincerely yours,
Mike.
I have fixed the test issue and submitted patch as v2. I'm sorry about issue v1 caused, hopefully v2 is going to be fully correct now.
Quoted 76 lines · Krystian Kaniewski
Hello,
Thanks for running tests on my patch. I've quickly looked into the test result and the problem seems to be related to
check_multi_find_4()test and not the fix itself. I'll send a v2 patch once I get the test fixed.pt., 4 wrz 2026 o 10:22 Mike Rapoport <[email protected]> napisał(a):
Quoted 68 lines · Andrew Morton
Quoted 1 line
On Thu, 3 Sep 2026 13:48:55 +0000 (UTC) "syzbot" <[email protected]>
Quoted 4 lines · wrote:
Quoted 4 lines
From: Krystian Kaniewski <[email protected]>
A bug in the XArray iterator
xas_find()causes the iterator's index (xas->xa_index) to jump backwards when iterating over a multi-indexentry
Quoted 3 lines
Quoted 3 lines
(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
Quoted 1 line
Quoted 1 line
xas->xa_offsetto the base offset of the entry, but leaves
xas->xa_indexatQuoted 1 line
Quoted 1 line
the requested index. When the caller subsequently wants to advance to
the
Quoted 6 lines
Quoted 6 lines
next entry,
xas_find()is called.xas_find()attempts to synchronizexas->xa_offsetwithxas->xa_indexbefore 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 incrementsxas->xa_offsetandrecalculates
Quoted 1 line
Quoted 1 line
xas->xa_indexbased on this new offset. This causesxas->xa_indextojump
Quoted 1 line
Quoted 1 line
backwards. If the THP was concurrently split, the entry at the new
offset
Quoted 1 line
Quoted 1 line
is a node pointer, so
xas_find()descends into it and returns thefolio at
Quoted 1 line
Quoted 1 line
the backwards index. The caller (
filemap_map_pages()) then calculatesthe
Quoted 1 line
Quoted 1 line
PTE pointer based on this backwards index, resulting in an invalid
memory
Quoted 1 line
Quoted 1 line
access such as an out-of-bounds read or use-after-free on a page-table
page
Quoted 5 lines
Quoted 5 lines
freed via
tlb_remove_table_rcu().To fix this, check if
xas->xa_offsetmatchesget_offset(xas->xa_index, xas->xa_node). If it does not and the node is a non-leaf node, setxas->xa_offsettoget_offset(xas->xa_index, xas->xa_node)beforeadvancing.
Quoted 5 lines
Quoted 2 lines
Also add test cases in
test_xarrayto verifyxas_find()behavior when iterating over and splitting multi-index entries.Thanks. But the changelog omits vital information: a description of the userspace-visible runtime effects of the bug.
Quoted 8 lines
Quoted 4 lines
Fixes b803b42823d0 ("xarray: Add XArray iterators")Assisted-by Gemini:gemini-3.7-flash syzbotReported-by [email protected]I see from there that it was a use-after-free, detected by KASAN. I'll update the changelog and I'll add cc:stable.
I'll queue the patch for testing while awaiting review input.
The testing shows there are issues with this patch:
https://github.com/linux-mm/linux-mm/actions/runs/33815757662/job/100847425973
And since it's in mm-unstable now it breaks all CI jobs :(
Quoted 1 line
Quoted 1 line
Link:
https://syzkaller.appspot.com/ai_job?id=a01c56bd-74d0-411c-afb4-ee6f0cb6cb61
Quoted 1 line
--
Sincerely yours,
Mike.
PATCH v2 1/1 xarray: fix index jumping backwards in xas_find()
In reply to Mike RapoportA 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().
A userspace access that faults in a file-backed mapping can trigger this path. When the index moves backwards, filemap_map_pages() can calculate a PTE outside the page locked for fault-around and dereference a freed page-table page, resulting in a KASAN-detected use-after-free read.
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.
---
Changes in v2:
- Derive the regression-test geometry from XA_CHUNK_SHIFT so it works with both the userspace harness (SHIFT=3) and the kernel (SHIFT=6).
- Check the xas_split_alloc() result before calling xas_split().
- Describe the userspace-triggered use-after-free and add the stable Cc.
v1: https://lore.kernel.org/all/[email protected]/
Tests:
- Applied with git am to linux-mm commit b0266eddcb10 (the base of the reported CI run).
- Ubuntu 24.04 x86_64 mm-ci host tests: memblock, VMA, Maple tree, XArray, Radix tree and IDA, Multiorder XArray, and IDR all passed.
- Userspace XArray tests with ASan/UBSan passed for SHIFT=3 and SHIFT=6.
- The built-in XArray test suite passed under generic KASAN.
lib/test_xarray.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++ lib/xarray.c | 8 ++++-- 2 files changed, 75 insertions(+), 3 deletions(-)
lib/test_xarray.c +70 -0
@@ -1247,6 +1247,75 @@ static noinline void check_multi_find_3(struct xarray *xa) } } +static noinline void check_multi_find_4(struct xarray *xa) +{ +#ifdef CONFIG_XARRAY_MULTI +unsigned int order =XA_CHUNK_SHIFT +1 ; +unsigned long next =1UL << order; +unsigned long start = next -1 ; +XA_STATE (xas, xa, start); +XA_STATE_ORDER (split, xa,0 ,0 ); +void *entry; +unsigned long i; + +/* Multi-index entry in two slots of a non-leaf node. */ +xa_store_order (xa,0 , order,xa_mk_index (0 ),GFP_KERNEL ); +XA_BUG_ON (xa,xa_store_index (xa, next,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 != start); + + entry =xas_find (&xas,ULONG_MAX ); +XA_BUG_ON (xa, entry !=xa_mk_index (next)); +XA_BUG_ON (xa, xas.xa_index != next); + + entry =xas_find (&xas,ULONG_MAX ); +XA_BUG_ON (xa, entry !=NULL ); +rcu_read_unlock (); + +xa_erase_index (xa, next); +xa_erase_index (xa,0 ); +XA_BUG_ON (xa, !xa_empty (xa)); + +/* Split the multi-index entry after a lookup begins inside it. */ +xa_store_order (xa,0 , order,xa_mk_index (0 ),GFP_KERNEL ); +XA_BUG_ON (xa,xa_store_index (xa, next,GFP_KERNEL ) !=NULL ); + +xas_set (&xas, start); +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 != start); +rcu_read_unlock (); + +xas_split_alloc (&split,xa_mk_index (0 ), order,GFP_KERNEL ); +if (xas_error (&split)) { +XA_BUG_ON (xa, true); + goto out; + } +xas_lock (&split); +xas_split (&split,xa_mk_index (0 ), order); +for (i =0 ; i < next; 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 (next)); +XA_BUG_ON (xa, xas.xa_index != next); + + entry =xas_find (&xas,ULONG_MAX ); +XA_BUG_ON (xa, entry !=NULL ); +rcu_read_unlock (); + +out: +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 +1439,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 */
lib/xarray.c +5 -3
@@ -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
Signature · 1 line
2.53.0