Skip to content

Commit eaa1176

Browse files
rjarrytmonjalo
authored andcommitted
graph: fix updating edge with active graph
After creating at least one graph and calling rte_node_edge_update to add a new edge on a node which is in use in the graph, the node memory is reallocated but the active graph still has a pointer to the freed memory. When destroying the graph, it causes a use-after-free error detected by libasan: ERROR: AddressSanitizer: heap-use-after-free READ of size 8 at 0x7c4baa5e4da8 thread T0 #0 0x0000005ad224 in graph_node_fini lib/graph/graph.c:256 #1 0x0000005ae657 in rte_graph_destroy lib/graph/graph.c:504 ... freed by thread T0 here: #0 0x7f1bac4e5e4b in realloc.part.0 (/lib64/libasan.so.8+0xe5e4b) #1 0x0000005ab6d7 in edge_update lib/graph/node.c:271 #2 0x0000005abb1b in rte_node_edge_update lib/graph/node.c:339 ... previously allocated by thread T0 here: #0 0x7f1bac4e5e4b in realloc.part.0 (/lib64/libasan.so.8+0xe5e4b) #1 0x0000005ab6d7 in edge_update lib/graph/node.c:271 #2 0x0000005abb1b in rte_node_edge_update lib/graph/node.c:339 ... Use malloc+memcpy and add an internal function to replace all references to the old node memory before freeing it. Fixes: c59dac2 ("graph: implement node operations") Cc: [email protected] Signed-off-by: Robin Jarry <[email protected]> Acked-by: Jerin Jacob <[email protected]>
1 parent cdeb353 commit eaa1176

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/graph/graph.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,20 @@ graph_node_fini(struct graph *graph)
277277
graph_node->node->name));
278278
}
279279

280+
void
281+
graph_node_replace_all(struct node *old, struct node *new)
282+
{
283+
struct graph_node *graph_node;
284+
struct graph *graph;
285+
286+
STAILQ_FOREACH(graph, &graph_list, next) {
287+
STAILQ_FOREACH(graph_node, &graph->node_list, next) {
288+
if (graph_node->node == old)
289+
graph_node->node = new;
290+
}
291+
}
292+
}
293+
280294
static struct rte_graph *
281295
graph_mem_fixup_node_ctx(struct rte_graph *graph)
282296
{

lib/graph/graph_private.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,18 @@ int graph_node_has_edge_to_src_node(struct graph *graph);
299299
*/
300300
int graph_node_has_loop_edge(struct graph *graph);
301301

302+
/**
303+
* @internal
304+
*
305+
* Replace all pointers of a given node with another one in all active graphs.
306+
*
307+
* @param old
308+
* Node pointer to replace in all graphs.
309+
* @param new
310+
* Updated pointer.
311+
*/
312+
void graph_node_replace_all(struct node *old, struct node *new);
313+
302314
/**
303315
* @internal
304316
*

lib/graph/node.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,15 @@ edge_update(struct node *node, struct node *prev, rte_edge_t from,
325325
need_realloc = max_edges > node->nb_edges;
326326
if (need_realloc) {
327327
sz = sizeof(struct node) + (max_edges * RTE_NODE_NAMESIZE);
328-
new_node = realloc(node, sz);
328+
new_node = malloc(sz);
329329
if (new_node == NULL) {
330330
rte_errno = ENOMEM;
331331
goto restore;
332332
} else {
333+
sz = sizeof(*node) + (node->nb_edges * RTE_NODE_NAMESIZE);
334+
memcpy(new_node, node, sz);
335+
graph_node_replace_all(node, new_node);
336+
free(node);
333337
node = new_node;
334338
}
335339
}

0 commit comments

Comments
 (0)