zram: unsafe zram_get_element call in zram_read_page()
Subsystems (2)
PATCH 1/1 zram: unsafe zram_get_element call in zram_read_page()
Taken lock is required to access the content of zram entry.
--- drivers/block/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/zram/zram_drv.c +2 -2
@@ -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
Sergey Senozhatsky
maintainerQuoted 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?
Sergey Senozhatsky
maintainerQuoted 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 afterread_from_bdev()that the entry at index still hasZRAM_WBset and, if so, that it points to the sameblk_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
@@ -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. */
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 afterread_from_bdev()that the entry at index still hasZRAM_WBset and, if so, that it points to the sameblk_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.
Sergey Senozhatsky
maintainerQuoted 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 afterread_from_bdev()that the entry at index still hasZRAM_WBset and, if so, that it points to the sameblk_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.