Skip to contentSkip to conversation

graph: fix out of bounds access when re-allocate node objs

PATCH Superseded Zhirun Yan 4 participants 5 messages Jul 27, 2022 – Oct 10, 2022

lore
Share & tools
lore

Zhirun Yan

author
#0 lore raw

PATCH 1/1 graph: fix out of bounds access when re-allocate node objs

For __rte_node_enqueue_prologue(), If the number of objs is more than the node->size * 2, the extra objs will write out of bounds memory. It should use __rte_node_stream_alloc_size() to request enough memory.

And for rte_node_next_stream_put(), it will re-allocate a small size, when the node free space is small and new objs is less than the current node->size. Some objs pointers behind new size may be lost. And it will cause memory leak. It should request enough size of memory, containing the original objs and new objs at least.

Fixes 40d4f51403ec ("graph: implement fastpath routines")
---
 lib/graph/rte_graph_worker.h | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
lib/graph/rte_graph_worker.h +10 -4
index 0c0b9c095a..b7d145c3cb 100644
@@ -218,13 +218,16 @@ static __rte_always_inline void
 __rte_node_enqueue_prologue(struct rte_graph *graph, struct rte_node *node,
 			    const uint16_t idx, const uint16_t space)
 {
+	uint32_t req_size;
 
 	/* Add to the pending stream list if the node is new */
 	if (idx == 0)
 		__rte_node_enqueue_tail_update(graph, node);
 
-	if (unlikely(node->size < (idx + space)))
-		__rte_node_stream_alloc(graph, node);
+	if (unlikely(node->size < (idx + space))) {
+		req_size = rte_align32pow2(node->size + space);
+		__rte_node_stream_alloc_size(graph, node, req_size);
+	}
 }
 
 /**
@@ -430,9 +433,12 @@ rte_node_next_stream_get(struct rte_graph *graph, struct rte_node *node,
 	node = __rte_node_next_node_get(node, next);
 	const uint16_t idx = node->idx;
 	uint16_t free_space = node->size - idx;
+	uint32_t req_size;
 
-	if (unlikely(free_space < nb_objs))
-		__rte_node_stream_alloc_size(graph, node, nb_objs);
+	if (unlikely(free_space < nb_objs)) {
+		req_size = rte_align32pow2(node->size + nb_objs);
+		__rte_node_stream_alloc_size(graph, node, req_size);
+	}
 
 	return &node->objs[idx];
 }
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw
Quoted 29 lines · Zhirun Yan

For __rte_node_enqueue_prologue(), If the number of objs is more than the node->size * 2, the extra objs will write out of bounds memory. It should use __rte_node_stream_alloc_size() to request enough memory.

And for rte_node_next_stream_put(), it will re-allocate a small size, when the node free space is small and new objs is less than the current node->size. Some objs pointers behind new size may be lost. And it will cause memory leak. It should request enough size of memory, containing the original objs and new objs at least.

Fixes 40d4f51403ec ("graph: implement fastpath routines")
---
 lib/graph/rte_graph_worker.h | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
lib/graph/rte_graph_worker.h +1 -0
index 0c0b9c095a..b7d145c3cb 100644
@@ -218,13 +218,16 @@ static __rte_always_inline void
 __rte_node_enqueue_prologue(struct rte_graph *graph, struct rte_node *node,
                            const uint16_t idx, const uint16_t space)
 {
+       uint32_t req_size;
 

lib/graph/rte_graph_worker.h static __rte_always_inline void · line 230

Quoted 10 lines
lib/graph/rte_graph_worker.h +4 -2
        /* Add to the pending stream list if the node is new */
        if (idx == 0)
                __rte_node_enqueue_tail_update(graph, node);
 
-       if (unlikely(node->size < (idx + space)))
-               __rte_node_stream_alloc(graph, node);
+       if (unlikely(node->size < (idx + space))) {
+               req_size = rte_align32pow2(node->size + space);
+               __rte_node_stream_alloc_size(graph, node, req_size);
+       }

Change looks good to me.

Please have an inline function to avoid code duplication(Same change in rte_node_next_stream_get())

With above change:

