xref: /openbmc/linux/fs/attr.c (revision e2ebff9c57fe4eb104ce4768f6ebcccf76bef849)
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
2581a1807dSChristian Brauner  * @ia_vfsuid:	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,
34b27c82e1SChristian Brauner 		     const struct inode *inode, vfsuid_t ia_vfsuid)
350031181cSEric W. Biederman {
36b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
37b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
38b27c82e1SChristian Brauner 	    vfsuid_eq(ia_vfsuid, vfsuid))
390031181cSEric W. Biederman 		return true;
402f221d6fSChristian Brauner 	if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
410031181cSEric W. Biederman 		return true;
42b27c82e1SChristian Brauner 	if (!vfsuid_valid(vfsuid) &&
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
5281a1807dSChristian Brauner  * @ia_vfsgid:	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,
61b27c82e1SChristian Brauner 		     const struct inode *inode, vfsgid_t ia_vfsgid)
620031181cSEric W. Biederman {
63b27c82e1SChristian Brauner 	vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
64b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
65b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
66b27c82e1SChristian Brauner 		if (vfsgid_eq(ia_vfsgid, vfsgid))
670031181cSEric W. Biederman 			return true;
68b27c82e1SChristian Brauner 		if (vfsgid_in_group_p(ia_vfsgid))
69168f9128SChristian Brauner 			return true;
70168f9128SChristian Brauner 	}
712f221d6fSChristian Brauner 	if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
720031181cSEric W. Biederman 		return true;
73b27c82e1SChristian Brauner 	if (!vfsgid_valid(vfsgid) &&
740031181cSEric W. Biederman 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
750031181cSEric W. Biederman 		return true;
760031181cSEric W. Biederman 	return false;
770031181cSEric W. Biederman }
780031181cSEric W. Biederman 
792c27c65eSChristoph Hellwig /**
8031051c85SJan Kara  * setattr_prepare - check if attribute changes to a dentry are allowed
812f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
8231051c85SJan Kara  * @dentry:	dentry to check
832c27c65eSChristoph Hellwig  * @attr:	attributes to change
842c27c65eSChristoph Hellwig  *
852c27c65eSChristoph Hellwig  * Check if we are allowed to change the attributes contained in @attr
8631051c85SJan Kara  * in the given dentry.  This includes the normal unix access permission
8731051c85SJan Kara  * checks, as well as checks for rlimits and others. The function also clears
8831051c85SJan Kara  * SGID bit from mode if user is not allowed to set it. Also file capabilities
8931051c85SJan Kara  * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
902c27c65eSChristoph Hellwig  *
912f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
922f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
932f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
942f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
952f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
962f221d6fSChristian Brauner  *
972c27c65eSChristoph Hellwig  * Should be called as the first thing in ->setattr implementations,
982c27c65eSChristoph Hellwig  * possibly after taking additional locks.
992c27c65eSChristoph Hellwig  */
1002f221d6fSChristian Brauner int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
1012f221d6fSChristian Brauner 		    struct iattr *attr)
1021da177e4SLinus Torvalds {
10331051c85SJan Kara 	struct inode *inode = d_inode(dentry);
1041da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1051da177e4SLinus Torvalds 
1062c27c65eSChristoph Hellwig 	/*
1072c27c65eSChristoph Hellwig 	 * First check size constraints.  These can't be overriden using
1082c27c65eSChristoph Hellwig 	 * ATTR_FORCE.
1092c27c65eSChristoph Hellwig 	 */
1102c27c65eSChristoph Hellwig 	if (ia_valid & ATTR_SIZE) {
1112c27c65eSChristoph Hellwig 		int error = inode_newsize_ok(inode, attr->ia_size);
1122c27c65eSChristoph Hellwig 		if (error)
1132c27c65eSChristoph Hellwig 			return error;
1142c27c65eSChristoph Hellwig 	}
1152c27c65eSChristoph Hellwig 
1161da177e4SLinus Torvalds 	/* If force is set do it anyway. */
1171da177e4SLinus Torvalds 	if (ia_valid & ATTR_FORCE)
118030b533cSJan Kara 		goto kill_priv;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/* Make sure a caller can chown. */
121b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_UID) &&
122b27c82e1SChristian Brauner 	    !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
1232c27c65eSChristoph Hellwig 		return -EPERM;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/* Make sure caller can chgrp. */
126b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_GID) &&
127b27c82e1SChristian Brauner 	    !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
1282c27c65eSChristoph Hellwig 		return -EPERM;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	/* Make sure a caller can chmod. */
1311da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
132b27c82e1SChristian Brauner 		vfsgid_t vfsgid;
133168f9128SChristian Brauner 
1342f221d6fSChristian Brauner 		if (!inode_owner_or_capable(mnt_userns, inode))
1352c27c65eSChristoph Hellwig 			return -EPERM;
136168f9128SChristian Brauner 
137168f9128SChristian Brauner 		if (ia_valid & ATTR_GID)
138b27c82e1SChristian Brauner 			vfsgid = attr->ia_vfsgid;
139168f9128SChristian Brauner 		else
140b27c82e1SChristian Brauner 			vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
141168f9128SChristian Brauner 
1421da177e4SLinus Torvalds 		/* Also check the setgid bit! */
143b27c82e1SChristian Brauner 		if (!vfsgid_in_group_p(vfsgid) &&
1442f221d6fSChristian Brauner 		    !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
1451da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	/* Check for setting the inode time. */
1499767d749SMiklos Szeredi 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
1502f221d6fSChristian Brauner 		if (!inode_owner_or_capable(mnt_userns, inode))
1512c27c65eSChristoph Hellwig 			return -EPERM;
1521da177e4SLinus Torvalds 	}
1532c27c65eSChristoph Hellwig 
154030b533cSJan Kara kill_priv:
155030b533cSJan Kara 	/* User has permission for the change */
156030b533cSJan Kara 	if (ia_valid & ATTR_KILL_PRIV) {
157030b533cSJan Kara 		int error;
158030b533cSJan Kara 
15971bc356fSChristian Brauner 		error = security_inode_killpriv(mnt_userns, dentry);
160030b533cSJan Kara 		if (error)
161030b533cSJan Kara 			return error;
162030b533cSJan Kara 	}
163030b533cSJan Kara 
1642c27c65eSChristoph Hellwig 	return 0;
1651da177e4SLinus Torvalds }
16631051c85SJan Kara EXPORT_SYMBOL(setattr_prepare);
1671da177e4SLinus Torvalds 
16825d9e2d1Snpiggin@suse.de /**
16925d9e2d1Snpiggin@suse.de  * inode_newsize_ok - may this inode be truncated to a given size
17025d9e2d1Snpiggin@suse.de  * @inode:	the inode to be truncated
17125d9e2d1Snpiggin@suse.de  * @offset:	the new size to assign to the inode
17225d9e2d1Snpiggin@suse.de  *
1737bb46a67Snpiggin@suse.de  * inode_newsize_ok must be called with i_mutex held.
1747bb46a67Snpiggin@suse.de  *
17525d9e2d1Snpiggin@suse.de  * inode_newsize_ok will check filesystem limits and ulimits to check that the
17625d9e2d1Snpiggin@suse.de  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
17725d9e2d1Snpiggin@suse.de  * when necessary. Caller must not proceed with inode size change if failure is
17825d9e2d1Snpiggin@suse.de  * returned. @inode must be a file (not directory), with appropriate
17925d9e2d1Snpiggin@suse.de  * permissions to allow truncate (inode_newsize_ok does NOT check these
18025d9e2d1Snpiggin@suse.de  * conditions).
1813fae1746SMatthew Wilcox  *
1823fae1746SMatthew Wilcox  * Return: 0 on success, -ve errno on failure
18325d9e2d1Snpiggin@suse.de  */
18425d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
18525d9e2d1Snpiggin@suse.de {
186*e2ebff9cSDavid Howells 	if (offset < 0)
187*e2ebff9cSDavid Howells 		return -EINVAL;
18825d9e2d1Snpiggin@suse.de 	if (inode->i_size < offset) {
18925d9e2d1Snpiggin@suse.de 		unsigned long limit;
19025d9e2d1Snpiggin@suse.de 
191d554ed89SJiri Slaby 		limit = rlimit(RLIMIT_FSIZE);
19225d9e2d1Snpiggin@suse.de 		if (limit != RLIM_INFINITY && offset > limit)
19325d9e2d1Snpiggin@suse.de 			goto out_sig;
19425d9e2d1Snpiggin@suse.de 		if (offset > inode->i_sb->s_maxbytes)
19525d9e2d1Snpiggin@suse.de 			goto out_big;
19625d9e2d1Snpiggin@suse.de 	} else {
19725d9e2d1Snpiggin@suse.de 		/*
19825d9e2d1Snpiggin@suse.de 		 * truncation of in-use swapfiles is disallowed - it would
19925d9e2d1Snpiggin@suse.de 		 * cause subsequent swapout to scribble on the now-freed
20025d9e2d1Snpiggin@suse.de 		 * blocks.
20125d9e2d1Snpiggin@suse.de 		 */
20225d9e2d1Snpiggin@suse.de 		if (IS_SWAPFILE(inode))
20325d9e2d1Snpiggin@suse.de 			return -ETXTBSY;
20425d9e2d1Snpiggin@suse.de 	}
20525d9e2d1Snpiggin@suse.de 
20625d9e2d1Snpiggin@suse.de 	return 0;
20725d9e2d1Snpiggin@suse.de out_sig:
20825d9e2d1Snpiggin@suse.de 	send_sig(SIGXFSZ, current, 0);
20925d9e2d1Snpiggin@suse.de out_big:
21025d9e2d1Snpiggin@suse.de 	return -EFBIG;
21125d9e2d1Snpiggin@suse.de }
21225d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
21325d9e2d1Snpiggin@suse.de 
2147bb46a67Snpiggin@suse.de /**
2156a1a90adSChristoph Hellwig  * setattr_copy - copy simple metadata updates into the generic inode
2162f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
2177bb46a67Snpiggin@suse.de  * @inode:	the inode to be updated
2187bb46a67Snpiggin@suse.de  * @attr:	the new attributes
2197bb46a67Snpiggin@suse.de  *
2206a1a90adSChristoph Hellwig  * setattr_copy must be called with i_mutex held.
2217bb46a67Snpiggin@suse.de  *
2226a1a90adSChristoph Hellwig  * setattr_copy updates the inode's metadata with that specified
223b27c82e1SChristian Brauner  * in attr on idmapped mounts. Necessary permission checks to determine
2242f221d6fSChristian Brauner  * whether or not the S_ISGID property needs to be removed are performed with
2252f221d6fSChristian Brauner  * the correct idmapped mount permission helpers.
2262f221d6fSChristian Brauner  * Noticeably missing is inode size update, which is more complex
2272c27c65eSChristoph Hellwig  * as it requires pagecache updates.
2287bb46a67Snpiggin@suse.de  *
2292f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
2302f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
2312f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
2322f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
2332f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
2342f221d6fSChristian Brauner  *
2357bb46a67Snpiggin@suse.de  * The inode is not marked as dirty after this operation. The rationale is
2367bb46a67Snpiggin@suse.de  * that for "simple" filesystems, the struct inode is the inode storage.
2377bb46a67Snpiggin@suse.de  * The caller is free to mark the inode dirty afterwards if needed.
2387bb46a67Snpiggin@suse.de  */
2392f221d6fSChristian Brauner void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
2402f221d6fSChristian Brauner 		  const struct iattr *attr)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
2431da177e4SLinus Torvalds 
244b27c82e1SChristian Brauner 	i_uid_update(mnt_userns, attr, inode);
245b27c82e1SChristian Brauner 	i_gid_update(mnt_userns, attr, inode);
246eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_ATIME)
247eb31e2f6SAmir Goldstein 		inode->i_atime = attr->ia_atime;
248eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_MTIME)
249eb31e2f6SAmir Goldstein 		inode->i_mtime = attr->ia_mtime;
250eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_CTIME)
251eb31e2f6SAmir Goldstein 		inode->i_ctime = attr->ia_ctime;
2521da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
2531da177e4SLinus Torvalds 		umode_t mode = attr->ia_mode;
254b27c82e1SChristian Brauner 		vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
255b27c82e1SChristian Brauner 		if (!vfsgid_in_group_p(vfsgid) &&
2562f221d6fSChristian Brauner 		    !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
2571da177e4SLinus Torvalds 			mode &= ~S_ISGID;
2581da177e4SLinus Torvalds 		inode->i_mode = mode;
2591da177e4SLinus Torvalds 	}
2607bb46a67Snpiggin@suse.de }
2616a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
2627bb46a67Snpiggin@suse.de 
2637bb698f0SAndreas Gruenbacher int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
2647bb698f0SAndreas Gruenbacher 		unsigned int ia_valid)
2657bb698f0SAndreas Gruenbacher {
2667bb698f0SAndreas Gruenbacher 	int error;
2677bb698f0SAndreas Gruenbacher 
2687bb698f0SAndreas Gruenbacher 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
2697bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2707bb698f0SAndreas Gruenbacher 			return -EPERM;
2717bb698f0SAndreas Gruenbacher 	}
2727bb698f0SAndreas Gruenbacher 
2737bb698f0SAndreas Gruenbacher 	/*
2747bb698f0SAndreas Gruenbacher 	 * If utimes(2) and friends are called with times == NULL (or both
2757bb698f0SAndreas Gruenbacher 	 * times are UTIME_NOW), then we need to check for write permission
2767bb698f0SAndreas Gruenbacher 	 */
2777bb698f0SAndreas Gruenbacher 	if (ia_valid & ATTR_TOUCH) {
2787bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode))
2797bb698f0SAndreas Gruenbacher 			return -EPERM;
2807bb698f0SAndreas Gruenbacher 
2817bb698f0SAndreas Gruenbacher 		if (!inode_owner_or_capable(mnt_userns, inode)) {
2827bb698f0SAndreas Gruenbacher 			error = inode_permission(mnt_userns, inode, MAY_WRITE);
2837bb698f0SAndreas Gruenbacher 			if (error)
2847bb698f0SAndreas Gruenbacher 				return error;
2857bb698f0SAndreas Gruenbacher 		}
2867bb698f0SAndreas Gruenbacher 	}
2877bb698f0SAndreas Gruenbacher 	return 0;
2887bb698f0SAndreas Gruenbacher }
2897bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr);
2907bb698f0SAndreas Gruenbacher 
29127ac0ffeSJ. Bruce Fields /**
29227ac0ffeSJ. Bruce Fields  * notify_change - modify attributes of a filesytem object
2932f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
29427ac0ffeSJ. Bruce Fields  * @dentry:	object affected
2953fae1746SMatthew Wilcox  * @attr:	new attributes
29627ac0ffeSJ. Bruce Fields  * @delegated_inode: returns inode, if the inode is delegated
29727ac0ffeSJ. Bruce Fields  *
29827ac0ffeSJ. Bruce Fields  * The caller must hold the i_mutex on the affected object.
29927ac0ffeSJ. Bruce Fields  *
30027ac0ffeSJ. Bruce Fields  * If notify_change discovers a delegation in need of breaking,
30127ac0ffeSJ. Bruce Fields  * it will return -EWOULDBLOCK and return a reference to the inode in
30227ac0ffeSJ. Bruce Fields  * delegated_inode.  The caller should then break the delegation and
30327ac0ffeSJ. Bruce Fields  * retry.  Because breaking a delegation may take a long time, the
30427ac0ffeSJ. Bruce Fields  * caller should drop the i_mutex before doing so.
30527ac0ffeSJ. Bruce Fields  *
30627ac0ffeSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
30727ac0ffeSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
30827ac0ffeSJ. Bruce Fields  * to be NFS exported.  Also, passing NULL is fine for callers holding
30927ac0ffeSJ. Bruce Fields  * the file open for write, as there can be no conflicting delegation in
31027ac0ffeSJ. Bruce Fields  * that case.
3112f221d6fSChristian Brauner  *
3122f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
3132f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
3142f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
3152f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
3162f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
31727ac0ffeSJ. Bruce Fields  */
3182f221d6fSChristian Brauner int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
3192f221d6fSChristian Brauner 		  struct iattr *attr, struct inode **delegated_inode)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3228d334acdSAl Viro 	umode_t mode = inode->i_mode;
3231da177e4SLinus Torvalds 	int error;
32495582b00SDeepa Dinamani 	struct timespec64 now;
3251da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
3261da177e4SLinus Torvalds 
3275955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
328c4107b30SAndrew Morton 
3297bb698f0SAndreas Gruenbacher 	error = may_setattr(mnt_userns, inode, ia_valid);
330f2b20f6eSMiklos Szeredi 	if (error)
331f2b20f6eSMiklos Szeredi 		return error;
332f2b20f6eSMiklos Szeredi 
33369b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
3348d334acdSAl Viro 		umode_t amode = attr->ia_mode;
33569b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
33669b45732SAndi Kleen 		if (is_sxid(amode))
33769b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
33869b45732SAndi Kleen 	}
33969b45732SAndi Kleen 
340c2050a45SDeepa Dinamani 	now = current_time(inode);
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	attr->ia_ctime = now;
3431da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
3441da177e4SLinus Torvalds 		attr->ia_atime = now;
345eb31e2f6SAmir Goldstein 	else
346eb31e2f6SAmir Goldstein 		attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
3471da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
3481da177e4SLinus Torvalds 		attr->ia_mtime = now;
349eb31e2f6SAmir Goldstein 	else
350eb31e2f6SAmir Goldstein 		attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
351eb31e2f6SAmir Goldstein 
352b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
353b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
354030b533cSJan Kara 		if (error < 0)
355b5376771SSerge E. Hallyn 			return error;
356030b533cSJan Kara 		if (error == 0)
357030b533cSJan Kara 			ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
358b5376771SSerge E. Hallyn 	}
3596de0ec00SJeff Layton 
3606de0ec00SJeff Layton 	/*
3616de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
3626de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
3636de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
3646de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
3656de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
3666de0ec00SJeff Layton 	 */
3676de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
3686de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
3696de0ec00SJeff Layton 		BUG();
3706de0ec00SJeff Layton 
3711da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
3721da177e4SLinus Torvalds 		if (mode & S_ISUID) {
3731da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
3746de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
3781da177e4SLinus Torvalds 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
3791da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
3801da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
3811da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
3821da177e4SLinus Torvalds 			}
3831da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 	}
3866de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
3871da177e4SLinus Torvalds 		return 0;
3881da177e4SLinus Torvalds 
389a475acf0SSeth Forshee 	/*
390a475acf0SSeth Forshee 	 * Verify that uid/gid changes are valid in the target
391a475acf0SSeth Forshee 	 * namespace of the superblock.
392a475acf0SSeth Forshee 	 */
393a475acf0SSeth Forshee 	if (ia_valid & ATTR_UID &&
394b27c82e1SChristian Brauner 	    !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
395b27c82e1SChristian Brauner 				  attr->ia_vfsuid))
396a475acf0SSeth Forshee 		return -EOVERFLOW;
397a475acf0SSeth Forshee 	if (ia_valid & ATTR_GID &&
398b27c82e1SChristian Brauner 	    !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
399b27c82e1SChristian Brauner 				  attr->ia_vfsgid))
400a475acf0SSeth Forshee 		return -EOVERFLOW;
401a475acf0SSeth Forshee 
4020bd23d09SEric W. Biederman 	/* Don't allow modifications of files with invalid uids or
4030bd23d09SEric W. Biederman 	 * gids unless those uids & gids are being made valid.
4040bd23d09SEric W. Biederman 	 */
4052f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_UID) &&
406b27c82e1SChristian Brauner 	    !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
4070bd23d09SEric W. Biederman 		return -EOVERFLOW;
4082f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_GID) &&
409b27c82e1SChristian Brauner 	    !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
4100bd23d09SEric W. Biederman 		return -EOVERFLOW;
4110bd23d09SEric W. Biederman 
412b27c82e1SChristian Brauner 	error = security_inode_setattr(mnt_userns, dentry, attr);
413a77b72daSMiklos Szeredi 	if (error)
414a77b72daSMiklos Szeredi 		return error;
41527ac0ffeSJ. Bruce Fields 	error = try_break_deleg(inode, delegated_inode);
41627ac0ffeSJ. Bruce Fields 	if (error)
41727ac0ffeSJ. Bruce Fields 		return error;
418a77b72daSMiklos Szeredi 
419eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
420549c7297SChristian Brauner 		error = inode->i_op->setattr(mnt_userns, dentry, attr);
421eef2380cSChristoph Hellwig 	else
422549c7297SChristian Brauner 		error = simple_setattr(mnt_userns, dentry, attr);
4231da177e4SLinus Torvalds 
424975d2943SMimi Zohar 	if (!error) {
4250eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
426a2d2329eSChristian Brauner 		ima_inode_post_setattr(mnt_userns, dentry);
427975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
428975d2943SMimi Zohar 	}
4290eeca283SRobert Love 
4301da177e4SLinus Torvalds 	return error;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
433