inode.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell 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/slab.h>
  11. #include <linux/xattr.h>
  12. #include "overlayfs.h"
  13. static int ovl_copy_up_truncate(struct dentry *dentry)
  14. {
  15. int err;
  16. struct dentry *parent;
  17. struct kstat stat;
  18. struct path lowerpath;
  19. parent = dget_parent(dentry);
  20. err = ovl_copy_up(parent);
  21. if (err)
  22. goto out_dput_parent;
  23. ovl_path_lower(dentry, &lowerpath);
  24. err = vfs_getattr(&lowerpath, &stat);
  25. if (err)
  26. goto out_dput_parent;
  27. stat.size = 0;
  28. err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
  29. out_dput_parent:
  30. dput(parent);
  31. return err;
  32. }
  33. int ovl_setattr(struct dentry *dentry, struct iattr *attr)
  34. {
  35. int err;
  36. struct dentry *upperdentry;
  37. /*
  38. * Check for permissions before trying to copy-up. This is redundant
  39. * since it will be rechecked later by ->setattr() on upper dentry. But
  40. * without this, copy-up can be triggered by just about anybody.
  41. *
  42. * We don't initialize inode->size, which just means that
  43. * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
  44. * check for a swapfile (which this won't be anyway).
  45. */
  46. err = inode_change_ok(dentry->d_inode, attr);
  47. if (err)
  48. return err;
  49. err = ovl_want_write(dentry);
  50. if (err)
  51. goto out;
  52. err = ovl_copy_up(dentry);
  53. if (!err) {
  54. upperdentry = ovl_dentry_upper(dentry);
  55. if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  56. attr->ia_valid &= ~ATTR_MODE;
  57. mutex_lock(&upperdentry->d_inode->i_mutex);
  58. err = notify_change(upperdentry, attr, NULL);
  59. if (!err)
  60. ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
  61. mutex_unlock(&upperdentry->d_inode->i_mutex);
  62. }
  63. ovl_drop_write(dentry);
  64. out:
  65. return err;
  66. }
  67. static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
  68. struct kstat *stat)
  69. {
  70. struct path realpath;
  71. ovl_path_real(dentry, &realpath);
  72. return vfs_getattr(&realpath, stat);
  73. }
  74. int ovl_permission(struct inode *inode, int mask)
  75. {
  76. struct ovl_entry *oe;
  77. struct dentry *alias = NULL;
  78. struct inode *realinode;
  79. struct dentry *realdentry;
  80. bool is_upper;
  81. int err;
  82. if (S_ISDIR(inode->i_mode)) {
  83. oe = inode->i_private;
  84. } else if (mask & MAY_NOT_BLOCK) {
  85. return -ECHILD;
  86. } else {
  87. /*
  88. * For non-directories find an alias and get the info
  89. * from there.
  90. */
  91. alias = d_find_any_alias(inode);
  92. if (WARN_ON(!alias))
  93. return -ENOENT;
  94. oe = alias->d_fsdata;
  95. }
  96. realdentry = ovl_entry_real(oe, &is_upper);
  97. /* Careful in RCU walk mode */
  98. realinode = ACCESS_ONCE(realdentry->d_inode);
  99. if (!realinode) {
  100. WARN_ON(!(mask & MAY_NOT_BLOCK));
  101. err = -ENOENT;
  102. goto out_dput;
  103. }
  104. if (mask & MAY_WRITE) {
  105. umode_t mode = realinode->i_mode;
  106. /*
  107. * Writes will always be redirected to upper layer, so
  108. * ignore lower layer being read-only.
  109. *
  110. * If the overlay itself is read-only then proceed
  111. * with the permission check, don't return EROFS.
  112. * This will only happen if this is the lower layer of
  113. * another overlayfs.
  114. *
  115. * If upper fs becomes read-only after the overlay was
  116. * constructed return EROFS to prevent modification of
  117. * upper layer.
  118. */
  119. err = -EROFS;
  120. if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
  121. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  122. goto out_dput;
  123. }
  124. err = __inode_permission(realinode, mask);
  125. out_dput:
  126. dput(alias);
  127. return err;
  128. }
  129. struct ovl_link_data {
  130. struct dentry *realdentry;
  131. void *cookie;
  132. };
  133. static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
  134. {
  135. struct dentry *realdentry;
  136. struct inode *realinode;
  137. struct ovl_link_data *data = NULL;
  138. const char *ret;
  139. realdentry = ovl_dentry_real(dentry);
  140. realinode = realdentry->d_inode;
  141. if (WARN_ON(!realinode->i_op->follow_link))
  142. return ERR_PTR(-EPERM);
  143. if (realinode->i_op->put_link) {
  144. data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
  145. if (!data)
  146. return ERR_PTR(-ENOMEM);
  147. data->realdentry = realdentry;
  148. }
  149. ret = realinode->i_op->follow_link(realdentry, cookie);
  150. if (IS_ERR_OR_NULL(ret)) {
  151. kfree(data);
  152. return ret;
  153. }
  154. if (data)
  155. data->cookie = *cookie;
  156. *cookie = data;
  157. return ret;
  158. }
  159. static void ovl_put_link(struct inode *unused, void *c)
  160. {
  161. struct inode *realinode;
  162. struct ovl_link_data *data = c;
  163. if (!data)
  164. return;
  165. realinode = data->realdentry->d_inode;
  166. realinode->i_op->put_link(realinode, data->cookie);
  167. kfree(data);
  168. }
  169. static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  170. {
  171. struct path realpath;
  172. struct inode *realinode;
  173. ovl_path_real(dentry, &realpath);
  174. realinode = realpath.dentry->d_inode;
  175. if (!realinode->i_op->readlink)
  176. return -EINVAL;
  177. touch_atime(&realpath);
  178. return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
  179. }
  180. bool ovl_is_private_xattr(const char *name)
  181. {
  182. return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
  183. }
  184. int ovl_setxattr(struct dentry *dentry, const char *name,
  185. const void *value, size_t size, int flags)
  186. {
  187. int err;
  188. struct dentry *upperdentry;
  189. err = ovl_want_write(dentry);
  190. if (err)
  191. goto out;
  192. err = -EPERM;
  193. if (ovl_is_private_xattr(name))
  194. goto out_drop_write;
  195. err = ovl_copy_up(dentry);
  196. if (err)
  197. goto out_drop_write;
  198. upperdentry = ovl_dentry_upper(dentry);
  199. err = vfs_setxattr(upperdentry, name, value, size, flags);
  200. out_drop_write:
  201. ovl_drop_write(dentry);
  202. out:
  203. return err;
  204. }
  205. static bool ovl_need_xattr_filter(struct dentry *dentry,
  206. enum ovl_path_type type)
  207. {
  208. if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
  209. return S_ISDIR(dentry->d_inode->i_mode);
  210. else
  211. return false;
  212. }
  213. ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
  214. void *value, size_t size)
  215. {
  216. struct path realpath;
  217. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  218. if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
  219. return -ENODATA;
  220. return vfs_getxattr(realpath.dentry, name, value, size);
  221. }
  222. static bool ovl_can_list(const char *s)
  223. {
  224. /* List all non-trusted xatts */
  225. if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
  226. return true;
  227. /* Never list trusted.overlay, list other trusted for superuser only */
  228. return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
  229. }
  230. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  231. {
  232. struct path realpath;
  233. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  234. ssize_t res;
  235. size_t len;
  236. char *s;
  237. res = vfs_listxattr(realpath.dentry, list, size);
  238. if (res <= 0 || size == 0)
  239. return res;
  240. if (!ovl_need_xattr_filter(dentry, type))
  241. return res;
  242. /* filter out private xattrs */
  243. for (s = list, len = res; len;) {
  244. size_t slen = strnlen(s, len) + 1;
  245. /* underlying fs providing us with an broken xattr list? */
  246. if (WARN_ON(slen > len))
  247. return -EIO;
  248. len -= slen;
  249. if (!ovl_can_list(s)) {
  250. res -= slen;
  251. memmove(s, s + slen, len);
  252. } else {
  253. s += slen;
  254. }
  255. }
  256. return res;
  257. }
  258. int ovl_removexattr(struct dentry *dentry, const char *name)
  259. {
  260. int err;
  261. struct path realpath;
  262. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  263. err = ovl_want_write(dentry);
  264. if (err)
  265. goto out;
  266. err = -ENODATA;
  267. if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
  268. goto out_drop_write;
  269. if (!OVL_TYPE_UPPER(type)) {
  270. err = vfs_getxattr(realpath.dentry, name, NULL, 0);
  271. if (err < 0)
  272. goto out_drop_write;
  273. err = ovl_copy_up(dentry);
  274. if (err)
  275. goto out_drop_write;
  276. ovl_path_upper(dentry, &realpath);
  277. }
  278. err = vfs_removexattr(realpath.dentry, name);
  279. out_drop_write:
  280. ovl_drop_write(dentry);
  281. out:
  282. return err;
  283. }
  284. static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
  285. struct dentry *realdentry)
  286. {
  287. if (OVL_TYPE_UPPER(type))
  288. return false;
  289. if (special_file(realdentry->d_inode->i_mode))
  290. return false;
  291. if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  292. return false;
  293. return true;
  294. }
  295. struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
  296. {
  297. int err;
  298. struct path realpath;
  299. enum ovl_path_type type;
  300. if (d_is_dir(dentry))
  301. return d_backing_inode(dentry);
  302. type = ovl_path_real(dentry, &realpath);
  303. if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
  304. err = ovl_want_write(dentry);
  305. if (err)
  306. return ERR_PTR(err);
  307. if (file_flags & O_TRUNC)
  308. err = ovl_copy_up_truncate(dentry);
  309. else
  310. err = ovl_copy_up(dentry);
  311. ovl_drop_write(dentry);
  312. if (err)
  313. return ERR_PTR(err);
  314. ovl_path_upper(dentry, &realpath);
  315. }
  316. if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
  317. return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
  318. return d_backing_inode(realpath.dentry);
  319. }
  320. static const struct inode_operations ovl_file_inode_operations = {
  321. .setattr = ovl_setattr,
  322. .permission = ovl_permission,
  323. .getattr = ovl_getattr,
  324. .setxattr = ovl_setxattr,
  325. .getxattr = ovl_getxattr,
  326. .listxattr = ovl_listxattr,
  327. .removexattr = ovl_removexattr,
  328. };
  329. static const struct inode_operations ovl_symlink_inode_operations = {
  330. .setattr = ovl_setattr,
  331. .follow_link = ovl_follow_link,
  332. .put_link = ovl_put_link,
  333. .readlink = ovl_readlink,
  334. .getattr = ovl_getattr,
  335. .setxattr = ovl_setxattr,
  336. .getxattr = ovl_getxattr,
  337. .listxattr = ovl_listxattr,
  338. .removexattr = ovl_removexattr,
  339. };
  340. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
  341. struct ovl_entry *oe)
  342. {
  343. struct inode *inode;
  344. inode = new_inode(sb);
  345. if (!inode)
  346. return NULL;
  347. inode->i_ino = get_next_ino();
  348. inode->i_mode = mode;
  349. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  350. mode &= S_IFMT;
  351. switch (mode) {
  352. case S_IFDIR:
  353. inode->i_private = oe;
  354. inode->i_op = &ovl_dir_inode_operations;
  355. inode->i_fop = &ovl_dir_operations;
  356. break;
  357. case S_IFLNK:
  358. inode->i_op = &ovl_symlink_inode_operations;
  359. break;
  360. case S_IFREG:
  361. case S_IFSOCK:
  362. case S_IFBLK:
  363. case S_IFCHR:
  364. case S_IFIFO:
  365. inode->i_op = &ovl_file_inode_operations;
  366. break;
  367. default:
  368. WARN(1, "illegal file type: %i\n", mode);
  369. iput(inode);
  370. inode = NULL;
  371. }
  372. return inode;
  373. }