namei.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (C) 2011 Novell Inc.
  3. * Copyright (C) 2016 Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/cred.h>
  11. #include <linux/namei.h>
  12. #include <linux/xattr.h>
  13. #include <linux/ratelimit.h>
  14. #include <linux/mount.h>
  15. #include <linux/exportfs.h>
  16. #include "overlayfs.h"
  17. #include "ovl_entry.h"
  18. struct ovl_lookup_data {
  19. struct qstr name;
  20. bool is_dir;
  21. bool opaque;
  22. bool stop;
  23. bool last;
  24. char *redirect;
  25. };
  26. static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
  27. size_t prelen, const char *post)
  28. {
  29. int res;
  30. char *s, *next, *buf = NULL;
  31. res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
  32. if (res < 0) {
  33. if (res == -ENODATA || res == -EOPNOTSUPP)
  34. return 0;
  35. goto fail;
  36. }
  37. buf = kzalloc(prelen + res + strlen(post) + 1, GFP_TEMPORARY);
  38. if (!buf)
  39. return -ENOMEM;
  40. if (res == 0)
  41. goto invalid;
  42. res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
  43. if (res < 0)
  44. goto fail;
  45. if (res == 0)
  46. goto invalid;
  47. if (buf[0] == '/') {
  48. for (s = buf; *s++ == '/'; s = next) {
  49. next = strchrnul(s, '/');
  50. if (s == next)
  51. goto invalid;
  52. }
  53. } else {
  54. if (strchr(buf, '/') != NULL)
  55. goto invalid;
  56. memmove(buf + prelen, buf, res);
  57. memcpy(buf, d->name.name, prelen);
  58. }
  59. strcat(buf, post);
  60. kfree(d->redirect);
  61. d->redirect = buf;
  62. d->name.name = d->redirect;
  63. d->name.len = strlen(d->redirect);
  64. return 0;
  65. err_free:
  66. kfree(buf);
  67. return 0;
  68. fail:
  69. pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
  70. goto err_free;
  71. invalid:
  72. pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
  73. goto err_free;
  74. }
  75. static int ovl_acceptable(void *ctx, struct dentry *dentry)
  76. {
  77. return 1;
  78. }
  79. static struct dentry *ovl_get_origin(struct dentry *dentry,
  80. struct vfsmount *mnt)
  81. {
  82. int res;
  83. struct ovl_fh *fh = NULL;
  84. struct dentry *origin = NULL;
  85. int bytes;
  86. res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
  87. if (res < 0) {
  88. if (res == -ENODATA || res == -EOPNOTSUPP)
  89. return NULL;
  90. goto fail;
  91. }
  92. /* Zero size value means "copied up but origin unknown" */
  93. if (res == 0)
  94. return NULL;
  95. fh = kzalloc(res, GFP_TEMPORARY);
  96. if (!fh)
  97. return ERR_PTR(-ENOMEM);
  98. res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, fh, res);
  99. if (res < 0)
  100. goto fail;
  101. if (res < sizeof(struct ovl_fh) || res < fh->len)
  102. goto invalid;
  103. if (fh->magic != OVL_FH_MAGIC)
  104. goto invalid;
  105. /* Treat larger version and unknown flags as "origin unknown" */
  106. if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
  107. goto out;
  108. /* Treat endianness mismatch as "origin unknown" */
  109. if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
  110. (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
  111. goto out;
  112. bytes = (fh->len - offsetof(struct ovl_fh, fid));
  113. /*
  114. * Make sure that the stored uuid matches the uuid of the lower
  115. * layer where file handle will be decoded.
  116. */
  117. if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
  118. goto out;
  119. origin = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
  120. bytes >> 2, (int)fh->type,
  121. ovl_acceptable, NULL);
  122. if (IS_ERR(origin)) {
  123. /* Treat stale file handle as "origin unknown" */
  124. if (origin == ERR_PTR(-ESTALE))
  125. origin = NULL;
  126. goto out;
  127. }
  128. if (ovl_dentry_weird(origin) ||
  129. ((d_inode(origin)->i_mode ^ d_inode(dentry)->i_mode) & S_IFMT)) {
  130. dput(origin);
  131. origin = NULL;
  132. goto invalid;
  133. }
  134. out:
  135. kfree(fh);
  136. return origin;
  137. fail:
  138. pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
  139. goto out;
  140. invalid:
  141. pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
  142. goto out;
  143. }
  144. static bool ovl_is_opaquedir(struct dentry *dentry)
  145. {
  146. return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
  147. }
  148. static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
  149. const char *name, unsigned int namelen,
  150. size_t prelen, const char *post,
  151. struct dentry **ret)
  152. {
  153. struct dentry *this;
  154. int err;
  155. this = lookup_one_len_unlocked(name, base, namelen);
  156. if (IS_ERR(this)) {
  157. err = PTR_ERR(this);
  158. this = NULL;
  159. if (err == -ENOENT || err == -ENAMETOOLONG)
  160. goto out;
  161. goto out_err;
  162. }
  163. if (!this->d_inode)
  164. goto put_and_out;
  165. if (ovl_dentry_weird(this)) {
  166. /* Don't support traversing automounts and other weirdness */
  167. err = -EREMOTE;
  168. goto out_err;
  169. }
  170. if (ovl_is_whiteout(this)) {
  171. d->stop = d->opaque = true;
  172. goto put_and_out;
  173. }
  174. if (!d_can_lookup(this)) {
  175. d->stop = true;
  176. if (d->is_dir)
  177. goto put_and_out;
  178. goto out;
  179. }
  180. d->is_dir = true;
  181. if (!d->last && ovl_is_opaquedir(this)) {
  182. d->stop = d->opaque = true;
  183. goto out;
  184. }
  185. err = ovl_check_redirect(this, d, prelen, post);
  186. if (err)
  187. goto out_err;
  188. out:
  189. *ret = this;
  190. return 0;
  191. put_and_out:
  192. dput(this);
  193. this = NULL;
  194. goto out;
  195. out_err:
  196. dput(this);
  197. return err;
  198. }
  199. static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
  200. struct dentry **ret)
  201. {
  202. /* Counting down from the end, since the prefix can change */
  203. size_t rem = d->name.len - 1;
  204. struct dentry *dentry = NULL;
  205. int err;
  206. if (d->name.name[0] != '/')
  207. return ovl_lookup_single(base, d, d->name.name, d->name.len,
  208. 0, "", ret);
  209. while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
  210. const char *s = d->name.name + d->name.len - rem;
  211. const char *next = strchrnul(s, '/');
  212. size_t thislen = next - s;
  213. bool end = !next[0];
  214. /* Verify we did not go off the rails */
  215. if (WARN_ON(s[-1] != '/'))
  216. return -EIO;
  217. err = ovl_lookup_single(base, d, s, thislen,
  218. d->name.len - rem, next, &base);
  219. dput(dentry);
  220. if (err)
  221. return err;
  222. dentry = base;
  223. if (end)
  224. break;
  225. rem -= thislen + 1;
  226. if (WARN_ON(rem >= d->name.len))
  227. return -EIO;
  228. }
  229. *ret = dentry;
  230. return 0;
  231. }
  232. static int ovl_check_origin(struct dentry *dentry, struct dentry *upperdentry,
  233. struct path **stackp, unsigned int *ctrp)
  234. {
  235. struct super_block *same_sb = ovl_same_sb(dentry->d_sb);
  236. struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
  237. struct vfsmount *mnt;
  238. struct dentry *origin;
  239. if (!same_sb || !roe->numlower)
  240. return 0;
  241. /*
  242. * Since all layers are on the same fs, we use the first layer for
  243. * decoding the file handle. We may get a disconnected dentry,
  244. * which is fine, because we only need to hold the origin inode in
  245. * cache and use its inode number. We may even get a connected dentry,
  246. * that is not under the first layer's root. That is also fine for
  247. * using it's inode number - it's the same as if we held a reference
  248. * to a dentry in first layer that was moved under us.
  249. */
  250. mnt = roe->lowerstack[0].mnt;
  251. origin = ovl_get_origin(upperdentry, mnt);
  252. if (IS_ERR_OR_NULL(origin))
  253. return PTR_ERR(origin);
  254. BUG_ON(*stackp || *ctrp);
  255. *stackp = kmalloc(sizeof(struct path), GFP_TEMPORARY);
  256. if (!*stackp) {
  257. dput(origin);
  258. return -ENOMEM;
  259. }
  260. **stackp = (struct path) { .dentry = origin, .mnt = mnt };
  261. *ctrp = 1;
  262. return 0;
  263. }
  264. /*
  265. * Returns next layer in stack starting from top.
  266. * Returns -1 if this is the last layer.
  267. */
  268. int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
  269. {
  270. struct ovl_entry *oe = dentry->d_fsdata;
  271. BUG_ON(idx < 0);
  272. if (idx == 0) {
  273. ovl_path_upper(dentry, path);
  274. if (path->dentry)
  275. return oe->numlower ? 1 : -1;
  276. idx++;
  277. }
  278. BUG_ON(idx > oe->numlower);
  279. *path = oe->lowerstack[idx - 1];
  280. return (idx < oe->numlower) ? idx + 1 : -1;
  281. }
  282. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  283. unsigned int flags)
  284. {
  285. struct ovl_entry *oe;
  286. const struct cred *old_cred;
  287. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  288. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  289. struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
  290. struct path *stack = NULL;
  291. struct dentry *upperdir, *upperdentry = NULL;
  292. unsigned int ctr = 0;
  293. struct inode *inode = NULL;
  294. bool upperopaque = false;
  295. bool upperimpure = false;
  296. char *upperredirect = NULL;
  297. struct dentry *this;
  298. unsigned int i;
  299. int err;
  300. struct ovl_lookup_data d = {
  301. .name = dentry->d_name,
  302. .is_dir = false,
  303. .opaque = false,
  304. .stop = false,
  305. .last = !poe->numlower,
  306. .redirect = NULL,
  307. };
  308. if (dentry->d_name.len > ofs->namelen)
  309. return ERR_PTR(-ENAMETOOLONG);
  310. old_cred = ovl_override_creds(dentry->d_sb);
  311. upperdir = ovl_upperdentry_dereference(poe);
  312. if (upperdir) {
  313. err = ovl_lookup_layer(upperdir, &d, &upperdentry);
  314. if (err)
  315. goto out;
  316. if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
  317. dput(upperdentry);
  318. err = -EREMOTE;
  319. goto out;
  320. }
  321. if (upperdentry && !d.is_dir) {
  322. BUG_ON(!d.stop || d.redirect);
  323. err = ovl_check_origin(dentry, upperdentry,
  324. &stack, &ctr);
  325. if (err)
  326. goto out;
  327. }
  328. if (d.redirect) {
  329. upperredirect = kstrdup(d.redirect, GFP_KERNEL);
  330. if (!upperredirect)
  331. goto out_put_upper;
  332. if (d.redirect[0] == '/')
  333. poe = roe;
  334. }
  335. upperopaque = d.opaque;
  336. if (upperdentry && d.is_dir)
  337. upperimpure = ovl_is_impuredir(upperdentry);
  338. }
  339. if (!d.stop && poe->numlower) {
  340. err = -ENOMEM;
  341. stack = kcalloc(ofs->numlower, sizeof(struct path),
  342. GFP_TEMPORARY);
  343. if (!stack)
  344. goto out_put_upper;
  345. }
  346. for (i = 0; !d.stop && i < poe->numlower; i++) {
  347. struct path lowerpath = poe->lowerstack[i];
  348. d.last = i == poe->numlower - 1;
  349. err = ovl_lookup_layer(lowerpath.dentry, &d, &this);
  350. if (err)
  351. goto out_put;
  352. if (!this)
  353. continue;
  354. stack[ctr].dentry = this;
  355. stack[ctr].mnt = lowerpath.mnt;
  356. ctr++;
  357. if (d.stop)
  358. break;
  359. if (d.redirect && d.redirect[0] == '/' && poe != roe) {
  360. poe = roe;
  361. /* Find the current layer on the root dentry */
  362. for (i = 0; i < poe->numlower; i++)
  363. if (poe->lowerstack[i].mnt == lowerpath.mnt)
  364. break;
  365. if (WARN_ON(i == poe->numlower))
  366. break;
  367. }
  368. }
  369. oe = ovl_alloc_entry(ctr);
  370. err = -ENOMEM;
  371. if (!oe)
  372. goto out_put;
  373. if (upperdentry || ctr) {
  374. struct dentry *realdentry;
  375. struct inode *realinode;
  376. realdentry = upperdentry ? upperdentry : stack[0].dentry;
  377. realinode = d_inode(realdentry);
  378. err = -ENOMEM;
  379. if (upperdentry && !d_is_dir(upperdentry)) {
  380. inode = ovl_get_inode(dentry->d_sb, realinode);
  381. } else {
  382. inode = ovl_new_inode(dentry->d_sb, realinode->i_mode,
  383. realinode->i_rdev);
  384. if (inode)
  385. ovl_inode_init(inode, realinode, !!upperdentry);
  386. }
  387. if (!inode)
  388. goto out_free_oe;
  389. ovl_copyattr(realdentry->d_inode, inode);
  390. }
  391. revert_creds(old_cred);
  392. oe->opaque = upperopaque;
  393. oe->impure = upperimpure;
  394. oe->redirect = upperredirect;
  395. oe->__upperdentry = upperdentry;
  396. memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
  397. kfree(stack);
  398. kfree(d.redirect);
  399. dentry->d_fsdata = oe;
  400. d_add(dentry, inode);
  401. return NULL;
  402. out_free_oe:
  403. kfree(oe);
  404. out_put:
  405. for (i = 0; i < ctr; i++)
  406. dput(stack[i].dentry);
  407. kfree(stack);
  408. out_put_upper:
  409. dput(upperdentry);
  410. kfree(upperredirect);
  411. out:
  412. kfree(d.redirect);
  413. revert_creds(old_cred);
  414. return ERR_PTR(err);
  415. }
  416. bool ovl_lower_positive(struct dentry *dentry)
  417. {
  418. struct ovl_entry *oe = dentry->d_fsdata;
  419. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  420. const struct qstr *name = &dentry->d_name;
  421. unsigned int i;
  422. bool positive = false;
  423. bool done = false;
  424. /*
  425. * If dentry is negative, then lower is positive iff this is a
  426. * whiteout.
  427. */
  428. if (!dentry->d_inode)
  429. return oe->opaque;
  430. /* Negative upper -> positive lower */
  431. if (!oe->__upperdentry)
  432. return true;
  433. /* Positive upper -> have to look up lower to see whether it exists */
  434. for (i = 0; !done && !positive && i < poe->numlower; i++) {
  435. struct dentry *this;
  436. struct dentry *lowerdir = poe->lowerstack[i].dentry;
  437. this = lookup_one_len_unlocked(name->name, lowerdir,
  438. name->len);
  439. if (IS_ERR(this)) {
  440. switch (PTR_ERR(this)) {
  441. case -ENOENT:
  442. case -ENAMETOOLONG:
  443. break;
  444. default:
  445. /*
  446. * Assume something is there, we just couldn't
  447. * access it.
  448. */
  449. positive = true;
  450. break;
  451. }
  452. } else {
  453. if (this->d_inode) {
  454. positive = !ovl_is_whiteout(this);
  455. done = true;
  456. }
  457. dput(this);
  458. }
  459. }
  460. return positive;
  461. }