attr.c 8.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *  linux/fs/attr.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  changes by Thomas Schoebel-Theuer
 */

8
#include <linux/export.h>
L
Linus Torvalds 已提交
9 10 11
#include <linux/time.h>
#include <linux/mm.h>
#include <linux/string.h>
12
#include <linux/capability.h>
R
Robert Love 已提交
13
#include <linux/fsnotify.h>
L
Linus Torvalds 已提交
14 15
#include <linux/fcntl.h>
#include <linux/security.h>
M
Mimi Zohar 已提交
16
#include <linux/evm.h>
M
Mimi Zohar 已提交
17
#include <linux/ima.h>
L
Linus Torvalds 已提交
18

19 20 21 22 23 24 25 26 27 28 29 30
/**
 * inode_change_ok - check if attribute changes to an inode are allowed
 * @inode:	inode to check
 * @attr:	attributes to change
 *
 * Check if we are allowed to change the attributes contained in @attr
 * in the given inode.  This includes the normal unix access permission
 * checks, as well as checks for rlimits and others.
 *
 * Should be called as the first thing in ->setattr implementations,
 * possibly after taking additional locks.
 */
N
npiggin@suse.de 已提交
31
int inode_change_ok(const struct inode *inode, struct iattr *attr)
L
Linus Torvalds 已提交
32 33 34
{
	unsigned int ia_valid = attr->ia_valid;

35 36 37 38 39 40 41 42 43 44
	/*
	 * First check size constraints.  These can't be overriden using
	 * ATTR_FORCE.
	 */
	if (ia_valid & ATTR_SIZE) {
		int error = inode_newsize_ok(inode, attr->ia_size);
		if (error)
			return error;
	}

L
Linus Torvalds 已提交
45 46
	/* If force is set do it anyway. */
	if (ia_valid & ATTR_FORCE)
47
		return 0;
L
Linus Torvalds 已提交
48 49 50

	/* Make sure a caller can chown. */
	if ((ia_valid & ATTR_UID) &&
51
	    (!uid_eq(current_fsuid(), inode->i_uid) ||
52 53
	     !uid_eq(attr->ia_uid, inode->i_uid)) &&
	    !inode_capable(inode, CAP_CHOWN))
54
		return -EPERM;
L
Linus Torvalds 已提交
55 56 57

	/* Make sure caller can chgrp. */
	if ((ia_valid & ATTR_GID) &&
58 59
	    (!uid_eq(current_fsuid(), inode->i_uid) ||
	    (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
60
	    !inode_capable(inode, CAP_CHOWN))
61
		return -EPERM;
L
Linus Torvalds 已提交
62 63 64

	/* Make sure a caller can chmod. */
	if (ia_valid & ATTR_MODE) {
65
		if (!inode_owner_or_capable(inode))
66
			return -EPERM;
L
Linus Torvalds 已提交
67 68
		/* Also check the setgid bit! */
		if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
69 70
				inode->i_gid) &&
		    !inode_capable(inode, CAP_FSETID))
L
Linus Torvalds 已提交
71 72 73 74
			attr->ia_mode &= ~S_ISGID;
	}

	/* Check for setting the inode time. */
75
	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
76
		if (!inode_owner_or_capable(inode))
77
			return -EPERM;
L
Linus Torvalds 已提交
78
	}
79 80

	return 0;
L
Linus Torvalds 已提交
81 82 83
}
EXPORT_SYMBOL(inode_change_ok);

N
npiggin@suse.de 已提交
84 85 86 87 88 89
/**
 * inode_newsize_ok - may this inode be truncated to a given size
 * @inode:	the inode to be truncated
 * @offset:	the new size to assign to the inode
 * @Returns:	0 on success, -ve errno on failure
 *
90 91
 * inode_newsize_ok must be called with i_mutex held.
 *
N
npiggin@suse.de 已提交
92 93 94 95 96 97 98 99 100 101 102 103
 * inode_newsize_ok will check filesystem limits and ulimits to check that the
 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
 * when necessary. Caller must not proceed with inode size change if failure is
 * returned. @inode must be a file (not directory), with appropriate
 * permissions to allow truncate (inode_newsize_ok does NOT check these
 * conditions).
 */
