xref: /openbmc/linux/fs/attr.c (revision 31051c85b5e2aaaf6315f74c72a732673632a905)
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 /**
20*31051c85SJan Kara  * setattr_prepare - check if attribute changes to a dentry are allowed
21*31051c85SJan Kara  * @dentry:	dentry to check
222c27c65eSChristoph Hellwig  * @attr:	attributes to change
232c27c65eSChristoph Hellwig  *
242c27c65eSChristoph Hellwig  * Check if we are allowed to change the attributes contained in @attr
25*31051c85SJan Kara  * in the given dentry.  This includes the normal unix access permission
26*31051c85SJan Kara  * checks, as well as checks for rlimits and others. The function also clears
27*31051c85SJan Kara  * SGID bit from mode if user is not allowed to set it. Also file capabilities
28*31051c85SJan Kara  * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
292c27c65eSChristoph Hellwig  *
302c27c65eSChristoph Hellwig  * Should be called as the first thing in ->setattr implementations,
312c27c65eSChristoph Hellwig  * possibly after taking additional locks.
322c27c65eSChristoph Hellwig  */
33*31051c85SJan Kara int setattr_prepare(struct dentry *dentry, struct iattr *attr)
341da177e4SLinus Torvalds {
35*31051c85SJan Kara 	struct inode *inode = d_inode(dentry);
361da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
371da177e4SLinus Torvalds 
382c27c65eSChristoph Hellwig 	/*
392c27c65eSChristoph Hellwig 	 * First check size constraints.  These can't be overriden using
402c27c65eSChristoph Hellwig 	 * ATTR_FORCE.
412c27c65eSChristoph Hellwig 	 */
422c27c65eSChristoph Hellwig 	if (ia_valid & ATTR_SIZE) {
432c27c65eSChristoph Hellwig 		int error = inode_newsize_ok(inode, attr->ia_size);
442c27c65eSChristoph Hellwig 		if (error)
452c27c65eSChristoph Hellwig 			return error;
462c27c65eSChristoph Hellwig 	}
472c27c65eSChristoph Hellwig 
481da177e4SLinus Torvalds 	/* If force is set do it anyway. */
491da177e4SLinus Torvalds 	if (ia_valid & ATTR_FORCE)
502c27c65eSChristoph Hellwig 		return 0;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	/* Make sure a caller can chown. */
531da177e4SLinus Torvalds 	if ((ia_valid & ATTR_UID) &&
548e96e3b7SEric W. Biederman 	    (!uid_eq(current_fsuid(), inode->i_uid) ||
557fa294c8SEric W. Biederman 	     !uid_eq(attr->ia_uid, inode->i_uid)) &&
5623adbe12SAndy Lutomirski 	    !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
572c27c65eSChristoph Hellwig 		return -EPERM;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	/* Make sure caller can chgrp. */
601da177e4SLinus Torvalds 	if ((ia_valid & ATTR_GID) &&
618e96e3b7SEric W. Biederman 	    (!uid_eq(current_fsuid(), inode->i_uid) ||
628e96e3b7SEric W. Biederman 	    (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
6323adbe12SAndy Lutomirski 	    !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
642c27c65eSChristoph Hellwig 		return -EPERM;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	/* Make sure a caller can chmod. */
671da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
682e149670SSerge E. Hallyn 		if (!inode_owner_or_capable(inode))
692c27c65eSChristoph Hellwig 			return -EPERM;
701da177e4SLinus Torvalds 		/* Also check the setgid bit! */
711da177e4SLinus Torvalds 		if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
727fa294c8SEric W. Biederman 				inode->i_gid) &&
7323adbe12SAndy Lutomirski 		    !capable_wrt_inode_uidgid(inode, CAP_FSETID))
741da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
751da177e4SLinus Torvalds 	}
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	/* Check for setting the inode time. */
789767d749SMiklos Szeredi 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
792e149670SSerge E. Hallyn 		if (!inode_owner_or_capable(inode))
802c27c65eSChristoph Hellwig 			return -EPERM;
811da177e4SLinus Torvalds 	}
822c27c65eSChristoph Hellwig 
832c27c65eSChristoph Hellwig 	return 0;
841da177e4SLinus Torvalds }
85*31051c85SJan Kara EXPORT_SYMBOL(setattr_prepare);
861da177e4SLinus Torvalds 
8725d9e2d1Snpiggin@suse.de /**
8825d9e2d1Snpiggin@suse.de  * inode_newsize_ok - may this inode be truncated to a given size
8925d9e2d1Snpiggin@suse.de  * @inode:	the inode to be truncated
9025d9e2d1Snpiggin@suse.de  * @offset:	the new size to assign to the inode
9125d9e2d1Snpiggin@suse.de  * @Returns:	0 on success, -ve errno on failure
9225d9e2d1Snpiggin@suse.de  *
937bb46a67Snpiggin@suse.de  * inode_newsize_ok must be called with i_mutex held.
947bb46a67Snpiggin@suse.de  *
9525d9e2d1Snpiggin@suse.de  * inode_newsize_ok will check filesystem limits and ulimits to check that the
9625d9e2d1Snpiggin@suse.de  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
9725d9e2d1Snpiggin@suse.de  * when necessary. Caller must not proceed with inode size change if failure is
9825d9e2d1Snpiggin@suse.de  * returned. @inode must be a file (not directory), with appropriate
9925d9e2d1Snpiggin@suse.de  * permissions to allow truncate (inode_newsize_ok does NOT check these
10025d9e2d1Snpiggin@suse.de  * conditions).
10125d9e2d1Snpiggin@suse.de  */
10225d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
10325d9e2d1Snpiggin@suse.de {
10425d9e2d1Snpiggin@suse.de 	if (inode->i_size < offset) {
10525d9e2d1Snpiggin@suse.de 		unsigned long limit;
10625d9e2d1Snpiggin@suse.de 
107d554ed89SJiri Slaby 		limit = rlimit(RLIMIT_FSIZE);
10825d9e2d1Snpiggin@suse.de 		if (limit != RLIM_INFINITY && offset > limit)
10925d9e2d1Snpiggin@suse.de 			goto out_sig;
11025d9e2d1Snpiggin@suse.de 		if (offset > inode->i_sb->s_maxbytes)
11125d9e2d1Snpiggin@suse.de 			goto out_big;
11225d9e2d1Snpiggin@suse.de 	} else {
11325d9e2d1Snpiggin@suse.de 		/*
11425d9e2d1Snpiggin@suse.de 		 * truncation of in-use swapfiles is disallowed - it would
11525d9e2d1Snpiggin@suse.de 		 * cause subsequent swapout to scribble on the now-freed
11625d9e2d1Snpiggin@suse.de 		 * blocks.
11725d9e2d1Snpiggin@suse.de 		 */
11825d9e2d1Snpiggin@suse.de 		if (IS_SWAPFILE(inode))
11925d9e2d1Snpiggin@suse.de 			return -ETXTBSY;
12025d9e2d1Snpiggin@suse.de 	}
12125d9e2d1Snpiggin@suse.de 
12225d9e2d1Snpiggin@suse.de 	return 0;
12325d9e2d1Snpiggin@suse.de out_sig:
12425d9e2d1Snpiggin@suse.de 	send_sig(SIGXFSZ, current, 0);
12525d9e2d1Snpiggin@suse.de out_big:
12625d9e2d1Snpiggin@suse.de 	return -EFBIG;
12725d9e2d1Snpiggin@suse.de }
12825d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
12925d9e2d1Snpiggin@suse.de 
1307bb46a67Snpiggin@suse.de /**
1316a1a90adSChristoph Hellwig  * setattr_copy - copy simple metadata updates into the generic inode
1327bb46a67Snpiggin@suse.de  * @inode:	the inode to be updated
1337bb46a67Snpiggin@suse.de  * @attr:	the new attributes
1347bb46a67Snpiggin@suse.de  *
1356a1a90adSChristoph Hellwig  * setattr_copy must be called with i_mutex held.
1367bb46a67Snpiggin@suse.de  *
1376a1a90adSChristoph Hellwig  * setattr_copy updates the inode's metadata with that specified
13825985edcSLucas De Marchi  * in attr. Noticeably missing is inode size update, which is more complex
1392c27c65eSChristoph Hellwig  * as it requires pagecache updates.
1407bb46a67Snpiggin@suse.de  *
1417bb46a67Snpiggin@suse.de  * The inode is not marked as dirty after this operation. The rationale is
1427bb46a67Snpiggin@suse.de  * that for "simple" filesystems, the struct inode is the inode storage.
1437bb46a67Snpiggin@suse.de  * The caller is free to mark the inode dirty afterwards if needed.
1447bb46a67Snpiggin@suse.de  */
1456a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	if (ia_valid & ATTR_UID)
1501da177e4SLinus Torvalds 		inode->i_uid = attr->ia_uid;
1511da177e4SLinus Torvalds 	if (ia_valid & ATTR_GID)
1521da177e4SLinus Torvalds 		inode->i_gid = attr->ia_gid;
1531da177e4SLinus Torvalds 	if (ia_valid & ATTR_ATIME)
1541da177e4SLinus Torvalds 		inode->i_atime = timespec_trunc(attr->ia_atime,
1551da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1561da177e4SLinus Torvalds 	if (ia_valid & ATTR_MTIME)
1571da177e4SLinus Torvalds 		inode->i_mtime = timespec_trunc(attr->ia_mtime,
1581da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1591da177e4SLinus Torvalds 	if (ia_valid & ATTR_CTIME)
1601da177e4SLinus Torvalds 		inode->i_ctime = timespec_trunc(attr->ia_ctime,
1611da177e4SLinus Torvalds 						inode->i_sb->s_time_gran);
1621da177e4SLinus Torvalds 	if (ia_valid & ATTR_MODE) {
1631da177e4SLinus Torvalds 		umode_t mode = attr->ia_mode;
1641da177e4SLinus Torvalds 
1657fa294c8SEric W. Biederman 		if (!in_group_p(inode->i_gid) &&
16623adbe12SAndy Lutomirski 		    !capable_wrt_inode_uidgid(inode, CAP_FSETID))
1671da177e4SLinus Torvalds 			mode &= ~S_ISGID;
1681da177e4SLinus Torvalds 		inode->i_mode = mode;
1691da177e4SLinus Torvalds 	}
1707bb46a67Snpiggin@suse.de }
1716a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
1727bb46a67Snpiggin@suse.de 
17327ac0ffeSJ. Bruce Fields /**
17427ac0ffeSJ. Bruce Fields  * notify_change - modify attributes of a filesytem object
17527ac0ffeSJ. Bruce Fields  * @dentry:	object affected
17627ac0ffeSJ. Bruce Fields  * @iattr:	new attributes
17727ac0ffeSJ. Bruce Fields  * @delegated_inode: returns inode, if the inode is delegated
17827ac0ffeSJ. Bruce Fields  *
17927ac0ffeSJ. Bruce Fields  * The caller must hold the i_mutex on the affected object.
18027ac0ffeSJ. Bruce Fields  *
18127ac0ffeSJ. Bruce Fields  * If notify_change discovers a delegation in need of breaking,
18227ac0ffeSJ. Bruce Fields  * it will return -EWOULDBLOCK and return a reference to the inode in
18327ac0ffeSJ. Bruce Fields  * delegated_inode.  The caller should then break the delegation and
18427ac0ffeSJ. Bruce Fields  * retry.  Because breaking a delegation may take a long time, the
18527ac0ffeSJ. Bruce Fields  * caller should drop the i_mutex before doing so.
18627ac0ffeSJ. Bruce Fields  *
18727ac0ffeSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
18827ac0ffeSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
18927ac0ffeSJ. Bruce Fields  * to be NFS exported.  Also, passing NULL is fine for callers holding
19027ac0ffeSJ. Bruce Fields  * the file open for write, as there can be no conflicting delegation in
19127ac0ffeSJ. Bruce Fields  * that case.
19227ac0ffeSJ. Bruce Fields  */
19327ac0ffeSJ. Bruce Fields int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1968d334acdSAl Viro 	umode_t mode = inode->i_mode;
1971da177e4SLinus Torvalds 	int error;
1981da177e4SLinus Torvalds 	struct timespec now;
1991da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
2001da177e4SLinus Torvalds 
2015955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
202c4107b30SAndrew Morton 
203beb29e05SMiklos Szeredi 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
204beb29e05SMiklos Szeredi 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
205beb29e05SMiklos Szeredi 			return -EPERM;
206beb29e05SMiklos Szeredi 	}
207beb29e05SMiklos Szeredi 
20869b45732SAndi Kleen 	if ((ia_valid & ATTR_MODE)) {
2098d334acdSAl Viro 		umode_t amode = attr->ia_mode;
21069b45732SAndi Kleen 		/* Flag setting protected by i_mutex */
21169b45732SAndi Kleen 		if (is_sxid(amode))
21269b45732SAndi Kleen 			inode->i_flags &= ~S_NOSEC;
21369b45732SAndi Kleen 	}
21469b45732SAndi Kleen 
2151da177e4SLinus Torvalds 	now = current_fs_time(inode->i_sb);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	attr->ia_ctime = now;
2181da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_ATIME_SET))
2191da177e4SLinus Torvalds 		attr->ia_atime = now;
2201da177e4SLinus Torvalds 	if (!(ia_valid & ATTR_MTIME_SET))
2211da177e4SLinus Torvalds 		attr->ia_mtime = now;
222b5376771SSerge E. Hallyn 	if (ia_valid & ATTR_KILL_PRIV) {
223b5376771SSerge E. Hallyn 		attr->ia_valid &= ~ATTR_KILL_PRIV;
224b5376771SSerge E. Hallyn 		ia_valid &= ~ATTR_KILL_PRIV;
225b5376771SSerge E. Hallyn 		error = security_inode_need_killpriv(dentry);
226b5376771SSerge E. Hallyn 		if (error > 0)
227b5376771SSerge E. Hallyn 			error = security_inode_killpriv(dentry);
228b5376771SSerge E. Hallyn 		if (error)
229b5376771SSerge E. Hallyn 			return error;
230b5376771SSerge E. Hallyn 	}
2316de0ec00SJeff Layton 
2326de0ec00SJeff Layton 	/*
2336de0ec00SJeff Layton 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
2346de0ec00SJeff Layton 	 * that the function has the ability to reinterpret a mode change
2356de0ec00SJeff Layton 	 * that's due to these bits. This adds an implicit restriction that
2366de0ec00SJeff Layton 	 * no function will ever call notify_change with both ATTR_MODE and
2376de0ec00SJeff Layton 	 * ATTR_KILL_S*ID set.
2386de0ec00SJeff Layton 	 */
2396de0ec00SJeff Layton 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
2406de0ec00SJeff Layton 	    (ia_valid & ATTR_MODE))
2416de0ec00SJeff Layton 		BUG();
2426de0ec00SJeff Layton 
2431da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SUID) {
2441da177e4SLinus Torvalds 		if (mode & S_ISUID) {
2451da177e4SLinus Torvalds 			ia_valid = attr->ia_valid |= ATTR_MODE;
2466de0ec00SJeff Layton 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
2471da177e4SLinus Torvalds 		}
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 	if (ia_valid & ATTR_KILL_SGID) {
2501da177e4SLinus Torvalds 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
2511da177e4SLinus Torvalds 			if (!(ia_valid & ATTR_MODE)) {
2521da177e4SLinus Torvalds 				ia_valid = attr->ia_valid |= ATTR_MODE;
2531da177e4SLinus Torvalds 				attr->ia_mode = inode->i_mode;
2541da177e4SLinus Torvalds 			}
2551da177e4SLinus Torvalds 			attr->ia_mode &= ~S_ISGID;
2561da177e4SLinus Torvalds 		}
2571da177e4SLinus Torvalds 	}
2586de0ec00SJeff Layton 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
2591da177e4SLinus Torvalds 		return 0;
2601da177e4SLinus Torvalds 
261a475acf0SSeth Forshee 	/*
262a475acf0SSeth Forshee 	 * Verify that uid/gid changes are valid in the target
263a475acf0SSeth Forshee 	 * namespace of the superblock.
264a475acf0SSeth Forshee 	 */
265a475acf0SSeth Forshee 	if (ia_valid & ATTR_UID &&
266a475acf0SSeth Forshee 	    !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
267a475acf0SSeth Forshee 		return -EOVERFLOW;
268a475acf0SSeth Forshee 	if (ia_valid & ATTR_GID &&
269a475acf0SSeth Forshee 	    !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
270a475acf0SSeth Forshee 		return -EOVERFLOW;
271a475acf0SSeth Forshee 
2720bd23d09SEric W. Biederman 	/* Don't allow modifications of files with invalid uids or
2730bd23d09SEric W. Biederman 	 * gids unless those uids & gids are being made valid.
2740bd23d09SEric W. Biederman 	 */
2750bd23d09SEric W. Biederman 	if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
2760bd23d09SEric W. Biederman 		return -EOVERFLOW;
2770bd23d09SEric W. Biederman 	if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
2780bd23d09SEric W. Biederman 		return -EOVERFLOW;
2790bd23d09SEric W. Biederman 
280a77b72daSMiklos Szeredi 	error = security_inode_setattr(dentry, attr);
281a77b72daSMiklos Szeredi 	if (error)
282a77b72daSMiklos Szeredi 		return error;
28327ac0ffeSJ. Bruce Fields 	error = try_break_deleg(inode, delegated_inode);
28427ac0ffeSJ. Bruce Fields 	if (error)
28527ac0ffeSJ. Bruce Fields 		return error;
286a77b72daSMiklos Szeredi 
287eef2380cSChristoph Hellwig 	if (inode->i_op->setattr)
2881da177e4SLinus Torvalds 		error = inode->i_op->setattr(dentry, attr);
289eef2380cSChristoph Hellwig 	else
290eef2380cSChristoph Hellwig 		error = simple_setattr(dentry, attr);
2911da177e4SLinus Torvalds 
292975d2943SMimi Zohar 	if (!error) {
2930eeca283SRobert Love 		fsnotify_change(dentry, ia_valid);
2949957a504SMimi Zohar 		ima_inode_post_setattr(dentry);
295975d2943SMimi Zohar 		evm_inode_post_setattr(dentry, ia_valid);
296975d2943SMimi Zohar 	}
2970eeca283SRobert Love 
2981da177e4SLinus Torvalds 	return error;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
301