namei.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Copyright (C) 2011 Novell Inc.
  3. * Copyright (C) 2016 Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/cred.h>
  11. #include <linux/ctype.h>
  12. #include <linux/namei.h>
  13. #include <linux/xattr.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/mount.h>
  16. #include <linux/exportfs.h>
  17. #include "overlayfs.h"
  18. struct ovl_lookup_data {
  19. struct super_block *sb;
  20. struct qstr name;
  21. bool is_dir;
  22. bool opaque;
  23. bool stop;
  24. bool last;
  25. char *redirect;
  26. bool metacopy;
  27. };
  28. static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
  29. size_t prelen, const char *post)
  30. {
  31. int res;
  32. char *buf;
  33. buf = ovl_get_redirect_xattr(dentry, prelen + strlen(post));
  34. if (IS_ERR_OR_NULL(buf))
  35. return PTR_ERR(buf);
  36. if (buf[0] == '/') {
  37. /*
  38. * One of the ancestor path elements in an absolute path
  39. * lookup in ovl_lookup_layer() could have been opaque and
  40. * that will stop further lookup in lower layers (d->stop=true)
  41. * But we have found an absolute redirect in decendant path
  42. * element and that should force continue lookup in lower
  43. * layers (reset d->stop).
  44. */
  45. d->stop = false;
  46. } else {
  47. res = strlen(buf) + 1;
  48. memmove(buf + prelen, buf, res);
  49. memcpy(buf, d->name.name, prelen);
  50. }
  51. strcat(buf, post);
  52. kfree(d->redirect);
  53. d->redirect = buf;
  54. d->name.name = d->redirect;
  55. d->name.len = strlen(d->redirect);
  56. return 0;
  57. }
  58. static int ovl_acceptable(void *ctx, struct dentry *dentry)
  59. {
  60. /*
  61. * A non-dir origin may be disconnected, which is fine, because
  62. * we only need it for its unique inode number.
  63. */
  64. if (!d_is_dir(dentry))
  65. return 1;
  66. /* Don't decode a deleted empty directory */
  67. if (d_unhashed(dentry))
  68. return 0;
  69. /* Check if directory belongs to the layer we are decoding from */
  70. return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
  71. }
  72. /*
  73. * Check validity of an overlay file handle buffer.
  74. *
  75. * Return 0 for a valid file handle.
  76. * Return -ENODATA for "origin unknown".
  77. * Return <0 for an invalid file handle.
  78. */
  79. int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
  80. {
  81. if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
  82. return -EINVAL;
  83. if (fh->magic != OVL_FH_MAGIC)
  84. return -EINVAL;
  85. /* Treat larger version and unknown flags as "origin unknown" */
  86. if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
  87. return -ENODATA;
  88. /* Treat endianness mismatch as "origin unknown" */
  89. if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
  90. (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
  91. return -ENODATA;
  92. return 0;
  93. }
  94. static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
  95. {
  96. int res, err;
  97. struct ovl_fh *fh = NULL;
  98. res = vfs_getxattr(dentry, name, NULL, 0);
  99. if (res < 0) {
  100. if (res == -ENODATA || res == -EOPNOTSUPP)
  101. return NULL;
  102. goto fail;
  103. }
  104. /* Zero size value means "copied up but origin unknown" */
  105. if (res == 0)
  106. return NULL;
  107. fh = kzalloc(res, GFP_KERNEL);
  108. if (!fh)
  109. return ERR_PTR(-ENOMEM);
  110. res = vfs_getxattr(dentry, name, fh, res);
  111. if (res < 0)
  112. goto fail;
  113. err = ovl_check_fh_len(fh, res);
  114. if (err < 0) {
  115. if (err == -ENODATA)
  116. goto out;
  117. goto invalid;
  118. }
  119. return fh;
  120. out:
  121. kfree(fh);
  122. return NULL;
  123. fail:
  124. pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
  125. goto out;
  126. invalid:
  127. pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
  128. goto out;
  129. }
  130. struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
  131. bool connected)
  132. {
  133. struct dentry *real;
  134. int bytes;
  135. /*
  136. * Make sure that the stored uuid matches the uuid of the lower
  137. * layer where file handle will be decoded.
  138. */
  139. if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
  140. return NULL;
  141. bytes = (fh->len - offsetof(struct ovl_fh, fid));
  142. real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
  143. bytes >> 2, (int)fh->type,
  144. connected ? ovl_acceptable : NULL, mnt);
  145. if (IS_ERR(real)) {
  146. /*
  147. * Treat stale file handle to lower file as "origin unknown".
  148. * upper file handle could become stale when upper file is
  149. * unlinked and this information is needed to handle stale
  150. * index entries correctly.
  151. */
  152. if (real == ERR_PTR(-ESTALE) &&
  153. !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
  154. real = NULL;
  155. return real;
  156. }
  157. if (ovl_dentry_weird(real)) {
  158. dput(real);
  159. return NULL;
  160. }
  161. return real;
  162. }
  163. static bool ovl_is_opaquedir(struct dentry *dentry)
  164. {
  165. return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
  166. }
  167. static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
  168. const char *name, unsigned int namelen,
  169. size_t prelen, const char *post,
  170. struct dentry **ret)
  171. {
  172. struct dentry *this;
  173. int err;
  174. bool last_element = !post[0];
  175. this = lookup_one_len_unlocked(name, base, namelen);
  176. if (IS_ERR(this)) {
  177. err = PTR_ERR(this);
  178. this = NULL;
  179. if (err == -ENOENT || err == -ENAMETOOLONG)
  180. goto out;
  181. goto out_err;
  182. }
  183. if (!this->d_inode)
  184. goto put_and_out;
  185. if (ovl_dentry_weird(this)) {
  186. /* Don't support traversing automounts and other weirdness */
  187. err = -EREMOTE;
  188. goto out_err;
  189. }
  190. if (ovl_is_whiteout(this)) {
  191. d->stop = d->opaque = true;
  192. goto put_and_out;
  193. }
  194. /*
  195. * This dentry should be a regular file if previous layer lookup
  196. * found a metacopy dentry.
  197. */
  198. if (last_element && d->metacopy && !d_is_reg(this)) {
  199. d->stop = true;
  200. goto put_and_out;
  201. }
  202. if (!d_can_lookup(this)) {
  203. if (d->is_dir || !last_element) {
  204. d->stop = true;
  205. goto put_and_out;
  206. }
  207. err = ovl_check_metacopy_xattr(this);
  208. if (err < 0)
  209. goto out_err;
  210. d->metacopy = err;
  211. d->stop = !d->metacopy;
  212. if (!d->metacopy || d->last)
  213. goto out;
  214. } else {
  215. if (ovl_lookup_trap_inode(d->sb, this)) {
  216. /* Caught in a trap of overlapping layers */
  217. err = -ELOOP;
  218. goto out_err;
  219. }
  220. if (last_element)
  221. d->is_dir = true;
  222. if (d->last)
  223. goto out;
  224. if (ovl_is_opaquedir(this)) {
  225. d->stop = true;
  226. if (last_element)
  227. d->opaque = true;
  228. goto out;
  229. }
  230. }
  231. err = ovl_check_redirect(this, d, prelen, post);
  232. if (err)
  233. goto out_err;
  234. out:
  235. *ret = this;
  236. return 0;
  237. put_and_out:
  238. dput(this);
  239. this = NULL;
  240. goto out;
  241. out_err:
  242. dput(this);
  243. return err;
  244. }
  245. static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
  246. struct dentry **ret)
  247. {
  248. /* Counting down from the end, since the prefix can change */
  249. size_t rem = d->name.len - 1;
  250. struct dentry *dentry = NULL;
  251. int err;
  252. if (d->name.name[0] != '/')
  253. return ovl_lookup_single(base, d, d->name.name, d->name.len,
  254. 0, "", ret);
  255. while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
  256. const char *s = d->name.name + d->name.len - rem;
  257. const char *next = strchrnul(s, '/');
  258. size_t thislen = next - s;
  259. bool end = !next[0];
  260. /* Verify we did not go off the rails */
  261. if (WARN_ON(s[-1] != '/'))
  262. return -EIO;
  263. err = ovl_lookup_single(base, d, s, thislen,
  264. d->name.len - rem, next, &base);
  265. dput(dentry);
  266. if (err)
  267. return err;
  268. dentry = base;
  269. if (end)
  270. break;
  271. rem -= thislen + 1;
  272. if (WARN_ON(rem >= d->name.len))
  273. return -EIO;
  274. }
  275. *ret = dentry;
  276. return 0;
  277. }
  278. int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
  279. struct dentry *upperdentry, struct ovl_path **stackp)
  280. {
  281. struct dentry *origin = NULL;
  282. int i;
  283. for (i = 0; i < ofs->numlower; i++) {
  284. origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt,
  285. connected);
  286. if (origin)
  287. break;
  288. }
  289. if (!origin)
  290. return -ESTALE;
  291. else if (IS_ERR(origin))
  292. return PTR_ERR(origin);
  293. if (upperdentry && !ovl_is_whiteout(upperdentry) &&
  294. ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
  295. goto invalid;
  296. if (!*stackp)
  297. *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
  298. if (!*stackp) {
  299. dput(origin);
  300. return -ENOMEM;
  301. }
  302. **stackp = (struct ovl_path){
  303. .dentry = origin,
  304. .layer = &ofs->lower_layers[i]
  305. };
  306. return 0;
  307. invalid:
  308. pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
  309. upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
  310. d_inode(origin)->i_mode & S_IFMT);
  311. dput(origin);
  312. return -EIO;
  313. }
  314. static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
  315. struct ovl_path **stackp, unsigned int *ctrp)
  316. {
  317. struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
  318. int err;
  319. if (IS_ERR_OR_NULL(fh))
  320. return PTR_ERR(fh);
  321. err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
  322. kfree(fh);
  323. if (err) {
  324. if (err == -ESTALE)
  325. return 0;
  326. return err;
  327. }
  328. if (WARN_ON(*ctrp))
  329. return -EIO;
  330. *ctrp = 1;
  331. return 0;
  332. }
  333. /*
  334. * Verify that @fh matches the file handle stored in xattr @name.
  335. * Return 0 on match, -ESTALE on mismatch, < 0 on error.
  336. */
  337. static int ovl_verify_fh(struct dentry *dentry, const char *name,
  338. const struct ovl_fh *fh)
  339. {
  340. struct ovl_fh *ofh = ovl_get_fh(dentry, name);
  341. int err = 0;
  342. if (!ofh)
  343. return -ENODATA;
  344. if (IS_ERR(ofh))
  345. return PTR_ERR(ofh);
  346. if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
  347. err = -ESTALE;
  348. kfree(ofh);
  349. return err;
  350. }
  351. /*
  352. * Verify that @real dentry matches the file handle stored in xattr @name.
  353. *
  354. * If @set is true and there is no stored file handle, encode @real and store
  355. * file handle in xattr @name.
  356. *
  357. * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
  358. */
  359. int ovl_verify_set_fh(struct dentry *dentry, const char *name,
  360. struct dentry *real, bool is_upper, bool set)
  361. {
  362. struct inode *inode;
  363. struct ovl_fh *fh;
  364. int err;
  365. fh = ovl_encode_real_fh(real, is_upper);
  366. err = PTR_ERR(fh);
  367. if (IS_ERR(fh)) {
  368. fh = NULL;
  369. goto fail;
  370. }
  371. err = ovl_verify_fh(dentry, name, fh);
  372. if (set && err == -ENODATA)
  373. err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
  374. if (err)
  375. goto fail;
  376. out:
  377. kfree(fh);
  378. return err;
  379. fail:
  380. inode = d_inode(real);
  381. pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
  382. is_upper ? "upper" : "origin", real,
  383. inode ? inode->i_ino : 0, err);
  384. goto out;
  385. }
  386. /* Get upper dentry from index */
  387. struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
  388. {
  389. struct ovl_fh *fh;
  390. struct dentry *upper;
  391. if (!d_is_dir(index))
  392. return dget(index);
  393. fh = ovl_get_fh(index, OVL_XATTR_UPPER);
  394. if (IS_ERR_OR_NULL(fh))
  395. return ERR_CAST(fh);
  396. upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
  397. kfree(fh);
  398. if (IS_ERR_OR_NULL(upper))
  399. return upper ?: ERR_PTR(-ESTALE);
  400. if (!d_is_dir(upper)) {
  401. pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
  402. index, upper);
  403. dput(upper);
  404. return ERR_PTR(-EIO);
  405. }
  406. return upper;
  407. }
  408. /* Is this a leftover from create/whiteout of directory index entry? */
  409. static bool ovl_is_temp_index(struct dentry *index)
  410. {
  411. return index->d_name.name[0] == '#';
  412. }
  413. /*
  414. * Verify that an index entry name matches the origin file handle stored in
  415. * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
  416. * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
  417. */
  418. int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
  419. {
  420. struct ovl_fh *fh = NULL;
  421. size_t len;
  422. struct ovl_path origin = { };
  423. struct ovl_path *stack = &origin;
  424. struct dentry *upper = NULL;
  425. int err;
  426. if (!d_inode(index))
  427. return 0;
  428. /* Cleanup leftover from index create/cleanup attempt */
  429. err = -ESTALE;
  430. if (ovl_is_temp_index(index))
  431. goto fail;
  432. err = -EINVAL;
  433. if (index->d_name.len < sizeof(struct ovl_fh)*2)
  434. goto fail;
  435. err = -ENOMEM;
  436. len = index->d_name.len / 2;
  437. fh = kzalloc(len, GFP_KERNEL);
  438. if (!fh)
  439. goto fail;
  440. err = -EINVAL;
  441. if (hex2bin((u8 *)fh, index->d_name.name, len))
  442. goto fail;
  443. err = ovl_check_fh_len(fh, len);
  444. if (err)
  445. goto fail;
  446. /*
  447. * Whiteout index entries are used as an indication that an exported
  448. * overlay file handle should be treated as stale (i.e. after unlink
  449. * of the overlay inode). These entries contain no origin xattr.
  450. */
  451. if (ovl_is_whiteout(index))
  452. goto out;
  453. /*
  454. * Verifying directory index entries are not stale is expensive, so
  455. * only verify stale dir index if NFS export is enabled.
  456. */
  457. if (d_is_dir(index) && !ofs->config.nfs_export)
  458. goto out;
  459. /*
  460. * Directory index entries should have 'upper' xattr pointing to the
  461. * real upper dir. Non-dir index entries are hardlinks to the upper
  462. * real inode. For non-dir index, we can read the copy up origin xattr
  463. * directly from the index dentry, but for dir index we first need to
  464. * decode the upper directory.
  465. */
  466. upper = ovl_index_upper(ofs, index);
  467. if (IS_ERR_OR_NULL(upper)) {
  468. err = PTR_ERR(upper);
  469. /*
  470. * Directory index entries with no 'upper' xattr need to be
  471. * removed. When dir index entry has a stale 'upper' xattr,
  472. * we assume that upper dir was removed and we treat the dir
  473. * index as orphan entry that needs to be whited out.
  474. */
  475. if (err == -ESTALE)
  476. goto orphan;
  477. else if (!err)
  478. err = -ESTALE;
  479. goto fail;
  480. }
  481. err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
  482. dput(upper);
  483. if (err)
  484. goto fail;
  485. /* Check if non-dir index is orphan and don't warn before cleaning it */
  486. if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
  487. err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
  488. if (err)
  489. goto fail;
  490. if (ovl_get_nlink(origin.dentry, index, 0) == 0)
  491. goto orphan;
  492. }
  493. out:
  494. dput(origin.dentry);
  495. kfree(fh);
  496. return err;
  497. fail:
  498. pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
  499. index, d_inode(index)->i_mode & S_IFMT, err);
  500. goto out;
  501. orphan:
  502. pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
  503. index, d_inode(index)->i_mode & S_IFMT,
  504. d_inode(index)->i_nlink);
  505. err = -ENOENT;
  506. goto out;
  507. }
  508. static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
  509. {
  510. char *n, *s;
  511. n = kcalloc(fh->len, 2, GFP_KERNEL);
  512. if (!n)
  513. return -ENOMEM;
  514. s = bin2hex(n, fh, fh->len);
  515. *name = (struct qstr) QSTR_INIT(n, s - n);
  516. return 0;
  517. }
  518. /*
  519. * Lookup in indexdir for the index entry of a lower real inode or a copy up
  520. * origin inode. The index entry name is the hex representation of the lower
  521. * inode file handle.
  522. *
  523. * If the index dentry in negative, then either no lower aliases have been
  524. * copied up yet, or aliases have been copied up in older kernels and are
  525. * not indexed.
  526. *
  527. * If the index dentry for a copy up origin inode is positive, but points
  528. * to an inode different than the upper inode, then either the upper inode
  529. * has been copied up and not indexed or it was indexed, but since then
  530. * index dir was cleared. Either way, that index cannot be used to indentify
  531. * the overlay inode.
  532. */
  533. int ovl_get_index_name(struct dentry *origin, struct qstr *name)
  534. {
  535. struct ovl_fh *fh;
  536. int err;
  537. fh = ovl_encode_real_fh(origin, false);
  538. if (IS_ERR(fh))
  539. return PTR_ERR(fh);
  540. err = ovl_get_index_name_fh(fh, name);
  541. kfree(fh);
  542. return err;
  543. }
  544. /* Lookup index by file handle for NFS export */
  545. struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
  546. {
  547. struct dentry *index;
  548. struct qstr name;
  549. int err;
  550. err = ovl_get_index_name_fh(fh, &name);
  551. if (err)
  552. return ERR_PTR(err);
  553. index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
  554. kfree(name.name);
  555. if (IS_ERR(index)) {
  556. if (PTR_ERR(index) == -ENOENT)
  557. index = NULL;
  558. return index;
  559. }
  560. if (d_is_negative(index))
  561. err = 0;
  562. else if (ovl_is_whiteout(index))
  563. err = -ESTALE;
  564. else if (ovl_dentry_weird(index))
  565. err = -EIO;
  566. else
  567. return index;
  568. dput(index);
  569. return ERR_PTR(err);
  570. }
  571. struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
  572. struct dentry *origin, bool verify)
  573. {
  574. struct dentry *index;
  575. struct inode *inode;
  576. struct qstr name;
  577. bool is_dir = d_is_dir(origin);
  578. int err;
  579. err = ovl_get_index_name(origin, &name);
  580. if (err)
  581. return ERR_PTR(err);
  582. index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
  583. if (IS_ERR(index)) {
  584. err = PTR_ERR(index);
  585. if (err == -ENOENT) {
  586. index = NULL;
  587. goto out;
  588. }
  589. pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
  590. "overlayfs: mount with '-o index=off' to disable inodes index.\n",
  591. d_inode(origin)->i_ino, name.len, name.name,
  592. err);
  593. goto out;
  594. }
  595. inode = d_inode(index);
  596. if (d_is_negative(index)) {
  597. goto out_dput;
  598. } else if (ovl_is_whiteout(index) && !verify) {
  599. /*
  600. * When index lookup is called with !verify for decoding an
  601. * overlay file handle, a whiteout index implies that decode
  602. * should treat file handle as stale and no need to print a
  603. * warning about it.
  604. */
  605. dput(index);
  606. index = ERR_PTR(-ESTALE);
  607. goto out;
  608. } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
  609. ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
  610. /*
  611. * Index should always be of the same file type as origin
  612. * except for the case of a whiteout index. A whiteout
  613. * index should only exist if all lower aliases have been
  614. * unlinked, which means that finding a lower origin on lookup
  615. * whose index is a whiteout should be treated as an error.
  616. */
  617. pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
  618. index, d_inode(index)->i_mode & S_IFMT,
  619. d_inode(origin)->i_mode & S_IFMT);
  620. goto fail;
  621. } else if (is_dir && verify) {
  622. if (!upper) {
  623. pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
  624. origin, index);
  625. goto fail;
  626. }
  627. /* Verify that dir index 'upper' xattr points to upper dir */
  628. err = ovl_verify_upper(index, upper, false);
  629. if (err) {
  630. if (err == -ESTALE) {
  631. pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
  632. upper, origin, index);
  633. }
  634. goto fail;
  635. }
  636. } else if (upper && d_inode(upper) != inode) {
  637. goto out_dput;
  638. }
  639. out:
  640. kfree(name.name);
  641. return index;
  642. out_dput:
  643. dput(index);
  644. index = NULL;
  645. goto out;
  646. fail:
  647. dput(index);
  648. index = ERR_PTR(-EIO);
  649. goto out;
  650. }
  651. /*
  652. * Returns next layer in stack starting from top.
  653. * Returns -1 if this is the last layer.
  654. */
  655. int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
  656. {
  657. struct ovl_entry *oe = dentry->d_fsdata;
  658. BUG_ON(idx < 0);
  659. if (idx == 0) {
  660. ovl_path_upper(dentry, path);
  661. if (path->dentry)
  662. return oe->numlower ? 1 : -1;
  663. idx++;
  664. }
  665. BUG_ON(idx > oe->numlower);
  666. path->dentry = oe->lowerstack[idx - 1].dentry;
  667. path->mnt = oe->lowerstack[idx - 1].layer->mnt;
  668. return (idx < oe->numlower) ? idx + 1 : -1;
  669. }
  670. /* Fix missing 'origin' xattr */
  671. static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
  672. struct dentry *upper)
  673. {
  674. int err;
  675. if (ovl_check_origin_xattr(upper))
  676. return 0;
  677. err = ovl_want_write(dentry);
  678. if (err)
  679. return err;
  680. err = ovl_set_origin(dentry, lower, upper);
  681. if (!err)
  682. err = ovl_set_impure(dentry->d_parent, upper->d_parent);
  683. ovl_drop_write(dentry);
  684. return err;
  685. }
  686. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  687. unsigned int flags)
  688. {
  689. struct ovl_entry *oe;
  690. const struct cred *old_cred;
  691. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  692. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  693. struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
  694. struct ovl_path *stack = NULL, *origin_path = NULL;
  695. struct dentry *upperdir, *upperdentry = NULL;
  696. struct dentry *origin = NULL;
  697. struct dentry *index = NULL;
  698. unsigned int ctr = 0;
  699. struct inode *inode = NULL;
  700. bool upperopaque = false;
  701. char *upperredirect = NULL;
  702. struct dentry *this;
  703. unsigned int i;
  704. int err;
  705. bool metacopy = false;
  706. struct ovl_lookup_data d = {
  707. .sb = dentry->d_sb,
  708. .name = dentry->d_name,
  709. .is_dir = false,
  710. .opaque = false,
  711. .stop = false,
  712. .last = ofs->config.redirect_follow ? false : !poe->numlower,
  713. .redirect = NULL,
  714. .metacopy = false,
  715. };
  716. if (dentry->d_name.len > ofs->namelen)
  717. return ERR_PTR(-ENAMETOOLONG);
  718. old_cred = ovl_override_creds(dentry->d_sb);
  719. upperdir = ovl_dentry_upper(dentry->d_parent);
  720. if (upperdir) {
  721. err = ovl_lookup_layer(upperdir, &d, &upperdentry);
  722. if (err)
  723. goto out;
  724. if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
  725. dput(upperdentry);
  726. err = -EREMOTE;
  727. goto out;
  728. }
  729. if (upperdentry && !d.is_dir) {
  730. unsigned int origin_ctr = 0;
  731. /*
  732. * Lookup copy up origin by decoding origin file handle.
  733. * We may get a disconnected dentry, which is fine,
  734. * because we only need to hold the origin inode in
  735. * cache and use its inode number. We may even get a
  736. * connected dentry, that is not under any of the lower
  737. * layers root. That is also fine for using it's inode
  738. * number - it's the same as if we held a reference
  739. * to a dentry in lower layer that was moved under us.
  740. */
  741. err = ovl_check_origin(ofs, upperdentry, &origin_path,
  742. &origin_ctr);
  743. if (err)
  744. goto out_put_upper;
  745. if (d.metacopy)
  746. metacopy = true;
  747. }
  748. if (d.redirect) {
  749. err = -ENOMEM;
  750. upperredirect = kstrdup(d.redirect, GFP_KERNEL);
  751. if (!upperredirect)
  752. goto out_put_upper;
  753. if (d.redirect[0] == '/')
  754. poe = roe;
  755. }
  756. upperopaque = d.opaque;
  757. }
  758. if (!d.stop && poe->numlower) {
  759. err = -ENOMEM;
  760. stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
  761. GFP_KERNEL);
  762. if (!stack)
  763. goto out_put_upper;
  764. }
  765. for (i = 0; !d.stop && i < poe->numlower; i++) {
  766. struct ovl_path lower = poe->lowerstack[i];
  767. if (!ofs->config.redirect_follow)
  768. d.last = i == poe->numlower - 1;
  769. else
  770. d.last = lower.layer->idx == roe->numlower;
  771. err = ovl_lookup_layer(lower.dentry, &d, &this);
  772. if (err)
  773. goto out_put;
  774. if (!this)
  775. continue;
  776. /*
  777. * If no origin fh is stored in upper of a merge dir, store fh
  778. * of lower dir and set upper parent "impure".
  779. */
  780. if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
  781. err = ovl_fix_origin(dentry, this, upperdentry);
  782. if (err) {
  783. dput(this);
  784. goto out_put;
  785. }
  786. }
  787. /*
  788. * When "verify_lower" feature is enabled, do not merge with a
  789. * lower dir that does not match a stored origin xattr. In any
  790. * case, only verified origin is used for index lookup.
  791. *
  792. * For non-dir dentry, if index=on, then ensure origin
  793. * matches the dentry found using path based lookup,
  794. * otherwise error out.
  795. */
  796. if (upperdentry && !ctr &&
  797. ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
  798. (!d.is_dir && ofs->config.index && origin_path))) {
  799. err = ovl_verify_origin(upperdentry, this, false);
  800. if (err) {
  801. dput(this);
  802. if (d.is_dir)
  803. break;
  804. goto out_put;
  805. }
  806. origin = this;
  807. }
  808. if (d.metacopy)
  809. metacopy = true;
  810. /*
  811. * Do not store intermediate metacopy dentries in chain,
  812. * except top most lower metacopy dentry
  813. */
  814. if (d.metacopy && ctr) {
  815. dput(this);
  816. continue;
  817. }
  818. stack[ctr].dentry = this;
  819. stack[ctr].layer = lower.layer;
  820. ctr++;
  821. /*
  822. * Following redirects can have security consequences: it's like
  823. * a symlink into the lower layer without the permission checks.
  824. * This is only a problem if the upper layer is untrusted (e.g
  825. * comes from an USB drive). This can allow a non-readable file
  826. * or directory to become readable.
  827. *
  828. * Only following redirects when redirects are enabled disables
  829. * this attack vector when not necessary.
  830. */
  831. err = -EPERM;
  832. if (d.redirect && !ofs->config.redirect_follow) {
  833. pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
  834. dentry);
  835. goto out_put;
  836. }
  837. if (d.stop)
  838. break;
  839. if (d.redirect && d.redirect[0] == '/' && poe != roe) {
  840. poe = roe;
  841. /* Find the current layer on the root dentry */
  842. i = lower.layer->idx - 1;
  843. }
  844. }
  845. if (metacopy) {
  846. /*
  847. * Found a metacopy dentry but did not find corresponding
  848. * data dentry
  849. */
  850. if (d.metacopy) {
  851. err = -EIO;
  852. goto out_put;
  853. }
  854. err = -EPERM;
  855. if (!ofs->config.metacopy) {
  856. pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
  857. dentry);
  858. goto out_put;
  859. }
  860. } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
  861. if (WARN_ON(stack != NULL)) {
  862. err = -EIO;
  863. goto out_put;
  864. }
  865. stack = origin_path;
  866. ctr = 1;
  867. origin_path = NULL;
  868. }
  869. /*
  870. * Lookup index by lower inode and verify it matches upper inode.
  871. * We only trust dir index if we verified that lower dir matches
  872. * origin, otherwise dir index entries may be inconsistent and we
  873. * ignore them.
  874. *
  875. * For non-dir upper metacopy dentry, we already set "origin" if we
  876. * verified that lower matched upper origin. If upper origin was
  877. * not present (because lower layer did not support fh encode/decode),
  878. * or indexing is not enabled, do not set "origin" and skip looking up
  879. * index. This case should be handled in same way as a non-dir upper
  880. * without ORIGIN is handled.
  881. *
  882. * Always lookup index of non-dir non-metacopy and non-upper.
  883. */
  884. if (ctr && (!upperdentry || (!d.is_dir && !metacopy)))
  885. origin = stack[0].dentry;
  886. if (origin && ovl_indexdir(dentry->d_sb) &&
  887. (!d.is_dir || ovl_index_all(dentry->d_sb))) {
  888. index = ovl_lookup_index(ofs, upperdentry, origin, true);
  889. if (IS_ERR(index)) {
  890. err = PTR_ERR(index);
  891. index = NULL;
  892. goto out_put;
  893. }
  894. }
  895. oe = ovl_alloc_entry(ctr);
  896. err = -ENOMEM;
  897. if (!oe)
  898. goto out_put;
  899. memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
  900. dentry->d_fsdata = oe;
  901. if (upperopaque)
  902. ovl_dentry_set_opaque(dentry);
  903. if (upperdentry)
  904. ovl_dentry_set_upper_alias(dentry);
  905. else if (index) {
  906. upperdentry = dget(index);
  907. upperredirect = ovl_get_redirect_xattr(upperdentry, 0);
  908. if (IS_ERR(upperredirect)) {
  909. err = PTR_ERR(upperredirect);
  910. upperredirect = NULL;
  911. goto out_free_oe;
  912. }
  913. }
  914. if (upperdentry || ctr) {
  915. struct ovl_inode_params oip = {
  916. .upperdentry = upperdentry,
  917. .lowerpath = stack,
  918. .index = index,
  919. .numlower = ctr,
  920. .redirect = upperredirect,
  921. .lowerdata = (ctr > 1 && !d.is_dir) ?
  922. stack[ctr - 1].dentry : NULL,
  923. };
  924. inode = ovl_get_inode(dentry->d_sb, &oip);
  925. err = PTR_ERR(inode);
  926. if (IS_ERR(inode))
  927. goto out_free_oe;
  928. }
  929. revert_creds(old_cred);
  930. if (origin_path) {
  931. dput(origin_path->dentry);
  932. kfree(origin_path);
  933. }
  934. dput(index);
  935. kfree(stack);
  936. kfree(d.redirect);
  937. return d_splice_alias(inode, dentry);
  938. out_free_oe:
  939. dentry->d_fsdata = NULL;
  940. kfree(oe);
  941. out_put:
  942. dput(index);
  943. for (i = 0; i < ctr; i++)
  944. dput(stack[i].dentry);
  945. kfree(stack);
  946. out_put_upper:
  947. if (origin_path) {
  948. dput(origin_path->dentry);
  949. kfree(origin_path);
  950. }
  951. dput(upperdentry);
  952. kfree(upperredirect);
  953. out:
  954. kfree(d.redirect);
  955. revert_creds(old_cred);
  956. return ERR_PTR(err);
  957. }
  958. bool ovl_lower_positive(struct dentry *dentry)
  959. {
  960. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  961. const struct qstr *name = &dentry->d_name;
  962. const struct cred *old_cred;
  963. unsigned int i;
  964. bool positive = false;
  965. bool done = false;
  966. /*
  967. * If dentry is negative, then lower is positive iff this is a
  968. * whiteout.
  969. */
  970. if (!dentry->d_inode)
  971. return ovl_dentry_is_opaque(dentry);
  972. /* Negative upper -> positive lower */
  973. if (!ovl_dentry_upper(dentry))
  974. return true;
  975. old_cred = ovl_override_creds(dentry->d_sb);
  976. /* Positive upper -> have to look up lower to see whether it exists */
  977. for (i = 0; !done && !positive && i < poe->numlower; i++) {
  978. struct dentry *this;
  979. struct dentry *lowerdir = poe->lowerstack[i].dentry;
  980. this = lookup_one_len_unlocked(name->name, lowerdir,
  981. name->len);
  982. if (IS_ERR(this)) {
  983. switch (PTR_ERR(this)) {
  984. case -ENOENT:
  985. case -ENAMETOOLONG:
  986. break;
  987. default:
  988. /*
  989. * Assume something is there, we just couldn't
  990. * access it.
  991. */
  992. positive = true;
  993. break;
  994. }
  995. } else {
  996. if (this->d_inode) {
  997. positive = !ovl_is_whiteout(this);
  998. done = true;
  999. }
  1000. dput(this);
  1001. }
  1002. }
  1003. revert_creds(old_cred);
  1004. return positive;
  1005. }