blob: ed40ce5742fda1bf69bae6c52791f339fc85138e [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 },
Al Viro34264ae2019-12-16 13:45:41 -050023 { },
David Howells31d921c2018-11-01 23:07:24 +000024};
25
Al Viro34264ae2019-12-16 13:45:41 -050026static const struct constant_table *
27__lookup_constant(const struct constant_table *tbl, const char *name)
28{
29 for ( ; tbl->name; tbl++)
30 if (strcmp(name, tbl->name) == 0)
31 return tbl;
32 return NULL;
33}
34
David Howells31d921c2018-11-01 23:07:24 +000035/**
36 * lookup_constant - Look up a constant by name in an ordered table
37 * @tbl: The table of constants to search.
David Howells31d921c2018-11-01 23:07:24 +000038 * @name: The name to look up.
39 * @not_found: The value to return if the name is not found.
40 */
Al Viro34264ae2019-12-16 13:45:41 -050041int lookup_constant(const struct constant_table *tbl, const char *name, int not_found)
David Howells31d921c2018-11-01 23:07:24 +000042{
Al Viro34264ae2019-12-16 13:45:41 -050043 const struct constant_table *p = __lookup_constant(tbl, name);
David Howells31d921c2018-11-01 23:07:24 +000044
Al Viro34264ae2019-12-16 13:45:41 -050045 return p ? p->value : not_found;
David Howells31d921c2018-11-01 23:07:24 +000046}
Al Viro34264ae2019-12-16 13:45:41 -050047EXPORT_SYMBOL(lookup_constant);
David Howells31d921c2018-11-01 23:07:24 +000048
Al Viro48ce73b2019-12-17 20:03:59 -050049static inline bool is_flag(const struct fs_parameter_spec *p)
50{
Al Viro328de522019-12-18 00:02:31 -050051 return p->type == NULL;
Al Viro48ce73b2019-12-17 20:03:59 -050052}
53
David Howells31d921c2018-11-01 23:07:24 +000054static const struct fs_parameter_spec *fs_lookup_key(
Al Virod7167b12019-09-07 07:23:15 -040055 const struct fs_parameter_spec *desc,
Al Viro48ce73b2019-12-17 20:03:59 -050056 struct fs_parameter *param, bool *negated)
David Howells31d921c2018-11-01 23:07:24 +000057{
Al Viro48ce73b2019-12-17 20:03:59 -050058 const struct fs_parameter_spec *p, *other = NULL;
59 const char *name = param->key;
60 bool want_flag = param->type == fs_value_is_flag;
David Howells31d921c2018-11-01 23:07:24 +000061
Al Viro48ce73b2019-12-17 20:03:59 -050062 *negated = false;
63 for (p = desc; p->name; p++) {
64 if (strcmp(p->name, name) != 0)
65 continue;
66 if (likely(is_flag(p) == want_flag))
David Howells31d921c2018-11-01 23:07:24 +000067 return p;
Al Viro48ce73b2019-12-17 20:03:59 -050068 other = p;
69 }
70 if (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)
74 continue;
75 if (!(p->flags & fs_param_neg_with_no))
76 continue;
77 *negated = true;
78 return p;
79 }
80 }
81 }
82 return other;
David Howells31d921c2018-11-01 23:07:24 +000083}
84
85/*
86 * fs_parse - Parse a filesystem configuration parameter
87 * @fc: 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
91 *
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.
97 *
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.
102 */
Al Viro7f5d3812019-12-20 23:52:55 -0500103int __fs_parse(struct p_log *log,
Al Virod7167b12019-09-07 07:23:15 -0400104 const struct fs_parameter_spec *desc,
David Howells31d921c2018-11-01 23:07:24 +0000105 struct fs_parameter *param,
106 struct fs_parse_result *result)
107{
108 const struct fs_parameter_spec *p;
David Howells31d921c2018-11-01 23:07:24 +0000109
David Howells31d921c2018-11-01 23:07:24 +0000110 result->uint_64 = 0;
111
Al Viro48ce73b2019-12-17 20:03:59 -0500112 p = fs_lookup_key(desc, param, &result->negated);
113 if (!p)
114 return -ENOPARAM;
David Howells31d921c2018-11-01 23:07:24 +0000115
116 if (p->flags & fs_param_deprecated)
Al Viro7f5d3812019-12-20 23:52:55 -0500117 warn_plog(log, "Deprecated parameter '%s'", param->key);
David Howells31d921c2018-11-01 23:07:24 +0000118
David Howells31d921c2018-11-01 23:07:24 +0000119 /* Try to turn the type we were given into the type desired by the
120 * parameter and give an error if we can't.
121 */
Al Viro328de522019-12-18 00:02:31 -0500122 if (is_flag(p)) {
Al Viro0f895892019-12-17 14:15:04 -0500123 if (param->type != fs_value_is_flag)
Al Viro7f5d3812019-12-20 23:52:55 -0500124 return inval_plog(log, "Unexpected value for '%s'",
125 param->key);
Al Viro48ce73b2019-12-17 20:03:59 -0500126 result->boolean = !result->negated;
Al Viro328de522019-12-18 00:02:31 -0500127 } else {
128 int ret = p->type(log, p, param, result);
129 if (ret)
130 return ret;
David Howells31d921c2018-11-01 23:07:24 +0000131 }
David Howells31d921c2018-11-01 23:07:24 +0000132 return p->opt;
David Howells31d921c2018-11-01 23:07:24 +0000133}
Al Viro7f5d3812019-12-20 23:52:55 -0500134EXPORT_SYMBOL(__fs_parse);
135
David Howells31d921c2018-11-01 23:07:24 +0000136/**
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 * @_path: The result of the lookup
142 */
143int fs_lookup_param(struct fs_context *fc,
144 struct fs_parameter *param,
145 bool want_bdev,
146 struct path *_path)
147{
148 struct filename *f;
149 unsigned int flags = 0;
150 bool put_f;
151 int ret;
152
153 switch (param->type) {
154 case fs_value_is_string:
155 f = getname_kernel(param->string);
156 if (IS_ERR(f))
157 return PTR_ERR(f);
158 put_f = true;
159 break;
David Howells31d921c2018-11-01 23:07:24 +0000160 case fs_value_is_filename:
161 f = param->name;
162 put_f = false;
163 break;
164 default:
165 return invalf(fc, "%s: not usable as path", param->key);
166 }
167
168 ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
169 if (ret < 0) {
170 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
171 goto out;
172 }
173
174 if (want_bdev &&
175 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
176 path_put(_path);
177 _path->dentry = NULL;
178 _path->mnt = NULL;
179 errorf(fc, "%s: Non-blockdev passed as '%s'",
180 param->key, f->name);
181 ret = -ENOTBLK;
182 }
183
184out:
185 if (put_f)
186 putname(f);
187 return ret;
188}
189EXPORT_SYMBOL(fs_lookup_param);
190
Luo Jiaxing97383c72020-10-13 16:48:30 -0700191static int fs_param_bad_value(struct p_log *log, struct fs_parameter *param)
Al Viro328de522019-12-18 00:02:31 -0500192{
193 return inval_plog(log, "Bad value for '%s'", param->key);
194}
195
196int fs_param_is_bool(struct p_log *log, const struct fs_parameter_spec *p,
197 struct fs_parameter *param, struct fs_parse_result *result)
198{
199 int b;
200 if (param->type != fs_value_is_string)
201 return fs_param_bad_value(log, param);
Lukas Czerner6abfaaf2021-10-27 16:18:45 +0200202 if (!*param->string && (p->flags & fs_param_can_be_empty))
203 return 0;
Al Viro328de522019-12-18 00:02:31 -0500204 b = lookup_constant(bool_names, param->string, -1);
205 if (b == -1)
206 return fs_param_bad_value(log, param);
207 result->boolean = b;
208 return 0;
209}
210EXPORT_SYMBOL(fs_param_is_bool);
211
212int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p,
213 struct fs_parameter *param, struct fs_parse_result *result)
214{
215 int base = (unsigned long)p->data;
Lukas Czerner6abfaaf2021-10-27 16:18:45 +0200216 if (param->type != fs_value_is_string)
217 return fs_param_bad_value(log, param);
218 if (!*param->string && (p->flags & fs_param_can_be_empty))
219 return 0;
220 if (kstrtouint(param->string, base, &result->uint_32) < 0)
Al Viro328de522019-12-18 00:02:31 -0500221 return fs_param_bad_value(log, param);
222 return 0;
223}
224EXPORT_SYMBOL(fs_param_is_u32);
225
226int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p,
227 struct fs_parameter *param, struct fs_parse_result *result)
228{
Lukas Czerner6abfaaf2021-10-27 16:18:45 +0200229 if (param->type != fs_value_is_string)
230 return fs_param_bad_value(log, param);
231 if (!*param->string && (p->flags & fs_param_can_be_empty))
232 return 0;
233 if (kstrtoint(param->string, 0, &result->int_32) < 0)
Al Viro328de522019-12-18 00:02:31 -0500234 return fs_param_bad_value(log, param);
235 return 0;
236}
237EXPORT_SYMBOL(fs_param_is_s32);
238
239int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p,
240 struct fs_parameter *param, struct fs_parse_result *result)
241{
Lukas Czerner6abfaaf2021-10-27 16:18:45 +0200242 if (param->type != fs_value_is_string)
243 return fs_param_bad_value(log, param);
244 if (!*param->string && (p->flags & fs_param_can_be_empty))
245 return 0;
246 if (kstrtoull(param->string, 0, &result->uint_64) < 0)
Al Viro328de522019-12-18 00:02:31 -0500247 return fs_param_bad_value(log, param);
248 return 0;
249}
250EXPORT_SYMBOL(fs_param_is_u64);
251
252int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p,
253 struct fs_parameter *param, struct fs_parse_result *result)
254{
255 const struct constant_table *c;
256 if (param->type != fs_value_is_string)
257 return fs_param_bad_value(log, param);
Lukas Czerner6abfaaf2021-10-27 16:18:45 +0200258 if (!*param->string && (p->flags & fs_param_can_be_empty))
259 return 0;
Al Viro328de522019-12-18 00:02:31 -0500260 c = __lookup_constant(p->data, param->string);
261 if (!c)
262 return fs_param_bad_value(log, param);
263 result->uint_32 = c->value;
264 return 0;
265}
266EXPORT_SYMBOL(fs_param_is_enum);
267
268int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p,
269 struct fs_parameter *param, struct fs_parse_result *result)
270{
Lukas Czerner6abfaaf2021-10-27 16:18:45 +0200271 if (param->type != fs_value_is_string ||
272 (!*param->string && !(p->flags & fs_param_can_be_empty)))
Al Viro328de522019-12-18 00:02:31 -0500273 return fs_param_bad_value(log, param);
274 return 0;
275}
276EXPORT_SYMBOL(fs_param_is_string);
277
278int fs_param_is_blob(struct p_log *log, const struct fs_parameter_spec *p,
279 struct fs_parameter *param, struct fs_parse_result *result)
280{
281 if (param->type != fs_value_is_blob)
282 return fs_param_bad_value(log, param);
283 return 0;
284}
285EXPORT_SYMBOL(fs_param_is_blob);
286
287int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p,
288 struct fs_parameter *param, struct fs_parse_result *result)
289{
290 switch (param->type) {
291 case fs_value_is_string:
Lukas Czerner6abfaaf2021-10-27 16:18:45 +0200292 if ((!*param->string && !(p->flags & fs_param_can_be_empty)) ||
293 kstrtouint(param->string, 0, &result->uint_32) < 0)
Al Viro328de522019-12-18 00:02:31 -0500294 break;
295 if (result->uint_32 <= INT_MAX)
296 return 0;
297 break;
298 case fs_value_is_file:
299 result->uint_32 = param->dirfd;
300 if (result->uint_32 <= INT_MAX)
301 return 0;
302 break;
303 default:
304 break;
305 }
306 return fs_param_bad_value(log, param);
307}
308EXPORT_SYMBOL(fs_param_is_fd);
309
310int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p,
311 struct fs_parameter *param, struct fs_parse_result *result)
312{
313 return 0;
314}
315EXPORT_SYMBOL(fs_param_is_blockdev);
316
317int fs_param_is_path(struct p_log *log, const struct fs_parameter_spec *p,
318 struct fs_parameter *param, struct fs_parse_result *result)
319{
320 return 0;
321}
322EXPORT_SYMBOL(fs_param_is_path);
323
David Howells31d921c2018-11-01 23:07:24 +0000324#ifdef CONFIG_VALIDATE_FS_PARSER
325/**
326 * validate_constant_table - Validate a constant table
David Howells31d921c2018-11-01 23:07:24 +0000327 * @tbl: The constant table to validate.
328 * @tbl_size: The size of the table.
329 * @low: The lowest permissible value.
330 * @high: The highest permissible value.
331 * @special: One special permissible value outside of the range.
332 */
333bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
334 int low, int high, int special)
335{
336 size_t i;
337 bool good = true;
338
339 if (tbl_size == 0) {
340 pr_warn("VALIDATE C-TBL: Empty\n");
341 return true;
342 }
343
344 for (i = 0; i < tbl_size; i++) {
345 if (!tbl[i].name) {
346 pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
347 good = false;
348 } else if (i > 0 && tbl[i - 1].name) {
349 int c = strcmp(tbl[i-1].name, tbl[i].name);
350
351 if (c == 0) {
352 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
353 i, tbl[i].name);
354 good = false;
355 }
356 if (c > 0) {
357 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
358 i, tbl[i-1].name, tbl[i].name);
359 good = false;
360 }
361 }
362
363 if (tbl[i].value != special &&
364 (tbl[i].value < low || tbl[i].value > high)) {
365 pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
366 i, tbl[i].name, tbl[i].value, low, high);
367 good = false;
368 }
369 }
370
371 return good;
372}
373
374/**
375 * fs_validate_description - Validate a parameter description
Randy Dunlap21ae3ad2021-04-29 22:54:17 -0700376 * @name: The parameter name to search for.
David Howells31d921c2018-11-01 23:07:24 +0000377 * @desc: The parameter description to validate.
378 */
Eric Sandeen96cafb92019-12-06 10:45:01 -0600379bool fs_validate_description(const char *name,
Al Virod7167b12019-09-07 07:23:15 -0400380 const struct fs_parameter_spec *desc)
David Howells31d921c2018-11-01 23:07:24 +0000381{
382 const struct fs_parameter_spec *param, *p2;
Al Viro2710c957a2019-09-06 22:12:08 -0400383 bool good = true;
David Howells31d921c2018-11-01 23:07:24 +0000384
Al Virod7167b12019-09-07 07:23:15 -0400385 for (param = desc; param->name; param++) {
Al Virod7167b12019-09-07 07:23:15 -0400386 /* Check for duplicate parameter names */
387 for (p2 = desc; p2 < param; p2++) {
388 if (strcmp(param->name, p2->name) == 0) {
Al Viro48ce73b2019-12-17 20:03:59 -0500389 if (is_flag(param) != is_flag(p2))
390 continue;
Al Virod7167b12019-09-07 07:23:15 -0400391 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
392 name, param->name);
393 good = false;
David Howells31d921c2018-11-01 23:07:24 +0000394 }
395 }
David Howells31d921c2018-11-01 23:07:24 +0000396 }
David Howells31d921c2018-11-01 23:07:24 +0000397 return good;
398}
399#endif /* CONFIG_VALIDATE_FS_PARSER */