fs_parser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 ([email protected])
  6. */
  7. #include <linux/export.h>
  8. #include <linux/fs_context.h>
  9. #include <linux/fs_parser.h>
  10. #include <linux/slab.h>
  11. #include <linux/security.h>
  12. #include <linux/namei.h>
  13. #include "internal.h"
  14. static const struct constant_table bool_names[] = {
  15. { "0", false },
  16. { "1", true },
  17. { "false", false },
  18. { "no", false },
  19. { "true", true },
  20. { "yes", true },
  21. { },
  22. };
  23. static const struct constant_table *
  24. __lookup_constant(const struct constant_table *tbl, const char *name)
  25. {
  26. for ( ; tbl->name; tbl++)
  27. if (strcmp(name, tbl->name) == 0)
  28. return tbl;
  29. return NULL;
  30. }
  31. /**
  32. * lookup_constant - Look up a constant by name in an ordered table
  33. * @tbl: The table of constants to search.
  34. * @name: The name to look up.
  35. * @not_found: The value to return if the name is not found.
  36. */
  37. int lookup_constant(const struct constant_table *tbl, const char *name, int not_found)
  38. {
  39. const struct constant_table *p = __lookup_constant(tbl, name);
  40. return p ? p->value : not_found;
  41. }
  42. EXPORT_SYMBOL(lookup_constant);
  43. static inline bool is_flag(const struct fs_parameter_spec *p)
  44. {
  45. return p->type == NULL;
  46. }
  47. static const struct fs_parameter_spec *fs_lookup_key(
  48. const struct fs_parameter_spec *desc,
  49. struct fs_parameter *param, bool *negated)
  50. {
  51. const struct fs_parameter_spec *p, *other = NULL;
  52. const char *name = param->key;
  53. bool want_flag = param->type == fs_value_is_flag;
  54. *negated = false;
  55. for (p = desc; p->name; p++) {
  56. if (strcmp(p->name, name) != 0)
  57. continue;
  58. if (likely(is_flag(p) == want_flag))
  59. return p;
  60. other = p;
  61. }
  62. if (want_flag) {
  63. if (name[0] == 'n' && name[1] == 'o' && name[2]) {
  64. for (p = desc; p->name; p++) {
  65. if (strcmp(p->name, name + 2) != 0)
  66. continue;
  67. if (!(p->flags & fs_param_neg_with_no))
  68. continue;
  69. *negated = true;
  70. return p;
  71. }
  72. }
  73. }
  74. return other;
  75. }
  76. /*
  77. * fs_parse - Parse a filesystem configuration parameter
  78. * @fc: The filesystem context to log errors through.
  79. * @desc: The parameter description to use.
  80. * @param: The parameter.
  81. * @result: Where to place the result of the parse
  82. *
  83. * Parse a filesystem configuration parameter and attempt a conversion for a
  84. * simple parameter for which this is requested. If successful, the determined
  85. * parameter ID is placed into @result->key, the desired type is indicated in
  86. * @result->t and any converted value is placed into an appropriate member of
  87. * the union in @result.
  88. *
  89. * The function returns the parameter number if the parameter was matched,
  90. * -ENOPARAM if it wasn't matched and @desc->ignore_unknown indicated that
  91. * unknown parameters are okay and -EINVAL if there was a conversion issue or
  92. * the parameter wasn't recognised and unknowns aren't okay.
  93. */
  94. int __fs_parse(struct p_log *log,
  95. const struct fs_parameter_spec *desc,
  96. struct fs_parameter *param,
  97. struct fs_parse_result *result)
  98. {
  99. const struct fs_parameter_spec *p;
  100. result->uint_64 = 0;
  101. p = fs_lookup_key(desc, param, &result->negated);
  102. if (!p)
  103. return -ENOPARAM;
  104. if (p->flags & fs_param_deprecated)
  105. warn_plog(log, "Deprecated parameter '%s'", param->key);
  106. /* Try to turn the type we were given into the type desired by the
  107. * parameter and give an error if we can't.
  108. */
  109. if (is_flag(p)) {
  110. if (param->type != fs_value_is_flag)
  111. return inval_plog(log, "Unexpected value for '%s'",
  112. param->key);
  113. result->boolean = !result->negated;
  114. } else {
  115. int ret = p->type(log, p, param, result);
  116. if (ret)
  117. return ret;
  118. }
  119. return p->opt;
  120. }
  121. EXPORT_SYMBOL(__fs_parse);
  122. /**
  123. * fs_lookup_param - Look up a path referred to by a parameter
  124. * @fc: The filesystem context to log errors through.
  125. * @param: The parameter.
  126. * @want_bdev: T if want a blockdev
  127. * @flags: Pathwalk flags passed to filename_lookup()
  128. * @_path: The result of the lookup
  129. */
  130. int fs_lookup_param(struct fs_context *fc,
  131. struct fs_parameter *param,
  132. bool want_bdev,
  133. unsigned int flags,
  134. struct path *_path)
  135. {
  136. struct filename *f;
  137. bool put_f;
  138. int ret;
  139. switch (param->type) {
  140. case fs_value_is_string:
  141. f = getname_kernel(param->string);
  142. if (IS_ERR(f))
  143. return PTR_ERR(f);
  144. put_f = true;
  145. break;
  146. case fs_value_is_filename:
  147. f = param->name;
  148. put_f = false;
  149. break;
  150. default:
  151. return invalf(fc, "%s: not usable as path", param->key);
  152. }
  153. ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
  154. if (ret < 0) {
  155. errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
  156. goto out;
  157. }
  158. if (want_bdev &&
  159. !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
  160. path_put(_path);
  161. _path->dentry = NULL;
  162. _path->mnt = NULL;
  163. errorf(fc, "%s: Non-blockdev passed as '%s'",
  164. param->key, f->name);
  165. ret = -ENOTBLK;
  166. }
  167. out:
  168. if (put_f)
  169. putname(f);
  170. return ret;
  171. }
  172. EXPORT_SYMBOL(fs_lookup_param);
  173. static int fs_param_bad_value(struct p_log *log, struct fs_parameter *param)
  174. {
  175. return inval_plog(log, "Bad value for '%s'", param->key);
  176. }
  177. int fs_param_is_bool(struct p_log *log, const struct fs_parameter_spec *p,
  178. struct fs_parameter *param, struct fs_parse_result *result)
  179. {
  180. int b;
  181. if (param->type != fs_value_is_string)
  182. return fs_param_bad_value(log, param);
  183. if (!*param->string && (p->flags & fs_param_can_be_empty))
  184. return 0;
  185. b = lookup_constant(bool_names, param->string, -1);
  186. if (b == -1)
  187. return fs_param_bad_value(log, param);
  188. result->boolean = b;
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(fs_param_is_bool);
  192. int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p,
  193. struct fs_parameter *param, struct fs_parse_result *result)
  194. {
  195. int base = (unsigned long)p->data;
  196. if (param->type != fs_value_is_string)
  197. return fs_param_bad_value(log, param);
  198. if (!*param->string && (p->flags & fs_param_can_be_empty))
  199. return 0;
  200. if (kstrtouint(param->string, base, &result->uint_32) < 0)
  201. return fs_param_bad_value(log, param);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(fs_param_is_u32);
  205. int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p,
  206. struct fs_parameter *param, struct fs_parse_result *result)
  207. {
  208. if (param->type != fs_value_is_string)
  209. return fs_param_bad_value(log, param);
  210. if (!*param->string && (p->flags & fs_param_can_be_empty))
  211. return 0;
  212. if (kstrtoint(param->string, 0, &result->int_32) < 0)
  213. return fs_param_bad_value(log, param);
  214. return 0;
  215. }
  216. EXPORT_SYMBOL(fs_param_is_s32);
  217. int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p,
  218. struct fs_parameter *param, struct fs_parse_result *result)
  219. {
  220. if (param->type != fs_value_is_string)
  221. return fs_param_bad_value(log, param);
  222. if (!*param->string && (p->flags & fs_param_can_be_empty))
  223. return 0;
  224. if (kstrtoull(param->string, 0, &result->uint_64) < 0)
  225. return fs_param_bad_value(log, param);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(fs_param_is_u64);
  229. int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p,
  230. struct fs_parameter *param, struct fs_parse_result *result)
  231. {
  232. const struct constant_table *c;
  233. if (param->type != fs_value_is_string)
  234. return fs_param_bad_value(log, param);
  235. if (!*param->string && (p->flags & fs_param_can_be_empty))
  236. return 0;
  237. c = __lookup_constant(p->data, param->string);
  238. if (!c)
  239. return fs_param_bad_value(log, param);
  240. result->uint_32 = c->value;
  241. return 0;
  242. }
  243. EXPORT_SYMBOL(fs_param_is_enum);
  244. int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p,
  245. struct fs_parameter *param, struct fs_parse_result *result)
  246. {
  247. if (param->type != fs_value_is_string ||
  248. (!*param->string && !(p->flags & fs_param_can_be_empty)))
  249. return fs_param_bad_value(log, param);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(fs_param_is_string);
  253. int fs_param_is_blob(struct p_log *log, const struct fs_parameter_spec *p,
  254. struct fs_parameter *param, struct fs_parse_result *result)
  255. {
  256. if (param->type != fs_value_is_blob)
  257. return fs_param_bad_value(log, param);
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(fs_param_is_blob);
  261. int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p,
  262. struct fs_parameter *param, struct fs_parse_result *result)
  263. {
  264. switch (param->type) {
  265. case fs_value_is_string:
  266. if ((!*param->string && !(p->flags & fs_param_can_be_empty)) ||
  267. kstrtouint(param->string, 0, &result->uint_32) < 0)
  268. break;
  269. if (result->uint_32 <= INT_MAX)
  270. return 0;
  271. break;
  272. case fs_value_is_file:
  273. result->uint_32 = param->dirfd;
  274. if (result->uint_32 <= INT_MAX)
  275. return 0;
  276. break;
  277. default:
  278. break;
  279. }
  280. return fs_param_bad_value(log, param);
  281. }
  282. EXPORT_SYMBOL(fs_param_is_fd);
  283. int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p,
  284. struct fs_parameter *param, struct fs_parse_result *result)
  285. {
  286. return 0;
  287. }
  288. EXPORT_SYMBOL(fs_param_is_blockdev);
  289. int fs_param_is_path(struct p_log *log, const struct fs_parameter_spec *p,
  290. struct fs_parameter *param, struct fs_parse_result *result)
  291. {
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(fs_param_is_path);
  295. #ifdef CONFIG_VALIDATE_FS_PARSER
  296. /**
  297. * validate_constant_table - Validate a constant table
  298. * @tbl: The constant table to validate.
  299. * @tbl_size: The size of the table.
  300. * @low: The lowest permissible value.
  301. * @high: The highest permissible value.
  302. * @special: One special permissible value outside of the range.
  303. */
  304. bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
  305. int low, int high, int special)
  306. {
  307. size_t i;
  308. bool good = true;
  309. if (tbl_size == 0) {
  310. pr_warn("VALIDATE C-TBL: Empty\n");
  311. return true;
  312. }
  313. for (i = 0; i < tbl_size; i++) {
  314. if (!tbl[i].name) {
  315. pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
  316. good = false;
  317. } else if (i > 0 && tbl[i - 1].name) {
  318. int c = strcmp(tbl[i-1].name, tbl[i].name);
  319. if (c == 0) {
  320. pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
  321. i, tbl[i].name);
  322. good = false;
  323. }
  324. if (c > 0) {
  325. pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
  326. i, tbl[i-1].name, tbl[i].name);
  327. good = false;
  328. }
  329. }
  330. if (tbl[i].value != special &&
  331. (tbl[i].value < low || tbl[i].value > high)) {
  332. pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
  333. i, tbl[i].name, tbl[i].value, low, high);
  334. good = false;
  335. }
  336. }
  337. return good;
  338. }
  339. /**
  340. * fs_validate_description - Validate a parameter description
  341. * @name: The parameter name to search for.
  342. * @desc: The parameter description to validate.
  343. */
  344. bool fs_validate_description(const char *name,
  345. const struct fs_parameter_spec *desc)
  346. {
  347. const struct fs_parameter_spec *param, *p2;
  348. bool good = true;
  349. for (param = desc; param->name; param++) {
  350. /* Check for duplicate parameter names */
  351. for (p2 = desc; p2 < param; p2++) {
  352. if (strcmp(param->name, p2->name) == 0) {
  353. if (is_flag(param) != is_flag(p2))
  354. continue;
  355. pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
  356. name, param->name);
  357. good = false;
  358. }
  359. }
  360. }
  361. return good;
  362. }
  363. #endif /* CONFIG_VALIDATE_FS_PARSER */