blob: 07ae041b83a8bc918e10dd619d4c5b8812a62167 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells31d921c2018-11-01 23:07:24 +00002/* Filesystem parameter parser.
3 *
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells31d921c2018-11-01 23:07:24 +00006 */
7
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>
14#include "internal.h"
15
16static const struct constant_table bool_names[] = {
17 { "0", false },
18 { "1", true },
19 { "false", false },
20 { "no", false },
21 { "true", true },
22 { "yes", true },
23};
24
25/**
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.
31 */
32int __lookup_constant(const struct constant_table *tbl, size_t tbl_size,
33 const char *name, int not_found)
34{
35 unsigned int i;
36
37 for (i = 0; i < tbl_size; i++)
38 if (strcmp(name, tbl[i].name) == 0)
39 return tbl[i].value;
40
41 return not_found;
42}
43EXPORT_SYMBOL(__lookup_constant);
44
45static const struct fs_parameter_spec *fs_lookup_key(
46 const struct fs_parameter_description *desc,
47 const char *name)
48{
49 const struct fs_parameter_spec *p;
50
51 if (!desc->specs)
52 return NULL;
53
54 for (p = desc->specs; p->name; p++)
55 if (strcmp(p->name, name) == 0)
56 return p;
57
58 return NULL;
59}
60
61/*
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
67 *
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.
73 *
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.
78 */
79int fs_parse(struct fs_context *fc,
80 const struct fs_parameter_description *desc,
81 struct fs_parameter *param,
82 struct fs_parse_result *result)
83{
84 const struct fs_parameter_spec *p;
85 const struct fs_parameter_enum *e;
86 int ret = -ENOPARAM, b;
87
David Howells31d921c2018-11-01 23:07:24 +000088 result->negated = false;
89 result->uint_64 = 0;
90
91 p = fs_lookup_key(desc, param->key);
92 if (!p) {
93 /* If we didn't find something that looks like "noxxx", see if
94 * "xxx" takes the "no"-form negative - but only if there
95 * wasn't an value.
96 */
Al Viro0f895892019-12-17 14:15:04 -050097 if (param->type != fs_value_is_flag)
David Howells31d921c2018-11-01 23:07:24 +000098 goto unknown_parameter;
99 if (param->key[0] != 'n' || param->key[1] != 'o' || !param->key[2])
100 goto unknown_parameter;
101
102 p = fs_lookup_key(desc, param->key + 2);
103 if (!p)
104 goto unknown_parameter;
105 if (!(p->flags & fs_param_neg_with_no))
106 goto unknown_parameter;
107 result->boolean = false;
108 result->negated = true;
109 }
110
111 if (p->flags & fs_param_deprecated)
112 warnf(fc, "%s: Deprecated parameter '%s'",
113 desc->name, param->key);
114
115 if (result->negated)
116 goto okay;
117
118 /* Certain parameter types only take a string and convert it. */
119 switch (p->type) {
120 case __fs_param_wasnt_defined:
121 return -EINVAL;
122 case fs_param_is_u32:
123 case fs_param_is_u32_octal:
124 case fs_param_is_u32_hex:
125 case fs_param_is_s32:
126 case fs_param_is_u64:
127 case fs_param_is_enum:
128 case fs_param_is_string:
Al Viro0f895892019-12-17 14:15:04 -0500129 if (param->type == fs_value_is_string) {
130 if (p->flags & fs_param_v_optional)
131 break;
132 if (!*param->string)
133 goto bad_value;
134 break;
135 }
136 if (param->type == fs_value_is_flag) {
David Howells31d921c2018-11-01 23:07:24 +0000137 if (p->flags & fs_param_v_optional)
138 goto okay;
David Howells31d921c2018-11-01 23:07:24 +0000139 }
Al Viro0f895892019-12-17 14:15:04 -0500140 goto bad_value;
David Howells31d921c2018-11-01 23:07:24 +0000141 default:
142 break;
143 }
144
145 /* Try to turn the type we were given into the type desired by the
146 * parameter and give an error if we can't.
147 */
148 switch (p->type) {
149 case fs_param_is_flag:
Al Viro0f895892019-12-17 14:15:04 -0500150 if (param->type != fs_value_is_flag)
David Howells31d921c2018-11-01 23:07:24 +0000151 return invalf(fc, "%s: Unexpected value for '%s'",
152 desc->name, param->key);
153 result->boolean = true;
154 goto okay;
155
156 case fs_param_is_bool:
157 switch (param->type) {
158 case fs_value_is_flag:
159 result->boolean = true;
160 goto okay;
161 case fs_value_is_string:
162 if (param->size == 0) {
163 result->boolean = true;
164 goto okay;
165 }
166 b = lookup_constant(bool_names, param->string, -1);
167 if (b == -1)
168 goto bad_value;
169 result->boolean = b;
170 goto okay;
171 default:
172 goto bad_value;
173 }
174
175 case fs_param_is_u32:
176 ret = kstrtouint(param->string, 0, &result->uint_32);
177 goto maybe_okay;
178 case fs_param_is_u32_octal:
179 ret = kstrtouint(param->string, 8, &result->uint_32);
180 goto maybe_okay;
181 case fs_param_is_u32_hex:
182 ret = kstrtouint(param->string, 16, &result->uint_32);
183 goto maybe_okay;
184 case fs_param_is_s32:
185 ret = kstrtoint(param->string, 0, &result->int_32);
186 goto maybe_okay;
187 case fs_param_is_u64:
188 ret = kstrtoull(param->string, 0, &result->uint_64);
189 goto maybe_okay;
190
191 case fs_param_is_enum:
192 for (e = desc->enums; e->name[0]; e++) {
193 if (e->opt == p->opt &&
194 strcmp(e->name, param->string) == 0) {
195 result->uint_32 = e->value;
196 goto okay;
197 }
198 }
199 goto bad_value;
200
201 case fs_param_is_string:
202 goto okay;
203 case fs_param_is_blob:
204 if (param->type != fs_value_is_blob)
205 goto bad_value;
206 goto okay;
207
208 case fs_param_is_fd: {
David Howells74983ac2019-03-25 16:38:31 +0000209 switch (param->type) {
210 case fs_value_is_string:
David Howells74983ac2019-03-25 16:38:31 +0000211 ret = kstrtouint(param->string, 0, &result->uint_32);
212 break;
213 case fs_value_is_file:
214 result->uint_32 = param->dirfd;
215 ret = 0;
216 default:
David Howells31d921c2018-11-01 23:07:24 +0000217 goto bad_value;
David Howells74983ac2019-03-25 16:38:31 +0000218 }
219
220 if (result->uint_32 > INT_MAX)
221 goto bad_value;
222 goto maybe_okay;
David Howells31d921c2018-11-01 23:07:24 +0000223 }
224
225 case fs_param_is_blockdev:
226 case fs_param_is_path:
227 goto okay;
228 default:
229 BUG();
230 }
231
232maybe_okay:
233 if (ret < 0)
234 goto bad_value;
235okay:
236 return p->opt;
237
238bad_value:
239 return invalf(fc, "%s: Bad value for '%s'", desc->name, param->key);
240unknown_parameter:
241 return -ENOPARAM;
242}
243EXPORT_SYMBOL(fs_parse);
244
245/**
246 * fs_lookup_param - Look up a path referred to by a parameter
247 * @fc: The filesystem context to log errors through.
248 * @param: The parameter.
249 * @want_bdev: T if want a blockdev
250 * @_path: The result of the lookup
251 */
252int fs_lookup_param(struct fs_context *fc,
253 struct fs_parameter *param,
254 bool want_bdev,
255 struct path *_path)
256{
257 struct filename *f;
258 unsigned int flags = 0;
259 bool put_f;
260 int ret;
261
262 switch (param->type) {
263 case fs_value_is_string:
264 f = getname_kernel(param->string);
265 if (IS_ERR(f))
266 return PTR_ERR(f);
267 put_f = true;
268 break;
269 case fs_value_is_filename_empty:
270 flags = LOOKUP_EMPTY;
271 /* Fall through */
272 case fs_value_is_filename:
273 f = param->name;
274 put_f = false;
275 break;
276 default:
277 return invalf(fc, "%s: not usable as path", param->key);
278 }
279
David Howells7cdfa442019-03-25 16:38:22 +0000280 f->refcnt++; /* filename_lookup() drops our ref. */
David Howells31d921c2018-11-01 23:07:24 +0000281 ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
282 if (ret < 0) {
283 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
284 goto out;
285 }
286
287 if (want_bdev &&
288 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
289 path_put(_path);
290 _path->dentry = NULL;
291 _path->mnt = NULL;
292 errorf(fc, "%s: Non-blockdev passed as '%s'",
293 param->key, f->name);
294 ret = -ENOTBLK;
295 }
296
297out:
298 if (put_f)
299 putname(f);
300 return ret;
301}
302EXPORT_SYMBOL(fs_lookup_param);
303
304#ifdef CONFIG_VALIDATE_FS_PARSER
305/**
306 * validate_constant_table - Validate a constant table
307 * @name: Name to use in reporting
308 * @tbl: The constant table to validate.
309 * @tbl_size: The size of the table.
310 * @low: The lowest permissible value.
311 * @high: The highest permissible value.
312 * @special: One special permissible value outside of the range.
313 */
314bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
315 int low, int high, int special)
316{
317 size_t i;
318 bool good = true;
319
320 if (tbl_size == 0) {
321 pr_warn("VALIDATE C-TBL: Empty\n");
322 return true;
323 }
324
325 for (i = 0; i < tbl_size; i++) {
326 if (!tbl[i].name) {
327 pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
328 good = false;
329 } else if (i > 0 && tbl[i - 1].name) {
330 int c = strcmp(tbl[i-1].name, tbl[i].name);
331
332 if (c == 0) {
333 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
334 i, tbl[i].name);
335 good = false;
336 }
337 if (c > 0) {
338 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
339 i, tbl[i-1].name, tbl[i].name);
340 good = false;
341 }
342 }
343
344 if (tbl[i].value != special &&
345 (tbl[i].value < low || tbl[i].value > high)) {
346 pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
347 i, tbl[i].name, tbl[i].value, low, high);
348 good = false;
349 }
350 }
351
352 return good;
353}
354
355/**
356 * fs_validate_description - Validate a parameter description
357 * @desc: The parameter description to validate.
358 */
359bool fs_validate_description(const struct fs_parameter_description *desc)
360{
361 const struct fs_parameter_spec *param, *p2;
362 const struct fs_parameter_enum *e;
363 const char *name = desc->name;
364 unsigned int nr_params = 0;
365 bool good = true, enums = false;
366
367 pr_notice("*** VALIDATE %s ***\n", name);
368
369 if (!name[0]) {
370 pr_err("VALIDATE Parser: No name\n");
371 name = "Unknown";
372 good = false;
373 }
374
375 if (desc->specs) {
376 for (param = desc->specs; param->name; param++) {
377 enum fs_parameter_type t = param->type;
378
379 /* Check that the type is in range */
380 if (t == __fs_param_wasnt_defined ||
381 t >= nr__fs_parameter_type) {
382 pr_err("VALIDATE %s: PARAM[%s] Bad type %u\n",
383 name, param->name, t);
384 good = false;
385 } else if (t == fs_param_is_enum) {
386 enums = true;
387 }
388
389 /* Check for duplicate parameter names */
390 for (p2 = desc->specs; p2 < param; p2++) {
391 if (strcmp(param->name, p2->name) == 0) {
392 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
393 name, param->name);
394 good = false;
395 }
396 }
397 }
398
399 nr_params = param - desc->specs;
400 }
401
402 if (desc->enums) {
403 if (!nr_params) {
404 pr_err("VALIDATE %s: Enum table but no parameters\n",
405 name);
406 good = false;
407 goto no_enums;
408 }
409 if (!enums) {
410 pr_err("VALIDATE %s: Enum table but no enum-type values\n",
411 name);
412 good = false;
413 goto no_enums;
414 }
415
416 for (e = desc->enums; e->name[0]; e++) {
417 /* Check that all entries in the enum table have at
418 * least one parameter that uses them.
419 */
420 for (param = desc->specs; param->name; param++) {
421 if (param->opt == e->opt &&
422 param->type != fs_param_is_enum) {
Randy Dunlap26203272019-03-28 20:44:36 -0700423 pr_err("VALIDATE %s: e[%tu] enum val for %s\n",
David Howells31d921c2018-11-01 23:07:24 +0000424 name, e - desc->enums, param->name);
425 good = false;
426 }
427 }
428 }
429
430 /* Check that all enum-type parameters have at least one enum
431 * value in the enum table.
432 */
433 for (param = desc->specs; param->name; param++) {
434 if (param->type != fs_param_is_enum)
435 continue;
436 for (e = desc->enums; e->name[0]; e++)
437 if (e->opt == param->opt)
438 break;
439 if (!e->name[0]) {
440 pr_err("VALIDATE %s: PARAM[%s] enum with no values\n",
441 name, param->name);
442 good = false;
443 }
444 }
445 } else {
446 if (enums) {
447 pr_err("VALIDATE %s: enum-type values, but no enum table\n",
448 name);
449 good = false;
450 goto no_enums;
451 }
452 }
453
454no_enums:
455 return good;
456}
457#endif /* CONFIG_VALIDATE_FS_PARSER */