Skip to contentSkip to conversation

btrfs: harden parsing of compress mount options

PATCH In review Daniel Vacek 2 participants 8 messages Jun 2, 2025 – Jun 12, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

v3 cover btrfs: harden parsing of compress mount options

This series hardens the compress mount option parsing.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

v2: Drop useless check for comma and split compress options
into a separate helper function

Daniel Vacek (2):
btrfs: factor out compress mount options parsing
btrfs: harden parsing of compress mount options

 fs/btrfs/super.c | 112 ++++++++++++++++++++++++++++-------------------
 1 file changed, 66 insertions(+), 46 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 1/2 btrfs: factor out compress mount options parsing

There are many options making the parsing a bit lenghty. Factor the compress options out into a helper function. The next patch is going to harden this function.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

 fs/btrfs/super.c | 100 +++++++++++++++++++++++++----------------------
 1 file changed, 54 insertions(+), 46 deletions(-)
fs/btrfs/super.c +54 -46
index 40709e2a44fce..6291ab45ab2a5 100644
@@ -270,6 +270,59 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
 	return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
 }
 
+static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
+				struct fs_parameter *param, int opt)
+{
+	/*
+	 * Provide the same semantics as older kernels that don't use fs
+	 * context, specifying the "compress" option clears
+	 * "force-compress" without the need to pass
+	 * "compress-force=[no|none]" before specifying "compress".
+	 */
+	if (opt != Opt_compress_force && opt != Opt_compress_force_type)
+		btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
+
+	if (opt == Opt_compress || opt == Opt_compress_force) {
+		ctx->compress_type = BTRFS_COMPRESS_ZLIB;
+		ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "zlib", 4) == 0) {
+		ctx->compress_type = BTRFS_COMPRESS_ZLIB;
+		ctx->compress_level =
+			btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
+						 param->string + 4);
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "lzo", 3) == 0) {
+		ctx->compress_type = BTRFS_COMPRESS_LZO;
+		ctx->compress_level = 0;
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "zstd", 4) == 0) {
+		ctx->compress_type = BTRFS_COMPRESS_ZSTD;
+		ctx->compress_level =
+			btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
+						 param->string + 4);
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "no", 2) == 0) {
+		ctx->compress_level = 0;
+		ctx->compress_type = 0;
+		btrfs_clear_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
+	} else {
+		btrfs_err(NULL, "unrecognized compression value %s",
+			  param->string);
+		return -EINVAL;
+	}
+	return 0;
+}
+
 static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
 {
 	struct btrfs_fs_context *ctx = fc->fs_private;
@@ -339,53 +392,8 @@ static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
 		fallthrough;
 	case Opt_compress:
 	case Opt_compress_type:
-		/*
-		 * Provide the same semantics as older kernels that don't use fs
-		 * context, specifying the "compress" option clears
-		 * "force-compress" without the need to pass
-		 * "compress-force=[no|none]" before specifying "compress".
-		 */
-		if (opt != Opt_compress_force && opt != Opt_compress_force_type)
-			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
-
-		if (opt == Opt_compress || opt == Opt_compress_force) {
-			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
-			ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "zlib", 4) == 0) {
-			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
-			ctx->compress_level =
-				btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
-							 param->string + 4);
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "lzo", 3) == 0) {
-			ctx->compress_type = BTRFS_COMPRESS_LZO;
-			ctx->compress_level = 0;
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "zstd", 4) == 0) {
-			ctx->compress_type = BTRFS_COMPRESS_ZSTD;
-			ctx->compress_level =
-				btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
-							 param->string + 4);
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "no", 2) == 0) {
-			ctx->compress_level = 0;
-			ctx->compress_type = 0;
-			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
-		} else {
-			btrfs_err(NULL, "unrecognized compression value %s",
-				  param->string);
+		if (btrfs_parse_compress(ctx, param, opt))
 			return -EINVAL;
-		}
 		break;
 	case Opt_ssd:
 		if (result.negated) {
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH v3 1/2] btrfs: factor out compress mount options parsing

Quoted 17 lines · Daniel Vacek

There are many options making the parsing a bit lenghty. Factor the compress options out into a helper function. The next patch is going to harden this function.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

 fs/btrfs/super.c | 100 +++++++++++++++++++++++++----------------------
 1 file changed, 54 insertions(+), 46 deletions(-)
fs/btrfs/super.c +0 -0
index 40709e2a44fce..6291ab45ab2a5 100644

fs/btrfs/super.c static inline blk_mode_t btrfs_open_mode(struct fs_context *fc) · line 274

Quoted 5 lines
fs/btrfs/super.c +2 -0
@@ -270,6 +270,59 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
 	return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
 }
 
+static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
+				struct fs_parameter *param, int opt)

parame can be const

