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