attr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 = inode_newsize_ok(inode, attr->ia_size);
  161. if (error)
  162. return error;
  163. }
  164. /* If force is set do it anyway. */
  165. if (ia_valid & ATTR_FORCE)
  166. goto kill_priv;
  167. /* Make sure a caller can chown. */
  168. if ((ia_valid & ATTR_UID) &&
  169. !chown_ok(idmap, inode, attr->ia_vfsuid))
  170. return -EPERM;
  171. /* Make sure caller can chgrp. */
  172. if ((ia_valid & ATTR_GID) &&
  173. !chgrp_ok(idmap, inode, attr->ia_vfsgid))
  174. return -EPERM;
  175. /* Make sure a caller can chmod. */
  176. if (ia_valid & ATTR_MODE) {
  177. vfsgid_t vfsgid;
  178. if (!inode_owner_or_capable(idmap, inode))
  179. return -EPERM;
  180. if (ia_valid & ATTR_GID)
  181. vfsgid = attr->ia_vfsgid;
  182. else
  183. vfsgid = i_gid_into_vfsgid(idmap, inode);
  184. /* Also check the setgid bit! */
  185. if (!in_group_or_capable(idmap, inode, vfsgid))
  186. attr->ia_mode &= ~S_ISGID;
  187. }
  188. /* Check for setting the inode time. */
  189. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  190. if (!inode_owner_or_capable(idmap, inode))
  191. return -EPERM;
  192. }
  193. kill_priv:
  194. /* User has permission for the change */
  195. if (ia_valid & ATTR_KILL_PRIV) {
  196. int error;
  197. error = security_inode_killpriv(idmap, dentry);
  198. if (error)
  199. return error;
  200. }
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(setattr_prepare);
  204. /**
  205. * inode_newsize_ok - may this inode be truncated to a given size
  206. * @inode: the inode to be truncated
  207. * @offset: the new size to assign to the inode
  208. *
  209. * inode_newsize_ok must be called with i_mutex held.
  210. *
  211. * inode_newsize_ok will check filesystem limits and ulimits to check that the
  212. * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  213. * when necessary. Caller must not proceed with inode size change if failure is
  214. * returned. @inode must be a file (not directory), with appropriate
  215. * permissions to allow truncate (inode_newsize_ok does NOT check these
  216. * conditions).
  217. *
  218. * Return: 0 on success, -ve errno on failure
  219. */
  220. int inode_newsize_ok(const struct inode *inode, loff_t offset)
  221. {
  222. if (offset < 0)
  223. return -EINVAL;
  224. if (inode->i_size < offset) {
  225. unsigned long limit;
  226. limit = rlimit(RLIMIT_FSIZE);
  227. if (limit != RLIM_INFINITY && offset > limit)
  228. goto out_sig;
  229. if (offset > inode->i_sb->s_maxbytes)
  230. goto out_big;
  231. } else {
  232. /*
  233. * truncation of in-use swapfiles is disallowed - it would
  234. * cause subsequent swapout to scribble on the now-freed
  235. * blocks.
  236. */
  237. if (IS_SWAPFILE(inode))
  238. return -ETXTBSY;
  239. }
  240. return 0;
  241. out_sig:
  242. send_sig(SIGXFSZ, current, 0);
  243. out_big:
  244. return -EFBIG;
  245. }
  246. EXPORT_SYMBOL(inode_newsize_ok);
  247. /**
  248. * setattr_copy - copy simple metadata updates into the generic inode
  249. * @idmap: idmap of the mount the inode was found from
  250. * @inode: the inode to be updated
  251. * @attr: the new attributes
  252. *
  253. * setattr_copy must be called with i_mutex held.
  254. *
  255. * setattr_copy updates the inode's metadata with that specified
  256. * in attr on idmapped mounts. Necessary permission checks to determine
  257. * whether or not the S_ISGID property needs to be removed are performed with
  258. * the correct idmapped mount permission helpers.
  259. * Noticeably missing is inode size update, which is more complex
  260. * as it requires pagecache updates.
  261. *
  262. * If the inode has been found through an idmapped mount the idmap of
  263. * the vfsmount must be passed through @idmap. This function will then
  264. * take care to map the inode according to @idmap before checking
  265. * permissions. On non-idmapped mounts or if permission checking is to be
  266. * performed on the raw inode simply pass @nop_mnt_idmap.
  267. *
  268. * The inode is not marked as dirty after this operation. The rationale is
  269. * that for "simple" filesystems, the struct inode is the inode storage.
  270. * The caller is free to mark the inode dirty afterwards if needed.
  271. */
  272. void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,
  273. const struct iattr *attr)
  274. {
  275. unsigned int ia_valid = attr->ia_valid;
  276. i_uid_update(idmap, attr, inode);
  277. i_gid_update(idmap, attr, inode);
  278. if (ia_valid & ATTR_ATIME)
  279. inode_set_atime_to_ts(inode, attr->ia_atime);
  280. if (ia_valid & ATTR_MTIME)
  281. inode_set_mtime_to_ts(inode, attr->ia_mtime);
  282. if (ia_valid & ATTR_CTIME)
  283. inode_set_ctime_to_ts(inode, attr->ia_ctime);
  284. if (ia_valid & ATTR_MODE) {
  285. umode_t mode = attr->ia_mode;
  286. if (!in_group_or_capable(idmap, inode,
  287. i_gid_into_vfsgid(idmap, inode)))
  288. mode &= ~S_ISGID;
  289. inode->i_mode = mode;
  290. }
  291. }
  292. EXPORT_SYMBOL(setattr_copy);
  293. int may_setattr(struct mnt_idmap *idmap, struct inode *inode,
  294. unsigned int ia_valid)
  295. {
  296. int error;
  297. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
  298. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  299. return -EPERM;
  300. }
  301. /*
  302. * If utimes(2) and friends are called with times == NULL (or both
  303. * times are UTIME_NOW), then we need to check for write permission
  304. */
  305. if (ia_valid & ATTR_TOUCH) {
  306. if (IS_IMMUTABLE(inode))
  307. return -EPERM;
  308. if (!inode_owner_or_capable(idmap, inode)) {
  309. error = inode_permission(idmap, inode, MAY_WRITE);
  310. if (error)
  311. return error;
  312. }
  313. }
  314. return 0;
  315. }
  316. EXPORT_SYMBOL(may_setattr);
  317. /**
  318. * notify_change - modify attributes of a filesystem object
  319. * @idmap: idmap of the mount the inode was found from
  320. * @dentry: object affected
  321. * @attr: new attributes
  322. * @delegated_inode: returns inode, if the inode is delegated
  323. *
  324. * The caller must hold the i_mutex on the affected object.
  325. *
  326. * If notify_change discovers a delegation in need of breaking,
  327. * it will return -EWOULDBLOCK and return a reference to the inode in
  328. * delegated_inode. The caller should then break the delegation and
  329. * retry. Because breaking a delegation may take a long time, the
  330. * caller should drop the i_mutex before doing so.
  331. *
  332. * Alternatively, a caller may pass NULL for delegated_inode. This may
  333. * be appropriate for callers that expect the underlying filesystem not
  334. * to be NFS exported. Also, passing NULL is fine for callers holding
  335. * the file open for write, as there can be no conflicting delegation in
  336. * that case.
  337. *
  338. * If the inode has been found through an idmapped mount the idmap of
  339. * the vfsmount must be passed through @idmap. This function will then
  340. * take care to map the inode according to @idmap before checking
  341. * permissions. On non-idmapped mounts or if permission checking is to be
  342. * performed on the raw inode simply pass @nop_mnt_idmap.
  343. */
  344. int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,
  345. struct iattr *attr, struct inode **delegated_inode)
  346. {
  347. struct inode *inode = dentry->d_inode;
  348. umode_t mode = inode->i_mode;
  349. int error;
  350. struct timespec64 now;
  351. unsigned int ia_valid = attr->ia_valid;
  352. WARN_ON_ONCE(!inode_is_locked(inode));
  353. error = may_setattr(idmap, inode, ia_valid);
  354. if (error)
  355. return error;
  356. if ((ia_valid & ATTR_MODE)) {
  357. /*
  358. * Don't allow changing the mode of symlinks:
  359. *
  360. * (1) The vfs doesn't take the mode of symlinks into account
  361. * during permission checking.
  362. * (2) This has never worked correctly. Most major filesystems
  363. * did return EOPNOTSUPP due to interactions with POSIX ACLs
  364. * but did still updated the mode of the symlink.
  365. * This inconsistency led system call wrapper providers such
  366. * as libc to block changing the mode of symlinks with
  367. * EOPNOTSUPP already.
  368. * (3) To even do this in the first place one would have to use
  369. * specific file descriptors and quite some effort.
  370. */
  371. if (S_ISLNK(inode->i_mode))
  372. return -EOPNOTSUPP;
  373. /* Flag setting protected by i_mutex */
  374. if (is_sxid(attr->ia_mode))
  375. inode->i_flags &= ~S_NOSEC;
  376. }
  377. now = current_time(inode);
  378. attr->ia_ctime = now;
  379. if (!(ia_valid & ATTR_ATIME_SET))
  380. attr->ia_atime = now;
  381. else
  382. attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
  383. if (!(ia_valid & ATTR_MTIME_SET))
  384. attr->ia_mtime = now;
  385. else
  386. attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
  387. if (ia_valid & ATTR_KILL_PRIV) {
  388. error = security_inode_need_killpriv(dentry);
  389. if (error < 0)
  390. return error;
  391. if (error == 0)
  392. ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
  393. }
  394. /*
  395. * We now pass ATTR_KILL_S*ID to the lower level setattr function so
  396. * that the function has the ability to reinterpret a mode change
  397. * that's due to these bits. This adds an implicit restriction that
  398. * no function will ever call notify_change with both ATTR_MODE and
  399. * ATTR_KILL_S*ID set.
  400. */
  401. if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
  402. (ia_valid & ATTR_MODE))
  403. BUG();
  404. if (ia_valid & ATTR_KILL_SUID) {
  405. if (mode & S_ISUID) {
  406. ia_valid = attr->ia_valid |= ATTR_MODE;
  407. attr->ia_mode = (inode->i_mode & ~S_ISUID);
  408. }
  409. }
  410. if (ia_valid & ATTR_KILL_SGID) {
  411. if (mode & S_ISGID) {
  412. if (!(ia_valid & ATTR_MODE)) {
  413. ia_valid = attr->ia_valid |= ATTR_MODE;
  414. attr->ia_mode = inode->i_mode;
  415. }
  416. attr->ia_mode &= ~S_ISGID;
  417. }
  418. }
  419. if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
  420. return 0;
  421. /*
  422. * Verify that uid/gid changes are valid in the target
  423. * namespace of the superblock.
  424. */
  425. if (ia_valid & ATTR_UID &&
  426. !vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
  427. attr->ia_vfsuid))
  428. return -EOVERFLOW;
  429. if (ia_valid & ATTR_GID &&
  430. !vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
  431. attr->ia_vfsgid))
  432. return -EOVERFLOW;
  433. /* Don't allow modifications of files with invalid uids or
  434. * gids unless those uids & gids are being made valid.
  435. */
  436. if (!(ia_valid & ATTR_UID) &&
  437. !vfsuid_valid(i_uid_into_vfsuid(idmap, inode)))
  438. return -EOVERFLOW;
  439. if (!(ia_valid & ATTR_GID) &&
  440. !vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))
  441. return -EOVERFLOW;
  442. error = security_inode_setattr(idmap, dentry, attr);
  443. if (error)
  444. return error;
  445. /*
  446. * If ATTR_DELEG is set, then these attributes are being set on
  447. * behalf of the holder of a write delegation. We want to avoid
  448. * breaking the delegation in this case.
  449. */
  450. if (!(ia_valid & ATTR_DELEG)) {
  451. error = try_break_deleg(inode, delegated_inode);
  452. if (error)
  453. return error;
  454. }
  455. if (inode->i_op->setattr)
  456. error = inode->i_op->setattr(idmap, dentry, attr);
  457. else
  458. error = simple_setattr(idmap, dentry, attr);
  459. if (!error) {
  460. fsnotify_change(dentry, ia_valid);
  461. security_inode_post_setattr(idmap, dentry, ia_valid);
  462. }
  463. return error;
  464. }
  465. EXPORT_SYMBOL(notify_change);