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); 3896821970SChristian Brauner if (uid_eq(current_fsuid(), kuid) && uid_eq(uid, inode->i_uid)) 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); 64*168f9128SChristian Brauner if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) { 65*168f9128SChristian Brauner kgid_t mapped_gid; 66*168f9128SChristian Brauner 67*168f9128SChristian Brauner if (gid_eq(gid, inode->i_gid)) 680031181cSEric W. Biederman return true; 69*168f9128SChristian Brauner mapped_gid = mapped_kgid_fs(mnt_userns, i_user_ns(inode), gid); 70*168f9128SChristian Brauner if (in_group_p(mapped_gid)) 71*168f9128SChristian Brauner return true; 72*168f9128SChristian Brauner } 732f221d6fSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN)) 740031181cSEric W. Biederman return true; 752f221d6fSChristian Brauner if (gid_eq(kgid, INVALID_GID) && 760031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 770031181cSEric W. Biederman return true; 780031181cSEric W. Biederman return false; 790031181cSEric W. Biederman } 800031181cSEric W. Biederman 812c27c65eSChristoph Hellwig /** 8231051c85SJan Kara * setattr_prepare - check if attribute changes to a dentry are allowed 832f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 8431051c85SJan Kara * @dentry: dentry to check 852c27c65eSChristoph Hellwig * @attr: attributes to change 862c27c65eSChristoph Hellwig * 872c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr 8831051c85SJan Kara * in the given dentry. This includes the normal unix access permission 8931051c85SJan Kara * checks, as well as checks for rlimits and others. The function also clears 9031051c85SJan Kara * SGID bit from mode if user is not allowed to set it. Also file capabilities 9131051c85SJan Kara * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. 922c27c65eSChristoph Hellwig * 932f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 942f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 952f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 962f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 972f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 982f221d6fSChristian Brauner * 992c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations, 1002c27c65eSChristoph Hellwig * possibly after taking additional locks. 1012c27c65eSChristoph Hellwig */ 1022f221d6fSChristian Brauner int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry, 1032f221d6fSChristian Brauner struct iattr *attr) 1041da177e4SLinus Torvalds { 10531051c85SJan Kara struct inode *inode = d_inode(dentry); 1061da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1071da177e4SLinus Torvalds 1082c27c65eSChristoph Hellwig /* 1092c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using 1102c27c65eSChristoph Hellwig * ATTR_FORCE. 1112c27c65eSChristoph Hellwig */ 1122c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) { 1132c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size); 1142c27c65eSChristoph Hellwig if (error) 1152c27c65eSChristoph Hellwig return error; 1162c27c65eSChristoph Hellwig } 1172c27c65eSChristoph Hellwig 1181da177e4SLinus Torvalds /* If force is set do it anyway. */ 1191da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE) 120030b533cSJan Kara goto kill_priv; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* Make sure a caller can chown. */ 1232f221d6fSChristian Brauner if ((ia_valid & ATTR_UID) && !chown_ok(mnt_userns, inode, attr->ia_uid)) 1242c27c65eSChristoph Hellwig return -EPERM; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* Make sure caller can chgrp. */ 1272f221d6fSChristian Brauner if ((ia_valid & ATTR_GID) && !chgrp_ok(mnt_userns, inode, attr->ia_gid)) 1282c27c65eSChristoph Hellwig return -EPERM; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* Make sure a caller can chmod. */ 1311da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 132*168f9128SChristian Brauner kgid_t mapped_gid; 133*168f9128SChristian Brauner 1342f221d6fSChristian Brauner if (!inode_owner_or_capable(mnt_userns, inode)) 1352c27c65eSChristoph Hellwig return -EPERM; 136*168f9128SChristian Brauner 137*168f9128SChristian Brauner if (ia_valid & ATTR_GID) 138*168f9128SChristian Brauner mapped_gid = mapped_kgid_fs(mnt_userns, 139*168f9128SChristian Brauner i_user_ns(inode), attr->ia_gid); 140*168f9128SChristian Brauner else 141*168f9128SChristian Brauner mapped_gid = i_gid_into_mnt(mnt_userns, inode); 142*168f9128SChristian Brauner 1431da177e4SLinus Torvalds /* Also check the setgid bit! */ 144*168f9128SChristian Brauner if (!in_group_p(mapped_gid) && 1452f221d6fSChristian Brauner !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) 1461da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* Check for setting the inode time. */ 1509767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { 1512f221d6fSChristian Brauner if (!inode_owner_or_capable(mnt_userns, inode)) 1522c27c65eSChristoph Hellwig return -EPERM; 1531da177e4SLinus Torvalds } 1542c27c65eSChristoph Hellwig 155030b533cSJan Kara kill_priv: 156030b533cSJan Kara /* User has permission for the change */ 157030b533cSJan Kara if (ia_valid & ATTR_KILL_PRIV) { 158030b533cSJan Kara int error; 159030b533cSJan Kara 16071bc356fSChristian Brauner error = security_inode_killpriv(mnt_userns, dentry); 161030b533cSJan Kara if (error) 162030b533cSJan Kara return error; 163030b533cSJan Kara } 164030b533cSJan Kara 1652c27c65eSChristoph Hellwig return 0; 1661da177e4SLinus Torvalds } 16731051c85SJan Kara EXPORT_SYMBOL(setattr_prepare); 1681da177e4SLinus Torvalds 16925d9e2d1Snpiggin@suse.de /** 17025d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size 17125d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated 17225d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode 17325d9e2d1Snpiggin@suse.de * 1747bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 1757bb46a67Snpiggin@suse.de * 17625d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 17725d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 17825d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 17925d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 18025d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 18125d9e2d1Snpiggin@suse.de * conditions). 1823fae1746SMatthew Wilcox * 1833fae1746SMatthew Wilcox * Return: 0 on success, -ve errno on failure 18425d9e2d1Snpiggin@suse.de */ 18525d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 18625d9e2d1Snpiggin@suse.de { 18725d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 18825d9e2d1Snpiggin@suse.de unsigned long limit; 18925d9e2d1Snpiggin@suse.de 190d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 19125d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 19225d9e2d1Snpiggin@suse.de goto out_sig; 19325d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 19425d9e2d1Snpiggin@suse.de goto out_big; 19525d9e2d1Snpiggin@suse.de } else { 19625d9e2d1Snpiggin@suse.de /* 19725d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 19825d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 19925d9e2d1Snpiggin@suse.de * blocks. 20025d9e2d1Snpiggin@suse.de */ 20125d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 20225d9e2d1Snpiggin@suse.de return -ETXTBSY; 20325d9e2d1Snpiggin@suse.de } 20425d9e2d1Snpiggin@suse.de 20525d9e2d1Snpiggin@suse.de return 0; 20625d9e2d1Snpiggin@suse.de out_sig: 20725d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 20825d9e2d1Snpiggin@suse.de out_big: 20925d9e2d1Snpiggin@suse.de return -EFBIG; 21025d9e2d1Snpiggin@suse.de } 21125d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 21225d9e2d1Snpiggin@suse.de 2137bb46a67Snpiggin@suse.de /** 2146a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 2152f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 2167bb46a67Snpiggin@suse.de * @inode: the inode to be updated 2177bb46a67Snpiggin@suse.de * @attr: the new attributes 2187bb46a67Snpiggin@suse.de * 2196a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 2207bb46a67Snpiggin@suse.de * 2216a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 2222f221d6fSChristian Brauner * in attr on idmapped mounts. If file ownership is changed setattr_copy 2232f221d6fSChristian Brauner * doesn't map ia_uid and ia_gid. It will asssume the caller has already 2242f221d6fSChristian Brauner * provided the intended values. Necessary permission checks to determine 2252f221d6fSChristian Brauner * whether or not the S_ISGID property needs to be removed are performed with 2262f221d6fSChristian Brauner * the correct idmapped mount permission helpers. 2272f221d6fSChristian Brauner * Noticeably missing is inode size update, which is more complex 2282c27c65eSChristoph Hellwig * as it requires pagecache updates. 2297bb46a67Snpiggin@suse.de * 2302f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 2312f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 2322f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 2332f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 2342f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 2352f221d6fSChristian Brauner * 2367bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 2377bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 2387bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 2397bb46a67Snpiggin@suse.de */ 2402f221d6fSChristian Brauner void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode, 2412f221d6fSChristian Brauner const struct iattr *attr) 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds if (ia_valid & ATTR_UID) 2461da177e4SLinus Torvalds inode->i_uid = attr->ia_uid; 2471da177e4SLinus Torvalds if (ia_valid & ATTR_GID) 2481da177e4SLinus Torvalds inode->i_gid = attr->ia_gid; 249eb31e2f6SAmir Goldstein if (ia_valid & ATTR_ATIME) 250eb31e2f6SAmir Goldstein inode->i_atime = attr->ia_atime; 251eb31e2f6SAmir Goldstein if (ia_valid & ATTR_MTIME) 252eb31e2f6SAmir Goldstein inode->i_mtime = attr->ia_mtime; 253eb31e2f6SAmir Goldstein if (ia_valid & ATTR_CTIME) 254eb31e2f6SAmir Goldstein inode->i_ctime = attr->ia_ctime; 2551da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 2561da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 2572f221d6fSChristian Brauner kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); 2582f221d6fSChristian Brauner if (!in_group_p(kgid) && 2592f221d6fSChristian Brauner !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) 2601da177e4SLinus Torvalds mode &= ~S_ISGID; 2611da177e4SLinus Torvalds inode->i_mode = mode; 2621da177e4SLinus Torvalds } 2637bb46a67Snpiggin@suse.de } 2646a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 2657bb46a67Snpiggin@suse.de 2667bb698f0SAndreas Gruenbacher int may_setattr(struct user_namespace *mnt_userns, struct inode *inode, 2677bb698f0SAndreas Gruenbacher unsigned int ia_valid) 2687bb698f0SAndreas Gruenbacher { 2697bb698f0SAndreas Gruenbacher int error; 2707bb698f0SAndreas Gruenbacher 2717bb698f0SAndreas Gruenbacher if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 2727bb698f0SAndreas Gruenbacher if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 2737bb698f0SAndreas Gruenbacher return -EPERM; 2747bb698f0SAndreas Gruenbacher } 2757bb698f0SAndreas Gruenbacher 2767bb698f0SAndreas Gruenbacher /* 2777bb698f0SAndreas Gruenbacher * If utimes(2) and friends are called with times == NULL (or both 2787bb698f0SAndreas Gruenbacher * times are UTIME_NOW), then we need to check for write permission 2797bb698f0SAndreas Gruenbacher */ 2807bb698f0SAndreas Gruenbacher if (ia_valid & ATTR_TOUCH) { 2817bb698f0SAndreas Gruenbacher if (IS_IMMUTABLE(inode)) 2827bb698f0SAndreas Gruenbacher return -EPERM; 2837bb698f0SAndreas Gruenbacher 2847bb698f0SAndreas Gruenbacher if (!inode_owner_or_capable(mnt_userns, inode)) { 2857bb698f0SAndreas Gruenbacher error = inode_permission(mnt_userns, inode, MAY_WRITE); 2867bb698f0SAndreas Gruenbacher if (error) 2877bb698f0SAndreas Gruenbacher return error; 2887bb698f0SAndreas Gruenbacher } 2897bb698f0SAndreas Gruenbacher } 2907bb698f0SAndreas Gruenbacher return 0; 2917bb698f0SAndreas Gruenbacher } 2927bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr); 2937bb698f0SAndreas Gruenbacher 29427ac0ffeSJ. Bruce Fields /** 29527ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 2962f221d6fSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 29727ac0ffeSJ. Bruce Fields * @dentry: object affected 2983fae1746SMatthew Wilcox * @attr: new attributes 29927ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 30027ac0ffeSJ. Bruce Fields * 30127ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 30227ac0ffeSJ. Bruce Fields * 30327ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 30427ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 30527ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 30627ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 30727ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 30827ac0ffeSJ. Bruce Fields * 3092f221d6fSChristian Brauner * If file ownership is changed notify_change() doesn't map ia_uid and 3102f221d6fSChristian Brauner * ia_gid. It will asssume the caller has already provided the intended values. 3112f221d6fSChristian Brauner * 31227ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 31327ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 31427ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 31527ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 31627ac0ffeSJ. Bruce Fields * that case. 3172f221d6fSChristian Brauner * 3182f221d6fSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 3192f221d6fSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then 3202f221d6fSChristian Brauner * take care to map the inode according to @mnt_userns before checking 3212f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be 3222f221d6fSChristian Brauner * performed on the raw inode simply passs init_user_ns. 32327ac0ffeSJ. Bruce Fields */ 3242f221d6fSChristian Brauner int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry, 3252f221d6fSChristian Brauner struct iattr *attr, struct inode **delegated_inode) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 3288d334acdSAl Viro umode_t mode = inode->i_mode; 3291da177e4SLinus Torvalds int error; 33095582b00SDeepa Dinamani struct timespec64 now; 3311da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 3321da177e4SLinus Torvalds 3335955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode)); 334c4107b30SAndrew Morton 3357bb698f0SAndreas Gruenbacher error = may_setattr(mnt_userns, inode, ia_valid); 336f2b20f6eSMiklos Szeredi if (error) 337f2b20f6eSMiklos Szeredi return error; 338f2b20f6eSMiklos Szeredi 33969b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 3408d334acdSAl Viro umode_t amode = attr->ia_mode; 34169b45732SAndi Kleen /* Flag setting protected by i_mutex */ 34269b45732SAndi Kleen if (is_sxid(amode)) 34369b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 34469b45732SAndi Kleen } 34569b45732SAndi Kleen 346c2050a45SDeepa Dinamani now = current_time(inode); 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds attr->ia_ctime = now; 3491da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 3501da177e4SLinus Torvalds attr->ia_atime = now; 351eb31e2f6SAmir Goldstein else 352eb31e2f6SAmir Goldstein attr->ia_atime = timestamp_truncate(attr->ia_atime, inode); 3531da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 3541da177e4SLinus Torvalds attr->ia_mtime = now; 355eb31e2f6SAmir Goldstein else 356eb31e2f6SAmir Goldstein attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); 357eb31e2f6SAmir Goldstein 358b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 359b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 360030b533cSJan Kara if (error < 0) 361b5376771SSerge E. Hallyn return error; 362030b533cSJan Kara if (error == 0) 363030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; 364b5376771SSerge E. Hallyn } 3656de0ec00SJeff Layton 3666de0ec00SJeff Layton /* 3676de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 3686de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 3696de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 3706de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 3716de0ec00SJeff Layton * ATTR_KILL_S*ID set. 3726de0ec00SJeff Layton */ 3736de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 3746de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 3756de0ec00SJeff Layton BUG(); 3766de0ec00SJeff Layton 3771da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 3781da177e4SLinus Torvalds if (mode & S_ISUID) { 3791da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 3806de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 3841da177e4SLinus Torvalds if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 3851da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 3861da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 3871da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds } 3926de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 3931da177e4SLinus Torvalds return 0; 3941da177e4SLinus Torvalds 395a475acf0SSeth Forshee /* 396a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target 397a475acf0SSeth Forshee * namespace of the superblock. 398a475acf0SSeth Forshee */ 399a475acf0SSeth Forshee if (ia_valid & ATTR_UID && 400a475acf0SSeth Forshee !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) 401a475acf0SSeth Forshee return -EOVERFLOW; 402a475acf0SSeth Forshee if (ia_valid & ATTR_GID && 403a475acf0SSeth Forshee !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) 404a475acf0SSeth Forshee return -EOVERFLOW; 405a475acf0SSeth Forshee 4060bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or 4070bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid. 4080bd23d09SEric W. Biederman */ 4092f221d6fSChristian Brauner if (!(ia_valid & ATTR_UID) && 4102f221d6fSChristian Brauner !uid_valid(i_uid_into_mnt(mnt_userns, inode))) 4110bd23d09SEric W. Biederman return -EOVERFLOW; 4122f221d6fSChristian Brauner if (!(ia_valid & ATTR_GID) && 4132f221d6fSChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 4140bd23d09SEric W. Biederman return -EOVERFLOW; 4150bd23d09SEric W. Biederman 416a77b72daSMiklos Szeredi error = security_inode_setattr(dentry, attr); 417a77b72daSMiklos Szeredi if (error) 418a77b72daSMiklos Szeredi return error; 41927ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 42027ac0ffeSJ. Bruce Fields if (error) 42127ac0ffeSJ. Bruce Fields return error; 422a77b72daSMiklos Szeredi 423eef2380cSChristoph Hellwig if (inode->i_op->setattr) 424549c7297SChristian Brauner error = inode->i_op->setattr(mnt_userns, dentry, attr); 425eef2380cSChristoph Hellwig else 426549c7297SChristian Brauner error = simple_setattr(mnt_userns, dentry, attr); 4271da177e4SLinus Torvalds 428975d2943SMimi Zohar if (!error) { 4290eeca283SRobert Love fsnotify_change(dentry, ia_valid); 430a2d2329eSChristian Brauner ima_inode_post_setattr(mnt_userns, dentry); 431975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 432975d2943SMimi Zohar } 4330eeca283SRobert Love 4341da177e4SLinus Torvalds return error; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 437