xref: /openbmc/linux/fs/attr.c (revision 975d294373d8c1c913ad2bf4eb93966d4c7ca38f)
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 
81da177e4SLinus Torvalds #include <linux/module.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>
16*975d2943SMimi Zohar #include <linux/evm.h>
171da177e4SLinus Torvalds 
182c27c65eSChristoph Hellwig /**
192c27c65eSChristoph Hellwig  * inode_change_ok - check if attribute changes to an inode are allowed
202c27c65eSChristoph Hellwig  * @inode:	inode to check
212c27c65eSChristoph Hellwig  * @attr:	attributes to change
222c27c65eSChristoph Hellwig  *
232c27c65eSChristoph Hellwig  * Check if we are allowed to change the attributes contained in @attr
242c27c65eSChristoph Hellwig  * in the given inode.  This includes the normal unix access permission
252c27c65eSChristoph Hellwig  * checks, as well as checks for rlimits and others.
262c27c65eSChristoph Hellwig  *
272c27c65eSChristoph Hellwig  * Should be called as the first thing in ->setattr implementations,
282c27c65eSChristoph Hellwig  * possibly after taking additional locks.
292c27c65eSChristoph Hellwig  */
3025d9e2d1Snpiggin@suse.de int inode_change_ok(const struct inode *inode, struct iattr *attr)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
331da177e4SLinus Torvalds 
342c27c65eSChristoph Hellwig 	/*
352c27c65eSChristoph Hellwig 	 * First check size constraints.  These can't be overriden using
362c27c65eSChristoph Hellwig 	 * ATTR_FORCE.
372c27c65eSChristoph Hellwig 	 */
382c27c65eSChristoph Hellwig 	if (ia_valid & ATTR_SIZE) {
392c27c65eSChristoph Hellwig 		int error = inode_newsize_ok(inode, attr->ia_size);
402c27c65eSChristoph Hellwig 		if (error)
412c27c65eSChristoph Hellwig 			return error;
422c27c65eSChristoph Hellwig 	}
432c27c65eSChristoph Hellwig 
441da177e4SLinus Torvalds 	/* If force is set do it anyway. */
451da177e4SLinus Torvalds 	if (ia_valid & ATTR_FORCE)
462c27c65eSChristoph Hellwig 		return 0;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	/* Make sure a caller can chown. */
491da177e4SLinus Torvalds 	if ((ia_valid & ATTR_UID) &&
50da9592edSDavid Howells 	    (current_fsuid() != inode->i_uid ||
511da177e4SLinus Torvalds 	     attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
522c27c65eSChristoph Hellwig 		return -EPERM;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	/* Make sure caller can chgrp. */
551da177e4SLinus Torvalds 	if ((ia_valid & ATTR_GID) &&
56da9592edSDavid Howells 	    (current_fsuid() != inode->i_uid ||
571da177e4SLinus Torvalds 	    (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
581da177e4SLinus Torvalds 	    !capable(CAP_CHOWN))
592c27c65eSChristoph Hellwig 		return -EPERM;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	/* Make sure a caller can chmod. */
621da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
632e149670SSerge E. Hallyn 		if (!inode_owner_or_capable(inode))
642c27c65eSChristoph Hellwig 			return -EPERM;
651da177e4SLinus Torvalds 		/* Also check the setgid bit! */
661da177e4SLinus Torvalds 		if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
671da177e4SLinus Torvalds 				inode->i_gid) && !capable(CAP_FSETID))
681da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	/* Check for setting the inode time. */
729767d749SMiklos Szeredi 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
732e149670SSerge E. Hallyn 		if (!inode_owner_or_capable(inode))
742c27c65eSChristoph Hellwig 			return -EPERM;
751da177e4SLinus Torvalds 	}
762c27c65eSChristoph Hellwig 
772c27c65eSChristoph Hellwig 	return 0;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds EXPORT_SYMBOL(inode_change_ok);
801da177e4SLinus Torvalds 
8125d9e2d1Snpiggin@suse.de /**
8225d9e2d1Snpiggin@suse.de  * inode_newsize_ok - may this inode be truncated to a given size
8325d9e2d1Snpiggin@suse.de  * @inode:	the inode to be truncated
8425d9e2d1Snpiggin@suse.de  * @offset:	the new size to assign to the inode
8525d9e2d1Snpiggin@suse.de  * @Returns:	0 on success, -ve errno on failure
8625d9e2d1Snpiggin@suse.de  *
877bb46a67Snpiggin@suse.de  * inode_newsize_ok must be called with i_mutex held.
887bb46a67Snpiggin@suse.de  *
8925d9e2d1Snpiggin@suse.de  * inode_newsize_ok will check filesystem limits and ulimits to check that the
9025d9e2d1Snpiggin@suse.de  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
9125d9e2d1Snpiggin@suse.de  * when necessary. Caller must not proceed with inode size change if failure is
9225d9e2d1Snpiggin@suse.de  * returned. @inode must be a file (not directory), with appropriate
9325d9e2d1Snpiggin@suse.de  * permissions to allow truncate (inode_newsize_ok does NOT check these
9425d9e2d1Snpiggin@suse.de  * conditions).
9525d9e2d1Snpiggin@suse.de  */
9625d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
9725d9e2d1Snpiggin@suse.de {
9825d9e2d1Snpiggin@suse.de 	if (inode->i_size < offset) {
9925d9e2d1Snpiggin@suse.de 		unsigned long limit;
10025d9e2d1Snpiggin@suse.de 
101d554ed89SJiri Slaby 		limit = rlimit(RLIMIT_FSIZE);
10225d9e2d1Snpiggin@suse.de 		if (limit != RLIM_INFINITY && offset > limit)
10325d9e2d1Snpiggin@suse.de 			goto out_sig;
10425d9e2d1Snpiggin@suse.de 		if (offset > inode->i_sb->s_maxbytes)
10525d9e2d1Snpiggin@suse.de 			goto out_big;
10625d9e2d1Snpiggin@suse.de 	} else {
10725d9e2d1Snpiggin@suse.de 		/*
10825d9e2d1Snpiggin@suse.de 		 * truncation of in-use swapfiles is disallowed - it would
10925d9e2d1Snpiggin@suse.de 		 * cause subsequent swapout to scribble on the now-freed
11025d9e2d1Snpiggin@suse.de 		 * blocks.
11125d9e2d1Snpiggin@suse.de 		 */
11225d9e2d1Snpiggin@suse.de 		if (IS_SWAPFILE(inode))
11325d9e2d1Snpiggin@suse.de 			return -ETXTBSY;
11425d9e2d1Snpiggin@suse.de 	}
11525d9e2d1Snpiggin@suse.de 
11625d9e2d1Snpiggin@suse.de 	return 0;
11725d9e2d1Snpiggin@suse.de out_sig:
11825d9e2d1Snpiggin@suse.de 	send_sig(SIGXFSZ, current, 0);
11925d9e2d1Snpiggin@suse.de out_big:
12025d9e2d1Snpiggin@suse.de 	return -EFBIG;
12125d9e2d1Snpiggin@suse.de }
12225d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
12325d9e2d1Snpiggin@suse.de 
1247bb46a67Snpiggin@suse.de /**
1256a1a90adSChristoph Hellwig  * setattr_copy - copy simple metadata updates into the generic inode
1267bb46a67Snpiggin@suse.de  * @inode:	the inode to be updated
1277bb46a67Snpiggin@suse.de  * @attr:	the new attributes
1287bb46a67Snpiggin@suse.de  *
1296a1a90adSChristoph Hellwig  * setattr_copy must be called with i_mutex held.
1307bb46a67Snpiggin@suse.de  *
1316a1a90adSChristoph Hellwig  * setattr_copy updates the inode's metadata with that specified
13225985edcSLucas De Marchi  * in attr. Noticeably missing is inode size update, which is more complex
1332c27c65eSChristoph Hellwig  * as it requires pagecache updates.
1347bb46a67Snpiggin@suse.de  *
1357bb46a67Snpiggin@suse.de  * The inode is not marked as dirty after this operation. The rationale is
1367bb46a67Snpiggin@suse.de  * that for "simple" filesystems, the struct inode is the inode storage.
1377bb46a67Snpiggin@suse.de  * The caller is free to mark the inode dirty afterwards if needed.
1387bb46a67Snpiggin@suse.de  */
1396a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	if (ia_valid & ATTR_UID)
1441da177e4SLinus Torvalds 		inode->i_uid = attr->ia_uid;
1451da177e4SLinus Torvalds 	if (ia_valid & ATTR_GID)
1461da177e4SLinus Torvalds 		inode->i_gid = attr->ia_gid;
1471da177e4SLinus Torvalds 	if (ia_valid & ATTR_ATIME)
1481da177e4SLinus Torvalds 		inode->i_atime = timespec_trunc(attr->ia_atime,
1491da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1501da177e4SLinus Torvalds 	if (ia_valid & ATTR_MTIME)
1511da177e4SLinus Torvalds 		inode->i_mtime = timespec_trunc(attr->ia_mtime,
1521da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1531da177e4SLinus Torvalds 	if (ia_valid & ATTR_CTIME)
1541da177e4SLinus Torvalds 		inode->i_ctime = timespec_trunc(attr->ia_ctime,
1551da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1561da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
1571da177e4SLinus Torvalds 		umode_t mode = attr->ia_mode;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 		if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
1601da177e4SLinus Torvalds 			mode &= ~S_ISGID;
1611da177e4SLinus Torvalds 		inode->i_mode = mode;
1621da177e4SLinus Torvalds 	}
1637bb46a67Snpiggin@suse.de }
1646a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
1657bb46a67Snpiggin@suse.de 
1661da177e4SLinus Torvalds int notify_change(struct dentry * dentry, struct iattr * attr)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1696de0ec00SJeff Layton 	mode_t mode = inode->i_mode;
1701da177e4SLinus Torvalds 	int error;
1711da177e4SLinus Torvalds 	struct timespec now;
1721da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1731da177e4SLinus Torvalds 
174beb29e05SMiklos Szeredi 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
175beb29e05SMiklos Szeredi 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
176beb29e05SMiklos Szeredi 			return -EPERM;
177beb29e05SMiklos Szeredi 	}
178beb29e05SMiklos Szeredi 
17969b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
18069b45732SAndi Kleen 		mode_t amode = attr->ia_mode;
18169b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
18269b45732SAndi Kleen 		if (is_sxid(amode))
18369b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
18469b45732SAndi Kleen 	}
18569b45732SAndi Kleen 
1861da177e4SLinus Torvalds 	now = current_fs_time(inode->i_sb);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	attr->ia_ctime = now;
1891da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
1901da177e4SLinus Torvalds 		attr->ia_atime = now;
1911da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
1921da177e4SLinus Torvalds 		attr->ia_mtime = now;
193b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
194b5376771SSerge E. Hallyn 		attr->ia_valid &= ~ATTR_KILL_PRIV;
195b5376771SSerge E. Hallyn 		ia_valid &= ~ATTR_KILL_PRIV;
196b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
197b5376771SSerge E. Hallyn 		if (error > 0)
198b5376771SSerge E. Hallyn 			error = security_inode_killpriv(dentry);
199b5376771SSerge E. Hallyn 		if (error)
200b5376771SSerge E. Hallyn 			return error;
201b5376771SSerge E. Hallyn 	}
2026de0ec00SJeff Layton 
2036de0ec00SJeff Layton 	/*
2046de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
2056de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
2066de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
2076de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
2086de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
2096de0ec00SJeff Layton 	 */
2106de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
2116de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
2126de0ec00SJeff Layton 		BUG();
2136de0ec00SJeff Layton 
2141da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
2151da177e4SLinus Torvalds 		if (mode & S_ISUID) {
2161da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
2176de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
2181da177e4SLinus Torvalds 		}
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
2211da177e4SLinus Torvalds 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
2221da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
2231da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
2241da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
2251da177e4SLinus Torvalds 			}
2261da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
2271da177e4SLinus Torvalds 		}
2281da177e4SLinus Torvalds 	}
2296de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
2301da177e4SLinus Torvalds 		return 0;
2311da177e4SLinus Torvalds 
232a77b72daSMiklos Szeredi 	error = security_inode_setattr(dentry, attr);
233a77b72daSMiklos Szeredi 	if (error)
234a77b72daSMiklos Szeredi 		return error;
235a77b72daSMiklos Szeredi 
2361da177e4SLinus Torvalds 	if (ia_valid & ATTR_SIZE)
2371da177e4SLinus Torvalds 		down_write(&dentry->d_inode->i_alloc_sem);
2381da177e4SLinus Torvalds 
239eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
2401da177e4SLinus Torvalds 		error = inode->i_op->setattr(dentry, attr);
241eef2380cSChristoph Hellwig 	else
242eef2380cSChristoph Hellwig 		error = simple_setattr(dentry, attr);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	if (ia_valid & ATTR_SIZE)
2451da177e4SLinus Torvalds 		up_write(&dentry->d_inode->i_alloc_sem);
2461da177e4SLinus Torvalds 
247*975d2943SMimi Zohar 	if (!error) {
2480eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
249*975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
250*975d2943SMimi Zohar 	}
2510eeca283SRobert Love 
2521da177e4SLinus Torvalds 	return error;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
256