Skip to contentSkip to conversation

mm/cma: fix NULL pointer dereference when cma could not be activated

PATCH Applied Jianqun Xu 2 participants 2 messages Jun 15, 2020

Subsystems (2)
lore
Share & tools
lore

Jianqun Xu

author
#0 lore raw

PATCH 1/1 mm/cma: fix NULL pointer dereference when cma could not be activated

In some case the cma area could not be activated, but the cma_alloc be used under this case, then the kernel will crash caused by NULL pointer dereference.

Add bitmap valid check in cma_alloc to avoid this issue.

---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/cma.c +1 -1
index 0463ad2ce06b..488496fa2972 100644
@@ -425,7 +425,7 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 	struct page *page = NULL;
 	int ret = -ENOMEM;
 
-	if (!cma || !cma->count)
+	if (!cma || !cma->count || !cma->bitmap)
 		return NULL;
 
 	pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Jianqun Xu

In some case the cma area could not be activated, but the cma_alloc be used under this case, then the kernel will crash caused by NULL pointer dereference.

Add bitmap valid check in cma_alloc to avoid this issue.

---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/cma.c +0 -0
index 0463ad2ce06b..488496fa2972 100644

mm/cma.c struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align, · line 431

Quoted 8 lines
mm/cma.c +1 -1
@@ -425,7 +425,7 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 	struct page *page = NULL;
 	int ret = -ENOMEM;
 
-	if (!cma || !cma->count)
+	if (!cma || !cma->count || !cma->bitmap)
 		return NULL;
 
 	pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,

If cma_activate_area() fails, cma_init_reserved_areas() fails. AFAIKS, init/main.c will ignore any errors via do_one_initcall(). So this seems to be possible.

Signature · 3 lines
Thanks,

David / dhildenb