1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Filesystem parameter parser.
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
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 inline bool is_flag(const struct fs_parameter_spec *p)
51 return p->type == NULL;
54 static const struct fs_parameter_spec *fs_lookup_key(
55 const struct fs_parameter_spec *desc,
56 struct fs_parameter *param, bool *negated)
58 const struct fs_parameter_spec *p, *other = NULL;
59 const char *name = param->key;
60 bool want_flag = param->type == fs_value_is_flag;
63 for (p = desc; p->name; p++) {
64 if (strcmp(p->name, name) != 0)
66 if (likely(is_flag(p) == want_flag))
71 if (name[0] == 'n' && name[1] == 'o' && name[2]) {
72 for (p = desc; p->name; p++) {
73 if (strcmp(p->name, name + 2) != 0)
75 if (!(p->flags & fs_param_neg_with_no))
86 * __fs_parse - Parse a filesystem configuration parameter
87 * @log: The filesystem context to log errors through.
88 * @desc: The parameter description to use.
89 * @param: The parameter.
90 * @result: Where to place the result of the parse
92 * Parse a filesystem configuration parameter and attempt a conversion for a
93 * simple parameter for which this is requested. If successful, the determined
94 * parameter ID is placed into @result->key, the desired type is indicated in
95 * @result->t and any converted value is placed into an appropriate member of
96 * the union in @result.
98 * The function returns the parameter number if the parameter was matched,
99 * -ENOPARAM if it wasn't matched and @desc->ignore_unknown indicated that
100 * unknown parameters are okay and -EINVAL if there was a conversion issue or
101 * the parameter wasn't recognised and unknowns aren't okay.
103 int __fs_parse(struct p_log *log,
104 const struct fs_parameter_spec *desc,
105 struct fs_parameter *param,
106 struct fs_parse_result *result)
108 const struct fs_parameter_spec *p;
112 p = fs_lookup_key(desc, param, &result->negated);
116 if (p->flags & fs_param_deprecated)
117 warn_plog(log, "Deprecated parameter '%s'", param->key);
119 /* Try to turn the type we were given into the type desired by the
120 * parameter and give an error if we can't.
123 if (param->type != fs_value_is_flag)
124 return inval_plog(log, "Unexpected value for '%s'",
126 result->boolean = !result->negated;
128 int ret = p->type(log, p, param, result);
134 EXPORT_SYMBOL(__fs_parse);
137 * fs_lookup_param - Look up a path referred to by a parameter
138 * @fc: The filesystem context to log errors through.
139 * @param: The parameter.
140 * @want_bdev: T if want a blockdev
141 * @flags: Pathwalk flags passed to filename_lookup()
142 * @_path: The result of the lookup
144 int fs_lookup_param(struct fs_context *fc,
145 struct fs_parameter *param,
154 switch (param->type) {
155 case fs_value_is_string:
156 f = getname_kernel(param->string);
159 param->dirfd = AT_FDCWD;
162 case fs_value_is_filename:
167 return invalf(fc, "%s: not usable as path", param->key);
170 ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
172 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
177 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
179 _path->dentry = NULL;
181 errorf(fc, "%s: Non-blockdev passed as '%s'",
182 param->key, f->name);
191 EXPORT_SYMBOL(fs_lookup_param);
193 static int fs_param_bad_value(struct p_log *log, struct fs_parameter *param)
195 return inval_plog(log, "Bad value for '%s'", param->key);
198 int fs_param_is_bool(struct p_log *log, const struct fs_parameter_spec *p,
199 struct fs_parameter *param, struct fs_parse_result *result)
202 if (param->type != fs_value_is_string)
203 return fs_param_bad_value(log, param);
204 if (!*param->string && (p->flags & fs_param_can_be_empty))
206 b = lookup_constant(bool_names, param->string, -1);
208 return fs_param_bad_value(log, param);
212 EXPORT_SYMBOL(fs_param_is_bool);
214 int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p,
215 struct fs_parameter *param, struct fs_parse_result *result)
217 int base = (unsigned long)p->data;
218 if (param->type != fs_value_is_string)
219 return fs_param_bad_value(log, param);
220 if (!*param->string && (p->flags & fs_param_can_be_empty))
222 if (kstrtouint(param->string, base, &result->uint_32) < 0)
223 return fs_param_bad_value(log, param);
226 EXPORT_SYMBOL(fs_param_is_u32);
228 int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p,
229 struct fs_parameter *param, struct fs_parse_result *result)
231 if (param->type != fs_value_is_string)
232 return fs_param_bad_value(log, param);
233 if (!*param->string && (p->flags & fs_param_can_be_empty))
235 if (kstrtoint(param->string, 0, &result->int_32) < 0)
236 return fs_param_bad_value(log, param);
239 EXPORT_SYMBOL(fs_param_is_s32);
241 int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p,
242 struct fs_parameter *param, struct fs_parse_result *result)
244 if (param->type != fs_value_is_string)
245 return fs_param_bad_value(log, param);
246 if (!*param->string && (p->flags & fs_param_can_be_empty))
248 if (kstrtoull(param->string, 0, &result->uint_64) < 0)
249 return fs_param_bad_value(log, param);
252 EXPORT_SYMBOL(fs_param_is_u64);
254 int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p,
255 struct fs_parameter *param, struct fs_parse_result *result)
257 const struct constant_table *c;
258 if (param->type != fs_value_is_string)
259 return fs_param_bad_value(log, param);
260 if (!*param->string && (p->flags & fs_param_can_be_empty))
262 c = __lookup_constant(p->data, param->string);
264 return fs_param_bad_value(log, param);
265 result->uint_32 = c->value;
268 EXPORT_SYMBOL(fs_param_is_enum);
270 int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p,
271 struct fs_parameter *param, struct fs_parse_result *result)
273 if (param->type != fs_value_is_string ||
274 (!*param->string && !(p->flags & fs_param_can_be_empty)))
275 return fs_param_bad_value(log, param);
278 EXPORT_SYMBOL(fs_param_is_string);
280 int fs_param_is_blob(struct p_log *log, const struct fs_parameter_spec *p,
281 struct fs_parameter *param, struct fs_parse_result *result)
283 if (param->type != fs_value_is_blob)
284 return fs_param_bad_value(log, param);
287 EXPORT_SYMBOL(fs_param_is_blob);
289 int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p,
290 struct fs_parameter *param, struct fs_parse_result *result)
292 switch (param->type) {
293 case fs_value_is_string:
294 if ((!*param->string && !(p->flags & fs_param_can_be_empty)) ||
295 kstrtouint(param->string, 0, &result->uint_32) < 0)
297 if (result->uint_32 <= INT_MAX)
300 case fs_value_is_file:
301 result->uint_32 = param->dirfd;
302 if (result->uint_32 <= INT_MAX)
308 return fs_param_bad_value(log, param);
310 EXPORT_SYMBOL(fs_param_is_fd);
312 int fs_param_is_file_or_string(struct p_log *log,
313 const struct fs_parameter_spec *p,
314 struct fs_parameter *param,
315 struct fs_parse_result *result)
317 switch (param->type) {
318 case fs_value_is_string:
319 return fs_param_is_string(log, p, param, result);
320 case fs_value_is_file:
321 result->uint_32 = param->dirfd;
322 if (result->uint_32 <= INT_MAX)
328 return fs_param_bad_value(log, param);
330 EXPORT_SYMBOL(fs_param_is_file_or_string);
332 int fs_param_is_uid(struct p_log *log, const struct fs_parameter_spec *p,
333 struct fs_parameter *param, struct fs_parse_result *result)
337 if (fs_param_is_u32(log, p, param, result) != 0)
338 return fs_param_bad_value(log, param);
340 uid = make_kuid(current_user_ns(), result->uint_32);
342 return inval_plog(log, "Invalid uid '%s'", param->string);
347 EXPORT_SYMBOL(fs_param_is_uid);
349 int fs_param_is_gid(struct p_log *log, const struct fs_parameter_spec *p,
350 struct fs_parameter *param, struct fs_parse_result *result)
354 if (fs_param_is_u32(log, p, param, result) != 0)
355 return fs_param_bad_value(log, param);
357 gid = make_kgid(current_user_ns(), result->uint_32);
359 return inval_plog(log, "Invalid gid '%s'", param->string);
364 EXPORT_SYMBOL(fs_param_is_gid);
366 int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p,
367 struct fs_parameter *param, struct fs_parse_result *result)
371 EXPORT_SYMBOL(fs_param_is_blockdev);
373 int fs_param_is_path(struct p_log *log, const struct fs_parameter_spec *p,
374 struct fs_parameter *param, struct fs_parse_result *result)
378 EXPORT_SYMBOL(fs_param_is_path);
380 #ifdef CONFIG_VALIDATE_FS_PARSER
382 * validate_constant_table - Validate a constant table
383 * @tbl: The constant table to validate.
384 * @tbl_size: The size of the table.
385 * @low: The lowest permissible value.
386 * @high: The highest permissible value.
387 * @special: One special permissible value outside of the range.
389 bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
390 int low, int high, int special)
396 pr_warn("VALIDATE C-TBL: Empty\n");
400 for (i = 0; i < tbl_size; i++) {
402 pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
404 } else if (i > 0 && tbl[i - 1].name) {
405 int c = strcmp(tbl[i-1].name, tbl[i].name);
408 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
413 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
414 i, tbl[i-1].name, tbl[i].name);
419 if (tbl[i].value != special &&
420 (tbl[i].value < low || tbl[i].value > high)) {
421 pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
422 i, tbl[i].name, tbl[i].value, low, high);
431 * fs_validate_description - Validate a parameter description
432 * @name: The parameter name to search for.
433 * @desc: The parameter description to validate.
435 bool fs_validate_description(const char *name,
436 const struct fs_parameter_spec *desc)
438 const struct fs_parameter_spec *param, *p2;
441 for (param = desc; param->name; param++) {
442 /* Check for duplicate parameter names */
443 for (p2 = desc; p2 < param; p2++) {
444 if (strcmp(param->name, p2->name) == 0) {
445 if (is_flag(param) != is_flag(p2))
447 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
455 #endif /* CONFIG_VALIDATE_FS_PARSER */