[PATCH 5/5] block: blk-sysfs: match the write_cache strings exactly
From: shao.mingyin
Date: Thu Dec 03 2026 - 09:17:24 EST
From: Shao Mingyin <shao.mingyin@xxxxxxxxxx>
queue_wc_store() matches the write_cache settings with strncmp() and a
fixed length, so any value that starts with "write back", "write
through" or "none" is accepted. For example, "write backdoor" enables
the write cache even though it is not a valid setting.
Use sysfs_streq() so that only the documented strings are accepted.
Fixes: 93e9d8e836cb ("block: add ability to flag write back caching on a device")
Signed-off-by: Shao Mingyin <shao.mingyin@xxxxxxxxxx>
---
Tested on 7.4.0-rc5-next-20261130 (QEMU/TCG guest) with a
memory-backed null_blk device with an 8 MiB cache. Writing "none"
disables the write cache; a following write of "write backdoor" was
accepted as "write back":
unpatched patched
write "write backdoor" accepted, write "write backdoor" rejected
cache re-enabled (write back) (-EINVAL), stays write through
block/blk-sysfs.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c
index 22a536c9c77e..48d866e425e6 100644
--- a/block/blk-sysfs.c
+++ b/block/blk-sysfs.c
@@ -571,10 +571,10 @@ static int queue_wc_store(struct gendisk *disk, const char *page,
{
bool disable;
- if (!strncmp(page, "write back", 10)) {
+ if (sysfs_streq(page, "write back")) {
disable = false;
- } else if (!strncmp(page, "write through", 13) ||
- !strncmp(page, "none", 4)) {
+ } else if (sysfs_streq(page, "write through") ||
+ sysfs_streq(page, "none")) {
disable = true;
} else {
return -EINVAL;
--
2.34.1