namei.c 25 KB

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