attr.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /*
  187. * If utimes(2) and friends are called with times == NULL (or both
  188. * times are UTIME_NOW), then we need to check for write permission
  189. */
  190. if (ia_valid & ATTR_TOUCH) {
  191. if (IS_IMMUTABLE(inode))
  192. return -EPERM;
  193. if (!inode_owner_or_capable(inode)) {
  194. error = inode_permission(inode, MAY_WRITE);
  195. if (error)
  196. return error;
  197. }
  198. }
  199. if ((ia_valid & ATTR_MODE)) {
  200. umode_t amode = attr->ia_mode;
  201. /* Flag setting protected by i_mutex */
  202. if (is_sxid(amode))
  203. inode->i_flags &= ~S_NOSEC;
  204. }
  205. now = current_fs_time(inode->i_sb);
  206. attr->ia_ctime = now;
  207. if (!(ia_valid & ATTR_ATIME_SET))
  208. attr->ia_atime = now;
  209. if (!(ia_valid & ATTR_MTIME_SET))
  210. attr->ia_mtime = now;
  211. if (ia_valid & ATTR_KILL_PRIV) {
  212. attr->ia_valid &= ~ATTR_KILL_PRIV;
  213. ia_valid &= ~ATTR_KILL_PRIV;
  214. error = security_inode_need_killpriv(dentry);
  215. if (error > 0)
  216. error = security_inode_killpriv(dentry);
  217. if (error)
  218. return error;
  219. }
  220. /*
  221. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  222. * that the function has the ability to reinterpret a mode change
  223. * that's due to these bits. This adds an implicit restriction that
  224. * no function will ever call notify_change with both ATTR_MODE and
  225. * ATTR_KILL_S*ID set.
  226. */
  227. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  228. (ia_valid & ATTR_MODE))
  229. BUG();
  230. if (ia_valid & ATTR_KILL_SUID) {
  231. if (mode & S_ISUID) {
  232. ia_valid = attr->ia_valid |= ATTR_MODE;
  233. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  234. }
  235. }
  236. if (ia_valid & ATTR_KILL_SGID) {
  237. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  238. if (!(ia_valid & ATTR_MODE)) {
  239. ia_valid = attr->ia_valid |= ATTR_MODE;
  240. attr->ia_mode = inode->i_mode;
  241. }
  242. attr->ia_mode &= ~S_ISGID;
  243. }
  244. }
  245. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  246. return 0;
  247. error = security_inode_setattr(dentry, attr);
  248. if (error)
  249. return error;
  250. error = try_break_deleg(inode, delegated_inode);
  251. if (error)
  252. return error;
  253. if (inode->i_op->setattr)
  254. error = inode->i_op->setattr(dentry, attr);
  255. else
  256. error = simple_setattr(dentry, attr);
  257. if (!error) {
  258. fsnotify_change(dentry, ia_valid);
  259. ima_inode_post_setattr(dentry);
  260. evm_inode_post_setattr(dentry, ia_valid);
  261. }
  262. return error;
  263. }
  264. EXPORT_SYMBOL(notify_change);