xref: /openbmc/linux/fs/attr.c (revision 72ae017c5451860443a16fb2a8c243bff3e396b8)
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*72ae017cSChristian Brauner /**
24*72ae017cSChristian Brauner  * setattr_should_drop_sgid - determine whether the setgid bit needs to be
25*72ae017cSChristian Brauner  *                            removed
26*72ae017cSChristian Brauner  * @mnt_userns:	user namespace of the mount @inode was found from
27*72ae017cSChristian Brauner  * @inode:	inode to check
28*72ae017cSChristian Brauner  *
29*72ae017cSChristian Brauner  * This function determines whether the setgid bit needs to be removed.
30*72ae017cSChristian Brauner  * We retain backwards compatibility and require setgid bit to be removed
31*72ae017cSChristian Brauner  * unconditionally if S_IXGRP is set. Otherwise we have the exact same
32*72ae017cSChristian Brauner  * requirements as setattr_prepare() and setattr_copy().
33*72ae017cSChristian Brauner  *
34*72ae017cSChristian Brauner  * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
35*72ae017cSChristian Brauner  */
36*72ae017cSChristian Brauner int setattr_should_drop_sgid(struct user_namespace *mnt_userns,
37*72ae017cSChristian Brauner 			     const struct inode *inode)
38*72ae017cSChristian Brauner {
39*72ae017cSChristian Brauner 	umode_t mode = inode->i_mode;
40*72ae017cSChristian Brauner 
41*72ae017cSChristian Brauner 	if (!(mode & S_ISGID))
42*72ae017cSChristian Brauner 		return 0;
43*72ae017cSChristian Brauner 	if (mode & S_IXGRP)
44*72ae017cSChristian Brauner 		return ATTR_KILL_SGID;
45*72ae017cSChristian Brauner 	if (!in_group_or_capable(mnt_userns, inode,
46*72ae017cSChristian Brauner 				 i_gid_into_vfsgid(mnt_userns, inode)))
47*72ae017cSChristian Brauner 		return ATTR_KILL_SGID;
48*72ae017cSChristian Brauner 	return 0;
49*72ae017cSChristian Brauner }
50*72ae017cSChristian Brauner 
51e243e3f9SChristian Brauner /*
52e243e3f9SChristian Brauner  * The logic we want is
53e243e3f9SChristian Brauner  *
54e243e3f9SChristian Brauner  *	if suid or (sgid and xgrp)
55e243e3f9SChristian Brauner  *		remove privs
56e243e3f9SChristian Brauner  */
57e243e3f9SChristian Brauner int should_remove_suid(struct dentry *dentry)
58e243e3f9SChristian Brauner {
59e243e3f9SChristian Brauner 	umode_t mode = d_inode(dentry)->i_mode;
60e243e3f9SChristian Brauner 	int kill = 0;
61e243e3f9SChristian Brauner 
62e243e3f9SChristian Brauner 	/* suid always must be killed */
63e243e3f9SChristian Brauner 	if (unlikely(mode & S_ISUID))
64e243e3f9SChristian Brauner 		kill = ATTR_KILL_SUID;
65e243e3f9SChristian Brauner 
66e243e3f9SChristian Brauner 	/*
67e243e3f9SChristian Brauner 	 * sgid without any exec bits is just a mandatory locking mark; leave
68e243e3f9SChristian Brauner 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
69e243e3f9SChristian Brauner 	 */
70e243e3f9SChristian Brauner 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
71e243e3f9SChristian Brauner 		kill |= ATTR_KILL_SGID;
72e243e3f9SChristian Brauner 
73e243e3f9SChristian Brauner 	if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
74e243e3f9SChristian Brauner 		return kill;
75e243e3f9SChristian Brauner 
76e243e3f9SChristian Brauner 	return 0;
77e243e3f9SChristian Brauner }
78e243e3f9SChristian Brauner EXPORT_SYMBOL(should_remove_suid);
79e243e3f9SChristian Brauner 
802f221d6fSChristian Brauner /**
812f221d6fSChristian Brauner  * chown_ok - verify permissions to chown inode
822f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount @inode was found from
832f221d6fSChristian Brauner  * @inode:	inode to check permissions on
8481a1807dSChristian Brauner  * @ia_vfsuid:	uid to chown @inode to
852f221d6fSChristian Brauner  *
862f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
872f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
882f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
892f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
902f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
912f221d6fSChristian Brauner  */
922f221d6fSChristian Brauner static bool chown_ok(struct user_namespace *mnt_userns,
93b27c82e1SChristian Brauner 		     const struct inode *inode, vfsuid_t ia_vfsuid)
940031181cSEric W. Biederman {
95b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
96b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
97b27c82e1SChristian Brauner 	    vfsuid_eq(ia_vfsuid, vfsuid))
980031181cSEric W. Biederman 		return true;
992f221d6fSChristian Brauner 	if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
1000031181cSEric W. Biederman 		return true;
101b27c82e1SChristian Brauner 	if (!vfsuid_valid(vfsuid) &&
1020031181cSEric W. Biederman 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
1030031181cSEric W. Biederman 		return true;
1040031181cSEric W. Biederman 	return false;
1050031181cSEric W. Biederman }
1060031181cSEric W. Biederman 
1072f221d6fSChristian Brauner /**
1082f221d6fSChristian Brauner  * chgrp_ok - verify permissions to chgrp inode
1092f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount @inode was found from
1102f221d6fSChristian Brauner  * @inode:	inode to check permissions on
11181a1807dSChristian Brauner  * @ia_vfsgid:	gid to chown @inode to
1122f221d6fSChristian Brauner  *
1132f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
1142f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
1152f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
1162f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
1172f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
1182f221d6fSChristian Brauner  */
1192f221d6fSChristian Brauner static bool chgrp_ok(struct user_namespace *mnt_userns,
120b27c82e1SChristian Brauner 		     const struct inode *inode, vfsgid_t ia_vfsgid)
1210031181cSEric W. Biederman {
122b27c82e1SChristian Brauner 	vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
123b27c82e1SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
124b27c82e1SChristian Brauner 	if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
125b27c82e1SChristian Brauner 		if (vfsgid_eq(ia_vfsgid, vfsgid))
1260031181cSEric W. Biederman 			return true;
127b27c82e1SChristian Brauner 		if (vfsgid_in_group_p(ia_vfsgid))
128168f9128SChristian Brauner 			return true;
129168f9128SChristian Brauner 	}
1302f221d6fSChristian Brauner 	if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
1310031181cSEric W. Biederman 		return true;
132b27c82e1SChristian Brauner 	if (!vfsgid_valid(vfsgid) &&
1330031181cSEric W. Biederman 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
1340031181cSEric W. Biederman 		return true;
1350031181cSEric W. Biederman 	return false;
1360031181cSEric W. Biederman }
1370031181cSEric W. Biederman 
1382c27c65eSChristoph Hellwig /**
13931051c85SJan Kara  * setattr_prepare - check if attribute changes to a dentry are allowed
1402f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
14131051c85SJan Kara  * @dentry:	dentry to check
1422c27c65eSChristoph Hellwig  * @attr:	attributes to change
1432c27c65eSChristoph Hellwig  *
1442c27c65eSChristoph Hellwig  * Check if we are allowed to change the attributes contained in @attr
14531051c85SJan Kara  * in the given dentry.  This includes the normal unix access permission
14631051c85SJan Kara  * checks, as well as checks for rlimits and others. The function also clears
14731051c85SJan Kara  * SGID bit from mode if user is not allowed to set it. Also file capabilities
14831051c85SJan Kara  * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
1492c27c65eSChristoph Hellwig  *
1502f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
1512f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
1522f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
1532f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
1542f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
1552f221d6fSChristian Brauner  *
1562c27c65eSChristoph Hellwig  * Should be called as the first thing in ->setattr implementations,
1572c27c65eSChristoph Hellwig  * possibly after taking additional locks.
1582c27c65eSChristoph Hellwig  */
1592f221d6fSChristian Brauner int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
1602f221d6fSChristian Brauner 		    struct iattr *attr)
1611da177e4SLinus Torvalds {
16231051c85SJan Kara 	struct inode *inode = d_inode(dentry);
1631da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1641da177e4SLinus Torvalds 
1652c27c65eSChristoph Hellwig 	/*
1662c27c65eSChristoph Hellwig 	 * First check size constraints.  These can't be overriden using
1672c27c65eSChristoph Hellwig 	 * ATTR_FORCE.
1682c27c65eSChristoph Hellwig 	 */
1692c27c65eSChristoph Hellwig 	if (ia_valid & ATTR_SIZE) {
1702c27c65eSChristoph Hellwig 		int error = inode_newsize_ok(inode, attr->ia_size);
1712c27c65eSChristoph Hellwig 		if (error)
1722c27c65eSChristoph Hellwig 			return error;
1732c27c65eSChristoph Hellwig 	}
1742c27c65eSChristoph Hellwig 
1751da177e4SLinus Torvalds 	/* If force is set do it anyway. */
1761da177e4SLinus Torvalds 	if (ia_valid & ATTR_FORCE)
177030b533cSJan Kara 		goto kill_priv;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* Make sure a caller can chown. */
180b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_UID) &&
181b27c82e1SChristian Brauner 	    !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
1822c27c65eSChristoph Hellwig 		return -EPERM;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	/* Make sure caller can chgrp. */
185b27c82e1SChristian Brauner 	if ((ia_valid & ATTR_GID) &&
186b27c82e1SChristian Brauner 	    !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
1872c27c65eSChristoph Hellwig 		return -EPERM;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/* Make sure a caller can chmod. */
1901da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
191b27c82e1SChristian Brauner 		vfsgid_t vfsgid;
192168f9128SChristian Brauner 
1932f221d6fSChristian Brauner 		if (!inode_owner_or_capable(mnt_userns, inode))
1942c27c65eSChristoph Hellwig 			return -EPERM;
195168f9128SChristian Brauner 
196168f9128SChristian Brauner 		if (ia_valid & ATTR_GID)
197b27c82e1SChristian Brauner 			vfsgid = attr->ia_vfsgid;
198168f9128SChristian Brauner 		else
199b27c82e1SChristian Brauner 			vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
200168f9128SChristian Brauner 
2011da177e4SLinus Torvalds 		/* Also check the setgid bit! */
20211c2a870SChristian Brauner 		if (!in_group_or_capable(mnt_userns, inode, vfsgid))
2031da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/* Check for setting the inode time. */
2079767d749SMiklos Szeredi 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
2082f221d6fSChristian Brauner 		if (!inode_owner_or_capable(mnt_userns, inode))
2092c27c65eSChristoph Hellwig 			return -EPERM;
2101da177e4SLinus Torvalds 	}
2112c27c65eSChristoph Hellwig 
212030b533cSJan Kara kill_priv:
213030b533cSJan Kara 	/* User has permission for the change */
214030b533cSJan Kara 	if (ia_valid & ATTR_KILL_PRIV) {
215030b533cSJan Kara 		int error;
216030b533cSJan Kara 
21771bc356fSChristian Brauner 		error = security_inode_killpriv(mnt_userns, dentry);
218030b533cSJan Kara 		if (error)
219030b533cSJan Kara 			return error;
220030b533cSJan Kara 	}
221030b533cSJan Kara 
2222c27c65eSChristoph Hellwig 	return 0;
2231da177e4SLinus Torvalds }
22431051c85SJan Kara EXPORT_SYMBOL(setattr_prepare);
2251da177e4SLinus Torvalds 
22625d9e2d1Snpiggin@suse.de /**
22725d9e2d1Snpiggin@suse.de  * inode_newsize_ok - may this inode be truncated to a given size
22825d9e2d1Snpiggin@suse.de  * @inode:	the inode to be truncated
22925d9e2d1Snpiggin@suse.de  * @offset:	the new size to assign to the inode
23025d9e2d1Snpiggin@suse.de  *
2317bb46a67Snpiggin@suse.de  * inode_newsize_ok must be called with i_mutex held.
2327bb46a67Snpiggin@suse.de  *
23325d9e2d1Snpiggin@suse.de  * inode_newsize_ok will check filesystem limits and ulimits to check that the
23425d9e2d1Snpiggin@suse.de  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
23525d9e2d1Snpiggin@suse.de  * when necessary. Caller must not proceed with inode size change if failure is
23625d9e2d1Snpiggin@suse.de  * returned. @inode must be a file (not directory), with appropriate
23725d9e2d1Snpiggin@suse.de  * permissions to allow truncate (inode_newsize_ok does NOT check these
23825d9e2d1Snpiggin@suse.de  * conditions).
2393fae1746SMatthew Wilcox  *
2403fae1746SMatthew Wilcox  * Return: 0 on success, -ve errno on failure
24125d9e2d1Snpiggin@suse.de  */
24225d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
24325d9e2d1Snpiggin@suse.de {
244e2ebff9cSDavid Howells 	if (offset < 0)
245e2ebff9cSDavid Howells 		return -EINVAL;
24625d9e2d1Snpiggin@suse.de 	if (inode->i_size < offset) {
24725d9e2d1Snpiggin@suse.de 		unsigned long limit;
24825d9e2d1Snpiggin@suse.de 
249d554ed89SJiri Slaby 		limit = rlimit(RLIMIT_FSIZE);
25025d9e2d1Snpiggin@suse.de 		if (limit != RLIM_INFINITY && offset > limit)
25125d9e2d1Snpiggin@suse.de 			goto out_sig;
25225d9e2d1Snpiggin@suse.de 		if (offset > inode->i_sb->s_maxbytes)
25325d9e2d1Snpiggin@suse.de 			goto out_big;
25425d9e2d1Snpiggin@suse.de 	} else {
25525d9e2d1Snpiggin@suse.de 		/*
25625d9e2d1Snpiggin@suse.de 		 * truncation of in-use swapfiles is disallowed - it would
25725d9e2d1Snpiggin@suse.de 		 * cause subsequent swapout to scribble on the now-freed
25825d9e2d1Snpiggin@suse.de 		 * blocks.
25925d9e2d1Snpiggin@suse.de 		 */
26025d9e2d1Snpiggin@suse.de 		if (IS_SWAPFILE(inode))
26125d9e2d1Snpiggin@suse.de 			return -ETXTBSY;
26225d9e2d1Snpiggin@suse.de 	}
26325d9e2d1Snpiggin@suse.de 
26425d9e2d1Snpiggin@suse.de 	return 0;
26525d9e2d1Snpiggin@suse.de out_sig:
26625d9e2d1Snpiggin@suse.de 	send_sig(SIGXFSZ, current, 0);
26725d9e2d1Snpiggin@suse.de out_big:
26825d9e2d1Snpiggin@suse.de 	return -EFBIG;
26925d9e2d1Snpiggin@suse.de }
27025d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
27125d9e2d1Snpiggin@suse.de 
2727bb46a67Snpiggin@suse.de /**
2736a1a90adSChristoph Hellwig  * setattr_copy - copy simple metadata updates into the generic inode
2742f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
2757bb46a67Snpiggin@suse.de  * @inode:	the inode to be updated
2767bb46a67Snpiggin@suse.de  * @attr:	the new attributes
2777bb46a67Snpiggin@suse.de  *
2786a1a90adSChristoph Hellwig  * setattr_copy must be called with i_mutex held.
2797bb46a67Snpiggin@suse.de  *
2806a1a90adSChristoph Hellwig  * setattr_copy updates the inode's metadata with that specified
281b27c82e1SChristian Brauner  * in attr on idmapped mounts. Necessary permission checks to determine
2822f221d6fSChristian Brauner  * whether or not the S_ISGID property needs to be removed are performed with
2832f221d6fSChristian Brauner  * the correct idmapped mount permission helpers.
2842f221d6fSChristian Brauner  * Noticeably missing is inode size update, which is more complex
2852c27c65eSChristoph Hellwig  * as it requires pagecache updates.
2867bb46a67Snpiggin@suse.de  *
2872f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
2882f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
2892f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
2902f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
2912f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
2922f221d6fSChristian Brauner  *
2937bb46a67Snpiggin@suse.de  * The inode is not marked as dirty after this operation. The rationale is
2947bb46a67Snpiggin@suse.de  * that for "simple" filesystems, the struct inode is the inode storage.
2957bb46a67Snpiggin@suse.de  * The caller is free to mark the inode dirty afterwards if needed.
2967bb46a67Snpiggin@suse.de  */
2972f221d6fSChristian Brauner void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
2982f221d6fSChristian Brauner 		  const struct iattr *attr)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
3011da177e4SLinus Torvalds 
302b27c82e1SChristian Brauner 	i_uid_update(mnt_userns, attr, inode);
303b27c82e1SChristian Brauner 	i_gid_update(mnt_userns, attr, inode);
304eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_ATIME)
305eb31e2f6SAmir Goldstein 		inode->i_atime = attr->ia_atime;
306eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_MTIME)
307eb31e2f6SAmir Goldstein 		inode->i_mtime = attr->ia_mtime;
308eb31e2f6SAmir Goldstein 	if (ia_valid & ATTR_CTIME)
309eb31e2f6SAmir Goldstein 		inode->i_ctime = attr->ia_ctime;
3101da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
3111da177e4SLinus Torvalds 		umode_t mode = attr->ia_mode;
31211c2a870SChristian Brauner 		if (!in_group_or_capable(mnt_userns, inode,
31311c2a870SChristian Brauner 					 i_gid_into_vfsgid(mnt_userns, inode)))
3141da177e4SLinus Torvalds 			mode &= ~S_ISGID;
3151da177e4SLinus Torvalds 		inode->i_mode = mode;
3161da177e4SLinus Torvalds 	}
3177bb46a67Snpiggin@suse.de }
3186a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
3197bb46a67Snpiggin@suse.de 
3207bb698f0SAndreas Gruenbacher int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
3217bb698f0SAndreas Gruenbacher 		unsigned int ia_valid)
3227bb698f0SAndreas Gruenbacher {
3237bb698f0SAndreas Gruenbacher 	int error;
3247bb698f0SAndreas Gruenbacher 
3257bb698f0SAndreas Gruenbacher 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
3267bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
3277bb698f0SAndreas Gruenbacher 			return -EPERM;
3287bb698f0SAndreas Gruenbacher 	}
3297bb698f0SAndreas Gruenbacher 
3307bb698f0SAndreas Gruenbacher 	/*
3317bb698f0SAndreas Gruenbacher 	 * If utimes(2) and friends are called with times == NULL (or both
3327bb698f0SAndreas Gruenbacher 	 * times are UTIME_NOW), then we need to check for write permission
3337bb698f0SAndreas Gruenbacher 	 */
3347bb698f0SAndreas Gruenbacher 	if (ia_valid & ATTR_TOUCH) {
3357bb698f0SAndreas Gruenbacher 		if (IS_IMMUTABLE(inode))
3367bb698f0SAndreas Gruenbacher 			return -EPERM;
3377bb698f0SAndreas Gruenbacher 
3387bb698f0SAndreas Gruenbacher 		if (!inode_owner_or_capable(mnt_userns, inode)) {
3397bb698f0SAndreas Gruenbacher 			error = inode_permission(mnt_userns, inode, MAY_WRITE);
3407bb698f0SAndreas Gruenbacher 			if (error)
3417bb698f0SAndreas Gruenbacher 				return error;
3427bb698f0SAndreas Gruenbacher 		}
3437bb698f0SAndreas Gruenbacher 	}
3447bb698f0SAndreas Gruenbacher 	return 0;
3457bb698f0SAndreas Gruenbacher }
3467bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr);
3477bb698f0SAndreas Gruenbacher 
34827ac0ffeSJ. Bruce Fields /**
34927ac0ffeSJ. Bruce Fields  * notify_change - modify attributes of a filesytem object
3502f221d6fSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
35127ac0ffeSJ. Bruce Fields  * @dentry:	object affected
3523fae1746SMatthew Wilcox  * @attr:	new attributes
35327ac0ffeSJ. Bruce Fields  * @delegated_inode: returns inode, if the inode is delegated
35427ac0ffeSJ. Bruce Fields  *
35527ac0ffeSJ. Bruce Fields  * The caller must hold the i_mutex on the affected object.
35627ac0ffeSJ. Bruce Fields  *
35727ac0ffeSJ. Bruce Fields  * If notify_change discovers a delegation in need of breaking,
35827ac0ffeSJ. Bruce Fields  * it will return -EWOULDBLOCK and return a reference to the inode in
35927ac0ffeSJ. Bruce Fields  * delegated_inode.  The caller should then break the delegation and
36027ac0ffeSJ. Bruce Fields  * retry.  Because breaking a delegation may take a long time, the
36127ac0ffeSJ. Bruce Fields  * caller should drop the i_mutex before doing so.
36227ac0ffeSJ. Bruce Fields  *
36327ac0ffeSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
36427ac0ffeSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
36527ac0ffeSJ. Bruce Fields  * to be NFS exported.  Also, passing NULL is fine for callers holding
36627ac0ffeSJ. Bruce Fields  * the file open for write, as there can be no conflicting delegation in
36727ac0ffeSJ. Bruce Fields  * that case.
3682f221d6fSChristian Brauner  *
3692f221d6fSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
3702f221d6fSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
3712f221d6fSChristian Brauner  * take care to map the inode according to @mnt_userns before checking
3722f221d6fSChristian Brauner  * permissions. On non-idmapped mounts or if permission checking is to be
3732f221d6fSChristian Brauner  * performed on the raw inode simply passs init_user_ns.
37427ac0ffeSJ. Bruce Fields  */
3752f221d6fSChristian Brauner int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
3762f221d6fSChristian Brauner 		  struct iattr *attr, struct inode **delegated_inode)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3798d334acdSAl Viro 	umode_t mode = inode->i_mode;
3801da177e4SLinus Torvalds 	int error;
38195582b00SDeepa Dinamani 	struct timespec64 now;
3821da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
3831da177e4SLinus Torvalds 
3845955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
385c4107b30SAndrew Morton 
3867bb698f0SAndreas Gruenbacher 	error = may_setattr(mnt_userns, inode, ia_valid);
387f2b20f6eSMiklos Szeredi 	if (error)
388f2b20f6eSMiklos Szeredi 		return error;
389f2b20f6eSMiklos Szeredi 
39069b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
3918d334acdSAl Viro 		umode_t amode = attr->ia_mode;
39269b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
39369b45732SAndi Kleen 		if (is_sxid(amode))
39469b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
39569b45732SAndi Kleen 	}
39669b45732SAndi Kleen 
397c2050a45SDeepa Dinamani 	now = current_time(inode);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	attr->ia_ctime = now;
4001da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
4011da177e4SLinus Torvalds 		attr->ia_atime = now;
402eb31e2f6SAmir Goldstein 	else
403eb31e2f6SAmir Goldstein 		attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
4041da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
4051da177e4SLinus Torvalds 		attr->ia_mtime = now;
406eb31e2f6SAmir Goldstein 	else
407eb31e2f6SAmir Goldstein 		attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
408eb31e2f6SAmir Goldstein 
409b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
410b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
411030b533cSJan Kara 		if (error < 0)
412b5376771SSerge E. Hallyn 			return error;
413030b533cSJan Kara 		if (error == 0)
414030b533cSJan Kara 			ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
415b5376771SSerge E. Hallyn 	}
4166de0ec00SJeff Layton 
4176de0ec00SJeff Layton 	/*
4186de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
4196de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
4206de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
4216de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
4226de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
4236de0ec00SJeff Layton 	 */
4246de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
4256de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
4266de0ec00SJeff Layton 		BUG();
4276de0ec00SJeff Layton 
4281da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
4291da177e4SLinus Torvalds 		if (mode & S_ISUID) {
4301da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
4316de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
4321da177e4SLinus Torvalds 		}
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
4351da177e4SLinus Torvalds 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
4361da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
4371da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
4381da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
4391da177e4SLinus Torvalds 			}
4401da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
4411da177e4SLinus Torvalds 		}
4421da177e4SLinus Torvalds 	}
4436de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
4441da177e4SLinus Torvalds 		return 0;
4451da177e4SLinus Torvalds 
446a475acf0SSeth Forshee 	/*
447a475acf0SSeth Forshee 	 * Verify that uid/gid changes are valid in the target
448a475acf0SSeth Forshee 	 * namespace of the superblock.
449a475acf0SSeth Forshee 	 */
450a475acf0SSeth Forshee 	if (ia_valid & ATTR_UID &&
451b27c82e1SChristian Brauner 	    !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
452b27c82e1SChristian Brauner 				  attr->ia_vfsuid))
453a475acf0SSeth Forshee 		return -EOVERFLOW;
454a475acf0SSeth Forshee 	if (ia_valid & ATTR_GID &&
455b27c82e1SChristian Brauner 	    !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
456b27c82e1SChristian Brauner 				  attr->ia_vfsgid))
457a475acf0SSeth Forshee 		return -EOVERFLOW;
458a475acf0SSeth Forshee 
4590bd23d09SEric W. Biederman 	/* Don't allow modifications of files with invalid uids or
4600bd23d09SEric W. Biederman 	 * gids unless those uids & gids are being made valid.
4610bd23d09SEric W. Biederman 	 */
4622f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_UID) &&
463b27c82e1SChristian Brauner 	    !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
4640bd23d09SEric W. Biederman 		return -EOVERFLOW;
4652f221d6fSChristian Brauner 	if (!(ia_valid & ATTR_GID) &&
466b27c82e1SChristian Brauner 	    !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
4670bd23d09SEric W. Biederman 		return -EOVERFLOW;
4680bd23d09SEric W. Biederman 
469b27c82e1SChristian Brauner 	error = security_inode_setattr(mnt_userns, dentry, attr);
470a77b72daSMiklos Szeredi 	if (error)
471a77b72daSMiklos Szeredi 		return error;
47227ac0ffeSJ. Bruce Fields 	error = try_break_deleg(inode, delegated_inode);
47327ac0ffeSJ. Bruce Fields 	if (error)
47427ac0ffeSJ. Bruce Fields 		return error;
475a77b72daSMiklos Szeredi 
476eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
477549c7297SChristian Brauner 		error = inode->i_op->setattr(mnt_userns, dentry, attr);
478eef2380cSChristoph Hellwig 	else
479549c7297SChristian Brauner 		error = simple_setattr(mnt_userns, dentry, attr);
4801da177e4SLinus Torvalds 
481975d2943SMimi Zohar 	if (!error) {
4820eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
483a2d2329eSChristian Brauner 		ima_inode_post_setattr(mnt_userns, dentry);
484975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
485975d2943SMimi Zohar 	}
4860eeca283SRobert Love 
4871da177e4SLinus Torvalds 	return error;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
490