All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] zram: do not modify cont sysfs store buffers
@ 2026-11-26  1:45 Sergey Senozhatsky
  2026-11-26  6:34 ` + zram-do-not-modify-cont-sysfs-store-buffers.patch added to mm-new branch Andrew Morton
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-26  1:45 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Minchan Kim, linux-kernel, linux-block, linux-mm,
	Sergey Senozhatsky, Alejandro Colomar

zram sysfs handlers that accept key=value arguments (e.g.
algorithm_params_store(), etc.) pass their const char *buf
parameter through skip_spaces(), discarding const, and then
mutate it in-place via next_arg().  This is not dangerous
per se because kernfs_fop_write_iter() passes a temp copy
of user string to zram, however it is still probably worth
fixing, just for the sake of correctness.

Allocate a temp non-const buffer for args tokenisation.

Link: https://lore.kernel.org/all/awcpkJD6K1xtx_Pe@devuan/
Reported-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
 drivers/block/zram/zram_drv.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 96b135e3f727..ca12c1918aec 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -1279,10 +1279,15 @@ static ssize_t writeback_store(struct device *dev,
 	unsigned long lo = 0, hi;
 	struct zram_pp_ctl *pp_ctl = NULL;
 	struct zram_wb_ctl *wb_ctl = NULL;
+	char *kbuf __free(kfree) = NULL;
 	char *args, *param, *val;
 	ssize_t ret = len;
 	int err, mode = 0;
 
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
 	guard(rwsem_write)(&zram->dev_lock);
 	if (!init_done(zram))
 		return -EINVAL;
@@ -1303,7 +1308,7 @@ static ssize_t writeback_store(struct device *dev,
 		goto out;
 	}
 
-	args = skip_spaces(buf);
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
@@ -1779,12 +1784,17 @@ static ssize_t algorithm_params_store(struct device *dev,
 	char *args, *param, *val, *algo = NULL, *dict_path = NULL;
 	struct deflate_params deflate_params;
 	struct zram *zram = dev_to_zram(dev);
+	char *kbuf __free(kfree) = NULL;
 	bool prio_param = false;
 	int ret;
 
 	deflate_params.winbits = ZCOMP_PARAM_NOT_SET;
 
-	args = skip_spaces(buf);
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
@@ -1899,12 +1909,17 @@ static ssize_t recomp_algorithm_store(struct device *dev,
 				      size_t len)
 {
 	struct zram *zram = dev_to_zram(dev);
+	char *kbuf __free(kfree) = NULL;
 	int prio = ZRAM_SECONDARY_COMP;
 	char *args, *param, *val;
 	char *alg = NULL;
 	int ret;
 
-	args = skip_spaces(buf);
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
@@ -2578,6 +2593,7 @@ static ssize_t recompress_store(struct device *dev,
 	struct zram *zram = dev_to_zram(dev);
 	char *args, *param, *val, *algo = NULL;
 	u64 num_recomp_pages = ULLONG_MAX;
+	char *kbuf __free(kfree) = NULL;
 	struct zram_pp_ctl *ctl = NULL;
 	s32 prio = ZRAM_SECONDARY_COMP;
 	u32 mode = 0, threshold = 0;
@@ -2586,7 +2602,11 @@ static ssize_t recompress_store(struct device *dev,
 	bool prio_param = false;
 	ssize_t ret;
 
-	args = skip_spaces(buf);
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* + zram-do-not-modify-cont-sysfs-store-buffers.patch added to mm-new branch
  2026-11-26  1:45 [PATCH] zram: do not modify cont sysfs store buffers Sergey Senozhatsky
@ 2026-11-26  6:34 ` Andrew Morton
  2026-11-26 11:24 ` [PATCH] zram: do not modify cont sysfs store buffers Alejandro Colomar
  2026-11-27  1:59 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-11-26  6:34 UTC (permalink / raw)
  To: mm-commits, minchan, alx, senozhatsky, akpm


The patch titled
     Subject: zram: do not modify cont sysfs store buffers
has been added to the -mm mm-new branch.  Its filename is
     zram-do-not-modify-cont-sysfs-store-buffers.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/zram-do-not-modify-cont-sysfs-store-buffers.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Sergey Senozhatsky <senozhatsky@chromium.org>
Subject: zram: do not modify cont sysfs store buffers
Date: Thu, 26 Nov 2026 10:45:07 +0900

zram sysfs handlers that accept key=value arguments (e.g. 
algorithm_params_store(), etc.) pass their const char *buf parameter
through skip_spaces(), discarding const, and then mutate it in-place via
next_arg().  This is not dangerous per se because kernfs_fop_write_iter()
passes a temp copy of user string to zram, however it is still probably
worth fixing, just for the sake of correctness.

Allocate a temp non-const buffer for args tokenisation.

Link: https://patch.msgid.link/20261126014518.1318583-1-senozhatsky@chromium.org
Link: https://lore.kernel.org/all/awcpkJD6K1xtx_Pe@devuan/
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: Alejandro Colomar <alx@kernel.org>
Cc: Minchan Kim <minchan@kernel.org>

 drivers/block/zram/zram_drv.c |   28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

--- a/drivers/block/zram/zram_drv.c~zram-do-not-modify-cont-sysfs-store-buffers
+++ a/drivers/block/zram/zram_drv.c
@@ -1247,10 +1247,15 @@ static ssize_t writeback_store(struct de
 	unsigned long lo = 0, hi;
 	struct zram_pp_ctl *pp_ctl = NULL;
 	struct zram_wb_ctl *wb_ctl = NULL;
+	char *kbuf __free(kfree) = NULL;
 	char *args, *param, *val;
 	ssize_t ret = len;
 	int err, mode = 0;
 
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
 	guard(rwsem_write)(&zram->dev_lock);
 	if (!init_done(zram))
 		return -EINVAL;
@@ -1271,7 +1276,7 @@ static ssize_t writeback_store(struct de
 		goto out;
 	}
 
-	args = skip_spaces(buf);
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
@@ -1747,12 +1752,17 @@ static ssize_t algorithm_params_store(st
 	char *args, *param, *val, *algo = NULL, *dict_path = NULL;
 	struct deflate_params deflate_params;
 	struct zram *zram = dev_to_zram(dev);
+	char *kbuf __free(kfree) = NULL;
 	bool prio_param = false;
 	int ret;
 
 	deflate_params.winbits = ZCOMP_PARAM_NOT_SET;
 
-	args = skip_spaces(buf);
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
@@ -1867,12 +1877,17 @@ static ssize_t recomp_algorithm_store(st
 				      size_t len)
 {
 	struct zram *zram = dev_to_zram(dev);
+	char *kbuf __free(kfree) = NULL;
 	int prio = ZRAM_SECONDARY_COMP;
 	char *args, *param, *val;
 	char *alg = NULL;
 	int ret;
 
-	args = skip_spaces(buf);
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
@@ -2560,6 +2575,7 @@ static ssize_t recompress_store(struct d
 	struct zram *zram = dev_to_zram(dev);
 	char *args, *param, *val, *algo = NULL;
 	u64 num_recomp_pages = ULLONG_MAX;
+	char *kbuf __free(kfree) = NULL;
 	struct zram_pp_ctl *ctl = NULL;
 	s32 prio = ZRAM_SECONDARY_COMP;
 	u32 mode = 0, threshold = 0;
@@ -2568,7 +2584,11 @@ static ssize_t recompress_store(struct d
 	bool prio_param = false;
 	ssize_t ret;
 
-	args = skip_spaces(buf);
+	kbuf = kstrndup(buf, len, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	args = skip_spaces(kbuf);
 	while (*args) {
 		args = next_arg(args, &param, &val);
 
_

Patches currently in -mm which might be from senozhatsky@chromium.org are

zram-prefix-backends-printk-s.patch
zram-remove-debugfs-entry-on-init-failure.patch
zram-reset-writeback-state-in-zram_reset_device.patch
zram-move-mark_slot_accessed-under-existing-slot-lock-sections.patch
zram-remove-stale-warn_on-from-zram_read_page.patch
zram-zstd-do-not-allocate-empty-c-d-dictionaries.patch
zram-lz4hc-pre-initialize-dictionary-stream-in-setup_params.patch
zram-lz4-use-lz4_decompress_safe_usingdict.patch
zram-lz4hc-use-lz4_decompress_safe_usingdict.patch
zram-zstd-do-not-use-kvzalloc-for-zstd-allocations.patch
zram-split-zcomp-into-separate-r-w-streams.patch
zram-introduce-shared-decompression-streams.patch
zram-allocate-bounce-buffer-for-spanning-objects.patch
zram-borrow-idle-decompression-streams-in-exclusive-mode.patch
zram-use-a-singleton-compression-context-for-recompression.patch
zram-drop-stale-comment-in-write_incompressible_page.patch
zram-drop-get_slot_handle-and-set_slot_handle.patch
zsmalloc-return-handle-from-obj_allocated.patch
zsmalloc-switch-handle-type-to-struct-zs_handle.patch
zram-check-mem_limit-before-allocating-zsmalloc-object.patch
zram-allocate-zsmalloc-object-under-slot-lock.patch
zswap-do-not-free-the-entry-if-its-pool-is-null.patch
zsmalloc-encode-1-based-obj_idx-so-valid-obj-is-never-0.patch
zsmalloc-embed-struct-zs_handle-into-zram-and-zswap-entries.patch
zsmalloc-fold-zs_obj_write-into-zs_store.patch
zsmalloc-populate-new-zspage-outside-class-lock-in-zs_store.patch
zram-fix-quadratic-writeback-bitmap-search.patch
zsmalloc-free-a-zspages-pages-in-one-batch.patch
zram-keep-the-hot-statistics-per-cpu.patch
zram-do-not-modify-cont-sysfs-store-buffers.patch
zsmalloc-fix-spdx-license-identifier.patch
zram-use-spdx-license-identifier.patch


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] zram: do not modify cont sysfs store buffers
  2026-11-26  1:45 [PATCH] zram: do not modify cont sysfs store buffers Sergey Senozhatsky
  2026-11-26  6:34 ` + zram-do-not-modify-cont-sysfs-store-buffers.patch added to mm-new branch Andrew Morton
