attr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. (current_fsuid() != inode->i_uid ||
  46. 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. (current_fsuid() != inode->i_uid ||
  51. (!in_group_p(attr->ia_gid) && 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. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  158. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  159. return -EPERM;
  160. }
  161. if ((ia_valid & ATTR_MODE)) {
  162. umode_t amode = attr->ia_mode;
  163. /* Flag setting protected by i_mutex */
  164. if (is_sxid(amode))
  165. inode->i_flags &= ~S_NOSEC;
  166. }
  167. now = current_fs_time(inode->i_sb);
  168. attr->ia_ctime = now;
  169. if (!(ia_valid & ATTR_ATIME_SET))
  170. attr->ia_atime = now;
  171. if (!(ia_valid & ATTR_MTIME_SET))
  172. attr->ia_mtime = now;
  173. if (ia_valid & ATTR_KILL_PRIV) {
  174. attr->ia_valid &= ~ATTR_KILL_PRIV;
  175. ia_valid &= ~ATTR_KILL_PRIV;
  176. error = security_inode_need_killpriv(dentry);
  177. if (error > 0)
  178. error = security_inode_killpriv(dentry);
  179. if (error)
  180. return error;
  181. }
  182. /*
  183. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  184. * that the function has the ability to reinterpret a mode change
  185. * that's due to these bits. This adds an implicit restriction that
  186. * no function will ever call notify_change with both ATTR_MODE and
  187. * ATTR_KILL_S*ID set.
  188. */
  189. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  190. (ia_valid & ATTR_MODE))
  191. BUG();
  192. if (ia_valid & ATTR_KILL_SUID) {
  193. if (mode & S_ISUID) {
  194. ia_valid = attr->ia_valid |= ATTR_MODE;
  195. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  196. }
  197. }
  198. if (ia_valid & ATTR_KILL_SGID) {
  199. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  200. if (!(ia_valid & ATTR_MODE)) {
  201. ia_valid = attr->ia_valid |= ATTR_MODE;
  202. attr->ia_mode = inode->i_mode;
  203. }
  204. attr->ia_mode &= ~S_ISGID;
  205. }
  206. }
  207. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  208. return 0;
  209. error = security_inode_setattr(dentry, attr);
  210. if (error)
  211. return error;
  212. if (inode->i_op->setattr)
  213. error = inode->i_op->setattr(dentry, attr);
  214. else
  215. error = simple_setattr(dentry, attr);
  216. if (!error) {
  217. fsnotify_change(dentry, ia_valid);
  218. evm_inode_post_setattr(dentry, ia_valid);
  219. }
  220. return error;
  221. }
  222. EXPORT_SYMBOL(notify_change);