1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Filesystem parameter parser.
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/export.h>
9 #include <linux/fs_context.h>
10 #include <linux/fs_parser.h>
11 #include <linux/slab.h>
12 #include <linux/security.h>
13 #include <linux/namei.h>
16 static const struct constant_table bool_names[] = {
26 * lookup_constant - Look up a constant by name in an ordered table
27 * @tbl: The table of constants to search.
28 * @tbl_size: The size of the table.
29 * @name: The name to look up.
30 * @not_found: The value to return if the name is not found.
32 int __lookup_constant(const struct constant_table *tbl, size_t tbl_size,
33 const char *name, int not_found)
37 for (i = 0; i < tbl_size; i++)
38 if (strcmp(name, tbl[i].name) == 0)
43 EXPORT_SYMBOL(__lookup_constant);
45 static const struct fs_parameter_spec *fs_lookup_key(
46 const struct fs_parameter_description *desc,
49 const struct fs_parameter_spec *p;
54 for (p = desc->specs; p->name; p++)
55 if (strcmp(p->name, name) == 0)
62 * fs_parse - Parse a filesystem configuration parameter
63 * @fc: The filesystem context to log errors through.
64 * @desc: The parameter description to use.
65 * @param: The parameter.
66 * @result: Where to place the result of the parse
68 * Parse a filesystem configuration parameter and attempt a conversion for a
69 * simple parameter for which this is requested. If successful, the determined
70 * parameter ID is placed into @result->key, the desired type is indicated in
71 * @result->t and any converted value is placed into an appropriate member of
72 * the union in @result.
74 * The function returns the parameter number if the parameter was matched,
75 * -ENOPARAM if it wasn't matched and @desc->ignore_unknown indicated that
76 * unknown parameters are okay and -EINVAL if there was a conversion issue or
77 * the parameter wasn't recognised and unknowns aren't okay.
79 int fs_parse(struct fs_context *fc,
80 const struct fs_parameter_description *desc,
81 struct fs_parameter *param,
82 struct fs_parse_result *result)
84 const struct fs_parameter_spec *p;
85 const struct fs_parameter_enum *e;
86 int ret = -ENOPARAM, b;
88 result->has_value = !!param->string;
89 result->negated = false;
92 p = fs_lookup_key(desc, param->key);
94 /* If we didn't find something that looks like "noxxx", see if
95 * "xxx" takes the "no"-form negative - but only if there
98 if (result->has_value)
99 goto unknown_parameter;
100 if (param->key[0] != 'n' || param->key[1] != 'o' || !param->key[2])
101 goto unknown_parameter;
103 p = fs_lookup_key(desc, param->key + 2);
105 goto unknown_parameter;
106 if (!(p->flags & fs_param_neg_with_no))
107 goto unknown_parameter;
108 result->boolean = false;
109 result->negated = true;
112 if (p->flags & fs_param_deprecated)
113 warnf(fc, "%s: Deprecated parameter '%s'",
114 desc->name, param->key);
119 /* Certain parameter types only take a string and convert it. */
121 case __fs_param_wasnt_defined:
123 case fs_param_is_u32:
124 case fs_param_is_u32_octal:
125 case fs_param_is_u32_hex:
126 case fs_param_is_s32:
127 case fs_param_is_u64:
128 case fs_param_is_enum:
129 case fs_param_is_string:
130 if (param->type != fs_value_is_string)
132 if (!result->has_value) {
133 if (p->flags & fs_param_v_optional)
142 /* Try to turn the type we were given into the type desired by the
143 * parameter and give an error if we can't.
146 case fs_param_is_flag:
147 if (param->type != fs_value_is_flag &&
148 (param->type != fs_value_is_string || result->has_value))
149 return invalf(fc, "%s: Unexpected value for '%s'",
150 desc->name, param->key);
151 result->boolean = true;
154 case fs_param_is_bool:
155 switch (param->type) {
156 case fs_value_is_flag:
157 result->boolean = true;
159 case fs_value_is_string:
160 if (param->size == 0) {
161 result->boolean = true;
164 b = lookup_constant(bool_names, param->string, -1);
173 case fs_param_is_u32:
174 ret = kstrtouint(param->string, 0, &result->uint_32);
176 case fs_param_is_u32_octal:
177 ret = kstrtouint(param->string, 8, &result->uint_32);
179 case fs_param_is_u32_hex:
180 ret = kstrtouint(param->string, 16, &result->uint_32);
182 case fs_param_is_s32:
183 ret = kstrtoint(param->string, 0, &result->int_32);
185 case fs_param_is_u64:
186 ret = kstrtoull(param->string, 0, &result->uint_64);
189 case fs_param_is_enum:
190 for (e = desc->enums; e->name[0]; e++) {
191 if (e->opt == p->opt &&
192 strcmp(e->name, param->string) == 0) {
193 result->uint_32 = e->value;
199 case fs_param_is_string:
201 case fs_param_is_blob:
202 if (param->type != fs_value_is_blob)
206 case fs_param_is_fd: {
207 switch (param->type) {
208 case fs_value_is_string:
209 if (!result->has_value)
212 ret = kstrtouint(param->string, 0, &result->uint_32);
214 case fs_value_is_file:
215 result->uint_32 = param->dirfd;
221 if (result->uint_32 > INT_MAX)
226 case fs_param_is_blockdev:
227 case fs_param_is_path:
240 return invalf(fc, "%s: Bad value for '%s'", desc->name, param->key);
244 EXPORT_SYMBOL(fs_parse);
247 * fs_lookup_param - Look up a path referred to by a parameter
248 * @fc: The filesystem context to log errors through.
249 * @param: The parameter.
250 * @want_bdev: T if want a blockdev
251 * @_path: The result of the lookup
253 int fs_lookup_param(struct fs_context *fc,
254 struct fs_parameter *param,
259 unsigned int flags = 0;
263 switch (param->type) {
264 case fs_value_is_string:
265 f = getname_kernel(param->string);
270 case fs_value_is_filename_empty:
271 flags = LOOKUP_EMPTY;
273 case fs_value_is_filename:
278 return invalf(fc, "%s: not usable as path", param->key);
281 f->refcnt++; /* filename_lookup() drops our ref. */
282 ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
284 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
289 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
291 _path->dentry = NULL;
293 errorf(fc, "%s: Non-blockdev passed as '%s'",
294 param->key, f->name);
303 EXPORT_SYMBOL(fs_lookup_param);
305 #ifdef CONFIG_VALIDATE_FS_PARSER
307 * validate_constant_table - Validate a constant table
308 * @name: Name to use in reporting
309 * @tbl: The constant table to validate.
310 * @tbl_size: The size of the table.
311 * @low: The lowest permissible value.
312 * @high: The highest permissible value.
313 * @special: One special permissible value outside of the range.
315 bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
316 int low, int high, int special)
322 pr_warn("VALIDATE C-TBL: Empty\n");
326 for (i = 0; i < tbl_size; i++) {
328 pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
330 } else if (i > 0 && tbl[i - 1].name) {
331 int c = strcmp(tbl[i-1].name, tbl[i].name);
334 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
339 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
340 i, tbl[i-1].name, tbl[i].name);
345 if (tbl[i].value != special &&
346 (tbl[i].value < low || tbl[i].value > high)) {
347 pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
348 i, tbl[i].name, tbl[i].value, low, high);
357 * fs_validate_description - Validate a parameter description
358 * @desc: The parameter description to validate.
360 bool fs_validate_description(const struct fs_parameter_description *desc)
362 const struct fs_parameter_spec *param, *p2;
363 const struct fs_parameter_enum *e;
364 const char *name = desc->name;
365 unsigned int nr_params = 0;
366 bool good = true, enums = false;
368 pr_notice("*** VALIDATE %s ***\n", name);
371 pr_err("VALIDATE Parser: No name\n");
377 for (param = desc->specs; param->name; param++) {
378 enum fs_parameter_type t = param->type;
380 /* Check that the type is in range */
381 if (t == __fs_param_wasnt_defined ||
382 t >= nr__fs_parameter_type) {
383 pr_err("VALIDATE %s: PARAM[%s] Bad type %u\n",
384 name, param->name, t);
386 } else if (t == fs_param_is_enum) {
390 /* Check for duplicate parameter names */
391 for (p2 = desc->specs; p2 < param; p2++) {
392 if (strcmp(param->name, p2->name) == 0) {
393 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
400 nr_params = param - desc->specs;
405 pr_err("VALIDATE %s: Enum table but no parameters\n",
411 pr_err("VALIDATE %s: Enum table but no enum-type values\n",
417 for (e = desc->enums; e->name[0]; e++) {
418 /* Check that all entries in the enum table have at
419 * least one parameter that uses them.
421 for (param = desc->specs; param->name; param++) {
422 if (param->opt == e->opt &&
423 param->type != fs_param_is_enum) {
424 pr_err("VALIDATE %s: e[%tu] enum val for %s\n",
425 name, e - desc->enums, param->name);
431 /* Check that all enum-type parameters have at least one enum
432 * value in the enum table.
434 for (param = desc->specs; param->name; param++) {
435 if (param->type != fs_param_is_enum)
437 for (e = desc->enums; e->name[0]; e++)
438 if (e->opt == param->opt)
441 pr_err("VALIDATE %s: PARAM[%s] enum with no values\n",
448 pr_err("VALIDATE %s: enum-type values, but no enum table\n",
458 #endif /* CONFIG_VALIDATE_FS_PARSER */