attr.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 will check filesystem limits and ulimits to check that the
  62. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  63. * when necessary. Caller must not proceed with inode size change if failure is
  64. * returned. @inode must be a file (not directory), with appropriate
  65. * permissions to allow truncate (inode_newsize_ok does NOT check these
  66. * conditions).
  67. *
  68. * inode_newsize_ok must be called with i_mutex held.
  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. int inode_setattr(struct inode * inode, struct iattr * attr)
  96. {
  97. unsigned int ia_valid = attr->ia_valid;
  98. if (ia_valid & ATTR_SIZE &&
  99. attr->ia_size != i_size_read(inode)) {
  100. int error = vmtruncate(inode, attr->ia_size);
  101. if (error)
  102. return error;
  103. }
  104. if (ia_valid & ATTR_UID)
  105. inode->i_uid = attr->ia_uid;
  106. if (ia_valid & ATTR_GID)
  107. inode->i_gid = attr->ia_gid;
  108. if (ia_valid & ATTR_ATIME)
  109. inode->i_atime = timespec_trunc(attr->ia_atime,
  110. inode->i_sb->s_time_gran);
  111. if (ia_valid & ATTR_MTIME)
  112. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  113. inode->i_sb->s_time_gran);
  114. if (ia_valid & ATTR_CTIME)
  115. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  116. inode->i_sb->s_time_gran);
  117. if (ia_valid & ATTR_MODE) {
  118. umode_t mode = attr->ia_mode;
  119. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  120. mode &= ~S_ISGID;
  121. inode->i_mode = mode;
  122. }
  123. mark_inode_dirty(inode);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(inode_setattr);
  127. int notify_change(struct dentry * dentry, struct iattr * attr)
  128. {
  129. struct inode *inode = dentry->d_inode;
  130. mode_t mode = inode->i_mode;
  131. int error;
  132. struct timespec now;
  133. unsigned int ia_valid = attr->ia_valid;
  134. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  135. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  136. return -EPERM;
  137. }
  138. now = current_fs_time(inode->i_sb);
  139. attr->ia_ctime = now;
  140. if (!(ia_valid & ATTR_ATIME_SET))
  141. attr->ia_atime = now;
  142. if (!(ia_valid & ATTR_MTIME_SET))
  143. attr->ia_mtime = now;
  144. if (ia_valid & ATTR_KILL_PRIV) {
  145. attr->ia_valid &= ~ATTR_KILL_PRIV;
  146. ia_valid &= ~ATTR_KILL_PRIV;
  147. error = security_inode_need_killpriv(dentry);
  148. if (error > 0)
  149. error = security_inode_killpriv(dentry);
  150. if (error)
  151. return error;
  152. }
  153. /*
  154. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  155. * that the function has the ability to reinterpret a mode change
  156. * that's due to these bits. This adds an implicit restriction that
  157. * no function will ever call notify_change with both ATTR_MODE and
  158. * ATTR_KILL_S*ID set.
  159. */
  160. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  161. (ia_valid & ATTR_MODE))
  162. BUG();
  163. if (ia_valid & ATTR_KILL_SUID) {
  164. if (mode & S_ISUID) {
  165. ia_valid = attr->ia_valid |= ATTR_MODE;
  166. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  167. }
  168. }
  169. if (ia_valid & ATTR_KILL_SGID) {
  170. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  171. if (!(ia_valid & ATTR_MODE)) {
  172. ia_valid = attr->ia_valid |= ATTR_MODE;
  173. attr->ia_mode = inode->i_mode;
  174. }
  175. attr->ia_mode &= ~S_ISGID;
  176. }
  177. }
  178. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  179. return 0;
  180. error = security_inode_setattr(dentry, attr);
  181. if (error)
  182. return error;
  183. if (ia_valid & ATTR_SIZE)
  184. down_write(&dentry->d_inode->i_alloc_sem);
  185. if (inode->i_op && inode->i_op->setattr) {
  186. error = inode->i_op->setattr(dentry, attr);
  187. } else {
  188. error = inode_change_ok(inode, attr);
  189. if (!error)
  190. error = inode_setattr(inode, attr);
  191. }
  192. if (ia_valid & ATTR_SIZE)
  193. up_write(&dentry->d_inode->i_alloc_sem);
  194. if (!error)
  195. fsnotify_change(dentry, ia_valid);
  196. return error;
  197. }
  198. EXPORT_SYMBOL(notify_change);