Skip to contentSkip to conversation

mm/cma_debug: fix invalid accesses for inactive CMA areas

PATCH Applied Muchun Song 5 participants 6 messages May 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/cma_debug: fix invalid accesses for inactive CMA areas

cma_activate_area() can fail after allocating range bitmaps. Its cleanup path frees those bitmaps, but only clears cma->count and cma->available_count. It leaves cma->nranges and each range's count in place, so cma_debugfs_init() can still register debugfs files for an area that never activated successfully.

That exposes two problems. Reading the bitmap file can make debugfs walk a freed range bitmap and trigger an invalid memory access. Reading maxchunk can also take cma->lock even though that lock is initialized only on the successful activation path.

Fix this by creating debugfs entries only for CMA areas that reached CMA_ACTIVATED.

Fixes c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested")
Fixes 2e32b947606d ("mm: cma: add functions to get region pages counters")
---
 mm/cma_debug.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/cma_debug.c +2 -1
index 5ae38f5abbcc..523ba4a0f9f7 100644
@@ -205,7 +205,8 @@ static int __init cma_debugfs_init(void)
 	cma_debugfs_root = debugfs_create_dir("cma", NULL);
 
 	for (i = 0; i < cma_area_count; i++)
-		cma_debugfs_add_one(&cma_areas[i], cma_debugfs_root);
+		if (test_bit(CMA_ACTIVATED, &cma_areas[i].flags))
+			cma_debugfs_add_one(&cma_areas[i], cma_debugfs_root);
 
 	return 0;
 }

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83

Signature · 1 line
2.54.0
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Muchun Song

cma_activate_area() can fail after allocating range bitmaps. Its cleanup path frees those bitmaps, but only clears cma->count and cma->available_count. It leaves cma->nranges and each range's count in place, so cma_debugfs_init() can still register debugfs files for an area that never activated successfully.

That exposes two problems. Reading the bitmap file can make debugfs walk a freed range bitmap and trigger an invalid memory access. Reading maxchunk can also take cma->lock even though that lock is initialized only on the successful activation path.

Quoted 7 lines

Fix this by creating debugfs entries only for CMA areas that reached CMA_ACTIVATED.

Fixes c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested")
Fixes 2e32b947606d ("mm: cma: add functions to get region pages counters")
Quoted 24 lines
---
 mm/cma_debug.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/cma_debug.c +2 -1
index 5ae38f5abbcc..523ba4a0f9f7 100644
@@ -205,7 +205,8 @@ static int __init cma_debugfs_init(void)
 	cma_debugfs_root = debugfs_create_dir("cma", NULL);
 
 	for (i = 0; i < cma_area_count; i++)
-		cma_debugfs_add_one(&cma_areas[i], cma_debugfs_root);
+		if (test_bit(CMA_ACTIVATED, &cma_areas[i].flags))
+			cma_debugfs_add_one(&cma_areas[i], cma_debugfs_root);
 
 	return 0;
 }

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83

Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours,
Mike.
Acked-by on v1 1/1
#2 lore raw
In reply to Muchun Song
Quoted 11 lines · Muchun Song

cma_activate_area() can fail after allocating range bitmaps. Its cleanup path frees those bitmaps, but only clears cma->count and cma->available_count. It leaves cma->nranges and each range's count in place, so cma_debugfs_init() can still register debugfs files for an area that never activated successfully.

That exposes two problems. Reading the bitmap file can make debugfs walk a freed range bitmap and trigger an invalid memory access. Reading maxchunk can also take cma->lock even though that lock is initialized only on the successful activation path.

Quoted 7 lines

Fix this by creating debugfs entries only for CMA areas that reached CMA_ACTIVATED.

Fixes c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested")
Fixes 2e32b947606d ("mm: cma: add functions to get region pages counters")

For the change:

About Fixes, does this mean that before c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested"), this was already triggerable after 2e32b947606d?

Signature · 2 lines
Oscar Salvador
SUSE Labs
#3 lore raw
Quoted 24 lines · Oscar Salvador (SUSE)
Quoted 11 lines · Muchun Song

cma_activate_area() can fail after allocating range bitmaps. Its cleanup path frees those bitmaps, but only clears cma->count and cma->available_count. It leaves cma->nranges and each range's count in place, so cma_debugfs_init() can still register debugfs files for an area that never activated successfully.

That exposes two problems. Reading the bitmap file can make debugfs walk a freed range bitmap and trigger an invalid memory access. Reading maxchunk can also take cma->lock even though that lock is initialized only on the successful activation path.

Quoted 7 lines

Fix this by creating debugfs entries only for CMA areas that reached CMA_ACTIVATED.

Fixes c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested")
Fixes 2e32b947606d ("mm: cma: add functions to get region pages counters")
Quoted 7 lines

For the change:

About Fixes, does this mean that before c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested"), this was already triggerable after 2e32b947606d?

c009da4258f9 introduced the invalid access to bitmap file. 2e32b947606d introduced the invalid access to cma->lock.

This change applies to both issues. So I added two Fixes tags.

Thanks.

Quoted 5 lines
Signature · 3 lines
-- 
Oscar Salvador
SUSE Labs
maintainer Acked-by on v1 1/1
#4 lore raw
In reply to Muchun Song
Quoted 11 lines · Muchun Song

cma_activate_area() can fail after allocating range bitmaps. Its cleanup path frees those bitmaps, but only clears cma->count and cma->available_count. It leaves cma->nranges and each range's count in place, so cma_debugfs_init() can still register debugfs files for an area that never activated successfully.

That exposes two problems. Reading the bitmap file can make debugfs walk a freed range bitmap and trigger an invalid memory access. Reading maxchunk can also take cma->lock even though that lock is initialized only on the successful activation path.

Quoted 8 lines

Fix this by creating debugfs entries only for CMA areas that reached CMA_ACTIVATED.

Fixes c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested")
Fixes 2e32b947606d ("mm: cma: add functions to get region pages counters")

---

cma_sysfs_init() also traverses all cma_area_count. Does it make sense to expose them there?

Signature · 3 lines
Cheers,

David
#5 lore raw
Quoted 19 lines · David Hildenbrand (Arm)
Quoted 14 lines · Muchun Song

cma_activate_area() can fail after allocating range bitmaps. Its cleanup path frees those bitmaps, but only clears cma->count and cma->available_count. It leaves cma->nranges and each range's count in place, so cma_debugfs_init() can still register debugfs files for an area that never activated successfully.

That exposes two problems. Reading the bitmap file can make debugfs walk a freed range bitmap and trigger an invalid memory access. Reading maxchunk can also take cma->lock even though that lock is initialized only on the successful activation path.

Fix this by creating debugfs entries only for CMA areas that reached CMA_ACTIVATED.

Quoted 7 lines
Quoted 5 lines
Fixes c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested")
Fixes 2e32b947606d ("mm: cma: add functions to get region pages counters")

---

Thanks.

Quoted 3 lines

cma_sysfs_init() also traverses all cma_area_count. Does it make sense to expose them there?

It is better to hide them from users. A separate cleanup patch is better since there is no critical issue when accessing those sysfs files.

Thanks,
Muhcun

Quoted 5 lines
Signature · 4 lines
-- 
Cheers,

David