From: koraynilay <koray.fra@gmail.com>
To: David Sterba <dsterba@suse.com>
Cc: Chris Mason <mason@kernel.org>, Qu Wenruo <wqu@suse.com>,
Zygo Blaxell <ce3g8jdj@umail.furryterror.org>,
linux-btrfs@vger.kernel.org, koraynilay <koray.fra@gmail.com>
Subject: [PATCH v6 RESEND 2/6] btrfs: also validate compression levels in btrfs_compress_is_valid_type()
Date: Sat, 26 Sep 2026 19:29:32 +0200 [thread overview]
Message-ID: <20260926172936.337085-3-koray.fra@gmail.com> (raw)
In-Reply-To: <20260926172936.337085-1-koray.fra@gmail.com>
Change btrfs_compress_is_valid_type() to use btrfs_match_compress_type()
and btrfs_compress_str2level() instead of a simple strncmp(), which would
validate even incorrect strings (e.g. "zstd:invalid" or "zstdinvalid").
This also makes the function validate levels in the same way the
compress= option gets parsed, allowing bigger or smaller values, but
still clamping them to the min or max supported.
Furthermore, remove the len parameter, since now it requires a
NUL-terminated string because of btrfs_compress_str2level(); this change
is fine because btrfs_compress_is_valid_type() is used only once in
props.c by prop_compression_validate(), which now uses a stack-based
buffer and memcpy() to convert the user-supplied xattr value to a
NUL-terminated string.
Assisted-by: Gemini:3.1-pro antigravity-cli-1.1.5
Signed-off-by: koraynilay <koray.fra@gmail.com>
---
fs/btrfs/compression.c | 18 +++++++++++++++---
fs/btrfs/compression.h | 2 +-
fs/btrfs/props.c | 18 +++++++++++++++++-
3 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 2880ee1f80d1..de10d96aee8a 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -70,18 +70,30 @@ static struct compressed_bio *alloc_compressed_bio(struct btrfs_inode *inode,
return to_compressed_bio(bbio);
}
-bool btrfs_compress_is_valid_type(const char *str, size_t len)
+/**
+ * btrfs_compress_is_valid_type - Check if a string is a valid compression type.
+ * @str: The compression string to check. Must be null-terminated.
+ *
+ * Return: %true if the string is a valid compression algorithm (optionally with
+ * a level suffix, e.g. "zstd" or "zstd:9"), %false otherwise.
+ */
+bool btrfs_compress_is_valid_type(const char *str)
{
+ size_t len = strlen(str);
int i;
for (i = 1; i < ARRAY_SIZE(btrfs_compress_types); i++) {
size_t comp_len = strlen(btrfs_compress_types[i]);
+ const char *comp_type = btrfs_compress_types[i];
+ int tmp_level;
if (len < comp_len)
continue;
- if (!strncmp(btrfs_compress_types[i], str, comp_len))
- return true;
+ if (btrfs_match_compress_type(str, comp_type, true)) {
+ if (btrfs_compress_str2level(i, str + comp_len, &tmp_level) == 0)
+ return true;
+ }
}
return false;
}
diff --git a/fs/btrfs/compression.h b/fs/btrfs/compression.h
index e67ba47b4cdc..c63bed9f4152 100644
--- a/fs/btrfs/compression.h
+++ b/fs/btrfs/compression.h
@@ -132,7 +132,7 @@ extern const struct btrfs_compress_levels btrfs_lzo_compress;
extern const struct btrfs_compress_levels btrfs_zstd_compress;
const char* btrfs_compress_type2str(enum btrfs_compression_type type);
-bool btrfs_compress_is_valid_type(const char *str, size_t len);
+bool btrfs_compress_is_valid_type(const char *str);
int btrfs_compress_heuristic(struct btrfs_inode *inode, u64 start, u64 end);
diff --git a/fs/btrfs/props.c b/fs/btrfs/props.c
index bb77d46376d4..2e706093e186 100644
--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -18,6 +18,15 @@
#include "super.h"
#include "dir-item.h"
+/*
+ * Max length of compression algorithm:level string.
+ *
+ * For now the longest possible string is "zstd:-15", which is
+ * 8 characters + 1 terminating null byte.
+ * Rounding it up to the closest power of 2 gives 16.
+ */
+#define BTRFS_COMPRESS_PROP_MAX_LEN 16
+
#define BTRFS_PROP_HANDLERS_HT_BITS 8
static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
@@ -295,13 +304,20 @@ int btrfs_load_inode_props(struct btrfs_inode *inode, struct btrfs_path *path)
static int prop_compression_validate(const struct btrfs_inode *inode,
const char *value, size_t len)
{
+ char value_str[BTRFS_COMPRESS_PROP_MAX_LEN];
+
if (!btrfs_inode_can_compress(inode))
return -EINVAL;
if (!value)
return 0;
- if (btrfs_compress_is_valid_type(value, len))
+ if (len >= BTRFS_COMPRESS_PROP_MAX_LEN)
+ return -EINVAL;
+
+ memcpy(value_str, value, len);
+ value_str[len] = '\0';
+ if (btrfs_compress_is_valid_type(value_str))
return 0;
if ((len == 2 && strncmp("no", value, 2) == 0) ||
--
2.55.0
next prev parent reply other threads:[~2026-09-26 17:30 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-26 17:29 [PATCH v6 RESEND 0/6] btrfs: add per-inode compression levels in xattrs koraynilay
2026-09-26 17:29 ` [PATCH v6 RESEND 1/6] btrfs: export btrfs_match_compress_type(), move it to compression.h koraynilay
2026-09-26 17:29 ` koraynilay [this message]
2026-09-26 17:29 ` [PATCH v6 RESEND 3/6] btrfs: add per-inode compression levels in xattrs koraynilay
2026-09-26 17:29 ` [PATCH v6 RESEND 4/6] btrfs: support inheritance for per-inode compression levels koraynilay
2026-09-26 17:29 ` [PATCH v6 RESEND 5/6] btrfs: add btrfs_compress_typelevel2str() helper koraynilay
2026-09-26 17:29 ` [PATCH v6 RESEND 6/6] btrfs: preserve btrfs.compression when setting inode flags koraynilay
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=20260926172936.337085-3-koray.fra@gmail.com \
--to=koray.fra@gmail.com \
--cc=ce3g8jdj@umail.furryterror.org \
--cc=dsterba@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=mason@kernel.org \
--cc=wqu@suse.com \
/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