attr.c 7.9 KB

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