namei.c 28 KB

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