Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2 | /* Filesystem parameter parser. |
| 3 | * |
| 4 | * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 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 }, |
Al Viro | 34264ae | 2019-12-16 13:45:41 -0500 | [diff] [blame] | 23 | { }, |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
Al Viro | 34264ae | 2019-12-16 13:45:41 -0500 | [diff] [blame] | 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 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 35 | /** |
| 36 | * lookup_constant - Look up a constant by name in an ordered table |
| 37 | * @tbl: The table of constants to search. |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 38 | * @name: The name to look up. |
| 39 | * @not_found: The value to return if the name is not found. |
| 40 | */ |
Al Viro | 34264ae | 2019-12-16 13:45:41 -0500 | [diff] [blame] | 41 | int lookup_constant(const struct constant_table *tbl, const char *name, int not_found) |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 42 | { |
Al Viro | 34264ae | 2019-12-16 13:45:41 -0500 | [diff] [blame] | 43 | const struct constant_table *p = __lookup_constant(tbl, name); |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 44 | |
Al Viro | 34264ae | 2019-12-16 13:45:41 -0500 | [diff] [blame] | 45 | return p ? p->value : not_found; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 46 | } |
Al Viro | 34264ae | 2019-12-16 13:45:41 -0500 | [diff] [blame] | 47 | EXPORT_SYMBOL(lookup_constant); |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 48 | |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 49 | static inline bool is_flag(const struct fs_parameter_spec *p) |
| 50 | { |
Al Viro | 328de52 | 2019-12-18 00:02:31 -0500 | [diff] [blame] | 51 | return p->type == NULL; |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 52 | } |
| 53 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 54 | static const struct fs_parameter_spec *fs_lookup_key( |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 55 | const struct fs_parameter_spec *desc, |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 56 | struct fs_parameter *param, bool *negated) |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 57 | { |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 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; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 61 | |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 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)) |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 67 | return p; |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 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; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 83 | } |
| 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 Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame] | 103 | int __fs_parse(struct p_log *log, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 104 | const struct fs_parameter_spec *desc, |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 105 | struct fs_parameter *param, |
| 106 | struct fs_parse_result *result) |
| 107 | { |
| 108 | const struct fs_parameter_spec *p; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 109 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 110 | result->uint_64 = 0; |
| 111 | |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 112 | p = fs_lookup_key(desc, param, &result->negated); |
| 113 | if (!p) |
| 114 | return -ENOPARAM; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 115 | |
| 116 | if (p->flags & fs_param_deprecated) |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame] | 117 | warn_plog(log, "Deprecated parameter '%s'", param->key); |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 118 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 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 | */ |
Al Viro | 328de52 | 2019-12-18 00:02:31 -0500 | [diff] [blame] | 122 | if (is_flag(p)) { |
Al Viro | 0f89589 | 2019-12-17 14:15:04 -0500 | [diff] [blame] | 123 | if (param->type != fs_value_is_flag) |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame] | 124 | return inval_plog(log, "Unexpected value for '%s'", |
| 125 | param->key); |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 126 | result->boolean = !result->negated; |
Al Viro | 328de52 | 2019-12-18 00:02:31 -0500 | [diff] [blame] | 127 | } else { |
| 128 | int ret = p->type(log, p, param, result); |
| 129 | if (ret) |
| 130 | return ret; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 131 | } |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 132 | return p->opt; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 133 | } |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame] | 134 | EXPORT_SYMBOL(__fs_parse); |
| 135 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 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 |
| 141 | * @_path: The result of the lookup |
| 142 | */ |
| 143 | int 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 Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 160 | 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 | |
David Howells | 7cdfa44 | 2019-03-25 16:38:22 +0000 | [diff] [blame] | 168 | f->refcnt++; /* filename_lookup() drops our ref. */ |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 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 | |
| 185 | out: |
| 186 | if (put_f) |
| 187 | putname(f); |
| 188 | return ret; |
| 189 | } |
| 190 | EXPORT_SYMBOL(fs_lookup_param); |
| 191 | |
Al Viro | 328de52 | 2019-12-18 00:02:31 -0500 | [diff] [blame] | 192 | int fs_param_bad_value(struct p_log *log, struct fs_parameter *param) |
| 193 | { |
| 194 | return inval_plog(log, "Bad value for '%s'", param->key); |
| 195 | } |
| 196 | |
| 197 | int 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); |
| 203 | b = lookup_constant(bool_names, param->string, -1); |
| 204 | if (b == -1) |
| 205 | return fs_param_bad_value(log, param); |
| 206 | result->boolean = b; |
| 207 | return 0; |
| 208 | } |
| 209 | EXPORT_SYMBOL(fs_param_is_bool); |
| 210 | |
| 211 | int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p, |
| 212 | struct fs_parameter *param, struct fs_parse_result *result) |
| 213 | { |
| 214 | int base = (unsigned long)p->data; |
| 215 | if (param->type != fs_value_is_string || |
| 216 | kstrtouint(param->string, base, &result->uint_32) < 0) |
| 217 | return fs_param_bad_value(log, param); |
| 218 | return 0; |
| 219 | } |
| 220 | EXPORT_SYMBOL(fs_param_is_u32); |
| 221 | |
| 222 | int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p, |
| 223 | struct fs_parameter *param, struct fs_parse_result *result) |
| 224 | { |
| 225 | if (param->type != fs_value_is_string || |
| 226 | kstrtoint(param->string, 0, &result->int_32) < 0) |
| 227 | return fs_param_bad_value(log, param); |
| 228 | return 0; |
| 229 | } |
| 230 | EXPORT_SYMBOL(fs_param_is_s32); |
| 231 | |
| 232 | int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p, |
| 233 | struct fs_parameter *param, struct fs_parse_result *result) |
| 234 | { |
| 235 | if (param->type != fs_value_is_string || |
| 236 | kstrtoull(param->string, 0, &result->uint_64) < 0) |
| 237 | return fs_param_bad_value(log, param); |
| 238 | return 0; |
| 239 | } |
| 240 | EXPORT_SYMBOL(fs_param_is_u64); |
| 241 | |
| 242 | int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p, |
| 243 | struct fs_parameter *param, struct fs_parse_result *result) |
| 244 | { |
| 245 | const struct constant_table *c; |
| 246 | if (param->type != fs_value_is_string) |
| 247 | return fs_param_bad_value(log, param); |
| 248 | c = __lookup_constant(p->data, param->string); |
| 249 | if (!c) |
| 250 | return fs_param_bad_value(log, param); |
| 251 | result->uint_32 = c->value; |
| 252 | return 0; |
| 253 | } |
| 254 | EXPORT_SYMBOL(fs_param_is_enum); |
| 255 | |
| 256 | int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p, |
| 257 | struct fs_parameter *param, struct fs_parse_result *result) |
| 258 | { |
| 259 | if (param->type != fs_value_is_string || !*param->string) |
| 260 | return fs_param_bad_value(log, param); |
| 261 | return 0; |
| 262 | } |
| 263 | EXPORT_SYMBOL(fs_param_is_string); |
| 264 | |
| 265 | int fs_param_is_blob(struct p_log *log, const struct fs_parameter_spec *p, |
| 266 | struct fs_parameter *param, struct fs_parse_result *result) |
| 267 | { |
| 268 | if (param->type != fs_value_is_blob) |
| 269 | return fs_param_bad_value(log, param); |
| 270 | return 0; |
| 271 | } |
| 272 | EXPORT_SYMBOL(fs_param_is_blob); |
| 273 | |
| 274 | int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p, |
| 275 | struct fs_parameter *param, struct fs_parse_result *result) |
| 276 | { |
| 277 | switch (param->type) { |
| 278 | case fs_value_is_string: |
| 279 | if (kstrtouint(param->string, 0, &result->uint_32) < 0) |
| 280 | break; |
| 281 | if (result->uint_32 <= INT_MAX) |
| 282 | return 0; |
| 283 | break; |
| 284 | case fs_value_is_file: |
| 285 | result->uint_32 = param->dirfd; |
| 286 | if (result->uint_32 <= INT_MAX) |
| 287 | return 0; |
| 288 | break; |
| 289 | default: |
| 290 | break; |
| 291 | } |
| 292 | return fs_param_bad_value(log, param); |
| 293 | } |
| 294 | EXPORT_SYMBOL(fs_param_is_fd); |
| 295 | |
| 296 | int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p, |
| 297 | struct fs_parameter *param, struct fs_parse_result *result) |
| 298 | { |
| 299 | return 0; |
| 300 | } |
| 301 | EXPORT_SYMBOL(fs_param_is_blockdev); |
| 302 | |
| 303 | int fs_param_is_path(struct p_log *log, const struct fs_parameter_spec *p, |
| 304 | struct fs_parameter *param, struct fs_parse_result *result) |
| 305 | { |
| 306 | return 0; |
| 307 | } |
| 308 | EXPORT_SYMBOL(fs_param_is_path); |
| 309 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 310 | #ifdef CONFIG_VALIDATE_FS_PARSER |
| 311 | /** |
| 312 | * validate_constant_table - Validate a constant table |
| 313 | * @name: Name to use in reporting |
| 314 | * @tbl: The constant table to validate. |
| 315 | * @tbl_size: The size of the table. |
| 316 | * @low: The lowest permissible value. |
| 317 | * @high: The highest permissible value. |
| 318 | * @special: One special permissible value outside of the range. |
| 319 | */ |
| 320 | bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size, |
| 321 | int low, int high, int special) |
| 322 | { |
| 323 | size_t i; |
| 324 | bool good = true; |
| 325 | |
| 326 | if (tbl_size == 0) { |
| 327 | pr_warn("VALIDATE C-TBL: Empty\n"); |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | for (i = 0; i < tbl_size; i++) { |
| 332 | if (!tbl[i].name) { |
| 333 | pr_err("VALIDATE C-TBL[%zu]: Null\n", i); |
| 334 | good = false; |
| 335 | } else if (i > 0 && tbl[i - 1].name) { |
| 336 | int c = strcmp(tbl[i-1].name, tbl[i].name); |
| 337 | |
| 338 | if (c == 0) { |
| 339 | pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n", |
| 340 | i, tbl[i].name); |
| 341 | good = false; |
| 342 | } |
| 343 | if (c > 0) { |
| 344 | pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n", |
| 345 | i, tbl[i-1].name, tbl[i].name); |
| 346 | good = false; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (tbl[i].value != special && |
| 351 | (tbl[i].value < low || tbl[i].value > high)) { |
| 352 | pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n", |
| 353 | i, tbl[i].name, tbl[i].value, low, high); |
| 354 | good = false; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return good; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * fs_validate_description - Validate a parameter description |
| 363 | * @desc: The parameter description to validate. |
| 364 | */ |
Eric Sandeen | 96cafb9 | 2019-12-06 10:45:01 -0600 | [diff] [blame] | 365 | bool fs_validate_description(const char *name, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 366 | const struct fs_parameter_spec *desc) |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 367 | { |
| 368 | const struct fs_parameter_spec *param, *p2; |
Al Viro | 2710c957a | 2019-09-06 22:12:08 -0400 | [diff] [blame] | 369 | bool good = true; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 370 | |
| 371 | pr_notice("*** VALIDATE %s ***\n", name); |
| 372 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 373 | for (param = desc; param->name; param++) { |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 374 | /* Check for duplicate parameter names */ |
| 375 | for (p2 = desc; p2 < param; p2++) { |
| 376 | if (strcmp(param->name, p2->name) == 0) { |
Al Viro | 48ce73b | 2019-12-17 20:03:59 -0500 | [diff] [blame] | 377 | if (is_flag(param) != is_flag(p2)) |
| 378 | continue; |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 379 | pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n", |
| 380 | name, param->name); |
| 381 | good = false; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 384 | } |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 385 | return good; |
| 386 | } |
| 387 | #endif /* CONFIG_VALIDATE_FS_PARSER */ |