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