xref: /openbmc/linux/fs/attr.c (revision 7bb698f09bdd01fbb6d48c14bb1dde556dc1af00)
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 
252*7bb698f0SAndreas Gruenbacher int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
253*7bb698f0SAndreas Gruenbacher 		unsigned int ia_valid)
254*7bb698f0SAndreas Gruenbacher {
255*7bb698f0SAndreas Gruenbacher 	int error;
256*7bb698f0SAndreas Gruenbacher 
257*7bb698f0SAndreas Gruenbacher 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
258*7bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
259*7bb698f0SAndreas Gruenbacher 			return -EPERM;
260*7bb698f0SAndreas Gruenbacher 	}
261*7bb698f0SAndreas Gruenbacher 
262*7bb698f0SAndreas Gruenbacher 	/*
263*7bb698f0SAndreas Gruenbacher 	 * If utimes(2) and friends are called with times == NULL (or both
264*7bb698f0SAndreas Gruenbacher 	 * times are UTIME_NOW), then we need to check for write permission
265*7bb698f0SAndreas Gruenbacher 	 */
266*7bb698f0SAndreas Gruenbacher 	if (ia_valid & ATTR_TOUCH) {
267*7bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode))
268*7bb698f0SAndreas Gruenbacher 			return -EPERM;
269*7bb698f0SAndreas Gruenbacher 
270*7bb698f0SAndreas Gruenbacher 		if (!inode_owner_or_capable(mnt_userns, inode)) {
271*7bb698f0SAndreas Gruenbacher 			error = inode_permission(mnt_userns, inode, MAY_WRITE);
272*7bb698f0SAndreas Gruenbacher 			if (error)
273*7bb698f0SAndreas Gruenbacher 				return error;
274*7bb698f0SAndreas Gruenbacher 		}
275*7bb698f0SAndreas Gruenbacher 	}
276*7bb698f0SAndreas Gruenbacher 	return 0;
277*7bb698f0SAndreas Gruenbacher }
278*7bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr);
279*7bb698f0SAndreas Gruenbacher 
28027ac0ffeSJ. Bruce Fields /**
28127ac0ffeSJ. Bruce Fields  * notify_change - modify attributes of a filesytem object
2822f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
28327ac0ffeSJ. Bruce Fields  * @dentry:	object affected
2843fae1746SMatthew Wilcox  * @attr:	new attributes
28527ac0ffeSJ. Bruce Fields  * @delegated_inode: returns inode, if the inode is delegated
28627ac0ffeSJ. Bruce Fields  *
28727ac0ffeSJ. Bruce Fields  * The caller must hold the i_mutex on the affected object.
28827ac0ffeSJ. Bruce Fields  *
28927ac0ffeSJ. Bruce Fields  * If notify_change discovers a delegation in need of breaking,
29027ac0ffeSJ. Bruce Fields  * it will return -EWOULDBLOCK and return a reference to the inode in
29127ac0ffeSJ. Bruce Fields  * delegated_inode.  The caller should then break the delegation and
29227ac0ffeSJ. Bruce Fields  * retry.  Because breaking a delegation may take a long time, the
29327ac0ffeSJ. Bruce Fields  * caller should drop the i_mutex before doing so.
29427ac0ffeSJ. Bruce Fields  *
2952f221d6fSChristian Brauner  * If file ownership is changed notify_change() doesn't map ia_uid and
2962f221d6fSChristian Brauner  * ia_gid. It will asssume the caller has already provided the intended values.
2972f221d6fSChristian Brauner  *
29827ac0ffeSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
29927ac0ffeSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
30027ac0ffeSJ. Bruce Fields  * to be NFS exported.  Also, passing NULL is fine for callers holding
30127ac0ffeSJ. Bruce Fields  * the file open for write, as there can be no conflicting delegation in
30227ac0ffeSJ. Bruce Fields  * that case.
3032f221d6fSChristian Brauner  *
3042f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
3052f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
3062f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
3072f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
3082f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
30927ac0ffeSJ. Bruce Fields  */
3102f221d6fSChristian Brauner int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
3112f221d6fSChristian Brauner 		  struct iattr *attr, struct inode **delegated_inode)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3148d334acdSAl Viro 	umode_t mode = inode->i_mode;
3151da177e4SLinus Torvalds 	int error;
31695582b00SDeepa Dinamani 	struct timespec64 now;
3171da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
3181da177e4SLinus Torvalds 
3195955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
320c4107b30SAndrew Morton 
321*7bb698f0SAndreas Gruenbacher 	error = may_setattr(mnt_userns, inode, ia_valid);
322f2b20f6eSMiklos Szeredi 	if (error)
323f2b20f6eSMiklos Szeredi 		return error;
324f2b20f6eSMiklos Szeredi 
32569b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
3268d334acdSAl Viro 		umode_t amode = attr->ia_mode;
32769b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
32869b45732SAndi Kleen 		if (is_sxid(amode))
32969b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
33069b45732SAndi Kleen 	}
33169b45732SAndi Kleen 
332c2050a45SDeepa Dinamani 	now = current_time(inode);
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	attr->ia_ctime = now;
3351da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
3361da177e4SLinus Torvalds 		attr->ia_atime = now;
337eb31e2f6SAmir Goldstein 	else
338eb31e2f6SAmir Goldstein 		attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
3391da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
3401da177e4SLinus Torvalds 		attr->ia_mtime = now;
341eb31e2f6SAmir Goldstein 	else
342eb31e2f6SAmir Goldstein 		attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
343eb31e2f6SAmir Goldstein 
344b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
345b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
346030b533cSJan Kara 		if (error < 0)
347b5376771SSerge E. Hallyn 			return error;
348030b533cSJan Kara 		if (error == 0)
349030b533cSJan Kara 			ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
350b5376771SSerge E. Hallyn 	}
3516de0ec00SJeff Layton 
3526de0ec00SJeff Layton 	/*
3536de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
3546de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
3556de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
3566de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
3576de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
3586de0ec00SJeff Layton 	 */
3596de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
3606de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
3616de0ec00SJeff Layton 		BUG();
3626de0ec00SJeff Layton 
3631da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
3641da177e4SLinus Torvalds 		if (mode & S_ISUID) {
3651da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
3666de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
3671da177e4SLinus Torvalds 		}
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
3701da177e4SLinus Torvalds 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
3711da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
3721da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
3731da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
3741da177e4SLinus Torvalds 			}
3751da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
3761da177e4SLinus Torvalds 		}
3771da177e4SLinus Torvalds 	}
3786de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
3791da177e4SLinus Torvalds 		return 0;
3801da177e4SLinus Torvalds 
381a475acf0SSeth Forshee 	/*
382a475acf0SSeth Forshee 	 * Verify that uid/gid changes are valid in the target
383a475acf0SSeth Forshee 	 * namespace of the superblock.
384a475acf0SSeth Forshee 	 */
385a475acf0SSeth Forshee 	if (ia_valid & ATTR_UID &&
386a475acf0SSeth Forshee 	    !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
387a475acf0SSeth Forshee 		return -EOVERFLOW;
388a475acf0SSeth Forshee 	if (ia_valid & ATTR_GID &&
389a475acf0SSeth Forshee 	    !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
390a475acf0SSeth Forshee 		return -EOVERFLOW;
391a475acf0SSeth Forshee 
3920bd23d09SEric W. Biederman 	/* Don't allow modifications of files with invalid uids or
3930bd23d09SEric W. Biederman 	 * gids unless those uids & gids are being made valid.
3940bd23d09SEric W. Biederman 	 */
3952f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_UID) &&
3962f221d6fSChristian Brauner 	    !uid_valid(i_uid_into_mnt(mnt_userns, inode)))
3970bd23d09SEric W. Biederman 		return -EOVERFLOW;
3982f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_GID) &&
3992f221d6fSChristian Brauner 	    !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
4000bd23d09SEric W. Biederman 		return -EOVERFLOW;
4010bd23d09SEric W. Biederman 
402a77b72daSMiklos Szeredi 	error = security_inode_setattr(dentry, attr);
403a77b72daSMiklos Szeredi 	if (error)
404a77b72daSMiklos Szeredi 		return error;
40527ac0ffeSJ. Bruce Fields 	error = try_break_deleg(inode, delegated_inode);
40627ac0ffeSJ. Bruce Fields 	if (error)
40727ac0ffeSJ. Bruce Fields 		return error;
408a77b72daSMiklos Szeredi 
409eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
410549c7297SChristian Brauner 		error = inode->i_op->setattr(mnt_userns, dentry, attr);
411eef2380cSChristoph Hellwig 	else
412549c7297SChristian Brauner 		error = simple_setattr(mnt_userns, dentry, attr);
4131da177e4SLinus Torvalds 
414975d2943SMimi Zohar 	if (!error) {
4150eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
416a2d2329eSChristian Brauner 		ima_inode_post_setattr(mnt_userns, dentry);
417975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
418975d2943SMimi Zohar 	}
4190eeca283SRobert Love 
4201da177e4SLinus Torvalds 	return error;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
423