namei.c 34 KB

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