fs_parser.c 11 KB

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