xref: /openbmc/linux/fs/attr.c (revision 9452e93e6dae862d7aeff2b11236d79bde6f9b66)
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 
2372ae017cSChristian Brauner /**
2472ae017cSChristian Brauner  * setattr_should_drop_sgid - determine whether the setgid bit needs to be
2572ae017cSChristian Brauner  *                            removed
26*9452e93eSChristian Brauner  * @idmap:	idmap of the mount @inode was found from
2772ae017cSChristian Brauner  * @inode:	inode to check
2872ae017cSChristian Brauner  *
2972ae017cSChristian Brauner  * This function determines whether the setgid bit needs to be removed.
3072ae017cSChristian Brauner  * We retain backwards compatibility and require setgid bit to be removed
3172ae017cSChristian Brauner  * unconditionally if S_IXGRP is set. Otherwise we have the exact same
3272ae017cSChristian Brauner  * requirements as setattr_prepare() and setattr_copy().
3372ae017cSChristian Brauner  *
3472ae017cSChristian Brauner  * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
3572ae017cSChristian Brauner  */
36*9452e93eSChristian Brauner int setattr_should_drop_sgid(struct mnt_idmap *idmap,
3772ae017cSChristian Brauner 			     const struct inode *inode)
3872ae017cSChristian Brauner {
39*9452e93eSChristian Brauner 	struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
4072ae017cSChristian Brauner 	umode_t mode = inode->i_mode;
4172ae017cSChristian Brauner 
4272ae017cSChristian Brauner 	if (!(mode & S_ISGID))
4372ae017cSChristian Brauner 		return 0;
4472ae017cSChristian Brauner 	if (mode & S_IXGRP)
4572ae017cSChristian Brauner 		return ATTR_KILL_SGID;
46*9452e93eSChristian Brauner 	if (!in_group_or_capable(idmap, inode,
4772ae017cSChristian Brauner 				 i_gid_into_vfsgid(mnt_userns, inode)))
4872ae017cSChristian Brauner 		return ATTR_KILL_SGID;
4972ae017cSChristian Brauner 	return 0;
5072ae017cSChristian Brauner }
5172ae017cSChristian Brauner 
52ed5a7047SChristian Brauner /**
53ed5a7047SChristian Brauner  * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to
54ed5a7047SChristian Brauner  *                               be dropped
55*9452e93eSChristian Brauner  * @idmap:	idmap of the mount @inode was found from
56ed5a7047SChristian Brauner  * @inode:	inode to check
57e243e3f9SChristian Brauner  *
58ed5a7047SChristian Brauner  * This function determines whether the set{g,u}id bits need to be removed.
59ed5a7047SChristian Brauner  * If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the
60ed5a7047SChristian Brauner  * setgid bit needs to be removed ATTR_KILL_SGID is returned. If both
61ed5a7047SChristian Brauner  * set{g,u}id bits need to be removed the corresponding mask of both flags is
62ed5a7047SChristian Brauner  * returned.
63ed5a7047SChristian Brauner  *
64ed5a7047SChristian Brauner  * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits
65ed5a7047SChristian Brauner  * to remove, 0 otherwise.
66e243e3f9SChristian Brauner  */
67*9452e93eSChristian Brauner int setattr_should_drop_suidgid(struct mnt_idmap *idmap,
68ed5a7047SChristian Brauner 				struct inode *inode)
69e243e3f9SChristian Brauner {
70ed5a7047SChristian Brauner 	umode_t mode = inode->i_mode;
71e243e3f9SChristian Brauner 	int kill = 0;
72e243e3f9SChristian Brauner 
73e243e3f9SChristian Brauner 	/* suid always must be killed */
74e243e3f9SChristian Brauner 	if (unlikely(mode & S_ISUID))
75e243e3f9SChristian Brauner 		kill = ATTR_KILL_SUID;
76e243e3f9SChristian Brauner 
77*9452e93eSChristian Brauner 	kill |= setattr_should_drop_sgid(idmap, inode);
78e243e3f9SChristian Brauner 
79e243e3f9SChristian Brauner 	if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
80e243e3f9SChristian Brauner 		return kill;
81e243e3f9SChristian Brauner 
82e243e3f9SChristian Brauner 	return 0;
83e243e3f9SChristian Brauner }
84ed5a7047SChristian Brauner EXPORT_SYMBOL(setattr_should_drop_suidgid);
85e243e3f9SChristian Brauner 
862f221d6fSChristian Brauner /**
872f221d6fSChristian Brauner  * chown_ok - verify permissions to chown inode
88*9452e93eSChristian Brauner  * @idmap:	idmap of the mount @inode was found from
892f221d6fSChristian Brauner  * @inode:	inode to check permissions on
9081a1807dSChristian Brauner  * @ia_vfsuid:	uid to chown @inode to
912f221d6fSChristian Brauner  *
92*9452e93eSChristian Brauner  * If the inode has been found through an idmapped mount the idmap of
93*9452e93eSChristian Brauner  * the vfsmount must be passed through @idmap. This function will then
94*9452e93eSChristian Brauner  * take care to map the inode according to @idmap before checking
952f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
96*9452e93eSChristian Brauner  * performed on the raw inode simply pass @nop_mnt_idmap.
972f221d6fSChristian Brauner  */
98*9452e93eSChristian Brauner static bool chown_ok(struct mnt_idmap *idmap,
99b27c82e1SChristian Brauner 		     const struct inode *inode, vfsuid_t ia_vfsuid)
1000031181cSEric W. Biederman {
101*9452e93eSChristian Brauner 	struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
102*9452e93eSChristian Brauner 
103b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
104b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
105b27c82e1SChristian Brauner 	    vfsuid_eq(ia_vfsuid, vfsuid))
1060031181cSEric W. Biederman 		return true;
107*9452e93eSChristian Brauner 	if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
1080031181cSEric W. Biederman 		return true;
109b27c82e1SChristian Brauner 	if (!vfsuid_valid(vfsuid) &&
1100031181cSEric W. Biederman 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
1110031181cSEric W. Biederman 		return true;
1120031181cSEric W. Biederman 	return false;
1130031181cSEric W. Biederman }
1140031181cSEric W. Biederman 
1152f221d6fSChristian Brauner /**
1162f221d6fSChristian Brauner  * chgrp_ok - verify permissions to chgrp inode
117*9452e93eSChristian Brauner  * @idmap:	idmap of the mount @inode was found from
1182f221d6fSChristian Brauner  * @inode:	inode to check permissions on
11981a1807dSChristian Brauner  * @ia_vfsgid:	gid to chown @inode to
1202f221d6fSChristian Brauner  *
121*9452e93eSChristian Brauner  * If the inode has been found through an idmapped mount the idmap of
122*9452e93eSChristian Brauner  * the vfsmount must be passed through @idmap. This function will then
123*9452e93eSChristian Brauner  * take care to map the inode according to @idmap before checking
1242f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
125*9452e93eSChristian Brauner  * performed on the raw inode simply pass @nop_mnt_idmap.
1262f221d6fSChristian Brauner  */
127*9452e93eSChristian Brauner static bool chgrp_ok(struct mnt_idmap *idmap,
128b27c82e1SChristian Brauner 		     const struct inode *inode, vfsgid_t ia_vfsgid)
1290031181cSEric W. Biederman {
130*9452e93eSChristian Brauner 	struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
131*9452e93eSChristian Brauner 
132b27c82e1SChristian Brauner 	vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
133b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
134b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
135b27c82e1SChristian Brauner 		if (vfsgid_eq(ia_vfsgid, vfsgid))
1360031181cSEric W. Biederman 			return true;
137b27c82e1SChristian Brauner 		if (vfsgid_in_group_p(ia_vfsgid))
138168f9128SChristian Brauner 			return true;
139168f9128SChristian Brauner 	}
140*9452e93eSChristian Brauner 	if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
1410031181cSEric W. Biederman 		return true;
142b27c82e1SChristian Brauner 	if (!vfsgid_valid(vfsgid) &&
1430031181cSEric W. Biederman 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
1440031181cSEric W. Biederman 		return true;
1450031181cSEric W. Biederman 	return false;
1460031181cSEric W. Biederman }
1470031181cSEric W. Biederman 
1482c27c65eSChristoph Hellwig /**
14931051c85SJan Kara  * setattr_prepare - check if attribute changes to a dentry are allowed
150c1632a0fSChristian Brauner  * @idmap:	idmap of the mount the inode was found from
15131051c85SJan Kara  * @dentry:	dentry to check
1522c27c65eSChristoph Hellwig  * @attr:	attributes to change
1532c27c65eSChristoph Hellwig  *
1542c27c65eSChristoph Hellwig  * Check if we are allowed to change the attributes contained in @attr
15531051c85SJan Kara  * in the given dentry.  This includes the normal unix access permission
15631051c85SJan Kara  * checks, as well as checks for rlimits and others. The function also clears
15731051c85SJan Kara  * SGID bit from mode if user is not allowed to set it. Also file capabilities
15831051c85SJan Kara  * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
1592c27c65eSChristoph Hellwig  *
160c1632a0fSChristian Brauner  * If the inode has been found through an idmapped mount the idmap of
161c1632a0fSChristian Brauner  * the vfsmount must be passed through @idmap. This function will then
162c1632a0fSChristian Brauner  * take care to map the inode according to @idmap before checking
1632f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
164c1632a0fSChristian Brauner  * performed on the raw inode simply passs @nop_mnt_idmap.
1652f221d6fSChristian Brauner  *
1662c27c65eSChristoph Hellwig  * Should be called as the first thing in ->setattr implementations,
1672c27c65eSChristoph Hellwig  * possibly after taking additional locks.
1682c27c65eSChristoph Hellwig  */
169c1632a0fSChristian Brauner int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,
1702f221d6fSChristian Brauner 		    struct iattr *attr)
1711da177e4SLinus Torvalds {
172c1632a0fSChristian Brauner 	struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
17331051c85SJan Kara 	struct inode *inode = d_inode(dentry);
1741da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1751da177e4SLinus Torvalds 
1762c27c65eSChristoph Hellwig 	/*
1772c27c65eSChristoph Hellwig 	 * First check size constraints.  These can't be overriden using
1782c27c65eSChristoph Hellwig 	 * ATTR_FORCE.
1792c27c65eSChristoph Hellwig 	 */
1802c27c65eSChristoph Hellwig 	if (ia_valid & ATTR_SIZE) {
1812c27c65eSChristoph Hellwig 		int error = inode_newsize_ok(inode, attr->ia_size);
1822c27c65eSChristoph Hellwig 		if (error)
1832c27c65eSChristoph Hellwig 			return error;
1842c27c65eSChristoph Hellwig 	}
1852c27c65eSChristoph Hellwig 
1861da177e4SLinus Torvalds 	/* If force is set do it anyway. */
1871da177e4SLinus Torvalds 	if (ia_valid & ATTR_FORCE)
188030b533cSJan Kara 		goto kill_priv;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	/* Make sure a caller can chown. */
191b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_UID) &&
192*9452e93eSChristian Brauner 	    !chown_ok(idmap, inode, attr->ia_vfsuid))
1932c27c65eSChristoph Hellwig 		return -EPERM;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/* Make sure caller can chgrp. */
196b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_GID) &&
197*9452e93eSChristian Brauner 	    !chgrp_ok(idmap, inode, attr->ia_vfsgid))
1982c27c65eSChristoph Hellwig 		return -EPERM;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	/* Make sure a caller can chmod. */
2011da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
202b27c82e1SChristian Brauner 		vfsgid_t vfsgid;
203168f9128SChristian Brauner 
20401beba79SChristian Brauner 		if (!inode_owner_or_capable(idmap, inode))
2052c27c65eSChristoph Hellwig 			return -EPERM;
206168f9128SChristian Brauner 
207168f9128SChristian Brauner 		if (ia_valid & ATTR_GID)
208b27c82e1SChristian Brauner 			vfsgid = attr->ia_vfsgid;
209168f9128SChristian Brauner 		else
210b27c82e1SChristian Brauner 			vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
211168f9128SChristian Brauner 
2121da177e4SLinus Torvalds 		/* Also check the setgid bit! */
213*9452e93eSChristian Brauner 		if (!in_group_or_capable(idmap, inode, vfsgid))
2141da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* Check for setting the inode time. */
2189767d749SMiklos Szeredi 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
21901beba79SChristian Brauner 		if (!inode_owner_or_capable(idmap, inode))
2202c27c65eSChristoph Hellwig 			return -EPERM;
2211da177e4SLinus Torvalds 	}
2222c27c65eSChristoph Hellwig 
223030b533cSJan Kara kill_priv:
224030b533cSJan Kara 	/* User has permission for the change */
225030b533cSJan Kara 	if (ia_valid & ATTR_KILL_PRIV) {
226030b533cSJan Kara 		int error;
227030b533cSJan Kara 
22839f60c1cSChristian Brauner 		error = security_inode_killpriv(idmap, dentry);
229030b533cSJan Kara 		if (error)
230030b533cSJan Kara 			return error;
231030b533cSJan Kara 	}
232030b533cSJan Kara 
2332c27c65eSChristoph Hellwig 	return 0;
2341da177e4SLinus Torvalds }
23531051c85SJan Kara EXPORT_SYMBOL(setattr_prepare);
2361da177e4SLinus Torvalds 
23725d9e2d1Snpiggin@suse.de /**
23825d9e2d1Snpiggin@suse.de  * inode_newsize_ok - may this inode be truncated to a given size
23925d9e2d1Snpiggin@suse.de  * @inode:	the inode to be truncated
24025d9e2d1Snpiggin@suse.de  * @offset:	the new size to assign to the inode
24125d9e2d1Snpiggin@suse.de  *
2427bb46a67Snpiggin@suse.de  * inode_newsize_ok must be called with i_mutex held.
2437bb46a67Snpiggin@suse.de  *
24425d9e2d1Snpiggin@suse.de  * inode_newsize_ok will check filesystem limits and ulimits to check that the
24525d9e2d1Snpiggin@suse.de  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
24625d9e2d1Snpiggin@suse.de  * when necessary. Caller must not proceed with inode size change if failure is
24725d9e2d1Snpiggin@suse.de  * returned. @inode must be a file (not directory), with appropriate
24825d9e2d1Snpiggin@suse.de  * permissions to allow truncate (inode_newsize_ok does NOT check these
24925d9e2d1Snpiggin@suse.de  * conditions).
2503fae1746SMatthew Wilcox  *
2513fae1746SMatthew Wilcox  * Return: 0 on success, -ve errno on failure
25225d9e2d1Snpiggin@suse.de  */
25325d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
25425d9e2d1Snpiggin@suse.de {
255e2ebff9cSDavid Howells 	if (offset < 0)
256e2ebff9cSDavid Howells 		return -EINVAL;
25725d9e2d1Snpiggin@suse.de 	if (inode->i_size < offset) {
25825d9e2d1Snpiggin@suse.de 		unsigned long limit;
25925d9e2d1Snpiggin@suse.de 
260d554ed89SJiri Slaby 		limit = rlimit(RLIMIT_FSIZE);
26125d9e2d1Snpiggin@suse.de 		if (limit != RLIM_INFINITY && offset > limit)
26225d9e2d1Snpiggin@suse.de 			goto out_sig;
26325d9e2d1Snpiggin@suse.de 		if (offset > inode->i_sb->s_maxbytes)
26425d9e2d1Snpiggin@suse.de 			goto out_big;
26525d9e2d1Snpiggin@suse.de 	} else {
26625d9e2d1Snpiggin@suse.de 		/*
26725d9e2d1Snpiggin@suse.de 		 * truncation of in-use swapfiles is disallowed - it would
26825d9e2d1Snpiggin@suse.de 		 * cause subsequent swapout to scribble on the now-freed
26925d9e2d1Snpiggin@suse.de 		 * blocks.
27025d9e2d1Snpiggin@suse.de 		 */
27125d9e2d1Snpiggin@suse.de 		if (IS_SWAPFILE(inode))
27225d9e2d1Snpiggin@suse.de 			return -ETXTBSY;
27325d9e2d1Snpiggin@suse.de 	}
27425d9e2d1Snpiggin@suse.de 
27525d9e2d1Snpiggin@suse.de 	return 0;
27625d9e2d1Snpiggin@suse.de out_sig:
27725d9e2d1Snpiggin@suse.de 	send_sig(SIGXFSZ, current, 0);
27825d9e2d1Snpiggin@suse.de out_big:
27925d9e2d1Snpiggin@suse.de 	return -EFBIG;
28025d9e2d1Snpiggin@suse.de }
28125d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
28225d9e2d1Snpiggin@suse.de 
2837bb46a67Snpiggin@suse.de /**
2846a1a90adSChristoph Hellwig  * setattr_copy - copy simple metadata updates into the generic inode
285c1632a0fSChristian Brauner  * @idmap:	idmap of the mount the inode was found from
2867bb46a67Snpiggin@suse.de  * @inode:	the inode to be updated
2877bb46a67Snpiggin@suse.de  * @attr:	the new attributes
2887bb46a67Snpiggin@suse.de  *
2896a1a90adSChristoph Hellwig  * setattr_copy must be called with i_mutex held.
2907bb46a67Snpiggin@suse.de  *
2916a1a90adSChristoph Hellwig  * setattr_copy updates the inode's metadata with that specified
292b27c82e1SChristian Brauner  * in attr on idmapped mounts. Necessary permission checks to determine
2932f221d6fSChristian Brauner  * whether or not the S_ISGID property needs to be removed are performed with
2942f221d6fSChristian Brauner  * the correct idmapped mount permission helpers.
2952f221d6fSChristian Brauner  * Noticeably missing is inode size update, which is more complex
2962c27c65eSChristoph Hellwig  * as it requires pagecache updates.
2977bb46a67Snpiggin@suse.de  *
298c1632a0fSChristian Brauner  * If the inode has been found through an idmapped mount the idmap of
299c1632a0fSChristian Brauner  * the vfsmount must be passed through @idmap. This function will then
300c1632a0fSChristian Brauner  * take care to map the inode according to @idmap before checking
3012f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
302c1632a0fSChristian Brauner  * performed on the raw inode simply pass @nop_mnt_idmap.
3032f221d6fSChristian Brauner  *
3047bb46a67Snpiggin@suse.de  * The inode is not marked as dirty after this operation. The rationale is
3057bb46a67Snpiggin@suse.de  * that for "simple" filesystems, the struct inode is the inode storage.
3067bb46a67Snpiggin@suse.de  * The caller is free to mark the inode dirty afterwards if needed.
3077bb46a67Snpiggin@suse.de  */
308c1632a0fSChristian Brauner void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,
3092f221d6fSChristian Brauner 		  const struct iattr *attr)
3101da177e4SLinus Torvalds {
311c1632a0fSChristian Brauner 	struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
3121da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
3131da177e4SLinus Torvalds 
314b27c82e1SChristian Brauner 	i_uid_update(mnt_userns, attr, inode);
315b27c82e1SChristian Brauner 	i_gid_update(mnt_userns, attr, inode);
316eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_ATIME)
317eb31e2f6SAmir Goldstein 		inode->i_atime = attr->ia_atime;
318eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_MTIME)
319eb31e2f6SAmir Goldstein 		inode->i_mtime = attr->ia_mtime;
320eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_CTIME)
321eb31e2f6SAmir Goldstein 		inode->i_ctime = attr->ia_ctime;
3221da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
3231da177e4SLinus Torvalds 		umode_t mode = attr->ia_mode;
324*9452e93eSChristian Brauner 		if (!in_group_or_capable(idmap, inode,
32511c2a870SChristian Brauner 					 i_gid_into_vfsgid(mnt_userns, inode)))
3261da177e4SLinus Torvalds 			mode &= ~S_ISGID;
3271da177e4SLinus Torvalds 		inode->i_mode = mode;
3281da177e4SLinus Torvalds 	}
3297bb46a67Snpiggin@suse.de }
3306a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
3317bb46a67Snpiggin@suse.de 
3324609e1f1SChristian Brauner int may_setattr(struct mnt_idmap *idmap, struct inode *inode,
3337bb698f0SAndreas Gruenbacher 		unsigned int ia_valid)
3347bb698f0SAndreas Gruenbacher {
3357bb698f0SAndreas Gruenbacher 	int error;
3367bb698f0SAndreas Gruenbacher 
3377bb698f0SAndreas Gruenbacher 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
3387bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
3397bb698f0SAndreas Gruenbacher 			return -EPERM;
3407bb698f0SAndreas Gruenbacher 	}
3417bb698f0SAndreas Gruenbacher 
3427bb698f0SAndreas Gruenbacher 	/*
3437bb698f0SAndreas Gruenbacher 	 * If utimes(2) and friends are called with times == NULL (or both
3447bb698f0SAndreas Gruenbacher 	 * times are UTIME_NOW), then we need to check for write permission
3457bb698f0SAndreas Gruenbacher 	 */
3467bb698f0SAndreas Gruenbacher 	if (ia_valid & ATTR_TOUCH) {
3477bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode))
3487bb698f0SAndreas Gruenbacher 			return -EPERM;
3497bb698f0SAndreas Gruenbacher 
35001beba79SChristian Brauner 		if (!inode_owner_or_capable(idmap, inode)) {
3514609e1f1SChristian Brauner 			error = inode_permission(idmap, inode, MAY_WRITE);
3527bb698f0SAndreas Gruenbacher 			if (error)
3537bb698f0SAndreas Gruenbacher 				return error;
3547bb698f0SAndreas Gruenbacher 		}
3557bb698f0SAndreas Gruenbacher 	}
3567bb698f0SAndreas Gruenbacher 	return 0;
3577bb698f0SAndreas Gruenbacher }
3587bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr);
3597bb698f0SAndreas Gruenbacher 
36027ac0ffeSJ. Bruce Fields /**
36127ac0ffeSJ. Bruce Fields  * notify_change - modify attributes of a filesytem object
362abf08576SChristian Brauner  * @idmap:	idmap of the mount the inode was found from
36327ac0ffeSJ. Bruce Fields  * @dentry:	object affected
3643fae1746SMatthew Wilcox  * @attr:	new attributes
36527ac0ffeSJ. Bruce Fields  * @delegated_inode: returns inode, if the inode is delegated
36627ac0ffeSJ. Bruce Fields  *
36727ac0ffeSJ. Bruce Fields  * The caller must hold the i_mutex on the affected object.
36827ac0ffeSJ. Bruce Fields  *
36927ac0ffeSJ. Bruce Fields  * If notify_change discovers a delegation in need of breaking,
37027ac0ffeSJ. Bruce Fields  * it will return -EWOULDBLOCK and return a reference to the inode in
37127ac0ffeSJ. Bruce Fields  * delegated_inode.  The caller should then break the delegation and
37227ac0ffeSJ. Bruce Fields  * retry.  Because breaking a delegation may take a long time, the
37327ac0ffeSJ. Bruce Fields  * caller should drop the i_mutex before doing so.
37427ac0ffeSJ. Bruce Fields  *
37527ac0ffeSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
37627ac0ffeSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
37727ac0ffeSJ. Bruce Fields  * to be NFS exported.  Also, passing NULL is fine for callers holding
37827ac0ffeSJ. Bruce Fields  * the file open for write, as there can be no conflicting delegation in
37927ac0ffeSJ. Bruce Fields  * that case.
3802f221d6fSChristian Brauner  *
381abf08576SChristian Brauner  * If the inode has been found through an idmapped mount the idmap of
382abf08576SChristian Brauner  * the vfsmount must be passed through @idmap. This function will then
383abf08576SChristian Brauner  * take care to map the inode according to @idmap before checking
3842f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
385abf08576SChristian Brauner  * performed on the raw inode simply pass @nop_mnt_idmap.
38627ac0ffeSJ. Bruce Fields  */
387abf08576SChristian Brauner int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,
3882f221d6fSChristian Brauner 		  struct iattr *attr, struct inode **delegated_inode)
3891da177e4SLinus Torvalds {
390abf08576SChristian Brauner 	struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
3911da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3928d334acdSAl Viro 	umode_t mode = inode->i_mode;
3931da177e4SLinus Torvalds 	int error;
39495582b00SDeepa Dinamani 	struct timespec64 now;
3951da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
3961da177e4SLinus Torvalds 
3975955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
398c4107b30SAndrew Morton 
3994609e1f1SChristian Brauner 	error = may_setattr(idmap, inode, ia_valid);
400f2b20f6eSMiklos Szeredi 	if (error)
401f2b20f6eSMiklos Szeredi 		return error;
402f2b20f6eSMiklos Szeredi 
40369b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
4048d334acdSAl Viro 		umode_t amode = attr->ia_mode;
40569b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
40669b45732SAndi Kleen 		if (is_sxid(amode))
40769b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
40869b45732SAndi Kleen 	}
40969b45732SAndi Kleen 
410c2050a45SDeepa Dinamani 	now = current_time(inode);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	attr->ia_ctime = now;
4131da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
4141da177e4SLinus Torvalds 		attr->ia_atime = now;
415eb31e2f6SAmir Goldstein 	else
416eb31e2f6SAmir Goldstein 		attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
4171da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
4181da177e4SLinus Torvalds 		attr->ia_mtime = now;
419eb31e2f6SAmir Goldstein 	else
420eb31e2f6SAmir Goldstein 		attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
421eb31e2f6SAmir Goldstein 
422b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
423b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
424030b533cSJan Kara 		if (error < 0)
425b5376771SSerge E. Hallyn 			return error;
426030b533cSJan Kara 		if (error == 0)
427030b533cSJan Kara 			ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
428b5376771SSerge E. Hallyn 	}
4296de0ec00SJeff Layton 
4306de0ec00SJeff Layton 	/*
4316de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
4326de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
4336de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
4346de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
4356de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
4366de0ec00SJeff Layton 	 */
4376de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
4386de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
4396de0ec00SJeff Layton 		BUG();
4406de0ec00SJeff Layton 
4411da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
4421da177e4SLinus Torvalds 		if (mode & S_ISUID) {
4431da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
4446de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
4451da177e4SLinus Torvalds 		}
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
448ed5a7047SChristian Brauner 		if (mode & S_ISGID) {
4491da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
4501da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
4511da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
4521da177e4SLinus Torvalds 			}
4531da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
4541da177e4SLinus Torvalds 		}
4551da177e4SLinus Torvalds 	}
4566de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
4571da177e4SLinus Torvalds 		return 0;
4581da177e4SLinus Torvalds 
459a475acf0SSeth Forshee 	/*
460a475acf0SSeth Forshee 	 * Verify that uid/gid changes are valid in the target
461a475acf0SSeth Forshee 	 * namespace of the superblock.
462a475acf0SSeth Forshee 	 */
463a475acf0SSeth Forshee 	if (ia_valid & ATTR_UID &&
464b27c82e1SChristian Brauner 	    !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
465b27c82e1SChristian Brauner 				  attr->ia_vfsuid))
466a475acf0SSeth Forshee 		return -EOVERFLOW;
467a475acf0SSeth Forshee 	if (ia_valid & ATTR_GID &&
468b27c82e1SChristian Brauner 	    !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
469b27c82e1SChristian Brauner 				  attr->ia_vfsgid))
470a475acf0SSeth Forshee 		return -EOVERFLOW;
471a475acf0SSeth Forshee 
4720bd23d09SEric W. Biederman 	/* Don't allow modifications of files with invalid uids or
4730bd23d09SEric W. Biederman 	 * gids unless those uids & gids are being made valid.
4740bd23d09SEric W. Biederman 	 */
4752f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_UID) &&
476b27c82e1SChristian Brauner 	    !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
4770bd23d09SEric W. Biederman 		return -EOVERFLOW;
4782f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_GID) &&
479b27c82e1SChristian Brauner 	    !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
4800bd23d09SEric W. Biederman 		return -EOVERFLOW;
4810bd23d09SEric W. Biederman 
482c1632a0fSChristian Brauner 	error = security_inode_setattr(idmap, dentry, attr);
483a77b72daSMiklos Szeredi 	if (error)
484a77b72daSMiklos Szeredi 		return error;
48527ac0ffeSJ. Bruce Fields 	error = try_break_deleg(inode, delegated_inode);
48627ac0ffeSJ. Bruce Fields 	if (error)
48727ac0ffeSJ. Bruce Fields 		return error;
488a77b72daSMiklos Szeredi 
489eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
490c1632a0fSChristian Brauner 		error = inode->i_op->setattr(idmap, dentry, attr);
491eef2380cSChristoph Hellwig 	else
492c1632a0fSChristian Brauner 		error = simple_setattr(idmap, dentry, attr);
4931da177e4SLinus Torvalds 
494975d2943SMimi Zohar 	if (!error) {
4950eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
49639f60c1cSChristian Brauner 		ima_inode_post_setattr(idmap, dentry);
497975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
498975d2943SMimi Zohar 	}
4990eeca283SRobert Love 
5001da177e4SLinus Torvalds 	return error;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
503