attr.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * linux/fs/attr.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * changes by Thomas Schoebel-Theuer
  6. */
  7. #include <linux/export.h>
  8. #include <linux/time.h>
  9. #include <linux/mm.h>
  10. #include <linux/string.h>
  11. #include <linux/capability.h>
  12. #include <linux/fsnotify.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/security.h>
  15. #include <linux/evm.h>
  16. /**
  17. * inode_change_ok - check if attribute changes to an inode are allowed
  18. * @inode: inode to check
  19. * @attr: attributes to change
  20. *
  21. * Check if we are allowed to change the attributes contained in @attr
  22. * in the given inode. This includes the normal unix access permission
  23. * checks, as well as checks for rlimits and others.
  24. *
  25. * Should be called as the first thing in ->setattr implementations,
  26. * possibly after taking additional locks.
  27. */
  28. int inode_change_ok(const struct inode *inode, struct iattr *attr)
  29. {
  30. unsigned int ia_valid = attr->ia_valid;
  31. /*
  32. * First check size constraints. These can't be overriden using
  33. * ATTR_FORCE.
  34. */
  35. if (ia_valid & ATTR_SIZE) {
  36. int error = inode_newsize_ok(inode, attr->ia_size);
  37. if (error)
  38. return error;
  39. }
  40. /* If force is set do it anyway. */
  41. if (ia_valid & ATTR_FORCE)
  42. return 0;
  43. /* Make sure a caller can chown. */
  44. if ((ia_valid & ATTR_UID) &&
  45. (!uid_eq(current_fsuid(), inode->i_uid) ||
  46. !uid_eq(attr->ia_uid, inode->i_uid)) &&
  47. !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
  48. return -EPERM;
  49. /* Make sure caller can chgrp. */
  50. if ((ia_valid & ATTR_GID) &&
  51. (!uid_eq(current_fsuid(), inode->i_uid) ||
  52. (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
  53. !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
  54. return -EPERM;
  55. /* Make sure a caller can chmod. */
  56. if (ia_valid & ATTR_MODE) {
  57. if (!inode_owner_or_capable(inode))
  58. return -EPERM;
  59. /* Also check the setgid bit! */
  60. if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
  61. inode->i_gid) &&
  62. !capable_wrt_inode_uidgid(inode, CAP_FSETID))
  63. attr->ia_mode &= ~S_ISGID;
  64. }
  65. /* Check for setting the inode time. */
  66. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  67. if (!inode_owner_or_capable(inode))
  68. return -EPERM;
  69. }
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(inode_change_ok);
  73. /**
  74. * inode_newsize_ok - may this inode be truncated to a given size
  75. * @inode: the inode to be truncated
  76. * @offset: the new size to assign to the inode
  77. * @Returns: 0 on success, -ve errno on failure
  78. *
  79. * inode_newsize_ok must be called with i_mutex held.
  80. *
  81. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  82. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  83. * when necessary. Caller must not proceed with inode size change if failure is
  84. * returned. @inode must be a file (not directory), with appropriate
  85. * permissions to allow truncate (inode_newsize_ok does NOT check these
  86. * conditions).
  87. */
  88. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  89. {
  90. if (inode->i_size < offset) {
  91. unsigned long limit;
  92. limit = rlimit(RLIMIT_FSIZE);
  93. if (limit != RLIM_INFINITY && offset > limit)
  94. goto out_sig;
  95. if (offset > inode->i_sb->s_maxbytes)
  96. goto out_big;
  97. } else {
  98. /*
  99. * truncation of in-use swapfiles is disallowed - it would
  100. * cause subsequent swapout to scribble on the now-freed
  101. * blocks.
  102. */
  103. if (IS_SWAPFILE(inode))
  104. return -ETXTBSY;
  105. }
  106. return 0;
  107. out_sig:
  108. send_sig(SIGXFSZ, current, 0);
  109. out_big:
  110. return -EFBIG;
  111. }
  112. EXPORT_SYMBOL(inode_newsize_ok);
  113. /**
  114. * setattr_copy - copy simple metadata updates into the generic inode
  115. * @inode: the inode to be updated
  116. * @attr: the new attributes
  117. *
  118. * setattr_copy must be called with i_mutex held.
  119. *
  120. * setattr_copy updates the inode's metadata with that specified
  121. * in attr. Noticeably missing is inode size update, which is more complex
  122. * as it requires pagecache updates.
  123. *
  124. * The inode is not marked as dirty after this operation. The rationale is
  125. * that for "simple" filesystems, the struct inode is the inode storage.
  126. * The caller is free to mark the inode dirty afterwards if needed.
  127. */
  128. void setattr_copy(struct inode *inode, const struct iattr *attr)
  129. {
  130. unsigned int ia_valid = attr->ia_valid;
  131. if (ia_valid & ATTR_UID)
  132. inode->i_uid = attr->ia_uid;
  133. if (ia_valid & ATTR_GID)
  134. inode->i_gid = attr->ia_gid;
  135. if (ia_valid & ATTR_ATIME)
  136. inode->i_atime = timespec_trunc(attr->ia_atime,
  137. inode->i_sb->s_time_gran);
  138. if (ia_valid & ATTR_MTIME)
  139. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  140. inode->i_sb->s_time_gran);
  141. if (ia_valid & ATTR_CTIME)
  142. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  143. inode->i_sb->s_time_gran);
  144. if (ia_valid & ATTR_MODE) {
  145. umode_t mode = attr->ia_mode;
  146. if (!in_group_p(inode->i_gid) &&
  147. !capable_wrt_inode_uidgid(inode, CAP_FSETID))
  148. mode &= ~S_ISGID;
  149. inode->i_mode = mode;
  150. }
  151. }
  152. EXPORT_SYMBOL(setattr_copy);
  153. int notify_change2(struct vfsmount *mnt, struct dentry * dentry, struct iattr * attr)
  154. {
  155. struct inode *inode = dentry->d_inode;
  156. umode_t mode = inode->i_mode;
  157. int error;
  158. struct timespec now;
  159. unsigned int ia_valid = attr->ia_valid;
  160. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  161. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  162. return -EPERM;
  163. }
  164. if ((ia_valid & ATTR_SIZE) && IS_I_VERSION(inode)) {
  165. if (attr->ia_size != inode->i_size)
  166. inode_inc_iversion(inode);
  167. }
  168. if ((ia_valid & ATTR_MODE)) {
  169. umode_t amode = attr->ia_mode;
  170. /* Flag setting protected by i_mutex */
  171. if (is_sxid(amode))
  172. inode->i_flags &= ~S_NOSEC;
  173. }
  174. now = current_fs_time(inode->i_sb);
  175. attr->ia_ctime = now;
  176. if (!(ia_valid & ATTR_ATIME_SET))
  177. attr->ia_atime = now;
  178. if (!(ia_valid & ATTR_MTIME_SET))
  179. attr->ia_mtime = now;
  180. if (ia_valid & ATTR_KILL_PRIV) {
  181. attr->ia_valid &= ~ATTR_KILL_PRIV;
  182. ia_valid &= ~ATTR_KILL_PRIV;
  183. error = security_inode_need_killpriv(dentry);
  184. if (error > 0)
  185. error = security_inode_killpriv(dentry);
  186. if (error)
  187. return error;
  188. }
  189. /*
  190. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  191. * that the function has the ability to reinterpret a mode change
  192. * that's due to these bits. This adds an implicit restriction that
  193. * no function will ever call notify_change with both ATTR_MODE and
  194. * ATTR_KILL_S*ID set.
  195. */
  196. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  197. (ia_valid & ATTR_MODE))
  198. BUG();
  199. if (ia_valid & ATTR_KILL_SUID) {
  200. if (mode & S_ISUID) {
  201. ia_valid = attr->ia_valid |= ATTR_MODE;
  202. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  203. }
  204. }
  205. if (ia_valid & ATTR_KILL_SGID) {
  206. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  207. if (!(ia_valid & ATTR_MODE)) {
  208. ia_valid = attr->ia_valid |= ATTR_MODE;
  209. attr->ia_mode = inode->i_mode;
  210. }
  211. attr->ia_mode &= ~S_ISGID;
  212. }
  213. }
  214. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  215. return 0;
  216. error = security_inode_setattr(dentry, attr);
  217. if (error)
  218. return error;
  219. if (mnt && inode->i_op->setattr2)
  220. error = inode->i_op->setattr2(mnt, dentry, attr);
  221. else if (inode->i_op->setattr)
  222. error = inode->i_op->setattr(dentry, attr);
  223. else
  224. error = simple_setattr(dentry, attr);
  225. if (!error) {
  226. fsnotify_change(dentry, ia_valid);
  227. evm_inode_post_setattr(dentry, ia_valid);
  228. }
  229. return error;
  230. }
  231. EXPORT_SYMBOL(notify_change2);
  232. int notify_change(struct dentry * dentry, struct iattr * attr)
  233. {
  234. return notify_change2(NULL, dentry, attr);
  235. }
  236. EXPORT_SYMBOL(notify_change);