Quoted 21 lines
lib/graph/rte_graph_worker.h +5 -3
 }
 
 /**
@@ -430,9 +433,12 @@ rte_node_next_stream_get(struct rte_graph *graph, struct rte_node *node,
        node = __rte_node_next_node_get(node, next);
        const uint16_t idx = node->idx;
        uint16_t free_space = node->size - idx;
+       uint32_t req_size;
 
-       if (unlikely(free_space < nb_objs))
-               __rte_node_stream_alloc_size(graph, node, nb_objs);
+       if (unlikely(free_space < nb_objs)) {
+               req_size = rte_align32pow2(node->size + nb_objs);
+               __rte_node_stream_alloc_size(graph, node, req_size);
+       }
 
        return &node->objs[idx];
 }
--

2.25.1

author
#2 lore raw

PATCH v2 1/1 graph: fix out of bounds access when re-allocate node objs

In reply to Zhirun Yan

For __rte_node_enqueue_prologue(), If the number of objs is more than the node->size * 2, the extra objs will write out of bounds memory. It should use __rte_node_stream_alloc_size() to request enough memory.

And for rte_node_next_stream_put(), it will re-allocate a small size, when the node free space is small and new objs is less than the current node->size. Some objs pointers behind new size may be lost. And it will cause memory leak. It should request enough size of memory, containing the original objs and new objs at least.

Fixes 40d4f51403ec ("graph: implement fastpath routines")
---
 lib/graph/rte_graph_worker.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/graph/rte_graph_worker.h +2 -2
index 0c0b9c095a..6dc7461659 100644
@@ -224,7 +224,7 @@ __rte_node_enqueue_prologue(struct rte_graph *graph, struct rte_node *node,
 		__rte_node_enqueue_tail_update(graph, node);
 
 	if (unlikely(node->size < (idx + space)))
-		__rte_node_stream_alloc(graph, node);
+		__rte_node_stream_alloc_size(graph, node, node->size + space);
 }
 
 /**
@@ -432,7 +432,7 @@ rte_node_next_stream_get(struct rte_graph *graph, struct rte_node *node,
 	uint16_t free_space = node->size - idx;
 
 	if (unlikely(free_space < nb_objs))
-		__rte_node_stream_alloc_size(graph, node, nb_objs);
+		__rte_node_stream_alloc_size(graph, node, node->size + nb_objs);
 
 	return &node->objs[idx];
 }
Signature · 1 line
2.25.1
Acked-by on v2 1/1
#3 lore raw
Quoted 25 lines

-----Original Message-----
From: Zhirun Yan <[email protected]>
Sent: Thursday, August 4, 2022 11:33 AM
To: [email protected]; Jerin Jacob Kollanukkaran <[email protected]>; Kiran Kumar Kokkilagadda <[email protected]>
Cc: Zhirun Yan <[email protected]>; Cunming Liang <[email protected]>
Subject: [EXT] [PATCH v2] graph: fix out of bounds access when re-allocate node objs

External Email

----------------------------------------------------------------------
For __rte_node_enqueue_prologue(), If the number of objs is more than the node->size * 2, the extra objs will write out of bounds memory. It should use __rte_node_stream_alloc_size() to request enough memory.

And for rte_node_next_stream_put(), it will re-allocate a small size, when the node free space is small and new objs is less than the current node->size. Some objs pointers behind new size may be lost. And it will cause memory leak. It should request enough size of memory, containing the original objs and new objs at least.

Quoted 4 lines
Fixes 40d4f51403ec ("graph: implement fastpath routines")
Quoted 32 lines
---
 lib/graph/rte_graph_worker.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/graph/rte_graph_worker.h index renamed from lib/graph/rte_graph_worker.h +0 -0

0c0b9c095a..6dc7461659 100644

lib/graph/rte_graph_worker.h +1 -1
@@ -224,7 +224,7 @@ __rte_node_enqueue_prologue(struct rte_graph
 *graph, struct rte_node *node,
 		__rte_node_enqueue_tail_update(graph, node);
 
 	if (unlikely(node->size < (idx + space)))
-		__rte_node_stream_alloc(graph, node);
+		__rte_node_stream_alloc_size(graph, node, node->size +
 space);
 }

/**

lib/graph/rte_graph_worker.h +1 -1
@@ -432,7 +432,7 @@ rte_node_next_stream_get(struct rte_graph *graph,
 struct rte_node *node,
 	uint16_t free_space = node->size - idx;
 
 	if (unlikely(free_space < nb_objs))
-		__rte_node_stream_alloc_size(graph, node, nb_objs);
+		__rte_node_stream_alloc_size(graph, node, node->size +
 nb_objs);
 

return &node->objs[idx];
}
--
2.25.1

#4 lore raw
Quoted 10 lines
Quoted 10 lines

For __rte_node_enqueue_prologue(), If the number of objs is more than the node->size * 2, the extra objs will write out of bounds memory. It should use __rte_node_stream_alloc_size() to request enough memory.

And for rte_node_next_stream_put(), it will re-allocate a small size, when the node free space is small and new objs is less than the current node->size. Some objs pointers behind new size may be lost. And it will cause memory leak. It should request enough size of memory, containing the original objs and new objs at least.

Quoted 6 lines
Quoted 4 lines
Fixes 40d4f51403ec ("graph: implement fastpath routines")

Applied, thanks.