attr.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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)) && !capable(CAP_CHOWN))
  47. return -EPERM;
  48. /* Make sure caller can chgrp. */
  49. if ((ia_valid & ATTR_GID) &&
  50. (!uid_eq(current_fsuid(), inode->i_uid) ||
  51. (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
  52. !capable(CAP_CHOWN))
  53. return -EPERM;
  54. /* Make sure a caller can chmod. */
  55. if (ia_valid & ATTR_MODE) {
  56. if (!inode_owner_or_capable(inode))
  57. return -EPERM;
  58. /* Also check the setgid bit! */
  59. if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
  60. inode->i_gid) && !capable(CAP_FSETID))
  61. attr->ia_mode &= ~S_ISGID;
  62. }
  63. /* Check for setting the inode time. */
  64. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  65. if (!inode_owner_or_capable(inode))
  66. return -EPERM;
  67. }
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(inode_change_ok);
  71. /**
  72. * inode_newsize_ok - may this inode be truncated to a given size
  73. * @inode: the inode to be truncated
  74. * @offset: the new size to assign to the inode
  75. * @Returns: 0 on success, -ve errno on failure
  76. *
  77. * inode_newsize_ok must be called with i_mutex held.
  78. *
  79. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  80. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  81. * when necessary. Caller must not proceed with inode size change if failure is
  82. * returned. @inode must be a file (not directory), with appropriate
  83. * permissions to allow truncate (inode_newsize_ok does NOT check these
  84. * conditions).
  85. */
  86. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  87. {
  88. if (inode->i_size < offset) {
  89. unsigned long limit;
  90. limit = rlimit(RLIMIT_FSIZE);
  91. if (limit != RLIM_INFINITY && offset > limit)
  92. goto out_sig;
  93. if (offset > inode->i_sb->s_maxbytes)
  94. goto out_big;
  95. } else {
  96. /*
  97. * truncation of in-use swapfiles is disallowed - it would
  98. * cause subsequent swapout to scribble on the now-freed
  99. * blocks.
  100. */
  101. if (IS_SWAPFILE(inode))
  102. return -ETXTBSY;
  103. }
  104. return 0;
  105. out_sig:
  106. send_sig(SIGXFSZ, current, 0);
  107. out_big:
  108. return -EFBIG;
  109. }
  110. EXPORT_SYMBOL(inode_newsize_ok);
  111. /**
  112. * setattr_copy - copy simple metadata updates into the generic inode
  113. * @inode: the inode to be updated
  114. * @attr: the new attributes
  115. *
  116. * setattr_copy must be called with i_mutex held.
  117. *
  118. * setattr_copy updates the inode's metadata with that specified
  119. * in attr. Noticeably missing is inode size update, which is more complex
  120. * as it requires pagecache updates.
  121. *
  122. * The inode is not marked as dirty after this operation. The rationale is
  123. * that for "simple" filesystems, the struct inode is the inode storage.
  124. * The caller is free to mark the inode dirty afterwards if needed.
  125. */
  126. void setattr_copy(struct inode *inode, const struct iattr *attr)
  127. {
  128. unsigned int ia_valid = attr->ia_valid;
  129. if (ia_valid & ATTR_UID)
  130. inode->i_uid = attr->ia_uid;
  131. if (ia_valid & ATTR_GID)
  132. inode->i_gid = attr->ia_gid;
  133. if (ia_valid & ATTR_ATIME)
  134. inode->i_atime = timespec_trunc(attr->ia_atime,
  135. inode->i_sb->s_time_gran);
  136. if (ia_valid & ATTR_MTIME)
  137. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  138. inode->i_sb->s_time_gran);
  139. if (ia_valid & ATTR_CTIME)
  140. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  141. inode->i_sb->s_time_gran);
  142. if (ia_valid & ATTR_MODE) {
  143. umode_t mode = attr->ia_mode;
  144. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  145. mode &= ~S_ISGID;
  146. inode->i_mode = mode;
  147. }
  148. }
  149. EXPORT_SYMBOL(setattr_copy);
  150. int notify_change(struct dentry * dentry, struct iattr * attr)
  151. {
  152. struct inode *inode = dentry->d_inode;
  153. umode_t mode = inode->i_mode;
  154. int error;
  155. struct timespec now;
  156. unsigned int ia_valid = attr->ia_valid;
  157. WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
  158. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  159. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  160. return -EPERM;
  161. }
  162. if ((ia_valid & ATTR_SIZE) && IS_I_VERSION(inode)) {
  163. if (attr->ia_size != inode->i_size)
  164. inode_inc_iversion(inode);
  165. }
  166. if ((ia_valid & ATTR_MODE)) {
  167. umode_t amode = attr->ia_mode;
  168. /* Flag setting protected by i_mutex */
  169. if (is_sxid(amode))
  170. inode->i_flags &= ~S_NOSEC;
  171. }
  172. now = current_fs_time(inode->i_sb);
  173. attr->ia_ctime = now;
  174. if (!(ia_valid & ATTR_ATIME_SET))
  175. attr->ia_atime = now;
  176. if (!(ia_valid & ATTR_MTIME_SET))
  177. attr->ia_mtime = now;
  178. if (ia_valid & ATTR_KILL_PRIV) {
  179. attr->ia_valid &= ~ATTR_KILL_PRIV;
  180. ia_valid &= ~ATTR_KILL_PRIV;
  181. error = security_inode_need_killpriv(dentry);
  182. if (error > 0)
  183. error = security_inode_killpriv(dentry);
  184. if (error)
  185. return error;
  186. }
  187. /*
  188. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  189. * that the function has the ability to reinterpret a mode change
  190. * that's due to these bits. This adds an implicit restriction that
  191. * no function will ever call notify_change with both ATTR_MODE and
  192. * ATTR_KILL_S*ID set.
  193. */
  194. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  195. (ia_valid & ATTR_MODE))
  196. BUG();
  197. if (ia_valid & ATTR_KILL_SUID) {
  198. if (mode & S_ISUID) {
  199. ia_valid = attr->ia_valid |= ATTR_MODE;
  200. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  201. }
  202. }
  203. if (ia_valid & ATTR_KILL_SGID) {
  204. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  205. if (!(ia_valid & ATTR_MODE)) {
  206. ia_valid = attr->ia_valid |= ATTR_MODE;
  207. attr->ia_mode = inode->i_mode;
  208. }
  209. attr->ia_mode &= ~S_ISGID;
  210. }
  211. }
  212. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  213. return 0;
  214. error = security_inode_setattr(dentry, attr);
  215. if (error)
  216. return error;
  217. if (inode->i_op->setattr)
  218. error = inode->i_op->setattr(dentry, attr);
  219. else
  220. error = simple_setattr(dentry, attr);
  221. if (!error) {
  222. fsnotify_change(dentry, ia_valid);
  223. evm_inode_post_setattr(dentry, ia_valid);
  224. }
  225. return error;
  226. }
  227. EXPORT_SYMBOL(notify_change);