[v2,06/11] malloc: check result of rte_mem_virt2memseg_list

Message ID 20221121223208.1147154-7-okaya@kernel.org (mailing list archive)
State Superseded, archived
Headers
Series codeql fixes for various subsystems |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Sinan Kaya Nov. 21, 2022, 10:32 p.m. UTC
  From: Sinan Kaya <okaya@kernel.org>

In alloc_pages_on_heap result of call to rte_mem_virt2memseg_list
is dereferenced here and may be null.

Signed-off-by: Sinan Kaya <okaya@kernel.org>
---
 lib/eal/common/malloc_heap.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Dmitry Kozlyuk Nov. 22, 2022, 3:52 p.m. UTC | #1
2022-11-21 17:32 (UTC-0500), okaya@kernel.org:
> From: Sinan Kaya <okaya@kernel.org>
> 
> In alloc_pages_on_heap result of call to rte_mem_virt2memseg_list
> is dereferenced here and may be null.
> 
> Signed-off-by: Sinan Kaya <okaya@kernel.org>
> ---
>  lib/eal/common/malloc_heap.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/lib/eal/common/malloc_heap.c b/lib/eal/common/malloc_heap.c
> index 503e551bf9..3f41430e42 100644
> --- a/lib/eal/common/malloc_heap.c
> +++ b/lib/eal/common/malloc_heap.c
> @@ -323,6 +323,8 @@ alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
>  
>  	map_addr = ms[0]->addr;
>  	msl = rte_mem_virt2memseg_list(map_addr);
> +	if (msl == NULL)
> +		return NULL;

It is not really possible, because the memory lock is held,
so "map_addr" cannot be unmapped/remapped concurrently,
and "ms" belongs to some MSL by definition of memseg.
RTE_ASSERT() can be added for clarity.
  

Patch

diff --git a/lib/eal/common/malloc_heap.c b/lib/eal/common/malloc_heap.c
index 503e551bf9..3f41430e42 100644
--- a/lib/eal/common/malloc_heap.c
+++ b/lib/eal/common/malloc_heap.c
@@ -323,6 +323,8 @@  alloc_pages_on_heap(struct malloc_heap *heap, uint64_t pg_sz, size_t elt_size,
 
 	map_addr = ms[0]->addr;
 	msl = rte_mem_virt2memseg_list(map_addr);
+	if (msl == NULL)
+		return NULL;
 
 	/* check if we wanted contiguous memory but didn't get it */
 	if (contig && !eal_memalloc_is_contig(msl, map_addr, alloc_sz)) {