inode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. inode_lock(upperdentry->d_inode);
  56. err = notify_change(upperdentry, attr, NULL);
  57. inode_unlock(upperdentry->d_inode);
  58. }
  59. ovl_drop_write(dentry);
  60. out:
  61. return err;
  62. }
  63. static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
  64. struct kstat *stat)
  65. {
  66. struct path realpath;
  67. ovl_path_real(dentry, &realpath);
  68. return vfs_getattr(&realpath, stat);
  69. }
  70. int ovl_permission(struct inode *inode, int mask)
  71. {
  72. struct ovl_entry *oe;
  73. struct dentry *alias = NULL;
  74. struct inode *realinode;
  75. struct dentry *realdentry;
  76. bool is_upper;
  77. int err;
  78. if (S_ISDIR(inode->i_mode)) {
  79. oe = inode->i_private;
  80. } else if (mask & MAY_NOT_BLOCK) {
  81. return -ECHILD;
  82. } else {
  83. /*
  84. * For non-directories find an alias and get the info
  85. * from there.
  86. */
  87. alias = d_find_any_alias(inode);
  88. if (WARN_ON(!alias))
  89. return -ENOENT;
  90. oe = alias->d_fsdata;
  91. }
  92. realdentry = ovl_entry_real(oe, &is_upper);
  93. if (ovl_is_default_permissions(inode)) {
  94. struct kstat stat;
  95. struct path realpath = { .dentry = realdentry };
  96. if (mask & MAY_NOT_BLOCK)
  97. return -ECHILD;
  98. realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
  99. err = vfs_getattr(&realpath, &stat);
  100. if (err)
  101. return err;
  102. if ((stat.mode ^ inode->i_mode) & S_IFMT)
  103. return -ESTALE;
  104. inode->i_mode = stat.mode;
  105. inode->i_uid = stat.uid;
  106. inode->i_gid = stat.gid;
  107. return generic_permission(inode, mask);
  108. }
  109. /* Careful in RCU walk mode */
  110. realinode = ACCESS_ONCE(realdentry->d_inode);
  111. if (!realinode) {
  112. WARN_ON(!(mask & MAY_NOT_BLOCK));
  113. err = -ENOENT;
  114. goto out_dput;
  115. }
  116. if (mask & MAY_WRITE) {
  117. umode_t mode = realinode->i_mode;
  118. /*
  119. * Writes will always be redirected to upper layer, so
  120. * ignore lower layer being read-only.
  121. *
  122. * If the overlay itself is read-only then proceed
  123. * with the permission check, don't return EROFS.
  124. * This will only happen if this is the lower layer of
  125. * another overlayfs.
  126. *
  127. * If upper fs becomes read-only after the overlay was
  128. * constructed return EROFS to prevent modification of
  129. * upper layer.
  130. */
  131. err = -EROFS;
  132. if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
  133. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  134. goto out_dput;
  135. }
  136. err = __inode_permission(realinode, mask);
  137. out_dput:
  138. dput(alias);
  139. return err;
  140. }
  141. static const char *ovl_get_link(struct dentry *dentry,
  142. struct inode *inode,
  143. struct delayed_call *done)
  144. {
  145. struct dentry *realdentry;
  146. struct inode *realinode;
  147. if (!dentry)
  148. return ERR_PTR(-ECHILD);
  149. realdentry = ovl_dentry_real(dentry);
  150. realinode = realdentry->d_inode;
  151. if (WARN_ON(!realinode->i_op->get_link))
  152. return ERR_PTR(-EPERM);
  153. return realinode->i_op->get_link(realdentry, realinode, done);
  154. }
  155. static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  156. {
  157. struct path realpath;
  158. struct inode *realinode;
  159. ovl_path_real(dentry, &realpath);
  160. realinode = realpath.dentry->d_inode;
  161. if (!realinode->i_op->readlink)
  162. return -EINVAL;
  163. touch_atime(&realpath);
  164. return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
  165. }
  166. static bool ovl_is_private_xattr(const char *name)
  167. {
  168. return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
  169. }
  170. int ovl_setxattr(struct dentry *dentry, const char *name,
  171. const void *value, size_t size, int flags)
  172. {
  173. int err;
  174. struct dentry *upperdentry;
  175. err = ovl_want_write(dentry);
  176. if (err)
  177. goto out;
  178. err = -EPERM;
  179. if (ovl_is_private_xattr(name))
  180. goto out_drop_write;
  181. err = ovl_copy_up(dentry);
  182. if (err)
  183. goto out_drop_write;
  184. upperdentry = ovl_dentry_upper(dentry);
  185. err = vfs_setxattr(upperdentry, name, value, size, flags);
  186. out_drop_write:
  187. ovl_drop_write(dentry);
  188. out:
  189. return err;
  190. }
  191. static bool ovl_need_xattr_filter(struct dentry *dentry,
  192. enum ovl_path_type type)
  193. {
  194. if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
  195. return S_ISDIR(dentry->d_inode->i_mode);
  196. else
  197. return false;
  198. }
  199. ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
  200. void *value, size_t size)
  201. {
  202. struct path realpath;
  203. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  204. if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
  205. return -ENODATA;
  206. return vfs_getxattr(realpath.dentry, name, value, size);
  207. }
  208. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  209. {
  210. struct path realpath;
  211. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  212. ssize_t res;
  213. int off;
  214. res = vfs_listxattr(realpath.dentry, list, size);
  215. if (res <= 0 || size == 0)
  216. return res;
  217. if (!ovl_need_xattr_filter(dentry, type))
  218. return res;
  219. /* filter out private xattrs */
  220. for (off = 0; off < res;) {
  221. char *s = list + off;
  222. size_t slen = strlen(s) + 1;
  223. BUG_ON(off + slen > res);
  224. if (ovl_is_private_xattr(s)) {
  225. res -= slen;
  226. memmove(s, s + slen, res - off);
  227. } else {
  228. off += slen;
  229. }
  230. }
  231. return res;
  232. }
  233. int ovl_removexattr(struct dentry *dentry, const char *name)
  234. {
  235. int err;
  236. struct path realpath;
  237. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  238. err = ovl_want_write(dentry);
  239. if (err)
  240. goto out;
  241. err = -ENODATA;
  242. if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
  243. goto out_drop_write;
  244. if (!OVL_TYPE_UPPER(type)) {
  245. err = vfs_getxattr(realpath.dentry, name, NULL, 0);
  246. if (err < 0)
  247. goto out_drop_write;
  248. err = ovl_copy_up(dentry);
  249. if (err)
  250. goto out_drop_write;
  251. ovl_path_upper(dentry, &realpath);
  252. }
  253. err = vfs_removexattr(realpath.dentry, name);
  254. out_drop_write:
  255. ovl_drop_write(dentry);
  256. out:
  257. return err;
  258. }
  259. static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
  260. struct dentry *realdentry)
  261. {
  262. if (OVL_TYPE_UPPER(type))
  263. return false;
  264. if (special_file(realdentry->d_inode->i_mode))
  265. return false;
  266. if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  267. return false;
  268. return true;
  269. }
  270. struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
  271. {
  272. int err;
  273. struct path realpath;
  274. enum ovl_path_type type;
  275. if (d_is_dir(dentry))
  276. return d_backing_inode(dentry);
  277. type = ovl_path_real(dentry, &realpath);
  278. if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
  279. err = ovl_want_write(dentry);
  280. if (err)
  281. return ERR_PTR(err);
  282. if (file_flags & O_TRUNC)
  283. err = ovl_copy_up_truncate(dentry);
  284. else
  285. err = ovl_copy_up(dentry);
  286. ovl_drop_write(dentry);
  287. if (err)
  288. return ERR_PTR(err);
  289. ovl_path_upper(dentry, &realpath);
  290. }
  291. if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
  292. return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
  293. return d_backing_inode(realpath.dentry);
  294. }
  295. static const struct inode_operations ovl_file_inode_operations = {
  296. .setattr = ovl_setattr,
  297. .permission = ovl_permission,
  298. .getattr = ovl_getattr,
  299. .setxattr = ovl_setxattr,
  300. .getxattr = ovl_getxattr,
  301. .listxattr = ovl_listxattr,
  302. .removexattr = ovl_removexattr,
  303. };
  304. static const struct inode_operations ovl_symlink_inode_operations = {
  305. .setattr = ovl_setattr,
  306. .get_link = ovl_get_link,
  307. .readlink = ovl_readlink,
  308. .getattr = ovl_getattr,
  309. .setxattr = ovl_setxattr,
  310. .getxattr = ovl_getxattr,
  311. .listxattr = ovl_listxattr,
  312. .removexattr = ovl_removexattr,
  313. };
  314. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
  315. struct ovl_entry *oe)
  316. {
  317. struct inode *inode;
  318. inode = new_inode(sb);
  319. if (!inode)
  320. return NULL;
  321. mode &= S_IFMT;
  322. inode->i_ino = get_next_ino();
  323. inode->i_mode = mode;
  324. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  325. switch (mode) {
  326. case S_IFDIR:
  327. inode->i_private = oe;
  328. inode->i_op = &ovl_dir_inode_operations;
  329. inode->i_fop = &ovl_dir_operations;
  330. break;
  331. case S_IFLNK:
  332. inode->i_op = &ovl_symlink_inode_operations;
  333. break;
  334. case S_IFREG:
  335. case S_IFSOCK:
  336. case S_IFBLK:
  337. case S_IFCHR:
  338. case S_IFIFO:
  339. inode->i_op = &ovl_file_inode_operations;
  340. break;
  341. default:
  342. WARN(1, "illegal file type: %i\n", mode);
  343. iput(inode);
  344. inode = NULL;
  345. }
  346. return inode;
  347. }