Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH] btrfs: reject unknown mount options correctly
@ 2023-10-12  4:44 Qu Wenruo
  2023-10-12  6:01 ` Su Yue
  0 siblings, 1 reply; 5+ messages in thread
From: Qu Wenruo @ 2023-10-12  4:44 UTC (permalink / raw)
  To: linux-btrfs

[BUG]
The following script would allow invalid mount options to be specified
(although such invalid options would just be ignored):

 # mkfs.btrfs -f $dev
 # mount $dev $mnt1		<<< Successful mount expected
 # mount $dev $mnt2 -o junk	<<< Failed mount expected
 # echo $?
 0

[CAUSE]
During the mount progress, btrfs_mount_root() would go different paths
depending on if there is already a mounted btrfs for it:

	s = sget();
	if (s->s_root) {
		/* do the cleanups and reuse the existing super */
	} else {
		/* do the real mount */
		error = btrfs_fill_super();
	}

Inside btrfs_fill_super() we call open_ctree() and then
btrfs_parse_options(), which would reject all the invalid options.

But if we got the other path, we won't really call
btrfs_parse_options(), thus we just ignore the mount options completely.

[FIX]
Instead of pure cleanups, if we found an existing mounted btrfs, we
still do a very basic mount options check, to reject unknown mount
options.

Inside btrfs_mount_root(), we have already called
security_sb_eat_lsm_opts(), which would have already stripe the security
mount options, thus if we hit an error, it must be an invalid one.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
This would be the proper fix for the recently reverted commit
5f521494cc73 ("btrfs: reject unknown mount options early").

With updated timing where the new check is after
security_sb_eat_lsm_opts().
---
 fs/btrfs/super.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index cc326969751f..4e4a2e4ba315 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -860,6 +860,50 @@ static int btrfs_parse_device_options(const char *options, blk_mode_t flags)
 	return error;
 }
 
+/*
+ * Check if the @options has any invalid ones.
+ *
+ * NOTE: this can only be called after security_sb_eat_lsm_opts().
+ *
+ * Return -ENOMEM if we failed to allocate the memory for the string
+ * Return -EINVAL if we found invalid mount options
+ * Return 0 otherwise.
+ */
+static int btrfs_check_invalid_options(const char *options)
+{
+	substring_t args[MAX_OPT_ARGS];
+	char *opts, *orig, *p;
+	int ret = 0;
+
+	if (!options)
+		return 0;
+
+	opts = kstrdup(options, GFP_KERNEL);
+	if (!opts)
+		return -ENOMEM;
+	orig = opts;
+
+	while ((p = strsep(&opts, ",")) != NULL) {
+		int token;
+
+		if (!*p)
+			continue;
+
+		token = match_token(p, tokens, args);
+		switch (token) {
+		case Opt_err:
+			btrfs_err(NULL, "unrecognized mount option '%s'", p);
+			ret = -EINVAL;
+			goto out;
+		default:
+			break;
+		}
+	}
+out:
+	kfree(orig);
+	return ret;
+}
+
 /*
  * Parse mount options that are related to subvolume id
  *
@@ -1474,6 +1518,8 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
 		btrfs_free_fs_info(fs_info);
 		if ((flags ^ s->s_flags) & SB_RDONLY)
 			error = -EBUSY;
+		if (!error)
+			error = btrfs_check_invalid_options(data);
 	} else {
 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
 		shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
-- 
2.42.0


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

end of thread, other threads:[~2023-11-03 16:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-12  4:44 [PATCH] btrfs: reject unknown mount options correctly Qu Wenruo
2023-10-12  6:01 ` Su Yue
2023-10-12  7:07   ` Qu Wenruo
2023-10-12 14:55     ` David Sterba
2023-11-03 16:41       ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox