Skip to content

Commit 648412b

Browse files
Daniel Vacekkdave
authored andcommitted
btrfs: harden parsing of compress mount option
Btrfs happily but incorrectly accepts the `-o compress=zlib+foo` and similar options with any random suffix. Let's handle that correctly. Signed-off-by: Daniel Vacek <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent f1b6b64 commit 648412b

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

fs/btrfs/super.c

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,67 @@ static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
270270
return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
271271
}
272272

273+
static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
274+
struct fs_parameter *param, int opt)
275+
{
276+
/*
277+
* Provide the same semantics as older kernels that don't use fs
278+
* context, specifying the "compress" option clears
279+
* "force-compress" without the need to pass
280+
* "compress-force=[no|none]" before specifying "compress".
281+
*/
282+
if (opt != Opt_compress_force && opt != Opt_compress_force_type)
283+
btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
284+
285+
if (opt == Opt_compress || opt == Opt_compress_force) {
286+
ctx->compress_type = BTRFS_COMPRESS_ZLIB;
287+
ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
288+
btrfs_set_opt(ctx->mount_opt, COMPRESS);
289+
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
290+
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
291+
} else if (strncmp(param->string, "zlib", 4) == 0 &&
292+
(param->string[4] == ':' ||
293+
param->string[4] == '\0')) {
294+
ctx->compress_type = BTRFS_COMPRESS_ZLIB;
295+
ctx->compress_level =
296+
btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
297+
param->string + 4);
298+
btrfs_set_opt(ctx->mount_opt, COMPRESS);
299+
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
300+
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
301+
} else if (strncmp(param->string, "lzo", 3) == 0 &&
302+
param->string[3] == '\0') {
303+
ctx->compress_type = BTRFS_COMPRESS_LZO;
304+
ctx->compress_level = 0;
305+
btrfs_set_opt(ctx->mount_opt, COMPRESS);
306+
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
307+
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
308+
} else if (strncmp(param->string, "zstd", 4) == 0 &&
309+
(param->string[4] == ':' ||
310+
param->string[4] == '\0')) {
311+
ctx->compress_type = BTRFS_COMPRESS_ZSTD;
312+
ctx->compress_level =
313+
btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
314+
param->string + 4);
315+
btrfs_set_opt(ctx->mount_opt, COMPRESS);
316+
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
317+
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
318+
} else if ((strncmp(param->string, "no", 2) == 0 &&
319+
param->string[2] == '\0') ||
320+
(strncmp(param->string, "none", 4) == 0 &&
321+
param->string[4] == '\0')) {
322+
ctx->compress_level = 0;
323+
ctx->compress_type = 0;
324+
btrfs_clear_opt(ctx->mount_opt, COMPRESS);
325+
btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
326+
} else {
327+
btrfs_err(NULL, "unrecognized compression value %s",
328+
param->string);
329+
return -EINVAL;
330+
}
331+
return 0;
332+
}
333+
273334
static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
274335
{
275336
struct btrfs_fs_context *ctx = fc->fs_private;
@@ -339,53 +400,8 @@ static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
339400
fallthrough;
340401
case Opt_compress:
341402
case Opt_compress_type:
342-
/*
343-
* Provide the same semantics as older kernels that don't use fs
344-
* context, specifying the "compress" option clears
345-
* "force-compress" without the need to pass
346-
* "compress-force=[no|none]" before specifying "compress".
347-
*/
348-
if (opt != Opt_compress_force && opt != Opt_compress_force_type)
349-
btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
350-
351-
if (opt == Opt_compress || opt == Opt_compress_force) {
352-
ctx->compress_type = BTRFS_COMPRESS_ZLIB;
353-
ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
354-
btrfs_set_opt(ctx->mount_opt, COMPRESS);
355-
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
356-
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
357-
} else if (strncmp(param->string, "zlib", 4) == 0) {
358-
ctx->compress_type = BTRFS_COMPRESS_ZLIB;
359-
ctx->compress_level =
360-
btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
361-
param->string + 4);
362-
btrfs_set_opt(ctx->mount_opt, COMPRESS);
363-
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
364-
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
365-
} else if (strncmp(param->string, "lzo", 3) == 0) {
366-
ctx->compress_type = BTRFS_COMPRESS_LZO;
367-
ctx->compress_level = 0;
368-
btrfs_set_opt(ctx->mount_opt, COMPRESS);
369-
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
370-
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
371-
} else if (strncmp(param->string, "zstd", 4) == 0) {
372-
ctx->compress_type = BTRFS_COMPRESS_ZSTD;
373-
ctx->compress_level =
374-
btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
375-
param->string + 4);
376-
btrfs_set_opt(ctx->mount_opt, COMPRESS);
377-
btrfs_clear_opt(ctx->mount_opt, NODATACOW);
378-
btrfs_clear_opt(ctx->mount_opt, NODATASUM);
379-
} else if (strncmp(param->string, "no", 2) == 0) {
380-
ctx->compress_level = 0;
381-
ctx->compress_type = 0;
382-
btrfs_clear_opt(ctx->mount_opt, COMPRESS);
383-
btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
384-
} else {
385-
btrfs_err(NULL, "unrecognized compression value %s",
386-
param->string);
403+
if (btrfs_parse_compress(ctx, param, opt))
387404
return -EINVAL;
388-
}
389405
break;
390406
case Opt_ssd:
391407
if (result.negated) {

0 commit comments

Comments
 (0)