]> git.hungrycats.org Git - linux/commitdiff
btrfs: also validate compression levels in btrfs_compress_is_valid_type()
authorkoraynilay <koray.fra@gmail.com>
Tue, 3 Nov 2026 19:47:30 +0000 (20:47 +0100)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 23:39:27 +0000 (18:39 -0500)
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>
Link: https://lore.kernel.org/r/20261103194734.598182-3-koray.fra@gmail.com
fs/btrfs/compression.c
fs/btrfs/compression.h
fs/btrfs/props.c

index 9f0e42424e865ca4eaa4b27287a485e273ccebe7..b1f8323fffe7d00ec10017dbb0fa388b7e7f7a33 100644 (file)
@@ -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;
 }
index e67ba47b4cdc5e85cba69a2e016234f11fcc3022..c63bed9f4152ed673fd218b76c0efa2ee897fb82 100644 (file)
@@ -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);
 
index 7cc82366a1dfc5391024c3ed3ac489711035e91c..d196b73f4ffa13d1bda018a43483767e24487daf 100644 (file)
 #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) ||