namei.c 27 KB

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