Quoted 112 lines
fs/btrfs/super.c +52 -46
+{
+	/*
+	 * Provide the same semantics as older kernels that don't use fs
+	 * context, specifying the "compress" option clears
+	 * "force-compress" without the need to pass
+	 * "compress-force=[no|none]" before specifying "compress".
+	 */
+	if (opt != Opt_compress_force && opt != Opt_compress_force_type)
+		btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
+
+	if (opt == Opt_compress || opt == Opt_compress_force) {
+		ctx->compress_type = BTRFS_COMPRESS_ZLIB;
+		ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "zlib", 4) == 0) {
+		ctx->compress_type = BTRFS_COMPRESS_ZLIB;
+		ctx->compress_level =
+			btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
+						 param->string + 4);
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "lzo", 3) == 0) {
+		ctx->compress_type = BTRFS_COMPRESS_LZO;
+		ctx->compress_level = 0;
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "zstd", 4) == 0) {
+		ctx->compress_type = BTRFS_COMPRESS_ZSTD;
+		ctx->compress_level =
+			btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
+						 param->string + 4);
+		btrfs_set_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
+		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
+	} else if (strncmp(param->string, "no", 2) == 0) {
+		ctx->compress_level = 0;
+		ctx->compress_type = 0;
+		btrfs_clear_opt(ctx->mount_opt, COMPRESS);
+		btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
+	} else {
+		btrfs_err(NULL, "unrecognized compression value %s",
+			  param->string);
+		return -EINVAL;
+	}
+	return 0;
+}
+
 static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
 {
 	struct btrfs_fs_context *ctx = fc->fs_private;
@@ -339,53 +392,8 @@ static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
 		fallthrough;
 	case Opt_compress:
 	case Opt_compress_type:
-		/*
-		 * Provide the same semantics as older kernels that don't use fs
-		 * context, specifying the "compress" option clears
-		 * "force-compress" without the need to pass
-		 * "compress-force=[no|none]" before specifying "compress".
-		 */
-		if (opt != Opt_compress_force && opt != Opt_compress_force_type)
-			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
-
-		if (opt == Opt_compress || opt == Opt_compress_force) {
-			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
-			ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "zlib", 4) == 0) {
-			ctx->compress_type = BTRFS_COMPRESS_ZLIB;
-			ctx->compress_level =
-				btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
-							 param->string + 4);
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "lzo", 3) == 0) {
-			ctx->compress_type = BTRFS_COMPRESS_LZO;
-			ctx->compress_level = 0;
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "zstd", 4) == 0) {
-			ctx->compress_type = BTRFS_COMPRESS_ZSTD;
-			ctx->compress_level =
-				btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
-							 param->string + 4);
-			btrfs_set_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, NODATACOW);
-			btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-		} else if (strncmp(param->string, "no", 2) == 0) {
-			ctx->compress_level = 0;
-			ctx->compress_type = 0;
-			btrfs_clear_opt(ctx->mount_opt, COMPRESS);
-			btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
-		} else {
-			btrfs_err(NULL, "unrecognized compression value %s",
-				  param->string);
+		if (btrfs_parse_compress(ctx, param, opt))
 			return -EINVAL;
-		}
 		break;
 	case Opt_ssd:
 		if (result.negated) {
Signature · 1 line
2.47.2
#3 lore raw

PATCH v3 2/2 btrfs: harden parsing of compress mount options

In reply to Daniel Vacek

Btrfs happily but incorrectly accepts the -o compress=zlib+foo and similar options with any random suffix.

Fix that by explicitly checking the end of the strings.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

 fs/btrfs/super.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
fs/btrfs/super.c +19 -7
index 6291ab45ab2a5..4510c5f7a785e 100644
@@ -270,9 +270,20 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
 	return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
 }
 
