inode.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <linux/posix_acl.h>
  13. #include "overlayfs.h"
  14. int ovl_setattr(struct dentry *dentry, struct iattr *attr)
  15. {
  16. int err;
  17. struct dentry *upperdentry;
  18. const struct cred *old_cred;
  19. /*
  20. * Check for permissions before trying to copy-up. This is redundant
  21. * since it will be rechecked later by ->setattr() on upper dentry. But
  22. * without this, copy-up can be triggered by just about anybody.
  23. *
  24. * We don't initialize inode->size, which just means that
  25. * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
  26. * check for a swapfile (which this won't be anyway).
  27. */
  28. err = setattr_prepare(dentry, attr);
  29. if (err)
  30. return err;
  31. err = ovl_want_write(dentry);
  32. if (err)
  33. goto out;
  34. err = ovl_copy_up(dentry);
  35. if (!err) {
  36. upperdentry = ovl_dentry_upper(dentry);
  37. if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  38. attr->ia_valid &= ~ATTR_MODE;
  39. inode_lock(upperdentry->d_inode);
  40. old_cred = ovl_override_creds(dentry->d_sb);
  41. err = notify_change(upperdentry, attr, NULL);
  42. revert_creds(old_cred);
  43. if (!err)
  44. ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
  45. inode_unlock(upperdentry->d_inode);
  46. }
  47. ovl_drop_write(dentry);
  48. out:
  49. return err;
  50. }
  51. static int ovl_getattr(const struct path *path, struct kstat *stat,
  52. u32 request_mask, unsigned int flags)
  53. {
  54. struct dentry *dentry = path->dentry;
  55. struct path realpath;
  56. const struct cred *old_cred;
  57. int err;
  58. ovl_path_real(dentry, &realpath);
  59. old_cred = ovl_override_creds(dentry->d_sb);
  60. err = vfs_getattr(&realpath, stat, request_mask, flags);
  61. revert_creds(old_cred);
  62. return err;
  63. }
  64. int ovl_permission(struct inode *inode, int mask)
  65. {
  66. bool is_upper;
  67. struct inode *realinode = ovl_inode_real(inode, &is_upper);
  68. const struct cred *old_cred;
  69. int err;
  70. /* Careful in RCU walk mode */
  71. if (!realinode) {
  72. WARN_ON(!(mask & MAY_NOT_BLOCK));
  73. return -ECHILD;
  74. }
  75. /*
  76. * Check overlay inode with the creds of task and underlying inode
  77. * with creds of mounter
  78. */
  79. err = generic_permission(inode, mask);
  80. if (err)
  81. return err;
  82. old_cred = ovl_override_creds(inode->i_sb);
  83. if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
  84. mask &= ~(MAY_WRITE | MAY_APPEND);
  85. /* Make sure mounter can read file for copy up later */
  86. mask |= MAY_READ;
  87. }
  88. err = inode_permission(realinode, mask);
  89. revert_creds(old_cred);
  90. return err;
  91. }
  92. static const char *ovl_get_link(struct dentry *dentry,
  93. struct inode *inode,
  94. struct delayed_call *done)
  95. {
  96. const struct cred *old_cred;
  97. const char *p;
  98. if (!dentry)
  99. return ERR_PTR(-ECHILD);
  100. old_cred = ovl_override_creds(dentry->d_sb);
  101. p = vfs_get_link(ovl_dentry_real(dentry), done);
  102. revert_creds(old_cred);
  103. return p;
  104. }
  105. bool ovl_is_private_xattr(const char *name)
  106. {
  107. return strncmp(name, OVL_XATTR_PREFIX,
  108. sizeof(OVL_XATTR_PREFIX) - 1) == 0;
  109. }
  110. int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
  111. size_t size, int flags)
  112. {
  113. int err;
  114. struct path realpath;
  115. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  116. const struct cred *old_cred;
  117. err = ovl_want_write(dentry);
  118. if (err)
  119. goto out;
  120. if (!value && !OVL_TYPE_UPPER(type)) {
  121. err = vfs_getxattr(realpath.dentry, name, NULL, 0);
  122. if (err < 0)
  123. goto out_drop_write;
  124. }
  125. err = ovl_copy_up(dentry);
  126. if (err)
  127. goto out_drop_write;
  128. if (!OVL_TYPE_UPPER(type))
  129. ovl_path_upper(dentry, &realpath);
  130. old_cred = ovl_override_creds(dentry->d_sb);
  131. if (value)
  132. err = vfs_setxattr(realpath.dentry, name, value, size, flags);
  133. else {
  134. WARN_ON(flags != XATTR_REPLACE);
  135. err = vfs_removexattr(realpath.dentry, name);
  136. }
  137. revert_creds(old_cred);
  138. out_drop_write:
  139. ovl_drop_write(dentry);
  140. out:
  141. return err;
  142. }
  143. int ovl_xattr_get(struct dentry *dentry, const char *name,
  144. void *value, size_t size)
  145. {
  146. struct dentry *realdentry = ovl_dentry_real(dentry);
  147. ssize_t res;
  148. const struct cred *old_cred;
  149. old_cred = ovl_override_creds(dentry->d_sb);
  150. res = vfs_getxattr(realdentry, name, value, size);
  151. revert_creds(old_cred);
  152. return res;
  153. }
  154. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  155. {
  156. struct dentry *realdentry = ovl_dentry_real(dentry);
  157. ssize_t res;
  158. size_t len;
  159. char *s;
  160. const struct cred *old_cred;
  161. old_cred = ovl_override_creds(dentry->d_sb);
  162. res = vfs_listxattr(realdentry, list, size);
  163. revert_creds(old_cred);
  164. if (res <= 0 || size == 0)
  165. return res;
  166. /* filter out private xattrs */
  167. for (s = list, len = res; len;) {
  168. size_t slen = strnlen(s, len) + 1;
  169. /* underlying fs providing us with an broken xattr list? */
  170. if (WARN_ON(slen > len))
  171. return -EIO;
  172. len -= slen;
  173. if (ovl_is_private_xattr(s)) {
  174. res -= slen;
  175. memmove(s, s + slen, len);
  176. } else {
  177. s += slen;
  178. }
  179. }
  180. return res;
  181. }
  182. struct posix_acl *ovl_get_acl(struct inode *inode, int type)
  183. {
  184. struct inode *realinode = ovl_inode_real(inode, NULL);
  185. const struct cred *old_cred;
  186. struct posix_acl *acl;
  187. if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
  188. return NULL;
  189. old_cred = ovl_override_creds(inode->i_sb);
  190. acl = get_acl(realinode, type);
  191. revert_creds(old_cred);
  192. return acl;
  193. }
  194. static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
  195. struct dentry *realdentry)
  196. {
  197. if (OVL_TYPE_UPPER(type))
  198. return false;
  199. if (special_file(realdentry->d_inode->i_mode))
  200. return false;
  201. if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  202. return false;
  203. return true;
  204. }
  205. int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
  206. {
  207. int err = 0;
  208. struct path realpath;
  209. enum ovl_path_type type;
  210. type = ovl_path_real(dentry, &realpath);
  211. if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
  212. err = ovl_want_write(dentry);
  213. if (!err) {
  214. err = ovl_copy_up_flags(dentry, file_flags);
  215. ovl_drop_write(dentry);
  216. }
  217. }
  218. return err;
  219. }
  220. int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
  221. {
  222. struct dentry *alias;
  223. struct path upperpath;
  224. if (!(flags & S_ATIME))
  225. return 0;
  226. alias = d_find_any_alias(inode);
  227. if (!alias)
  228. return 0;
  229. ovl_path_upper(alias, &upperpath);
  230. if (upperpath.dentry) {
  231. touch_atime(&upperpath);
  232. inode->i_atime = d_inode(upperpath.dentry)->i_atime;
  233. }
  234. dput(alias);
  235. return 0;
  236. }
  237. static const struct inode_operations ovl_file_inode_operations = {
  238. .setattr = ovl_setattr,
  239. .permission = ovl_permission,
  240. .getattr = ovl_getattr,
  241. .listxattr = ovl_listxattr,
  242. .get_acl = ovl_get_acl,
  243. .update_time = ovl_update_time,
  244. };
  245. static const struct inode_operations ovl_symlink_inode_operations = {
  246. .setattr = ovl_setattr,
  247. .get_link = ovl_get_link,
  248. .getattr = ovl_getattr,
  249. .listxattr = ovl_listxattr,
  250. .update_time = ovl_update_time,
  251. };
  252. static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
  253. {
  254. inode->i_ino = get_next_ino();
  255. inode->i_mode = mode;
  256. inode->i_flags |= S_NOCMTIME;
  257. #ifdef CONFIG_FS_POSIX_ACL
  258. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  259. #endif
  260. switch (mode & S_IFMT) {
  261. case S_IFREG:
  262. inode->i_op = &ovl_file_inode_operations;
  263. break;
  264. case S_IFDIR:
  265. inode->i_op = &ovl_dir_inode_operations;
  266. inode->i_fop = &ovl_dir_operations;
  267. break;
  268. case S_IFLNK:
  269. inode->i_op = &ovl_symlink_inode_operations;
  270. break;
  271. default:
  272. inode->i_op = &ovl_file_inode_operations;
  273. init_special_inode(inode, mode, rdev);
  274. break;
  275. }
  276. }
  277. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  278. {
  279. struct inode *inode;
  280. inode = new_inode(sb);
  281. if (inode)
  282. ovl_fill_inode(inode, mode, rdev);
  283. return inode;
  284. }
  285. static int ovl_inode_test(struct inode *inode, void *data)
  286. {
  287. return ovl_inode_real(inode, NULL) == data;
  288. }
  289. static int ovl_inode_set(struct inode *inode, void *data)
  290. {
  291. inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
  292. return 0;
  293. }
  294. struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
  295. {
  296. struct inode *inode;
  297. inode = iget5_locked(sb, (unsigned long) realinode,
  298. ovl_inode_test, ovl_inode_set, realinode);
  299. if (inode && inode->i_state & I_NEW) {
  300. ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
  301. set_nlink(inode, realinode->i_nlink);
  302. unlock_new_inode(inode);
  303. }
  304. return inode;
  305. }