Skip to content

Commit 1f5f12e

Browse files
Peng Zhangakpm00
authored andcommitted
maple_tree: fix a potential memory leak, OOB access, or other unpredictable bug
In mas_alloc_nodes(), "node->node_count = 0" means to initialize the node_count field of the new node, but the node may not be a new node. It may be a node that existed before and node_count has a value, setting it to 0 will cause a memory leak. At this time, mas->alloc->total will be greater than the actual number of nodes in the linked list, which may cause many other errors. For example, out-of-bounds access in mas_pop_node(), and mas_pop_node() may return addresses that should not be used. Fix it by initializing node_count only for new nodes. Also, by the way, an if-else statement was removed to simplify the code. Link: https://lkml.kernel.org/r/[email protected] Fixes: 54a611b ("Maple Tree: add new data structure") Signed-off-by: Peng Zhang <[email protected]> Reviewed-by: Liam R. Howlett <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 9235756 commit 1f5f12e

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

lib/maple_tree.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,26 +1303,21 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
13031303
node = mas->alloc;
13041304
node->request_count = 0;
13051305
while (requested) {
1306-
max_req = MAPLE_ALLOC_SLOTS;
1307-
if (node->node_count) {
1308-
unsigned int offset = node->node_count;
1309-
1310-
slots = (void **)&node->slot[offset];
1311-
max_req -= offset;
1312-
} else {
1313-
slots = (void **)&node->slot;
1314-
}
1315-
1306+
max_req = MAPLE_ALLOC_SLOTS - node->node_count;
1307+
slots = (void **)&node->slot[node->node_count];
13161308
max_req = min(requested, max_req);
13171309
count = mt_alloc_bulk(gfp, max_req, slots);
13181310
if (!count)
13191311
goto nomem_bulk;
13201312

1313+
if (node->node_count == 0) {
1314+
node->slot[0]->node_count = 0;
1315+
node->slot[0]->request_count = 0;
1316+
}
1317+
13211318
node->node_count += count;
13221319
allocated += count;
13231320
node = node->slot[0];
1324-
node->node_count = 0;
1325-
node->request_count = 0;
13261321
requested -= count;
13271322
}
13281323
mas->alloc->total = allocated;

0 commit comments

Comments
 (0)