attr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/quotaops.h>
  15. #include <linux/security.h>
  16. /* Taken over from the old code... */
  17. /* POSIX UID/GID verification for setting inode attributes. */
  18. int inode_change_ok(const struct inode *inode, struct iattr *attr)
  19. {
  20. int retval = -EPERM;
  21. unsigned int ia_valid = attr->ia_valid;
  22. /* If force is set do it anyway. */
  23. if (ia_valid & ATTR_FORCE)
  24. goto fine;
  25. /* Make sure a caller can chown. */
  26. if ((ia_valid & ATTR_UID) &&
  27. (current_fsuid() != inode->i_uid ||
  28. attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
  29. goto error;
  30. /* Make sure caller can chgrp. */
  31. if ((ia_valid & ATTR_GID) &&
  32. (current_fsuid() != inode->i_uid ||
  33. (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
  34. !capable(CAP_CHOWN))
  35. goto error;
  36. /* Make sure a caller can chmod. */
  37. if (ia_valid & ATTR_MODE) {
  38. if (!is_owner_or_cap(inode))
  39. goto error;
  40. /* Also check the setgid bit! */
  41. if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
  42. inode->i_gid) && !capable(CAP_FSETID))
  43. attr->ia_mode &= ~S_ISGID;
  44. }
  45. /* Check for setting the inode time. */
  46. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  47. if (!is_owner_or_cap(inode))
  48. goto error;
  49. }
  50. fine:
  51. retval = 0;
  52. error:
  53. return retval;
  54. }
  55. EXPORT_SYMBOL(inode_change_ok);
  56. /**
  57. * inode_newsize_ok - may this inode be truncated to a given size
  58. * @inode: the inode to be truncated
  59. * @offset: the new size to assign to the inode
  60. * @Returns: 0 on success, -ve errno on failure
  61. *
  62. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  63. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  64. * when necessary. Caller must not proceed with inode size change if failure is
  65. * returned. @inode must be a file (not directory), with appropriate
  66. * permissions to allow truncate (inode_newsize_ok does NOT check these
  67. * conditions).
  68. *
  69. * inode_newsize_ok must be called with i_mutex held.
  70. */
  71. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  72. {
  73. if (inode->i_size < offset) {
  74. unsigned long limit;
  75. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  76. if (limit != RLIM_INFINITY && offset > limit)
  77. goto out_sig;
  78. if (offset > inode->i_sb->s_maxbytes)
  79. goto out_big;
  80. } else {
  81. /*
  82. * truncation of in-use swapfiles is disallowed - it would
  83. * cause subsequent swapout to scribble on the now-freed
  84. * blocks.
  85. */
  86. if (IS_SWAPFILE(inode))
  87. return -ETXTBSY;
  88. }
  89. return 0;
  90. out_sig:
  91. send_sig(SIGXFSZ, current, 0);
  92. out_big:
  93. return -EFBIG;
  94. }
  95. EXPORT_SYMBOL(inode_newsize_ok);
  96. int inode_setattr(struct inode * inode, struct iattr * attr)
  97. {
  98. unsigned int ia_valid = attr->ia_valid;
  99. if (ia_valid & ATTR_SIZE &&
  100. attr->ia_size != i_size_read(inode)) {
  101. int error = vmtruncate(inode, attr->ia_size);
  102. if (error)
  103. return error;
  104. }
  105. if (ia_valid & ATTR_UID)
  106. inode->i_uid = attr->ia_uid;
  107. if (ia_valid & ATTR_GID)
  108. inode->i_gid = attr->ia_gid;
  109. if (ia_valid & ATTR_ATIME)
  110. inode->i_atime = timespec_trunc(attr->ia_atime,
  111. inode->i_sb->s_time_gran);
  112. if (ia_valid & ATTR_MTIME)
  113. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  114. inode->i_sb->s_time_gran);
  115. if (ia_valid & ATTR_CTIME)
  116. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  117. inode->i_sb->s_time_gran);
  118. if (ia_valid & ATTR_MODE) {
  119. umode_t mode = attr->ia_mode;
  120. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  121. mode &= ~S_ISGID;
  122. inode->i_mode = mode;
  123. }
  124. mark_inode_dirty(inode);
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(inode_setattr);
  128. int notify_change(struct dentry * dentry, struct iattr * attr)
  129. {
  130. struct inode *inode = dentry->d_inode;
  131. mode_t mode = inode->i_mode;
  132. int error;
  133. struct timespec now;
  134. unsigned int ia_valid = attr->ia_valid;
  135. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  136. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  137. return -EPERM;
  138. }
  139. now = current_fs_time(inode->i_sb);
  140. attr->ia_ctime = now;
  141. if (!(ia_valid & ATTR_ATIME_SET))
  142. attr->ia_atime = now;
  143. if (!(ia_valid & ATTR_MTIME_SET))
  144. attr->ia_mtime = now;
  145. if (ia_valid & ATTR_KILL_PRIV) {
  146. attr->ia_valid &= ~ATTR_KILL_PRIV;
  147. ia_valid &= ~ATTR_KILL_PRIV;
  148. error = security_inode_need_killpriv(dentry);
  149. if (error > 0)
  150. error = security_inode_killpriv(dentry);
  151. if (error)
  152. return error;
  153. }
  154. /*
  155. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  156. * that the function has the ability to reinterpret a mode change
  157. * that's due to these bits. This adds an implicit restriction that
  158. * no function will ever call notify_change with both ATTR_MODE and
  159. * ATTR_KILL_S*ID set.
  160. */
  161. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  162. (ia_valid & ATTR_MODE))
  163. BUG();
  164. if (ia_valid & ATTR_KILL_SUID) {
  165. if (mode & S_ISUID) {
  166. ia_valid = attr->ia_valid |= ATTR_MODE;
  167. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  168. }
  169. }
  170. if (ia_valid & ATTR_KILL_SGID) {
  171. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  172. if (!(ia_valid & ATTR_MODE)) {
  173. ia_valid = attr->ia_valid |= ATTR_MODE;
  174. attr->ia_mode = inode->i_mode;
  175. }
  176. attr->ia_mode &= ~S_ISGID;
  177. }
  178. }
  179. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  180. return 0;
  181. error = security_inode_setattr(dentry, attr);
  182. if (error)
  183. return error;
  184. if (ia_valid & ATTR_SIZE)
  185. down_write(&dentry->d_inode->i_alloc_sem);
  186. if (inode->i_op && inode->i_op->setattr) {
  187. error = inode->i_op->setattr(dentry, attr);
  188. } else {
  189. error = inode_change_ok(inode, attr);
  190. if (!error) {
  191. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  192. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
  193. error = vfs_dq_transfer(inode, attr) ?
  194. -EDQUOT : 0;
  195. if (!error)
  196. error = inode_setattr(inode, attr);
  197. }
  198. }
  199. if (ia_valid & ATTR_SIZE)
  200. up_write(&dentry->d_inode->i_alloc_sem);
  201. if (!error)
  202. fsnotify_change(dentry, ia_valid);
  203. return error;
  204. }
  205. EXPORT_SYMBOL(notify_change);