attr.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include <linux/ima.h>
  17. /**
  18. * setattr_prepare - check if attribute changes to a dentry are allowed
  19. * @dentry: dentry to check
  20. * @attr: attributes to change
  21. *
  22. * Check if we are allowed to change the attributes contained in @attr
  23. * in the given dentry. This includes the normal unix access permission
  24. * checks, as well as checks for rlimits and others. The function also clears
  25. * SGID bit from mode if user is not allowed to set it. Also file capabilities
  26. * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
  27. *
  28. * Should be called as the first thing in ->setattr implementations,
  29. * possibly after taking additional locks.
  30. */
  31. int setattr_prepare(struct dentry *dentry, struct iattr *attr)
  32. {
  33. struct inode *inode = d_inode(dentry);
  34. unsigned int ia_valid = attr->ia_valid;
  35. /*
  36. * First check size constraints. These can't be overriden using
  37. * ATTR_FORCE.
  38. */
  39. if (ia_valid & ATTR_SIZE) {
  40. int error = inode_newsize_ok(inode, attr->ia_size);
  41. if (error)
  42. return error;
  43. }
  44. /* If force is set do it anyway. */
  45. if (ia_valid & ATTR_FORCE)
  46. goto kill_priv;
  47. /* Make sure a caller can chown. */
  48. if ((ia_valid & ATTR_UID) &&
  49. (!uid_eq(current_fsuid(), inode->i_uid) ||
  50. !uid_eq(attr->ia_uid, inode->i_uid)) &&
  51. !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
  52. return -EPERM;
  53. /* Make sure caller can chgrp. */
  54. if ((ia_valid & ATTR_GID) &&
  55. (!uid_eq(current_fsuid(), inode->i_uid) ||
  56. (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
  57. !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
  58. return -EPERM;
  59. /* Make sure a caller can chmod. */
  60. if (ia_valid & ATTR_MODE) {
  61. if (!inode_owner_or_capable(inode))
  62. return -EPERM;
  63. /* Also check the setgid bit! */
  64. if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
  65. inode->i_gid) &&
  66. !capable_wrt_inode_uidgid(inode, CAP_FSETID))
  67. attr->ia_mode &= ~S_ISGID;
  68. }
  69. /* Check for setting the inode time. */
  70. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  71. if (!inode_owner_or_capable(inode))
  72. return -EPERM;
  73. }
  74. kill_priv:
  75. /* User has permission for the change */
  76. if (ia_valid & ATTR_KILL_PRIV) {
  77. int error;
  78. error = security_inode_killpriv(dentry);
  79. if (error)
  80. return error;
  81. }
  82. return 0;
  83. }
  84. EXPORT_SYMBOL(setattr_prepare);
  85. /**
  86. * inode_newsize_ok - may this inode be truncated to a given size
  87. * @inode: the inode to be truncated
  88. * @offset: the new size to assign to the inode
  89. * @Returns: 0 on success, -ve errno on failure
  90. *
  91. * inode_newsize_ok must be called with i_mutex held.
  92. *
  93. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  94. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  95. * when necessary. Caller must not proceed with inode size change if failure is
  96. * returned. @inode must be a file (not directory), with appropriate
  97. * permissions to allow truncate (inode_newsize_ok does NOT check these
  98. * conditions).
  99. */
  100. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  101. {
  102. if (inode->i_size < offset) {
  103. unsigned long limit;
  104. limit = rlimit(RLIMIT_FSIZE);
  105. if (limit != RLIM_INFINITY && offset > limit)
  106. goto out_sig;
  107. if (offset > inode->i_sb->s_maxbytes)
  108. goto out_big;
  109. } else {
  110. /*
  111. * truncation of in-use swapfiles is disallowed - it would
  112. * cause subsequent swapout to scribble on the now-freed
  113. * blocks.
  114. */
  115. if (IS_SWAPFILE(inode))
  116. return -ETXTBSY;
  117. }
  118. return 0;
  119. out_sig:
  120. send_sig(SIGXFSZ, current, 0);
  121. out_big:
  122. return -EFBIG;
  123. }
  124. EXPORT_SYMBOL(inode_newsize_ok);
  125. /**
  126. * setattr_copy - copy simple metadata updates into the generic inode
  127. * @inode: the inode to be updated
  128. * @attr: the new attributes
  129. *
  130. * setattr_copy must be called with i_mutex held.
  131. *
  132. * setattr_copy updates the inode's metadata with that specified
  133. * in attr. Noticeably missing is inode size update, which is more complex
  134. * as it requires pagecache updates.
  135. *
  136. * The inode is not marked as dirty after this operation. The rationale is
  137. * that for "simple" filesystems, the struct inode is the inode storage.
  138. * The caller is free to mark the inode dirty afterwards if needed.
  139. */
  140. void setattr_copy(struct inode *inode, const struct iattr *attr)
  141. {
  142. unsigned int ia_valid = attr->ia_valid;
  143. if (ia_valid & ATTR_UID)
  144. inode->i_uid = attr->ia_uid;
  145. if (ia_valid & ATTR_GID)
  146. inode->i_gid = attr->ia_gid;
  147. if (ia_valid & ATTR_ATIME)
  148. inode->i_atime = timespec_trunc(attr->ia_atime,
  149. inode->i_sb->s_time_gran);
  150. if (ia_valid & ATTR_MTIME)
  151. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  152. inode->i_sb->s_time_gran);
  153. if (ia_valid & ATTR_CTIME)
  154. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  155. inode->i_sb->s_time_gran);
  156. if (ia_valid & ATTR_MODE) {
  157. umode_t mode = attr->ia_mode;
  158. if (!in_group_p(inode->i_gid) &&
  159. !capable_wrt_inode_uidgid(inode, CAP_FSETID))
  160. mode &= ~S_ISGID;
  161. inode->i_mode = mode;
  162. }
  163. }
  164. EXPORT_SYMBOL(setattr_copy);
  165. /**
  166. * notify_change - modify attributes of a filesytem object
  167. * @dentry: object affected
  168. * @iattr: new attributes
  169. * @delegated_inode: returns inode, if the inode is delegated
  170. *
  171. * The caller must hold the i_mutex on the affected object.
  172. *
  173. * If notify_change discovers a delegation in need of breaking,
  174. * it will return -EWOULDBLOCK and return a reference to the inode in
  175. * delegated_inode. The caller should then break the delegation and
  176. * retry. Because breaking a delegation may take a long time, the
  177. * caller should drop the i_mutex before doing so.
  178. *
  179. * Alternatively, a caller may pass NULL for delegated_inode. This may
  180. * be appropriate for callers that expect the underlying filesystem not
  181. * to be NFS exported. Also, passing NULL is fine for callers holding
  182. * the file open for write, as there can be no conflicting delegation in
  183. * that case.
  184. */
  185. int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
  186. {
  187. struct inode *inode = dentry->d_inode;
  188. umode_t mode = inode->i_mode;
  189. int error;
  190. struct timespec now;
  191. unsigned int ia_valid = attr->ia_valid;
  192. WARN_ON_ONCE(!inode_is_locked(inode));
  193. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  194. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  195. return -EPERM;
  196. }
  197. /*
  198. * If utimes(2) and friends are called with times == NULL (or both
  199. * times are UTIME_NOW), then we need to check for write permission
  200. */
  201. if (ia_valid & ATTR_TOUCH) {
  202. if (IS_IMMUTABLE(inode))
  203. return -EPERM;
  204. if (!inode_owner_or_capable(inode)) {
  205. error = inode_permission(inode, MAY_WRITE);
  206. if (error)
  207. return error;
  208. }
  209. }
  210. if ((ia_valid & ATTR_MODE)) {
  211. umode_t amode = attr->ia_mode;
  212. /* Flag setting protected by i_mutex */
  213. if (is_sxid(amode))
  214. inode->i_flags &= ~S_NOSEC;
  215. }
  216. now = current_time(inode);
  217. attr->ia_ctime = now;
  218. if (!(ia_valid & ATTR_ATIME_SET))
  219. attr->ia_atime = now;
  220. if (!(ia_valid & ATTR_MTIME_SET))
  221. attr->ia_mtime = now;
  222. if (ia_valid & ATTR_KILL_PRIV) {
  223. error = security_inode_need_killpriv(dentry);
  224. if (error < 0)
  225. return error;
  226. if (error == 0)
  227. ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
  228. }
  229. /*
  230. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  231. * that the function has the ability to reinterpret a mode change
  232. * that's due to these bits. This adds an implicit restriction that
  233. * no function will ever call notify_change with both ATTR_MODE and
  234. * ATTR_KILL_S*ID set.
  235. */
  236. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  237. (ia_valid & ATTR_MODE))
  238. BUG();
  239. if (ia_valid & ATTR_KILL_SUID) {
  240. if (mode & S_ISUID) {
  241. ia_valid = attr->ia_valid |= ATTR_MODE;
  242. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  243. }
  244. }
  245. if (ia_valid & ATTR_KILL_SGID) {
  246. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  247. if (!(ia_valid & ATTR_MODE)) {
  248. ia_valid = attr->ia_valid |= ATTR_MODE;
  249. attr->ia_mode = inode->i_mode;
  250. }
  251. attr->ia_mode &= ~S_ISGID;
  252. }
  253. }
  254. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  255. return 0;
  256. /*
  257. * Verify that uid/gid changes are valid in the target
  258. * namespace of the superblock.
  259. */
  260. if (ia_valid & ATTR_UID &&
  261. !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
  262. return -EOVERFLOW;
  263. if (ia_valid & ATTR_GID &&
  264. !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
  265. return -EOVERFLOW;
  266. /* Don't allow modifications of files with invalid uids or
  267. * gids unless those uids & gids are being made valid.
  268. */
  269. if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
  270. return -EOVERFLOW;
  271. if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
  272. return -EOVERFLOW;
  273. error = security_inode_setattr(dentry, attr);
  274. if (error)
  275. return error;
  276. error = try_break_deleg(inode, delegated_inode);
  277. if (error)
  278. return error;
  279. if (inode->i_op->setattr)
  280. error = inode->i_op->setattr(dentry, attr);
  281. else
  282. error = simple_setattr(dentry, attr);
  283. if (!error) {
  284. fsnotify_change(dentry, ia_valid);
  285. ima_inode_post_setattr(dentry);
  286. evm_inode_post_setattr(dentry, ia_valid);
  287. }
  288. return error;
  289. }
  290. EXPORT_SYMBOL(notify_change);