attr.c 16 KB

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