xref: /openbmc/linux/fs/attr.c (revision f2b20f6ee842313a0d681dbbf7f87b70291a6a3b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/attr.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  *  changes by Thomas Schoebel-Theuer
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
8630d9c47SPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include <linux/time.h>
101da177e4SLinus Torvalds #include <linux/mm.h>
111da177e4SLinus Torvalds #include <linux/string.h>
1216f7e0feSRandy Dunlap #include <linux/capability.h>
130eeca283SRobert Love #include <linux/fsnotify.h>
141da177e4SLinus Torvalds #include <linux/fcntl.h>
151da177e4SLinus Torvalds #include <linux/security.h>
16975d2943SMimi Zohar #include <linux/evm.h>
179957a504SMimi Zohar #include <linux/ima.h>
181da177e4SLinus Torvalds 
192c27c65eSChristoph Hellwig /**
202c27c65eSChristoph Hellwig  * inode_change_ok - check if attribute changes to an inode are allowed
212c27c65eSChristoph Hellwig  * @inode:	inode to check
222c27c65eSChristoph Hellwig  * @attr:	attributes to change
232c27c65eSChristoph Hellwig  *
242c27c65eSChristoph Hellwig  * Check if we are allowed to change the attributes contained in @attr
252c27c65eSChristoph Hellwig  * in the given inode.  This includes the normal unix access permission
262c27c65eSChristoph Hellwig  * checks, as well as checks for rlimits and others.
272c27c65eSChristoph Hellwig  *
282c27c65eSChristoph Hellwig  * Should be called as the first thing in ->setattr implementations,
292c27c65eSChristoph Hellwig  * possibly after taking additional locks.
302c27c65eSChristoph Hellwig  */
3125d9e2d1Snpiggin@suse.de int inode_change_ok(const struct inode *inode, struct iattr *attr)
321da177e4SLinus Torvalds {
331da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
341da177e4SLinus Torvalds 
352c27c65eSChristoph Hellwig 	/*
362c27c65eSChristoph Hellwig 	 * First check size constraints.  These can't be overriden using
372c27c65eSChristoph Hellwig 	 * ATTR_FORCE.
382c27c65eSChristoph Hellwig 	 */
392c27c65eSChristoph Hellwig 	if (ia_valid & ATTR_SIZE) {
402c27c65eSChristoph Hellwig 		int error = inode_newsize_ok(inode, attr->ia_size);
412c27c65eSChristoph Hellwig 		if (error)
422c27c65eSChristoph Hellwig 			return error;
432c27c65eSChristoph Hellwig 	}
442c27c65eSChristoph Hellwig 
451da177e4SLinus Torvalds 	/* If force is set do it anyway. */
461da177e4SLinus Torvalds 	if (ia_valid & ATTR_FORCE)
472c27c65eSChristoph Hellwig 		return 0;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	/* Make sure a caller can chown. */
501da177e4SLinus Torvalds 	if ((ia_valid & ATTR_UID) &&
518e96e3b7SEric W. Biederman 	    (!uid_eq(current_fsuid(), inode->i_uid) ||
527fa294c8SEric W. Biederman 	     !uid_eq(attr->ia_uid, inode->i_uid)) &&
5323adbe12SAndy Lutomirski 	    !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
542c27c65eSChristoph Hellwig 		return -EPERM;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	/* Make sure caller can chgrp. */
571da177e4SLinus Torvalds 	if ((ia_valid & ATTR_GID) &&
588e96e3b7SEric W. Biederman 	    (!uid_eq(current_fsuid(), inode->i_uid) ||
598e96e3b7SEric W. Biederman 	    (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
6023adbe12SAndy Lutomirski 	    !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
612c27c65eSChristoph Hellwig 		return -EPERM;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	/* Make sure a caller can chmod. */
641da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
652e149670SSerge E. Hallyn 		if (!inode_owner_or_capable(inode))
662c27c65eSChristoph Hellwig 			return -EPERM;
671da177e4SLinus Torvalds 		/* Also check the setgid bit! */
681da177e4SLinus Torvalds 		if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
697fa294c8SEric W. Biederman 				inode->i_gid) &&
7023adbe12SAndy Lutomirski 		    !capable_wrt_inode_uidgid(inode, CAP_FSETID))
711da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
721da177e4SLinus Torvalds 	}
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	/* Check for setting the inode time. */
759767d749SMiklos Szeredi 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
762e149670SSerge E. Hallyn 		if (!inode_owner_or_capable(inode))
772c27c65eSChristoph Hellwig 			return -EPERM;
781da177e4SLinus Torvalds 	}
792c27c65eSChristoph Hellwig 
802c27c65eSChristoph Hellwig 	return 0;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds EXPORT_SYMBOL(inode_change_ok);
831da177e4SLinus Torvalds 
8425d9e2d1Snpiggin@suse.de /**
8525d9e2d1Snpiggin@suse.de  * inode_newsize_ok - may this inode be truncated to a given size
8625d9e2d1Snpiggin@suse.de  * @inode:	the inode to be truncated
8725d9e2d1Snpiggin@suse.de  * @offset:	the new size to assign to the inode
8825d9e2d1Snpiggin@suse.de  * @Returns:	0 on success, -ve errno on failure
8925d9e2d1Snpiggin@suse.de  *
907bb46a67Snpiggin@suse.de  * inode_newsize_ok must be called with i_mutex held.
917bb46a67Snpiggin@suse.de  *
9225d9e2d1Snpiggin@suse.de  * inode_newsize_ok will check filesystem limits and ulimits to check that the
9325d9e2d1Snpiggin@suse.de  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
9425d9e2d1Snpiggin@suse.de  * when necessary. Caller must not proceed with inode size change if failure is
9525d9e2d1Snpiggin@suse.de  * returned. @inode must be a file (not directory), with appropriate
9625d9e2d1Snpiggin@suse.de  * permissions to allow truncate (inode_newsize_ok does NOT check these
9725d9e2d1Snpiggin@suse.de  * conditions).
9825d9e2d1Snpiggin@suse.de  */
9925d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
10025d9e2d1Snpiggin@suse.de {
10125d9e2d1Snpiggin@suse.de 	if (inode->i_size < offset) {
10225d9e2d1Snpiggin@suse.de 		unsigned long limit;
10325d9e2d1Snpiggin@suse.de 
104d554ed89SJiri Slaby 		limit = rlimit(RLIMIT_FSIZE);
10525d9e2d1Snpiggin@suse.de 		if (limit != RLIM_INFINITY && offset > limit)
10625d9e2d1Snpiggin@suse.de 			goto out_sig;
10725d9e2d1Snpiggin@suse.de 		if (offset > inode->i_sb->s_maxbytes)
10825d9e2d1Snpiggin@suse.de 			goto out_big;
10925d9e2d1Snpiggin@suse.de 	} else {
11025d9e2d1Snpiggin@suse.de 		/*
11125d9e2d1Snpiggin@suse.de 		 * truncation of in-use swapfiles is disallowed - it would
11225d9e2d1Snpiggin@suse.de 		 * cause subsequent swapout to scribble on the now-freed
11325d9e2d1Snpiggin@suse.de 		 * blocks.
11425d9e2d1Snpiggin@suse.de 		 */
11525d9e2d1Snpiggin@suse.de 		if (IS_SWAPFILE(inode))
11625d9e2d1Snpiggin@suse.de 			return -ETXTBSY;
11725d9e2d1Snpiggin@suse.de 	}
11825d9e2d1Snpiggin@suse.de 
11925d9e2d1Snpiggin@suse.de 	return 0;
12025d9e2d1Snpiggin@suse.de out_sig:
12125d9e2d1Snpiggin@suse.de 	send_sig(SIGXFSZ, current, 0);
12225d9e2d1Snpiggin@suse.de out_big:
12325d9e2d1Snpiggin@suse.de 	return -EFBIG;
12425d9e2d1Snpiggin@suse.de }
12525d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
12625d9e2d1Snpiggin@suse.de 
1277bb46a67Snpiggin@suse.de /**
1286a1a90adSChristoph Hellwig  * setattr_copy - copy simple metadata updates into the generic inode
1297bb46a67Snpiggin@suse.de  * @inode:	the inode to be updated
1307bb46a67Snpiggin@suse.de  * @attr:	the new attributes
1317bb46a67Snpiggin@suse.de  *
1326a1a90adSChristoph Hellwig  * setattr_copy must be called with i_mutex held.
1337bb46a67Snpiggin@suse.de  *
1346a1a90adSChristoph Hellwig  * setattr_copy updates the inode's metadata with that specified
13525985edcSLucas De Marchi  * in attr. Noticeably missing is inode size update, which is more complex
1362c27c65eSChristoph Hellwig  * as it requires pagecache updates.
1377bb46a67Snpiggin@suse.de  *
1387bb46a67Snpiggin@suse.de  * The inode is not marked as dirty after this operation. The rationale is
1397bb46a67Snpiggin@suse.de  * that for "simple" filesystems, the struct inode is the inode storage.
1407bb46a67Snpiggin@suse.de  * The caller is free to mark the inode dirty afterwards if needed.
1417bb46a67Snpiggin@suse.de  */
1426a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	if (ia_valid & ATTR_UID)
1471da177e4SLinus Torvalds 		inode->i_uid = attr->ia_uid;
1481da177e4SLinus Torvalds 	if (ia_valid & ATTR_GID)
1491da177e4SLinus Torvalds 		inode->i_gid = attr->ia_gid;
1501da177e4SLinus Torvalds 	if (ia_valid & ATTR_ATIME)
1511da177e4SLinus Torvalds 		inode->i_atime = timespec_trunc(attr->ia_atime,
1521da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1531da177e4SLinus Torvalds 	if (ia_valid & ATTR_MTIME)
1541da177e4SLinus Torvalds 		inode->i_mtime = timespec_trunc(attr->ia_mtime,
1551da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1561da177e4SLinus Torvalds 	if (ia_valid & ATTR_CTIME)
1571da177e4SLinus Torvalds 		inode->i_ctime = timespec_trunc(attr->ia_ctime,
1581da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1591da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
1601da177e4SLinus Torvalds 		umode_t mode = attr->ia_mode;
1611da177e4SLinus Torvalds 
1627fa294c8SEric W. Biederman 		if (!in_group_p(inode->i_gid) &&
16323adbe12SAndy Lutomirski 		    !capable_wrt_inode_uidgid(inode, CAP_FSETID))
1641da177e4SLinus Torvalds 			mode &= ~S_ISGID;
1651da177e4SLinus Torvalds 		inode->i_mode = mode;
1661da177e4SLinus Torvalds 	}
1677bb46a67Snpiggin@suse.de }
1686a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
1697bb46a67Snpiggin@suse.de 
17027ac0ffeSJ. Bruce Fields /**
17127ac0ffeSJ. Bruce Fields  * notify_change - modify attributes of a filesytem object
17227ac0ffeSJ. Bruce Fields  * @dentry:	object affected
17327ac0ffeSJ. Bruce Fields  * @iattr:	new attributes
17427ac0ffeSJ. Bruce Fields  * @delegated_inode: returns inode, if the inode is delegated
17527ac0ffeSJ. Bruce Fields  *
17627ac0ffeSJ. Bruce Fields  * The caller must hold the i_mutex on the affected object.
17727ac0ffeSJ. Bruce Fields  *
17827ac0ffeSJ. Bruce Fields  * If notify_change discovers a delegation in need of breaking,
17927ac0ffeSJ. Bruce Fields  * it will return -EWOULDBLOCK and return a reference to the inode in
18027ac0ffeSJ. Bruce Fields  * delegated_inode.  The caller should then break the delegation and
18127ac0ffeSJ. Bruce Fields  * retry.  Because breaking a delegation may take a long time, the
18227ac0ffeSJ. Bruce Fields  * caller should drop the i_mutex before doing so.
18327ac0ffeSJ. Bruce Fields  *
18427ac0ffeSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
18527ac0ffeSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
18627ac0ffeSJ. Bruce Fields  * to be NFS exported.  Also, passing NULL is fine for callers holding
18727ac0ffeSJ. Bruce Fields  * the file open for write, as there can be no conflicting delegation in
18827ac0ffeSJ. Bruce Fields  * that case.
18927ac0ffeSJ. Bruce Fields  */
19027ac0ffeSJ. Bruce Fields int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1938d334acdSAl Viro 	umode_t mode = inode->i_mode;
1941da177e4SLinus Torvalds 	int error;
1951da177e4SLinus Torvalds 	struct timespec now;
1961da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1971da177e4SLinus Torvalds 
1985955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
199c4107b30SAndrew Morton 
200beb29e05SMiklos Szeredi 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
201beb29e05SMiklos Szeredi 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
202beb29e05SMiklos Szeredi 			return -EPERM;
203beb29e05SMiklos Szeredi 	}
204beb29e05SMiklos Szeredi 
205*f2b20f6eSMiklos Szeredi 	/*
206*f2b20f6eSMiklos Szeredi 	 * If utimes(2) and friends are called with times == NULL (or both
207*f2b20f6eSMiklos Szeredi 	 * times are UTIME_NOW), then we need to check for write permission
208*f2b20f6eSMiklos Szeredi 	 */
209*f2b20f6eSMiklos Szeredi 	if (ia_valid & ATTR_TOUCH) {
210*f2b20f6eSMiklos Szeredi 		if (IS_IMMUTABLE(inode))
211*f2b20f6eSMiklos Szeredi 			return -EPERM;
212*f2b20f6eSMiklos Szeredi 
213*f2b20f6eSMiklos Szeredi 		if (!inode_owner_or_capable(inode)) {
214*f2b20f6eSMiklos Szeredi 			error = inode_permission(inode, MAY_WRITE);
215*f2b20f6eSMiklos Szeredi 			if (error)
216*f2b20f6eSMiklos Szeredi 				return error;
217*f2b20f6eSMiklos Szeredi 		}
218*f2b20f6eSMiklos Szeredi 	}
219*f2b20f6eSMiklos Szeredi 
22069b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
2218d334acdSAl Viro 		umode_t amode = attr->ia_mode;
22269b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
22369b45732SAndi Kleen 		if (is_sxid(amode))
22469b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
22569b45732SAndi Kleen 	}
22669b45732SAndi Kleen 
2271da177e4SLinus Torvalds 	now = current_fs_time(inode->i_sb);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	attr->ia_ctime = now;
2301da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
2311da177e4SLinus Torvalds 		attr->ia_atime = now;
2321da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
2331da177e4SLinus Torvalds 		attr->ia_mtime = now;
234b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
235b5376771SSerge E. Hallyn 		attr->ia_valid &= ~ATTR_KILL_PRIV;
236b5376771SSerge E. Hallyn 		ia_valid &= ~ATTR_KILL_PRIV;
237b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
238b5376771SSerge E. Hallyn 		if (error > 0)
239b5376771SSerge E. Hallyn 			error = security_inode_killpriv(dentry);
240b5376771SSerge E. Hallyn 		if (error)
241b5376771SSerge E. Hallyn 			return error;
242b5376771SSerge E. Hallyn 	}
2436de0ec00SJeff Layton 
2446de0ec00SJeff Layton 	/*
2456de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
2466de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
2476de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
2486de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
2496de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
2506de0ec00SJeff Layton 	 */
2516de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
2526de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
2536de0ec00SJeff Layton 		BUG();
2546de0ec00SJeff Layton 
2551da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
2561da177e4SLinus Torvalds 		if (mode & S_ISUID) {
2571da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
2586de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
2591da177e4SLinus Torvalds 		}
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
2621da177e4SLinus Torvalds 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
2631da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
2641da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
2651da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
2661da177e4SLinus Torvalds 			}
2671da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
2681da177e4SLinus Torvalds 		}
2691da177e4SLinus Torvalds 	}
2706de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
2711da177e4SLinus Torvalds 		return 0;
2721da177e4SLinus Torvalds 
273a475acf0SSeth Forshee 	/*
274a475acf0SSeth Forshee 	 * Verify that uid/gid changes are valid in the target
275a475acf0SSeth Forshee 	 * namespace of the superblock.
276a475acf0SSeth Forshee 	 */
277a475acf0SSeth Forshee 	if (ia_valid & ATTR_UID &&
278a475acf0SSeth Forshee 	    !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
279a475acf0SSeth Forshee 		return -EOVERFLOW;
280a475acf0SSeth Forshee 	if (ia_valid & ATTR_GID &&
281a475acf0SSeth Forshee 	    !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
282a475acf0SSeth Forshee 		return -EOVERFLOW;
283a475acf0SSeth Forshee 
2840bd23d09SEric W. Biederman 	/* Don't allow modifications of files with invalid uids or
2850bd23d09SEric W. Biederman 	 * gids unless those uids & gids are being made valid.
2860bd23d09SEric W. Biederman 	 */
2870bd23d09SEric W. Biederman 	if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
2880bd23d09SEric W. Biederman 		return -EOVERFLOW;
2890bd23d09SEric W. Biederman 	if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
2900bd23d09SEric W. Biederman 		return -EOVERFLOW;
2910bd23d09SEric W. Biederman 
292a77b72daSMiklos Szeredi 	error = security_inode_setattr(dentry, attr);
293a77b72daSMiklos Szeredi 	if (error)
294a77b72daSMiklos Szeredi 		return error;
29527ac0ffeSJ. Bruce Fields 	error = try_break_deleg(inode, delegated_inode);
29627ac0ffeSJ. Bruce Fields 	if (error)
29727ac0ffeSJ. Bruce Fields 		return error;
298a77b72daSMiklos Szeredi 
299eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
3001da177e4SLinus Torvalds 		error = inode->i_op->setattr(dentry, attr);
301eef2380cSChristoph Hellwig 	else
302eef2380cSChristoph Hellwig 		error = simple_setattr(dentry, attr);
3031da177e4SLinus Torvalds 
304975d2943SMimi Zohar 	if (!error) {
3050eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
3069957a504SMimi Zohar 		ima_inode_post_setattr(dentry);
307975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
308975d2943SMimi Zohar 	}
3090eeca283SRobert Love 
3101da177e4SLinus Torvalds 	return error;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
313