linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] btrfs: harden parsing of compress mount options
@ 2025-06-02 15:53 Daniel Vacek
  2025-06-02 15:53 ` [PATCH v3 1/2] btrfs: factor out compress mount options parsing Daniel Vacek
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Daniel Vacek @ 2025-06-02 15:53 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Nick Terrell
  Cc: linux-btrfs, linux-kernel, Daniel Vacek

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

-- 
2.47.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3 1/2] btrfs: factor out compress mount options parsing
  2025-06-02 15:53 [PATCH v3 0/2] btrfs: harden parsing of compress mount options Daniel Vacek
@ 2025-06-02 15:53 ` Daniel Vacek
  2025-06-02 17:28   ` David Sterba
  2025-06-02 15:53 ` [PATCH v3 2/2] btrfs: harden parsing of compress mount options Daniel Vacek
  2025-06-02 17:28 ` [PATCH v3 0/2] " David Sterba
  2 siblings, 1 reply; 8+ messages in thread
From: Daniel Vacek @ 2025-06-02 15:53 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Nick Terrell
  Cc: linux-btrfs, linux-kernel, 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.

Signed-off-by: Daniel Vacek <neelx@suse.com>
---
v3 changes: Split into two patches to ease backporting,
            no functional changes.

 fs/btrfs/super.c | 100 +++++++++++++++++++++++++----------------------
 1 file changed, 54 insertions(+), 46 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 40709e2a44fce..6291ab45ab2a5 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -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) {
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v3 2/2] btrfs: harden parsing of compress mount options
  2025-06-02 15:53 [PATCH v3 0/2] btrfs: harden parsing of compress mount options Daniel Vacek
  2025-06-02 15:53 ` [PATCH v3 1/2] btrfs: factor out compress mount options parsing Daniel Vacek
@ 2025-06-02 15:53 ` Daniel Vacek
  2025-06-02 17:29   ` David Sterba
  2025-06-02 17:28 ` [PATCH v3 0/2] " David Sterba
  2 siblings, 1 reply; 8+ messages in thread
From: Daniel Vacek @ 2025-06-02 15:53 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Nick Terrell
  Cc: linux-btrfs, linux-kernel, 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.

Signed-off-by: Daniel Vacek <neelx@suse.com>
---
v3 changes: Split into two patches to ease backporting,
            no functional changes.

 fs/btrfs/super.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 6291ab45ab2a5..4510c5f7a785e 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -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;
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 0/2] btrfs: harden parsing of compress mount options
  2025-06-02 15:53 [PATCH v3 0/2] btrfs: harden parsing of compress mount options Daniel Vacek
  2025-06-02 15:53 ` [PATCH v3 1/2] btrfs: factor out compress mount options parsing Daniel Vacek
  2025-06-02 15:53 ` [PATCH v3 2/2] btrfs: harden parsing of compress mount options Daniel Vacek
@ 2025-06-02 17:28 ` David Sterba
  2 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2025-06-02 17:28 UTC (permalink / raw)
  To: Daniel Vacek
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, linux-btrfs,
	linux-kernel

On Mon, Jun 02, 2025 at 05:53:17PM +0200, Daniel Vacek wrote:
> 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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/2] btrfs: factor out compress mount options parsing
  2025-06-02 15:53 ` [PATCH v3 1/2] btrfs: factor out compress mount options parsing Daniel Vacek
@ 2025-06-02 17:28   ` David Sterba
  0 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2025-06-02 17:28 UTC (permalink / raw)
  To: Daniel Vacek
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, linux-btrfs,
	linux-kernel

On Mon, Jun 02, 2025 at 05:53:18PM +0200, Daniel Vacek wrote:
> 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.
> 
> Signed-off-by: Daniel Vacek <neelx@suse.com>
> ---
> v3 changes: Split into two patches to ease backporting,
>             no functional changes.
> 
>  fs/btrfs/super.c | 100 +++++++++++++++++++++++++----------------------
>  1 file changed, 54 insertions(+), 46 deletions(-)
> 
> diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> index 40709e2a44fce..6291ab45ab2a5 100644
> --- a/fs/btrfs/super.c
> +++ b/fs/btrfs/super.c
> @@ -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

