inode.c 9.5 KB

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