namei.c 29 KB

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