]> git.hungrycats.org Git - linux/commitdiff
btrfs: props: reject NUL bytes in btrfs.compression values
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 23:41:30 +0000 (18:41 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 23:41:30 +0000 (18:41 -0500)
The btrfs.compression xattr value is length-delimited, and
prop_compression_validate() copies it into a NUL-terminated buffer before
btrfs_compress_is_valid_type() parses it.  An embedded NUL byte ends the
string early in the copy, so a value such as "zstd:3" followed by a NUL
and arbitrary junk passes validation and is stored verbatim, junk and
all, where getxattr returns it.

No algorithm name or level contains a NUL byte, so reject such values
before the copy.  "no" and "none" were already compared with an exact
length and are not affected.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/props.c

index adc4dc540840ceb9f0f11a962b7d91384ef3ba96..0fabd64cffd2be57c4e59ae6cd1794f6c9c8e5dd 100644 (file)
@@ -303,6 +303,14 @@ static int prop_compression_validate(const struct btrfs_inode *inode,
        if (len >= BTRFS_COMPRESS_PROP_MAX_LEN)
                return -EINVAL;
 
+       /*
+        * The value is length-delimited and may contain NUL bytes, which the
+        * NUL-terminated copy would hide from the parser:  "zstd:3" followed
+        * by a NUL and junk would pass and be stored verbatim.
+        */
+       if (memchr(value, '\0', len))
+               return -EINVAL;
+
        memcpy(value_str, value, len);
        value_str[len] = '\0';
        if (btrfs_compress_is_valid_type(value_str)) {