+static bool btrfs_match_compress_type(char *string, char *type, bool may_have_level)
+{
+	int len = strlen(type);
+
+	return strncmp(string, type, len) == 0 &&
+		((may_have_level && string[len] == ':') ||
+				    string[len] == '\0');
+}
+
 static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
 				struct fs_parameter *param, int opt)
 {
+	char *string = param->string;
+
 	/*
 	 * Provide the same semantics as older kernels that don't use fs
 	 * context, specifying the "compress" option clears
@@ -288,36 +299,37 @@ static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
 		btrfs_set_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-	} else if (strncmp(param->string, "zlib", 4) == 0) {
+	} else if (btrfs_match_compress_type(string, "zlib", true)) {
 		ctx->compress_type = BTRFS_COMPRESS_ZLIB;
 		ctx->compress_level =
 			btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
-						 param->string + 4);
+						 string + 4);
 		btrfs_set_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-	} else if (strncmp(param->string, "lzo", 3) == 0) {
+	} else if (btrfs_match_compress_type(string, "lzo", false)) {
 		ctx->compress_type = BTRFS_COMPRESS_LZO;
 		ctx->compress_level = 0;
 		btrfs_set_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-	} else if (strncmp(param->string, "zstd", 4) == 0) {
+	} else if (btrfs_match_compress_type(string, "zstd", true)) {
 		ctx->compress_type = BTRFS_COMPRESS_ZSTD;
 		ctx->compress_level =
 			btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
-						 param->string + 4);
+						 string + 4);
 		btrfs_set_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, NODATACOW);
 		btrfs_clear_opt(ctx->mount_opt, NODATASUM);
-	} else if (strncmp(param->string, "no", 2) == 0) {
+	} else if (btrfs_match_compress_type(string, "no", false) ||
+		   btrfs_match_compress_type(string, "none", false)) {
 		ctx->compress_level = 0;
 		ctx->compress_type = 0;
 		btrfs_clear_opt(ctx->mount_opt, COMPRESS);
 		btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
 	} else {
 		btrfs_err(NULL, "unrecognized compression value %s",
-			  param->string);
+			  string);
 		return -EINVAL;
 	}
 	return 0;
Signature · 1 line
2.47.2
#4 lore raw
Quoted 18 lines · Daniel Vacek

Btrfs happily but incorrectly accepts the -o compress=zlib+foo and similar options with any random suffix.

Fix that by explicitly checking the end of the strings.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

 fs/btrfs/super.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
fs/btrfs/super.c +0 -0
index 6291ab45ab2a5..4510c5f7a785e 100644

fs/btrfs/super.c static inline blk_mode_t btrfs_open_mode(struct fs_context *fc) · line 273

Quoted 4 lines
fs/btrfs/super.c +1 -0
@@ -270,9 +270,20 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
 	return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
 }
 
+static bool btrfs_match_compress_type(char *string, char *type, bool may_have_level)

const also here, string, type

fs/btrfs/super.c static inline blk_mode_t btrfs_open_mode(struct fs_context *fc) · line 285

Quoted 12 lines
fs/btrfs/super.c +9 -0
+{
+	int len = strlen(type);
+
+	return strncmp(string, type, len) == 0 &&
+		((may_have_level && string[len] == ':') ||
+				    string[len] == '\0');
+}
+
 static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
 				struct fs_parameter *param, int opt)
 {
+	char *string = param->string;

and here

Quoted 4 lines
fs/btrfs/super.c +1 -0
+
 	/*
 	 * Provide the same semantics as older kernels that don't use fs
 	 * context, specifying the "compress" option clears
#5 lore raw
Quoted 27 lines · David Sterba
Quoted 18 lines · Daniel Vacek

Btrfs happily but incorrectly accepts the -o compress=zlib+foo and similar options with any random suffix.

Fix that by explicitly checking the end of the strings.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

 fs/btrfs/super.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
fs/btrfs/super.c +0 -0
index 6291ab45ab2a5..4510c5f7a785e 100644

fs/btrfs/super.c static inline blk_mode_t btrfs_open_mode(struct fs_context *fc) · line 273

Quoted 4 lines
fs/btrfs/super.c +1 -0
@@ -270,9 +270,20 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
      return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
 }
 
+static bool btrfs_match_compress_type(char *string, char *type, bool may_have_level)

const also here, string, type

Quoted 14 lines

fs/btrfs/super.c static inline blk_mode_t btrfs_open_mode(struct fs_context *fc) · line 285

Quoted 12 lines
fs/btrfs/super.c +9 -0
+{
+     int len = strlen(type);
+
+     return strncmp(string, type, len) == 0 &&
+             ((may_have_level && string[len] == ':') ||
+                                 string[len] == '\0');
+}
+
 static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
                              struct fs_parameter *param, int opt)
 {
+     char *string = param->string;

and here

Can be done at merge time. Or do you want a re-send?

Quoted 4 lines
Quoted 4 lines
fs/btrfs/super.c +1 -0
+
      /*
       * Provide the same semantics as older kernels that don't use fs
       * context, specifying the "compress" option clears
#6 lore raw
Quoted 43 lines · Daniel Vacek
Quoted 27 lines · David Sterba
Quoted 18 lines · Daniel Vacek

Btrfs happily but incorrectly accepts the -o compress=zlib+foo and similar options with any random suffix.

Fix that by explicitly checking the end of the strings.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

 fs/btrfs/super.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
fs/btrfs/super.c +0 -0
index 6291ab45ab2a5..4510c5f7a785e 100644

fs/btrfs/super.c static inline blk_mode_t btrfs_open_mode(struct fs_context *fc) · line 273

Quoted 4 lines
fs/btrfs/super.c +1 -0
@@ -270,9 +270,20 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
      return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
 }
 
+static bool btrfs_match_compress_type(char *string, char *type, bool may_have_level)

const also here, string, type

Quoted 14 lines

fs/btrfs/super.c static inline blk_mode_t btrfs_open_mode(struct fs_context *fc) · line 285

Quoted 12 lines
fs/btrfs/super.c +9 -0
+{
+     int len = strlen(type);
+
+     return strncmp(string, type, len) == 0 &&
+             ((may_have_level && string[len] == ':') ||
+                                 string[len] == '\0');
+}
+
 static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
                              struct fs_parameter *param, int opt)
 {
+     char *string = param->string;

and here

Quoted 1 line

Can be done at merge time. Or do you want a re-send?

No resend needed, I updated the patch in for-next. This was to let you know so that I don't need to fix it in future patches.

#7 lore raw
In reply to Daniel Vacek
Quoted 5 lines · Daniel Vacek

This series hardens the compress mount option parsing.

---
v3 changes: Split into two patches to ease backporting,
no functional changes.

This is much better, thanks.