Skip to contentSkip to conversation

md/bitmap: fix GPF in write_page caused by resize race

PATCH In review Jack Wang 2 participants 3 messages Jan 20 – Jan 26

Subsystems (2)
lore
Share & tools
lore

Jack Wang

author
#0 lore raw

PATCH 1/1 md/bitmap: fix GPF in write_page caused by resize race

A General Protection Fault occurs in write_page() during array resize:

RIP: 0010:write_page+0x22b/0x3c0 [md_mod]

This is a use-after-free race between bitmap_daemon_work() and __bitmap_resize(). The daemon iterates over bitmap->storage.filemap without locking, while the resize path frees that storage via md_bitmap_file_unmap(). quiesce() does not stop the md thread, allowing concurrent access to freed pages.

Fix by holding mddev->bitmap_info.mutex during the bitmap update.

---
 drivers/md/md-bitmap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/md-bitmap.c +2 -1
index 84b7e2af6dba..7bb56d0491a2 100644
@@ -2453,6 +2453,7 @@ static int __bitmap_resize(struct bitmap *bitmap, sector_t blocks,
 		memcpy(page_address(store.sb_page),
 		       page_address(bitmap->storage.sb_page),
 		       sizeof(bitmap_super_t));
+	mutex_lock(&bitmap->mddev->bitmap_info.mutex);
 	spin_lock_irq(&bitmap->counts.lock);
 	md_bitmap_file_unmap(&bitmap->storage);
 	bitmap->storage = store;
@@ -2560,7 +2561,7 @@ static int __bitmap_resize(struct bitmap *bitmap, sector_t blocks,
 			set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
 	}
 	spin_unlock_irq(&bitmap->counts.lock);
-
+	mutex_unlock(&bitmap->mddev->bitmap_info.mutex);
 	if (!init) {
 		__bitmap_unplug(bitmap);
 		bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · Jack Wang

A General Protection Fault occurs in write_page() during array resize:

RIP: 0010:write_page+0x22b/0x3c0 [md_mod]

This is a use-after-free race between bitmap_daemon_work() and __bitmap_resize(). The daemon iterates over bitmap->storage.filemap without locking, while the resize path frees that storage via md_bitmap_file_unmap(). quiesce() does not stop the md thread, allowing concurrent access to freed pages.

Quoted 8 lines

Fix by holding mddev->bitmap_info.mutex during the bitmap update.

---
  drivers/md/md-bitmap.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

Applied with a fixtag:

Fixes d60b479d177a ("md/bitmap: add bitmap_resize function to allow

bitmap resizing.")

Signature · 2 lines
Thansk,
Kuai
author
#2 lore raw
Quoted 21 lines · Yu Kuai
Quoted 9 lines · Jack Wang

A General Protection Fault occurs in write_page() during array resize:

RIP: 0010:write_page+0x22b/0x3c0 [md_mod]

This is a use-after-free race between bitmap_daemon_work() and __bitmap_resize(). The daemon iterates over bitmap->storage.filemap without locking, while the resize path frees that storage via md_bitmap_file_unmap(). quiesce() does not stop the md thread, allowing concurrent access to freed pages.

Quoted 8 lines

Fix by holding mddev->bitmap_info.mutex during the bitmap update.

---
  drivers/md/md-bitmap.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 8 lines

Applied with a fixtag:

Fixes d60b479d177a ("md/bitmap: add bitmap_resize function to allow

bitmap resizing.")

--
Thansk,
Kuai

Hi Kuai,

Thanks for applying the fix with the fixes tag.

Best,
Jinpu