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