From nobody Fri Dec 19 21:50:40 2025 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8479618DB19 for ; Wed, 23 Apr 2025 13:22:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745414554; cv=none; b=nmPX1l3TjzR6UQemQKo9OvXmSorJWZi36IJokLagWEZRUSkKIVTR++XFlwIXLZqV1Zs0DWC/1E6BaYB3CFvkKZ1NXDyVGGtoX9KvTX6q9fzfiRORPZzNs1cuZiCdZ/E7lu/fppUfN+PUTbn3Z3L4C9TO+nN9yH0VMPzDG3oqTpM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745414554; c=relaxed/simple; bh=2fSGqfGe9QF0hCkGFRwBT0+5mcM+4uAzRmVq171CGlg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jac+TpgAuqnfzNqVeH+K8g7A9ywOHoKgRRBBvqdve9VcE1K7v5Mh+C5VlTUZU09k16tGXn41cTSheKLenQyn7NH+K9EdcJy3nYJvMfTJLwluWn6VYeNkAZ4g/Laxna6+GJ2tsPZ6vQ/Z+lddK70YJH020y1q5SEsjPI0zrbf/Xw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=WYz23Fj6; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=WYz23Fj6; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="WYz23Fj6"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="WYz23Fj6" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 8312E1F441; Wed, 23 Apr 2025 13:22:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1745414550; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sbm+i1YS9B9TwaPdJxxSVF9m6Ca6BSDgVpX6BzZzH4E=; b=WYz23Fj6yBwM4ukDzfxvaRNJM6mI/ZA6pFm8VFNRtmk9ff48jVvbW/iXqKJPd5DHx1EeV8 vaafql/+9gIT5jmqwkBrf+zlQ29KXkWmafiB7WPDTryaT1w08eLEOmJ9LRnJFy8xHxlYkq nh4YjZeEPrtaYCZjCVim8XVmpEiKsaw= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=WYz23Fj6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1745414550; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sbm+i1YS9B9TwaPdJxxSVF9m6Ca6BSDgVpX6BzZzH4E=; b=WYz23Fj6yBwM4ukDzfxvaRNJM6mI/ZA6pFm8VFNRtmk9ff48jVvbW/iXqKJPd5DHx1EeV8 vaafql/+9gIT5jmqwkBrf+zlQ29KXkWmafiB7WPDTryaT1w08eLEOmJ9LRnJFy8xHxlYkq nh4YjZeEPrtaYCZjCVim8XVmpEiKsaw= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 619CE13A3D; Wed, 23 Apr 2025 13:22:30 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id RO7qFpbpCGhRWAAAD6G6ig (envelope-from ); Wed, 23 Apr 2025 13:22:30 +0000 From: Daniel Vacek To: Chris Mason , Josef Bacik , David Sterba , Nick Terrell Cc: Daniel Vacek , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] btrfs: harden parsing of compress mount option Date: Wed, 23 Apr 2025 15:22:19 +0200 Message-ID: <20250423132220.4052042-1-neelx@suse.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250423073329.4021878-1-neelx@suse.com> References: <20250423073329.4021878-1-neelx@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 8312E1F441 X-Spam-Level: X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_COUNT_TWO(0.00)[2]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[7]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:dkim,suse.com:mid,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+] X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Action: no action X-Spam-Score: -3.01 X-Spam-Flag: NO Content-Type: text/plain; charset="utf-8" Btrfs happily but incorrectly accepts the `-o compress=3Dzlib+foo` and simi= lar options with any random suffix. Let's handle that correctly. Signed-off-by: Daniel Vacek --- v2: Drop useless check for comma and split compress options into a separate helper function fs/btrfs/super.c | 108 +++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 46 deletions(-) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 40709e2a44fce..422fb82279877 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -270,6 +270,67 @@ static inline blk_mode_t btrfs_open_mode(struct fs_con= text *fc) return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES; } =20 +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=3D[no|none]" before specifying "compress". + */ + if (opt !=3D Opt_compress_force && opt !=3D Opt_compress_force_type) + btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS); + + if (opt =3D=3D Opt_compress || opt =3D=3D Opt_compress_force) { + ctx->compress_type =3D BTRFS_COMPRESS_ZLIB; + ctx->compress_level =3D 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) =3D=3D 0 && + (param->string[4] =3D=3D ':' || + param->string[4] =3D=3D '\0')) { + ctx->compress_type =3D BTRFS_COMPRESS_ZLIB; + ctx->compress_level =3D + 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) =3D=3D 0 && + param->string[3] =3D=3D '\0') { + ctx->compress_type =3D BTRFS_COMPRESS_LZO; + ctx->compress_level =3D 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) =3D=3D 0 && + (param->string[4] =3D=3D ':' || + param->string[4] =3D=3D '\0')) { + ctx->compress_type =3D BTRFS_COMPRESS_ZSTD; + ctx->compress_level =3D + 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) =3D=3D 0 && + param->string[2] =3D=3D '\0') || + (strncmp(param->string, "none", 4) =3D=3D 0 && + param->string[4] =3D=3D '\0')) { + ctx->compress_level =3D 0; + ctx->compress_type =3D 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 *p= aram) { struct btrfs_fs_context *ctx =3D fc->fs_private; @@ -339,53 +400,8 @@ static int btrfs_parse_param(struct fs_context *fc, st= ruct 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=3D[no|none]" before specifying "compress". - */ - if (opt !=3D Opt_compress_force && opt !=3D Opt_compress_force_type) - btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS); - - if (opt =3D=3D Opt_compress || opt =3D=3D Opt_compress_force) { - ctx->compress_type =3D BTRFS_COMPRESS_ZLIB; - ctx->compress_level =3D 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) =3D=3D 0) { - ctx->compress_type =3D BTRFS_COMPRESS_ZLIB; - ctx->compress_level =3D - 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) =3D=3D 0) { - ctx->compress_type =3D BTRFS_COMPRESS_LZO; - ctx->compress_level =3D 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) =3D=3D 0) { - ctx->compress_type =3D BTRFS_COMPRESS_ZSTD; - ctx->compress_level =3D - 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) =3D=3D 0) { - ctx->compress_level =3D 0; - ctx->compress_type =3D 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) { --=20 2.47.2