Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 390 lines 11 kB view raw
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 16static 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 26static 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 */ 41int 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} 47EXPORT_SYMBOL(lookup_constant); 48 49static inline bool is_flag(const struct fs_parameter_spec *p) 50{ 51 return p->type == NULL; 52} 53 54static const struct fs_parameter_spec *fs_lookup_key( 55 const struct fs_parameter_spec *desc, 56 struct fs_parameter *param, bool *negated) 57{ 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; 61 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)) 67 return p; 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; 83} 84 85/* 86 * __fs_parse - Parse a filesystem configuration parameter 87 * @log: 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 */ 103int __fs_parse(struct p_log *log, 104 const struct fs_parameter_spec *desc, 105 struct fs_parameter *param, 106 struct fs_parse_result *result) 107{ 108 const struct fs_parameter_spec *p; 109 110 result->uint_64 = 0; 111 112 p = fs_lookup_key(desc, param, &result->negated); 113 if (!p) 114 return -ENOPARAM; 115 116 if (p->flags & fs_param_deprecated) 117 warn_plog(log, "Deprecated parameter '%s'", param->key); 118 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 */ 122 if (is_flag(p)) { 123 if (param->type != fs_value_is_flag) 124 return inval_plog(log, "Unexpected value for '%s'", 125 param->key); 126 result->boolean = !result->negated; 127 } else { 128 int ret = p->type(log, p, param, result); 129 if (ret) 130 return ret; 131 } 132 return p->opt; 133} 134EXPORT_SYMBOL(__fs_parse); 135 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 * @flags: Pathwalk flags passed to filename_lookup() 142 * @_path: The result of the lookup 143 */ 144int fs_lookup_param(struct fs_context *fc, 145 struct fs_parameter *param, 146 bool want_bdev, 147 unsigned int flags, 148 struct path *_path) 149{ 150 struct filename *f; 151 bool put_f; 152 int ret; 153 154 switch (param->type) { 155 case fs_value_is_string: 156 f = getname_kernel(param->string); 157 if (IS_ERR(f)) 158 return PTR_ERR(f); 159 param->dirfd = AT_FDCWD; 160 put_f = true; 161 break; 162 case fs_value_is_filename: 163 f = param->name; 164 put_f = false; 165 break; 166 default: 167 return invalf(fc, "%s: not usable as path", param->key); 168 } 169 170 ret = filename_lookup(param->dirfd, f, flags, _path, NULL); 171 if (ret < 0) { 172 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name); 173 goto out; 174 } 175 176 if (want_bdev && 177 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) { 178 path_put(_path); 179 _path->dentry = NULL; 180 _path->mnt = NULL; 181 errorf(fc, "%s: Non-blockdev passed as '%s'", 182 param->key, f->name); 183 ret = -ENOTBLK; 184 } 185 186out: 187 if (put_f) 188 putname(f); 189 return ret; 190} 191EXPORT_SYMBOL(fs_lookup_param); 192 193static int fs_param_bad_value(struct p_log *log, struct fs_parameter *param) 194{ 195 return inval_plog(log, "Bad value for '%s'", param->key); 196} 197 198int fs_param_is_bool(struct p_log *log, const struct fs_parameter_spec *p, 199 struct fs_parameter *param, struct fs_parse_result *result) 200{ 201 int b; 202 if (param->type != fs_value_is_string) 203 return fs_param_bad_value(log, param); 204 if (!*param->string && (p->flags & fs_param_can_be_empty)) 205 return 0; 206 b = lookup_constant(bool_names, param->string, -1); 207 if (b == -1) 208 return fs_param_bad_value(log, param); 209 result->boolean = b; 210 return 0; 211} 212EXPORT_SYMBOL(fs_param_is_bool); 213 214int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p, 215 struct fs_parameter *param, struct fs_parse_result *result) 216{ 217 int base = (unsigned long)p->data; 218 if (param->type != fs_value_is_string) 219 return fs_param_bad_value(log, param); 220 if (!*param->string && (p->flags & fs_param_can_be_empty)) 221 return 0; 222 if (kstrtouint(param->string, base, &result->uint_32) < 0) 223 return fs_param_bad_value(log, param); 224 return 0; 225} 226EXPORT_SYMBOL(fs_param_is_u32); 227 228int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p, 229 struct fs_parameter *param, struct fs_parse_result *result) 230{ 231 if (param->type != fs_value_is_string) 232 return fs_param_bad_value(log, param); 233 if (!*param->string && (p->flags & fs_param_can_be_empty)) 234 return 0; 235 if (kstrtoint(param->string, 0, &result->int_32) < 0) 236 return fs_param_bad_value(log, param); 237 return 0; 238} 239EXPORT_SYMBOL(fs_param_is_s32); 240 241int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p, 242 struct fs_parameter *param, struct fs_parse_result *result) 243{ 244 if (param->type != fs_value_is_string) 245 return fs_param_bad_value(log, param); 246 if (!*param->string && (p->flags & fs_param_can_be_empty)) 247 return 0; 248 if (kstrtoull(param->string, 0, &result->uint_64) < 0) 249 return fs_param_bad_value(log, param); 250 return 0; 251} 252EXPORT_SYMBOL(fs_param_is_u64); 253 254int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p, 255 struct fs_parameter *param, struct fs_parse_result *result) 256{ 257 const struct constant_table *c; 258 if (param->type != fs_value_is_string) 259 return fs_param_bad_value(log, param); 260 if (!*param->string && (p->flags & fs_param_can_be_empty)) 261 return 0; 262 c = __lookup_constant(p->data, param->string); 263 if (!c) 264 return fs_param_bad_value(log, param); 265 result->uint_32 = c->value; 266 return 0; 267} 268EXPORT_SYMBOL(fs_param_is_enum); 269 270int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p, 271 struct fs_parameter *param, struct fs_parse_result *result) 272{ 273 if (param->type != fs_value_is_string || 274 (!*param->string && !(p->flags & fs_param_can_be_empty))) 275 return fs_param_bad_value(log, param); 276 return 0; 277} 278EXPORT_SYMBOL(fs_param_is_string); 279 280int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p, 281 struct fs_parameter *param, struct fs_parse_result *result) 282{ 283 switch (param->type) { 284 case fs_value_is_string: 285 if ((!*param->string && !(p->flags & fs_param_can_be_empty)) || 286 kstrtouint(param->string, 0, &result->uint_32) < 0) 287 break; 288 if (result->uint_32 <= INT_MAX) 289 return 0; 290 break; 291 case fs_value_is_file: 292 result->uint_32 = param->dirfd; 293 if (result->uint_32 <= INT_MAX) 294 return 0; 295 break; 296 default: 297 break; 298 } 299 return fs_param_bad_value(log, param); 300} 301EXPORT_SYMBOL(fs_param_is_fd); 302 303int fs_param_is_file_or_string(struct p_log *log, 304 const struct fs_parameter_spec *p, 305 struct fs_parameter *param, 306 struct fs_parse_result *result) 307{ 308 switch (param->type) { 309 case fs_value_is_string: 310 return fs_param_is_string(log, p, param, result); 311 case fs_value_is_file: 312 result->uint_32 = param->dirfd; 313 if (result->uint_32 <= INT_MAX) 314 return 0; 315 break; 316 default: 317 break; 318 } 319 return fs_param_bad_value(log, param); 320} 321EXPORT_SYMBOL(fs_param_is_file_or_string); 322 323int fs_param_is_uid(struct p_log *log, const struct fs_parameter_spec *p, 324 struct fs_parameter *param, struct fs_parse_result *result) 325{ 326 kuid_t uid; 327 328 if (fs_param_is_u32(log, p, param, result) != 0) 329 return fs_param_bad_value(log, param); 330 331 uid = make_kuid(current_user_ns(), result->uint_32); 332 if (!uid_valid(uid)) 333 return inval_plog(log, "Invalid uid '%s'", param->string); 334 335 result->uid = uid; 336 return 0; 337} 338EXPORT_SYMBOL(fs_param_is_uid); 339 340int fs_param_is_gid(struct p_log *log, const struct fs_parameter_spec *p, 341 struct fs_parameter *param, struct fs_parse_result *result) 342{ 343 kgid_t gid; 344 345 if (fs_param_is_u32(log, p, param, result) != 0) 346 return fs_param_bad_value(log, param); 347 348 gid = make_kgid(current_user_ns(), result->uint_32); 349 if (!gid_valid(gid)) 350 return inval_plog(log, "Invalid gid '%s'", param->string); 351 352 result->gid = gid; 353 return 0; 354} 355EXPORT_SYMBOL(fs_param_is_gid); 356 357int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p, 358 struct fs_parameter *param, struct fs_parse_result *result) 359{ 360 return 0; 361} 362EXPORT_SYMBOL(fs_param_is_blockdev); 363 364#ifdef CONFIG_VALIDATE_FS_PARSER 365/** 366 * fs_validate_description - Validate a parameter specification array 367 * @name: Owner name of the parameter specification array 368 * @desc: The parameter specification array to validate. 369 */ 370bool fs_validate_description(const char *name, 371 const struct fs_parameter_spec *desc) 372{ 373 const struct fs_parameter_spec *param, *p2; 374 bool good = true; 375 376 for (param = desc; param->name; param++) { 377 /* Check for duplicate parameter names */ 378 for (p2 = desc; p2 < param; p2++) { 379 if (strcmp(param->name, p2->name) == 0) { 380 if (is_flag(param) != is_flag(p2)) 381 continue; 382 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n", 383 name, param->name); 384 good = false; 385 } 386 } 387 } 388 return good; 389} 390#endif /* CONFIG_VALIDATE_FS_PARSER */