]> Git Repo - linux.git/blame - fs/fs_parser.c
netfs: Fix interaction of streaming writes with zero-point tracker
[linux.git] / fs / fs_parser.c
CommitLineData
b4d0d230 1// SPDX-License-Identifier: GPL-2.0-or-later
31d921c7
DH
2/* Filesystem parameter parser.
3 *
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells ([email protected])
31d921c7
DH
6 */
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 },
34264ae3 23 { },
31d921c7
DH
24};
25
34264ae3
AV
26static 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
31d921c7
DH
35/**
36 * lookup_constant - Look up a constant by name in an ordered table
37 * @tbl: The table of constants to search.
31d921c7
DH
38 * @name: The name to look up.
39 * @not_found: The value to return if the name is not found.
40 */
34264ae3 41int lookup_constant(const struct constant_table *tbl, const char *name, int not_found)
31d921c7 42{
34264ae3 43 const struct constant_table *p = __lookup_constant(tbl, name);
31d921c7 44
34264ae3 45 return p ? p->value : not_found;
31d921c7 46}
34264ae3 47EXPORT_SYMBOL(lookup_constant);
31d921c7 48
48ce73b1
AV
49static inline bool is_flag(const struct fs_parameter_spec *p)
50{
328de528 51 return p->type == NULL;
48ce73b1
AV
52}
53
31d921c7 54static const struct fs_parameter_spec *fs_lookup_key(
d7167b14 55 const struct fs_parameter_spec *desc,
48ce73b1 56 struct fs_parameter *param, bool *negated)
31d921c7 57{
48ce73b1
AV
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;
61
62 *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))
31d921c7 67 return p;
48ce73b1
AV
68 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;
31d921c7
DH
83}
84
85/*
57c69067
CH
86 * __fs_parse - Parse a filesystem configuration parameter
87 * @log: The filesystem context to log errors through.
31d921c7
DH
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 */
7f5d3814 103int __fs_parse(struct p_log *log,
d7167b14 104 const struct fs_parameter_spec *desc,
31d921c7
DH
105 struct fs_parameter *param,
106 struct fs_parse_result *result)
107{
108 const struct fs_parameter_spec *p;
31d921c7 109
31d921c7
DH
110 result->uint_64 = 0;
111
48ce73b1
AV
112 p = fs_lookup_key(desc, param, &result->negated);
113 if (!p)
114 return -ENOPARAM;
31d921c7
DH
115
116 if (p->flags & fs_param_deprecated)
7f5d3814 117 warn_plog(log, "Deprecated parameter '%s'", param->key);
31d921c7 118
31d921c7
DH
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.
121 */
328de528 122 if (is_flag(p)) {
0f89589a 123 if (param->type != fs_value_is_flag)
7f5d3814
AV
124 return inval_plog(log, "Unexpected value for '%s'",
125 param->key);
48ce73b1 126 result->boolean = !result->negated;
328de528
AV
127 } else {
128 int ret = p->type(log, p, param, result);
129 if (ret)
130 return ret;
31d921c7 131 }
31d921c7 132 return p->opt;
31d921c7 133}
7f5d3814
AV
134EXPORT_SYMBOL(__fs_parse);
135
31d921c7
DH
136/**
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
e3ea75ee 141 * @flags: Pathwalk flags passed to filename_lookup()
31d921c7
DH
142 * @_path: The result of the lookup
143 */
144int fs_lookup_param(struct fs_context *fc,
145 struct fs_parameter *param,
146 bool want_bdev,
e3ea75ee 147 unsigned int flags,
31d921c7
DH
148 struct path *_path)
149{
150 struct filename *f;
31d921c7
DH
151 bool put_f;
152 int ret;
153
154 switch (param->type) {
155 case fs_value_is_string:
156 f = getname_kernel(param->string);
157 if (IS_ERR(f))
158 return PTR_ERR(f);
159 put_f = true;
160 break;
31d921c7
DH
161 case fs_value_is_filename:
162 f = param->name;
163 put_f = false;
164 break;
165 default:
166 return invalf(fc, "%s: not usable as path", param->key);
167 }
168
169 ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
170 if (ret < 0) {
171 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
172 goto out;
173 }
174
175 if (want_bdev &&
176 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
177 path_put(_path);
178 _path->dentry = NULL;
179 _path->mnt = NULL;
180 errorf(fc, "%s: Non-blockdev passed as '%s'",
181 param->key, f->name);
182 ret = -ENOTBLK;
183 }
184
185out:
186 if (put_f)
187 putname(f);
188 return ret;
189}
190EXPORT_SYMBOL(fs_lookup_param);
191
97383c74 192static int fs_param_bad_value(struct p_log *log, struct fs_parameter *param)
328de528
AV
193{
194 return inval_plog(log, "Bad value for '%s'", param->key);
195}
196
197int fs_param_is_bool(struct p_log *log, const struct fs_parameter_spec *p,
198 struct fs_parameter *param, struct fs_parse_result *result)
199{
200 int b;
201 if (param->type != fs_value_is_string)
202 return fs_param_bad_value(log, param);
6abfaaf1
LC
203 if (!*param->string && (p->flags & fs_param_can_be_empty))
204 return 0;
328de528
AV
205 b = lookup_constant(bool_names, param->string, -1);
206 if (b == -1)
207 return fs_param_bad_value(log, param);
208 result->boolean = b;
209 return 0;
210}
211EXPORT_SYMBOL(fs_param_is_bool);
212
213int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p,
214 struct fs_parameter *param, struct fs_parse_result *result)
215{
216 int base = (unsigned long)p->data;
6abfaaf1
LC
217 if (param->type != fs_value_is_string)
218 return fs_param_bad_value(log, param);
219 if (!*param->string && (p->flags & fs_param_can_be_empty))
220 return 0;
221 if (kstrtouint(param->string, base, &result->uint_32) < 0)
328de528
AV
222 return fs_param_bad_value(log, param);
223 return 0;
224}
225EXPORT_SYMBOL(fs_param_is_u32);
226
227int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p,
228 struct fs_parameter *param, struct fs_parse_result *result)
229{
6abfaaf1
LC
230 if (param->type != fs_value_is_string)
231 return fs_param_bad_value(log, param);
232 if (!*param->string && (p->flags & fs_param_can_be_empty))
233 return 0;
234 if (kstrtoint(param->string, 0, &result->int_32) < 0)
328de528
AV
235 return fs_param_bad_value(log, param);
236 return 0;
237}
238EXPORT_SYMBOL(fs_param_is_s32);
239
240int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p,
241 struct fs_parameter *param, struct fs_parse_result *result)
242{
6abfaaf1
LC
243 if (param->type != fs_value_is_string)
244 return fs_param_bad_value(log, param);
245 if (!*param->string && (p->flags & fs_param_can_be_empty))
246 return 0;
247 if (kstrtoull(param->string, 0, &result->uint_64) < 0)
328de528
AV
248 return fs_param_bad_value(log, param);
249 return 0;
250}
251EXPORT_SYMBOL(fs_param_is_u64);
252
253int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p,
254 struct fs_parameter *param, struct fs_parse_result *result)
255{
256 const struct constant_table *c;
257 if (param->type != fs_value_is_string)
258 return fs_param_bad_value(log, param);
6abfaaf1
LC
259 if (!*param->string && (p->flags & fs_param_can_be_empty))
260 return 0;
328de528
AV
261 c = __lookup_constant(p->data, param->string);
262 if (!c)
263 return fs_param_bad_value(log, param);
264 result->uint_32 = c->value;
265 return 0;
266}
267EXPORT_SYMBOL(fs_param_is_enum);
268
269int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p,
270 struct fs_parameter *param, struct fs_parse_result *result)
271{
6abfaaf1
LC
272 if (param->type != fs_value_is_string ||
273 (!*param->string && !(p->flags & fs_param_can_be_empty)))
328de528
AV
274 return fs_param_bad_value(log, param);
275 return 0;
276}
277EXPORT_SYMBOL(fs_param_is_string);
278
279int fs_param_is_blob(struct p_log *log, const struct fs_parameter_spec *p,
280 struct fs_parameter *param, struct fs_parse_result *result)
281{
282 if (param->type != fs_value_is_blob)
283 return fs_param_bad_value(log, param);
284 return 0;
285}
286EXPORT_SYMBOL(fs_param_is_blob);
287
288int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p,
289 struct fs_parameter *param, struct fs_parse_result *result)
290{
291 switch (param->type) {
292 case fs_value_is_string:
6abfaaf1
LC
293 if ((!*param->string && !(p->flags & fs_param_can_be_empty)) ||
294 kstrtouint(param->string, 0, &result->uint_32) < 0)
328de528
AV
295 break;
296 if (result->uint_32 <= INT_MAX)
297 return 0;
298 break;
299 case fs_value_is_file:
300 result->uint_32 = param->dirfd;
301 if (result->uint_32 <= INT_MAX)
302 return 0;
303 break;
304 default:
305 break;
306 }
307 return fs_param_bad_value(log, param);
308}
309EXPORT_SYMBOL(fs_param_is_fd);
310
9f111059
ES
311int fs_param_is_uid(struct p_log *log, const struct fs_parameter_spec *p,
312 struct fs_parameter *param, struct fs_parse_result *result)
313{
314 kuid_t uid;
315
316 if (fs_param_is_u32(log, p, param, result) != 0)
317 return fs_param_bad_value(log, param);
318
319 uid = make_kuid(current_user_ns(), result->uint_32);
320 if (!uid_valid(uid))
321 return inval_plog(log, "Invalid uid '%s'", param->string);
322
323 result->uid = uid;
324 return 0;
325}
326EXPORT_SYMBOL(fs_param_is_uid);
327
328int fs_param_is_gid(struct p_log *log, const struct fs_parameter_spec *p,
329 struct fs_parameter *param, struct fs_parse_result *result)
330{
331 kgid_t gid;
332
333 if (fs_param_is_u32(log, p, param, result) != 0)
334 return fs_param_bad_value(log, param);
335
336 gid = make_kgid(current_user_ns(), result->uint_32);
337 if (!gid_valid(gid))
338 return inval_plog(log, "Invalid gid '%s'", param->string);
339
340 result->gid = gid;
341 return 0;
342}
343EXPORT_SYMBOL(fs_param_is_gid);
344
328de528
AV
345int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p,
346 struct fs_parameter *param, struct fs_parse_result *result)
347{
348 return 0;
349}
350EXPORT_SYMBOL(fs_param_is_blockdev);
351
352int fs_param_is_path(struct p_log *log, const struct fs_parameter_spec *p,
353 struct fs_parameter *param, struct fs_parse_result *result)
354{
355 return 0;
356}
357EXPORT_SYMBOL(fs_param_is_path);
358
31d921c7
DH
359#ifdef CONFIG_VALIDATE_FS_PARSER
360/**
361 * validate_constant_table - Validate a constant table
31d921c7
DH
362 * @tbl: The constant table to validate.
363 * @tbl_size: The size of the table.
364 * @low: The lowest permissible value.
365 * @high: The highest permissible value.
366 * @special: One special permissible value outside of the range.
367 */
368bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
369 int low, int high, int special)
370{
371 size_t i;
372 bool good = true;
373
374 if (tbl_size == 0) {
375 pr_warn("VALIDATE C-TBL: Empty\n");
376 return true;
377 }
378
379 for (i = 0; i < tbl_size; i++) {
380 if (!tbl[i].name) {
381 pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
382 good = false;
383 } else if (i > 0 && tbl[i - 1].name) {
384 int c = strcmp(tbl[i-1].name, tbl[i].name);
385
386 if (c == 0) {
387 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
388 i, tbl[i].name);
389 good = false;
390 }
391 if (c > 0) {
392 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
393 i, tbl[i-1].name, tbl[i].name);
394 good = false;
395 }
396 }
397
398 if (tbl[i].value != special &&
399 (tbl[i].value < low || tbl[i].value > high)) {
400 pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
401 i, tbl[i].name, tbl[i].value, low, high);
402 good = false;
403 }
404 }
405
406 return good;
407}
408
409/**
410 * fs_validate_description - Validate a parameter description
21ae3ad1 411 * @name: The parameter name to search for.
31d921c7
DH
412 * @desc: The parameter description to validate.
413 */
96cafb9c 414bool fs_validate_description(const char *name,
d7167b14 415 const struct fs_parameter_spec *desc)
31d921c7
DH
416{
417 const struct fs_parameter_spec *param, *p2;
2710c957 418 bool good = true;
31d921c7 419
d7167b14 420 for (param = desc; param->name; param++) {
d7167b14
AV
421 /* Check for duplicate parameter names */
422 for (p2 = desc; p2 < param; p2++) {
423 if (strcmp(param->name, p2->name) == 0) {
48ce73b1
AV
424 if (is_flag(param) != is_flag(p2))
425 continue;
d7167b14
AV
426 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
427 name, param->name);
428 good = false;
31d921c7
DH
429 }
430 }
31d921c7 431 }
31d921c7
DH
432 return good;
433}
434#endif /* CONFIG_VALIDATE_FS_PARSER */
This page took 0.375065 seconds and 4 git commands to generate.