From: Alejandro Colomar <alx@kernel.org>
To: linux-kernel@vger.kernel.org, Minchan Kim <minchan@kernel.org>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-hardening@vger.kernel.org
Subject: Writing through const-qualified pointer in next_arg() from recomp_algorithm_store()
Date: Wed, 25 Nov 2026 20:08:31 +0100 [thread overview]
Message-ID: <awcpkJD6K1xtx_Pe@devuan> (raw)
[-- Attachment #1: Type: text/plain, Size: 3266 bytes --]
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, ¶m, &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
Reported-by: Alejandro Colomar <alx@kernel.org>
if you fix that.
Have a lovely night!
Alex
--
<https://www.alejandro-colomar.es>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
reply other threads:[~2026-11-25 19:08 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awcpkJD6K1xtx_Pe@devuan \
--to=alx@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=minchan@kernel.org \
--cc=senozhatsky@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®