FAILED: patch "[PATCH] zram: fix out-of-bounds access in writeback_store()" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-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.12.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-desolate-sports-060e@gregkh' --subject-prefix 'PATCH 6.12.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/3 zram: remove entry element member
From: Sergey Senozhatsky <[email protected]>
[ Upstream commit b4444a849f1827c04df81a6f1ef26ddb6f072a94 ]
Element is in the same anon union as handle and hence holds the same value, which makes code below sort of confusing
handle = zram_get_handle()
if (!handle)
element = zram_get_element()
Element doesn't really simplify the code, let's just remove it. We already re-purpose handle to store the block id a written back page.
Stable-dep-of: 894913e2d35c ("zram: fix out-of-bounds access in writeback_store()")
--- drivers/block/zram/zram_drv.c | 23 +++++------------------ drivers/block/zram/zram_drv.h | 5 +---- 2 files changed, 6 insertions(+), 22 deletions(-)
drivers/block/zram/zram_drv.c +5 -18
@@ -112,17 +112,6 @@ static void zram_clear_flag(struct zram *zram, u32 index, zram->table[index].flags &= ~BIT (flag); } -static inline void zram_set_element (struct zram *zram,u32 index, -unsigned long element) -{ - zram->table[index].element = element; -} - -static unsigned long zram_get_element (struct zram *zram,u32 index) -{ -return zram->table[index].element; -} -static size_t zram_get_obj_size (struct zram *zram,u32 index) {return zram->table[index].flags & (BIT (ZRAM_FLAG_SHIFT ) -1 );
@@ -762,7 +751,7 @@ static ssize_t writeback_store(struct device *dev,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_set_handle (zram, index, blk_idx); blk_idx =0 ;atomic64_inc (&zram->stats.pages_stored);spin_lock (&zram->wb_limit_lock);
@@ -1388,7 +1377,7 @@ static void zram_free_page(struct zram *zram, size_t index)if (zram_test_flag (zram, index,ZRAM_WB )) {zram_clear_flag (zram, index,ZRAM_WB ); -free_block_bdev (zram,zram_get_element (zram, index)); +free_block_bdev (zram,zram_get_handle (zram, index)); goto out; }
@@ -1434,12 +1423,10 @@ static int zram_read_from_zspool(struct zram *zram, struct page *page, handle =zram_get_handle (zram, index);if (!handle ||zram_test_flag (zram, index,ZRAM_SAME )) { -unsigned long value;void *mem; - value = handle ?zram_get_element (zram, index) :0 ; mem =kmap_local_page (page); -zram_fill_page (mem,PAGE_SIZE , value); +zram_fill_page (mem,PAGE_SIZE , handle);kunmap_local (mem);return 0 ; }
@@ -1485,7 +1472,7 @@ static int zram_read_page(struct zram *zram, struct page *page, u32 index,*/ zram_slot_unlock (zram, index); - ret =read_from_bdev (zram, page,zram_get_element (zram, index), + ret =read_from_bdev (zram, page,zram_get_handle (zram, index), parent); }
@@ -1637,7 +1624,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)if (flags) {zram_set_flag (zram, index, flags); -zram_set_element (zram, index, element); +zram_set_handle (zram, index, element); }else {zram_set_handle (zram, index, handle);zram_set_obj_size (zram, index, comp_len);
drivers/block/zram/zram_drv.h +1 -4
@@ -62,10 +62,7 @@ enum zram_pageflags {/* Allocated for each disk page */ struct zram_table_entry { -union { -unsigned long handle; -unsigned long element; - }; +unsigned long handle;unsigned int flags;spinlock_t lock;#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 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 | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/block/zram/zram_drv.c +5 -7
@@ -55,8 +55,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) {
@@ -694,14 +694,12 @@ static ssize_t writeback_store(struct device *dev,zram_set_flag (zram, index,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); +if (zram_read_from_zspool (zram, page, index)) {zram_clear_flag (zram, index,ZRAM_UNDER_WB );zram_clear_flag (zram, index,ZRAM_IDLE ); -zram_slot_unlock (zram, index); -continue ; + goto next; } +zram_slot_unlock (zram, index);bio_init (&bio, zram->bdev, &bio_vec,1 ,REQ_OP_WRITE |REQ_SYNC );
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 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 | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/block/zram/zram_drv.c +11 -4
@@ -599,7 +599,7 @@ static ssize_t writeback_store(struct device *dev, 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 nr_pages;unsigned long index =0 ;struct bio bio;struct bio_vec bio_vec;
@@ -620,11 +620,9 @@ static ssize_t writeback_store(struct device *dev,if (strncmp (buf,PAGE_WB_SIG ,sizeof (PAGE_WB_SIG ) -1 ))return -EINVAL ; -if (kstrtol (buf +sizeof (PAGE_WB_SIG ) -1 ,10 , &index) || - index >= nr_pages) +if (kstrtol (buf +sizeof (PAGE_WB_SIG ) -1 ,10 , &index))return -EINVAL ; - nr_pages =1 ; mode =PAGE_WRITEBACK ; }
@@ -634,6 +632,15 @@ static ssize_t writeback_store(struct device *dev, goto release_init_lock; } + nr_pages = zram->disksize >>PAGE_SHIFT ; +if (mode ==PAGE_WRITEBACK ) { +if (index >= nr_pages) { +up_read (&zram->init_lock); +return -EINVAL ; + } + nr_pages =1 ; + } +/* Do not permit concurrent post-processing actions. */ if (atomic_xchg (&zram->pp_in_progress,1 )) {up_read (&zram->init_lock);
Signature · 1 line
2.53.0