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 static const struct constant_table *
27 __lookup_constant(const struct constant_table *tbl, const char *name)
29 for ( ; tbl->name; tbl++)
30 if (strcmp(name, tbl->name) == 0)
36 * lookup_constant - Look up a constant by name in an ordered table
37 * @tbl: The table of constants to search.
38 * @name: The name to look up.
39 * @not_found: The value to return if the name is not found.
41 int lookup_constant(const struct constant_table *tbl, const char *name, int not_found)
43 const struct constant_table *p = __lookup_constant(tbl, name);
45 return p ? p->value : not_found;
47 EXPORT_SYMBOL(lookup_constant);
49 static const struct fs_parameter_spec *fs_lookup_key(
50 const struct fs_parameter_description *desc,
53 const struct fs_parameter_spec *p;
58 for (p = desc->specs; p->name; p++)
59 if (strcmp(p->name, name) == 0)
66 * fs_parse - Parse a filesystem configuration parameter
67 * @fc: The filesystem context to log errors through.
68 * @desc: The parameter description to use.
69 * @param: The parameter.
70 * @result: Where to place the result of the parse
72 * Parse a filesystem configuration parameter and attempt a conversion for a
73 * simple parameter for which this is requested. If successful, the determined
74 * parameter ID is placed into @result->key, the desired type is indicated in
75 * @result->t and any converted value is placed into an appropriate member of
76 * the union in @result.
78 * The function returns the parameter number if the parameter was matched,
79 * -ENOPARAM if it wasn't matched and @desc->ignore_unknown indicated that
80 * unknown parameters are okay and -EINVAL if there was a conversion issue or
81 * the parameter wasn't recognised and unknowns aren't okay.
83 int __fs_parse(struct p_log *log,
84 const struct fs_parameter_description *desc,
85 struct fs_parameter *param,
86 struct fs_parse_result *result)
88 const struct fs_parameter_spec *p;
89 const struct constant_table *e;
90 int ret = -ENOPARAM, b;
92 result->negated = false;
95 p = fs_lookup_key(desc, param->key);
97 /* If we didn't find something that looks like "noxxx", see if
98 * "xxx" takes the "no"-form negative - but only if there
101 if (param->type != fs_value_is_flag)
102 goto unknown_parameter;
103 if (param->key[0] != 'n' || param->key[1] != 'o' || !param->key[2])
104 goto unknown_parameter;
106 p = fs_lookup_key(desc, param->key + 2);
108 goto unknown_parameter;
109 if (!(p->flags & fs_param_neg_with_no))
110 goto unknown_parameter;
111 result->boolean = false;
112 result->negated = true;
115 if (p->flags & fs_param_deprecated)
116 warn_plog(log, "Deprecated parameter '%s'", param->key);
121 /* Certain parameter types only take a string and convert it. */
123 case __fs_param_wasnt_defined:
125 case fs_param_is_u32:
126 case fs_param_is_u32_octal:
127 case fs_param_is_u32_hex:
128 case fs_param_is_s32:
129 case fs_param_is_u64:
130 case fs_param_is_enum:
131 case fs_param_is_string:
132 if (param->type == fs_value_is_string) {
133 if (p->flags & fs_param_v_optional)
139 if (param->type == fs_value_is_flag) {
140 if (p->flags & fs_param_v_optional)
148 /* Try to turn the type we were given into the type desired by the
149 * parameter and give an error if we can't.
152 case fs_param_is_flag:
153 if (param->type != fs_value_is_flag)
154 return inval_plog(log, "Unexpected value for '%s'",
156 result->boolean = true;
159 case fs_param_is_bool:
160 switch (param->type) {
161 case fs_value_is_flag:
162 result->boolean = true;
164 case fs_value_is_string:
165 if (param->size == 0) {
166 result->boolean = true;
169 b = lookup_constant(bool_names, param->string, -1);
178 case fs_param_is_u32:
179 ret = kstrtouint(param->string, 0, &result->uint_32);
181 case fs_param_is_u32_octal:
182 ret = kstrtouint(param->string, 8, &result->uint_32);
184 case fs_param_is_u32_hex:
185 ret = kstrtouint(param->string, 16, &result->uint_32);
187 case fs_param_is_s32:
188 ret = kstrtoint(param->string, 0, &result->int_32);
190 case fs_param_is_u64:
191 ret = kstrtoull(param->string, 0, &result->uint_64);
194 case fs_param_is_enum:
195 e = __lookup_constant(p->data, param->string);
197 result->uint_32 = e->value;
202 case fs_param_is_string:
204 case fs_param_is_blob:
205 if (param->type != fs_value_is_blob)
209 case fs_param_is_fd: {
210 switch (param->type) {
211 case fs_value_is_string:
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 inval_plog(log, "Bad value for '%s'", 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:
275 return invalf(fc, "%s: not usable as path", param->key);
278 f->refcnt++; /* filename_lookup() drops our ref. */
279 ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
281 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
286 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
288 _path->dentry = NULL;
290 errorf(fc, "%s: Non-blockdev passed as '%s'",
291 param->key, f->name);
300 EXPORT_SYMBOL(fs_lookup_param);
302 #ifdef CONFIG_VALIDATE_FS_PARSER
304 * validate_constant_table - Validate a constant table
305 * @name: Name to use in reporting
306 * @tbl: The constant table to validate.
307 * @tbl_size: The size of the table.
308 * @low: The lowest permissible value.
309 * @high: The highest permissible value.
310 * @special: One special permissible value outside of the range.
312 bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
313 int low, int high, int special)
319 pr_warn("VALIDATE C-TBL: Empty\n");
323 for (i = 0; i < tbl_size; i++) {
325 pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
327 } else if (i > 0 && tbl[i - 1].name) {
328 int c = strcmp(tbl[i-1].name, tbl[i].name);
331 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
336 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
337 i, tbl[i-1].name, tbl[i].name);
342 if (tbl[i].value != special &&
343 (tbl[i].value < low || tbl[i].value > high)) {
344 pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
345 i, tbl[i].name, tbl[i].value, low, high);
354 * fs_validate_description - Validate a parameter description
355 * @desc: The parameter description to validate.
357 bool fs_validate_description(const struct fs_parameter_description *desc)
359 const struct fs_parameter_spec *param, *p2;
360 const char *name = desc->name;
363 pr_notice("*** VALIDATE %s ***\n", name);
366 pr_err("VALIDATE Parser: No name\n");
372 for (param = desc->specs; param->name; param++) {
373 enum fs_parameter_type t = param->type;
375 /* Check that the type is in range */
376 if (t == __fs_param_wasnt_defined ||
377 t >= nr__fs_parameter_type) {
378 pr_err("VALIDATE %s: PARAM[%s] Bad type %u\n",
379 name, param->name, t);
381 } else if (t == fs_param_is_enum) {
382 const struct constant_table *e = param->data;
383 if (!e || !e->name) {
384 pr_err("VALIDATE %s: PARAM[%s] enum with no values\n",
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",
402 #endif /* CONFIG_VALIDATE_FS_PARSER */