// 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_SET)289now = inode_set_ctime_deleg(inode, attr->ia_ctime);290else if (ia_valid & ATTR_CTIME)291now = inode_set_ctime_current(inode);292else293now = current_time(inode);294295if (ia_valid & ATTR_ATIME_SET)296inode_set_atime_to_ts(inode, attr->ia_atime);297else if (ia_valid & ATTR_ATIME)298inode_set_atime_to_ts(inode, now);299300if (ia_valid & ATTR_MTIME_SET)301inode_set_mtime_to_ts(inode, attr->ia_mtime);302else if (ia_valid & ATTR_MTIME)303inode_set_mtime_to_ts(inode, now);304}305306/**307* setattr_copy - copy simple metadata updates into the generic inode308* @idmap: idmap of the mount the inode was found from309* @inode: the inode to be updated310* @attr: the new attributes311*312* setattr_copy must be called with i_rwsem held exclusively.313*314* setattr_copy updates the inode's metadata with that specified315* in attr on idmapped mounts. Necessary permission checks to determine316* whether or not the S_ISGID property needs to be removed are performed with317* the correct idmapped mount permission helpers.318* Noticeably missing is inode size update, which is more complex319* as it requires pagecache updates.320*321* If the inode has been found through an idmapped mount the idmap of322* the vfsmount must be passed through @idmap. This function will then323* take care to map the inode according to @idmap before checking324* permissions. On non-idmapped mounts or if permission checking is to be325* performed on the raw inode simply pass @nop_mnt_idmap.326*327* The inode is not marked as dirty after this operation. The rationale is328* that for "simple" filesystems, the struct inode is the inode storage.329* The caller is free to mark the inode dirty afterwards if needed.330*/331void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,332const struct iattr *attr)333{334unsigned int ia_valid = attr->ia_valid;335336i_uid_update(idmap, attr, inode);337i_gid_update(idmap, attr, inode);338if (ia_valid & ATTR_MODE) {339umode_t mode = attr->ia_mode;340if (!in_group_or_capable(idmap, inode,341i_gid_into_vfsgid(idmap, inode)))342mode &= ~S_ISGID;343inode->i_mode = mode;344}345346if (is_mgtime(inode))347return setattr_copy_mgtime(inode, attr);348349if (ia_valid & ATTR_ATIME)350inode_set_atime_to_ts(inode, attr->ia_atime);351if (ia_valid & ATTR_MTIME)352inode_set_mtime_to_ts(inode, attr->ia_mtime);353354if (ia_valid & ATTR_CTIME_SET)355inode_set_ctime_deleg(inode, attr->ia_ctime);356else if (ia_valid & ATTR_CTIME)357inode_set_ctime_to_ts(inode, attr->ia_ctime);358}359EXPORT_SYMBOL(setattr_copy);360361int may_setattr(struct mnt_idmap *idmap, struct inode *inode,362unsigned int ia_valid)363{364int error;365366if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {367if (IS_IMMUTABLE(inode) || IS_APPEND(inode))368return -EPERM;369}370371/*372* If utimes(2) and friends are called with times == NULL (or both373* times are UTIME_NOW), then we need to check for write permission374*/375if (ia_valid & ATTR_TOUCH) {376if (IS_IMMUTABLE(inode))377return -EPERM;378379if (!inode_owner_or_capable(idmap, inode)) {380error = inode_permission(idmap, inode, MAY_WRITE);381if (error)382return error;383}384}385return 0;386}387EXPORT_SYMBOL(may_setattr);388389/**390* notify_change - modify attributes of a filesystem object391* @idmap: idmap of the mount the inode was found from392* @dentry: object affected393* @attr: new attributes394* @delegated_inode: returns inode, if the inode is delegated395*396* The caller must hold the i_rwsem exclusively on the affected object.397*398* If notify_change discovers a delegation in need of breaking,399* it will return -EWOULDBLOCK and return a reference to the inode in400* delegated_inode. The caller should then break the delegation and401* retry. Because breaking a delegation may take a long time, the402* caller should drop the i_rwsem before doing so.403*404* Alternatively, a caller may pass NULL for delegated_inode. This may405* be appropriate for callers that expect the underlying filesystem not406* to be NFS exported. Also, passing NULL is fine for callers holding407* the file open for write, as there can be no conflicting delegation in408* that case.409*410* If the inode has been found through an idmapped mount the idmap of411* the vfsmount must be passed through @idmap. This function will then412* take care to map the inode according to @idmap before checking413* permissions. On non-idmapped mounts or if permission checking is to be414* performed on the raw inode simply pass @nop_mnt_idmap.415*/416int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,417struct iattr *attr, struct inode **delegated_inode)418{419struct inode *inode = dentry->d_inode;420umode_t mode = inode->i_mode;421int error;422struct timespec64 now;423unsigned int ia_valid = attr->ia_valid;424425WARN_ON_ONCE(!inode_is_locked(inode));426427error = may_setattr(idmap, inode, ia_valid);428if (error)429return error;430431if ((ia_valid & ATTR_MODE)) {432/*433* Don't allow changing the mode of symlinks:434*435* (1) The vfs doesn't take the mode of symlinks into account436* during permission checking.437* (2) This has never worked correctly. Most major filesystems438* did return EOPNOTSUPP due to interactions with POSIX ACLs439* but did still updated the mode of the symlink.440* This inconsistency led system call wrapper providers such441* as libc to block changing the mode of symlinks with442* EOPNOTSUPP already.443* (3) To even do this in the first place one would have to use444* specific file descriptors and quite some effort.445*/446if (S_ISLNK(inode->i_mode))447return -EOPNOTSUPP;448449/* Flag setting protected by i_rwsem */450if (is_sxid(attr->ia_mode))451inode->i_flags &= ~S_NOSEC;452}453454now = current_time(inode);455456if (ia_valid & ATTR_ATIME_SET)457attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);458else459attr->ia_atime = now;460if (ia_valid & ATTR_CTIME_SET)461attr->ia_ctime = timestamp_truncate(attr->ia_ctime, inode);462else463attr->ia_ctime = now;464if (ia_valid & ATTR_MTIME_SET)465attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);466else467attr->ia_mtime = now;468469if (ia_valid & ATTR_KILL_PRIV) {470error = security_inode_need_killpriv(dentry);471if (error < 0)472return error;473if (error == 0)474ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;475}476477/*478* We now pass ATTR_KILL_S*ID to the lower level setattr function so479* that the function has the ability to reinterpret a mode change480* that's due to these bits. This adds an implicit restriction that481* no function will ever call notify_change with both ATTR_MODE and482* ATTR_KILL_S*ID set.483*/484if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&485(ia_valid & ATTR_MODE))486BUG();487488if (ia_valid & ATTR_KILL_SUID) {489if (mode & S_ISUID) {490ia_valid = attr->ia_valid |= ATTR_MODE;491attr->ia_mode = (inode->i_mode & ~S_ISUID);492}493}494if (ia_valid & ATTR_KILL_SGID) {495if (mode & S_ISGID) {496if (!(ia_valid & ATTR_MODE)) {497ia_valid = attr->ia_valid |= ATTR_MODE;498attr->ia_mode = inode->i_mode;499}500attr->ia_mode &= ~S_ISGID;501}502}503if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))504return 0;505506/*507* Verify that uid/gid changes are valid in the target508* namespace of the superblock.509*/510if (ia_valid & ATTR_UID &&511!vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns,512attr->ia_vfsuid))513return -EOVERFLOW;514if (ia_valid & ATTR_GID &&515!vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns,516attr->ia_vfsgid))517return -EOVERFLOW;518519/* Don't allow modifications of files with invalid uids or520* gids unless those uids & gids are being made valid.521*/522if (!(ia_valid & ATTR_UID) &&523!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)))524return -EOVERFLOW;525if (!(ia_valid & ATTR_GID) &&526!vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))527return -EOVERFLOW;528529error = security_inode_setattr(idmap, dentry, attr);530if (error)531return error;532533/*534* If ATTR_DELEG is set, then these attributes are being set on535* behalf of the holder of a write delegation. We want to avoid536* breaking the delegation in this case.537*/538if (!(ia_valid & ATTR_DELEG)) {539error = try_break_deleg(inode, delegated_inode);540if (error)541return error;542}543544if (inode->i_op->setattr)545error = inode->i_op->setattr(idmap, dentry, attr);546else547error = simple_setattr(idmap, dentry, attr);548549if (!error) {550fsnotify_change(dentry, ia_valid);551security_inode_post_setattr(idmap, dentry, ia_valid);552}553554return error;555}556EXPORT_SYMBOL(notify_change);557558559