@ 2026-11-26 11:24 ` Alejandro Colomar
  2026-11-27  1:59 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2026-11-26 11:24 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Andrew Morton, Minchan Kim, linux-kernel, linux-block, linux-mm

[-- Attachment #1: Type: text/plain, Size: 4049 bytes --]

Hi Sergey,

> Date: 2026-11-26 10:45:07+0900
> From: Sergey Senozhatsky <senozhatsky@chromium.org>
>
> zram sysfs handlers that accept key=value arguments (e.g.
> algorithm_params_store(), etc.) pass their const char *buf
> parameter through skip_spaces(), discarding const, and then
> mutate it in-place via next_arg().  This is not dangerous
> per se because kernfs_fop_write_iter() passes a temp copy
> of user string to zram, however it is still probably worth
> fixing, just for the sake of correctness.
> 
> Allocate a temp non-const buffer for args tokenisation.
> 
> Link: https://lore.kernel.org/all/awcpkJD6K1xtx_Pe@devuan/
> Reported-by: Alejandro Colomar <alx@kernel.org>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> ---
>  drivers/block/zram/zram_drv.c | 28 ++++++++++++++++++++++++----
>  1 file changed, 24 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index 96b135e3f727..ca12c1918aec 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c
> @@ -1279,10 +1279,15 @@ static ssize_t writeback_store(struct device *dev,
>  	unsigned long lo = 0, hi;
>  	struct zram_pp_ctl *pp_ctl = NULL;
>  	struct zram_wb_ctl *wb_ctl = NULL;
> +	char *kbuf __free(kfree) = NULL;
>  	char *args, *param, *val;
>  	ssize_t ret = len;
>  	int err, mode = 0;
>  
> +	kbuf = kstrndup(buf, len, GFP_KERNEL);
> +	if (!kbuf)
> +		return -ENOMEM;
> +

Thanks!  The fix sounds good to me.  While this could be a breaking,
since we're changing (or rather, not changing anymore) the string held
by the caller, I assume the caller didn't want it changed (since it was
const-qualified).  Thus,

Reviewed-by: Alejandro Colomar <alx@kernel.org>


Have a lovely day!
Alex

>  	guard(rwsem_write)(&zram->dev_lock);
>  	if (!init_done(zram))
>  		return -EINVAL;
> @@ -1303,7 +1308,7 @@ static ssize_t writeback_store(struct device *dev,
>  		goto out;
>  	}
>  
> -	args = skip_spaces(buf);
> +	args = skip_spaces(kbuf);
>  	while (*args) {
>  		args = next_arg(args, &param, &val);
>  
> @@ -1779,12 +1784,17 @@ static ssize_t algorithm_params_store(struct device *dev,
>  	char *args, *param, *val, *algo = NULL, *dict_path = NULL;
>  	struct deflate_params deflate_params;
>  	struct zram *zram = dev_to_zram(dev);
> +	char *kbuf __free(kfree) = NULL;
>  	bool prio_param = false;
>  	int ret;
>  
>  	deflate_params.winbits = ZCOMP_PARAM_NOT_SET;
>  
> -	args = skip_spaces(buf);
> +	kbuf = kstrndup(buf, len, GFP_KERNEL);
> +	if (!kbuf)
> +		return -ENOMEM;
> +
> +	args = skip_spaces(kbuf);
>  	while (*args) {
>  		args = next_arg(args, &param, &val);
>  
> @@ -1899,12 +1909,17 @@ static ssize_t recomp_algorithm_store(struct device *dev,
>  				      size_t len)
>  {
>  	struct zram *zram = dev_to_zram(dev);
> +	char *kbuf __free(kfree) = NULL;
>  	int prio = ZRAM_SECONDARY_COMP;
>  	char *args, *param, *val;
>  	char *alg = NULL;
>  	int ret;
>  
> -	args = skip_spaces(buf);
> +	kbuf = kstrndup(buf, len, GFP_KERNEL);
> +	if (!kbuf)
> +		return -ENOMEM;
> +
> +	args = skip_spaces(kbuf);
>  	while (*args) {
>  		args = next_arg(args, &param, &val);
>  
> @@ -2578,6 +2593,7 @@ static ssize_t recompress_store(struct device *dev,
>  	struct zram *zram = dev_to_zram(dev);
>  	char *args, *param, *val, *algo = NULL;
>  	u64 num_recomp_pages = ULLONG_MAX;
> +	char *kbuf __free(kfree) = NULL;
>  	struct zram_pp_ctl *ctl = NULL;
>  	s32 prio = ZRAM_SECONDARY_COMP;
>  	u32 mode = 0, threshold = 0;
> @@ -2586,7 +2602,11 @@ static ssize_t recompress_store(struct device *dev,
>  	bool prio_param = false;
>  	ssize_t ret;
>  
> -	args = skip_spaces(buf);
> +	kbuf = kstrndup(buf, len, GFP_KERNEL);
> +	if (!kbuf)
> +		return -ENOMEM;
> +
> +	args = skip_spaces(kbuf);
>  	while (*args) {
>  		args = next_arg(args, &param, &val);
>  
> -- 
> 2.56.1.839.g22a8290db9-goog
> 

-- 
<https://www.alejandro-colomar.es>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] zram: do not modify cont sysfs store buffers
  2026-11-26  1:45 [PATCH] zram: do not modify cont sysfs store buffers Sergey Senozhatsky
  2026-11-26  6:34 ` + zram-do-not-modify-cont-sysfs-store-buffers.patch added to mm-new branch Andrew Morton
  2026-11-26 11:24 ` [PATCH] zram: do not modify cont sysfs store buffers Alejandro Colomar
@ 2026-11-27  1:59 ` sashiko-bot
  2026-11-27  2:06   ` Sergey Senozhatsky
  2 siblings, 1 reply; 5+ messages in thread
From: sashiko-bot @ 2026-11-27  1:59 UTC (permalink / raw)
  To: Sergey Senozhatsky, Andrew Morton
  Cc: Minchan Kim, linux-mm, Alejandro Colomar

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] zram: separate declaration and initialization of __free(kfree)
  kbuf
--

commit be297424ad592ae67e98644a43d786e259a7383b
Author: Sergey Senozhatsky <senozhatsky@chromium.org>

zram: do not modify cont sysfs store buffers

This commit duplicates the const input buffer with kstrndup() in zram sysfs
store handlers before tokenizing key=value arguments in-place with
next_arg().

Link: https://lore.kernel.org/all/awcpkJD6K1xtx_Pe@devuan/

> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index d969f17b55afe..ff9e53f001bc2 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c
> @@ -1285,10 +1285,15 @@ static ssize_t writeback_store(struct device *dev,
>  	unsigned long lo = 0, hi;
>  	struct zram_pp_ctl *pp_ctl = NULL;
>  	struct zram_wb_ctl *wb_ctl = NULL;
> +	char *kbuf __free(kfree) = NULL;
>  	char *args, *param, *val;
>  	ssize_t ret = len;
>  	int err, mode = 0;
>  
> +	kbuf = kstrndup(buf, len, GFP_KERNEL);
> +	if (!kbuf)
> +		return -ENOMEM;

[Severity: Low]
This isn't a bug, but include/linux/cleanup.h recommends defining and
initializing __free() variables in a single statement rather than declaring
them "= NULL" at the top of the function and assigning them later.  In
writeback_store(), algorithm_params_store(), recomp_algorithm_store() and
recompress_store(), kbuf can be defined directly at the kstrndup() call:

	char *kbuf __free(kfree) = kstrndup(buf, len, GFP_KERNEL);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126014518.1318583-1-senozhatsky@chromium.org?part=1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] zram: do not modify cont sysfs store buffers
  2026-11-27  1:59 ` sashiko-bot
@ 2026-11-27  2:06   ` Sergey Senozhatsky
  0 siblings, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-27  2:06 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: Sergey Senozhatsky, Andrew Morton, Minchan Kim, linux-mm,
	Alejandro Colomar

On (26/11/27 01:59), sashiko-bot@kernel.org wrote:
> [Severity: Low]
> This isn't a bug, but include/linux/cleanup.h recommends defining and
> initializing __free() variables in a single statement rather than declaring
> them "= NULL" at the top of the function and assigning them later.

Yes, that's a recommendation.  I decided to follow the existing zram
code flow/style.  Like you said, this isn't a bug.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-27  2:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  1:45 [PATCH] zram: do not modify cont sysfs store buffers Sergey Senozhatsky
2026-11-26  6:34 ` + zram-do-not-modify-cont-sysfs-store-buffers.patch added to mm-new branch Andrew Morton
2026-11-26 11:24 ` [PATCH] zram: do not modify cont sysfs store buffers Alejandro Colomar
2026-11-27  1:59 ` sashiko-bot
2026-11-27  2:06   ` Sergey Senozhatsky

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.