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 21*0031181cSEric W. Biederman static bool chown_ok(const struct inode *inode, kuid_t uid) 22*0031181cSEric W. Biederman { 23*0031181cSEric W. Biederman if (uid_eq(current_fsuid(), inode->i_uid) && 24*0031181cSEric W. Biederman uid_eq(uid, inode->i_uid)) 25*0031181cSEric W. Biederman return true; 26*0031181cSEric W. Biederman if (capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 27*0031181cSEric W. Biederman return true; 28*0031181cSEric W. Biederman if (uid_eq(inode->i_uid, INVALID_UID) && 29*0031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 30*0031181cSEric W. Biederman return true; 31*0031181cSEric W. Biederman return false; 32*0031181cSEric W. Biederman } 33*0031181cSEric W. Biederman 34*0031181cSEric W. Biederman static bool chgrp_ok(const struct inode *inode, kgid_t gid) 35*0031181cSEric W. Biederman { 36*0031181cSEric W. Biederman if (uid_eq(current_fsuid(), inode->i_uid) && 37*0031181cSEric W. Biederman (in_group_p(gid) || gid_eq(gid, inode->i_gid))) 38*0031181cSEric W. Biederman return true; 39*0031181cSEric W. Biederman if (capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 40*0031181cSEric W. Biederman return true; 41*0031181cSEric W. Biederman if (gid_eq(inode->i_gid, INVALID_GID) && 42*0031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) 43*0031181cSEric W. Biederman return true; 44*0031181cSEric W. Biederman return false; 45*0031181cSEric W. Biederman } 46*0031181cSEric 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. */ 81*0031181cSEric 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. */ 85*0031181cSEric 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 * @Returns: 0 on success, -ve errno on failure 12425d9e2d1Snpiggin@suse.de * 1257bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 1267bb46a67Snpiggin@suse.de * 12725d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 12825d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 12925d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 13025d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 13125d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 13225d9e2d1Snpiggin@suse.de * conditions). 13325d9e2d1Snpiggin@suse.de */ 13425d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 13525d9e2d1Snpiggin@suse.de { 13625d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 13725d9e2d1Snpiggin@suse.de unsigned long limit; 13825d9e2d1Snpiggin@suse.de 139d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 14025d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 14125d9e2d1Snpiggin@suse.de goto out_sig; 14225d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 14325d9e2d1Snpiggin@suse.de goto out_big; 14425d9e2d1Snpiggin@suse.de } else { 14525d9e2d1Snpiggin@suse.de /* 14625d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 14725d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 14825d9e2d1Snpiggin@suse.de * blocks. 14925d9e2d1Snpiggin@suse.de */ 15025d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 15125d9e2d1Snpiggin@suse.de return -ETXTBSY; 15225d9e2d1Snpiggin@suse.de } 15325d9e2d1Snpiggin@suse.de 15425d9e2d1Snpiggin@suse.de return 0; 15525d9e2d1Snpiggin@suse.de out_sig: 15625d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 15725d9e2d1Snpiggin@suse.de out_big: 15825d9e2d1Snpiggin@suse.de return -EFBIG; 15925d9e2d1Snpiggin@suse.de } 16025d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 16125d9e2d1Snpiggin@suse.de 1627bb46a67Snpiggin@suse.de /** 1636a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 1647bb46a67Snpiggin@suse.de * @inode: the inode to be updated 1657bb46a67Snpiggin@suse.de * @attr: the new attributes 1667bb46a67Snpiggin@suse.de * 1676a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 1687bb46a67Snpiggin@suse.de * 1696a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 17025985edcSLucas De Marchi * in attr. Noticeably missing is inode size update, which is more complex 1712c27c65eSChristoph Hellwig * as it requires pagecache updates. 1727bb46a67Snpiggin@suse.de * 1737bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 1747bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 1757bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 1767bb46a67Snpiggin@suse.de */ 1776a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds if (ia_valid & ATTR_UID) 1821da177e4SLinus Torvalds inode->i_uid = attr->ia_uid; 1831da177e4SLinus Torvalds if (ia_valid & ATTR_GID) 1841da177e4SLinus Torvalds inode->i_gid = attr->ia_gid; 1851da177e4SLinus Torvalds if (ia_valid & ATTR_ATIME) 1861da177e4SLinus Torvalds inode->i_atime = timespec_trunc(attr->ia_atime, 1871da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1881da177e4SLinus Torvalds if (ia_valid & ATTR_MTIME) 1891da177e4SLinus Torvalds inode->i_mtime = timespec_trunc(attr->ia_mtime, 1901da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1911da177e4SLinus Torvalds if (ia_valid & ATTR_CTIME) 1921da177e4SLinus Torvalds inode->i_ctime = timespec_trunc(attr->ia_ctime, 1931da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1941da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 1951da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 1961da177e4SLinus Torvalds 1977fa294c8SEric W. Biederman if (!in_group_p(inode->i_gid) && 19823adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_FSETID)) 1991da177e4SLinus Torvalds mode &= ~S_ISGID; 2001da177e4SLinus Torvalds inode->i_mode = mode; 2011da177e4SLinus Torvalds } 2027bb46a67Snpiggin@suse.de } 2036a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 2047bb46a67Snpiggin@suse.de 20527ac0ffeSJ. Bruce Fields /** 20627ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 20727ac0ffeSJ. Bruce Fields * @dentry: object affected 20827ac0ffeSJ. Bruce Fields * @iattr: new attributes 20927ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 21027ac0ffeSJ. Bruce Fields * 21127ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 21227ac0ffeSJ. Bruce Fields * 21327ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 21427ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 21527ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 21627ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 21727ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 21827ac0ffeSJ. Bruce Fields * 21927ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 22027ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 22127ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 22227ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 22327ac0ffeSJ. Bruce Fields * that case. 22427ac0ffeSJ. Bruce Fields */ 22527ac0ffeSJ. Bruce Fields int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode) 2261da177e4SLinus Torvalds { 2271da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2288d334acdSAl Viro umode_t mode = inode->i_mode; 2291da177e4SLinus Torvalds int error; 2301da177e4SLinus Torvalds struct timespec now; 2311da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 2321da177e4SLinus Torvalds 2335955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode)); 234c4107b30SAndrew Morton 235beb29e05SMiklos Szeredi if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 236beb29e05SMiklos Szeredi if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 237beb29e05SMiklos Szeredi return -EPERM; 238beb29e05SMiklos Szeredi } 239beb29e05SMiklos Szeredi 240f2b20f6eSMiklos Szeredi /* 241f2b20f6eSMiklos Szeredi * If utimes(2) and friends are called with times == NULL (or both 242f2b20f6eSMiklos Szeredi * times are UTIME_NOW), then we need to check for write permission 243f2b20f6eSMiklos Szeredi */ 244f2b20f6eSMiklos Szeredi if (ia_valid & ATTR_TOUCH) { 245f2b20f6eSMiklos Szeredi if (IS_IMMUTABLE(inode)) 246f2b20f6eSMiklos Szeredi return -EPERM; 247f2b20f6eSMiklos Szeredi 248f2b20f6eSMiklos Szeredi if (!inode_owner_or_capable(inode)) { 249f2b20f6eSMiklos Szeredi error = inode_permission(inode, MAY_WRITE); 250f2b20f6eSMiklos Szeredi if (error) 251f2b20f6eSMiklos Szeredi return error; 252f2b20f6eSMiklos Szeredi } 253f2b20f6eSMiklos Szeredi } 254f2b20f6eSMiklos Szeredi 25569b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 2568d334acdSAl Viro umode_t amode = attr->ia_mode; 25769b45732SAndi Kleen /* Flag setting protected by i_mutex */ 25869b45732SAndi Kleen if (is_sxid(amode)) 25969b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 26069b45732SAndi Kleen } 26169b45732SAndi Kleen 262c2050a45SDeepa Dinamani now = current_time(inode); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds attr->ia_ctime = now; 2651da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 2661da177e4SLinus Torvalds attr->ia_atime = now; 2671da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 2681da177e4SLinus Torvalds attr->ia_mtime = now; 269b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 270b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 271030b533cSJan Kara if (error < 0) 272b5376771SSerge E. Hallyn return error; 273030b533cSJan Kara if (error == 0) 274030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; 275b5376771SSerge E. Hallyn } 2766de0ec00SJeff Layton 2776de0ec00SJeff Layton /* 2786de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 2796de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 2806de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 2816de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 2826de0ec00SJeff Layton * ATTR_KILL_S*ID set. 2836de0ec00SJeff Layton */ 2846de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 2856de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 2866de0ec00SJeff Layton BUG(); 2876de0ec00SJeff Layton 2881da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 2891da177e4SLinus Torvalds if (mode & S_ISUID) { 2901da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2916de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 2951da177e4SLinus Torvalds if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 2961da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 2971da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2981da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds } 3036de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 3041da177e4SLinus Torvalds return 0; 3051da177e4SLinus Torvalds 306a475acf0SSeth Forshee /* 307a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target 308a475acf0SSeth Forshee * namespace of the superblock. 309a475acf0SSeth Forshee */ 310a475acf0SSeth Forshee if (ia_valid & ATTR_UID && 311a475acf0SSeth Forshee !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) 312a475acf0SSeth Forshee return -EOVERFLOW; 313a475acf0SSeth Forshee if (ia_valid & ATTR_GID && 314a475acf0SSeth Forshee !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) 315a475acf0SSeth Forshee return -EOVERFLOW; 316a475acf0SSeth Forshee 3170bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or 3180bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid. 3190bd23d09SEric W. Biederman */ 3200bd23d09SEric W. Biederman if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid)) 3210bd23d09SEric W. Biederman return -EOVERFLOW; 3220bd23d09SEric W. Biederman if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid)) 3230bd23d09SEric W. Biederman return -EOVERFLOW; 3240bd23d09SEric W. Biederman 325a77b72daSMiklos Szeredi error = security_inode_setattr(dentry, attr); 326a77b72daSMiklos Szeredi if (error) 327a77b72daSMiklos Szeredi return error; 32827ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 32927ac0ffeSJ. Bruce Fields if (error) 33027ac0ffeSJ. Bruce Fields return error; 331a77b72daSMiklos Szeredi 332eef2380cSChristoph Hellwig if (inode->i_op->setattr) 3331da177e4SLinus Torvalds error = inode->i_op->setattr(dentry, attr); 334eef2380cSChristoph Hellwig else 335eef2380cSChristoph Hellwig error = simple_setattr(dentry, attr); 3361da177e4SLinus Torvalds 337975d2943SMimi Zohar if (!error) { 3380eeca283SRobert Love fsnotify_change(dentry, ia_valid); 3399957a504SMimi Zohar ima_inode_post_setattr(dentry); 340975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 341975d2943SMimi Zohar } 3420eeca283SRobert Love 3431da177e4SLinus Torvalds return error; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 346