1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * changes by Thomas Schoebel-Theuer
9 #include <linux/export.h>
10 #include <linux/time.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>
24 * setattr_should_drop_sgid - determine whether the setgid bit needs to be
26 * @mnt_userns: user namespace of the mount @inode was found from
27 * @inode: inode to check
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().
34 * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
36 int setattr_should_drop_sgid(struct user_namespace *mnt_userns,
37 const struct inode *inode)
39 umode_t mode = inode->i_mode;
41 if (!(mode & S_ISGID))
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;
50 EXPORT_SYMBOL(setattr_should_drop_sgid);
53 * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to
55 * @mnt_userns: user namespace of the mount @inode was found from
56 * @inode: inode to check
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
64 * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits
65 * to remove, 0 otherwise.
67 int setattr_should_drop_suidgid(struct user_namespace *mnt_userns,
70 umode_t mode = inode->i_mode;
73 /* suid always must be killed */
74 if (unlikely(mode & S_ISUID))
75 kill = ATTR_KILL_SUID;
77 kill |= setattr_should_drop_sgid(mnt_userns, inode);
79 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
84 EXPORT_SYMBOL(setattr_should_drop_suidgid);
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
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.
98 static bool chown_ok(struct user_namespace *mnt_userns,
99 const struct inode *inode,
102 kuid_t kuid = i_uid_into_mnt(mnt_userns, inode);
103 if (uid_eq(current_fsuid(), kuid) && uid_eq(uid, inode->i_uid))
105 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
107 if (uid_eq(kuid, INVALID_UID) &&
108 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
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
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.
125 static bool chgrp_ok(struct user_namespace *mnt_userns,
126 const struct inode *inode, kgid_t gid)
128 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
129 if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) {
132 if (gid_eq(gid, inode->i_gid))
134 mapped_gid = mapped_kgid_fs(mnt_userns, i_user_ns(inode), gid);
135 if (in_group_p(mapped_gid))
138 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
140 if (gid_eq(kgid, INVALID_GID) &&
141 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
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
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.
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.
164 * Should be called as the first thing in ->setattr implementations,
165 * possibly after taking additional locks.
167 int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
170 struct inode *inode = d_inode(dentry);
171 unsigned int ia_valid = attr->ia_valid;
174 * First check size constraints. These can't be overriden using
177 if (ia_valid & ATTR_SIZE) {
178 int error = inode_newsize_ok(inode, attr->ia_size);
183 /* If force is set do it anyway. */
184 if (ia_valid & ATTR_FORCE)
187 /* Make sure a caller can chown. */
188 if ((ia_valid & ATTR_UID) && !chown_ok(mnt_userns, inode, attr->ia_uid))
191 /* Make sure caller can chgrp. */
192 if ((ia_valid & ATTR_GID) && !chgrp_ok(mnt_userns, inode, attr->ia_gid))
195 /* Make sure a caller can chmod. */
196 if (ia_valid & ATTR_MODE) {
199 if (!inode_owner_or_capable(mnt_userns, inode))
202 if (ia_valid & ATTR_GID)
203 mapped_gid = mapped_kgid_fs(mnt_userns,
204 i_user_ns(inode), attr->ia_gid);
206 mapped_gid = i_gid_into_mnt(mnt_userns, inode);
208 /* Also check the setgid bit! */
209 if (!in_group_or_capable(mnt_userns, inode, mapped_gid))
210 attr->ia_mode &= ~S_ISGID;
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))
220 /* User has permission for the change */
221 if (ia_valid & ATTR_KILL_PRIV) {
224 error = security_inode_killpriv(mnt_userns, dentry);
231 EXPORT_SYMBOL(setattr_prepare);
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
238 * inode_newsize_ok must be called with i_mutex held.
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
247 * Return: 0 on success, -ve errno on failure
249 int inode_newsize_ok(const struct inode *inode, loff_t offset)
253 if (inode->i_size < offset) {
256 limit = rlimit(RLIMIT_FSIZE);
257 if (limit != RLIM_INFINITY && offset > limit)
259 if (offset > inode->i_sb->s_maxbytes)
263 * truncation of in-use swapfiles is disallowed - it would
264 * cause subsequent swapout to scribble on the now-freed
267 if (IS_SWAPFILE(inode))
273 send_sig(SIGXFSZ, current, 0);
277 EXPORT_SYMBOL(inode_newsize_ok);
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
285 * setattr_copy must be called with i_mutex held.
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.
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.
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.
306 void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
307 const struct iattr *attr)
309 unsigned int ia_valid = attr->ia_valid;
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))
326 inode->i_mode = mode;
329 EXPORT_SYMBOL(setattr_copy);
331 int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
332 unsigned int ia_valid)
336 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
337 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
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
345 if (ia_valid & ATTR_TOUCH) {
346 if (IS_IMMUTABLE(inode))
349 if (!inode_owner_or_capable(mnt_userns, inode)) {
350 error = inode_permission(mnt_userns, inode, MAY_WRITE);
357 EXPORT_SYMBOL(may_setattr);
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
366 * The caller must hold the i_mutex on the affected object.
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.
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.
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
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.
389 int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
390 struct iattr *attr, struct inode **delegated_inode)
392 struct inode *inode = dentry->d_inode;
393 umode_t mode = inode->i_mode;
395 struct timespec64 now;
396 unsigned int ia_valid = attr->ia_valid;
398 WARN_ON_ONCE(!inode_is_locked(inode));
400 error = may_setattr(mnt_userns, inode, ia_valid);
404 if ((ia_valid & ATTR_MODE)) {
406 * Don't allow changing the mode of symlinks:
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.
419 if (S_ISLNK(inode->i_mode))
422 /* Flag setting protected by i_mutex */
423 if (is_sxid(attr->ia_mode))
424 inode->i_flags &= ~S_NOSEC;
427 now = current_time(inode);
429 attr->ia_ctime = now;
430 if (!(ia_valid & ATTR_ATIME_SET))
431 attr->ia_atime = now;
433 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
434 if (!(ia_valid & ATTR_MTIME_SET))
435 attr->ia_mtime = now;
437 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
439 if (ia_valid & ATTR_KILL_PRIV) {
440 error = security_inode_need_killpriv(dentry);
444 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
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.
454 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
455 (ia_valid & ATTR_MODE))
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);
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;
470 attr->ia_mode &= ~S_ISGID;
473 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
477 * Verify that uid/gid changes are valid in the target
478 * namespace of the superblock.
480 if (ia_valid & ATTR_UID &&
481 !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
483 if (ia_valid & ATTR_GID &&
484 !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
487 /* Don't allow modifications of files with invalid uids or
488 * gids unless those uids & gids are being made valid.
490 if (!(ia_valid & ATTR_UID) &&
491 !uid_valid(i_uid_into_mnt(mnt_userns, inode)))
493 if (!(ia_valid & ATTR_GID) &&
494 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
497 error = security_inode_setattr(dentry, attr);
500 error = try_break_deleg(inode, delegated_inode);
504 if (inode->i_op->setattr)
505 error = inode->i_op->setattr(mnt_userns, dentry, attr);
507 error = simple_setattr(mnt_userns, dentry, attr);
510 fsnotify_change(dentry, ia_valid);
511 ima_inode_post_setattr(mnt_userns, dentry);
512 evm_inode_post_setattr(dentry, ia_valid);
517 EXPORT_SYMBOL(notify_change);