attr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * linux/fs/attr.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * changes by Thomas Schoebel-Theuer
  6. */
  7. #include <linux/module.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. /* Taken over from the old code... */
  16. /* POSIX UID/GID verification for setting inode attributes. */
  17. int inode_change_ok(const struct inode *inode, struct iattr *attr)
  18. {
  19. int retval = -EPERM;
  20. unsigned int ia_valid = attr->ia_valid;
  21. /* If force is set do it anyway. */
  22. if (ia_valid & ATTR_FORCE)
  23. goto fine;
  24. /* Make sure a caller can chown. */
  25. if ((ia_valid & ATTR_UID) &&
  26. (current_fsuid() != inode->i_uid ||
  27. attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
  28. goto error;
  29. /* Make sure caller can chgrp. */
  30. if ((ia_valid & ATTR_GID) &&
  31. (current_fsuid() != inode->i_uid ||
  32. (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
  33. !capable(CAP_CHOWN))
  34. goto error;
  35. /* Make sure a caller can chmod. */
  36. if (ia_valid & ATTR_MODE) {
  37. if (!is_owner_or_cap(inode))
  38. goto error;
  39. /* Also check the setgid bit! */
  40. if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
  41. inode->i_gid) && !capable(CAP_FSETID))
  42. attr->ia_mode &= ~S_ISGID;
  43. }
  44. /* Check for setting the inode time. */
  45. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  46. if (!is_owner_or_cap(inode))
  47. goto error;
  48. }
  49. fine:
  50. retval = 0;
  51. error:
  52. return retval;
  53. }
  54. EXPORT_SYMBOL(inode_change_ok);
  55. /**
  56. * inode_newsize_ok - may this inode be truncated to a given size
  57. * @inode: the inode to be truncated
  58. * @offset: the new size to assign to the inode
  59. * @Returns: 0 on success, -ve errno on failure
  60. *
  61. * inode_newsize_ok must be called with i_mutex held.
  62. *
  63. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  64. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  65. * when necessary. Caller must not proceed with inode size change if failure is
  66. * returned. @inode must be a file (not directory), with appropriate
  67. * permissions to allow truncate (inode_newsize_ok does NOT check these
  68. * conditions).
  69. */
  70. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  71. {
  72. if (inode->i_size < offset) {
  73. unsigned long limit;
  74. limit = rlimit(RLIMIT_FSIZE);
  75. if (limit != RLIM_INFINITY && offset > limit)
  76. goto out_sig;
  77. if (offset > inode->i_sb->s_maxbytes)
  78. goto out_big;
  79. } else {
  80. /*
  81. * truncation of in-use swapfiles is disallowed - it would
  82. * cause subsequent swapout to scribble on the now-freed
  83. * blocks.
  84. */
  85. if (IS_SWAPFILE(inode))
  86. return -ETXTBSY;
  87. }
  88. return 0;
  89. out_sig:
  90. send_sig(SIGXFSZ, current, 0);
  91. out_big:
  92. return -EFBIG;
  93. }
  94. EXPORT_SYMBOL(inode_newsize_ok);
  95. /**
  96. * generic_setattr - copy simple metadata updates into the generic inode
  97. * @inode: the inode to be updated
  98. * @attr: the new attributes
  99. *
  100. * generic_setattr must be called with i_mutex held.
  101. *
  102. * generic_setattr updates the inode's metadata with that specified
  103. * in attr. Noticably missing is inode size update, which is more complex
  104. * as it requires pagecache updates. See simple_setsize.
  105. *
  106. * The inode is not marked as dirty after this operation. The rationale is
  107. * that for "simple" filesystems, the struct inode is the inode storage.
  108. * The caller is free to mark the inode dirty afterwards if needed.
  109. */
  110. void generic_setattr(struct inode *inode, const struct iattr *attr)
  111. {
  112. unsigned int ia_valid = attr->ia_valid;
  113. if (ia_valid & ATTR_UID)
  114. inode->i_uid = attr->ia_uid;
  115. if (ia_valid & ATTR_GID)
  116. inode->i_gid = attr->ia_gid;
  117. if (ia_valid & ATTR_ATIME)
  118. inode->i_atime = timespec_trunc(attr->ia_atime,
  119. inode->i_sb->s_time_gran);
  120. if (ia_valid & ATTR_MTIME)
  121. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  122. inode->i_sb->s_time_gran);
  123. if (ia_valid & ATTR_CTIME)
  124. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  125. inode->i_sb->s_time_gran);
  126. if (ia_valid & ATTR_MODE) {
  127. umode_t mode = attr->ia_mode;
  128. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  129. mode &= ~S_ISGID;
  130. inode->i_mode = mode;
  131. }
  132. }
  133. EXPORT_SYMBOL(generic_setattr);
  134. /*
  135. * note this function is deprecated, the new truncate sequence should be
  136. * used instead -- see eg. simple_setsize, generic_setattr.
  137. */
  138. int inode_setattr(struct inode *inode, const struct iattr *attr)
  139. {
  140. unsigned int ia_valid = attr->ia_valid;
  141. if (ia_valid & ATTR_SIZE &&
  142. attr->ia_size != i_size_read(inode)) {
  143. int error;
  144. error = vmtruncate(inode, attr->ia_size);
  145. if (error)
  146. return error;
  147. }
  148. generic_setattr(inode, attr);
  149. mark_inode_dirty(inode);
  150. return 0;
  151. }
  152. EXPORT_SYMBOL(inode_setattr);
  153. int notify_change(struct dentry * dentry, struct iattr * attr)
  154. {
  155. struct inode *inode = dentry->d_inode;
  156. mode_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. now = current_fs_time(inode->i_sb);
  165. attr->ia_ctime = now;
  166. if (!(ia_valid & ATTR_ATIME_SET))
  167. attr->ia_atime = now;
  168. if (!(ia_valid & ATTR_MTIME_SET))
  169. attr->ia_mtime = now;
  170. if (ia_valid & ATTR_KILL_PRIV) {
  171. attr->ia_valid &= ~ATTR_KILL_PRIV;
  172. ia_valid &= ~ATTR_KILL_PRIV;
  173. error = security_inode_need_killpriv(dentry);
  174. if (error > 0)
  175. error = security_inode_killpriv(dentry);
  176. if (error)
  177. return error;
  178. }
  179. /*
  180. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  181. * that the function has the ability to reinterpret a mode change
  182. * that's due to these bits. This adds an implicit restriction that
  183. * no function will ever call notify_change with both ATTR_MODE and
  184. * ATTR_KILL_S*ID set.
  185. */
  186. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  187. (ia_valid & ATTR_MODE))
  188. BUG();
  189. if (ia_valid & ATTR_KILL_SUID) {
  190. if (mode & S_ISUID) {
  191. ia_valid = attr->ia_valid |= ATTR_MODE;
  192. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  193. }
  194. }
  195. if (ia_valid & ATTR_KILL_SGID) {
  196. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  197. if (!(ia_valid & ATTR_MODE)) {
  198. ia_valid = attr->ia_valid |= ATTR_MODE;
  199. attr->ia_mode = inode->i_mode;
  200. }
  201. attr->ia_mode &= ~S_ISGID;
  202. }
  203. }
  204. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  205. return 0;
  206. error = security_inode_setattr(dentry, attr);
  207. if (error)
  208. return error;
  209. if (ia_valid & ATTR_SIZE)
  210. down_write(&dentry->d_inode->i_alloc_sem);
  211. if (inode->i_op && inode->i_op->setattr) {
  212. error = inode->i_op->setattr(dentry, attr);
  213. } else {
  214. error = inode_change_ok(inode, attr);
  215. if (!error)
  216. error = inode_setattr(inode, attr);
  217. }
  218. if (ia_valid & ATTR_SIZE)
  219. up_write(&dentry->d_inode->i_alloc_sem);
  220. if (!error)
  221. fsnotify_change(dentry, ia_valid);
  222. return error;
  223. }
  224. EXPORT_SYMBOL(notify_change);