1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/attr.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds * changes by Thomas Schoebel-Theuer 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 9630d9c47SPaul Gortmaker #include <linux/export.h> 101da177e4SLinus Torvalds #include <linux/time.h> 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/string.h> 133f07c014SIngo Molnar #include <linux/sched/signal.h> 1416f7e0feSRandy Dunlap #include <linux/capability.h> 150eeca283SRobert Love #include <linux/fsnotify.h> 161da177e4SLinus Torvalds #include <linux/fcntl.h> 171da177e4SLinus Torvalds #include <linux/security.h> 18975d2943SMimi Zohar #include <linux/evm.h> 199957a504SMimi Zohar #include <linux/ima.h> 201da177e4SLinus Torvalds 2111c2a870SChristian Brauner #include "internal.h" 2211c2a870SChristian Brauner 2372ae017cSChristian Brauner /** 2472ae017cSChristian Brauner * setattr_should_drop_sgid - determine whether the setgid bit needs to be 2572ae017cSChristian Brauner * removed 269452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from 2772ae017cSChristian Brauner * @inode: inode to check 2872ae017cSChristian Brauner * 2972ae017cSChristian Brauner * This function determines whether the setgid bit needs to be removed. 3072ae017cSChristian Brauner * We retain backwards compatibility and require setgid bit to be removed 3172ae017cSChristian Brauner * unconditionally if S_IXGRP is set. Otherwise we have the exact same 3272ae017cSChristian Brauner * requirements as setattr_prepare() and setattr_copy(). 3372ae017cSChristian Brauner * 3472ae017cSChristian Brauner * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise. 3572ae017cSChristian Brauner */ 369452e93eSChristian Brauner int setattr_should_drop_sgid(struct mnt_idmap *idmap, 3772ae017cSChristian Brauner const struct inode *inode) 3872ae017cSChristian Brauner { 3972ae017cSChristian Brauner umode_t mode = inode->i_mode; 4072ae017cSChristian Brauner 4172ae017cSChristian Brauner if (!(mode & S_ISGID)) 4272ae017cSChristian Brauner return 0; 4372ae017cSChristian Brauner if (mode & S_IXGRP) 4472ae017cSChristian Brauner return ATTR_KILL_SGID; 45*e67fe633SChristian Brauner if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode))) 4672ae017cSChristian Brauner return ATTR_KILL_SGID; 4772ae017cSChristian Brauner return 0; 4872ae017cSChristian Brauner } 4972ae017cSChristian Brauner 50ed5a7047SChristian Brauner /** 51ed5a7047SChristian Brauner * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to 52ed5a7047SChristian Brauner * be dropped 539452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from 54ed5a7047SChristian Brauner * @inode: inode to check 55e243e3f9SChristian Brauner * 56ed5a7047SChristian Brauner * This function determines whether the set{g,u}id bits need to be removed. 57ed5a7047SChristian Brauner * If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the 58ed5a7047SChristian Brauner * setgid bit needs to be removed ATTR_KILL_SGID is returned. If both 59ed5a7047SChristian Brauner * set{g,u}id bits need to be removed the corresponding mask of both flags is 60ed5a7047SChristian Brauner * returned. 61ed5a7047SChristian Brauner * 62ed5a7047SChristian Brauner * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits 63ed5a7047SChristian Brauner * to remove, 0 otherwise. 64e243e3f9SChristian Brauner */ 659452e93eSChristian Brauner int setattr_should_drop_suidgid(struct mnt_idmap *idmap, 66ed5a7047SChristian Brauner struct inode *inode) 67e243e3f9SChristian Brauner { 68ed5a7047SChristian Brauner umode_t mode = inode->i_mode; 69e243e3f9SChristian Brauner int kill = 0; 70e243e3f9SChristian Brauner 71e243e3f9SChristian Brauner /* suid always must be killed */ 72e243e3f9SChristian Brauner if (unlikely(mode & S_ISUID)) 73e243e3f9SChristian Brauner kill = ATTR_KILL_SUID; 74e243e3f9SChristian Brauner 759452e93eSChristian Brauner kill |= setattr_should_drop_sgid(idmap, inode); 76e243e3f9SChristian Brauner 77e243e3f9SChristian Brauner if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode))) 78e243e3f9SChristian Brauner return kill; 79e243e3f9SChristian Brauner 80e243e3f9SChristian Brauner return 0; 81e243e3f9SChristian Brauner } 82ed5a7047SChristian Brauner EXPORT_SYMBOL(setattr_should_drop_suidgid); 83e243e3f9SChristian Brauner 842f221d6fSChristian Brauner /** 852f221d6fSChristian Brauner * chown_ok - verify permissions to chown inode 869452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from 872f221d6fSChristian Brauner * @inode: inode to check permissions on 8881a1807dSChristian Brauner * @ia_vfsuid: uid to chown @inode to 892f221d6fSChristian Brauner * 909452e93eSChristian Brauner * If the inode has been found through an idmapped mount the idmap of 919452e93eSChristian Brauner * the vfsmount must be passed through @idmap. This function will then 929452e93eSChristian Brauner * take care to map the inode according to @idmap before checking 932f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 949452e93eSChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap. 952f221d6fSChristian Brauner */ 969452e93eSChristian Brauner static bool chown_ok(struct mnt_idmap *idmap, 97b27c82e1SChristian Brauner const struct inode *inode, vfsuid_t ia_vfsuid) 980031181cSEric W. Biederman { 99*e67fe633SChristian Brauner vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 100b27c82e1SChristian Brauner if (vfsuid_eq_kuid(vfsuid, current_fsuid()) && 101b27c82e1SChristian Brauner vfsuid_eq(ia_vfsuid, vfsuid)) 1020031181cSEric W. Biederman return true; 1039452e93eSChristian Brauner if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN)) 1040031181cSEric W. Biederman return true; 105b27c82e1SChristian Brauner if (!vfsuid_valid(vfsuid) && 1060031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 1070031181cSEric W. Biederman return true; 1080031181cSEric W. Biederman return false; 1090031181cSEric W. Biederman } 1100031181cSEric W. Biederman 1112f221d6fSChristian Brauner /** 1122f221d6fSChristian Brauner * chgrp_ok - verify permissions to chgrp inode 1139452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from 1142f221d6fSChristian Brauner * @inode: inode to check permissions on 11581a1807dSChristian Brauner * @ia_vfsgid: gid to chown @inode to 1162f221d6fSChristian Brauner * 1179452e93eSChristian Brauner * If the inode has been found through an idmapped mount the idmap of 1189452e93eSChristian Brauner * the vfsmount must be passed through @idmap. This function will then 1199452e93eSChristian Brauner * take care to map the inode according to @idmap before checking 1202f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 1219452e93eSChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap. 1222f221d6fSChristian Brauner */ 1239452e93eSChristian Brauner static bool chgrp_ok(struct mnt_idmap *idmap, 124b27c82e1SChristian Brauner const struct inode *inode, vfsgid_t ia_vfsgid) 1250031181cSEric W. Biederman { 126*e67fe633SChristian Brauner vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 127*e67fe633SChristian Brauner vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 128b27c82e1SChristian Brauner if (vfsuid_eq_kuid(vfsuid, current_fsuid())) { 129b27c82e1SChristian Brauner if (vfsgid_eq(ia_vfsgid, vfsgid)) 1300031181cSEric W. Biederman return true; 131b27c82e1SChristian Brauner if (vfsgid_in_group_p(ia_vfsgid)) 132168f9128SChristian Brauner return true; 133168f9128SChristian Brauner } 1349452e93eSChristian Brauner if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN)) 1350031181cSEric W. Biederman return true; 136b27c82e1SChristian Brauner if (!vfsgid_valid(vfsgid) && 1370031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 1380031181cSEric W. Biederman return true; 1390031181cSEric W. Biederman return false; 1400031181cSEric W. Biederman } 1410031181cSEric W. Biederman 1422c27c65eSChristoph Hellwig /** 14331051c85SJan Kara * setattr_prepare - check if attribute changes to a dentry are allowed 144c1632a0fSChristian Brauner * @idmap: idmap of the mount the inode was found from 14531051c85SJan Kara * @dentry: dentry to check 1462c27c65eSChristoph Hellwig * @attr: attributes to change 1472c27c65eSChristoph Hellwig * 1482c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr 14931051c85SJan Kara * in the given dentry. This includes the normal unix access permission 15031051c85SJan Kara * checks, as well as checks for rlimits and others. The function also clears 15131051c85SJan Kara * SGID bit from mode if user is not allowed to set it. Also file capabilities 15231051c85SJan Kara * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. 1532c27c65eSChristoph Hellwig * 154c1632a0fSChristian Brauner * If the inode has been found through an idmapped mount the idmap of 155c1632a0fSChristian Brauner * the vfsmount must be passed through @idmap. This function will then 156c1632a0fSChristian Brauner * take care to map the inode according to @idmap before checking 1572f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 158c1632a0fSChristian Brauner * performed on the raw inode simply passs @nop_mnt_idmap. 1592f221d6fSChristian Brauner * 1602c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations, 1612c27c65eSChristoph Hellwig * possibly after taking additional locks. 1622c27c65eSChristoph Hellwig */ 163c1632a0fSChristian Brauner int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry, 1642f221d6fSChristian Brauner struct iattr *attr) 1651da177e4SLinus Torvalds { 16631051c85SJan Kara struct inode *inode = d_inode(dentry); 1671da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1681da177e4SLinus Torvalds 1692c27c65eSChristoph Hellwig /* 1702c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using 1712c27c65eSChristoph Hellwig * ATTR_FORCE. 1722c27c65eSChristoph Hellwig */ 1732c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) { 1742c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size); 1752c27c65eSChristoph Hellwig if (error) 1762c27c65eSChristoph Hellwig return error; 1772c27c65eSChristoph Hellwig } 1782c27c65eSChristoph Hellwig 1791da177e4SLinus Torvalds /* If force is set do it anyway. */ 1801da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE) 181030b533cSJan Kara goto kill_priv; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* Make sure a caller can chown. */ 184b27c82e1SChristian Brauner if ((ia_valid & ATTR_UID) && 1859452e93eSChristian Brauner !chown_ok(idmap, inode, attr->ia_vfsuid)) 1862c27c65eSChristoph Hellwig return -EPERM; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* Make sure caller can chgrp. */ 189b27c82e1SChristian Brauner if ((ia_valid & ATTR_GID) && 1909452e93eSChristian Brauner !chgrp_ok(idmap, inode, attr->ia_vfsgid)) 1912c27c65eSChristoph Hellwig return -EPERM; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* Make sure a caller can chmod. */ 1941da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 195b27c82e1SChristian Brauner vfsgid_t vfsgid; 196168f9128SChristian Brauner 19701beba79SChristian Brauner if (!inode_owner_or_capable(idmap, inode)) 1982c27c65eSChristoph Hellwig return -EPERM; 199168f9128SChristian Brauner 200168f9128SChristian Brauner if (ia_valid & ATTR_GID) 201b27c82e1SChristian Brauner vfsgid = attr->ia_vfsgid; 202168f9128SChristian Brauner else 203*e67fe633SChristian Brauner vfsgid = i_gid_into_vfsgid(idmap, inode); 204168f9128SChristian Brauner 2051da177e4SLinus Torvalds /* Also check the setgid bit! */ 2069452e93eSChristian Brauner if (!in_group_or_capable(idmap, inode, vfsgid)) 2071da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* Check for setting the inode time. */ 2119767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { 21201beba79SChristian Brauner if (!inode_owner_or_capable(idmap, inode)) 2132c27c65eSChristoph Hellwig return -EPERM; 2141da177e4SLinus Torvalds } 2152c27c65eSChristoph Hellwig 216030b533cSJan Kara kill_priv: 217030b533cSJan Kara /* User has permission for the change */ 218030b533cSJan Kara if (ia_valid & ATTR_KILL_PRIV) { 219030b533cSJan Kara int error; 220030b533cSJan Kara 22139f60c1cSChristian Brauner error = security_inode_killpriv(idmap, dentry); 222030b533cSJan Kara if (error) 223030b533cSJan Kara return error; 224030b533cSJan Kara } 225030b533cSJan Kara 2262c27c65eSChristoph Hellwig return 0; 2271da177e4SLinus Torvalds } 22831051c85SJan Kara EXPORT_SYMBOL(setattr_prepare); 2291da177e4SLinus Torvalds 23025d9e2d1Snpiggin@suse.de /** 23125d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size 23225d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated 23325d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode 23425d9e2d1Snpiggin@suse.de * 2357bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 2367bb46a67Snpiggin@suse.de * 23725d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 23825d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 23925d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 24025d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 24125d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 24225d9e2d1Snpiggin@suse.de * conditions). 2433fae1746SMatthew Wilcox * 2443fae1746SMatthew Wilcox * Return: 0 on success, -ve errno on failure 24525d9e2d1Snpiggin@suse.de */ 24625d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 24725d9e2d1Snpiggin@suse.de { 248e2ebff9cSDavid Howells if (offset < 0) 249e2ebff9cSDavid Howells return -EINVAL; 25025d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 25125d9e2d1Snpiggin@suse.de unsigned long limit; 25225d9e2d1Snpiggin@suse.de 253d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 25425d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 25525d9e2d1Snpiggin@suse.de goto out_sig; 25625d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 25725d9e2d1Snpiggin@suse.de goto out_big; 25825d9e2d1Snpiggin@suse.de } else { 25925d9e2d1Snpiggin@suse.de /* 26025d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 26125d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 26225d9e2d1Snpiggin@suse.de * blocks. 26325d9e2d1Snpiggin@suse.de */ 26425d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 26525d9e2d1Snpiggin@suse.de return -ETXTBSY; 26625d9e2d1Snpiggin@suse.de } 26725d9e2d1Snpiggin@suse.de 26825d9e2d1Snpiggin@suse.de return 0; 26925d9e2d1Snpiggin@suse.de out_sig: 27025d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 27125d9e2d1Snpiggin@suse.de out_big: 27225d9e2d1Snpiggin@suse.de return -EFBIG; 27325d9e2d1Snpiggin@suse.de } 27425d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 27525d9e2d1Snpiggin@suse.de 2767bb46a67Snpiggin@suse.de /** 2776a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 278c1632a0fSChristian Brauner * @idmap: idmap of the mount the inode was found from 2797bb46a67Snpiggin@suse.de * @inode: the inode to be updated 2807bb46a67Snpiggin@suse.de * @attr: the new attributes 2817bb46a67Snpiggin@suse.de * 2826a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 2837bb46a67Snpiggin@suse.de * 2846a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 285b27c82e1SChristian Brauner * in attr on idmapped mounts. Necessary permission checks to determine 2862f221d6fSChristian Brauner * whether or not the S_ISGID property needs to be removed are performed with 2872f221d6fSChristian Brauner * the correct idmapped mount permission helpers. 2882f221d6fSChristian Brauner * Noticeably missing is inode size update, which is more complex 2892c27c65eSChristoph Hellwig * as it requires pagecache updates. 2907bb46a67Snpiggin@suse.de * 291c1632a0fSChristian Brauner * If the inode has been found through an idmapped mount the idmap of 292c1632a0fSChristian Brauner * the vfsmount must be passed through @idmap. This function will then 293c1632a0fSChristian Brauner * take care to map the inode according to @idmap before checking 2942f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 295c1632a0fSChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap. 2962f221d6fSChristian Brauner * 2977bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 2987bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 2997bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 3007bb46a67Snpiggin@suse.de */ 301c1632a0fSChristian Brauner void setattr_copy(struct mnt_idmap *idmap, struct inode *inode, 3022f221d6fSChristian Brauner const struct iattr *attr) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 3051da177e4SLinus Torvalds 3060dbe12f2SChristian Brauner i_uid_update(idmap, attr, inode); 3070dbe12f2SChristian Brauner i_gid_update(idmap, attr, inode); 308eb31e2f6SAmir Goldstein if (ia_valid & ATTR_ATIME) 309eb31e2f6SAmir Goldstein inode->i_atime = attr->ia_atime; 310eb31e2f6SAmir Goldstein if (ia_valid & ATTR_MTIME) 311eb31e2f6SAmir Goldstein inode->i_mtime = attr->ia_mtime; 312eb31e2f6SAmir Goldstein if (ia_valid & ATTR_CTIME) 313eb31e2f6SAmir Goldstein inode->i_ctime = attr->ia_ctime; 3141da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 3151da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 3169452e93eSChristian Brauner if (!in_group_or_capable(idmap, inode, 317*e67fe633SChristian Brauner i_gid_into_vfsgid(idmap, inode))) 3181da177e4SLinus Torvalds mode &= ~S_ISGID; 3191da177e4SLinus Torvalds inode->i_mode = mode; 3201da177e4SLinus Torvalds } 3217bb46a67Snpiggin@suse.de } 3226a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 3237bb46a67Snpiggin@suse.de 3244609e1f1SChristian Brauner int may_setattr(struct mnt_idmap *idmap, struct inode *inode, 3257bb698f0SAndreas Gruenbacher unsigned int ia_valid) 3267bb698f0SAndreas Gruenbacher { 3277bb698f0SAndreas Gruenbacher int error; 3287bb698f0SAndreas Gruenbacher 3297bb698f0SAndreas Gruenbacher if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 3307bb698f0SAndreas Gruenbacher if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 3317bb698f0SAndreas Gruenbacher return -EPERM; 3327bb698f0SAndreas Gruenbacher } 3337bb698f0SAndreas Gruenbacher 3347bb698f0SAndreas Gruenbacher /* 3357bb698f0SAndreas Gruenbacher * If utimes(2) and friends are called with times == NULL (or both 3367bb698f0SAndreas Gruenbacher * times are UTIME_NOW), then we need to check for write permission 3377bb698f0SAndreas Gruenbacher */ 3387bb698f0SAndreas Gruenbacher if (ia_valid & ATTR_TOUCH) { 3397bb698f0SAndreas Gruenbacher if (IS_IMMUTABLE(inode)) 3407bb698f0SAndreas Gruenbacher return -EPERM; 3417bb698f0SAndreas Gruenbacher 34201beba79SChristian Brauner if (!inode_owner_or_capable(idmap, inode)) { 3434609e1f1SChristian Brauner error = inode_permission(idmap, inode, MAY_WRITE); 3447bb698f0SAndreas Gruenbacher if (error) 3457bb698f0SAndreas Gruenbacher return error; 3467bb698f0SAndreas Gruenbacher } 3477bb698f0SAndreas Gruenbacher } 3487bb698f0SAndreas Gruenbacher return 0; 3497bb698f0SAndreas Gruenbacher } 3507bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr); 3517bb698f0SAndreas Gruenbacher 35227ac0ffeSJ. Bruce Fields /** 35327ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 354abf08576SChristian Brauner * @idmap: idmap of the mount the inode was found from 35527ac0ffeSJ. Bruce Fields * @dentry: object affected 3563fae1746SMatthew Wilcox * @attr: new attributes 35727ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 35827ac0ffeSJ. Bruce Fields * 35927ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 36027ac0ffeSJ. Bruce Fields * 36127ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 36227ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 36327ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 36427ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 36527ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 36627ac0ffeSJ. Bruce Fields * 36727ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 36827ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 36927ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 37027ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 37127ac0ffeSJ. Bruce Fields * that case. 3722f221d6fSChristian Brauner * 373abf08576SChristian Brauner * If the inode has been found through an idmapped mount the idmap of 374abf08576SChristian Brauner * the vfsmount must be passed through @idmap. This function will then 375abf08576SChristian Brauner * take care to map the inode according to @idmap before checking 3762f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 377abf08576SChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap. 37827ac0ffeSJ. Bruce Fields */ 379abf08576SChristian Brauner int notify_change(struct mnt_idmap *idmap, struct dentry *dentry, 3802f221d6fSChristian Brauner struct iattr *attr, struct inode **delegated_inode) 3811da177e4SLinus Torvalds { 382abf08576SChristian Brauner struct user_namespace *mnt_userns = mnt_idmap_owner(idmap); 3831da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 3848d334acdSAl Viro umode_t mode = inode->i_mode; 3851da177e4SLinus Torvalds int error; 38695582b00SDeepa Dinamani struct timespec64 now; 3871da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 3881da177e4SLinus Torvalds 3895955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode)); 390c4107b30SAndrew Morton 3914609e1f1SChristian Brauner error = may_setattr(idmap, inode, ia_valid); 392f2b20f6eSMiklos Szeredi if (error) 393f2b20f6eSMiklos Szeredi return error; 394f2b20f6eSMiklos Szeredi 39569b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 3968d334acdSAl Viro umode_t amode = attr->ia_mode; 39769b45732SAndi Kleen /* Flag setting protected by i_mutex */ 39869b45732SAndi Kleen if (is_sxid(amode)) 39969b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 40069b45732SAndi Kleen } 40169b45732SAndi Kleen 402c2050a45SDeepa Dinamani now = current_time(inode); 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds attr->ia_ctime = now; 4051da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 4061da177e4SLinus Torvalds attr->ia_atime = now; 407eb31e2f6SAmir Goldstein else 408eb31e2f6SAmir Goldstein attr->ia_atime = timestamp_truncate(attr->ia_atime, inode); 4091da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 4101da177e4SLinus Torvalds attr->ia_mtime = now; 411eb31e2f6SAmir Goldstein else 412eb31e2f6SAmir Goldstein attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); 413eb31e2f6SAmir Goldstein 414b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 415b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 416030b533cSJan Kara if (error < 0) 417b5376771SSerge E. Hallyn return error; 418030b533cSJan Kara if (error == 0) 419030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; 420b5376771SSerge E. Hallyn } 4216de0ec00SJeff Layton 4226de0ec00SJeff Layton /* 4236de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 4246de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 4256de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 4266de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 4276de0ec00SJeff Layton * ATTR_KILL_S*ID set. 4286de0ec00SJeff Layton */ 4296de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 4306de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 4316de0ec00SJeff Layton BUG(); 4326de0ec00SJeff Layton 4331da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 4341da177e4SLinus Torvalds if (mode & S_ISUID) { 4351da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 4366de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 440ed5a7047SChristian Brauner if (mode & S_ISGID) { 4411da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 4421da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 4431da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds } 4486de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 4491da177e4SLinus Torvalds return 0; 4501da177e4SLinus Torvalds 451a475acf0SSeth Forshee /* 452a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target 453a475acf0SSeth Forshee * namespace of the superblock. 454a475acf0SSeth Forshee */ 455a475acf0SSeth Forshee if (ia_valid & ATTR_UID && 456b27c82e1SChristian Brauner !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns, 457b27c82e1SChristian Brauner attr->ia_vfsuid)) 458a475acf0SSeth Forshee return -EOVERFLOW; 459a475acf0SSeth Forshee if (ia_valid & ATTR_GID && 460b27c82e1SChristian Brauner !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns, 461b27c82e1SChristian Brauner attr->ia_vfsgid)) 462a475acf0SSeth Forshee return -EOVERFLOW; 463a475acf0SSeth Forshee 4640bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or 4650bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid. 4660bd23d09SEric W. Biederman */ 4672f221d6fSChristian Brauner if (!(ia_valid & ATTR_UID) && 468*e67fe633SChristian Brauner !vfsuid_valid(i_uid_into_vfsuid(idmap, inode))) 4690bd23d09SEric W. Biederman return -EOVERFLOW; 4702f221d6fSChristian Brauner if (!(ia_valid & ATTR_GID) && 471*e67fe633SChristian Brauner !vfsgid_valid(i_gid_into_vfsgid(idmap, inode))) 4720bd23d09SEric W. Biederman return -EOVERFLOW; 4730bd23d09SEric W. Biederman 474c1632a0fSChristian Brauner error = security_inode_setattr(idmap, dentry, attr); 475a77b72daSMiklos Szeredi if (error) 476a77b72daSMiklos Szeredi return error; 47727ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 47827ac0ffeSJ. Bruce Fields if (error) 47927ac0ffeSJ. Bruce Fields return error; 480a77b72daSMiklos Szeredi 481eef2380cSChristoph Hellwig if (inode->i_op->setattr) 482c1632a0fSChristian Brauner error = inode->i_op->setattr(idmap, dentry, attr); 483eef2380cSChristoph Hellwig else 484c1632a0fSChristian Brauner error = simple_setattr(idmap, dentry, attr); 4851da177e4SLinus Torvalds 486975d2943SMimi Zohar if (!error) { 4870eeca283SRobert Love fsnotify_change(dentry, ia_valid); 48839f60c1cSChristian Brauner ima_inode_post_setattr(idmap, dentry); 489975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 490975d2943SMimi Zohar } 4910eeca283SRobert Love 4921da177e4SLinus Torvalds return error; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 495