Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E931A3939A4; Sat, 17 Oct 2026 17:56:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792259782; cv=none; b=sFwK0lq9h4DLo+vJQBUDIeKv/hCc/i9b/8VmtnmlH43rNCWGzTpbFKQBxrnX12VC0lLU0AZI7zA/gt1pcvnEAVx0hiCc7F7A0qghyOdWMNRI6z0jPbS3mjR+30m8dHMlkIcEKl8P0XuejgbXGs6Pz4UC4gzFwolIeKwUhTuuYG8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792259782; c=relaxed/simple; bh=mun7lygbxDvQD7D91pirXnz6uOoXq/6VkbxJPM9gz/8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iLNaPD++zAQFYssuae7Tv4911i6fGwui1efPn8jyKj3367nNv1JoGniomw4s8Sy1AKC3fZWSQtEFtMHT9OXaROMqLR1VMdA5yxy/WY7Z34c8Y5nLv8SwdU2R3MGn2L6sAG2LTWzuZH6ib5Q5nTi5lhOLBG2nevD5TyV8YNH6KUs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=x/RnCauT; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="x/RnCauT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 497AA1F000C4; Sat, 17 Oct 2026 17:56:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1792259780; bh=Jnp+ml9cWC6VwqORB9cVYxPx8F/abGxzg6r+DL/VVvY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=x/RnCauTJOFpapSVK7nyI/vfMPBV+7k1pw8hMOeLtm4uUtrJ98Rj47aWXwkMmO8lU Iithx5r/INujwym3G5GyQFOyNU+VutuX2x/kl1Hw0Q8XbXkHAR49ER25Cd9FXT2+48 XwVirqnR1ONyw2IisT8shMtqu3TBmumVYXSbO5mc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Longlong Xia , Sergey Senozhatsky , Jens Axboe , Minchan Kim , Andrew Morton , Sasha Levin Subject: [PATCH 6.6 1329/1674] zram: fix out-of-bounds access in writeback_store() Date: Sat, 17 Oct 2026 18:00:01 +0200 Message-ID: <20261017160505.260093984@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261017160431.119607952@linuxfoundation.org> References: <20261017160431.119607952@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Longlong Xia [ 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. Link: https://lore.kernel.org/20260804065919.3970386-1-xialonglong2025@163.com Link: https://lore.kernel.org/20260804065919.3970386-2-xialonglong2025@163.com Fixes: a939888ec38b ("zram: support idle/huge page writeback") Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Longlong Xia Reviewed-by: Sergey Senozhatsky Cc: Jens Axboe Cc: Minchan Kim Cc: Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- drivers/block/zram/zram_drv.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 966cb1842343c..451908dd2d675 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -627,7 +627,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; @@ -648,11 +648,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; } @@ -667,6 +665,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) { + ret = -EINVAL; + goto release_init_lock; + } + nr_pages = 1; + } + page = alloc_page(GFP_KERNEL); if (!page) { ret = -ENOMEM; -- 2.53.0