Commit d1d488d8 authored by Christian Brauner's avatar Christian Brauner Committed by Linus Torvalds

fs: add vfs_parse_fs_param_source() helper

Add a simple helper that filesystems can use in their parameter parser
to parse the "source" parameter. A few places open-coded this function
and that already caused a bug in the cgroup v1 parser that we fixed.
Let's make it harder to get this wrong by introducing a helper which
performs all necessary checks.

Link: https://syzkaller.appspot.com/bug?id=6312526aba5beae046fdae8f00399f87aab48b12
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: default avatarChristian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 3b046272
...@@ -79,6 +79,35 @@ static int vfs_parse_sb_flag(struct fs_context *fc, const char *key) ...@@ -79,6 +79,35 @@ static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
return -ENOPARAM; return -ENOPARAM;
} }
/**
* vfs_parse_fs_param_source - Handle setting "source" via parameter
* @fc: The filesystem context to modify
* @param: The parameter
*
* This is a simple helper for filesystems to verify that the "source" they
* accept is sane.
*
* Returns 0 on success, -ENOPARAM if this is not "source" parameter, and
* -EINVAL otherwise. In the event of failure, supplementary error information
* is logged.
*/
int vfs_parse_fs_param_source(struct fs_context *fc, struct fs_parameter *param)
{
if (strcmp(param->key, "source") != 0)
return -ENOPARAM;
if (param->type != fs_value_is_string)
return invalf(fc, "Non-string source");
if (fc->source)
return invalf(fc, "Multiple sources");
fc->source = param->string;
param->string = NULL;
return 0;
}
EXPORT_SYMBOL(vfs_parse_fs_param_source);
/** /**
* vfs_parse_fs_param - Add a single parameter to a superblock config * vfs_parse_fs_param - Add a single parameter to a superblock config
* @fc: The filesystem context to modify * @fc: The filesystem context to modify
...@@ -122,15 +151,9 @@ int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param) ...@@ -122,15 +151,9 @@ int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
/* If the filesystem doesn't take any arguments, give it the /* If the filesystem doesn't take any arguments, give it the
* default handling of source. * default handling of source.
*/ */
if (strcmp(param->key, "source") == 0) { ret = vfs_parse_fs_param_source(fc, param);
if (param->type != fs_value_is_string) if (ret != -ENOPARAM)
return invalf(fc, "VFS: Non-string source"); return ret;
if (fc->source)
return invalf(fc, "VFS: Multiple sources");
fc->source = param->string;
param->string = NULL;
return 0;
}
return invalf(fc, "%s: Unknown parameter '%s'", return invalf(fc, "%s: Unknown parameter '%s'",
fc->fs_type->name, param->key); fc->fs_type->name, param->key);
...@@ -504,16 +527,11 @@ static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param) ...@@ -504,16 +527,11 @@ static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
struct legacy_fs_context *ctx = fc->fs_private; struct legacy_fs_context *ctx = fc->fs_private;
unsigned int size = ctx->data_size; unsigned int size = ctx->data_size;
size_t len = 0; size_t len = 0;
int ret;
if (strcmp(param->key, "source") == 0) { ret = vfs_parse_fs_param_source(fc, param);
if (param->type != fs_value_is_string) if (ret != -ENOPARAM)
return invalf(fc, "VFS: Legacy: Non-string source"); return ret;
if (fc->source)
return invalf(fc, "VFS: Legacy: Multiple sources");
fc->source = param->string;
param->string = NULL;
return 0;
}
if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS) if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options"); return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
......
...@@ -139,6 +139,8 @@ extern int vfs_parse_fs_string(struct fs_context *fc, const char *key, ...@@ -139,6 +139,8 @@ extern int vfs_parse_fs_string(struct fs_context *fc, const char *key,
extern int generic_parse_monolithic(struct fs_context *fc, void *data); extern int generic_parse_monolithic(struct fs_context *fc, void *data);
extern int vfs_get_tree(struct fs_context *fc); extern int vfs_get_tree(struct fs_context *fc);
extern void put_fs_context(struct fs_context *fc); extern void put_fs_context(struct fs_context *fc);
extern int vfs_parse_fs_param_source(struct fs_context *fc,
struct fs_parameter *param);
/* /*
* sget() wrappers to be called from the ->get_tree() op. * sget() wrappers to be called from the ->get_tree() op.
......
...@@ -911,15 +911,11 @@ int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param) ...@@ -911,15 +911,11 @@ int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param)
opt = fs_parse(fc, cgroup1_fs_parameters, param, &result); opt = fs_parse(fc, cgroup1_fs_parameters, param, &result);
if (opt == -ENOPARAM) { if (opt == -ENOPARAM) {
if (strcmp(param->key, "source") == 0) { int ret;
if (param->type != fs_value_is_string)
return invalf(fc, "Non-string source"); ret = vfs_parse_fs_param_source(fc, param);
if (fc->source) if (ret != -ENOPARAM)
return invalf(fc, "Multiple sources not supported"); return ret;
fc->source = param->string;
param->string = NULL;
return 0;
}
for_each_subsys(ss, i) { for_each_subsys(ss, i) {
if (strcmp(param->key, ss->legacy_name)) if (strcmp(param->key, ss->legacy_name))
continue; continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment