Skip to content

Commit 955a923

Browse files
howlettakpm00
authored andcommitted
maple_tree: fix mas_empty_area_rev() null pointer dereference
Currently the code calls mas_start() followed by mas_data_end() if the maple state is MA_START, but mas_start() may return with the maple state node == NULL. This will lead to a null pointer dereference when checking information in the NULL node, which is done in mas_data_end(). Avoid setting the offset if there is no node by waiting until after the maple state is checked for an empty or single entry state. A user could trigger the events to cause a kernel oops by unmapping all vmas to produce an empty maple tree, then mapping a vma that would cause the scenario described above. Link: https://lkml.kernel.org/r/[email protected] Fixes: 54a611b ("Maple Tree: add new data structure") Signed-off-by: Liam R. Howlett <[email protected]> Reported-by: Marius Fleischer <[email protected]> Closes: https://lore.kernel.org/lkml/CAJg=8jyuSxDL6XvqEXY_66M20psRK2J53oBTP+fjV5xpW2-R6w@mail.gmail.com/ Link: https://lore.kernel.org/lkml/CAJg=8jyuSxDL6XvqEXY_66M20psRK2J53oBTP+fjV5xpW2-R6w@mail.gmail.com/ Tested-by: Marius Fleischer <[email protected]> Tested-by: Sidhartha Kumar <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent c88033e commit 955a923

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/maple_tree.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5109,18 +5109,18 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
51095109
if (size == 0 || max - min < size - 1)
51105110
return -EINVAL;
51115111

5112-
if (mas_is_start(mas)) {
5112+
if (mas_is_start(mas))
51135113
mas_start(mas);
5114-
mas->offset = mas_data_end(mas);
5115-
} else if (mas->offset >= 2) {
5116-
mas->offset -= 2;
5117-
} else if (!mas_rewind_node(mas)) {
5114+
else if ((mas->offset < 2) && (!mas_rewind_node(mas)))
51185115
return -EBUSY;
5119-
}
51205116

5121-
/* Empty set. */
5122-
if (mas_is_none(mas) || mas_is_ptr(mas))
5117+
if (unlikely(mas_is_none(mas) || mas_is_ptr(mas)))
51235118
return mas_sparse_area(mas, min, max, size, false);
5119+
else if (mas->offset >= 2)
5120+
mas->offset -= 2;
5121+
else
5122+
mas->offset = mas_data_end(mas);
5123+
51245124

51255125
/* The start of the window can only be within these values. */
51265126
mas->index = min;

0 commit comments

Comments
 (0)