Skip to contentSkip to conversation

Writing through const-qualified pointer in next_arg() from recomp_algorithm_store()

DISCUSSION Alejandro Colomar 2 participants 2 messages Nov 25 – Nov 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi!

I'm working on preserving the const qualifier in some <linux/string.h> APIs. That uncovered some places where we are currently discarding the const qualifier.

Here's one of the cases I've found:

	$ grepc recomp_algorithm_store drivers/block/zram/zram_drv.c \
	| grep -e '^\S' -e '\<buf\>' -e '\<args\>';
	drivers/block/zram/zram_drv.c:static ssize_t recomp_algorithm_store(struct device *dev,
					      const char *buf,
	{
		char *args, *param, *val;
		args = skip_spaces(buf);
		while (*args) {
			args = next_arg(args, &param, &val);
	}

The function above takes 'buf' as a 'const char *', and then derives the 'args' pointer from it, but discards const (because skip_spaces() doesn't currently preserve const in the return type).

The non-const 'args' pointer is then passed to next_arg().

	$ git ls-files \
	| grep '\.[ch]$' \
	| xargs grepc next_arg \
	| grep -e '^\S' -e '\<args\>.* = ';
	include/linux/string.h:extern char *next_arg(char *args, char **param, char **val);
	lib/cmdline.c:char *next_arg(char *args, char **param, char **val)
	{
			args[equals] = '\0';
					args[i-1] = '\0';
			args[i-1] = '\0';
			args[i] = '\0';
	}

But next_arg() takes it as a non-const pointer, and actually writes to it. So, we're writing to a string to which we promised to not write (in the caller).

In other cases, the fix was trivial, by adding const in one or a few places, since we didn't write to it. But in this case, I don't know what's the appropriate fix. Should we make the 'buf' parameter of recomp_algorithm_store() non-const? Or is there a bug somewhere?

The same thing happens in a few more functions in the same file. Here's the full list of issues I've found in this file:

CC [M] drivers/block/zram/zram_drv.o
drivers/block/zram/zram_drv.c: In function ‘writeback_store’:
drivers/block/zram/zram_drv.c:1274:14: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
1274 | args = skip_spaces(buf);
| ^
drivers/block/zram/zram_drv.c: In function ‘algorithm_params_store’:
drivers/block/zram/zram_drv.c:1755:14: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
1755 | args = skip_spaces(buf);
| ^
drivers/block/zram/zram_drv.c: In function ‘recomp_algorithm_store’:
drivers/block/zram/zram_drv.c:1875:14: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
1875 | args = skip_spaces(buf);
| ^
drivers/block/zram/zram_drv.c: In function ‘recompress_store’:
drivers/block/zram/zram_drv.c:2571:14: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
2571 | args = skip_spaces(buf);
| ^

If we change those functions to take a non-const, it requires many changes in a few places, so I think it's better if someone more familiar with that code does that.

Please use

if you fix that.

Have a lovely night!
Alex

Signature · 1 line
<https://www.alejandro-colomar.es>
#1 lore raw

Hi,

Quoted 6 lines · Alejandro Colomar

I'm working on preserving the const qualifier in some <linux/string.h> APIs. That uncovered some places where we are currently discarding the const qualifier.

Here's one of the cases I've found:

Quoted 10 lines
	$ grepc recomp_algorithm_store drivers/block/zram/zram_drv.c \
	| grep -e '^\S' -e '\<buf\>' -e '\<args\>';
	drivers/block/zram/zram_drv.c:static ssize_t recomp_algorithm_store(struct device *dev,
					      const char *buf,
	{
		char *args, *param, *val;
		args = skip_spaces(buf);
		while (*args) {
			args = next_arg(args, &param, &val);
	}

kernfs_fop_write_iter() gives us a kmalloc(len + 1, GFP_KERNEL) copy of the user buffer, so it's "safe" to write to it, per se. But I get the correctness of the code point of view, so... probably another copy of the buffer? :/

---

drivers/block/zram/zram_drv.c +24 -4
index 96b135e3f727..ca12c1918aec 100644
@@ -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);
 

---

[..]

Quoted 3 lines

Should we make the 'buf' parameter of recomp_algorithm_store() non-const?

device attr store requires a const chat *.