OSDN Git Service

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