attr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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/filelock.h>
  17. #include <linux/security.h>
  18. /**
  19. * setattr_should_drop_sgid - determine whether the setgid bit needs to be
  20. * removed
  21. * @idmap: idmap of the mount @inode was found from
  22. * @inode: inode to check
  23. *
  24. * This function determines whether the setgid bit needs to be removed.
  25. * We retain backwards compatibility and require setgid bit to be removed
  26. * unconditionally if S_IXGRP is set. Otherwise we have the exact same
  27. * requirements as setattr_prepare() and setattr_copy().
  28. *
  29. * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
  30. */
  31. int setattr_should_drop_sgid(struct mnt_idmap *idmap,
  32. const struct inode *inode)
  33. {
  34. umode_t mode = inode->i_mode;
  35. if (!(mode & S_ISGID))
  36. return 0;
  37. if (mode & S_IXGRP)
  38. return ATTR_KILL_SGID;
  39. if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))
  40. return ATTR_KILL_SGID;
  41. return 0;
  42. }
  43. EXPORT_SYMBOL(setattr_should_drop_sgid);
  44. /**
  45. * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to
  46. * be dropped
  47. * @idmap: idmap of the mount @inode was found from
  48. * @inode: inode to check
  49. *
  50. * This function determines whether the set{g,u}id bits need to be removed.
  51. * If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the
  52. * setgid bit needs to be removed ATTR_KILL_SGID is returned. If both
  53. * set{g,u}id bits need to be removed the corresponding mask of both flags is
  54. * returned.
  55. *
  56. * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits
  57. * to remove, 0 otherwise.
  58. */
  59. int setattr_should_drop_suidgid(struct mnt_idmap *idmap,
  60. struct inode *inode)
  61. {
  62. umode_t mode = inode->i_mode;
  63. int kill = 0;
  64. /* suid always must be killed */
  65. if (unlikely(mode & S_ISUID))
  66. kill = ATTR_KILL_SUID;
  67. kill |= setattr_should_drop_sgid(idmap, inode);
  68. if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
  69. return kill;
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(setattr_should_drop_suidgid);
  73. /**
  74. * chown_ok - verify permissions to chown inode
  75. * @idmap: idmap of the mount @inode was found from
  76. * @inode: inode to check permissions on
  77. * @ia_vfsuid: uid to chown @inode to
  78. *
  79. * If the inode has been found through an idmapped mount the idmap of
  80. * the vfsmount must be passed through @idmap. This function will then
  81. * take care to map the inode according to @idmap before checking
  82. * permissions. On non-idmapped mounts or if permission checking is to be
  83. * performed on the raw inode simply pass @nop_mnt_idmap.
  84. */
  85. static bool chown_ok(struct mnt_idmap *idmap,
  86. const struct inode *inode, vfsuid_t ia_vfsuid)
  87. {
  88. vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
  89. if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
  90. vfsuid_eq(ia_vfsuid, vfsuid))
  91. return true;
  92. if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
  93. return true;
  94. if (!vfsuid_valid(vfsuid) &&
  95. ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
  96. return true;
  97. return false;
  98. }
  99. /**
  100. * chgrp_ok - verify permissions to chgrp inode
  101. * @idmap: idmap of the mount @inode was found from
  102. * @inode: inode to check permissions on
  103. * @ia_vfsgid: gid to chown @inode to
  104. *
  105. * If the inode has been found through an idmapped mount the idmap of
  106. * the vfsmount must be passed through @idmap. This function will then
  107. * take care to map the inode according to @idmap before checking
  108. * permissions. On non-idmapped mounts or if permission checking is to be
  109. * performed on the raw inode simply pass @nop_mnt_idmap.
  110. */
  111. static bool chgrp_ok(struct mnt_idmap *idmap,
  112. const struct inode *inode, vfsgid_t ia_vfsgid)
  113. {
  114. vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
  115. vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
  116. if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
  117. if (vfsgid_eq(ia_vfsgid, vfsgid))
  118. return true;
  119. if (vfsgid_in_group_p(ia_vfsgid))
  120. return true;
  121. }
  122. if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
  123. return true;
  124. if (!vfsgid_valid(vfsgid) &&
  125. ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
  126. return true;
  127. return false;
  128. }
  129. /**
  130. * setattr_prepare - check if attribute changes to a dentry are allowed
  131. * @idmap: idmap of the mount the inode was found from
  132. * @dentry: dentry to check
  133. * @attr: attributes to change
  134. *
  135. * Check if we are allowed to change the attributes contained in @attr
  136. * in the given dentry. This includes the normal unix access permission
  137. * checks, as well as checks for rlimits and others. The function also clears
  138. * SGID bit from mode if user is not allowed to set it. Also file capabilities
  139. * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
  140. *
  141. * If the inode has been found through an idmapped mount the idmap of
  142. * the vfsmount must be passed through @idmap. This function will then
  143. * take care to map the inode according to @idmap before checking
  144. * permissions. On non-idmapped mounts or if permission checking is to be
  145. * performed on the raw inode simply pass @nop_mnt_idmap.
  146. *
  147. * Should be called as the first thing in ->setattr implementations,
  148. * possibly after taking additional locks.
  149. */
  150. int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,
  151. struct iattr *attr)
  152. {
  153. struct inode *inode = d_inode(dentry);
  154. unsigned int ia_valid = attr->ia_valid;
  155. /*
  156. * First check size constraints. These can't be overriden using
  157. * ATTR_FORCE.
  158. */
  159. if (ia_valid & ATTR_SIZE) {
  160. int error;
  161. /*
  162. * Verity files are immutable, so deny truncates. This isn't
  163. * covered by the open-time check because sys_truncate() takes a
  164. * path, not an open file.
  165. */
  166. if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
  167. return -EPERM;
  168. error = inode_newsize_ok(inode, attr->ia_size);
  169. if (error)
  170. return error;
  171. }
  172. /* If force is set do it anyway. */
  173. if (ia_valid & ATTR_FORCE)
  174. goto kill_priv;
  175. /* Make sure a caller can chown. */
  176. if ((ia_valid & ATTR_UID) &&
  177. !chown_ok(idmap, inode, attr->ia_vfsuid))
  178. return -EPERM;
  179. /* Make sure caller can chgrp. */
  180. if ((ia_valid & ATTR_GID) &&
  181. !chgrp_ok(idmap, inode, attr->ia_vfsgid))
  182. return -EPERM;
  183. /* Make sure a caller can chmod. */
  184. if (ia_valid & ATTR_MODE) {
  185. vfsgid_t vfsgid;
  186. if (!inode_owner_or_capable(idmap, inode))
  187. return -EPERM;
  188. if (ia_valid & ATTR_GID)
  189. vfsgid = attr->ia_vfsgid;
  190. else
  191. vfsgid = i_gid_into_vfsgid(idmap, inode);
  192. /* Also check the setgid bit! */
  193. if (!in_group_or_capable(idmap, inode, vfsgid))
  194. attr->ia_mode &= ~S_ISGID;
  195. }
  196. /* Check for setting the inode time. */
  197. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  198. if (!inode_owner_or_capable(idmap, inode))
  199. return -EPERM;
  200. }
  201. kill_priv:
  202. /* User has permission for the change */
  203. if (ia_valid & ATTR_KILL_PRIV) {
  204. int error;
  205. error = security_inode_killpriv(idmap, dentry);
  206. if (error)
  207. return error;
  208. }
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(setattr_prepare);
  212. /**
  213. * inode_newsize_ok - may this inode be truncated to a given size
  214. * @inode: the inode to be truncated
  215. * @offset: the new size to assign to the inode
  216. *
  217. * inode_newsize_ok must be called with i_rwsem held exclusively.
  218. *
  219. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  220. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  221. * when necessary. Caller must not proceed with inode size change if failure is
  222. * returned. @inode must be a file (not directory), with appropriate
  223. * permissions to allow truncate (inode_newsize_ok does NOT check these
  224. * conditions).
  225. *
  226. * Return: 0 on success, -ve errno on failure
  227. */
  228. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  229. {
  230. if (offset < 0)
  231. return -EINVAL;
  232. if (inode->i_size < offset) {
  233. unsigned long limit;
  234. limit = rlimit(RLIMIT_FSIZE);
  235. if (limit != RLIM_INFINITY && offset > limit)
  236. goto out_sig;
  237. if (offset > inode->i_sb->s_maxbytes)
  238. goto out_big;
  239. } else {
  240. /*
  241. * truncation of in-use swapfiles is disallowed - it would
  242. * cause subsequent swapout to scribble on the now-freed
  243. * blocks.
  244. */
  245. if (IS_SWAPFILE(inode))
  246. return -ETXTBSY;
  247. }
  248. return 0;
  249. out_sig:
  250. send_sig(SIGXFSZ, current, 0);
  251. out_big:
  252. return -EFBIG;
  253. }
  254. EXPORT_SYMBOL(inode_newsize_ok);
  255. /**
  256. * setattr_copy_mgtime - update timestamps for mgtime inodes
  257. * @inode: inode timestamps to be updated
  258. * @attr: attrs for the update
  259. *
  260. * With multigrain timestamps, take more care to prevent races when
  261. * updating the ctime. Always update the ctime to the very latest using
  262. * the standard mechanism, and use that to populate the atime and mtime
  263. * appropriately (unless those are being set to specific values).
  264. */
  265. static void setattr_copy_mgtime(struct inode *inode, const struct iattr *attr)
  266. {
  267. unsigned int ia_valid = attr->ia_valid;
  268. struct timespec64 now;
  269. if (ia_valid & ATTR_CTIME_SET)
  270. now = inode_set_ctime_deleg(inode, attr->ia_ctime);
  271. else if (ia_valid & ATTR_CTIME)
  272. now = inode_set_ctime_current(inode);
  273. else
  274. now = current_time(inode);
  275. if (ia_valid & ATTR_ATIME_SET)
  276. inode_set_atime_to_ts(inode, attr->ia_atime);
  277. else if (ia_valid & ATTR_ATIME)
  278. inode_set_atime_to_ts(inode, now);
  279. if (ia_valid & ATTR_MTIME_SET)
  280. inode_set_mtime_to_ts(inode, attr->ia_mtime);
  281. else if (ia_valid & ATTR_MTIME)
  282. inode_set_mtime_to_ts(inode, now);
  283. }
  284. /**
  285. * setattr_copy - copy simple metadata updates into the generic inode
  286. * @idmap: idmap of the mount the inode was found from
  287. * @inode: the inode to be updated
  288. * @attr: the new attributes
  289. *
  290. * setattr_copy must be called with i_rwsem held exclusively.
  291. *
  292. * setattr_copy updates the inode's metadata with that specified
  293. * in attr on idmapped mounts. Necessary permission checks to determine
  294. * whether or not the S_ISGID property needs to be removed are performed with
  295. * the correct idmapped mount permission helpers.
  296. * Noticeably missing is inode size update, which is more complex
  297. * as it requires pagecache updates.
  298. *
  299. * If the inode has been found through an idmapped mount the idmap of
  300. * the vfsmount must be passed through @idmap. This function will then
  301. * take care to map the inode according to @idmap before checking
  302. * permissions. On non-idmapped mounts or if permission checking is to be
  303. * performed on the raw inode simply pass @nop_mnt_idmap.
  304. *
  305. * The inode is not marked as dirty after this operation. The rationale is
  306. * that for "simple" filesystems, the struct inode is the inode storage.
  307. * The caller is free to mark the inode dirty afterwards if needed.
  308. */
  309. void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,
  310. const struct iattr *attr)
  311. {
  312. unsigned int ia_valid = attr->ia_valid;
  313. i_uid_update(idmap, attr, inode);
  314. i_gid_update(idmap, attr, inode);
  315. if (ia_valid & ATTR_MODE) {
  316. umode_t mode = attr->ia_mode;
  317. if (!in_group_or_capable(idmap, inode,
  318. i_gid_into_vfsgid(idmap, inode)))
  319. mode &= ~S_ISGID;
  320. inode->i_mode = mode;
  321. }
  322. if (is_mgtime(inode))
  323. return setattr_copy_mgtime(inode, attr);
  324. if (ia_valid & ATTR_ATIME)
  325. inode_set_atime_to_ts(inode, attr->ia_atime);
  326. if (ia_valid & ATTR_MTIME)
  327. inode_set_mtime_to_ts(inode, attr->ia_mtime);
  328. if (ia_valid & ATTR_CTIME_SET)
  329. inode_set_ctime_deleg(inode, attr->ia_ctime);
  330. else if (ia_valid & ATTR_CTIME)
  331. inode_set_ctime_to_ts(inode, attr->ia_ctime);
  332. }
  333. EXPORT_SYMBOL(setattr_copy);
  334. int may_setattr(struct mnt_idmap *idmap, struct inode *inode,
  335. unsigned int ia_valid)
  336. {
  337. int error;
  338. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  339. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  340. return -EPERM;
  341. }
  342. /*
  343. * If utimes(2) and friends are called with times == NULL (or both
  344. * times are UTIME_NOW), then we need to check for write permission
  345. */
  346. if (ia_valid & ATTR_TOUCH) {
  347. if (IS_IMMUTABLE(inode))
  348. return -EPERM;
  349. if (!inode_owner_or_capable(idmap, inode)) {
  350. error = inode_permission(idmap, inode, MAY_WRITE);
  351. if (error)
  352. return error;
  353. }
  354. }
  355. return 0;
  356. }
  357. EXPORT_SYMBOL(may_setattr);
  358. /**
  359. * notify_change - modify attributes of a filesystem object
  360. * @idmap: idmap of the mount the inode was found from
  361. * @dentry: object affected
  362. * @attr: new attributes
  363. * @delegated_inode: returns inode, if the inode is delegated
  364. *
  365. * The caller must hold the i_rwsem exclusively on the affected object.
  366. *
  367. * If notify_change discovers a delegation in need of breaking,
  368. * it will return -EWOULDBLOCK and return a reference to the inode in
  369. * delegated_inode. The caller should then break the delegation and
  370. * retry. Because breaking a delegation may take a long time, the
  371. * caller should drop the i_rwsem before doing so.
  372. *
  373. * Alternatively, a caller may pass NULL for delegated_inode. This may
  374. * be appropriate for callers that expect the underlying filesystem not
  375. * to be NFS exported. Also, passing NULL is fine for callers holding
  376. * the file open for write, as there can be no conflicting delegation in
  377. * that case.
  378. *
  379. * If the inode has been found through an idmapped mount the idmap of
  380. * the vfsmount must be passed through @idmap. This function will then
  381. * take care to map the inode according to @idmap before checking
  382. * permissions. On non-idmapped mounts or if permission checking is to be
  383. * performed on the raw inode simply pass @nop_mnt_idmap.
  384. */
  385. int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,
  386. struct iattr *attr, struct delegated_inode *delegated_inode)
  387. {
  388. struct inode *inode = dentry->d_inode;
  389. umode_t mode = inode->i_mode;
  390. int error;
  391. struct timespec64 now;
  392. unsigned int ia_valid = attr->ia_valid;
  393. WARN_ON_ONCE(!inode_is_locked(inode));
  394. error = may_setattr(idmap, inode, ia_valid);
  395. if (error)
  396. return error;
  397. if ((ia_valid & ATTR_MODE)) {
  398. /*
  399. * Don't allow changing the mode of symlinks:
  400. *
  401. * (1) The vfs doesn't take the mode of symlinks into account
  402. * during permission checking.
  403. * (2) This has never worked correctly. Most major filesystems
  404. * did return EOPNOTSUPP due to interactions with POSIX ACLs
  405. * but did still updated the mode of the symlink.
  406. * This inconsistency led system call wrapper providers such
  407. * as libc to block changing the mode of symlinks with
  408. * EOPNOTSUPP already.
  409. * (3) To even do this in the first place one would have to use
  410. * specific file descriptors and quite some effort.
  411. */
  412. if (S_ISLNK(inode->i_mode))
  413. return -EOPNOTSUPP;
  414. /* Flag setting protected by i_rwsem */
  415. if (is_sxid(attr->ia_mode))
  416. inode->i_flags &= ~S_NOSEC;
  417. }
  418. now = current_time(inode);
  419. if (ia_valid & ATTR_ATIME_SET)
  420. attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
  421. else
  422. attr->ia_atime = now;
  423. if (ia_valid & ATTR_CTIME_SET)
  424. attr->ia_ctime = timestamp_truncate(attr->ia_ctime, inode);
  425. else
  426. attr->ia_ctime = now;
  427. if (ia_valid & ATTR_MTIME_SET)
  428. attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
  429. else
  430. attr->ia_mtime = now;
  431. if (ia_valid & ATTR_KILL_PRIV) {
  432. error = security_inode_need_killpriv(dentry);
  433. if (error < 0)
  434. return error;
  435. if (error == 0)
  436. ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
  437. }
  438. /*
  439. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  440. * that the function has the ability to reinterpret a mode change
  441. * that's due to these bits. This adds an implicit restriction that
  442. * no function will ever call notify_change with both ATTR_MODE and
  443. * ATTR_KILL_S*ID set.
  444. */
  445. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  446. (ia_valid & ATTR_MODE))
  447. BUG();
  448. if (ia_valid & ATTR_KILL_SUID) {
  449. if (mode & S_ISUID) {
  450. ia_valid = attr->ia_valid |= ATTR_MODE;
  451. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  452. }
  453. }
  454. if (ia_valid & ATTR_KILL_SGID) {
  455. if (mode & S_ISGID) {
  456. if (!(ia_valid & ATTR_MODE)) {
  457. ia_valid = attr->ia_valid |= ATTR_MODE;
  458. attr->ia_mode = inode->i_mode;
  459. }
  460. attr->ia_mode &= ~S_ISGID;
  461. }
  462. }
  463. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  464. return 0;
  465. /*
  466. * Verify that uid/gid changes are valid in the target
  467. * namespace of the superblock.
  468. */
  469. if (ia_valid & ATTR_UID &&
  470. !vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
  471. attr->ia_vfsuid))
  472. return -EOVERFLOW;
  473. if (ia_valid & ATTR_GID &&
  474. !vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
  475. attr->ia_vfsgid))
  476. return -EOVERFLOW;
  477. /* Don't allow modifications of files with invalid uids or
  478. * gids unless those uids & gids are being made valid.
  479. */
  480. if (!(ia_valid & ATTR_UID) &&
  481. !vfsuid_valid(i_uid_into_vfsuid(idmap, inode)))
  482. return -EOVERFLOW;
  483. if (!(ia_valid & ATTR_GID) &&
  484. !vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))
  485. return -EOVERFLOW;
  486. error = security_inode_setattr(idmap, dentry, attr);
  487. if (error)
  488. return error;
  489. /*
  490. * If ATTR_DELEG is set, then these attributes are being set on
  491. * behalf of the holder of a write delegation. We want to avoid
  492. * breaking the delegation in this case.
  493. */
  494. if (!(ia_valid & ATTR_DELEG)) {
  495. error = try_break_deleg(inode, delegated_inode);
  496. if (error)
  497. return error;
  498. }
  499. if (inode->i_op->setattr)
  500. error = inode->i_op->setattr(idmap, dentry, attr);
  501. else
  502. error = simple_setattr(idmap, dentry, attr);
  503. if (!error) {
  504. fsnotify_change(dentry, ia_valid);
  505. security_inode_post_setattr(idmap, dentry, ia_valid);
  506. }
  507. return error;
  508. }
  509. EXPORT_SYMBOL(notify_change);