1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/attr.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds * changes by Thomas Schoebel-Theuer 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 9630d9c47SPaul Gortmaker #include <linux/export.h> 101da177e4SLinus Torvalds #include <linux/time.h> 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/string.h> 133f07c014SIngo Molnar #include <linux/sched/signal.h> 1416f7e0feSRandy Dunlap #include <linux/capability.h> 150eeca283SRobert Love #include <linux/fsnotify.h> 161da177e4SLinus Torvalds #include <linux/fcntl.h> 171da177e4SLinus Torvalds #include <linux/security.h> 18975d2943SMimi Zohar #include <linux/evm.h> 199957a504SMimi Zohar #include <linux/ima.h> 201da177e4SLinus Torvalds 210031181cSEric W. Biederman static bool chown_ok(const struct inode *inode, kuid_t uid) 220031181cSEric W. Biederman { 230031181cSEric W. Biederman if (uid_eq(current_fsuid(), inode->i_uid) && 240031181cSEric W. Biederman uid_eq(uid, inode->i_uid)) 250031181cSEric W. Biederman return true; 260031181cSEric W. Biederman if (capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 270031181cSEric W. Biederman return true; 280031181cSEric W. Biederman if (uid_eq(inode->i_uid, INVALID_UID) && 290031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 300031181cSEric W. Biederman return true; 310031181cSEric W. Biederman return false; 320031181cSEric W. Biederman } 330031181cSEric W. Biederman 340031181cSEric W. Biederman static bool chgrp_ok(const struct inode *inode, kgid_t gid) 350031181cSEric W. Biederman { 360031181cSEric W. Biederman if (uid_eq(current_fsuid(), inode->i_uid) && 370031181cSEric W. Biederman (in_group_p(gid) || gid_eq(gid, inode->i_gid))) 380031181cSEric W. Biederman return true; 390031181cSEric W. Biederman if (capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 400031181cSEric W. Biederman return true; 410031181cSEric W. Biederman if (gid_eq(inode->i_gid, INVALID_GID) && 420031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 430031181cSEric W. Biederman return true; 440031181cSEric W. Biederman return false; 450031181cSEric W. Biederman } 460031181cSEric W. Biederman 472c27c65eSChristoph Hellwig /** 4831051c85SJan Kara * setattr_prepare - check if attribute changes to a dentry are allowed 4931051c85SJan Kara * @dentry: dentry to check 502c27c65eSChristoph Hellwig * @attr: attributes to change 512c27c65eSChristoph Hellwig * 522c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr 5331051c85SJan Kara * in the given dentry. This includes the normal unix access permission 5431051c85SJan Kara * checks, as well as checks for rlimits and others. The function also clears 5531051c85SJan Kara * SGID bit from mode if user is not allowed to set it. Also file capabilities 5631051c85SJan Kara * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. 572c27c65eSChristoph Hellwig * 582c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations, 592c27c65eSChristoph Hellwig * possibly after taking additional locks. 602c27c65eSChristoph Hellwig */ 6131051c85SJan Kara int setattr_prepare(struct dentry *dentry, struct iattr *attr) 621da177e4SLinus Torvalds { 6331051c85SJan Kara struct inode *inode = d_inode(dentry); 641da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 651da177e4SLinus Torvalds 662c27c65eSChristoph Hellwig /* 672c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using 682c27c65eSChristoph Hellwig * ATTR_FORCE. 692c27c65eSChristoph Hellwig */ 702c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) { 712c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size); 722c27c65eSChristoph Hellwig if (error) 732c27c65eSChristoph Hellwig return error; 742c27c65eSChristoph Hellwig } 752c27c65eSChristoph Hellwig 761da177e4SLinus Torvalds /* If force is set do it anyway. */ 771da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE) 78030b533cSJan Kara goto kill_priv; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /* Make sure a caller can chown. */ 810031181cSEric W. Biederman if ((ia_valid & ATTR_UID) && !chown_ok(inode, attr->ia_uid)) 822c27c65eSChristoph Hellwig return -EPERM; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* Make sure caller can chgrp. */ 850031181cSEric W. Biederman if ((ia_valid & ATTR_GID) && !chgrp_ok(inode, attr->ia_gid)) 862c27c65eSChristoph Hellwig return -EPERM; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* Make sure a caller can chmod. */ 891da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 902e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 912c27c65eSChristoph Hellwig return -EPERM; 921da177e4SLinus Torvalds /* Also check the setgid bit! */ 931da177e4SLinus Torvalds if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid : 947fa294c8SEric W. Biederman inode->i_gid) && 9523adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_FSETID)) 961da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* Check for setting the inode time. */ 1009767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { 1012e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1022c27c65eSChristoph Hellwig return -EPERM; 1031da177e4SLinus Torvalds } 1042c27c65eSChristoph Hellwig 105030b533cSJan Kara kill_priv: 106030b533cSJan Kara /* User has permission for the change */ 107030b533cSJan Kara if (ia_valid & ATTR_KILL_PRIV) { 108030b533cSJan Kara int error; 109030b533cSJan Kara 110030b533cSJan Kara error = security_inode_killpriv(dentry); 111030b533cSJan Kara if (error) 112030b533cSJan Kara return error; 113030b533cSJan Kara } 114030b533cSJan Kara 1152c27c65eSChristoph Hellwig return 0; 1161da177e4SLinus Torvalds } 11731051c85SJan Kara EXPORT_SYMBOL(setattr_prepare); 1181da177e4SLinus Torvalds 11925d9e2d1Snpiggin@suse.de /** 12025d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size 12125d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated 12225d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode 12325d9e2d1Snpiggin@suse.de * 1247bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 1257bb46a67Snpiggin@suse.de * 12625d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 12725d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 12825d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 12925d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 13025d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 13125d9e2d1Snpiggin@suse.de * conditions). 1323fae1746SMatthew Wilcox * 1333fae1746SMatthew Wilcox * Return: 0 on success, -ve errno on failure 13425d9e2d1Snpiggin@suse.de */ 13525d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 13625d9e2d1Snpiggin@suse.de { 13725d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 13825d9e2d1Snpiggin@suse.de unsigned long limit; 13925d9e2d1Snpiggin@suse.de 140d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 14125d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 14225d9e2d1Snpiggin@suse.de goto out_sig; 14325d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 14425d9e2d1Snpiggin@suse.de goto out_big; 14525d9e2d1Snpiggin@suse.de } else { 14625d9e2d1Snpiggin@suse.de /* 14725d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 14825d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 14925d9e2d1Snpiggin@suse.de * blocks. 15025d9e2d1Snpiggin@suse.de */ 15125d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 15225d9e2d1Snpiggin@suse.de return -ETXTBSY; 15325d9e2d1Snpiggin@suse.de } 15425d9e2d1Snpiggin@suse.de 15525d9e2d1Snpiggin@suse.de return 0; 15625d9e2d1Snpiggin@suse.de out_sig: 15725d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 15825d9e2d1Snpiggin@suse.de out_big: 15925d9e2d1Snpiggin@suse.de return -EFBIG; 16025d9e2d1Snpiggin@suse.de } 16125d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 16225d9e2d1Snpiggin@suse.de 1637bb46a67Snpiggin@suse.de /** 1646a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 1657bb46a67Snpiggin@suse.de * @inode: the inode to be updated 1667bb46a67Snpiggin@suse.de * @attr: the new attributes 1677bb46a67Snpiggin@suse.de * 1686a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 1697bb46a67Snpiggin@suse.de * 1706a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 17125985edcSLucas De Marchi * in attr. Noticeably missing is inode size update, which is more complex 1722c27c65eSChristoph Hellwig * as it requires pagecache updates. 1737bb46a67Snpiggin@suse.de * 1747bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 1757bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 1767bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 1777bb46a67Snpiggin@suse.de */ 1786a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds if (ia_valid & ATTR_UID) 1831da177e4SLinus Torvalds inode->i_uid = attr->ia_uid; 1841da177e4SLinus Torvalds if (ia_valid & ATTR_GID) 1851da177e4SLinus Torvalds inode->i_gid = attr->ia_gid; 186*eb31e2f6SAmir Goldstein if (ia_valid & ATTR_ATIME) 187*eb31e2f6SAmir Goldstein inode->i_atime = attr->ia_atime; 188*eb31e2f6SAmir Goldstein if (ia_valid & ATTR_MTIME) 189*eb31e2f6SAmir Goldstein inode->i_mtime = attr->ia_mtime; 190*eb31e2f6SAmir Goldstein if (ia_valid & ATTR_CTIME) 191*eb31e2f6SAmir Goldstein inode->i_ctime = attr->ia_ctime; 1921da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 1931da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 1941da177e4SLinus Torvalds 1957fa294c8SEric W. Biederman if (!in_group_p(inode->i_gid) && 19623adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_FSETID)) 1971da177e4SLinus Torvalds mode &= ~S_ISGID; 1981da177e4SLinus Torvalds inode->i_mode = mode; 1991da177e4SLinus Torvalds } 2007bb46a67Snpiggin@suse.de } 2016a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 2027bb46a67Snpiggin@suse.de 20327ac0ffeSJ. Bruce Fields /** 20427ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 20527ac0ffeSJ. Bruce Fields * @dentry: object affected 2063fae1746SMatthew Wilcox * @attr: new attributes 20727ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 20827ac0ffeSJ. Bruce Fields * 20927ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 21027ac0ffeSJ. Bruce Fields * 21127ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 21227ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 21327ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 21427ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 21527ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 21627ac0ffeSJ. Bruce Fields * 21727ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 21827ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 21927ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 22027ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 22127ac0ffeSJ. Bruce Fields * that case. 22227ac0ffeSJ. Bruce Fields */ 22327ac0ffeSJ. Bruce Fields int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2268d334acdSAl Viro umode_t mode = inode->i_mode; 2271da177e4SLinus Torvalds int error; 22895582b00SDeepa Dinamani struct timespec64 now; 2291da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 2301da177e4SLinus Torvalds 2315955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode)); 232c4107b30SAndrew Morton 233beb29e05SMiklos Szeredi if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 234beb29e05SMiklos Szeredi if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 235beb29e05SMiklos Szeredi return -EPERM; 236beb29e05SMiklos Szeredi } 237beb29e05SMiklos Szeredi 238f2b20f6eSMiklos Szeredi /* 239f2b20f6eSMiklos Szeredi * If utimes(2) and friends are called with times == NULL (or both 240f2b20f6eSMiklos Szeredi * times are UTIME_NOW), then we need to check for write permission 241f2b20f6eSMiklos Szeredi */ 242f2b20f6eSMiklos Szeredi if (ia_valid & ATTR_TOUCH) { 243f2b20f6eSMiklos Szeredi if (IS_IMMUTABLE(inode)) 244f2b20f6eSMiklos Szeredi return -EPERM; 245f2b20f6eSMiklos Szeredi 246f2b20f6eSMiklos Szeredi if (!inode_owner_or_capable(inode)) { 247f2b20f6eSMiklos Szeredi error = inode_permission(inode, MAY_WRITE); 248f2b20f6eSMiklos Szeredi if (error) 249f2b20f6eSMiklos Szeredi return error; 250f2b20f6eSMiklos Szeredi } 251f2b20f6eSMiklos Szeredi } 252f2b20f6eSMiklos Szeredi 25369b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 2548d334acdSAl Viro umode_t amode = attr->ia_mode; 25569b45732SAndi Kleen /* Flag setting protected by i_mutex */ 25669b45732SAndi Kleen if (is_sxid(amode)) 25769b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 25869b45732SAndi Kleen } 25969b45732SAndi Kleen 260c2050a45SDeepa Dinamani now = current_time(inode); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds attr->ia_ctime = now; 2631da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 2641da177e4SLinus Torvalds attr->ia_atime = now; 265*eb31e2f6SAmir Goldstein else 266*eb31e2f6SAmir Goldstein attr->ia_atime = timestamp_truncate(attr->ia_atime, inode); 2671da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 2681da177e4SLinus Torvalds attr->ia_mtime = now; 269*eb31e2f6SAmir Goldstein else 270*eb31e2f6SAmir Goldstein attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); 271*eb31e2f6SAmir Goldstein 272b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 273b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 274030b533cSJan Kara if (error < 0) 275b5376771SSerge E. Hallyn return error; 276030b533cSJan Kara if (error == 0) 277030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; 278b5376771SSerge E. Hallyn } 2796de0ec00SJeff Layton 2806de0ec00SJeff Layton /* 2816de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 2826de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 2836de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 2846de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 2856de0ec00SJeff Layton * ATTR_KILL_S*ID set. 2866de0ec00SJeff Layton */ 2876de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 2886de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 2896de0ec00SJeff Layton BUG(); 2906de0ec00SJeff Layton 2911da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 2921da177e4SLinus Torvalds if (mode & S_ISUID) { 2931da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2946de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 2981da177e4SLinus Torvalds if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 2991da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 3001da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 3011da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds } 3066de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 3071da177e4SLinus Torvalds return 0; 3081da177e4SLinus Torvalds 309a475acf0SSeth Forshee /* 310a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target 311a475acf0SSeth Forshee * namespace of the superblock. 312a475acf0SSeth Forshee */ 313a475acf0SSeth Forshee if (ia_valid & ATTR_UID && 314a475acf0SSeth Forshee !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) 315a475acf0SSeth Forshee return -EOVERFLOW; 316a475acf0SSeth Forshee if (ia_valid & ATTR_GID && 317a475acf0SSeth Forshee !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) 318a475acf0SSeth Forshee return -EOVERFLOW; 319a475acf0SSeth Forshee 3200bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or 3210bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid. 3220bd23d09SEric W. Biederman */ 3230bd23d09SEric W. Biederman if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid)) 3240bd23d09SEric W. Biederman return -EOVERFLOW; 3250bd23d09SEric W. Biederman if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid)) 3260bd23d09SEric W. Biederman return -EOVERFLOW; 3270bd23d09SEric W. Biederman 328a77b72daSMiklos Szeredi error = security_inode_setattr(dentry, attr); 329a77b72daSMiklos Szeredi if (error) 330a77b72daSMiklos Szeredi return error; 33127ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 33227ac0ffeSJ. Bruce Fields if (error) 33327ac0ffeSJ. Bruce Fields return error; 334a77b72daSMiklos Szeredi 335eef2380cSChristoph Hellwig if (inode->i_op->setattr) 3361da177e4SLinus Torvalds error = inode->i_op->setattr(dentry, attr); 337eef2380cSChristoph Hellwig else 338eef2380cSChristoph Hellwig error = simple_setattr(dentry, attr); 3391da177e4SLinus Torvalds 340975d2943SMimi Zohar if (!error) { 3410eeca283SRobert Love fsnotify_change(dentry, ia_valid); 3429957a504SMimi Zohar ima_inode_post_setattr(dentry); 343975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 344975d2943SMimi Zohar } 3450eeca283SRobert Love 3461da177e4SLinus Torvalds return error; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 349