inode.c 10 KB

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