attr.c 6.8 KB

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