FAILED: patch "[PATCH] zram: fix out-of-bounds access in writeback_store()" failed to apply to 6.6-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 894913e2d35c46ff19a77530907771ae57862b96
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026090305-dimly-backboned-037d@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 894913e2d35c46ff19a77530907771ae57862b96 Mon Sep 17 00:00:00 2001
From: Longlong Xia <[email protected]>
Date: Tue, 4 Aug 2026 14:59:18 +0800
Subject: [PATCH] zram: fix out-of-bounds access in writeback_store()
Patch series "zram: fix stale scan bounds after reinitialization".
Both writeback_store() and read_block_state() derive their table scan bounds from zram->disksize before acquiring dev_lock. If the device is reset and reinitialized with a smaller disksize between that read and lock acquisition, the bound can describe the old table while the scan operates on the new one. This can lead to out-of-bounds slot accesses.
Move both bound calculations under dev_lock so each bound remains consistent with the table throughout its scan. Keep the fixes separate because the affected interfaces originate from different commits and can be backported independently.
This patch (of 2):
writeback_store() calculates the table scan bounds before taking dev_lock. A reset followed by reconfiguration with a smaller disksize can therefore replace zram->table while writeback_store() is waiting for the lock. Once it acquires the lock, it sees an initialized device but scans the new table using the old upper bound, resulting in an out-of-bounds access.
Calculate the number of pages while holding dev_lock so the scan bound matches the table protected by the lock.
drivers/block/zram/zram_drv.c +5 -2
@@ -1235,8 +1235,8 @@ static ssize_t writeback_store(struct device *dev, const char *buf, size_t len) { struct zram *zram = dev_to_zram(dev); -u64 nr_pages = zram->disksize >>PAGE_SHIFT ; -unsigned long lo =0 , hi = nr_pages; +u64 nr_pages; +unsigned long lo =0 , hi;struct zram_pp_ctl *pp_ctl =NULL ;struct zram_wb_ctl *wb_ctl =NULL ;char *args, *param, *val;
@@ -1250,6 +1250,9 @@ static ssize_t writeback_store(struct device *dev,if (!zram->backing_dev)return -ENODEV ; + nr_pages = zram->disksize >>PAGE_SHIFT ; + hi = nr_pages; + pp_ctl =init_pp_ctl ();if (!pp_ctl)return -ENOMEM ;
Sasha Levin
maintainerPATCH 1/4 zram: use zram_read_from_zspool() in writeback
From: Sergey Senozhatsky <[email protected]>
[ Upstream commit b8d3ff7bb5111c6a88d5672469de445d9b46cf22 ]
We only can read pages from zspool in writeback, zram_read_page() is not really right in that context not only because it's a more generic function that handles ZRAM_WB pages, but also because it requires us to unlock slot between slot flag check and actual page read. Use zram_read_from_zspool() instead and do slot flags check and page read under the same slot lock.
Stable-dep-of: 894913e2d35c ("zram: fix out-of-bounds access in writeback_store()")
--- drivers/block/zram/zram_drv.c | 136 ++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 65 deletions(-)
drivers/block/zram/zram_drv.c +71 -65
@@ -54,8 +54,8 @@ static size_t huge_class_size;static const struct block_device_operations zram_devops;static void zram_free_page (struct zram *zram,size_t index); -static int zram_read_page (struct zram *zram,struct page *page,u32 index, -struct bio *parent); +static int zram_read_from_zspool (struct zram *zram,struct page *page, +u32 index);static int zram_slot_trylock (struct zram *zram,u32 index) {
@@ -623,19 +623,42 @@ static void read_from_bdev_async(struct zram *zram, struct page *page,#define IDLE_WRITEBACK (1<<1)#define INCOMPRESSIBLE_WRITEBACK (1<<2) +/* Map newer writeback setup context to the 6.6 page allocation. */ +#define zram_pp_ctl page +#define pp_ctl page +#define init_pp_ctl () alloc_page(GFP_KERNEL) +static ssize_t writeback_store (struct device *dev, -struct device_attribute *attr,const char *buf,size_t len) +struct device_attribute *attr, +const char *buf,size_t len) {struct zram *zram =dev_to_zram (dev); -unsigned long nr_pages = zram->disksize >>PAGE_SHIFT ; -unsigned long index =0 ; +u64 nr_pages = zram->disksize >>PAGE_SHIFT ; +unsigned long lo =0 , hi = nr_pages; +struct zram_pp_ctl *pp_ctl =NULL ; +ssize_t ret = len; +int err, mode =0 ;struct bio bio;struct bio_vec bio_vec; -struct page *page; -ssize_t ret = len; -int mode, err;unsigned long blk_idx =0 ; +guard (rwsem_write)(&zram->init_lock); +if (!init_done (zram)) +return -EINVAL ; + +if (!zram->backing_dev) +return -ENODEV ; + + nr_pages = zram->disksize >>PAGE_SHIFT ; + hi = nr_pages; + + pp_ctl =init_pp_ctl (); +if (!pp_ctl) +return -ENOMEM ; +#undef init_pp_ctl +#undef pp_ctl +#undef zram_pp_ctl +if (sysfs_streq (buf,"idle" )) mode =IDLE_WRITEBACK ;else if (sysfs_streq (buf,"huge" ))
@@ -645,35 +668,22 @@ static ssize_t writeback_store(struct device *dev,else if (sysfs_streq (buf,"incompressible" )) mode =INCOMPRESSIBLE_WRITEBACK ;else { -if (strncmp (buf,PAGE_WB_SIG ,sizeof (PAGE_WB_SIG ) -1 )) -return -EINVAL ; +if (strncmp (buf,PAGE_WB_SIG ,sizeof (PAGE_WB_SIG ) -1 )) { + ret = -EINVAL ; + goto release_init_lock; + } -if (kstrtol (buf +sizeof (PAGE_WB_SIG ) -1 ,10 , &index) || - index >= nr_pages) -return -EINVAL ; +if (kstrtoul (buf +sizeof (PAGE_WB_SIG ) -1 ,10 , &lo) || + lo >= nr_pages) { + ret = -EINVAL ; + goto release_init_lock; + } - nr_pages =1 ; + hi = lo +1 ; mode =PAGE_WRITEBACK ; } -down_read (&zram->init_lock); -if (!init_done (zram)) { - ret = -EINVAL ; - goto release_init_lock; - } - -if (!zram->backing_dev) { - ret = -ENODEV ; - goto release_init_lock; - } - - page =alloc_page (GFP_KERNEL ); -if (!page) { - ret = -ENOMEM ; - goto release_init_lock; - } - -for (; nr_pages !=0 ; index++, nr_pages--) { +for (; lo < hi; lo++) {spin_lock (&zram->wb_limit_lock);if (zram->wb_limit_enable && !zram->bd_wb_limit) {spin_unlock (&zram->wb_limit_lock);
@@ -690,40 +700,38 @@ static ssize_t writeback_store(struct device *dev, } } -zram_slot_lock (zram, index); -if (!zram_allocated (zram, index)) +zram_slot_lock (zram, lo); +if (!zram_allocated (zram, lo)) goto next; -if (zram_test_flag (zram, index,ZRAM_WB ) || -zram_test_flag (zram, index,ZRAM_SAME ) || -zram_test_flag (zram, index,ZRAM_UNDER_WB )) +if (zram_test_flag (zram, lo,ZRAM_WB ) || +zram_test_flag (zram, lo,ZRAM_SAME ) || +zram_test_flag (zram, lo,ZRAM_UNDER_WB )) goto next;if (mode &IDLE_WRITEBACK && - !zram_test_flag (zram, index,ZRAM_IDLE )) + !zram_test_flag (zram, lo,ZRAM_IDLE )) goto next;if (mode &HUGE_WRITEBACK && - !zram_test_flag (zram, index,ZRAM_HUGE )) + !zram_test_flag (zram, lo,ZRAM_HUGE )) goto next;if (mode &INCOMPRESSIBLE_WRITEBACK && - !zram_test_flag (zram, index,ZRAM_INCOMPRESSIBLE )) + !zram_test_flag (zram, lo,ZRAM_INCOMPRESSIBLE )) goto next;/* * Clearing ZRAM_UNDER_WB is duty of caller. * IOW, zram_free_page never clear it. */ -zram_set_flag (zram, index,ZRAM_UNDER_WB ); +zram_set_flag (zram, lo,ZRAM_UNDER_WB );/* Need for hugepage writeback racing */ -zram_set_flag (zram, index,ZRAM_IDLE ); -zram_slot_unlock (zram, index); -if (zram_read_page (zram, page, index,NULL )) { -zram_slot_lock (zram, index); -zram_clear_flag (zram, index,ZRAM_UNDER_WB ); -zram_clear_flag (zram, index,ZRAM_IDLE ); -zram_slot_unlock (zram, index); -continue ; +zram_set_flag (zram, lo,ZRAM_IDLE ); +if (zram_read_from_zspool (zram, page, lo)) { +zram_clear_flag (zram, lo,ZRAM_UNDER_WB ); +zram_clear_flag (zram, lo,ZRAM_IDLE ); + goto next; } +zram_slot_unlock (zram, lo);bio_init (&bio, zram->bdev, &bio_vec,1 ,REQ_OP_WRITE |REQ_SYNC );
@@ -736,10 +744,10 @@ static ssize_t writeback_store(struct device *dev,*/ err =submit_bio_wait (&bio);if (err) { -zram_slot_lock (zram, index); -zram_clear_flag (zram, index,ZRAM_UNDER_WB ); -zram_clear_flag (zram, index,ZRAM_IDLE ); -zram_slot_unlock (zram, index); +zram_slot_lock (zram, lo); +zram_clear_flag (zram, lo,ZRAM_UNDER_WB ); +zram_clear_flag (zram, lo,ZRAM_IDLE ); +zram_slot_unlock (zram, lo);/* * BIO errors are not fatal, we continue and simply * attempt to writeback the remaining objects (pages).
@@ -762,18 +770,18 @@ static ssize_t writeback_store(struct device *dev,* mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB. * Thus, we could close the race by checking ZRAM_IDLE bit. */ -zram_slot_lock (zram, index); -if (!zram_allocated (zram, index) || - !zram_test_flag (zram, index,ZRAM_IDLE )) { -zram_clear_flag (zram, index,ZRAM_UNDER_WB ); -zram_clear_flag (zram, index,ZRAM_IDLE ); +zram_slot_lock (zram, lo); +if (!zram_allocated (zram, lo) || + !zram_test_flag (zram, lo,ZRAM_IDLE )) { +zram_clear_flag (zram, lo,ZRAM_UNDER_WB ); +zram_clear_flag (zram, lo,ZRAM_IDLE ); goto next; } -zram_free_page (zram, index); -zram_clear_flag (zram, index,ZRAM_UNDER_WB ); -zram_set_flag (zram, index,ZRAM_WB ); -zram_set_element (zram, index, blk_idx); +zram_free_page (zram, lo); +zram_clear_flag (zram, lo,ZRAM_UNDER_WB ); +zram_set_flag (zram, lo,ZRAM_WB ); +zram_set_element (zram, lo, blk_idx); blk_idx =0 ;atomic64_inc (&zram->stats.pages_stored);spin_lock (&zram->wb_limit_lock);
@@ -781,15 +789,13 @@ static ssize_t writeback_store(struct device *dev, zram->bd_wb_limit -=1UL << (PAGE_SHIFT -12 );spin_unlock (&zram->wb_limit_lock); next: -zram_slot_unlock (zram, index); +zram_slot_unlock (zram, lo); }if (blk_idx)free_block_bdev (zram, blk_idx);__free_page (page); release_init_lock: -up_read (&zram->init_lock); -return ret; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 zram: move post-processing target allocation
From: Sergey Senozhatsky <[email protected]>
[ Upstream commit 7e1b0212d4d59281a80c836841539a91b724bf24 ]
Allocate post-processing target in place_pp_slot(). This simplifies scan_slots_for_writeback() and scan_slots_for_recompress() loops because we don't need to track pps pointer state anymore. Previously we have to explicitly NULL the point if it has been added to a post-processing bucket or re-use previously allocated pointer otherwise and make sure we don't leak the memory in the end.
We are also fine doing GFP_NOIO allocation, as post-processing can be called under memory pressure so we better pick as many slots as we can as soon as we can and start post-processing them, possibly saving the memory. Allocation failure there is not fatal, we will post-process whatever we put into the buckets on previous iterations.
Stable-dep-of: 894913e2d35c ("zram: fix out-of-bounds access in writeback_store()")
--- drivers/block/zram/zram_drv.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/block/zram/zram_drv.c +8 -3
@@ -636,12 +636,20 @@ static ssize_t writeback_store(struct device *dev,u64 nr_pages = zram->disksize >>PAGE_SHIFT ;unsigned long lo =0 , hi = nr_pages;struct zram_pp_ctl *pp_ctl =NULL ; +struct zram_wb_ctl *wb_ctl =NULL ; +char *args, *param, *val;ssize_t ret = len;int err, mode =0 ;struct bio bio;struct bio_vec bio_vec;unsigned long blk_idx =0 ; +/* Keep the newer writeback setup context available for its fix. */ + (void )wb_ctl; + (void )args; + (void )param; + (void )val; +guard (rwsem_write)(&zram->init_lock);if (!init_done (zram))return -EINVAL ;
@@ -649,9 +657,6 @@ static ssize_t writeback_store(struct device *dev,if (!zram->backing_dev)return -ENODEV ; - nr_pages = zram->disksize >>PAGE_SHIFT ; - hi = nr_pages; - pp_ctl =init_pp_ctl ();if (!pp_ctl)return -ENOMEM ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 zram: do not leak page on writeback_store error path
In reply to Sasha LevinFrom: Sergey Senozhatsky <[email protected]>
[ Upstream commit a6d2193b3ef53d5c5a2c56d397227f5e891f2df3 ]
Ensure the page used for local object data is freed on error out path.
Stable-dep-of: 894913e2d35c ("zram: fix out-of-bounds access in writeback_store()")
--- drivers/block/zram/zram_drv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +2 -1
@@ -799,8 +799,9 @@ static ssize_t writeback_store(struct device *dev,if (blk_idx)free_block_bdev (zram, blk_idx); -__free_page (page); + release_init_lock: +__free_page (page);return ret; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 zram: fix out-of-bounds access in writeback_store()
In reply to Sasha LevinFrom: Longlong Xia <[email protected]>
[ Upstream commit 894913e2d35c46ff19a77530907771ae57862b96 ]
Patch series "zram: fix stale scan bounds after reinitialization".
Both writeback_store() and read_block_state() derive their table scan bounds from zram->disksize before acquiring dev_lock. If the device is reset and reinitialized with a smaller disksize between that read and lock acquisition, the bound can describe the old table while the scan operates on the new one. This can lead to out-of-bounds slot accesses.
Move both bound calculations under dev_lock so each bound remains consistent with the table throughout its scan. Keep the fixes separate because the affected interfaces originate from different commits and can be backported independently.
This patch (of 2):
writeback_store() calculates the table scan bounds before taking dev_lock. A reset followed by reconfiguration with a smaller disksize can therefore replace zram->table while writeback_store() is waiting for the lock. Once it acquires the lock, it sees an initialized device but scans the new table using the old upper bound, resulting in an out-of-bounds access.
Calculate the number of pages while holding dev_lock so the scan bound matches the table protected by the lock.
--- drivers/block/zram/zram_drv.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/block/zram/zram_drv.c +5 -2
@@ -633,8 +633,8 @@ static ssize_t writeback_store(struct device *dev, const char *buf, size_t len) { struct zram *zram = dev_to_zram(dev); -u64 nr_pages = zram->disksize >>PAGE_SHIFT ; -unsigned long lo =0 , hi = nr_pages; +u64 nr_pages; +unsigned long lo =0 , hi;struct zram_pp_ctl *pp_ctl =NULL ;struct zram_wb_ctl *wb_ctl =NULL ;char *args, *param, *val;
@@ -657,6 +657,9 @@ static ssize_t writeback_store(struct device *dev,if (!zram->backing_dev)return -ENODEV ; + nr_pages = zram->disksize >>PAGE_SHIFT ; + hi = nr_pages; + pp_ctl =init_pp_ctl ();if (!pp_ctl)return -ENOMEM ;
Signature · 1 line
2.53.0