Skip to contentSkip to conversation

linux_compat: fix NULL pointer dereference in get_mem()

PATCH In review Anton Moryakov 2 participants 3 messages Feb 26 – Mar 13 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux_compat: fix NULL pointer dereference in get_mem()

Add NULL check after memalign() call in get_mem() to prevent potential NULL pointer dereference (CWE-476).

The function memalign() can return NULL on allocation failure. Dereferencing the returned pointer without checking for NULL may cause a crash in low-memory conditions.

Changes:
- Add NULL check after memalign() allocation
- Return NULL on failure, consistent with function semantics

This fixes the static analyzer warning:
linux_compat.c:34: dereference of memalign return value without NULL check

Reported-by static analyzer Svace
---
 lib/linux_compat.c | 3 +++
 1 file changed, 3 insertions(+)
lib/linux_compat.c +3 -0
index 985e88eb397..e4a3293e3af 100644
@@ -32,6 +32,9 @@ struct kmem_cache *get_mem(int element_sz)
 	struct kmem_cache *ret;
 
 	ret = memalign(ARCH_DMA_MINALIGN, sizeof(struct kmem_cache));
+	if (!ret)
+		return NULL;
+
 	ret->sz = element_sz;
 
 	return ret;
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Anton Moryakov

Add NULL check after memalign() call in get_mem() to prevent potential NULL pointer dereference (CWE-476).

The function memalign() can return NULL on allocation failure. Dereferencing the returned pointer without checking for NULL may cause a crash in low-memory conditions.

Changes:
- Add NULL check after memalign() allocation
- Return NULL on failure, consistent with function semantics

Quoted 5 lines

This fixes the static analyzer warning:
linux_compat.c:34: dereference of memalign return value without NULL check

Reported-by static analyzer Svace
Signature · 1 line
Tom
#2 lore raw
In reply to Anton Moryakov
Quoted 8 lines · Anton Moryakov

Add NULL check after memalign() call in get_mem() to prevent potential NULL pointer dereference (CWE-476).

The function memalign() can return NULL on allocation failure. Dereferencing the returned pointer without checking for NULL may cause a crash in low-memory conditions.

[...]

Applied to u-boot/next, thanks!

[1/1] linux_compat: fix NULL pointer dereference in get_mem()

      commit: 21edd76aded65d566f20a6937495c0c3965b4063
Signature · 1 line
Tom