Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/fs/attr.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 1991, 1992  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  changes by Thomas Schoebel-Theuer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/fsnotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/evm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/ima.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static bool chown_ok(const struct inode *inode, kuid_t uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	if (uid_eq(current_fsuid(), inode->i_uid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	    uid_eq(uid, inode->i_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	if (capable_wrt_inode_uidgid(inode, CAP_CHOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (uid_eq(inode->i_uid, INVALID_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static bool chgrp_ok(const struct inode *inode, kgid_t gid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (uid_eq(current_fsuid(), inode->i_uid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	    (in_group_p(gid) || gid_eq(gid, inode->i_gid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (capable_wrt_inode_uidgid(inode, CAP_CHOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (gid_eq(inode->i_gid, INVALID_GID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	    ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * setattr_prepare - check if attribute changes to a dentry are allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * @dentry:	dentry to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * @attr:	attributes to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * Check if we are allowed to change the attributes contained in @attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * in the given dentry.  This includes the normal unix access permission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * checks, as well as checks for rlimits and others. The function also clears
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * SGID bit from mode if user is not allowed to set it. Also file capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * Should be called as the first thing in ->setattr implementations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * possibly after taking additional locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) int setattr_prepare(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned int ia_valid = attr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 * First check size constraints.  These can't be overriden using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 * ATTR_FORCE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		int error = inode_newsize_ok(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	/* If force is set do it anyway. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (ia_valid & ATTR_FORCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		goto kill_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* Make sure a caller can chown. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if ((ia_valid & ATTR_UID) && !chown_ok(inode, attr->ia_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* Make sure caller can chgrp. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if ((ia_valid & ATTR_GID) && !chgrp_ok(inode, attr->ia_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* Make sure a caller can chmod. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (ia_valid & ATTR_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		/* Also check the setgid bit! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				inode->i_gid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		    !capable_wrt_inode_uidgid(inode, CAP_FSETID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			attr->ia_mode &= ~S_ISGID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	/* Check for setting the inode time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) kill_priv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* User has permission for the change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (ia_valid & ATTR_KILL_PRIV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		error = security_inode_killpriv(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) EXPORT_SYMBOL_NS(setattr_prepare, ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * inode_newsize_ok - may this inode be truncated to a given size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * @inode:	the inode to be truncated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * @offset:	the new size to assign to the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * inode_newsize_ok must be called with i_mutex held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * inode_newsize_ok will check filesystem limits and ulimits to check that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * when necessary. Caller must not proceed with inode size change if failure is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * returned. @inode must be a file (not directory), with appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  * permissions to allow truncate (inode_newsize_ok does NOT check these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * conditions).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  * Return: 0 on success, -ve errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int inode_newsize_ok(const struct inode *inode, loff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (inode->i_size < offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		unsigned long limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		limit = rlimit(RLIMIT_FSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (limit != RLIM_INFINITY && offset > limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			goto out_sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (offset > inode->i_sb->s_maxbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			goto out_big;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		 * truncation of in-use swapfiles is disallowed - it would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		 * cause subsequent swapout to scribble on the now-freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		 * blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (IS_SWAPFILE(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			return -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) out_sig:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	send_sig(SIGXFSZ, current, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) out_big:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) EXPORT_SYMBOL_NS(inode_newsize_ok, ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * setattr_copy - copy simple metadata updates into the generic inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * @inode:	the inode to be updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * @attr:	the new attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * setattr_copy must be called with i_mutex held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * setattr_copy updates the inode's metadata with that specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * in attr. Noticeably missing is inode size update, which is more complex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * as it requires pagecache updates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * The inode is not marked as dirty after this operation. The rationale is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * that for "simple" filesystems, the struct inode is the inode storage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * The caller is free to mark the inode dirty afterwards if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) void setattr_copy(struct inode *inode, const struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	unsigned int ia_valid = attr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (ia_valid & ATTR_UID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		inode->i_uid = attr->ia_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (ia_valid & ATTR_GID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		inode->i_gid = attr->ia_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (ia_valid & ATTR_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		inode->i_atime = attr->ia_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (ia_valid & ATTR_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		inode->i_mtime = attr->ia_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (ia_valid & ATTR_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		inode->i_ctime = attr->ia_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (ia_valid & ATTR_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		umode_t mode = attr->ia_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		if (!in_group_p(inode->i_gid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		    !capable_wrt_inode_uidgid(inode, CAP_FSETID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			mode &= ~S_ISGID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) EXPORT_SYMBOL(setattr_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * notify_change - modify attributes of a filesytem object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * @dentry:	object affected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * @attr:	new attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * @delegated_inode: returns inode, if the inode is delegated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  * The caller must hold the i_mutex on the affected object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * If notify_change discovers a delegation in need of breaking,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * it will return -EWOULDBLOCK and return a reference to the inode in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * delegated_inode.  The caller should then break the delegation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * retry.  Because breaking a delegation may take a long time, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * caller should drop the i_mutex before doing so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  * Alternatively, a caller may pass NULL for delegated_inode.  This may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * be appropriate for callers that expect the underlying filesystem not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * to be NFS exported.  Also, passing NULL is fine for callers holding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * the file open for write, as there can be no conflicting delegation in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct inode *inode = dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	umode_t mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct timespec64 now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	unsigned int ia_valid = attr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	WARN_ON_ONCE(!inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	 * If utimes(2) and friends are called with times == NULL (or both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	 * times are UTIME_NOW), then we need to check for write permission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (ia_valid & ATTR_TOUCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		if (IS_IMMUTABLE(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (!inode_owner_or_capable(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			error = inode_permission(inode, MAY_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if ((ia_valid & ATTR_MODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		umode_t amode = attr->ia_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		/* Flag setting protected by i_mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (is_sxid(amode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			inode->i_flags &= ~S_NOSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	now = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	attr->ia_ctime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (!(ia_valid & ATTR_ATIME_SET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		attr->ia_atime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (!(ia_valid & ATTR_MTIME_SET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		attr->ia_mtime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (ia_valid & ATTR_KILL_PRIV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		error = security_inode_need_killpriv(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		if (error == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	 * that the function has the ability to reinterpret a mode change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * that's due to these bits. This adds an implicit restriction that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * no function will ever call notify_change with both ATTR_MODE and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * ATTR_KILL_S*ID set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	    (ia_valid & ATTR_MODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (ia_valid & ATTR_KILL_SUID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (mode & S_ISUID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			ia_valid = attr->ia_valid |= ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			attr->ia_mode = (inode->i_mode & ~S_ISUID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (ia_valid & ATTR_KILL_SGID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			if (!(ia_valid & ATTR_MODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				ia_valid = attr->ia_valid |= ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				attr->ia_mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			attr->ia_mode &= ~S_ISGID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 * Verify that uid/gid changes are valid in the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 * namespace of the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (ia_valid & ATTR_UID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	    !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (ia_valid & ATTR_GID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	    !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	/* Don't allow modifications of files with invalid uids or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	 * gids unless those uids & gids are being made valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	error = security_inode_setattr(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	error = try_break_deleg(inode, delegated_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (inode->i_op->setattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		error = inode->i_op->setattr(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		error = simple_setattr(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		fsnotify_change(dentry, ia_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		ima_inode_post_setattr(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		evm_inode_post_setattr(dentry, ia_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) EXPORT_SYMBOL_NS(notify_change, ANDROID_GKI_VFS_EXPORT_ONLY);