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 | |
| 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 | */ |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame^] | 83 | int __fs_parse(struct p_log *log, |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 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; |
Al Viro | 5eede62 | 2019-12-16 13:33:32 -0500 | [diff] [blame] | 89 | const struct constant_table *e; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 90 | int ret = -ENOPARAM, b; |
| 91 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 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 | */ |
Al Viro | 0f89589 | 2019-12-17 14:15:04 -0500 | [diff] [blame] | 101 | if (param->type != fs_value_is_flag) |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 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) |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame^] | 116 | warn_plog(log, "Deprecated parameter '%s'", param->key); |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 117 | |
| 118 | if (result->negated) |
| 119 | goto okay; |
| 120 | |
| 121 | /* Certain parameter types only take a string and convert it. */ |
| 122 | switch (p->type) { |
| 123 | case __fs_param_wasnt_defined: |
| 124 | return -EINVAL; |
| 125 | case fs_param_is_u32: |
| 126 | case fs_param_is_u32_octal: |
| 127 | case fs_param_is_u32_hex: |
| 128 | case fs_param_is_s32: |
| 129 | case fs_param_is_u64: |
| 130 | case fs_param_is_enum: |
| 131 | case fs_param_is_string: |
Al Viro | 0f89589 | 2019-12-17 14:15:04 -0500 | [diff] [blame] | 132 | if (param->type == fs_value_is_string) { |
| 133 | if (p->flags & fs_param_v_optional) |
| 134 | break; |
| 135 | if (!*param->string) |
| 136 | goto bad_value; |
| 137 | break; |
| 138 | } |
| 139 | if (param->type == fs_value_is_flag) { |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 140 | if (p->flags & fs_param_v_optional) |
| 141 | goto okay; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 142 | } |
Al Viro | 0f89589 | 2019-12-17 14:15:04 -0500 | [diff] [blame] | 143 | goto bad_value; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | /* Try to turn the type we were given into the type desired by the |
| 149 | * parameter and give an error if we can't. |
| 150 | */ |
| 151 | switch (p->type) { |
| 152 | case fs_param_is_flag: |
Al Viro | 0f89589 | 2019-12-17 14:15:04 -0500 | [diff] [blame] | 153 | if (param->type != fs_value_is_flag) |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame^] | 154 | return inval_plog(log, "Unexpected value for '%s'", |
| 155 | param->key); |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 156 | result->boolean = true; |
| 157 | goto okay; |
| 158 | |
| 159 | case fs_param_is_bool: |
| 160 | switch (param->type) { |
| 161 | case fs_value_is_flag: |
| 162 | result->boolean = true; |
| 163 | goto okay; |
| 164 | case fs_value_is_string: |
| 165 | if (param->size == 0) { |
| 166 | result->boolean = true; |
| 167 | goto okay; |
| 168 | } |
| 169 | b = lookup_constant(bool_names, param->string, -1); |
| 170 | if (b == -1) |
| 171 | goto bad_value; |
| 172 | result->boolean = b; |
| 173 | goto okay; |
| 174 | default: |
| 175 | goto bad_value; |
| 176 | } |
| 177 | |
| 178 | case fs_param_is_u32: |
| 179 | ret = kstrtouint(param->string, 0, &result->uint_32); |
| 180 | goto maybe_okay; |
| 181 | case fs_param_is_u32_octal: |
| 182 | ret = kstrtouint(param->string, 8, &result->uint_32); |
| 183 | goto maybe_okay; |
| 184 | case fs_param_is_u32_hex: |
| 185 | ret = kstrtouint(param->string, 16, &result->uint_32); |
| 186 | goto maybe_okay; |
| 187 | case fs_param_is_s32: |
| 188 | ret = kstrtoint(param->string, 0, &result->int_32); |
| 189 | goto maybe_okay; |
| 190 | case fs_param_is_u64: |
| 191 | ret = kstrtoull(param->string, 0, &result->uint_64); |
| 192 | goto maybe_okay; |
| 193 | |
| 194 | case fs_param_is_enum: |
Al Viro | 34264ae | 2019-12-16 13:45:41 -0500 | [diff] [blame] | 195 | e = __lookup_constant(p->data, param->string); |
| 196 | if (e) { |
| 197 | result->uint_32 = e->value; |
| 198 | goto okay; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 199 | } |
| 200 | goto bad_value; |
| 201 | |
| 202 | case fs_param_is_string: |
| 203 | goto okay; |
| 204 | case fs_param_is_blob: |
| 205 | if (param->type != fs_value_is_blob) |
| 206 | goto bad_value; |
| 207 | goto okay; |
| 208 | |
| 209 | case fs_param_is_fd: { |
David Howells | 74983ac | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 210 | switch (param->type) { |
| 211 | case fs_value_is_string: |
David Howells | 74983ac | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 212 | ret = kstrtouint(param->string, 0, &result->uint_32); |
| 213 | break; |
| 214 | case fs_value_is_file: |
| 215 | result->uint_32 = param->dirfd; |
| 216 | ret = 0; |
| 217 | default: |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 218 | goto bad_value; |
David Howells | 74983ac | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (result->uint_32 > INT_MAX) |
| 222 | goto bad_value; |
| 223 | goto maybe_okay; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | case fs_param_is_blockdev: |
| 227 | case fs_param_is_path: |
| 228 | goto okay; |
| 229 | default: |
| 230 | BUG(); |
| 231 | } |
| 232 | |
| 233 | maybe_okay: |
| 234 | if (ret < 0) |
| 235 | goto bad_value; |
| 236 | okay: |
| 237 | return p->opt; |
| 238 | |
| 239 | bad_value: |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame^] | 240 | return inval_plog(log, "Bad value for '%s'", param->key); |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 241 | unknown_parameter: |
| 242 | return -ENOPARAM; |
| 243 | } |
Al Viro | 7f5d381 | 2019-12-20 23:52:55 -0500 | [diff] [blame^] | 244 | EXPORT_SYMBOL(__fs_parse); |
| 245 | |
| 246 | int fs_parse(struct fs_context *fc, |
| 247 | const struct fs_parameter_description *desc, |
| 248 | struct fs_parameter *param, |
| 249 | struct fs_parse_result *result) |
| 250 | { |
| 251 | struct p_log log = {.prefix = desc->name, .log = fc->log}; |
| 252 | return __fs_parse(&log, desc, param, result); |
| 253 | } |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 254 | EXPORT_SYMBOL(fs_parse); |
| 255 | |
| 256 | /** |
| 257 | * fs_lookup_param - Look up a path referred to by a parameter |
| 258 | * @fc: The filesystem context to log errors through. |
| 259 | * @param: The parameter. |
| 260 | * @want_bdev: T if want a blockdev |
| 261 | * @_path: The result of the lookup |
| 262 | */ |
| 263 | int fs_lookup_param(struct fs_context *fc, |
| 264 | struct fs_parameter *param, |
| 265 | bool want_bdev, |
| 266 | struct path *_path) |
| 267 | { |
| 268 | struct filename *f; |
| 269 | unsigned int flags = 0; |
| 270 | bool put_f; |
| 271 | int ret; |
| 272 | |
| 273 | switch (param->type) { |
| 274 | case fs_value_is_string: |
| 275 | f = getname_kernel(param->string); |
| 276 | if (IS_ERR(f)) |
| 277 | return PTR_ERR(f); |
| 278 | put_f = true; |
| 279 | break; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 280 | case fs_value_is_filename: |
| 281 | f = param->name; |
| 282 | put_f = false; |
| 283 | break; |
| 284 | default: |
| 285 | return invalf(fc, "%s: not usable as path", param->key); |
| 286 | } |
| 287 | |
David Howells | 7cdfa44 | 2019-03-25 16:38:22 +0000 | [diff] [blame] | 288 | f->refcnt++; /* filename_lookup() drops our ref. */ |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 289 | ret = filename_lookup(param->dirfd, f, flags, _path, NULL); |
| 290 | if (ret < 0) { |
| 291 | errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name); |
| 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | if (want_bdev && |
| 296 | !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) { |
| 297 | path_put(_path); |
| 298 | _path->dentry = NULL; |
| 299 | _path->mnt = NULL; |
| 300 | errorf(fc, "%s: Non-blockdev passed as '%s'", |
| 301 | param->key, f->name); |
| 302 | ret = -ENOTBLK; |
| 303 | } |
| 304 | |
| 305 | out: |
| 306 | if (put_f) |
| 307 | putname(f); |
| 308 | return ret; |
| 309 | } |
| 310 | EXPORT_SYMBOL(fs_lookup_param); |
| 311 | |
| 312 | #ifdef CONFIG_VALIDATE_FS_PARSER |
| 313 | /** |
| 314 | * validate_constant_table - Validate a constant table |
| 315 | * @name: Name to use in reporting |
| 316 | * @tbl: The constant table to validate. |
| 317 | * @tbl_size: The size of the table. |
| 318 | * @low: The lowest permissible value. |
| 319 | * @high: The highest permissible value. |
| 320 | * @special: One special permissible value outside of the range. |
| 321 | */ |
| 322 | bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size, |
| 323 | int low, int high, int special) |
| 324 | { |
| 325 | size_t i; |
| 326 | bool good = true; |
| 327 | |
| 328 | if (tbl_size == 0) { |
| 329 | pr_warn("VALIDATE C-TBL: Empty\n"); |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | for (i = 0; i < tbl_size; i++) { |
| 334 | if (!tbl[i].name) { |
| 335 | pr_err("VALIDATE C-TBL[%zu]: Null\n", i); |
| 336 | good = false; |
| 337 | } else if (i > 0 && tbl[i - 1].name) { |
| 338 | int c = strcmp(tbl[i-1].name, tbl[i].name); |
| 339 | |
| 340 | if (c == 0) { |
| 341 | pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n", |
| 342 | i, tbl[i].name); |
| 343 | good = false; |
| 344 | } |
| 345 | if (c > 0) { |
| 346 | pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n", |
| 347 | i, tbl[i-1].name, tbl[i].name); |
| 348 | good = false; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (tbl[i].value != special && |
| 353 | (tbl[i].value < low || tbl[i].value > high)) { |
| 354 | pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n", |
| 355 | i, tbl[i].name, tbl[i].value, low, high); |
| 356 | good = false; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return good; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * fs_validate_description - Validate a parameter description |
| 365 | * @desc: The parameter description to validate. |
| 366 | */ |
| 367 | bool fs_validate_description(const struct fs_parameter_description *desc) |
| 368 | { |
| 369 | const struct fs_parameter_spec *param, *p2; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 370 | const char *name = desc->name; |
Al Viro | 2710c957a | 2019-09-06 22:12:08 -0400 | [diff] [blame] | 371 | bool good = true; |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 372 | |
| 373 | pr_notice("*** VALIDATE %s ***\n", name); |
| 374 | |
| 375 | if (!name[0]) { |
| 376 | pr_err("VALIDATE Parser: No name\n"); |
| 377 | name = "Unknown"; |
| 378 | good = false; |
| 379 | } |
| 380 | |
| 381 | if (desc->specs) { |
| 382 | for (param = desc->specs; param->name; param++) { |
| 383 | enum fs_parameter_type t = param->type; |
| 384 | |
| 385 | /* Check that the type is in range */ |
| 386 | if (t == __fs_param_wasnt_defined || |
| 387 | t >= nr__fs_parameter_type) { |
| 388 | pr_err("VALIDATE %s: PARAM[%s] Bad type %u\n", |
| 389 | name, param->name, t); |
| 390 | good = false; |
| 391 | } else if (t == fs_param_is_enum) { |
Al Viro | 5eede62 | 2019-12-16 13:33:32 -0500 | [diff] [blame] | 392 | const struct constant_table *e = param->data; |
Al Viro | 2710c957a | 2019-09-06 22:12:08 -0400 | [diff] [blame] | 393 | if (!e || !e->name) { |
| 394 | pr_err("VALIDATE %s: PARAM[%s] enum with no values\n", |
| 395 | name, param->name); |
| 396 | good = false; |
| 397 | } |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* Check for duplicate parameter names */ |
| 401 | for (p2 = desc->specs; p2 < param; p2++) { |
| 402 | if (strcmp(param->name, p2->name) == 0) { |
| 403 | pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n", |
| 404 | name, param->name); |
| 405 | good = false; |
| 406 | } |
| 407 | } |
| 408 | } |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 409 | } |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 410 | return good; |
| 411 | } |
| 412 | #endif /* CONFIG_VALIDATE_FS_PARSER */ |