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
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)) {