int inode_newsize_ok(const struct inode *inode, loff_t offset)
{
	if (inode->i_size < offset) {
		unsigned long limit;

J
Jiri Slaby 已提交
104
		limit = rlimit(RLIMIT_FSIZE);
N
npiggin@suse.de 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		if (limit != RLIM_INFINITY && offset > limit)
			goto out_sig;
		if (offset > inode->i_sb->s_maxbytes)
			goto out_big;
	} else {
		/*
		 * truncation of in-use swapfiles is disallowed - it would
		 * cause subsequent swapout to scribble on the now-freed
		 * blocks.
		 */
		if (IS_SWAPFILE(inode))
			return -ETXTBSY;
	}

	return 0;
out_sig:
	send_sig(SIGXFSZ, current, 0);
out_big:
	return -EFBIG;
}
EXPORT_SYMBOL(inode_newsize_ok);

127
/**
C
Christoph Hellwig 已提交
128
 * setattr_copy - copy simple metadata updates into the generic inode
129 130 131
 * @inode:	the inode to be updated
 * @attr:	the new attributes
 *
C
Christoph Hellwig 已提交
132
 * setattr_copy must be called with i_mutex held.
133
 *
C
Christoph Hellwig 已提交
134
 * setattr_copy updates the inode's metadata with that specified
L
Lucas De Marchi 已提交
135
 * in attr. Noticeably missing is inode size update, which is more complex
136
 * as it requires pagecache updates.
137 138 139 140 141
 *
 * The inode is not marked as dirty after this operation. The rationale is
 * that for "simple" filesystems, the struct inode is the inode storage.
 * The caller is free to mark the inode dirty afterwards if needed.
 */
C
Christoph Hellwig 已提交
142
void setattr_copy(struct inode *inode, const struct iattr *attr)
L
Linus Torvalds 已提交
143 144
{
	unsigned int ia_valid = attr->ia_valid;
145

L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	if (ia_valid & ATTR_UID)
		inode->i_uid = attr->ia_uid;
	if (ia_valid & ATTR_GID)
		inode->i_gid = attr->ia_gid;
	if (ia_valid & ATTR_ATIME)
		inode->i_atime = timespec_trunc(attr->ia_atime,
						inode->i_sb->s_time_gran);
	if (ia_valid & ATTR_MTIME)
		inode->i_mtime = timespec_trunc(attr->ia_mtime,
						inode->i_sb->s_time_gran);
	if (ia_valid & ATTR_CTIME)
		inode->i_ctime = timespec_trunc(attr->ia_ctime,
						inode->i_sb->s_time_gran);
	if (ia_valid & ATTR_MODE) {
		umode_t mode = attr->ia_mode;

162 163
		if (!in_group_p(inode->i_gid) &&
		    !inode_capable(inode, CAP_FSETID))
L
Linus Torvalds 已提交
164 165 166
			mode &= ~S_ISGID;
		inode->i_mode = mode;
	}
167
}
C
Christoph Hellwig 已提交
168
EXPORT_SYMBOL(setattr_copy);
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/**
 * notify_change - modify attributes of a filesytem object
 * @dentry:	object affected
 * @iattr:	new attributes
 * @delegated_inode: returns inode, if the inode is delegated
 *
 * The caller must hold the i_mutex on the affected object.
 *
 * If notify_change discovers a delegation in need of breaking,
 * it will return -EWOULDBLOCK and return a reference to the inode in
 * delegated_inode.  The caller should then break the delegation and
 * retry.  Because breaking a delegation may take a long time, the
 * caller should drop the i_mutex before doing so.
 *
 * Alternatively, a caller may pass NULL for delegated_inode.  This may
 * be appropriate for callers that expect the underlying filesystem not
 * to be NFS exported.  Also, passing NULL is fine for callers holding
 * the file open for write, as there can be no conflicting delegation in
 * that case.
 */