> +{
> +	/*
> +	 * 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) {
> -- 
> 2.47.2
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 2/2] btrfs: harden parsing of compress mount options
  2025-06-02 15:53 ` [PATCH v3 2/2] btrfs: harden parsing of compress mount options Daniel Vacek
@ 2025-06-02 17:29   ` David Sterba
  2025-06-12 13:29     ` Daniel Vacek
  0 siblings, 1 reply; 8+ messages in thread
From: David Sterba @ 2025-06-02 17:29 UTC (permalink / raw)
  To: Daniel Vacek
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, linux-btrfs,
	linux-kernel

On Mon, Jun 02, 2025 at 05:53:19PM +0200, Daniel Vacek wrote:
> 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.
> 
> Signed-off-by: Daniel Vacek <neelx@suse.com>
> ---
> v3 changes: Split into two patches to ease backporting,
>             no functional changes.
> 
>  fs/btrfs/super.c | 26 +++++++++++++++++++-------
>  1 file changed, 19 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> index 6291ab45ab2a5..4510c5f7a785e 100644
> --- a/fs/btrfs/super.c
> +++ b/fs/btrfs/super.c
> @@ -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

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

> +
>  	/*
>  	 * Provide the same semantics as older kernels that don't use fs
>  	 * context, specifying the "compress" option clears

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 2/2] btrfs: harden parsing of compress mount options
  2025-06-02 17:29   ` David Sterba
@ 2025-06-12 13:29     ` Daniel Vacek
  2025-06-12 15:18       ` David Sterba
  0 siblings, 1 reply; 8+ messages in thread
From: Daniel Vacek @ 2025-06-12 13:29 UTC (permalink / raw)
  To: dsterba
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, linux-btrfs,
	linux-kernel

On Mon, 2 Jun 2025 at 19:29, David Sterba <dsterba@suse.cz> wrote:
>
> On Mon, Jun 02, 2025 at 05:53:19PM +0200, Daniel Vacek wrote:
> > 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.
> >
> > Signed-off-by: Daniel Vacek <neelx@suse.com>
> > ---
> > v3 changes: Split into two patches to ease backporting,
> >             no functional changes.
> >
> >  fs/btrfs/super.c | 26 +++++++++++++++++++-------
> >  1 file changed, 19 insertions(+), 7 deletions(-)
> >
> > diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> > index 6291ab45ab2a5..4510c5f7a785e 100644
> > --- a/fs/btrfs/super.c
> > +++ b/fs/btrfs/super.c
> > @@ -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
>
> > +{
> > +     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?


> > +
> >       /*
> >        * Provide the same semantics as older kernels that don't use fs
> >        * context, specifying the "compress" option clears

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 2/2] btrfs: harden parsing of compress mount options
  2025-06-12 13:29     ` Daniel Vacek
@ 2025-06-12 15:18       ` David Sterba
  0 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2025-06-12 15:18 UTC (permalink / raw)
  To: Daniel Vacek
  Cc: Chris Mason, Josef Bacik, David Sterba, Nick Terrell, linux-btrfs,
	linux-kernel

On Thu, Jun 12, 2025 at 03:29:21PM +0200, Daniel Vacek wrote:
> On Mon, 2 Jun 2025 at 19:29, David Sterba <dsterba@suse.cz> wrote:
> >
> > On Mon, Jun 02, 2025 at 05:53:19PM +0200, Daniel Vacek wrote:
> > > 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.
> > >
> > > Signed-off-by: Daniel Vacek <neelx@suse.com>
> > > ---
> > > v3 changes: Split into two patches to ease backporting,
> > >             no functional changes.
> > >
> > >  fs/btrfs/super.c | 26 +++++++++++++++++++-------
> > >  1 file changed, 19 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> > > index 6291ab45ab2a5..4510c5f7a785e 100644
> > > --- a/fs/btrfs/super.c
> > > +++ b/fs/btrfs/super.c
> > > @@ -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
> >
> > > +{
> > > +     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?

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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-06-12 15:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-02 15:53 [PATCH v3 0/2] btrfs: harden parsing of compress mount options Daniel Vacek
2025-06-02 15:53 ` [PATCH v3 1/2] btrfs: factor out compress mount options parsing Daniel Vacek
2025-06-02 17:28   ` David Sterba
2025-06-02 15:53 ` [PATCH v3 2/2] btrfs: harden parsing of compress mount options Daniel Vacek
2025-06-02 17:29   ` David Sterba
2025-06-12 13:29     ` Daniel Vacek
2025-06-12 15:18       ` David Sterba
2025-06-02 17:28 ` [PATCH v3 0/2] " David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).