]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/fs_parser.c
5f8c06a1fb93e4bbb415a3742941b21de44c892e
[linux.git] / fs / fs_parser.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Filesystem parameter parser.
3  *
4  * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
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
16 static 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 static 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
35 /**
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.
40  */
41 int lookup_constant(const struct constant_table *tbl, const char *name, int not_found)
42 {
43         const struct constant_table *p = __lookup_constant(tbl, name);
44
45         return p ? p->value : not_found;
46 }
47 EXPORT_SYMBOL(lookup_constant);
48
49 static const struct fs_parameter_spec *fs_lookup_key(
50         const struct fs_parameter_spec *desc,
51         const char *name)
52 {
53         const struct fs_parameter_spec *p;
54         if (!desc)
55                 return NULL;
56
57         for (p = desc; p->name; p++)
58                 if (strcmp(p->name, name) == 0)
59                         return p;
60
61         return NULL;
62 }
63
64 /*
65  * fs_parse - Parse a filesystem configuration parameter
66  * @fc: The filesystem context to log errors through.
67  * @desc: The parameter description to use.
68  * @param: The parameter.
69  * @result: Where to place the result of the parse
70  *
71  * Parse a filesystem configuration parameter and attempt a conversion for a
72  * simple parameter for which this is requested.  If successful, the determined
73  * parameter ID is placed into @result->key, the desired type is indicated in
74  * @result->t and any converted value is placed into an appropriate member of
75  * the union in @result.
76  *
77  * The function returns the parameter number if the parameter was matched,
78  * -ENOPARAM if it wasn't matched and @desc->ignore_unknown indicated that
79  * unknown parameters are okay and -EINVAL if there was a conversion issue or
80  * the parameter wasn't recognised and unknowns aren't okay.
81  */
82 int __fs_parse(struct p_log *log,
83              const struct fs_parameter_spec *desc,
84              struct fs_parameter *param,
85              struct fs_parse_result *result)
86 {
87         const struct fs_parameter_spec *p;
88         const struct constant_table *e;
89         int ret = -ENOPARAM, b;
90
91         result->negated = false;
92         result->uint_64 = 0;
93
94         p = fs_lookup_key(desc, param->key);
95         if (!p) {
96                 /* If we didn't find something that looks like "noxxx", see if
97                  * "xxx" takes the "no"-form negative - but only if there
98                  * wasn't an value.
99                  */
100                 if (param->type != fs_value_is_flag)
101                         goto unknown_parameter;
102                 if (param->key[0] != 'n' || param->key[1] != 'o' || !param->key[2])
103                         goto unknown_parameter;
104
105                 p = fs_lookup_key(desc, param->key + 2);
106                 if (!p)
107                         goto unknown_parameter;
108                 if (!(p->flags & fs_param_neg_with_no))
109                         goto unknown_parameter;
110                 result->boolean = false;
111                 result->negated = true;
112         }
113
114         if (p->flags & fs_param_deprecated)
115                 warn_plog(log, "Deprecated parameter '%s'", param->key);
116
117         if (result->negated)
118                 goto okay;
119
120         /* Certain parameter types only take a string and convert it. */
121         switch (p->type) {
122         case __fs_param_wasnt_defined:
123                 return -EINVAL;
124         case fs_param_is_u32:
125         case fs_param_is_u32_octal:
126         case fs_param_is_u32_hex:
127         case fs_param_is_s32:
128         case fs_param_is_u64:
129         case fs_param_is_enum:
130         case fs_param_is_string:
131                 if (param->type == fs_value_is_string) {
132                         if (p->flags & fs_param_v_optional)
133                                 break;
134                         if (!*param->string)
135                                 goto bad_value;
136                         break;
137                 }
138                 if (param->type == fs_value_is_flag) {
139                         if (p->flags & fs_param_v_optional)
140                                 goto okay;
141                 }
142                 goto bad_value;
143         default:
144                 break;
145         }
146
147         /* Try to turn the type we were given into the type desired by the
148          * parameter and give an error if we can't.
149          */
150         switch (p->type) {
151         case fs_param_is_flag:
152                 if (param->type != fs_value_is_flag)
153                         return inval_plog(log, "Unexpected value for '%s'",
154                                       param->key);
155                 result->boolean = true;
156                 goto okay;
157
158         case fs_param_is_bool:
159                 switch (param->type) {
160                 case fs_value_is_flag:
161                         result->boolean = true;
162                         goto okay;
163                 case fs_value_is_string:
164                         if (param->size == 0) {
165                                 result->boolean = true;
166                                 goto okay;
167                         }
168                         b = lookup_constant(bool_names, param->string, -1);
169                         if (b == -1)
170                                 goto bad_value;
171                         result->boolean = b;
172                         goto okay;
173                 default:
174                         goto bad_value;
175                 }
176
177         case fs_param_is_u32:
178                 ret = kstrtouint(param->string, 0, &result->uint_32);
179                 goto maybe_okay;
180         case fs_param_is_u32_octal:
181                 ret = kstrtouint(param->string, 8, &result->uint_32);
182                 goto maybe_okay;
183         case fs_param_is_u32_hex:
184                 ret = kstrtouint(param->string, 16, &result->uint_32);
185                 goto maybe_okay;
186         case fs_param_is_s32:
187                 ret = kstrtoint(param->string, 0, &result->int_32);
188                 goto maybe_okay;
189         case fs_param_is_u64:
190                 ret = kstrtoull(param->string, 0, &result->uint_64);
191                 goto maybe_okay;
192
193         case fs_param_is_enum:
194                 e = __lookup_constant(p->data, param->string);
195                 if (e) {
196                         result->uint_32 = e->value;
197                         goto okay;
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: {
209                 switch (param->type) {
210                 case fs_value_is_string:
211                         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:
217                         goto bad_value;
218                 }
219
220                 if (result->uint_32 > INT_MAX)
221                         goto bad_value;
222                 goto maybe_okay;
223         }
224
225         case fs_param_is_blockdev:
226         case fs_param_is_path:
227                 goto okay;
228         default:
229                 BUG();
230         }
231
232 maybe_okay:
233         if (ret < 0)
234                 goto bad_value;
235 okay:
236         return p->opt;
237
238 bad_value:
239         return inval_plog(log, "Bad value for '%s'", param->key);
240 unknown_parameter:
241         return -ENOPARAM;
242 }
243 EXPORT_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  */
252 int 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:
270                 f = param->name;
271                 put_f = false;
272                 break;
273         default:
274                 return invalf(fc, "%s: not usable as path", param->key);
275         }
276
277         f->refcnt++; /* filename_lookup() drops our ref. */
278         ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
279         if (ret < 0) {
280                 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
281                 goto out;
282         }
283
284         if (want_bdev &&
285             !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
286                 path_put(_path);
287                 _path->dentry = NULL;
288                 _path->mnt = NULL;
289                 errorf(fc, "%s: Non-blockdev passed as '%s'",
290                        param->key, f->name);
291                 ret = -ENOTBLK;
292         }
293
294 out:
295         if (put_f)
296                 putname(f);
297         return ret;
298 }
299 EXPORT_SYMBOL(fs_lookup_param);
300
301 #ifdef CONFIG_VALIDATE_FS_PARSER
302 /**
303  * validate_constant_table - Validate a constant table
304  * @name: Name to use in reporting
305  * @tbl: The constant table to validate.
306  * @tbl_size: The size of the table.
307  * @low: The lowest permissible value.
308  * @high: The highest permissible value.
309  * @special: One special permissible value outside of the range.
310  */
311 bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
312                              int low, int high, int special)
313 {
314         size_t i;
315         bool good = true;
316
317         if (tbl_size == 0) {
318                 pr_warn("VALIDATE C-TBL: Empty\n");
319                 return true;
320         }
321
322         for (i = 0; i < tbl_size; i++) {
323                 if (!tbl[i].name) {
324                         pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
325                         good = false;
326                 } else if (i > 0 && tbl[i - 1].name) {
327                         int c = strcmp(tbl[i-1].name, tbl[i].name);
328
329                         if (c == 0) {
330                                 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
331                                        i, tbl[i].name);
332                                 good = false;
333                         }
334                         if (c > 0) {
335                                 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
336                                        i, tbl[i-1].name, tbl[i].name);
337                                 good = false;
338                         }
339                 }
340
341                 if (tbl[i].value != special &&
342                     (tbl[i].value < low || tbl[i].value > high)) {
343                         pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
344                                i, tbl[i].name, tbl[i].value, low, high);
345                         good = false;
346                 }
347         }
348
349         return good;
350 }
351
352 /**
353  * fs_validate_description - Validate a parameter description
354  * @desc: The parameter description to validate.
355  */
356 bool fs_validate_description(const char *name,
357         const struct fs_parameter_spec *desc)
358 {
359         const struct fs_parameter_spec *param, *p2;
360         bool good = true;
361
362         pr_notice("*** VALIDATE %s ***\n", name);
363
364         for (param = desc; param->name; param++) {
365                 enum fs_parameter_type t = param->type;
366
367                 /* Check that the type is in range */
368                 if (t == __fs_param_wasnt_defined ||
369                     t >= nr__fs_parameter_type) {
370                         pr_err("VALIDATE %s: PARAM[%s] Bad type %u\n",
371                                name, param->name, t);
372                         good = false;
373                 } else if (t == fs_param_is_enum) {
374                         const struct constant_table *e = param->data;
375                         if (!e || !e->name) {
376                                 pr_err("VALIDATE %s: PARAM[%s] enum with no values\n",
377                                        name, param->name);
378                                 good = false;
379                         }
380                 }
381
382                 /* Check for duplicate parameter names */
383                 for (p2 = desc; p2 < param; p2++) {
384                         if (strcmp(param->name, p2->name) == 0) {
385                                 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
386                                        name, param->name);
387                                 good = false;
388                         }
389                 }
390         }
391         return good;
392 }
393 #endif /* CONFIG_VALIDATE_FS_PARSER */