xref: /openbmc/linux/fs/attr.c (revision e243e3f94c804ecca9a8241b5babe28f35258ef4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/attr.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  *  changes by Thomas Schoebel-Theuer
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
9630d9c47SPaul Gortmaker #include <linux/export.h>
101da177e4SLinus Torvalds #include <linux/time.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/string.h>
133f07c014SIngo Molnar #include <linux/sched/signal.h>
1416f7e0feSRandy Dunlap #include <linux/capability.h>
150eeca283SRobert Love #include <linux/fsnotify.h>
161da177e4SLinus Torvalds #include <linux/fcntl.h>
171da177e4SLinus Torvalds #include <linux/security.h>
18975d2943SMimi Zohar #include <linux/evm.h>
199957a504SMimi Zohar #include <linux/ima.h>
201da177e4SLinus Torvalds 
2111c2a870SChristian Brauner #include "internal.h"
2211c2a870SChristian Brauner 
23*e243e3f9SChristian Brauner /*
24*e243e3f9SChristian Brauner  * The logic we want is
25*e243e3f9SChristian Brauner  *
26*e243e3f9SChristian Brauner  *	if suid or (sgid and xgrp)
27*e243e3f9SChristian Brauner  *		remove privs
28*e243e3f9SChristian Brauner  */
29*e243e3f9SChristian Brauner int should_remove_suid(struct dentry *dentry)
30*e243e3f9SChristian Brauner {
31*e243e3f9SChristian Brauner 	umode_t mode = d_inode(dentry)->i_mode;
32*e243e3f9SChristian Brauner 	int kill = 0;
33*e243e3f9SChristian Brauner 
34*e243e3f9SChristian Brauner 	/* suid always must be killed */
35*e243e3f9SChristian Brauner 	if (unlikely(mode & S_ISUID))
36*e243e3f9SChristian Brauner 		kill = ATTR_KILL_SUID;
37*e243e3f9SChristian Brauner 
38*e243e3f9SChristian Brauner 	/*
39*e243e3f9SChristian Brauner 	 * sgid without any exec bits is just a mandatory locking mark; leave
40*e243e3f9SChristian Brauner 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
41*e243e3f9SChristian Brauner 	 */
42*e243e3f9SChristian Brauner 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
43*e243e3f9SChristian Brauner 		kill |= ATTR_KILL_SGID;
44*e243e3f9SChristian Brauner 
45*e243e3f9SChristian Brauner 	if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
46*e243e3f9SChristian Brauner 		return kill;
47*e243e3f9SChristian Brauner 
48*e243e3f9SChristian Brauner 	return 0;
49*e243e3f9SChristian Brauner }
50*e243e3f9SChristian Brauner EXPORT_SYMBOL(should_remove_suid);
51*e243e3f9SChristian Brauner 
522f221d6fSChristian Brauner /**
532f221d6fSChristian Brauner  * chown_ok - verify permissions to chown inode
542f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount @inode was found from
552f221d6fSChristian Brauner  * @inode:	inode to check permissions on
5681a1807dSChristian Brauner  * @ia_vfsuid:	uid to chown @inode to
572f221d6fSChristian Brauner  *
582f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
592f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
602f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
612f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
622f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
632f221d6fSChristian Brauner  */
642f221d6fSChristian Brauner static bool chown_ok(struct user_namespace *mnt_userns,
65b27c82e1SChristian Brauner 		     const struct inode *inode, vfsuid_t ia_vfsuid)
660031181cSEric W. Biederman {
67b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
68b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
69b27c82e1SChristian Brauner 	    vfsuid_eq(ia_vfsuid, vfsuid))
700031181cSEric W. Biederman 		return true;
712f221d6fSChristian Brauner 	if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
720031181cSEric W. Biederman 		return true;
73b27c82e1SChristian Brauner 	if (!vfsuid_valid(vfsuid) &&
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 
792f221d6fSChristian Brauner /**
802f221d6fSChristian Brauner  * chgrp_ok - verify permissions to chgrp inode
812f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount @inode was found from
822f221d6fSChristian Brauner  * @inode:	inode to check permissions on
8381a1807dSChristian Brauner  * @ia_vfsgid:	gid to chown @inode to
842f221d6fSChristian Brauner  *
852f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
862f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
872f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
882f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
892f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
902f221d6fSChristian Brauner  */
912f221d6fSChristian Brauner static bool chgrp_ok(struct user_namespace *mnt_userns,
92b27c82e1SChristian Brauner 		     const struct inode *inode, vfsgid_t ia_vfsgid)
930031181cSEric W. Biederman {
94b27c82e1SChristian Brauner 	vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
95b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
96b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
97b27c82e1SChristian Brauner 		if (vfsgid_eq(ia_vfsgid, vfsgid))
980031181cSEric W. Biederman 			return true;
99b27c82e1SChristian Brauner 		if (vfsgid_in_group_p(ia_vfsgid))
100168f9128SChristian Brauner 			return true;
101168f9128SChristian Brauner 	}
1022f221d6fSChristian Brauner 	if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
1030031181cSEric W. Biederman 		return true;
104b27c82e1SChristian Brauner 	if (!vfsgid_valid(vfsgid) &&
1050031181cSEric W. Biederman 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
1060031181cSEric W. Biederman 		return true;
1070031181cSEric W. Biederman 	return false;
1080031181cSEric W. Biederman }
1090031181cSEric W. Biederman 
1102c27c65eSChristoph Hellwig /**
11131051c85SJan Kara  * setattr_prepare - check if attribute changes to a dentry are allowed
1122f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
11331051c85SJan Kara  * @dentry:	dentry to check
1142c27c65eSChristoph Hellwig  * @attr:	attributes to change
1152c27c65eSChristoph Hellwig  *
1162c27c65eSChristoph Hellwig  * Check if we are allowed to change the attributes contained in @attr
11731051c85SJan Kara  * in the given dentry.  This includes the normal unix access permission
11831051c85SJan Kara  * checks, as well as checks for rlimits and others. The function also clears
11931051c85SJan Kara  * SGID bit from mode if user is not allowed to set it. Also file capabilities
12031051c85SJan Kara  * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
1212c27c65eSChristoph Hellwig  *
1222f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
1232f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
1242f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
1252f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
1262f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
1272f221d6fSChristian Brauner  *
1282c27c65eSChristoph Hellwig  * Should be called as the first thing in ->setattr implementations,
1292c27c65eSChristoph Hellwig  * possibly after taking additional locks.
1302c27c65eSChristoph Hellwig  */
1312f221d6fSChristian Brauner int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
1322f221d6fSChristian Brauner 		    struct iattr *attr)
1331da177e4SLinus Torvalds {
13431051c85SJan Kara 	struct inode *inode = d_inode(dentry);
1351da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1361da177e4SLinus Torvalds 
1372c27c65eSChristoph Hellwig 	/*
1382c27c65eSChristoph Hellwig 	 * First check size constraints.  These can't be overriden using
1392c27c65eSChristoph Hellwig 	 * ATTR_FORCE.
1402c27c65eSChristoph Hellwig 	 */
1412c27c65eSChristoph Hellwig 	if (ia_valid & ATTR_SIZE) {
1422c27c65eSChristoph Hellwig 		int error = inode_newsize_ok(inode, attr->ia_size);
1432c27c65eSChristoph Hellwig 		if (error)
1442c27c65eSChristoph Hellwig 			return error;
1452c27c65eSChristoph Hellwig 	}
1462c27c65eSChristoph Hellwig 
1471da177e4SLinus Torvalds 	/* If force is set do it anyway. */
1481da177e4SLinus Torvalds 	if (ia_valid & ATTR_FORCE)
149030b533cSJan Kara 		goto kill_priv;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	/* Make sure a caller can chown. */
152b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_UID) &&
153b27c82e1SChristian Brauner 	    !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
1542c27c65eSChristoph Hellwig 		return -EPERM;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	/* Make sure caller can chgrp. */
157b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_GID) &&
158b27c82e1SChristian Brauner 	    !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
1592c27c65eSChristoph Hellwig 		return -EPERM;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* Make sure a caller can chmod. */
1621da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
163b27c82e1SChristian Brauner 		vfsgid_t vfsgid;
164168f9128SChristian Brauner 
1652f221d6fSChristian Brauner 		if (!inode_owner_or_capable(mnt_userns, inode))
1662c27c65eSChristoph Hellwig 			return -EPERM;
167168f9128SChristian Brauner 
168168f9128SChristian Brauner 		if (ia_valid & ATTR_GID)
169b27c82e1SChristian Brauner 			vfsgid = attr->ia_vfsgid;
170168f9128SChristian Brauner 		else
171b27c82e1SChristian Brauner 			vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
172168f9128SChristian Brauner 
1731da177e4SLinus Torvalds 		/* Also check the setgid bit! */
17411c2a870SChristian Brauner 		if (!in_group_or_capable(mnt_userns, inode, vfsgid))
1751da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/* Check for setting the inode time. */
1799767d749SMiklos Szeredi 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
1802f221d6fSChristian Brauner 		if (!inode_owner_or_capable(mnt_userns, inode))
1812c27c65eSChristoph Hellwig 			return -EPERM;
1821da177e4SLinus Torvalds 	}
1832c27c65eSChristoph Hellwig 
184030b533cSJan Kara kill_priv:
185030b533cSJan Kara 	/* User has permission for the change */
186030b533cSJan Kara 	if (ia_valid & ATTR_KILL_PRIV) {
187030b533cSJan Kara 		int error;
188030b533cSJan Kara 
18971bc356fSChristian Brauner 		error = security_inode_killpriv(mnt_userns, dentry);
190030b533cSJan Kara 		if (error)
191030b533cSJan Kara 			return error;
192030b533cSJan Kara 	}
193030b533cSJan Kara 
1942c27c65eSChristoph Hellwig 	return 0;
1951da177e4SLinus Torvalds }
19631051c85SJan Kara EXPORT_SYMBOL(setattr_prepare);
1971da177e4SLinus Torvalds 
19825d9e2d1Snpiggin@suse.de /**
19925d9e2d1Snpiggin@suse.de  * inode_newsize_ok - may this inode be truncated to a given size
20025d9e2d1Snpiggin@suse.de  * @inode:	the inode to be truncated
20125d9e2d1Snpiggin@suse.de  * @offset:	the new size to assign to the inode
20225d9e2d1Snpiggin@suse.de  *
2037bb46a67Snpiggin@suse.de  * inode_newsize_ok must be called with i_mutex held.
2047bb46a67Snpiggin@suse.de  *
20525d9e2d1Snpiggin@suse.de  * inode_newsize_ok will check filesystem limits and ulimits to check that the
20625d9e2d1Snpiggin@suse.de  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
20725d9e2d1Snpiggin@suse.de  * when necessary. Caller must not proceed with inode size change if failure is
20825d9e2d1Snpiggin@suse.de  * returned. @inode must be a file (not directory), with appropriate
20925d9e2d1Snpiggin@suse.de  * permissions to allow truncate (inode_newsize_ok does NOT check these
21025d9e2d1Snpiggin@suse.de  * conditions).
2113fae1746SMatthew Wilcox  *
2123fae1746SMatthew Wilcox  * Return: 0 on success, -ve errno on failure
21325d9e2d1Snpiggin@suse.de  */
21425d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
21525d9e2d1Snpiggin@suse.de {
216e2ebff9cSDavid Howells 	if (offset < 0)
217e2ebff9cSDavid Howells 		return -EINVAL;
21825d9e2d1Snpiggin@suse.de 	if (inode->i_size < offset) {
21925d9e2d1Snpiggin@suse.de 		unsigned long limit;
22025d9e2d1Snpiggin@suse.de 
221d554ed89SJiri Slaby 		limit = rlimit(RLIMIT_FSIZE);
22225d9e2d1Snpiggin@suse.de 		if (limit != RLIM_INFINITY && offset > limit)
22325d9e2d1Snpiggin@suse.de 			goto out_sig;
22425d9e2d1Snpiggin@suse.de 		if (offset > inode->i_sb->s_maxbytes)
22525d9e2d1Snpiggin@suse.de 			goto out_big;
22625d9e2d1Snpiggin@suse.de 	} else {
22725d9e2d1Snpiggin@suse.de 		/*
22825d9e2d1Snpiggin@suse.de 		 * truncation of in-use swapfiles is disallowed - it would
22925d9e2d1Snpiggin@suse.de 		 * cause subsequent swapout to scribble on the now-freed
23025d9e2d1Snpiggin@suse.de 		 * blocks.
23125d9e2d1Snpiggin@suse.de 		 */
23225d9e2d1Snpiggin@suse.de 		if (IS_SWAPFILE(inode))
23325d9e2d1Snpiggin@suse.de 			return -ETXTBSY;
23425d9e2d1Snpiggin@suse.de 	}
23525d9e2d1Snpiggin@suse.de 
23625d9e2d1Snpiggin@suse.de 	return 0;
23725d9e2d1Snpiggin@suse.de out_sig:
23825d9e2d1Snpiggin@suse.de 	send_sig(SIGXFSZ, current, 0);
23925d9e2d1Snpiggin@suse.de out_big:
24025d9e2d1Snpiggin@suse.de 	return -EFBIG;
24125d9e2d1Snpiggin@suse.de }
24225d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
24325d9e2d1Snpiggin@suse.de 
2447bb46a67Snpiggin@suse.de /**
2456a1a90adSChristoph Hellwig  * setattr_copy - copy simple metadata updates into the generic inode
2462f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
2477bb46a67Snpiggin@suse.de  * @inode:	the inode to be updated
2487bb46a67Snpiggin@suse.de  * @attr:	the new attributes
2497bb46a67Snpiggin@suse.de  *
2506a1a90adSChristoph Hellwig  * setattr_copy must be called with i_mutex held.
2517bb46a67Snpiggin@suse.de  *
2526a1a90adSChristoph Hellwig  * setattr_copy updates the inode's metadata with that specified
253b27c82e1SChristian Brauner  * in attr on idmapped mounts. Necessary permission checks to determine
2542f221d6fSChristian Brauner  * whether or not the S_ISGID property needs to be removed are performed with
2552f221d6fSChristian Brauner  * the correct idmapped mount permission helpers.
2562f221d6fSChristian Brauner  * Noticeably missing is inode size update, which is more complex
2572c27c65eSChristoph Hellwig  * as it requires pagecache updates.
2587bb46a67Snpiggin@suse.de  *
2592f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
2602f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
2612f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
2622f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
2632f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
2642f221d6fSChristian Brauner  *
2657bb46a67Snpiggin@suse.de  * The inode is not marked as dirty after this operation. The rationale is
2667bb46a67Snpiggin@suse.de  * that for "simple" filesystems, the struct inode is the inode storage.
2677bb46a67Snpiggin@suse.de  * The caller is free to mark the inode dirty afterwards if needed.
2687bb46a67Snpiggin@suse.de  */
2692f221d6fSChristian Brauner void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
2702f221d6fSChristian Brauner 		  const struct iattr *attr)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
2731da177e4SLinus Torvalds 
274b27c82e1SChristian Brauner 	i_uid_update(mnt_userns, attr, inode);
275b27c82e1SChristian Brauner 	i_gid_update(mnt_userns, attr, inode);
276eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_ATIME)
277eb31e2f6SAmir Goldstein 		inode->i_atime = attr->ia_atime;
278eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_MTIME)
279eb31e2f6SAmir Goldstein 		inode->i_mtime = attr->ia_mtime;
280eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_CTIME)
281eb31e2f6SAmir Goldstein 		inode->i_ctime = attr->ia_ctime;
2821da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
2831da177e4SLinus Torvalds 		umode_t mode = attr->ia_mode;
28411c2a870SChristian Brauner 		if (!in_group_or_capable(mnt_userns, inode,
28511c2a870SChristian Brauner 					 i_gid_into_vfsgid(mnt_userns, inode)))
2861da177e4SLinus Torvalds 			mode &= ~S_ISGID;
2871da177e4SLinus Torvalds 		inode->i_mode = mode;
2881da177e4SLinus Torvalds 	}
2897bb46a67Snpiggin@suse.de }
2906a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
2917bb46a67Snpiggin@suse.de 
2927bb698f0SAndreas Gruenbacher int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
2937bb698f0SAndreas Gruenbacher 		unsigned int ia_valid)
2947bb698f0SAndreas Gruenbacher {
2957bb698f0SAndreas Gruenbacher 	int error;
2967bb698f0SAndreas Gruenbacher 
2977bb698f0SAndreas Gruenbacher 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
2987bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2997bb698f0SAndreas Gruenbacher 			return -EPERM;
3007bb698f0SAndreas Gruenbacher 	}
3017bb698f0SAndreas Gruenbacher 
3027bb698f0SAndreas Gruenbacher 	/*
3037bb698f0SAndreas Gruenbacher 	 * If utimes(2) and friends are called with times == NULL (or both
3047bb698f0SAndreas Gruenbacher 	 * times are UTIME_NOW), then we need to check for write permission
3057bb698f0SAndreas Gruenbacher 	 */
3067bb698f0SAndreas Gruenbacher 	if (ia_valid & ATTR_TOUCH) {
3077bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode))
3087bb698f0SAndreas Gruenbacher 			return -EPERM;
3097bb698f0SAndreas Gruenbacher 
3107bb698f0SAndreas Gruenbacher 		if (!inode_owner_or_capable(mnt_userns, inode)) {
3117bb698f0SAndreas Gruenbacher 			error = inode_permission(mnt_userns, inode, MAY_WRITE);
3127bb698f0SAndreas Gruenbacher 			if (error)
3137bb698f0SAndreas Gruenbacher 				return error;
3147bb698f0SAndreas Gruenbacher 		}
3157bb698f0SAndreas Gruenbacher 	}
3167bb698f0SAndreas Gruenbacher 	return 0;
3177bb698f0SAndreas Gruenbacher }
3187bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr);
3197bb698f0SAndreas Gruenbacher 
32027ac0ffeSJ. Bruce Fields /**
32127ac0ffeSJ. Bruce Fields  * notify_change - modify attributes of a filesytem object
3222f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
32327ac0ffeSJ. Bruce Fields  * @dentry:	object affected
3243fae1746SMatthew Wilcox  * @attr:	new attributes
32527ac0ffeSJ. Bruce Fields  * @delegated_inode: returns inode, if the inode is delegated
32627ac0ffeSJ. Bruce Fields  *
32727ac0ffeSJ. Bruce Fields  * The caller must hold the i_mutex on the affected object.
32827ac0ffeSJ. Bruce Fields  *
32927ac0ffeSJ. Bruce Fields  * If notify_change discovers a delegation in need of breaking,
33027ac0ffeSJ. Bruce Fields  * it will return -EWOULDBLOCK and return a reference to the inode in
33127ac0ffeSJ. Bruce Fields  * delegated_inode.  The caller should then break the delegation and
33227ac0ffeSJ. Bruce Fields  * retry.  Because breaking a delegation may take a long time, the
33327ac0ffeSJ. Bruce Fields  * caller should drop the i_mutex before doing so.
33427ac0ffeSJ. Bruce Fields  *
33527ac0ffeSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
33627ac0ffeSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
33727ac0ffeSJ. Bruce Fields  * to be NFS exported.  Also, passing NULL is fine for callers holding
33827ac0ffeSJ. Bruce Fields  * the file open for write, as there can be no conflicting delegation in
33927ac0ffeSJ. Bruce Fields  * that case.
3402f221d6fSChristian Brauner  *
3412f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
3422f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
3432f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
3442f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
3452f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
34627ac0ffeSJ. Bruce Fields  */
3472f221d6fSChristian Brauner int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
3482f221d6fSChristian Brauner 		  struct iattr *attr, struct inode **delegated_inode)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3518d334acdSAl Viro 	umode_t mode = inode->i_mode;
3521da177e4SLinus Torvalds 	int error;
35395582b00SDeepa Dinamani 	struct timespec64 now;
3541da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
3551da177e4SLinus Torvalds 
3565955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
357c4107b30SAndrew Morton 
3587bb698f0SAndreas Gruenbacher 	error = may_setattr(mnt_userns, inode, ia_valid);
359f2b20f6eSMiklos Szeredi 	if (error)
360f2b20f6eSMiklos Szeredi 		return error;
361f2b20f6eSMiklos Szeredi 
36269b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
3638d334acdSAl Viro 		umode_t amode = attr->ia_mode;
36469b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
36569b45732SAndi Kleen 		if (is_sxid(amode))
36669b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
36769b45732SAndi Kleen 	}
36869b45732SAndi Kleen 
369c2050a45SDeepa Dinamani 	now = current_time(inode);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	attr->ia_ctime = now;
3721da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
3731da177e4SLinus Torvalds 		attr->ia_atime = now;
374eb31e2f6SAmir Goldstein 	else
375eb31e2f6SAmir Goldstein 		attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
3761da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
3771da177e4SLinus Torvalds 		attr->ia_mtime = now;
378eb31e2f6SAmir Goldstein 	else
379eb31e2f6SAmir Goldstein 		attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
380eb31e2f6SAmir Goldstein 
381b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
382b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
383030b533cSJan Kara 		if (error < 0)
384b5376771SSerge E. Hallyn 			return error;
385030b533cSJan Kara 		if (error == 0)
386030b533cSJan Kara 			ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
387b5376771SSerge E. Hallyn 	}
3886de0ec00SJeff Layton 
3896de0ec00SJeff Layton 	/*
3906de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
3916de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
3926de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
3936de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
3946de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
3956de0ec00SJeff Layton 	 */
3966de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
3976de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
3986de0ec00SJeff Layton 		BUG();
3996de0ec00SJeff Layton 
4001da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
4011da177e4SLinus Torvalds 		if (mode & S_ISUID) {
4021da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
4036de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
4041da177e4SLinus Torvalds 		}
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
4071da177e4SLinus Torvalds 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
4081da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
4091da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
4101da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
4111da177e4SLinus Torvalds 			}
4121da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 	}
4156de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
4161da177e4SLinus Torvalds 		return 0;
4171da177e4SLinus Torvalds 
418a475acf0SSeth Forshee 	/*
419a475acf0SSeth Forshee 	 * Verify that uid/gid changes are valid in the target
420a475acf0SSeth Forshee 	 * namespace of the superblock.
421a475acf0SSeth Forshee 	 */
422a475acf0SSeth Forshee 	if (ia_valid & ATTR_UID &&
423b27c82e1SChristian Brauner 	    !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
424b27c82e1SChristian Brauner 				  attr->ia_vfsuid))
425a475acf0SSeth Forshee 		return -EOVERFLOW;
426a475acf0SSeth Forshee 	if (ia_valid & ATTR_GID &&
427b27c82e1SChristian Brauner 	    !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
428b27c82e1SChristian Brauner 				  attr->ia_vfsgid))
429a475acf0SSeth Forshee 		return -EOVERFLOW;
430a475acf0SSeth Forshee 
4310bd23d09SEric W. Biederman 	/* Don't allow modifications of files with invalid uids or
4320bd23d09SEric W. Biederman 	 * gids unless those uids & gids are being made valid.
4330bd23d09SEric W. Biederman 	 */
4342f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_UID) &&
435b27c82e1SChristian Brauner 	    !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
4360bd23d09SEric W. Biederman 		return -EOVERFLOW;
4372f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_GID) &&
438b27c82e1SChristian Brauner 	    !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
4390bd23d09SEric W. Biederman 		return -EOVERFLOW;
4400bd23d09SEric W. Biederman 
441b27c82e1SChristian Brauner 	error = security_inode_setattr(mnt_userns, dentry, attr);
442a77b72daSMiklos Szeredi 	if (error)
443a77b72daSMiklos Szeredi 		return error;
44427ac0ffeSJ. Bruce Fields 	error = try_break_deleg(inode, delegated_inode);
44527ac0ffeSJ. Bruce Fields 	if (error)
44627ac0ffeSJ. Bruce Fields 		return error;
447a77b72daSMiklos Szeredi 
448eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
449549c7297SChristian Brauner 		error = inode->i_op->setattr(mnt_userns, dentry, attr);
450eef2380cSChristoph Hellwig 	else
451549c7297SChristian Brauner 		error = simple_setattr(mnt_userns, dentry, attr);
4521da177e4SLinus Torvalds 
453975d2943SMimi Zohar 	if (!error) {
4540eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
455a2d2329eSChristian Brauner 		ima_inode_post_setattr(mnt_userns, dentry);
456975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
457975d2943SMimi Zohar 	}
4580eeca283SRobert Love 
4591da177e4SLinus Torvalds 	return error;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
462