Skip to contentSkip to conversation

zram: unsafe zram_get_element call in zram_read_page()

PATCH In review Vasily Averin 2 participants 5 messages Nov 6, 2023 – Nov 8, 2023

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

PATCH 1/1 zram: unsafe zram_get_element call in zram_read_page()

Taken lock is required to access the content of zram entry.

Fixes 8e19d540d107 ("zram: extend zero pages to same element pages")
---
 drivers/block/zram/zram_drv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/zram/zram_drv.c +2 -2
index d77d3664ca08..9ac3d4e51d26 100644
@@ -1362,14 +1362,14 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index,
 		ret = zram_read_from_zspool(zram, page, index);
 		zram_slot_unlock(zram, index);
 	} else {
+		unsigned long entry = zram_get_element(zram, index);
 		/*
 		 * The slot should be unlocked before reading from the backing
 		 * device.
 		 */
 		zram_slot_unlock(zram, index);
 
-		ret = read_from_bdev(zram, page, zram_get_element(zram, index),
-				     parent);
+		ret = read_from_bdev(zram, page, entry, parent);
 	}
 
 	/* Should NEVER happen. Return bio error if it does. */
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · Vasily Averin
drivers/block/zram/zram_drv.c +0 -0
@@ -1362,14 +1362,14 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index,
 		ret = zram_read_from_zspool(zram, page, index);
 		zram_slot_unlock(zram, index);
 	} else {

drivers/block/zram/zram_drv.c static int zram_read_page(struct zram *zram, struct page *page, u32 index, · line 1372

Quoted 10 lines
drivers/block/zram/zram_drv.c +2 -2
+		unsigned long entry = zram_get_element(zram, index);
 		/*
 		 * The slot should be unlocked before reading from the backing
 		 * device.
 		 */
 		zram_slot_unlock(zram, index);
 
-		ret = read_from_bdev(zram, page, zram_get_element(zram, index),
-				     parent);
+		ret = read_from_bdev(zram, page, entry, parent);

Hmmm,
We may want to do more here. Basically, we probably need to re-confirm after read_from_bdev() that the entry at index still has ZRAM_WB set and, if so, that it points to the same blk_idx. IOW, check that it has not been free-ed and re-used under us.

Minchan, what do you think? Is that scenario possible?

#2 lore raw
Quoted 22 lines · Sergey Senozhatsky
Quoted 4 lines · Vasily Averin
(quoted diff) +0 -0
@@ -1362,14 +1362,14 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index,
 		ret = zram_read_from_zspool(zram, page, index);
 		zram_slot_unlock(zram, index);
 	} else {

Review static int zram_read_page(struct zram *zram, struct page *page, u32 index, · line 1372

Quoted 10 lines
(quoted diff) +2 -2
+		unsigned long entry = zram_get_element(zram, index);
 		/*
 		 * The slot should be unlocked before reading from the backing
 		 * device.
 		 */
 		zram_slot_unlock(zram, index);
 
-		ret = read_from_bdev(zram, page, zram_get_element(zram, index),
-				     parent);
+		ret = read_from_bdev(zram, page, entry, parent);

Hmmm,
We may want to do more here. Basically, we probably need to re-confirm after read_from_bdev() that the entry at index still has ZRAM_WB set and, if so, that it points to the same blk_idx. IOW, check that it has not been free-ed and re-used under us.

Quoted 1 line

Minchan, what do you think? Is that scenario possible?

Basically

---

drivers/block/zram/zram_drv.c +9 -2
index f5e3756fc21a..2d69f6efcee3 100644
@@ -1364,14 +1364,21 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index,
 		ret = zram_read_from_zspool(zram, page, index);
 		zram_slot_unlock(zram, index);
 	} else {
+		unsigned long idx = zram_get_element(zram, index);
 		/*
 		 * The slot should be unlocked before reading from the backing
 		 * device.
 		 */
 		zram_slot_unlock(zram, index);
 
-		ret = read_from_bdev(zram, page, zram_get_element(zram, index),
-				     parent);
+		ret = read_from_bdev(zram, page, idx, parent);
+		if (ret == 0) {
+			zram_slot_lock(zram, index);
+			if (!zram_test_flag(zram, index, ZRAM_WB) ||
+			    idx != zram_get_element(zram, index))
+				ret = -EINVAL;
+			zram_slot_unlock(zram, index);
+		}
 	}
 
 	/* Should NEVER happen. Return bio error if it does. */
#3 lore raw
Quoted 19 lines · Sergey Senozhatsky
Quoted 5 lines · Sergey Senozhatsky

Hmmm,
We may want to do more here. Basically, we probably need to re-confirm after read_from_bdev() that the entry at index still has ZRAM_WB set and, if so, that it points to the same blk_idx. IOW, check that it has not been free-ed and re-used under us.

drivers/block/zram/zram_drv.c +1 -0
@@ -1364,14 +1364,21 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index,
 		ret = zram_read_from_zspool(zram, page, index);
 		zram_slot_unlock(zram, index);
 	} else {
+		unsigned long idx = zram_get_element(zram, index);
 		/*
 		 * The slot should be unlocked before reading from the backing
 		 * device.
 		 */
 		zram_slot_unlock(zram, index);
 

drivers/block/zram/zram_drv.c static int zram_read_page(struct zram *zram, struct page *page, u32 index, · line 1381

Quoted 10 lines
drivers/block/zram/zram_drv.c +8 -2
-		ret = read_from_bdev(zram, page, zram_get_element(zram, index),
-				     parent);
+		ret = read_from_bdev(zram, page, idx, parent);
+		if (ret == 0) {
+			zram_slot_lock(zram, index);
+			if (!zram_test_flag(zram, index, ZRAM_WB) ||
+			    idx != zram_get_element(zram, index))
+				ret = -EINVAL;
+			zram_slot_unlock(zram, index);
+		}

Why overwritten page can not be pushed to WB to the same blk_idx?
However I'm agree that this is VERY unlikely case, and this check is better than nothing.

#4 lore raw
Quoted 20 lines · Vasily Averin
Quoted 19 lines · Sergey Senozhatsky
Quoted 5 lines · Sergey Senozhatsky

Hmmm,
We may want to do more here. Basically, we probably need to re-confirm after read_from_bdev() that the entry at index still has ZRAM_WB set and, if so, that it points to the same blk_idx. IOW, check that it has not been free-ed and re-used under us.

drivers/block/zram/zram_drv.c +1 -0
@@ -1364,14 +1364,21 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index,
 		ret = zram_read_from_zspool(zram, page, index);
 		zram_slot_unlock(zram, index);
 	} else {
+		unsigned long idx = zram_get_element(zram, index);
 		/*
 		 * The slot should be unlocked before reading from the backing
 		 * device.
 		 */
 		zram_slot_unlock(zram, index);
 
Quoted 12 lines

drivers/block/zram/zram_drv.c static int zram_read_page(struct zram *zram, struct page *page, u32 index, · line 1381

Quoted 10 lines
drivers/block/zram/zram_drv.c +8 -2
-		ret = read_from_bdev(zram, page, zram_get_element(zram, index),
-				     parent);
+		ret = read_from_bdev(zram, page, idx, parent);
+		if (ret == 0) {
+			zram_slot_lock(zram, index);
+			if (!zram_test_flag(zram, index, ZRAM_WB) ||
+			    idx != zram_get_element(zram, index))
+				ret = -EINVAL;
+			zram_slot_unlock(zram, index);
+		}

Why overwritten page can not be pushed to WB to the same blk_idx?

Yeah, so I thought about it too but didn't want to go too deep into it. We probably can only address it if we synchronize free_page (?), read_page() and writeback(), so that we never have concurrent bitmap modifications when one of the operations is in progress.