// SPDX-License-Identifier: GPL-2.01/*2* linux/fs/attr.c3*4* Copyright (C) 1991, 1992 Linus Torvalds5* changes by Thomas Schoebel-Theuer6*/78#include <linux/export.h>9#include <linux/time.h>10#include <linux/mm.h>11#include <linux/string.h>12#include <linux/sched/signal.h>13#include <linux/capability.h>14#include <linux/fsnotify.h>15#include <linux/fcntl.h>16#include <linux/filelock.h>17#include <linux/security.h>1819/**20* setattr_should_drop_sgid - determine whether the setgid bit needs to be21* removed22* @idmap: idmap of the mount @inode was found from23* @inode: inode to check24*25* This function determines whether the setgid bit needs to be removed.26* We retain backwards compatibility and require setgid bit to be removed27* unconditionally if S_IXGRP is set. Otherwise we have the exact same28* requirements as setattr_prepare() and setattr_copy().29*30* Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.31*/32int setattr_should_drop_sgid(struct mnt_idmap *idmap,33const struct inode *inode)34{35umode_t mode = inode->i_mode;3637if (!(mode & S_ISGID))38return 0;39if (mode & S_IXGRP)40return ATTR_KILL_SGID;41if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))42return ATTR_KILL_SGID;43return 0;44}45EXPORT_SYMBOL(setattr_should_drop_sgid);4647/**48* setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to49* be dropped50* @idmap: idmap of the mount @inode was found from51* @inode: inode to check52*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 the55* setgid bit needs to be removed ATTR_KILL_SGID is returned. If both56* set{g,u}id bits need to be removed the corresponding mask of both flags is57* returned.58*59* Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits60* to remove, 0 otherwise.61*/62int setattr_should_drop_suidgid(struct mnt_idmap *idmap,63struct inode *inode)64{65umode_t mode = inode->i_mode;66int kill = 0;6768/* suid always must be killed */69if (unlikely(mode & S_ISUID))70kill = ATTR_KILL_SUID;7172kill |= setattr_should_drop_sgid(idmap, inode);7374if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))75return kill;7677return 0;78}79EXPORT_SYMBOL(setattr_should_drop_suidgid);8081/**82* chown_ok - verify permissions to chown inode83* @idmap: idmap of the mount @inode was found from84* @inode: inode to check permissions on85* @ia_vfsuid: uid to chown @inode to86*87* If the inode has been found through an idmapped mount the idmap of88* the vfsmount must be passed through @idmap. This function will then89* take care to map the inode according to @idmap before checking90* permissions. On non-idmapped mounts or if permission checking is to be91* performed on the raw inode simply pass @nop_mnt_idmap.92*/93static bool chown_ok(struct mnt_idmap *idmap,94const struct inode *inode, vfsuid_t ia_vfsuid)95{96vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);97if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&98vfsuid_eq(ia_vfsuid, vfsuid))99return true;100if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))101return true;102if (!vfsuid_valid(vfsuid) &&103ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))104return true;105return false;106}107108/**109* chgrp_ok - verify permissions to chgrp inode110* @idmap: idmap of the mount @inode was found from111* @inode: inode to check permissions on112* @ia_vfsgid: gid to chown @inode to113*114* If the inode has been found through an idmapped mount the idmap of115* the vfsmount must be passed through @idmap. This function will then116* take care to map the inode according to @idmap before checking117* permissions. On non-idmapped mounts or if permission checking is to be118* performed on the raw inode simply pass @nop_mnt_idmap.119*/120static bool chgrp_ok(struct mnt_idmap *idmap,121const struct inode *inode, vfsgid_t ia_vfsgid)122{123vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);124vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);125if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {126if (vfsgid_eq(ia_vfsgid, vfsgid))127return true;128if (vfsgid_in_group_p(ia_vfsgid))129return true;130}131if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))132return true;133if (!vfsgid_valid(vfsgid) &&134ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))135return true;136return false;137}138139/**140* setattr_prepare - check if attribute changes to a dentry are allowed141* @idmap: idmap of the mount the inode was found from142* @dentry: dentry to check143* @attr: attributes to change144*145* Check if we are allowed to change the attributes contained in @attr146* in the given dentry. This includes the normal unix access permission147* checks, as well as checks for rlimits and others. The function also clears148* SGID bit from mode if user is not allowed to set it. Also file capabilities149* and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.150*151* If the inode has been found through an idmapped mount the idmap of152* the vfsmount must be passed through @idmap. This function will then153* take care to map the inode according to @idmap before checking154* permissions. On non-idmapped mounts or if permission checking is to be155* performed on the raw inode simply pass @nop_mnt_idmap.156*157* Should be called as the first thing in ->setattr implementations,158* possibly after taking additional locks.159*/160int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,161struct iattr *attr)162{163struct inode *inode = d_inode(dentry);164unsigned int ia_valid = attr->ia_valid;165166/*167* First check size constraints. These can't be overriden using168* ATTR_FORCE.169*/170if (ia_valid & ATTR_SIZE) {171int error = inode_newsize_ok(inode, attr->ia_size);172if (error)173return error;174}175176/* If force is set do it anyway. */177if (ia_valid & ATTR_FORCE)178goto kill_priv;179180/* Make sure a caller can chown. */181if ((ia_valid & ATTR_UID) &&182!chown_ok(idmap, inode, attr->ia_vfsuid))183return -EPERM;184185/* Make sure caller can chgrp. */186if ((ia_valid & ATTR_GID) &&187!chgrp_ok(idmap, inode, attr->ia_vfsgid))188return -EPERM;189190/* Make sure a caller can chmod. */191if (ia_valid & ATTR_MODE) {192vfsgid_t vfsgid;193194if (!inode_owner_or_capable(idmap, inode))195return -EPERM;196197if (ia_valid & ATTR_GID)198vfsgid = attr->ia_vfsgid;199else200vfsgid = i_gid_into_vfsgid(idmap, inode);201202/* Also check the setgid bit! */203if (!in_group_or_capable(idmap, inode, vfsgid))204attr->ia_mode &= ~S_ISGID;205}206207/* Check for setting the inode time. */208if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {209if (!inode_owner_or_capable(idmap, inode))210return -EPERM;211}212213kill_priv:214/* User has permission for the change */215if (ia_valid & ATTR_KILL_PRIV) {216int error;217218error = security_inode_killpriv(idmap, dentry);219if (error)220return error;221}222223return 0;224}225EXPORT_SYMBOL(setattr_prepare);226227/**228* inode_newsize_ok - may this inode be truncated to a given size229* @inode: the inode to be truncated230* @offset: the new size to assign to the inode231*232* inode_newsize_ok must be called with i_rwsem held exclusively.233*234* inode_newsize_ok will check filesystem limits and ulimits to check that the235* new inode size is within limits. inode_newsize_ok will also send SIGXFSZ236* when necessary. Caller must not proceed with inode size change if failure is237* returned. @inode must be a file (not directory), with appropriate238* permissions to allow truncate (inode_newsize_ok does NOT check these239* conditions).240*241* Return: 0 on success, -ve errno on failure242*/243int inode_newsize_ok(const struct inode *inode, loff_t offset)244{245if (offset < 0)246return -EINVAL;247if (inode->i_size < offset) {248unsigned long limit;249250limit = rlimit(RLIMIT_FSIZE);251if (limit != RLIM_INFINITY && offset > limit)252goto out_sig;253if (offset > inode->i_sb->s_maxbytes)254goto out_big;255} else {256/*257* truncation of in-use swapfiles is disallowed - it would258* cause subsequent swapout to scribble on the now-freed259* blocks.260*/261if (IS_SWAPFILE(inode))262return -ETXTBSY;263}264265return 0;266out_sig:267send_sig(SIGXFSZ, current, 0);268out_big:269return -EFBIG;270}271EXPORT_SYMBOL(inode_newsize_ok);272273/**274* setattr_copy_mgtime - update timestamps for mgtime inodes275* @inode: inode timestamps to be updated276* @attr: attrs for the update277*278* With multigrain timestamps, take more care to prevent races when279* updating the ctime. Always update the ctime to the very latest using280* the standard mechanism, and use that to populate the atime and mtime281* appropriately (unless those are being set to specific values).282*/283static void setattr_copy_mgtime(struct inode *inode, const struct iattr *attr)284{285unsigned int ia_valid = attr->ia_valid;286struct timespec64 now;287288if (ia_valid & ATTR_CTIME) {289/*290* In the case of an update for a write delegation, we must respect291* the value in ia_ctime and not use the current time.292*/293if (ia_valid & ATTR_DELEG)294now = inode_set_ctime_deleg(inode, attr->ia_ctime);295else296now = inode_set_ctime_current(inode);297} else {298/* If ATTR_CTIME isn't set, then ATTR_MTIME shouldn't be either. */299WARN_ON_ONCE(ia_valid & ATTR_MTIME);300now = current_time(inode);301}302303if (ia_valid & ATTR_ATIME_SET)304inode_set_atime_to_ts(inode, attr->ia_atime);305else if (ia_valid & ATTR_ATIME)306inode_set_atime_to_ts(inode, now);307308if (ia_valid & ATTR_MTIME_SET)309inode_set_mtime_to_ts(inode, attr->ia_mtime);310else if (ia_valid & ATTR_MTIME)311inode_set_mtime_to_ts(inode, now);312}313314/**315* setattr_copy - copy simple metadata updates into the generic inode316* @idmap: idmap of the mount the inode was found from317* @inode: the inode to be updated318* @attr: the new attributes319*320* setattr_copy must be called with i_rwsem held exclusively.321*322* setattr_copy updates the inode's metadata with that specified323* in attr on idmapped mounts. Necessary permission checks to determine324* whether or not the S_ISGID property needs to be removed are performed with325* the correct idmapped mount permission helpers.326* Noticeably missing is inode size update, which is more complex327* as it requires pagecache updates.328*329* If the inode has been found through an idmapped mount the idmap of330* the vfsmount must be passed through @idmap. This function will then331* take care to map the inode according to @idmap before checking332* permissions. On non-idmapped mounts or if permission checking is to be333* performed on the raw inode simply pass @nop_mnt_idmap.334*335* The inode is not marked as dirty after this operation. The rationale is336* that for "simple" filesystems, the struct inode is the inode storage.337* The caller is free to mark the inode dirty afterwards if needed.338*/339void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,340const struct iattr *attr)341{342unsigned int ia_valid = attr->ia_valid;343344i_uid_update(idmap, attr, inode);345i_gid_update(idmap, attr, inode);346if (ia_valid & ATTR_MODE) {347umode_t mode = attr->ia_mode;348if (!in_group_or_capable(idmap, inode,349i_gid_into_vfsgid(idmap, inode)))350mode &= ~S_ISGID;351inode->i_mode = mode;352}353354if (is_mgtime(inode))355return setattr_copy_mgtime(inode, attr);356357if (ia_valid & ATTR_ATIME)358inode_set_atime_to_ts(inode, attr->ia_atime);359if (ia_valid & ATTR_MTIME)360inode_set_mtime_to_ts(inode, attr->ia_mtime);361if (ia_valid & ATTR_CTIME) {362if (ia_valid & ATTR_DELEG)363inode_set_ctime_deleg(inode, attr->ia_ctime);364else365inode_set_ctime_to_ts(inode, attr->ia_ctime);366}367}368EXPORT_SYMBOL(setattr_copy);369370int may_setattr(struct mnt_idmap *idmap, struct inode *inode,371unsigned int ia_valid)372{373int error;374375if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {376if (IS_IMMUTABLE(inode) || IS_APPEND(inode))377return -EPERM;378}379380/*381* If utimes(2) and friends are called with times == NULL (or both382* times are UTIME_NOW), then we need to check for write permission383*/384if (ia_valid & ATTR_TOUCH) {385if (IS_IMMUTABLE(inode))386return -EPERM;387388if (!inode_owner_or_capable(idmap, inode)) {389error = inode_permission(idmap, inode, MAY_WRITE);390if (error)391return error;392}393}394return 0;395}396EXPORT_SYMBOL(may_setattr);397398/**399* notify_change - modify attributes of a filesystem object400* @idmap: idmap of the mount the inode was found from401* @dentry: object affected402* @attr: new attributes403* @delegated_inode: returns inode, if the inode is delegated404*405* The caller must hold the i_rwsem exclusively on the affected object.406*407* If notify_change discovers a delegation in need of breaking,408* it will return -EWOULDBLOCK and return a reference to the inode in409* delegated_inode. The caller should then break the delegation and410* retry. Because breaking a delegation may take a long time, the411* caller should drop the i_rwsem before doing so.412*413* Alternatively, a caller may pass NULL for delegated_inode. This may414* be appropriate for callers that expect the underlying filesystem not415* to be NFS exported. Also, passing NULL is fine for callers holding416* the file open for write, as there can be no conflicting delegation in417* that case.418*419* If the inode has been found through an idmapped mount the idmap of420* the vfsmount must be passed through @idmap. This function will then421* take care to map the inode according to @idmap before checking422* permissions. On non-idmapped mounts or if permission checking is to be423* performed on the raw inode simply pass @nop_mnt_idmap.424*/425int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,426struct iattr *attr, struct inode **delegated_inode)427{428struct inode *inode = dentry->d_inode;429umode_t mode = inode->i_mode;430int error;431struct timespec64 now;432unsigned int ia_valid = attr->ia_valid;433434WARN_ON_ONCE(!inode_is_locked(inode));435436error = may_setattr(idmap, inode, ia_valid);437if (error)438return error;439440if ((ia_valid & ATTR_MODE)) {441/*442* Don't allow changing the mode of symlinks:443*444* (1) The vfs doesn't take the mode of symlinks into account445* during permission checking.446* (2) This has never worked correctly. Most major filesystems447* did return EOPNOTSUPP due to interactions with POSIX ACLs448* but did still updated the mode of the symlink.449* This inconsistency led system call wrapper providers such450* as libc to block changing the mode of symlinks with451* EOPNOTSUPP already.452* (3) To even do this in the first place one would have to use453* specific file descriptors and quite some effort.454*/455if (S_ISLNK(inode->i_mode))456return -EOPNOTSUPP;457458/* Flag setting protected by i_rwsem */459if (is_sxid(attr->ia_mode))460inode->i_flags &= ~S_NOSEC;461}462463now = current_time(inode);464465attr->ia_ctime = now;466if (!(ia_valid & ATTR_ATIME_SET))467attr->ia_atime = now;468else469attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);470if (!(ia_valid & ATTR_MTIME_SET))471attr->ia_mtime = now;472else473attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);474475if (ia_valid & ATTR_KILL_PRIV) {476error = security_inode_need_killpriv(dentry);477if (error < 0)478return error;479if (error == 0)480ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;481}482483/*484* We now pass ATTR_KILL_S*ID to the lower level setattr function so485* that the function has the ability to reinterpret a mode change486* that's due to these bits. This adds an implicit restriction that487* no function will ever call notify_change with both ATTR_MODE and488* ATTR_KILL_S*ID set.489*/490if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&491(ia_valid & ATTR_MODE))492BUG();493494if (ia_valid & ATTR_KILL_SUID) {495if (mode & S_ISUID) {496ia_valid = attr->ia_valid |= ATTR_MODE;497attr->ia_mode = (inode->i_mode & ~S_ISUID);498}499}500if (ia_valid & ATTR_KILL_SGID) {501if (mode & S_ISGID) {502if (!(ia_valid & ATTR_MODE)) {503ia_valid = attr->ia_valid |= ATTR_MODE;504attr->ia_mode = inode->i_mode;505}506attr->ia_mode &= ~S_ISGID;507}508}509if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))510return 0;511512/*513* Verify that uid/gid changes are valid in the target514* namespace of the superblock.515*/516if (ia_valid & ATTR_UID &&517!vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns,518attr->ia_vfsuid))519return -EOVERFLOW;520if (ia_valid & ATTR_GID &&521!vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns,522attr->ia_vfsgid))523return -EOVERFLOW;524525/* Don't allow modifications of files with invalid uids or526* gids unless those uids & gids are being made valid.527*/528if (!(ia_valid & ATTR_UID) &&529!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)))530return -EOVERFLOW;531if (!(ia_valid & ATTR_GID) &&532!vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))533return -EOVERFLOW;534535error = security_inode_setattr(idmap, dentry, attr);536if (error)537return error;538539/*540* If ATTR_DELEG is set, then these attributes are being set on541* behalf of the holder of a write delegation. We want to avoid542* breaking the delegation in this case.543*/544if (!(ia_valid & ATTR_DELEG)) {545error = try_break_deleg(inode, delegated_inode);546if (error)547return error;548}549550if (inode->i_op->setattr)551error = inode->i_op->setattr(idmap, dentry, attr);552else553error = simple_setattr(idmap, dentry, attr);554555if (!error) {556fsnotify_change(dentry, ia_valid);557security_inode_post_setattr(idmap, dentry, ia_valid);558}559560return error;561}562EXPORT_SYMBOL(notify_change);563564565