int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
L
Linus Torvalds 已提交
191 192
{
	struct inode *inode = dentry->d_inode;
A
Al Viro 已提交
193
	umode_t mode = inode->i_mode;
L
Linus Torvalds 已提交
194 195 196 197
	int error;
	struct timespec now;
	unsigned int ia_valid = attr->ia_valid;

198 199
	WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));

200 201 202 203 204
	if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
		if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
			return -EPERM;
	}

205 206 207 208 209
	if ((ia_valid & ATTR_SIZE) && IS_I_VERSION(inode)) {
		if (attr->ia_size != inode->i_size)
			inode_inc_iversion(inode);
	}

210
	if ((ia_valid & ATTR_MODE)) {
A
Al Viro 已提交
211
		umode_t amode = attr->ia_mode;
212 213 214 215 216
		/* Flag setting protected by i_mutex */
		if (is_sxid(amode))
			inode->i_flags &= ~S_NOSEC;
	}

L
Linus Torvalds 已提交
217 218 219 220 221 222 223
	now = current_fs_time(inode->i_sb);

	attr->ia_ctime = now;
	if (!(ia_valid & ATTR_ATIME_SET))
		attr->ia_atime = now;
	if (!(ia_valid & ATTR_MTIME_SET))
		attr->ia_mtime = now;
224 225 226 227 228 229 230 231 232
	if (ia_valid & ATTR_KILL_PRIV) {
		attr->ia_valid &= ~ATTR_KILL_PRIV;
		ia_valid &= ~ATTR_KILL_PRIV;
		error = security_inode_need_killpriv(dentry);
		if (error > 0)
			error = security_inode_killpriv(dentry);
		if (error)
			return error;
	}
233 234 235 236 237 238 239 240 241 242 243 244

	/*
	 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
	 * that the function has the ability to reinterpret a mode change
	 * that's due to these bits. This adds an implicit restriction that
	 * no function will ever call notify_change with both ATTR_MODE and
	 * ATTR_KILL_S*ID set.
	 */
	if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
	    (ia_valid & ATTR_MODE))
		BUG();

L
Linus Torvalds 已提交
245 246
	if (ia_valid & ATTR_KILL_SUID) {
		if (mode & S_ISUID) {
247 248
			ia_valid = attr->ia_valid |= ATTR_MODE;
			attr->ia_mode = (inode->i_mode & ~S_ISUID);
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259
		}
	}
	if (ia_valid & ATTR_KILL_SGID) {
		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
			if (!(ia_valid & ATTR_MODE)) {
				ia_valid = attr->ia_valid |= ATTR_MODE;
				attr->ia_mode = inode->i_mode;
			}
			attr->ia_mode &= ~S_ISGID;
		}
	}
260
	if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
L
Linus Torvalds 已提交
261 262
		return 0;

263
	error = security_inode_setattr(dentry, attr);
264 265 266
	if (error)
		return error;
	error = try_break_deleg(inode, delegated_inode);
267 268 269
	if (error)
		return error;

C
Christoph Hellwig 已提交
270
	if (inode->i_op->setattr)
271
		error = inode->i_op->setattr(dentry, attr);
C
Christoph Hellwig 已提交
272 273
	else
		error = simple_setattr(dentry, attr);
L
Linus Torvalds 已提交
274

M
Mimi Zohar 已提交
275
	if (!error) {
R
Robert Love 已提交
276
		fsnotify_change(dentry, ia_valid);
M
Mimi Zohar 已提交
277
		ima_inode_post_setattr(dentry);
M
Mimi Zohar 已提交
278 279
		evm_inode_post_setattr(dentry, ia_valid);
	}
R
Robert Love 已提交
280

L
Linus Torvalds 已提交
281 282 283
	return error;
}
EXPORT_SYMBOL(notify_change);