Skip to contentSkip to conversation

mm: cma: Fix potential null dereference on pointer cma

PATCH In review Colin King 3 participants 3 messages Mar 16, 2021 – Mar 17, 2021

Subsystems (3)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 mm: cma: Fix potential null dereference on pointer cma

From: Colin Ian King <[email protected]>

At the start of the function there is a null pointer check on cma and then branch to error handling label 'out'. The subsequent call to cma_sysfs_fail_pages_count dereferences cma, hence there is a potential null pointer deference issue. Fix this by only calling cma_sysfs_fail_pages_count if cma is not null.

Addresses-Coverity: ("Dereference after null check")

Fixes dc4764f7e9ac ("mm: cma: support sysfs")
---
 mm/cma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/cma.c +2 -1
index 6d4dbafdb318..90e27458ddb7 100644
@@ -512,7 +512,8 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 		cma_sysfs_alloc_pages_count(cma, count);
 	} else {
 		count_vm_event(CMA_ALLOC_FAIL);
-		cma_sysfs_fail_pages_count(cma, count);
+		if (cma)
+			cma_sysfs_fail_pages_count(cma, count);
 	}
 
 	return page;
Signature · 1 line
2.30.2
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Colin King

From: Colin Ian King <[email protected]>

At the start of the function there is a null pointer check on cma and then branch to error handling label 'out'. The subsequent call to cma_sysfs_fail_pages_count dereferences cma, hence there is a potential null pointer deference issue. Fix this by only calling cma_sysfs_fail_pages_count if cma is not null.

Addresses-Coverity: ("Dereference after null check")

Fixes dc4764f7e9ac ("mm: cma: support sysfs")
Reviewed-by on v1 1/1
#2 lore raw
In reply to Colin King
Quoted 6 lines · Colin King

From: Colin Ian King <[email protected]>

At the start of the function there is a null pointer check on cma and then branch to error handling label 'out'. The subsequent call to cma_sysfs_fail_pages_count dereferences cma, hence there is a potential null pointer deference issue. Fix this by only calling

As far as I can tell, it's not possible to actually cause that null failure with the existing kernel code paths. *Might* be worth mentioning that here (unless I'm wrong), but either way, looks good, so:

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 24 lines

cma_sysfs_fail_pages_count if cma is not null.

Addresses-Coverity: ("Dereference after null check")

Fixes dc4764f7e9ac ("mm: cma: support sysfs")
---
  mm/cma.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
mm/cma.c +2 -1
index 6d4dbafdb318..90e27458ddb7 100644
@@ -512,7 +512,8 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
  		cma_sysfs_alloc_pages_count(cma, count);
  	} else {
  		count_vm_event(CMA_ALLOC_FAIL);
-		cma_sysfs_fail_pages_count(cma, count);
+		if (cma)
+			cma_sysfs_fail_pages_count(cma, count);
  	}
 
  	return page;