Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Su Yue <l@damenly.org>
To: Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs: reject unknown mount options correctly
Date: Thu, 12 Oct 2023 14:01:53 +0800	[thread overview]
Message-ID: <cyxk1i2x.fsf@damenly.org> (raw)
In-Reply-To: <a6a954a1f1c7d612104279c62916f49e47ba5811.1697085884.git.wqu@suse.com>


On Thu 12 Oct 2023 at 15:14, Qu Wenruo <wqu@suse.com> wrote:

> [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").
>
I'm a noob about selinux though. Better to draft a new fstest case 
to
avoid further regression?

--
Su

> 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,

  reply	other threads:[~2023-10-12  6:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12  4:44 [PATCH] btrfs: reject unknown mount options correctly Qu Wenruo
2023-10-12  6:01 ` Su Yue [this message]
2023-10-12  7:07   ` Qu Wenruo
2023-10-12 14:55     ` David Sterba
2023-11-03 16:41       ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cyxk1i2x.fsf@damenly.org \
    --to=l@damenly.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox