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