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 212f221d6fSChristian Brauner /** 222f221d6fSChristian Brauner * chown_ok - verify permissions to chown inode 232f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount @inode was found from 242f221d6fSChristian Brauner * @inode: inode to check permissions on 252f221d6fSChristian Brauner * @uid: uid to chown @inode to 262f221d6fSChristian Brauner * 272f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 282f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 292f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 302f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 312f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 322f221d6fSChristian Brauner */ 332f221d6fSChristian Brauner static bool chown_ok(struct user_namespace *mnt_userns, 342f221d6fSChristian Brauner const struct inode *inode, 352f221d6fSChristian Brauner kuid_t uid) 360031181cSEric W. Biederman { 372f221d6fSChristian Brauner kuid_t kuid = i_uid_into_mnt(mnt_userns, inode); 382f221d6fSChristian Brauner if (uid_eq(current_fsuid(), kuid) && uid_eq(uid, kuid)) 390031181cSEric W. Biederman return true; 402f221d6fSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN)) 410031181cSEric W. Biederman return true; 422f221d6fSChristian Brauner if (uid_eq(kuid, INVALID_UID) && 430031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 440031181cSEric W. Biederman return true; 450031181cSEric W. Biederman return false; 460031181cSEric W. Biederman } 470031181cSEric W. Biederman 482f221d6fSChristian Brauner /** 492f221d6fSChristian Brauner * chgrp_ok - verify permissions to chgrp inode 502f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount @inode was found from 512f221d6fSChristian Brauner * @inode: inode to check permissions on 522f221d6fSChristian Brauner * @gid: gid to chown @inode to 532f221d6fSChristian Brauner * 542f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 552f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 562f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 572f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 582f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 592f221d6fSChristian Brauner */ 602f221d6fSChristian Brauner static bool chgrp_ok(struct user_namespace *mnt_userns, 612f221d6fSChristian Brauner const struct inode *inode, kgid_t gid) 620031181cSEric W. Biederman { 632f221d6fSChristian Brauner kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); 642f221d6fSChristian Brauner if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) && 652f221d6fSChristian Brauner (in_group_p(gid) || gid_eq(gid, kgid))) 660031181cSEric W. Biederman return true; 672f221d6fSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN)) 680031181cSEric W. Biederman return true; 692f221d6fSChristian Brauner if (gid_eq(kgid, INVALID_GID) && 700031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 710031181cSEric W. Biederman return true; 720031181cSEric W. Biederman return false; 730031181cSEric W. Biederman } 740031181cSEric W. Biederman 752c27c65eSChristoph Hellwig /** 7631051c85SJan Kara * setattr_prepare - check if attribute changes to a dentry are allowed 772f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 7831051c85SJan Kara * @dentry: dentry to check 792c27c65eSChristoph Hellwig * @attr: attributes to change 802c27c65eSChristoph Hellwig * 812c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr 8231051c85SJan Kara * in the given dentry. This includes the normal unix access permission 8331051c85SJan Kara * checks, as well as checks for rlimits and others. The function also clears 8431051c85SJan Kara * SGID bit from mode if user is not allowed to set it. Also file capabilities 8531051c85SJan Kara * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. 862c27c65eSChristoph Hellwig * 872f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 882f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 892f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 902f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 912f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 922f221d6fSChristian Brauner * 932c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations, 942c27c65eSChristoph Hellwig * possibly after taking additional locks. 952c27c65eSChristoph Hellwig */ 962f221d6fSChristian Brauner int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry, 972f221d6fSChristian Brauner struct iattr *attr) 981da177e4SLinus Torvalds { 9931051c85SJan Kara struct inode *inode = d_inode(dentry); 1001da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1011da177e4SLinus Torvalds 1022c27c65eSChristoph Hellwig /* 1032c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using 1042c27c65eSChristoph Hellwig * ATTR_FORCE. 1052c27c65eSChristoph Hellwig */ 1062c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) { 1072c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size); 1082c27c65eSChristoph Hellwig if (error) 1092c27c65eSChristoph Hellwig return error; 1102c27c65eSChristoph Hellwig } 1112c27c65eSChristoph Hellwig 1121da177e4SLinus Torvalds /* If force is set do it anyway. */ 1131da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE) 114030b533cSJan Kara goto kill_priv; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* Make sure a caller can chown. */ 1172f221d6fSChristian Brauner if ((ia_valid & ATTR_UID) && !chown_ok(mnt_userns, inode, attr->ia_uid)) 1182c27c65eSChristoph Hellwig return -EPERM; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* Make sure caller can chgrp. */ 1212f221d6fSChristian Brauner if ((ia_valid & ATTR_GID) && !chgrp_ok(mnt_userns, inode, attr->ia_gid)) 1222c27c65eSChristoph Hellwig return -EPERM; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* Make sure a caller can chmod. */ 1251da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 1262f221d6fSChristian Brauner if (!inode_owner_or_capable(mnt_userns, inode)) 1272c27c65eSChristoph Hellwig return -EPERM; 1281da177e4SLinus Torvalds /* Also check the setgid bit! */ 1291da177e4SLinus Torvalds if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid : 1302f221d6fSChristian Brauner i_gid_into_mnt(mnt_userns, inode)) && 1312f221d6fSChristian Brauner !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) 1321da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* Check for setting the inode time. */ 1369767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { 1372f221d6fSChristian Brauner if (!inode_owner_or_capable(mnt_userns, inode)) 1382c27c65eSChristoph Hellwig return -EPERM; 1391da177e4SLinus Torvalds } 1402c27c65eSChristoph Hellwig 141030b533cSJan Kara kill_priv: 142030b533cSJan Kara /* User has permission for the change */ 143030b533cSJan Kara if (ia_valid & ATTR_KILL_PRIV) { 144030b533cSJan Kara int error; 145030b533cSJan Kara 14671bc356fSChristian Brauner error = security_inode_killpriv(mnt_userns, dentry); 147030b533cSJan Kara if (error) 148030b533cSJan Kara return error; 149030b533cSJan Kara } 150030b533cSJan Kara 1512c27c65eSChristoph Hellwig return 0; 1521da177e4SLinus Torvalds } 15331051c85SJan Kara EXPORT_SYMBOL(setattr_prepare); 1541da177e4SLinus Torvalds 15525d9e2d1Snpiggin@suse.de /** 15625d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size 15725d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated 15825d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode 15925d9e2d1Snpiggin@suse.de * 1607bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 1617bb46a67Snpiggin@suse.de * 16225d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 16325d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 16425d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 16525d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 16625d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 16725d9e2d1Snpiggin@suse.de * conditions). 1683fae1746SMatthew Wilcox * 1693fae1746SMatthew Wilcox * Return: 0 on success, -ve errno on failure 17025d9e2d1Snpiggin@suse.de */ 17125d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 17225d9e2d1Snpiggin@suse.de { 17325d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 17425d9e2d1Snpiggin@suse.de unsigned long limit; 17525d9e2d1Snpiggin@suse.de 176d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 17725d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 17825d9e2d1Snpiggin@suse.de goto out_sig; 17925d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 18025d9e2d1Snpiggin@suse.de goto out_big; 18125d9e2d1Snpiggin@suse.de } else { 18225d9e2d1Snpiggin@suse.de /* 18325d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 18425d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 18525d9e2d1Snpiggin@suse.de * blocks. 18625d9e2d1Snpiggin@suse.de */ 18725d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 18825d9e2d1Snpiggin@suse.de return -ETXTBSY; 18925d9e2d1Snpiggin@suse.de } 19025d9e2d1Snpiggin@suse.de 19125d9e2d1Snpiggin@suse.de return 0; 19225d9e2d1Snpiggin@suse.de out_sig: 19325d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 19425d9e2d1Snpiggin@suse.de out_big: 19525d9e2d1Snpiggin@suse.de return -EFBIG; 19625d9e2d1Snpiggin@suse.de } 19725d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 19825d9e2d1Snpiggin@suse.de 1997bb46a67Snpiggin@suse.de /** 2006a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 2012f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 2027bb46a67Snpiggin@suse.de * @inode: the inode to be updated 2037bb46a67Snpiggin@suse.de * @attr: the new attributes 2047bb46a67Snpiggin@suse.de * 2056a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 2067bb46a67Snpiggin@suse.de * 2076a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 2082f221d6fSChristian Brauner * in attr on idmapped mounts. If file ownership is changed setattr_copy 2092f221d6fSChristian Brauner * doesn't map ia_uid and ia_gid. It will asssume the caller has already 2102f221d6fSChristian Brauner * provided the intended values. Necessary permission checks to determine 2112f221d6fSChristian Brauner * whether or not the S_ISGID property needs to be removed are performed with 2122f221d6fSChristian Brauner * the correct idmapped mount permission helpers. 2132f221d6fSChristian Brauner * Noticeably missing is inode size update, which is more complex 2142c27c65eSChristoph Hellwig * as it requires pagecache updates. 2157bb46a67Snpiggin@suse.de * 2162f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 2172f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 2182f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 2192f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 2202f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 2212f221d6fSChristian Brauner * 2227bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 2237bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 2247bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 2257bb46a67Snpiggin@suse.de */ 2262f221d6fSChristian Brauner void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode, 2272f221d6fSChristian Brauner const struct iattr *attr) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds if (ia_valid & ATTR_UID) 2321da177e4SLinus Torvalds inode->i_uid = attr->ia_uid; 2331da177e4SLinus Torvalds if (ia_valid & ATTR_GID) 2341da177e4SLinus Torvalds inode->i_gid = attr->ia_gid; 235eb31e2f6SAmir Goldstein if (ia_valid & ATTR_ATIME) 236eb31e2f6SAmir Goldstein inode->i_atime = attr->ia_atime; 237eb31e2f6SAmir Goldstein if (ia_valid & ATTR_MTIME) 238eb31e2f6SAmir Goldstein inode->i_mtime = attr->ia_mtime; 239eb31e2f6SAmir Goldstein if (ia_valid & ATTR_CTIME) 240eb31e2f6SAmir Goldstein inode->i_ctime = attr->ia_ctime; 2411da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 2421da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 2432f221d6fSChristian Brauner kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); 2442f221d6fSChristian Brauner if (!in_group_p(kgid) && 2452f221d6fSChristian Brauner !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) 2461da177e4SLinus Torvalds mode &= ~S_ISGID; 2471da177e4SLinus Torvalds inode->i_mode = mode; 2481da177e4SLinus Torvalds } 2497bb46a67Snpiggin@suse.de } 2506a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 2517bb46a67Snpiggin@suse.de 25227ac0ffeSJ. Bruce Fields /** 25327ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 2542f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 25527ac0ffeSJ. Bruce Fields * @dentry: object affected 2563fae1746SMatthew Wilcox * @attr: new attributes 25727ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 25827ac0ffeSJ. Bruce Fields * 25927ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 26027ac0ffeSJ. Bruce Fields * 26127ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 26227ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 26327ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 26427ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 26527ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 26627ac0ffeSJ. Bruce Fields * 2672f221d6fSChristian Brauner * If file ownership is changed notify_change() doesn't map ia_uid and 2682f221d6fSChristian Brauner * ia_gid. It will asssume the caller has already provided the intended values. 2692f221d6fSChristian Brauner * 27027ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 27127ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 27227ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 27327ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 27427ac0ffeSJ. Bruce Fields * that case. 2752f221d6fSChristian Brauner * 2762f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 2772f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 2782f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 2792f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 2802f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 28127ac0ffeSJ. Bruce Fields */ 2822f221d6fSChristian Brauner int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry, 2832f221d6fSChristian Brauner struct iattr *attr, struct inode **delegated_inode) 2841da177e4SLinus Torvalds { 2851da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2868d334acdSAl Viro umode_t mode = inode->i_mode; 2871da177e4SLinus Torvalds int error; 28895582b00SDeepa Dinamani struct timespec64 now; 2891da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 2901da177e4SLinus Torvalds 2915955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode)); 292c4107b30SAndrew Morton 293beb29e05SMiklos Szeredi if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 294beb29e05SMiklos Szeredi if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 295beb29e05SMiklos Szeredi return -EPERM; 296beb29e05SMiklos Szeredi } 297beb29e05SMiklos Szeredi 298f2b20f6eSMiklos Szeredi /* 299f2b20f6eSMiklos Szeredi * If utimes(2) and friends are called with times == NULL (or both 300f2b20f6eSMiklos Szeredi * times are UTIME_NOW), then we need to check for write permission 301f2b20f6eSMiklos Szeredi */ 302f2b20f6eSMiklos Szeredi if (ia_valid & ATTR_TOUCH) { 303f2b20f6eSMiklos Szeredi if (IS_IMMUTABLE(inode)) 304f2b20f6eSMiklos Szeredi return -EPERM; 305f2b20f6eSMiklos Szeredi 3062f221d6fSChristian Brauner if (!inode_owner_or_capable(mnt_userns, inode)) { 3072f221d6fSChristian Brauner error = inode_permission(mnt_userns, inode, MAY_WRITE); 308f2b20f6eSMiklos Szeredi if (error) 309f2b20f6eSMiklos Szeredi return error; 310f2b20f6eSMiklos Szeredi } 311f2b20f6eSMiklos Szeredi } 312f2b20f6eSMiklos Szeredi 31369b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 3148d334acdSAl Viro umode_t amode = attr->ia_mode; 31569b45732SAndi Kleen /* Flag setting protected by i_mutex */ 31669b45732SAndi Kleen if (is_sxid(amode)) 31769b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 31869b45732SAndi Kleen } 31969b45732SAndi Kleen 320c2050a45SDeepa Dinamani now = current_time(inode); 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds attr->ia_ctime = now; 3231da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 3241da177e4SLinus Torvalds attr->ia_atime = now; 325eb31e2f6SAmir Goldstein else 326eb31e2f6SAmir Goldstein attr->ia_atime = timestamp_truncate(attr->ia_atime, inode); 3271da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 3281da177e4SLinus Torvalds attr->ia_mtime = now; 329eb31e2f6SAmir Goldstein else 330eb31e2f6SAmir Goldstein attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); 331eb31e2f6SAmir Goldstein 332b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 333b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 334030b533cSJan Kara if (error < 0) 335b5376771SSerge E. Hallyn return error; 336030b533cSJan Kara if (error == 0) 337030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; 338b5376771SSerge E. Hallyn } 3396de0ec00SJeff Layton 3406de0ec00SJeff Layton /* 3416de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 3426de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 3436de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 3446de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 3456de0ec00SJeff Layton * ATTR_KILL_S*ID set. 3466de0ec00SJeff Layton */ 3476de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 3486de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 3496de0ec00SJeff Layton BUG(); 3506de0ec00SJeff Layton 3511da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 3521da177e4SLinus Torvalds if (mode & S_ISUID) { 3531da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 3546de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 3581da177e4SLinus Torvalds if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 3591da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 3601da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 3611da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds } 3666de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 3671da177e4SLinus Torvalds return 0; 3681da177e4SLinus Torvalds 369a475acf0SSeth Forshee /* 370a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target 371a475acf0SSeth Forshee * namespace of the superblock. 372a475acf0SSeth Forshee */ 373a475acf0SSeth Forshee if (ia_valid & ATTR_UID && 374a475acf0SSeth Forshee !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) 375a475acf0SSeth Forshee return -EOVERFLOW; 376a475acf0SSeth Forshee if (ia_valid & ATTR_GID && 377a475acf0SSeth Forshee !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) 378a475acf0SSeth Forshee return -EOVERFLOW; 379a475acf0SSeth Forshee 3800bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or 3810bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid. 3820bd23d09SEric W. Biederman */ 3832f221d6fSChristian Brauner if (!(ia_valid & ATTR_UID) && 3842f221d6fSChristian Brauner !uid_valid(i_uid_into_mnt(mnt_userns, inode))) 3850bd23d09SEric W. Biederman return -EOVERFLOW; 3862f221d6fSChristian Brauner if (!(ia_valid & ATTR_GID) && 3872f221d6fSChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 3880bd23d09SEric W. Biederman return -EOVERFLOW; 3890bd23d09SEric W. Biederman 390a77b72daSMiklos Szeredi error = security_inode_setattr(dentry, attr); 391a77b72daSMiklos Szeredi if (error) 392a77b72daSMiklos Szeredi return error; 39327ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 39427ac0ffeSJ. Bruce Fields if (error) 39527ac0ffeSJ. Bruce Fields return error; 396a77b72daSMiklos Szeredi 397eef2380cSChristoph Hellwig if (inode->i_op->setattr) 398549c7297SChristian Brauner error = inode->i_op->setattr(mnt_userns, dentry, attr); 399eef2380cSChristoph Hellwig else 400549c7297SChristian Brauner error = simple_setattr(mnt_userns, dentry, attr); 4011da177e4SLinus Torvalds 402975d2943SMimi Zohar if (!error) { 4030eeca283SRobert Love fsnotify_change(dentry, ia_valid); 404*a2d2329eSChristian Brauner ima_inode_post_setattr(mnt_userns, dentry); 405975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 406975d2943SMimi Zohar } 4070eeca283SRobert Love 4081da177e4SLinus Torvalds return error; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 411