The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] zram: do not modify cont sysfs store buffers
@ 2026-11-26  1:45 Sergey Senozhatsky
  2026-11-26 11:24 ` Alejandro Colomar
  0 siblings, 1 reply; 2+ 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] 2+ 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 11:24 ` Alejandro Colomar
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-11-26 11:24 UTC | newest]

Thread overview: 2+ 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 11:24 ` Alejandro Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox