namei.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/namei.h>
  11. #include <linux/xattr.h>
  12. #include <linux/ratelimit.h>
  13. #include "overlayfs.h"
  14. #include "ovl_entry.h"
  15. struct ovl_lookup_data {
  16. struct qstr name;
  17. bool is_dir;
  18. bool opaque;
  19. bool stop;
  20. bool last;
  21. char *redirect;
  22. };
  23. static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
  24. size_t prelen, const char *post)
  25. {
  26. int res;
  27. char *s, *next, *buf = NULL;
  28. res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
  29. if (res < 0) {
  30. if (res == -ENODATA || res == -EOPNOTSUPP)
  31. return 0;
  32. goto fail;
  33. }
  34. buf = kzalloc(prelen + res + strlen(post) + 1, GFP_TEMPORARY);
  35. if (!buf)
  36. return -ENOMEM;
  37. if (res == 0)
  38. goto invalid;
  39. res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
  40. if (res < 0)
  41. goto fail;
  42. if (res == 0)
  43. goto invalid;
  44. if (buf[0] == '/') {
  45. for (s = buf; *s++ == '/'; s = next) {
  46. next = strchrnul(s, '/');
  47. if (s == next)
  48. goto invalid;
  49. }
  50. } else {
  51. if (strchr(buf, '/') != NULL)
  52. goto invalid;
  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. err_free:
  63. kfree(buf);
  64. return 0;
  65. fail:
  66. pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
  67. goto err_free;
  68. invalid:
  69. pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
  70. goto err_free;
  71. }
  72. static bool ovl_is_opaquedir(struct dentry *dentry)
  73. {
  74. int res;
  75. char val;
  76. if (!d_is_dir(dentry))
  77. return false;
  78. res = vfs_getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
  79. if (res == 1 && val == 'y')
  80. return true;
  81. return false;
  82. }
  83. static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
  84. const char *name, unsigned int namelen,
  85. size_t prelen, const char *post,
  86. struct dentry **ret)
  87. {
  88. struct dentry *this;
  89. int err;
  90. this = lookup_one_len_unlocked(name, base, namelen);
  91. if (IS_ERR(this)) {
  92. err = PTR_ERR(this);
  93. this = NULL;
  94. if (err == -ENOENT || err == -ENAMETOOLONG)
  95. goto out;
  96. goto out_err;
  97. }
  98. if (!this->d_inode)
  99. goto put_and_out;
  100. if (ovl_dentry_weird(this)) {
  101. /* Don't support traversing automounts and other weirdness */
  102. err = -EREMOTE;
  103. goto out_err;
  104. }
  105. if (ovl_is_whiteout(this)) {
  106. d->stop = d->opaque = true;
  107. goto put_and_out;
  108. }
  109. if (!d_can_lookup(this)) {
  110. d->stop = true;
  111. if (d->is_dir)
  112. goto put_and_out;
  113. goto out;
  114. }
  115. d->is_dir = true;
  116. if (!d->last && ovl_is_opaquedir(this)) {
  117. d->stop = d->opaque = true;
  118. goto out;
  119. }
  120. err = ovl_check_redirect(this, d, prelen, post);
  121. if (err)
  122. goto out_err;
  123. out:
  124. *ret = this;
  125. return 0;
  126. put_and_out:
  127. dput(this);
  128. this = NULL;
  129. goto out;
  130. out_err:
  131. dput(this);
  132. return err;
  133. }
  134. static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
  135. struct dentry **ret)
  136. {
  137. /* Counting down from the end, since the prefix can change */
  138. size_t rem = d->name.len - 1;
  139. struct dentry *dentry = NULL;
  140. int err;
  141. if (d->name.name[0] != '/')
  142. return ovl_lookup_single(base, d, d->name.name, d->name.len,
  143. 0, "", ret);
  144. while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
  145. const char *s = d->name.name + d->name.len - rem;
  146. const char *next = strchrnul(s, '/');
  147. size_t thislen = next - s;
  148. bool end = !next[0];
  149. /* Verify we did not go off the rails */
  150. if (WARN_ON(s[-1] != '/'))
  151. return -EIO;
  152. err = ovl_lookup_single(base, d, s, thislen,
  153. d->name.len - rem, next, &base);
  154. dput(dentry);
  155. if (err)
  156. return err;
  157. dentry = base;
  158. if (end)
  159. break;
  160. rem -= thislen + 1;
  161. if (WARN_ON(rem >= d->name.len))
  162. return -EIO;
  163. }
  164. *ret = dentry;
  165. return 0;
  166. }
  167. /*
  168. * Returns next layer in stack starting from top.
  169. * Returns -1 if this is the last layer.
  170. */
  171. int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
  172. {
  173. struct ovl_entry *oe = dentry->d_fsdata;
  174. BUG_ON(idx < 0);
  175. if (idx == 0) {
  176. ovl_path_upper(dentry, path);
  177. if (path->dentry)
  178. return oe->numlower ? 1 : -1;
  179. idx++;
  180. }
  181. BUG_ON(idx > oe->numlower);
  182. *path = oe->lowerstack[idx - 1];
  183. return (idx < oe->numlower) ? idx + 1 : -1;
  184. }
  185. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  186. unsigned int flags)
  187. {
  188. struct ovl_entry *oe;
  189. const struct cred *old_cred;
  190. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  191. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  192. struct path *stack = NULL;
  193. struct dentry *upperdir, *upperdentry = NULL;
  194. unsigned int ctr = 0;
  195. struct inode *inode = NULL;
  196. bool upperopaque = false;
  197. char *upperredirect = NULL;
  198. struct dentry *this;
  199. unsigned int i;
  200. int err;
  201. struct ovl_lookup_data d = {
  202. .name = dentry->d_name,
  203. .is_dir = false,
  204. .opaque = false,
  205. .stop = false,
  206. .last = !poe->numlower,
  207. .redirect = NULL,
  208. };
  209. if (dentry->d_name.len > ofs->namelen)
  210. return ERR_PTR(-ENAMETOOLONG);
  211. old_cred = ovl_override_creds(dentry->d_sb);
  212. upperdir = ovl_upperdentry_dereference(poe);
  213. if (upperdir) {
  214. err = ovl_lookup_layer(upperdir, &d, &upperdentry);
  215. if (err)
  216. goto out;
  217. if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
  218. dput(upperdentry);
  219. err = -EREMOTE;
  220. goto out;
  221. }
  222. if (d.redirect) {
  223. upperredirect = kstrdup(d.redirect, GFP_KERNEL);
  224. if (!upperredirect)
  225. goto out_put_upper;
  226. if (d.redirect[0] == '/')
  227. poe = dentry->d_sb->s_root->d_fsdata;
  228. }
  229. upperopaque = d.opaque;
  230. }
  231. if (!d.stop && poe->numlower) {
  232. err = -ENOMEM;
  233. stack = kcalloc(ofs->numlower, sizeof(struct path),
  234. GFP_TEMPORARY);
  235. if (!stack)
  236. goto out_put_upper;
  237. }
  238. for (i = 0; !d.stop && i < poe->numlower; i++) {
  239. struct path lowerpath = poe->lowerstack[i];
  240. d.last = i == poe->numlower - 1;
  241. err = ovl_lookup_layer(lowerpath.dentry, &d, &this);
  242. if (err)
  243. goto out_put;
  244. if (!this)
  245. continue;
  246. stack[ctr].dentry = this;
  247. stack[ctr].mnt = lowerpath.mnt;
  248. ctr++;
  249. if (d.stop)
  250. break;
  251. if (d.redirect &&
  252. d.redirect[0] == '/' &&
  253. poe != dentry->d_sb->s_root->d_fsdata) {
  254. poe = dentry->d_sb->s_root->d_fsdata;
  255. /* Find the current layer on the root dentry */
  256. for (i = 0; i < poe->numlower; i++)
  257. if (poe->lowerstack[i].mnt == lowerpath.mnt)
  258. break;
  259. if (WARN_ON(i == poe->numlower))
  260. break;
  261. }
  262. }
  263. oe = ovl_alloc_entry(ctr);
  264. err = -ENOMEM;
  265. if (!oe)
  266. goto out_put;
  267. if (upperdentry || ctr) {
  268. struct dentry *realdentry;
  269. struct inode *realinode;
  270. realdentry = upperdentry ? upperdentry : stack[0].dentry;
  271. realinode = d_inode(realdentry);
  272. err = -ENOMEM;
  273. if (upperdentry && !d_is_dir(upperdentry)) {
  274. inode = ovl_get_inode(dentry->d_sb, realinode);
  275. } else {
  276. inode = ovl_new_inode(dentry->d_sb, realinode->i_mode,
  277. realinode->i_rdev);
  278. if (inode)
  279. ovl_inode_init(inode, realinode, !!upperdentry);
  280. }
  281. if (!inode)
  282. goto out_free_oe;
  283. ovl_copyattr(realdentry->d_inode, inode);
  284. }
  285. revert_creds(old_cred);
  286. oe->opaque = upperopaque;
  287. oe->redirect = upperredirect;
  288. oe->__upperdentry = upperdentry;
  289. memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
  290. kfree(stack);
  291. kfree(d.redirect);
  292. dentry->d_fsdata = oe;
  293. d_add(dentry, inode);
  294. return NULL;
  295. out_free_oe:
  296. kfree(oe);
  297. out_put:
  298. for (i = 0; i < ctr; i++)
  299. dput(stack[i].dentry);
  300. kfree(stack);
  301. out_put_upper:
  302. dput(upperdentry);
  303. kfree(upperredirect);
  304. out:
  305. kfree(d.redirect);
  306. revert_creds(old_cred);
  307. return ERR_PTR(err);
  308. }
  309. bool ovl_lower_positive(struct dentry *dentry)
  310. {
  311. struct ovl_entry *oe = dentry->d_fsdata;
  312. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  313. const struct qstr *name = &dentry->d_name;
  314. unsigned int i;
  315. bool positive = false;
  316. bool done = false;
  317. /*
  318. * If dentry is negative, then lower is positive iff this is a
  319. * whiteout.
  320. */
  321. if (!dentry->d_inode)
  322. return oe->opaque;
  323. /* Negative upper -> positive lower */
  324. if (!oe->__upperdentry)
  325. return true;
  326. /* Positive upper -> have to look up lower to see whether it exists */
  327. for (i = 0; !done && !positive && i < poe->numlower; i++) {
  328. struct dentry *this;
  329. struct dentry *lowerdir = poe->lowerstack[i].dentry;
  330. this = lookup_one_len_unlocked(name->name, lowerdir,
  331. name->len);
  332. if (IS_ERR(this)) {
  333. switch (PTR_ERR(this)) {
  334. case -ENOENT:
  335. case -ENAMETOOLONG:
  336. break;
  337. default:
  338. /*
  339. * Assume something is there, we just couldn't
  340. * access it.
  341. */
  342. positive = true;
  343. break;
  344. }
  345. } else {
  346. if (this->d_inode) {
  347. positive = !ovl_is_whiteout(this);
  348. done = true;
  349. }
  350. dput(this);
  351. }
  352. }
  353. return positive;
  354. }