attr.c 9.7 KB

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