Skip to contentSkip to conversation

btrfs: reject invalid compression level

PATCH In review Qu Wenruo 3 participants 3 messages Aug 25, 2025 – Sep 18, 2025 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: reject invalid compression level

Inspired by recent changes to compression level parsing by Calvin Owens, it turns out that we do not do any extra validation for compression level, thus allowing things like "compress=lzo:invalid" to be accepted without extra warning or whatever.

Although we accept levels that are beyond the supported algorithm ranges, accepting completely invalid levels are beyond sanity.

Fix the too loose checks for compression level, by doing proper error handling of kstrtoint(), so that we will reject not only too large values (beyond int range) but also completely insane levels like "lzo:invalid".

---
 fs/btrfs/compression.c | 21 +++++++++++++--------
 fs/btrfs/compression.h |  2 +-
 fs/btrfs/super.c       | 27 +++++++++++++++++++--------
 3 files changed, 33 insertions(+), 17 deletions(-)
fs/btrfs/compression.c +13 -8
index 068339e86123..d13a66f7b4ac 100644
@@ -1652,24 +1652,29 @@ int btrfs_compress_heuristic(struct btrfs_inode *inode, u64 start, u64 end)
 
 /*
  * Convert the compression suffix (eg. after "zlib" starting with ":") to
- * level, unrecognized string will set the default level. Negative level
- * numbers are allowed.
+ * level.
+ *
+ * If the resulted level exceed the algo's supported levels, it will be clamped.
+ *
+ * Return <0 if no valid string can be found.
+ * Return 0 if everything is fine.
  */
-int btrfs_compress_str2level(unsigned int type, const char *str)
+int btrfs_compress_str2level(unsigned int type, const char *str, int *level_ret)
 {
 	int level = 0;
 	int ret;
 
-	if (!type)
+	if (!type) {
+		*level_ret = btrfs_compress_set_level(type, level);
 		return 0;
+	}
 
 	if (str[0] == ':') {
 		ret = kstrtoint(str + 1, 10, &level);
 		if (ret)
-			level = 0;
+			return ret;
 	}
 
-	level = btrfs_compress_set_level(type, level);
-
-	return level;
+	*level_ret = btrfs_compress_set_level(type, level);
+	return 0;
 }
fs/btrfs/compression.h +1 -1
index 760d4aac74e6..243771ae7d64 100644
@@ -110,7 +110,7 @@ void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,
 				   bool writeback);
 void btrfs_submit_compressed_read(struct btrfs_bio *bbio);
 
-int btrfs_compress_str2level(unsigned int type, const char *str);
+int btrfs_compress_str2level(unsigned int type, const char *str, int *level_ret);
 
 struct folio *btrfs_alloc_compr_folio(void);
 void btrfs_free_compr_folio(struct folio *folio);
fs/btrfs/super.c +19 -8
index b607c606fcfe..fb1e449ef26a 100644
@@ -274,6 +274,7 @@ static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
 				const struct fs_parameter *param, int opt)
 {
 	const char *string = param->string;
+	int ret;
 
 	/*
 	 * Provide the same semantics as older kernels that don't use fs
@@ -292,15 +293,19 @@ static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
 	} else if (btrfs_match_compress_type(string, "zlib", true)) {
 		ctx->compress_type = BTRFS_COMPRESS_ZLIB;
-		ctx->compress_level = btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
-							       string + 4);
+		ret = btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB, string + 4,
+					       &ctx->compress_level);
+		if (ret < 0)
+			goto error;
 		btrfs_set_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
 	} else if (btrfs_match_compress_type(string, "lzo", true)) {
 		ctx->compress_type = BTRFS_COMPRESS_LZO;
-		ctx->compress_level = btrfs_compress_str2level(BTRFS_COMPRESS_LZO,
-							       string + 3);
+		ret = btrfs_compress_str2level(BTRFS_COMPRESS_LZO, string + 3,
+					       &ctx->compress_level);
+		if (ret < 0)
+			goto error;
 		if (string[3] == ':' && string[4])
 			btrfs_warn(NULL, "Compression level ignored for LZO");
 		btrfs_set_opt(ctx->mount_opt, COMPRESS);
@@ -308,8 +313,10 @@ static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
 	} else if (btrfs_match_compress_type(string, "zstd", true)) {
 		ctx->compress_type = BTRFS_COMPRESS_ZSTD;
-		ctx->compress_level = btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
-							       string + 4);
+		ret = btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD, string + 4,
+					       &ctx->compress_level);
+		if (ret < 0)
+			goto error;
 		btrfs_set_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
@@ -320,10 +327,14 @@ static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
 		btrfs_clear_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
 	} else {
-		btrfs_err(NULL, "unrecognized compression value %s", string);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto error;
 	}
 	return 0;
+error:
+	btrfs_err(NULL, "failed to parse compression option '%s': %d", string, ret);
+	return ret;
+
 }
 
 static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Qu Wenruo

Inspired by recent changes to compression level parsing by Calvin Owens, it turns out that we do not do any extra validation for compression level, thus allowing things like "compress=lzo:invalid" to be accepted without extra warning or whatever.

Although we accept levels that are beyond the supported algorithm ranges, accepting completely invalid levels are beyond sanity.

Quoted 6 lines

Fix the too loose checks for compression level, by doing proper error handling of kstrtoint(), so that we will reject not only too large values (beyond int range) but also completely insane levels like "lzo:invalid".

fs/btrfs/super.c static int btrfs_parse_compress(struct btrfs_fs_context *ctx, · line 335

Quoted 1 line
fs/btrfs/super.c +1 -0
+	btrfs_err(NULL, "failed to parse compression option '%s': %d", string, ret);

I don't see how printing the %d/ret improves the error message, there will be probably ERANGE or EINVAL.

#2 lore raw
Quoted 14 lines · David Sterba
Quoted 13 lines · Qu Wenruo

Inspired by recent changes to compression level parsing by Calvin Owens, it turns out that we do not do any extra validation for compression level, thus allowing things like "compress=lzo:invalid" to be accepted without extra warning or whatever.

Although we accept levels that are beyond the supported algorithm ranges, accepting completely invalid levels are beyond sanity.

Fix the too loose checks for compression level, by doing proper error handling of kstrtoint(), so that we will reject not only too large values (beyond int range) but also completely insane levels like "lzo:invalid".

Quoted 9 lines
Quoted 1 line

fs/btrfs/super.c static int btrfs_parse_compress(struct btrfs_fs_context *ctx, · line 335

Quoted 1 line
fs/btrfs/super.c +1 -0
+	btrfs_err(NULL, "failed to parse compression option '%s': %d", string, ret);

I don't see how printing the %d/ret improves the error message, there will be probably ERANGE or EINVAL.

Pushed with that ret part dropped.

Thanks,
Qu