fs_parser.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #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. * @_path: The result of the lookup
  128. */
  129. int fs_lookup_param(struct fs_context *fc,
  130. struct fs_parameter *param,
  131. bool want_bdev,
  132. struct path *_path)
  133. {
  134. struct filename *f;
  135. unsigned int flags = 0;
  136. bool put_f;
  137. int ret;
  138. switch (param->type) {
  139. case fs_value_is_string:
  140. f = getname_kernel(param->string);
  141. if (IS_ERR(f))
  142. return PTR_ERR(f);
  143. put_f = true;
  144. break;
  145. case fs_value_is_filename:
  146. f = param->name;
  147. put_f = false;
  148. break;
  149. default:
  150. return invalf(fc, "%s: not usable as path", param->key);
  151. }
  152. f->refcnt++; /* filename_lookup() drops our ref. */
  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. b = lookup_constant(bool_names, param->string, -1);
  184. if (b == -1)
  185. return fs_param_bad_value(log, param);
  186. result->boolean = b;
  187. return 0;
  188. }
  189. EXPORT_SYMBOL(fs_param_is_bool);
  190. int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p,
  191. struct fs_parameter *param, struct fs_parse_result *result)
  192. {
  193. int base = (unsigned long)p->data;
  194. if (param->type != fs_value_is_string ||
  195. kstrtouint(param->string, base, &result->uint_32) < 0)
  196. return fs_param_bad_value(log, param);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(fs_param_is_u32);
  200. int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p,
  201. struct fs_parameter *param, struct fs_parse_result *result)
  202. {
  203. if (param->type != fs_value_is_string ||
  204. kstrtoint(param->string, 0, &result->int_32) < 0)
  205. return fs_param_bad_value(log, param);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(fs_param_is_s32);
  209. int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p,
  210. struct fs_parameter *param, struct fs_parse_result *result)
  211. {
  212. if (param->type != fs_value_is_string ||
  213. kstrtoull(param->string, 0, &result->uint_64) < 0)
  214. return fs_param_bad_value(log, param);
  215. return 0;
  216. }
  217. EXPORT_SYMBOL(fs_param_is_u64);
  218. int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p,
  219. struct fs_parameter *param, struct fs_parse_result *result)
  220. {
  221. const struct constant_table *c;
  222. if (param->type != fs_value_is_string)
  223. return fs_param_bad_value(log, param);
  224. c = __lookup_constant(p->data, param->string);
  225. if (!c)
  226. return fs_param_bad_value(log, param);
  227. result->uint_32 = c->value;
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(fs_param_is_enum);
  231. int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p,
  232. struct fs_parameter *param, struct fs_parse_result *result)
  233. {
  234. if (param->type != fs_value_is_string || !*param->string)
  235. return fs_param_bad_value(log, param);
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(fs_param_is_string);
  239. int fs_param_is_blob(struct p_log *log, const struct fs_parameter_spec *p,
  240. struct fs_parameter *param, struct fs_parse_result *result)
  241. {
  242. if (param->type != fs_value_is_blob)
  243. return fs_param_bad_value(log, param);
  244. return 0;
  245. }
  246. EXPORT_SYMBOL(fs_param_is_blob);
  247. int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p,
  248. struct fs_parameter *param, struct fs_parse_result *result)
  249. {
  250. switch (param->type) {
  251. case fs_value_is_string:
  252. if (kstrtouint(param->string, 0, &result->uint_32) < 0)
  253. break;
  254. if (result->uint_32 <= INT_MAX)
  255. return 0;
  256. break;
  257. case fs_value_is_file:
  258. result->uint_32 = param->dirfd;
  259. if (result->uint_32 <= INT_MAX)
  260. return 0;
  261. break;
  262. default:
  263. break;
  264. }
  265. return fs_param_bad_value(log, param);
  266. }
  267. EXPORT_SYMBOL(fs_param_is_fd);
  268. int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p,
  269. struct fs_parameter *param, struct fs_parse_result *result)
  270. {
  271. return 0;
  272. }
  273. EXPORT_SYMBOL(fs_param_is_blockdev);
  274. int fs_param_is_path(struct p_log *log, const struct fs_parameter_spec *p,
  275. struct fs_parameter *param, struct fs_parse_result *result)
  276. {
  277. return 0;
  278. }
  279. EXPORT_SYMBOL(fs_param_is_path);
  280. #ifdef CONFIG_VALIDATE_FS_PARSER
  281. /**
  282. * validate_constant_table - Validate a constant table
  283. * @name: Name to use in reporting
  284. * @tbl: The constant table to validate.
  285. * @tbl_size: The size of the table.
  286. * @low: The lowest permissible value.
  287. * @high: The highest permissible value.
  288. * @special: One special permissible value outside of the range.
  289. */
  290. bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
  291. int low, int high, int special)
  292. {
  293. size_t i;
  294. bool good = true;
  295. if (tbl_size == 0) {
  296. pr_warn("VALIDATE C-TBL: Empty\n");
  297. return true;
  298. }
  299. for (i = 0; i < tbl_size; i++) {
  300. if (!tbl[i].name) {
  301. pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
  302. good = false;
  303. } else if (i > 0 && tbl[i - 1].name) {
  304. int c = strcmp(tbl[i-1].name, tbl[i].name);
  305. if (c == 0) {
  306. pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
  307. i, tbl[i].name);
  308. good = false;
  309. }
  310. if (c > 0) {
  311. pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
  312. i, tbl[i-1].name, tbl[i].name);
  313. good = false;
  314. }
  315. }
  316. if (tbl[i].value != special &&
  317. (tbl[i].value < low || tbl[i].value > high)) {
  318. pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
  319. i, tbl[i].name, tbl[i].value, low, high);
  320. good = false;
  321. }
  322. }
  323. return good;
  324. }
  325. /**
  326. * fs_validate_description - Validate a parameter description
  327. * @desc: The parameter description to validate.
  328. */
  329. bool fs_validate_description(const char *name,
  330. const struct fs_parameter_spec *desc)
  331. {
  332. const struct fs_parameter_spec *param, *p2;
  333. bool good = true;
  334. for (param = desc; param->name; param++) {
  335. /* Check for duplicate parameter names */
  336. for (p2 = desc; p2 < param; p2++) {
  337. if (strcmp(param->name, p2->name) == 0) {
  338. if (is_flag(param) != is_flag(p2))
  339. continue;
  340. pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
  341. name, param->name);
  342. good = false;
  343. }
  344. }
  345. }
  346. return good;
  347. }
  348. #endif /* CONFIG_VALIDATE_FS_PARSER */