// 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;172173/*174* Verity files are immutable, so deny truncates. This isn't175* covered by the open-time check because sys_truncate() takes a176* path, not an open file.177*/178if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))179return -EPERM;180181error = inode_newsize_ok(inode, attr->ia_size);182if (error)183return error;184}185186/* If force is set do it anyway. */187if (ia_valid & ATTR_FORCE)188goto kill_priv;189190/* Make sure a caller can chown. */191if ((ia_valid & ATTR_UID) &&192!chown_ok(idmap, inode, attr->ia_vfsuid))193return -EPERM;194195/* Make sure caller can chgrp. */196if ((ia_valid & ATTR_GID) &&197!chgrp_ok(idmap, inode, attr->ia_vfsgid))198return -EPERM;199200/* Make sure a caller can chmod. */201if (ia_valid & ATTR_MODE) {202vfsgid_t vfsgid;203204if (!inode_owner_or_capable(idmap, inode))205return -EPERM;206207if (ia_valid & ATTR_GID)208vfsgid = attr->ia_vfsgid;209else210vfsgid = i_gid_into_vfsgid(idmap, inode);211212/* Also check the setgid bit! */213if (!in_group_or_capable(idmap, inode, vfsgid))214attr->ia_mode &= ~S_ISGID;215}216217/* Check for setting the inode time. */218if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {219if (!inode_owner_or_capable(idmap, inode))220return -EPERM;221}222223kill_priv:224/* User has permission for the change */225if (ia_valid & ATTR_KILL_PRIV) {226int error;227228error = security_inode_killpriv(idmap, dentry);229if (error)230return error;231}232233return 0;234}235EXPORT_SYMBOL(setattr_prepare);236237/**238* inode_newsize_ok - may this inode be truncated to a given size239* @inode: the inode to be truncated240* @offset: the new size to assign to the inode241*242* inode_newsize_ok must be called with i_rwsem held exclusively.243*244* inode_newsize_ok will check filesystem limits and ulimits to check that the245* new inode size is within limits. inode_newsize_ok will also send SIGXFSZ246* when necessary. Caller must not proceed with inode size change if failure is247* returned. @inode must be a file (not directory), with appropriate248* permissions to allow truncate (inode_newsize_ok does NOT check these249* conditions).250*251* Return: 0 on success, -ve errno on failure252*/253int inode_newsize_ok(const struct inode *inode, loff_t offset)254{255if (offset < 0)256return -EINVAL;257if (inode->i_size < offset) {258unsigned long limit;259260limit = rlimit(RLIMIT_FSIZE);261if (limit != RLIM_INFINITY && offset > limit)262goto out_sig;263if (offset > inode->i_sb->s_maxbytes)264goto out_big;265} else {266/*267* truncation of in-use swapfiles is disallowed - it would268* cause subsequent swapout to scribble on the now-freed269* blocks.270*/271if (IS_SWAPFILE(inode))272return -ETXTBSY;273}274275return 0;276out_sig:277send_sig(SIGXFSZ, current, 0);278out_big:279return -EFBIG;280}281EXPORT_SYMBOL(inode_newsize_ok);282283/**284* setattr_copy_mgtime - update timestamps for mgtime inodes285* @inode: inode timestamps to be updated286* @attr: attrs for the update287*288* With multigrain timestamps, take more care to prevent races when289* updating the ctime. Always update the ctime to the very latest using290* the standard mechanism, and use that to populate the atime and mtime291* appropriately (unless those are being set to specific values).292*/293static void setattr_copy_mgtime(struct inode *inode, const struct iattr *attr)294{295unsigned int ia_valid = attr->ia_valid;296struct timespec64 now;297298if (ia_valid & ATTR_CTIME_SET)299now = inode_set_ctime_deleg(inode, attr->ia_ctime);300else if (ia_valid & ATTR_CTIME)301now = inode_set_ctime_current(inode);302else303now = current_time(inode);304305if (ia_valid & ATTR_ATIME_SET)306inode_set_atime_to_ts(inode, attr->ia_atime);307else if (ia_valid & ATTR_ATIME)308inode_set_atime_to_ts(inode, now);309310if (ia_valid & ATTR_MTIME_SET)311inode_set_mtime_to_ts(inode, attr->ia_mtime);312else if (ia_valid & ATTR_MTIME)313inode_set_mtime_to_ts(inode, now);314}315316/**317* setattr_copy - copy simple metadata updates into the generic inode318* @idmap: idmap of the mount the inode was found from319* @inode: the inode to be updated320* @attr: the new attributes321*322* setattr_copy must be called with i_rwsem held exclusively.323*324* setattr_copy updates the inode's metadata with that specified325* in attr on idmapped mounts. Necessary permission checks to determine326* whether or not the S_ISGID property needs to be removed are performed with327* the correct idmapped mount permission helpers.328* Noticeably missing is inode size update, which is more complex329* as it requires pagecache updates.330*331* If the inode has been found through an idmapped mount the idmap of332* the vfsmount must be passed through @idmap. This function will then333* take care to map the inode according to @idmap before checking334* permissions. On non-idmapped mounts or if permission checking is to be335* performed on the raw inode simply pass @nop_mnt_idmap.336*337* The inode is not marked as dirty after this operation. The rationale is338* that for "simple" filesystems, the struct inode is the inode storage.339* The caller is free to mark the inode dirty afterwards if needed.340*/341void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,342const struct iattr *attr)343{344unsigned int ia_valid = attr->ia_valid;345346i_uid_update(idmap, attr, inode);347i_gid_update(idmap, attr, inode);348if (ia_valid & ATTR_MODE) {349umode_t mode = attr->ia_mode;350if (!in_group_or_capable(idmap, inode,351i_gid_into_vfsgid(idmap, inode)))352mode &= ~S_ISGID;353inode->i_mode = mode;354}355356if (is_mgtime(inode))357return setattr_copy_mgtime(inode, attr);358359if (ia_valid & ATTR_ATIME)360inode_set_atime_to_ts(inode, attr->ia_atime);361if (ia_valid & ATTR_MTIME)362inode_set_mtime_to_ts(inode, attr->ia_mtime);363364if (ia_valid & ATTR_CTIME_SET)365inode_set_ctime_deleg(inode, attr->ia_ctime);366else if (ia_valid & ATTR_CTIME)367inode_set_ctime_to_ts(inode, attr->ia_ctime);368}369EXPORT_SYMBOL(setattr_copy);370371int may_setattr(struct mnt_idmap *idmap, struct inode *inode,372unsigned int ia_valid)373{374int error;375376if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {377if (IS_IMMUTABLE(inode) || IS_APPEND(inode))378return -EPERM;379}380381/*382* If utimes(2) and friends are called with times == NULL (or both383* times are UTIME_NOW), then we need to check for write permission384*/385if (ia_valid & ATTR_TOUCH) {386if (IS_IMMUTABLE(inode))387return -EPERM;388389if (!inode_owner_or_capable(idmap, inode)) {390error = inode_permission(idmap, inode, MAY_WRITE);391if (error)392return error;393}394}395return 0;396}397EXPORT_SYMBOL(may_setattr);398399/**400* notify_change - modify attributes of a filesystem object401* @idmap: idmap of the mount the inode was found from402* @dentry: object affected403* @attr: new attributes404* @delegated_inode: returns inode, if the inode is delegated405*406* The caller must hold the i_rwsem exclusively on the affected object.407*408* If notify_change discovers a delegation in need of breaking,409* it will return -EWOULDBLOCK and return a reference to the inode in410* delegated_inode. The caller should then break the delegation and411* retry. Because breaking a delegation may take a long time, the412* caller should drop the i_rwsem before doing so.413*414* Alternatively, a caller may pass NULL for delegated_inode. This may415* be appropriate for callers that expect the underlying filesystem not416* to be NFS exported. Also, passing NULL is fine for callers holding417* the file open for write, as there can be no conflicting delegation in418* that case.419*420* If the inode has been found through an idmapped mount the idmap of421* the vfsmount must be passed through @idmap. This function will then422* take care to map the inode according to @idmap before checking423* permissions. On non-idmapped mounts or if permission checking is to be424* performed on the raw inode simply pass @nop_mnt_idmap.425*/426int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,427struct iattr *attr, struct delegated_inode *delegated_inode)428{429struct inode *inode = dentry->d_inode;430umode_t mode = inode->i_mode;431int error;432struct timespec64 now;433unsigned int ia_valid = attr->ia_valid;434435WARN_ON_ONCE(!inode_is_locked(inode));436437error = may_setattr(idmap, inode, ia_valid);438if (error)439return error;440441if ((ia_valid & ATTR_MODE)) {442/*443* Don't allow changing the mode of symlinks:444*445* (1) The vfs doesn't take the mode of symlinks into account446* during permission checking.447* (2) This has never worked correctly. Most major filesystems448* did return EOPNOTSUPP due to interactions with POSIX ACLs449* but did still updated the mode of the symlink.450* This inconsistency led system call wrapper providers such451* as libc to block changing the mode of symlinks with452* EOPNOTSUPP already.453* (3) To even do this in the first place one would have to use454* specific file descriptors and quite some effort.455*/456if (S_ISLNK(inode->i_mode))457return -EOPNOTSUPP;458459/* Flag setting protected by i_rwsem */460if (is_sxid(attr->ia_mode))461inode->i_flags &= ~S_NOSEC;462}463464now = current_time(inode);465466if (ia_valid & ATTR_ATIME_SET)467attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);468else469attr->ia_atime = now;470if (ia_valid & ATTR_CTIME_SET)471attr->ia_ctime = timestamp_truncate(attr->ia_ctime, inode);472else473attr->ia_ctime = now;474if (ia_valid & ATTR_MTIME_SET)475attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);476else477attr->ia_mtime = now;478479if (ia_valid & ATTR_KILL_PRIV) {480error = security_inode_need_killpriv(dentry);481if (error < 0)482return error;483if (error == 0)484ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;485}486487/*488* We now pass ATTR_KILL_S*ID to the lower level setattr function so489* that the function has the ability to reinterpret a mode change490* that's due to these bits. This adds an implicit restriction that491* no function will ever call notify_change with both ATTR_MODE and492* ATTR_KILL_S*ID set.493*/494if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&495(ia_valid & ATTR_MODE))496BUG();497498if (ia_valid & ATTR_KILL_SUID) {499if (mode & S_ISUID) {500ia_valid = attr->ia_valid |= ATTR_MODE;501attr->ia_mode = (inode->i_mode & ~S_ISUID);502}503}504if (ia_valid & ATTR_KILL_SGID) {505if (mode & S_ISGID) {506if (!(ia_valid & ATTR_MODE)) {507ia_valid = attr->ia_valid |= ATTR_MODE;508attr->ia_mode = inode->i_mode;509}510attr->ia_mode &= ~S_ISGID;511}512}513if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))514return 0;515516/*517* Verify that uid/gid changes are valid in the target518* namespace of the superblock.519*/520if (ia_valid & ATTR_UID &&521!vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns,522attr->ia_vfsuid))523return -EOVERFLOW;524if (ia_valid & ATTR_GID &&525!vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns,526attr->ia_vfsgid))527return -EOVERFLOW;528529/* Don't allow modifications of files with invalid uids or530* gids unless those uids & gids are being made valid.531*/532if (!(ia_valid & ATTR_UID) &&533!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)))534return -EOVERFLOW;535if (!(ia_valid & ATTR_GID) &&536!vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))537return -EOVERFLOW;538539error = security_inode_setattr(idmap, dentry, attr);540if (error)541return error;542543/*544* If ATTR_DELEG is set, then these attributes are being set on545* behalf of the holder of a write delegation. We want to avoid546* breaking the delegation in this case.547*/548if (!(ia_valid & ATTR_DELEG)) {549error = try_break_deleg(inode, delegated_inode);550if (error)551return error;552}553554if (inode->i_op->setattr)555error = inode->i_op->setattr(idmap, dentry, attr);556else557error = simple_setattr(idmap, dentry, attr);558559if (!error) {560fsnotify_change(dentry, ia_valid);561security_inode_post_setattr(idmap, dentry, ia_valid);562}563564return error;565}566EXPORT_SYMBOL(notify_change);567568569