Skip to contentSkip to conversation

lib/maple_tree: fix swapped arguments in mas_safe_pivot() call

PATCH Superseded Josh Law 2 participants 5 messages Mar 6

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Josh Law

author
#0 lore raw

PATCH 1/1 lib/maple_tree: fix swapped arguments in mas_safe_pivot() call

From: Josh Law <[email protected]>

The call to mas_safe_pivot() in mas_wr_extend_null() has the pivot index and maple type arguments swapped. The function signature expects (mas, pivots, piv, type) but the call passes (mas, pivots, type, piv).

This causes the pivot index to be interpreted as a maple node type and vice versa, leading to incorrect pivot lookups. In practice, this means a null-extending store into a maple tree node can read the wrong pivot value, potentially corrupting the range tracked by the maple state. For a VMA maple tree, this could cause an incorrect vm_area_struct range to be returned during operations like mmap or munmap, leading to silent memory mapping corruption.

Every other mas_safe_pivot() call site in the file passes the arguments in the correct (piv, type) order; this is the only one with them reversed.

---
 lib/maple_tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
index 5aa4c9500018..f82000821293 100644
@@ -3279,7 +3279,7 @@ static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
 	    (r_mas->last < r_mas->max) &&
 	    !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) {
 		r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots,
-					     r_wr_mas->type, r_mas->offset + 1);
+					     r_mas->offset + 1, r_wr_mas->type);
 		r_mas->offset++;
 	}
 }
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 23 lines · On Fri, 6 Mar 2026 20:08:20 +0000 Josh Law <[email protected]> wrote:

From: Josh Law <[email protected]>

The call to mas_safe_pivot() in mas_wr_extend_null() has the pivot index and maple type arguments swapped. The function signature expects (mas, pivots, piv, type) but the call passes (mas, pivots, type, piv).

This causes the pivot index to be interpreted as a maple node type and vice versa, leading to incorrect pivot lookups. In practice, this means a null-extending store into a maple tree node can read the wrong pivot value, potentially corrupting the range tracked by the maple state. For a VMA maple tree, this could cause an incorrect vm_area_struct range to be returned during operations like mmap or munmap, leading to silent memory mapping corruption.

Every other mas_safe_pivot() call site in the file passes the arguments in the correct (piv, type) order; this is the only one with them reversed.

...

lib/maple_tree.c static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas, · line 3284

Quoted 7 lines
lib/maple_tree.c +1 -1
@@ -3279,7 +3279,7 @@ static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
 	    (r_mas->last < r_mas->max) &&
 	    !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) {
 		r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots,
-					     r_wr_mas->type, r_mas->offset + 1);
+					     r_mas->offset + 1, r_wr_mas->type);
 		r_mas->offset++;
 	}

Whoops. How come nobody has noticed after 4+ years?

I'll add

	Fixes: 54a611b60590 ("Maple Tree: add new data structure")

and maybe cc:stable if we have a reason to do so.

author
#2 lore raw

6 Mar 2026 21:33:22 Andrew Morton <[email protected]>:

Quoted 35 lines · On Fri,  6 Mar 2026 20:08:20 +0000 Josh Law <[email protected]> wrote:
Quoted 24 lines

From: Josh Law <[email protected]>

The call to mas_safe_pivot() in mas_wr_extend_null() has the pivot index and maple type arguments swapped. The function signature expects (mas, pivots, piv, type) but the call passes (mas, pivots, type, piv).

This causes the pivot index to be interpreted as a maple node type and vice versa, leading to incorrect pivot lookups. In practice, this means a null-extending store into a maple tree node can read the wrong pivot value, potentially corrupting the range tracked by the maple state. For a VMA maple tree, this could cause an incorrect vm_area_struct range to be returned during operations like mmap or munmap, leading to silent memory mapping corruption.

Every other mas_safe_pivot() call site in the file passes the arguments in the correct (piv, type) order; this is the only one with them reversed.

...

lib/maple_tree.c +0 -0
@@ -3279,7 +3279,7 @@ static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
Quoted 7 lines

        (r_mas->last < r_mas->max) && !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) { r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots, -                        r_wr_mas->type, r_mas->offset + 1); +                        r_mas->offset + 1, r_wr_mas->type); r_mas->offset++;
    }

Whoops.  How come nobody has noticed after 4+ years?

Quoted 5 lines

I'll add

Fixes 54a611b60590 ("Maple Tree: add new data structure")

and maybe cc:stable if we have a reason to do so.

Hi Andrew, on thought, I'd like to add Cc: [email protected] to this.
Even though it's been 4 years, a swapped argument in a core data structure like Maple Tree is a silent bug that could cause rare corruption. Better to defuse it in the LTS kernels.

maintainer
#3 lore raw
Quoted 13 lines · On Fri, 6 Mar 2026 22:11:54 +0000 Josh Law <[email protected]> wrote:
Quoted 13 lines
Quoted 10 lines
lib/maple_tree.c +0 -0
@@ -3279,7 +3279,7 @@ static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,

        (r_mas->last < r_mas->max) && !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) { r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots, -                        r_wr_mas->type, r_mas->offset + 1); +                        r_mas->offset + 1, r_wr_mas->type); r_mas->offset++;
    }

Whoops.  How come nobody has noticed after 4+ years?

Quoted 8 lines
Quoted 5 lines

I'll add

Fixes 54a611b60590 ("Maple Tree: add new data structure")

and maybe cc:stable if we have a reason to do so.

Hi Andrew, on thought, I'd like to add Cc: [email protected] to this.
Even though it's been 4 years, a swapped argument in a core data structure like Maple Tree is a silent bug that could cause rare corruption. Better to defuse it in the LTS kernels.

Seems appropriate. But not as a hotfix, given the 3+ year thing.

I was hoping to hear some thoughts on why this has proven to be so benign.

author
#4 lore raw

6 Mar 2026 22:54:36 Andrew Morton <[email protected]>:

Quoted 19 lines · On Fri, 6 Mar 2026 22:11:54 +0000 Josh Law <[email protected]> wrote:
Quoted 18 lines
Quoted 18 lines
Quoted 11 lines
lib/maple_tree.c +0 -0
@@ -3279,7 +3279,7 @@ static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,

        (r_mas->last < r_mas->max) && !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) { r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots, -                        r_wr_mas->type, r_mas->offset + 1); +                        r_mas->offset + 1, r_wr_mas->type); r_mas->offset++;
    }

Whoops.  How come nobody has noticed after 4+ years?

I'll add

Fixes 54a611b60590 ("Maple Tree: add new data structure")
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

and maybe cc:stable if we have a reason to do so.

Hi Andrew, on thought, I'd like to add Cc: [email protected] to this.
Even though it's been 4 years, a swapped argument in a core data structure like Maple Tree is a silent bug that could cause rare corruption. Better to defuse it in the LTS kernels.

Seems appropriate.  But not as a hotfix, given the 3+ year thing.

I was hoping to hear some thoughts on why this has proven to be so benign.

I suspect it has proven benign because, in most common workloads, the maple_type enum and the offset happen to be small, overlapping integers. mas_safe_pivot likely returned a 'plausible' boundary that didn't trigger an immediate OOB access, and the tree's walking logic is resilient enough to handle a slightly miscalculated pivot during a spanning-null extension without crashing. It was a silent logic error rather than a loud memory error.

V/R

V/R

Josh law