attr.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. * @Returns: 0 on success, -ve errno on failure
  110. *
  111. * inode_newsize_ok must be called with i_mutex held.
  112. *
  113. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  114. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  115. * when necessary. Caller must not proceed with inode size change if failure is
  116. * returned. @inode must be a file (not directory), with appropriate
  117. * permissions to allow truncate (inode_newsize_ok does NOT check these
  118. * conditions).
  119. */
  120. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  121. {
  122. if (inode->i_size < offset) {
  123. unsigned long limit;
  124. limit = rlimit(RLIMIT_FSIZE);
  125. if (limit != RLIM_INFINITY && offset > limit)
  126. goto out_sig;
  127. if (offset > inode->i_sb->s_maxbytes)
  128. goto out_big;
  129. } else {
  130. /*
  131. * truncation of in-use swapfiles is disallowed - it would
  132. * cause subsequent swapout to scribble on the now-freed
  133. * blocks.
  134. */
  135. if (IS_SWAPFILE(inode))
  136. return -ETXTBSY;
  137. }
  138. return 0;
  139. out_sig:
  140. send_sig(SIGXFSZ, current, 0);
  141. out_big:
  142. return -EFBIG;
  143. }
  144. EXPORT_SYMBOL(inode_newsize_ok);
  145. /**
  146. * setattr_copy - copy simple metadata updates into the generic inode
  147. * @inode: the inode to be updated
  148. * @attr: the new attributes
  149. *
  150. * setattr_copy must be called with i_mutex held.
  151. *
  152. * setattr_copy updates the inode's metadata with that specified
  153. * in attr. Noticeably missing is inode size update, which is more complex
  154. * as it requires pagecache updates.
  155. *
  156. * The inode is not marked as dirty after this operation. The rationale is
  157. * that for "simple" filesystems, the struct inode is the inode storage.
  158. * The caller is free to mark the inode dirty afterwards if needed.
  159. */
  160. void setattr_copy(struct inode *inode, const struct iattr *attr)
  161. {
  162. unsigned int ia_valid = attr->ia_valid;
  163. if (ia_valid & ATTR_UID)
  164. inode->i_uid = attr->ia_uid;
  165. if (ia_valid & ATTR_GID)
  166. inode->i_gid = attr->ia_gid;
  167. if (ia_valid & ATTR_ATIME)
  168. inode->i_atime = timespec64_trunc(attr->ia_atime,
  169. inode->i_sb->s_time_gran);
  170. if (ia_valid & ATTR_MTIME)
  171. inode->i_mtime = timespec64_trunc(attr->ia_mtime,
  172. inode->i_sb->s_time_gran);
  173. if (ia_valid & ATTR_CTIME)
  174. inode->i_ctime = timespec64_trunc(attr->ia_ctime,
  175. inode->i_sb->s_time_gran);
  176. if (ia_valid & ATTR_MODE) {
  177. umode_t mode = attr->ia_mode;
  178. if (!in_group_p(inode->i_gid) &&
  179. !capable_wrt_inode_uidgid(inode, CAP_FSETID))
  180. mode &= ~S_ISGID;
  181. inode->i_mode = mode;
  182. }
  183. }
  184. EXPORT_SYMBOL(setattr_copy);
  185. /**
  186. * notify_change - modify attributes of a filesytem object
  187. * @dentry: object affected
  188. * @iattr: new attributes
  189. * @delegated_inode: returns inode, if the inode is delegated
  190. *
  191. * The caller must hold the i_mutex on the affected object.
  192. *
  193. * If notify_change discovers a delegation in need of breaking,
  194. * it will return -EWOULDBLOCK and return a reference to the inode in
  195. * delegated_inode. The caller should then break the delegation and
  196. * retry. Because breaking a delegation may take a long time, the
  197. * caller should drop the i_mutex before doing so.
  198. *
  199. * Alternatively, a caller may pass NULL for delegated_inode. This may
  200. * be appropriate for callers that expect the underlying filesystem not
  201. * to be NFS exported. Also, passing NULL is fine for callers holding
  202. * the file open for write, as there can be no conflicting delegation in
  203. * that case.
  204. */
  205. int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
  206. {
  207. struct inode *inode = dentry->d_inode;
  208. umode_t mode = inode->i_mode;
  209. int error;
  210. struct timespec64 now;
  211. unsigned int ia_valid = attr->ia_valid;
  212. WARN_ON_ONCE(!inode_is_locked(inode));
  213. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  214. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  215. return -EPERM;
  216. }
  217. /*
  218. * If utimes(2) and friends are called with times == NULL (or both
  219. * times are UTIME_NOW), then we need to check for write permission
  220. */
  221. if (ia_valid & ATTR_TOUCH) {
  222. if (IS_IMMUTABLE(inode))
  223. return -EPERM;
  224. if (!inode_owner_or_capable(inode)) {
  225. error = inode_permission(inode, MAY_WRITE);
  226. if (error)
  227. return error;
  228. }
  229. }
  230. if ((ia_valid & ATTR_MODE)) {
  231. umode_t amode = attr->ia_mode;
  232. /* Flag setting protected by i_mutex */
  233. if (is_sxid(amode))
  234. inode->i_flags &= ~S_NOSEC;
  235. }
  236. now = current_time(inode);
  237. attr->ia_ctime = now;
  238. if (!(ia_valid & ATTR_ATIME_SET))
  239. attr->ia_atime = now;
  240. if (!(ia_valid & ATTR_MTIME_SET))
  241. attr->ia_mtime = now;
  242. if (ia_valid & ATTR_KILL_PRIV) {
  243. error = security_inode_need_killpriv(dentry);
  244. if (error < 0)
  245. return error;
  246. if (error == 0)
  247. ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
  248. }
  249. /*
  250. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  251. * that the function has the ability to reinterpret a mode change
  252. * that's due to these bits. This adds an implicit restriction that
  253. * no function will ever call notify_change with both ATTR_MODE and
  254. * ATTR_KILL_S*ID set.
  255. */
  256. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  257. (ia_valid & ATTR_MODE))
  258. BUG();
  259. if (ia_valid & ATTR_KILL_SUID) {
  260. if (mode & S_ISUID) {
  261. ia_valid = attr->ia_valid |= ATTR_MODE;
  262. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  263. }
  264. }
  265. if (ia_valid & ATTR_KILL_SGID) {
  266. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  267. if (!(ia_valid & ATTR_MODE)) {
  268. ia_valid = attr->ia_valid |= ATTR_MODE;
  269. attr->ia_mode = inode->i_mode;
  270. }
  271. attr->ia_mode &= ~S_ISGID;
  272. }
  273. }
  274. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  275. return 0;
  276. /*
  277. * Verify that uid/gid changes are valid in the target
  278. * namespace of the superblock.
  279. */
  280. if (ia_valid & ATTR_UID &&
  281. !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
  282. return -EOVERFLOW;
  283. if (ia_valid & ATTR_GID &&
  284. !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
  285. return -EOVERFLOW;
  286. /* Don't allow modifications of files with invalid uids or
  287. * gids unless those uids & gids are being made valid.
  288. */
  289. if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
  290. return -EOVERFLOW;
  291. if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
  292. return -EOVERFLOW;
  293. error = security_inode_setattr(dentry, attr);
  294. if (error)
  295. return error;
  296. error = try_break_deleg(inode, delegated_inode);
  297. if (error)
  298. return error;
  299. if (inode->i_op->setattr)
  300. error = inode->i_op->setattr(dentry, attr);
  301. else
  302. error = simple_setattr(dentry, attr);
  303. if (!error) {
  304. fsnotify_change(dentry, ia_valid);
  305. ima_inode_post_setattr(dentry);
  306. evm_inode_post_setattr(dentry, ia_valid);
  307. }
  308. return error;
  309. }
  310. EXPORT_SYMBOL(notify_change);