attr.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_NS(setattr_prepare, ANDROID_GKI_VFS_EXPORT_ONLY);
  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_NS(inode_newsize_ok, ANDROID_GKI_VFS_EXPORT_ONLY);
  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 = attr->ia_atime;
  170. if (ia_valid & ATTR_MTIME)
  171. inode->i_mtime = attr->ia_mtime;
  172. if (ia_valid & ATTR_CTIME)
  173. inode->i_ctime = attr->ia_ctime;
  174. if (ia_valid & ATTR_MODE) {
  175. umode_t mode = attr->ia_mode;
  176. if (!in_group_p(inode->i_gid) &&
  177. !capable_wrt_inode_uidgid(inode, CAP_FSETID))
  178. mode &= ~S_ISGID;
  179. inode->i_mode = mode;
  180. }
  181. }
  182. EXPORT_SYMBOL(setattr_copy);
  183. /**
  184. * notify_change - modify attributes of a filesytem object
  185. * @dentry: object affected
  186. * @attr: new attributes
  187. * @delegated_inode: returns inode, if the inode is delegated
  188. *
  189. * The caller must hold the i_mutex on the affected object.
  190. *
  191. * If notify_change discovers a delegation in need of breaking,
  192. * it will return -EWOULDBLOCK and return a reference to the inode in
  193. * delegated_inode. The caller should then break the delegation and
  194. * retry. Because breaking a delegation may take a long time, the
  195. * caller should drop the i_mutex before doing so.
  196. *
  197. * Alternatively, a caller may pass NULL for delegated_inode. This may
  198. * be appropriate for callers that expect the underlying filesystem not
  199. * to be NFS exported. Also, passing NULL is fine for callers holding
  200. * the file open for write, as there can be no conflicting delegation in
  201. * that case.
  202. */
  203. int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
  204. {
  205. struct inode *inode = dentry->d_inode;
  206. umode_t mode = inode->i_mode;
  207. int error;
  208. struct timespec64 now;
  209. unsigned int ia_valid = attr->ia_valid;
  210. WARN_ON_ONCE(!inode_is_locked(inode));
  211. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  212. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  213. return -EPERM;
  214. }
  215. /*
  216. * If utimes(2) and friends are called with times == NULL (or both
  217. * times are UTIME_NOW), then we need to check for write permission
  218. */
  219. if (ia_valid & ATTR_TOUCH) {
  220. if (IS_IMMUTABLE(inode))
  221. return -EPERM;
  222. if (!inode_owner_or_capable(inode)) {
  223. error = inode_permission(inode, MAY_WRITE);
  224. if (error)
  225. return error;
  226. }
  227. }
  228. if ((ia_valid & ATTR_MODE)) {
  229. umode_t amode = attr->ia_mode;
  230. /* Flag setting protected by i_mutex */
  231. if (is_sxid(amode))
  232. inode->i_flags &= ~S_NOSEC;
  233. }
  234. now = current_time(inode);
  235. attr->ia_ctime = now;
  236. if (!(ia_valid & ATTR_ATIME_SET))
  237. attr->ia_atime = now;
  238. else
  239. attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
  240. if (!(ia_valid & ATTR_MTIME_SET))
  241. attr->ia_mtime = now;
  242. else
  243. attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
  244. if (ia_valid & ATTR_KILL_PRIV) {
  245. error = security_inode_need_killpriv(dentry);
  246. if (error < 0)
  247. return error;
  248. if (error == 0)
  249. ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
  250. }
  251. /*
  252. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  253. * that the function has the ability to reinterpret a mode change
  254. * that's due to these bits. This adds an implicit restriction that
  255. * no function will ever call notify_change with both ATTR_MODE and
  256. * ATTR_KILL_S*ID set.
  257. */
  258. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  259. (ia_valid & ATTR_MODE))
  260. BUG();
  261. if (ia_valid & ATTR_KILL_SUID) {
  262. if (mode & S_ISUID) {
  263. ia_valid = attr->ia_valid |= ATTR_MODE;
  264. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  265. }
  266. }
  267. if (ia_valid & ATTR_KILL_SGID) {
  268. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  269. if (!(ia_valid & ATTR_MODE)) {
  270. ia_valid = attr->ia_valid |= ATTR_MODE;
  271. attr->ia_mode = inode->i_mode;
  272. }
  273. attr->ia_mode &= ~S_ISGID;
  274. }
  275. }
  276. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  277. return 0;
  278. /*
  279. * Verify that uid/gid changes are valid in the target
  280. * namespace of the superblock.
  281. */
  282. if (ia_valid & ATTR_UID &&
  283. !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
  284. return -EOVERFLOW;
  285. if (ia_valid & ATTR_GID &&
  286. !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
  287. return -EOVERFLOW;
  288. /* Don't allow modifications of files with invalid uids or
  289. * gids unless those uids & gids are being made valid.
  290. */
  291. if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
  292. return -EOVERFLOW;
  293. if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
  294. return -EOVERFLOW;
  295. error = security_inode_setattr(dentry, attr);
  296. if (error)
  297. return error;
  298. error = try_break_deleg(inode, delegated_inode);
  299. if (error)
  300. return error;
  301. if (inode->i_op->setattr)
  302. error = inode->i_op->setattr(dentry, attr);
  303. else
  304. error = simple_setattr(dentry, attr);
  305. if (!error) {
  306. fsnotify_change(dentry, ia_valid);
  307. ima_inode_post_setattr(dentry);
  308. evm_inode_post_setattr(dentry, ia_valid);
  309. }
  310. return error;
  311. }
  312. EXPORT_SYMBOL_NS(notify_change, ANDROID_GKI_VFS_EXPORT_ONLY);