Skip to contentSkip to conversation

lib/maple_tree: fix potential NULL dereference in mas_pop_node()

PATCH In review Josh Law 2 participants 2 messages Mar 13 – Apr 1

Subsystems (3)
lore
Share & tools
lore

Josh Law

author
#0 lore raw

PATCH v2 1/1 lib/maple_tree: fix potential NULL dereference in mas_pop_node()

If kmem_cache_alloc_from_sheaf() returns NULL (possible under GFP_NOWAIT pressure), mas_pop_node() falls through to the out label and dereferences the NULL pointer in memset(ret, 0, sizeof(*ret)).

Add a NULL check after the sheaf allocation to bail out early.

---
v2: drop WARN_ON_ONCE — this is a recoverable allocation failure,
not a state that warrants a warning splat.

 lib/maple_tree.c | 2 ++
 1 file changed, 2 insertions(+)
lib/maple_tree.c +2 -0
index 739918e859e5..1eaaa5f964e9 100644
@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas)
 		return NULL;
 
 	ret = kmem_cache_alloc_from_sheaf(maple_node_cache, GFP_NOWAIT, mas->sheaf);
+	if (!ret)
+		return NULL;
 
 out:
 	memset(ret, 0, sizeof(*ret));

--
2.34.1

#1 lore raw
Quoted 19 lines · Josh Law

If kmem_cache_alloc_from_sheaf() returns NULL (possible under GFP_NOWAIT pressure), mas_pop_node() falls through to the out label and dereferences the NULL pointer in memset(ret, 0, sizeof(*ret)).

Add a NULL check after the sheaf allocation to bail out early.

---
v2: drop WARN_ON_ONCE — this is a recoverable allocation failure,
not a state that warrants a warning splat.

 lib/maple_tree.c | 2 ++
 1 file changed, 2 insertions(+)
lib/maple_tree.c +0 -0
index 739918e859e5..1eaaa5f964e9 100644

lib/maple_tree.c static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) · line 1068

Quoted 6 lines
lib/maple_tree.c +2 -0
@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas)
 		return NULL;
 
 	ret = kmem_cache_alloc_from_sheaf(maple_node_cache, GFP_NOWAIT, mas->sheaf);
+	if (!ret)
+		return NULL;
 

This isn't necessary, as explained to v1 after this was sent, I think?

What a mess. I'm objecting.

Liam