params.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/fs.h>
  3. #include <linux/module.h>
  4. #include <linux/namei.h>
  5. #include <linux/fs_context.h>
  6. #include <linux/fs_parser.h>
  7. #include <linux/posix_acl_xattr.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/xattr.h>
  10. #include "overlayfs.h"
  11. #include "params.h"
  12. static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
  13. module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
  14. MODULE_PARM_DESC(redirect_dir,
  15. "Default to on or off for the redirect_dir feature");
  16. static bool ovl_redirect_always_follow =
  17. IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
  18. module_param_named(redirect_always_follow, ovl_redirect_always_follow,
  19. bool, 0644);
  20. MODULE_PARM_DESC(redirect_always_follow,
  21. "Follow redirects even if redirect_dir feature is turned off");
  22. static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
  23. module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
  24. MODULE_PARM_DESC(xino_auto,
  25. "Auto enable xino feature");
  26. static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
  27. module_param_named(index, ovl_index_def, bool, 0644);
  28. MODULE_PARM_DESC(index,
  29. "Default to on or off for the inodes index feature");
  30. static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
  31. module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
  32. MODULE_PARM_DESC(nfs_export,
  33. "Default to on or off for the NFS export feature");
  34. static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
  35. module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
  36. MODULE_PARM_DESC(metacopy,
  37. "Default to on or off for the metadata only copy up feature");
  38. enum ovl_opt {
  39. Opt_lowerdir,
  40. Opt_lowerdir_add,
  41. Opt_datadir_add,
  42. Opt_upperdir,
  43. Opt_workdir,
  44. Opt_default_permissions,
  45. Opt_redirect_dir,
  46. Opt_index,
  47. Opt_uuid,
  48. Opt_nfs_export,
  49. Opt_userxattr,
  50. Opt_xino,
  51. Opt_metacopy,
  52. Opt_verity,
  53. Opt_volatile,
  54. };
  55. static const struct constant_table ovl_parameter_bool[] = {
  56. { "on", true },
  57. { "off", false },
  58. {}
  59. };
  60. static const struct constant_table ovl_parameter_uuid[] = {
  61. { "off", OVL_UUID_OFF },
  62. { "null", OVL_UUID_NULL },
  63. { "auto", OVL_UUID_AUTO },
  64. { "on", OVL_UUID_ON },
  65. {}
  66. };
  67. static const char *ovl_uuid_mode(struct ovl_config *config)
  68. {
  69. return ovl_parameter_uuid[config->uuid].name;
  70. }
  71. static int ovl_uuid_def(void)
  72. {
  73. return OVL_UUID_AUTO;
  74. }
  75. static const struct constant_table ovl_parameter_xino[] = {
  76. { "off", OVL_XINO_OFF },
  77. { "auto", OVL_XINO_AUTO },
  78. { "on", OVL_XINO_ON },
  79. {}
  80. };
  81. const char *ovl_xino_mode(struct ovl_config *config)
  82. {
  83. return ovl_parameter_xino[config->xino].name;
  84. }
  85. static int ovl_xino_def(void)
  86. {
  87. return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
  88. }
  89. const struct constant_table ovl_parameter_redirect_dir[] = {
  90. { "off", OVL_REDIRECT_OFF },
  91. { "follow", OVL_REDIRECT_FOLLOW },
  92. { "nofollow", OVL_REDIRECT_NOFOLLOW },
  93. { "on", OVL_REDIRECT_ON },
  94. {}
  95. };
  96. static const char *ovl_redirect_mode(struct ovl_config *config)
  97. {
  98. return ovl_parameter_redirect_dir[config->redirect_mode].name;
  99. }
  100. static int ovl_redirect_mode_def(void)
  101. {
  102. return ovl_redirect_dir_def ? OVL_REDIRECT_ON :
  103. ovl_redirect_always_follow ? OVL_REDIRECT_FOLLOW :
  104. OVL_REDIRECT_NOFOLLOW;
  105. }
  106. static const struct constant_table ovl_parameter_verity[] = {
  107. { "off", OVL_VERITY_OFF },
  108. { "on", OVL_VERITY_ON },
  109. { "require", OVL_VERITY_REQUIRE },
  110. {}
  111. };
  112. static const char *ovl_verity_mode(struct ovl_config *config)
  113. {
  114. return ovl_parameter_verity[config->verity_mode].name;
  115. }
  116. static int ovl_verity_mode_def(void)
  117. {
  118. return OVL_VERITY_OFF;
  119. }
  120. const struct fs_parameter_spec ovl_parameter_spec[] = {
  121. fsparam_string_empty("lowerdir", Opt_lowerdir),
  122. fsparam_string("lowerdir+", Opt_lowerdir_add),
  123. fsparam_string("datadir+", Opt_datadir_add),
  124. fsparam_string("upperdir", Opt_upperdir),
  125. fsparam_string("workdir", Opt_workdir),
  126. fsparam_flag("default_permissions", Opt_default_permissions),
  127. fsparam_enum("redirect_dir", Opt_redirect_dir, ovl_parameter_redirect_dir),
  128. fsparam_enum("index", Opt_index, ovl_parameter_bool),
  129. fsparam_enum("uuid", Opt_uuid, ovl_parameter_uuid),
  130. fsparam_enum("nfs_export", Opt_nfs_export, ovl_parameter_bool),
  131. fsparam_flag("userxattr", Opt_userxattr),
  132. fsparam_enum("xino", Opt_xino, ovl_parameter_xino),
  133. fsparam_enum("metacopy", Opt_metacopy, ovl_parameter_bool),
  134. fsparam_enum("verity", Opt_verity, ovl_parameter_verity),
  135. fsparam_flag("volatile", Opt_volatile),
  136. {}
  137. };
  138. static char *ovl_next_opt(char **s)
  139. {
  140. char *sbegin = *s;
  141. char *p;
  142. if (sbegin == NULL)
  143. return NULL;
  144. for (p = sbegin; *p; p++) {
  145. if (*p == '\\') {
  146. p++;
  147. if (!*p)
  148. break;
  149. } else if (*p == ',') {
  150. *p = '\0';
  151. *s = p + 1;
  152. return sbegin;
  153. }
  154. }
  155. *s = NULL;
  156. return sbegin;
  157. }
  158. static int ovl_parse_monolithic(struct fs_context *fc, void *data)
  159. {
  160. return vfs_parse_monolithic_sep(fc, data, ovl_next_opt);
  161. }
  162. static ssize_t ovl_parse_param_split_lowerdirs(char *str)
  163. {
  164. ssize_t nr_layers = 1, nr_colons = 0;
  165. char *s, *d;
  166. for (s = d = str;; s++, d++) {
  167. if (*s == '\\') {
  168. /* keep esc chars in split lowerdir */
  169. *d++ = *s++;
  170. } else if (*s == ':') {
  171. bool next_colon = (*(s + 1) == ':');
  172. nr_colons++;
  173. if (nr_colons == 2 && next_colon) {
  174. pr_err("only single ':' or double '::' sequences of unescaped colons in lowerdir mount option allowed.\n");
  175. return -EINVAL;
  176. }
  177. /* count layers, not colons */
  178. if (!next_colon)
  179. nr_layers++;
  180. *d = '\0';
  181. continue;
  182. }
  183. *d = *s;
  184. if (!*s) {
  185. /* trailing colons */
  186. if (nr_colons) {
  187. pr_err("unescaped trailing colons in lowerdir mount option.\n");
  188. return -EINVAL;
  189. }
  190. break;
  191. }
  192. nr_colons = 0;
  193. }
  194. return nr_layers;
  195. }
  196. static int ovl_mount_dir_noesc(const char *name, struct path *path)
  197. {
  198. int err = -EINVAL;
  199. if (!*name) {
  200. pr_err("empty lowerdir\n");
  201. goto out;
  202. }
  203. err = kern_path(name, LOOKUP_FOLLOW, path);
  204. if (err) {
  205. pr_err("failed to resolve '%s': %i\n", name, err);
  206. goto out;
  207. }
  208. return 0;
  209. out:
  210. return err;
  211. }
  212. static void ovl_unescape(char *s)
  213. {
  214. char *d = s;
  215. for (;; s++, d++) {
  216. if (*s == '\\')
  217. s++;
  218. *d = *s;
  219. if (!*s)
  220. break;
  221. }
  222. }
  223. static int ovl_mount_dir(const char *name, struct path *path)
  224. {
  225. int err = -ENOMEM;
  226. char *tmp = kstrdup(name, GFP_KERNEL);
  227. if (tmp) {
  228. ovl_unescape(tmp);
  229. err = ovl_mount_dir_noesc(tmp, path);
  230. kfree(tmp);
  231. }
  232. return err;
  233. }
  234. static int ovl_mount_dir_check(struct fs_context *fc, const struct path *path,
  235. enum ovl_opt layer, const char *name, bool upper)
  236. {
  237. struct ovl_fs_context *ctx = fc->fs_private;
  238. if (!d_is_dir(path->dentry))
  239. return invalfc(fc, "%s is not a directory", name);
  240. /*
  241. * Root dentries of case-insensitive capable filesystems might
  242. * not have the dentry operations set, but still be incompatible
  243. * with overlayfs. Check explicitly to prevent post-mount
  244. * failures.
  245. */
  246. if (sb_has_encoding(path->mnt->mnt_sb))
  247. return invalfc(fc, "case-insensitive capable filesystem on %s not supported", name);
  248. if (ovl_dentry_weird(path->dentry))
  249. return invalfc(fc, "filesystem on %s not supported", name);
  250. /*
  251. * Check whether upper path is read-only here to report failures
  252. * early. Don't forget to recheck when the superblock is created
  253. * as the mount attributes could change.
  254. */
  255. if (upper) {
  256. if (path->dentry->d_flags & DCACHE_OP_REAL)
  257. return invalfc(fc, "filesystem on %s not supported as upperdir", name);
  258. if (__mnt_is_readonly(path->mnt))
  259. return invalfc(fc, "filesystem on %s is read-only", name);
  260. } else {
  261. if (ctx->lowerdir_all && layer != Opt_lowerdir)
  262. return invalfc(fc, "lowerdir+ and datadir+ cannot follow lowerdir");
  263. if (ctx->nr_data && layer == Opt_lowerdir_add)
  264. return invalfc(fc, "regular lower layers cannot follow data layers");
  265. if (ctx->nr == OVL_MAX_STACK)
  266. return invalfc(fc, "too many lower directories, limit is %d",
  267. OVL_MAX_STACK);
  268. }
  269. return 0;
  270. }
  271. static int ovl_ctx_realloc_lower(struct fs_context *fc)
  272. {
  273. struct ovl_fs_context *ctx = fc->fs_private;
  274. struct ovl_fs_context_layer *l;
  275. size_t nr;
  276. if (ctx->nr < ctx->capacity)
  277. return 0;
  278. nr = min_t(size_t, max(4096 / sizeof(*l), ctx->capacity * 2),
  279. OVL_MAX_STACK);
  280. l = krealloc_array(ctx->lower, nr, sizeof(*l), GFP_KERNEL_ACCOUNT);
  281. if (!l)
  282. return -ENOMEM;
  283. ctx->lower = l;
  284. ctx->capacity = nr;
  285. return 0;
  286. }
  287. static void ovl_add_layer(struct fs_context *fc, enum ovl_opt layer,
  288. struct path *path, char **pname)
  289. {
  290. struct ovl_fs *ofs = fc->s_fs_info;
  291. struct ovl_config *config = &ofs->config;
  292. struct ovl_fs_context *ctx = fc->fs_private;
  293. struct ovl_fs_context_layer *l;
  294. switch (layer) {
  295. case Opt_workdir:
  296. swap(config->workdir, *pname);
  297. swap(ctx->work, *path);
  298. break;
  299. case Opt_upperdir:
  300. swap(config->upperdir, *pname);
  301. swap(ctx->upper, *path);
  302. break;
  303. case Opt_datadir_add:
  304. ctx->nr_data++;
  305. fallthrough;
  306. case Opt_lowerdir:
  307. fallthrough;
  308. case Opt_lowerdir_add:
  309. WARN_ON(ctx->nr >= ctx->capacity);
  310. l = &ctx->lower[ctx->nr++];
  311. memset(l, 0, sizeof(*l));
  312. swap(l->name, *pname);
  313. swap(l->path, *path);
  314. break;
  315. default:
  316. WARN_ON(1);
  317. }
  318. }
  319. static int ovl_parse_layer(struct fs_context *fc, const char *layer_name, enum ovl_opt layer)
  320. {
  321. char *name = kstrdup(layer_name, GFP_KERNEL);
  322. bool upper = (layer == Opt_upperdir || layer == Opt_workdir);
  323. struct path path;
  324. int err;
  325. if (!name)
  326. return -ENOMEM;
  327. if (upper || layer == Opt_lowerdir)
  328. err = ovl_mount_dir(name, &path);
  329. else
  330. err = ovl_mount_dir_noesc(name, &path);
  331. if (err)
  332. goto out_free;
  333. err = ovl_mount_dir_check(fc, &path, layer, name, upper);
  334. if (err)
  335. goto out_put;
  336. if (!upper) {
  337. err = ovl_ctx_realloc_lower(fc);
  338. if (err)
  339. goto out_put;
  340. }
  341. /* Store the user provided path string in ctx to show in mountinfo */
  342. ovl_add_layer(fc, layer, &path, &name);
  343. out_put:
  344. path_put(&path);
  345. out_free:
  346. kfree(name);
  347. return err;
  348. }
  349. static void ovl_reset_lowerdirs(struct ovl_fs_context *ctx)
  350. {
  351. struct ovl_fs_context_layer *l = ctx->lower;
  352. // Reset old user provided lowerdir string
  353. kfree(ctx->lowerdir_all);
  354. ctx->lowerdir_all = NULL;
  355. for (size_t nr = 0; nr < ctx->nr; nr++, l++) {
  356. path_put(&l->path);
  357. kfree(l->name);
  358. l->name = NULL;
  359. }
  360. ctx->nr = 0;
  361. ctx->nr_data = 0;
  362. }
  363. /*
  364. * Parse lowerdir= mount option:
  365. *
  366. * e.g.: lowerdir=/lower1:/lower2:/lower3::/data1::/data2
  367. * Set "/lower1", "/lower2", and "/lower3" as lower layers and
  368. * "/data1" and "/data2" as data lower layers. Any existing lower
  369. * layers are replaced.
  370. */
  371. static int ovl_parse_param_lowerdir(const char *name, struct fs_context *fc)
  372. {
  373. int err;
  374. struct ovl_fs_context *ctx = fc->fs_private;
  375. char *dup = NULL, *iter;
  376. ssize_t nr_lower, nr;
  377. bool data_layer = false;
  378. /*
  379. * Ensure we're backwards compatible with mount(2)
  380. * by allowing relative paths.
  381. */
  382. /* drop all existing lower layers */
  383. ovl_reset_lowerdirs(ctx);
  384. if (!*name)
  385. return 0;
  386. if (*name == ':') {
  387. pr_err("cannot append lower layer\n");
  388. return -EINVAL;
  389. }
  390. // Store user provided lowerdir string to show in mount options
  391. ctx->lowerdir_all = kstrdup(name, GFP_KERNEL);
  392. if (!ctx->lowerdir_all)
  393. return -ENOMEM;
  394. dup = kstrdup(name, GFP_KERNEL);
  395. if (!dup)
  396. return -ENOMEM;
  397. err = -EINVAL;
  398. nr_lower = ovl_parse_param_split_lowerdirs(dup);
  399. if (nr_lower < 0)
  400. goto out_err;
  401. if (nr_lower > OVL_MAX_STACK) {
  402. pr_err("too many lower directories, limit is %d\n", OVL_MAX_STACK);
  403. goto out_err;
  404. }
  405. iter = dup;
  406. for (nr = 0; nr < nr_lower; nr++) {
  407. err = ovl_parse_layer(fc, iter, Opt_lowerdir);
  408. if (err)
  409. goto out_err;
  410. if (data_layer)
  411. ctx->nr_data++;
  412. /* Calling strchr() again would overrun. */
  413. if (ctx->nr == nr_lower)
  414. break;
  415. err = -EINVAL;
  416. iter = strchr(iter, '\0') + 1;
  417. if (*iter) {
  418. /*
  419. * This is a regular layer so we require that
  420. * there are no data layers.
  421. */
  422. if (ctx->nr_data > 0) {
  423. pr_err("regular lower layers cannot follow data lower layers\n");
  424. goto out_err;
  425. }
  426. data_layer = false;
  427. continue;
  428. }
  429. /* This is a data lower layer. */
  430. data_layer = true;
  431. iter++;
  432. }
  433. kfree(dup);
  434. return 0;
  435. out_err:
  436. kfree(dup);
  437. /* Intentionally don't realloc to a smaller size. */
  438. return err;
  439. }
  440. static int ovl_parse_param(struct fs_context *fc, struct fs_parameter *param)
  441. {
  442. int err = 0;
  443. struct fs_parse_result result;
  444. struct ovl_fs *ofs = fc->s_fs_info;
  445. struct ovl_config *config = &ofs->config;
  446. struct ovl_fs_context *ctx = fc->fs_private;
  447. int opt;
  448. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  449. /*
  450. * On remount overlayfs has always ignored all mount
  451. * options no matter if malformed or not so for
  452. * backwards compatibility we do the same here.
  453. */
  454. if (fc->oldapi)
  455. return 0;
  456. /*
  457. * Give us the freedom to allow changing mount options
  458. * with the new mount api in the future. So instead of
  459. * silently ignoring everything we report a proper
  460. * error. This is only visible for users of the new
  461. * mount api.
  462. */
  463. return invalfc(fc, "No changes allowed in reconfigure");
  464. }
  465. opt = fs_parse(fc, ovl_parameter_spec, param, &result);
  466. if (opt < 0)
  467. return opt;
  468. switch (opt) {
  469. case Opt_lowerdir:
  470. err = ovl_parse_param_lowerdir(param->string, fc);
  471. break;
  472. case Opt_lowerdir_add:
  473. case Opt_datadir_add:
  474. case Opt_upperdir:
  475. case Opt_workdir:
  476. err = ovl_parse_layer(fc, param->string, opt);
  477. break;
  478. case Opt_default_permissions:
  479. config->default_permissions = true;
  480. break;
  481. case Opt_redirect_dir:
  482. config->redirect_mode = result.uint_32;
  483. if (config->redirect_mode == OVL_REDIRECT_OFF) {
  484. config->redirect_mode = ovl_redirect_always_follow ?
  485. OVL_REDIRECT_FOLLOW :
  486. OVL_REDIRECT_NOFOLLOW;
  487. }
  488. ctx->set.redirect = true;
  489. break;
  490. case Opt_index:
  491. config->index = result.uint_32;
  492. ctx->set.index = true;
  493. break;
  494. case Opt_uuid:
  495. config->uuid = result.uint_32;
  496. break;
  497. case Opt_nfs_export:
  498. config->nfs_export = result.uint_32;
  499. ctx->set.nfs_export = true;
  500. break;
  501. case Opt_xino:
  502. config->xino = result.uint_32;
  503. break;
  504. case Opt_metacopy:
  505. config->metacopy = result.uint_32;
  506. ctx->set.metacopy = true;
  507. break;
  508. case Opt_verity:
  509. config->verity_mode = result.uint_32;
  510. break;
  511. case Opt_volatile:
  512. config->ovl_volatile = true;
  513. break;
  514. case Opt_userxattr:
  515. config->userxattr = true;
  516. break;
  517. default:
  518. pr_err("unrecognized mount option \"%s\" or missing value\n",
  519. param->key);
  520. return -EINVAL;
  521. }
  522. return err;
  523. }
  524. static int ovl_get_tree(struct fs_context *fc)
  525. {
  526. return get_tree_nodev(fc, ovl_fill_super);
  527. }
  528. static inline void ovl_fs_context_free(struct ovl_fs_context *ctx)
  529. {
  530. ovl_reset_lowerdirs(ctx);
  531. path_put(&ctx->upper);
  532. path_put(&ctx->work);
  533. kfree(ctx->lower);
  534. kfree(ctx);
  535. }
  536. static void ovl_free(struct fs_context *fc)
  537. {
  538. struct ovl_fs *ofs = fc->s_fs_info;
  539. struct ovl_fs_context *ctx = fc->fs_private;
  540. /*
  541. * ofs is stored in the fs_context when it is initialized.
  542. * ofs is transferred to the superblock on a successful mount,
  543. * but if an error occurs before the transfer we have to free
  544. * it here.
  545. */
  546. if (ofs)
  547. ovl_free_fs(ofs);
  548. if (ctx)
  549. ovl_fs_context_free(ctx);
  550. }
  551. static int ovl_reconfigure(struct fs_context *fc)
  552. {
  553. struct super_block *sb = fc->root->d_sb;
  554. struct ovl_fs *ofs = OVL_FS(sb);
  555. struct super_block *upper_sb;
  556. int ret = 0;
  557. if (!(fc->sb_flags & SB_RDONLY) && ovl_force_readonly(ofs))
  558. return -EROFS;
  559. if (fc->sb_flags & SB_RDONLY && !sb_rdonly(sb)) {
  560. upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
  561. if (ovl_should_sync(ofs)) {
  562. down_read(&upper_sb->s_umount);
  563. ret = sync_filesystem(upper_sb);
  564. up_read(&upper_sb->s_umount);
  565. }
  566. }
  567. return ret;
  568. }
  569. static const struct fs_context_operations ovl_context_ops = {
  570. .parse_monolithic = ovl_parse_monolithic,
  571. .parse_param = ovl_parse_param,
  572. .get_tree = ovl_get_tree,
  573. .reconfigure = ovl_reconfigure,
  574. .free = ovl_free,
  575. };
  576. /*
  577. * This is called during fsopen() and will record the user namespace of
  578. * the caller in fc->user_ns since we've raised FS_USERNS_MOUNT. We'll
  579. * need it when we actually create the superblock to verify that the
  580. * process creating the superblock is in the same user namespace as
  581. * process that called fsopen().
  582. */
  583. int ovl_init_fs_context(struct fs_context *fc)
  584. {
  585. struct ovl_fs_context *ctx;
  586. struct ovl_fs *ofs;
  587. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
  588. if (!ctx)
  589. return -ENOMEM;
  590. /*
  591. * By default we allocate for three lower layers. It's likely
  592. * that it'll cover most users.
  593. */
  594. ctx->lower = kmalloc_array(3, sizeof(*ctx->lower), GFP_KERNEL_ACCOUNT);
  595. if (!ctx->lower)
  596. goto out_err;
  597. ctx->capacity = 3;
  598. ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  599. if (!ofs)
  600. goto out_err;
  601. ofs->config.redirect_mode = ovl_redirect_mode_def();
  602. ofs->config.index = ovl_index_def;
  603. ofs->config.uuid = ovl_uuid_def();
  604. ofs->config.nfs_export = ovl_nfs_export_def;
  605. ofs->config.xino = ovl_xino_def();
  606. ofs->config.metacopy = ovl_metacopy_def;
  607. fc->s_fs_info = ofs;
  608. fc->fs_private = ctx;
  609. fc->ops = &ovl_context_ops;
  610. return 0;
  611. out_err:
  612. ovl_fs_context_free(ctx);
  613. return -ENOMEM;
  614. }
  615. void ovl_free_fs(struct ovl_fs *ofs)
  616. {
  617. struct vfsmount **mounts;
  618. unsigned i;
  619. iput(ofs->workbasedir_trap);
  620. iput(ofs->workdir_trap);
  621. dput(ofs->whiteout);
  622. dput(ofs->workdir);
  623. if (ofs->workdir_locked)
  624. ovl_inuse_unlock(ofs->workbasedir);
  625. dput(ofs->workbasedir);
  626. if (ofs->upperdir_locked)
  627. ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
  628. /* Reuse ofs->config.lowerdirs as a vfsmount array before freeing it */
  629. mounts = (struct vfsmount **) ofs->config.lowerdirs;
  630. for (i = 0; i < ofs->numlayer; i++) {
  631. iput(ofs->layers[i].trap);
  632. kfree(ofs->config.lowerdirs[i]);
  633. mounts[i] = ofs->layers[i].mnt;
  634. }
  635. kern_unmount_array(mounts, ofs->numlayer);
  636. kfree(ofs->layers);
  637. for (i = 0; i < ofs->numfs; i++)
  638. free_anon_bdev(ofs->fs[i].pseudo_dev);
  639. kfree(ofs->fs);
  640. kfree(ofs->config.lowerdirs);
  641. kfree(ofs->config.upperdir);
  642. kfree(ofs->config.workdir);
  643. if (ofs->creator_cred)
  644. put_cred(ofs->creator_cred);
  645. kfree(ofs);
  646. }
  647. int ovl_fs_params_verify(const struct ovl_fs_context *ctx,
  648. struct ovl_config *config)
  649. {
  650. struct ovl_opt_set set = ctx->set;
  651. /* Workdir/index are useless in non-upper mount */
  652. if (!config->upperdir) {
  653. if (config->workdir) {
  654. pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
  655. config->workdir);
  656. kfree(config->workdir);
  657. config->workdir = NULL;
  658. }
  659. if (config->index && set.index) {
  660. pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
  661. set.index = false;
  662. }
  663. config->index = false;
  664. }
  665. if (!config->upperdir && config->ovl_volatile) {
  666. pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
  667. config->ovl_volatile = false;
  668. }
  669. if (!config->upperdir && config->uuid == OVL_UUID_ON) {
  670. pr_info("option \"uuid=on\" requires an upper fs, falling back to uuid=null.\n");
  671. config->uuid = OVL_UUID_NULL;
  672. }
  673. /* Resolve verity -> metacopy dependency */
  674. if (config->verity_mode && !config->metacopy) {
  675. /* Don't allow explicit specified conflicting combinations */
  676. if (set.metacopy) {
  677. pr_err("conflicting options: metacopy=off,verity=%s\n",
  678. ovl_verity_mode(config));
  679. return -EINVAL;
  680. }
  681. /* Otherwise automatically enable metacopy. */
  682. config->metacopy = true;
  683. }
  684. /*
  685. * This is to make the logic below simpler. It doesn't make any other
  686. * difference, since redirect_dir=on is only used for upper.
  687. */
  688. if (!config->upperdir && config->redirect_mode == OVL_REDIRECT_FOLLOW)
  689. config->redirect_mode = OVL_REDIRECT_ON;
  690. /* Resolve verity -> metacopy -> redirect_dir dependency */
  691. if (config->metacopy && config->redirect_mode != OVL_REDIRECT_ON) {
  692. if (set.metacopy && set.redirect) {
  693. pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
  694. ovl_redirect_mode(config));
  695. return -EINVAL;
  696. }
  697. if (config->verity_mode && set.redirect) {
  698. pr_err("conflicting options: verity=%s,redirect_dir=%s\n",
  699. ovl_verity_mode(config), ovl_redirect_mode(config));
  700. return -EINVAL;
  701. }
  702. if (set.redirect) {
  703. /*
  704. * There was an explicit redirect_dir=... that resulted
  705. * in this conflict.
  706. */
  707. pr_info("disabling metacopy due to redirect_dir=%s\n",
  708. ovl_redirect_mode(config));
  709. config->metacopy = false;
  710. } else {
  711. /* Automatically enable redirect otherwise. */
  712. config->redirect_mode = OVL_REDIRECT_ON;
  713. }
  714. }
  715. /* Resolve nfs_export -> index dependency */
  716. if (config->nfs_export && !config->index) {
  717. if (!config->upperdir &&
  718. config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
  719. pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
  720. config->nfs_export = false;
  721. } else if (set.nfs_export && set.index) {
  722. pr_err("conflicting options: nfs_export=on,index=off\n");
  723. return -EINVAL;
  724. } else if (set.index) {
  725. /*
  726. * There was an explicit index=off that resulted
  727. * in this conflict.
  728. */
  729. pr_info("disabling nfs_export due to index=off\n");
  730. config->nfs_export = false;
  731. } else {
  732. /* Automatically enable index otherwise. */
  733. config->index = true;
  734. }
  735. }
  736. /* Resolve nfs_export -> !metacopy && !verity dependency */
  737. if (config->nfs_export && config->metacopy) {
  738. if (set.nfs_export && set.metacopy) {
  739. pr_err("conflicting options: nfs_export=on,metacopy=on\n");
  740. return -EINVAL;
  741. }
  742. if (set.metacopy) {
  743. /*
  744. * There was an explicit metacopy=on that resulted
  745. * in this conflict.
  746. */
  747. pr_info("disabling nfs_export due to metacopy=on\n");
  748. config->nfs_export = false;
  749. } else if (config->verity_mode) {
  750. /*
  751. * There was an explicit verity=.. that resulted
  752. * in this conflict.
  753. */
  754. pr_info("disabling nfs_export due to verity=%s\n",
  755. ovl_verity_mode(config));
  756. config->nfs_export = false;
  757. } else {
  758. /*
  759. * There was an explicit nfs_export=on that resulted
  760. * in this conflict.
  761. */
  762. pr_info("disabling metacopy due to nfs_export=on\n");
  763. config->metacopy = false;
  764. }
  765. }
  766. /* Resolve userxattr -> !redirect && !metacopy && !verity dependency */
  767. if (config->userxattr) {
  768. if (set.redirect &&
  769. config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
  770. pr_err("conflicting options: userxattr,redirect_dir=%s\n",
  771. ovl_redirect_mode(config));
  772. return -EINVAL;
  773. }
  774. if (config->metacopy && set.metacopy) {
  775. pr_err("conflicting options: userxattr,metacopy=on\n");
  776. return -EINVAL;
  777. }
  778. if (config->verity_mode) {
  779. pr_err("conflicting options: userxattr,verity=%s\n",
  780. ovl_verity_mode(config));
  781. return -EINVAL;
  782. }
  783. /*
  784. * Silently disable default setting of redirect and metacopy.
  785. * This shall be the default in the future as well: these
  786. * options must be explicitly enabled if used together with
  787. * userxattr.
  788. */
  789. config->redirect_mode = OVL_REDIRECT_NOFOLLOW;
  790. config->metacopy = false;
  791. }
  792. /*
  793. * Fail if we don't have trusted xattr capability and a feature was
  794. * explicitly requested that requires them.
  795. */
  796. if (!config->userxattr && !capable(CAP_SYS_ADMIN)) {
  797. if (set.redirect &&
  798. config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
  799. pr_err("redirect_dir requires permission to access trusted xattrs\n");
  800. return -EPERM;
  801. }
  802. if (config->metacopy && set.metacopy) {
  803. pr_err("metacopy requires permission to access trusted xattrs\n");
  804. return -EPERM;
  805. }
  806. if (config->verity_mode) {
  807. pr_err("verity requires permission to access trusted xattrs\n");
  808. return -EPERM;
  809. }
  810. if (ctx->nr_data > 0) {
  811. pr_err("lower data-only dirs require permission to access trusted xattrs\n");
  812. return -EPERM;
  813. }
  814. /*
  815. * Other xattr-dependent features should be disabled without
  816. * great disturbance to the user in ovl_make_workdir().
  817. */
  818. }
  819. if (ctx->nr_data > 0 && !config->metacopy) {
  820. pr_err("lower data-only dirs require metacopy support.\n");
  821. return -EINVAL;
  822. }
  823. return 0;
  824. }
  825. /**
  826. * ovl_show_options
  827. * @m: the seq_file handle
  828. * @dentry: The dentry to query
  829. *
  830. * Prints the mount options for a given superblock.
  831. * Returns zero; does not fail.
  832. */
  833. int ovl_show_options(struct seq_file *m, struct dentry *dentry)
  834. {
  835. struct super_block *sb = dentry->d_sb;
  836. struct ovl_fs *ofs = OVL_FS(sb);
  837. size_t nr, nr_merged_lower, nr_lower = 0;
  838. char **lowerdirs = ofs->config.lowerdirs;
  839. /*
  840. * lowerdirs[0] holds the colon separated list that user provided
  841. * with lowerdir mount option.
  842. * lowerdirs[1..numlayer] hold the lowerdir paths that were added
  843. * using the lowerdir+ and datadir+ mount options.
  844. * For now, we do not allow mixing the legacy lowerdir mount option
  845. * with the new lowerdir+ and datadir+ mount options.
  846. */
  847. if (lowerdirs[0]) {
  848. seq_show_option(m, "lowerdir", lowerdirs[0]);
  849. } else {
  850. nr_lower = ofs->numlayer;
  851. nr_merged_lower = nr_lower - ofs->numdatalayer;
  852. }
  853. for (nr = 1; nr < nr_lower; nr++) {
  854. if (nr < nr_merged_lower)
  855. seq_show_option(m, "lowerdir+", lowerdirs[nr]);
  856. else
  857. seq_show_option(m, "datadir+", lowerdirs[nr]);
  858. }
  859. if (ofs->config.upperdir) {
  860. seq_show_option(m, "upperdir", ofs->config.upperdir);
  861. seq_show_option(m, "workdir", ofs->config.workdir);
  862. }
  863. if (ofs->config.default_permissions)
  864. seq_puts(m, ",default_permissions");
  865. if (ofs->config.redirect_mode != ovl_redirect_mode_def())
  866. seq_printf(m, ",redirect_dir=%s",
  867. ovl_redirect_mode(&ofs->config));
  868. if (ofs->config.index != ovl_index_def)
  869. seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
  870. if (ofs->config.uuid != ovl_uuid_def())
  871. seq_printf(m, ",uuid=%s", ovl_uuid_mode(&ofs->config));
  872. if (ofs->config.nfs_export != ovl_nfs_export_def)
  873. seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
  874. "on" : "off");
  875. if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(ofs))
  876. seq_printf(m, ",xino=%s", ovl_xino_mode(&ofs->config));
  877. if (ofs->config.metacopy != ovl_metacopy_def)
  878. seq_printf(m, ",metacopy=%s",
  879. ofs->config.metacopy ? "on" : "off");
  880. if (ofs->config.ovl_volatile)
  881. seq_puts(m, ",volatile");
  882. if (ofs->config.userxattr)
  883. seq_puts(m, ",userxattr");
  884. if (ofs->config.verity_mode != ovl_verity_mode_def())
  885. seq_printf(m, ",verity=%s",
  886. ovl_verity_mode(&ofs->config));
  887. return 0;
  888. }