inode.c 9.3 KB

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