11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/attr.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * changes by Thomas Schoebel-Theuer 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 8630d9c47SPaul Gortmaker #include <linux/export.h> 91da177e4SLinus Torvalds #include <linux/time.h> 101da177e4SLinus Torvalds #include <linux/mm.h> 111da177e4SLinus Torvalds #include <linux/string.h> 1216f7e0feSRandy Dunlap #include <linux/capability.h> 130eeca283SRobert Love #include <linux/fsnotify.h> 141da177e4SLinus Torvalds #include <linux/fcntl.h> 151da177e4SLinus Torvalds #include <linux/security.h> 16975d2943SMimi Zohar #include <linux/evm.h> 179957a504SMimi Zohar #include <linux/ima.h> 181da177e4SLinus Torvalds 192c27c65eSChristoph Hellwig /** 202c27c65eSChristoph Hellwig * inode_change_ok - check if attribute changes to an inode are allowed 212c27c65eSChristoph Hellwig * @inode: inode to check 222c27c65eSChristoph Hellwig * @attr: attributes to change 232c27c65eSChristoph Hellwig * 242c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr 252c27c65eSChristoph Hellwig * in the given inode. This includes the normal unix access permission 262c27c65eSChristoph Hellwig * checks, as well as checks for rlimits and others. 272c27c65eSChristoph Hellwig * 282c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations, 292c27c65eSChristoph Hellwig * possibly after taking additional locks. 302c27c65eSChristoph Hellwig */ 3125d9e2d1Snpiggin@suse.de int inode_change_ok(const struct inode *inode, struct iattr *attr) 321da177e4SLinus Torvalds { 331da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 341da177e4SLinus Torvalds 352c27c65eSChristoph Hellwig /* 362c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using 372c27c65eSChristoph Hellwig * ATTR_FORCE. 382c27c65eSChristoph Hellwig */ 392c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) { 402c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size); 412c27c65eSChristoph Hellwig if (error) 422c27c65eSChristoph Hellwig return error; 432c27c65eSChristoph Hellwig } 442c27c65eSChristoph Hellwig 451da177e4SLinus Torvalds /* If force is set do it anyway. */ 461da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE) 472c27c65eSChristoph Hellwig return 0; 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds /* Make sure a caller can chown. */ 501da177e4SLinus Torvalds if ((ia_valid & ATTR_UID) && 518e96e3b7SEric W. Biederman (!uid_eq(current_fsuid(), inode->i_uid) || 527fa294c8SEric W. Biederman !uid_eq(attr->ia_uid, inode->i_uid)) && 537fa294c8SEric W. Biederman !inode_capable(inode, CAP_CHOWN)) 542c27c65eSChristoph Hellwig return -EPERM; 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* Make sure caller can chgrp. */ 571da177e4SLinus Torvalds if ((ia_valid & ATTR_GID) && 588e96e3b7SEric W. Biederman (!uid_eq(current_fsuid(), inode->i_uid) || 598e96e3b7SEric W. Biederman (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) && 607fa294c8SEric W. Biederman !inode_capable(inode, CAP_CHOWN)) 612c27c65eSChristoph Hellwig return -EPERM; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* Make sure a caller can chmod. */ 641da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 652e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 662c27c65eSChristoph Hellwig return -EPERM; 671da177e4SLinus Torvalds /* Also check the setgid bit! */ 681da177e4SLinus Torvalds if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid : 697fa294c8SEric W. Biederman inode->i_gid) && 707fa294c8SEric W. Biederman !inode_capable(inode, CAP_FSETID)) 711da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* Check for setting the inode time. */ 759767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { 762e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 772c27c65eSChristoph Hellwig return -EPERM; 781da177e4SLinus Torvalds } 792c27c65eSChristoph Hellwig 802c27c65eSChristoph Hellwig return 0; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds EXPORT_SYMBOL(inode_change_ok); 831da177e4SLinus Torvalds 8425d9e2d1Snpiggin@suse.de /** 8525d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size 8625d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated 8725d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode 8825d9e2d1Snpiggin@suse.de * @Returns: 0 on success, -ve errno on failure 8925d9e2d1Snpiggin@suse.de * 907bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 917bb46a67Snpiggin@suse.de * 9225d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 9325d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 9425d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 9525d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 9625d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 9725d9e2d1Snpiggin@suse.de * conditions). 9825d9e2d1Snpiggin@suse.de */ 9925d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 10025d9e2d1Snpiggin@suse.de { 10125d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 10225d9e2d1Snpiggin@suse.de unsigned long limit; 10325d9e2d1Snpiggin@suse.de 104d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 10525d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 10625d9e2d1Snpiggin@suse.de goto out_sig; 10725d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 10825d9e2d1Snpiggin@suse.de goto out_big; 10925d9e2d1Snpiggin@suse.de } else { 11025d9e2d1Snpiggin@suse.de /* 11125d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 11225d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 11325d9e2d1Snpiggin@suse.de * blocks. 11425d9e2d1Snpiggin@suse.de */ 11525d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 11625d9e2d1Snpiggin@suse.de return -ETXTBSY; 11725d9e2d1Snpiggin@suse.de } 11825d9e2d1Snpiggin@suse.de 11925d9e2d1Snpiggin@suse.de return 0; 12025d9e2d1Snpiggin@suse.de out_sig: 12125d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 12225d9e2d1Snpiggin@suse.de out_big: 12325d9e2d1Snpiggin@suse.de return -EFBIG; 12425d9e2d1Snpiggin@suse.de } 12525d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 12625d9e2d1Snpiggin@suse.de 1277bb46a67Snpiggin@suse.de /** 1286a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 1297bb46a67Snpiggin@suse.de * @inode: the inode to be updated 1307bb46a67Snpiggin@suse.de * @attr: the new attributes 1317bb46a67Snpiggin@suse.de * 1326a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 1337bb46a67Snpiggin@suse.de * 1346a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 13525985edcSLucas De Marchi * in attr. Noticeably missing is inode size update, which is more complex 1362c27c65eSChristoph Hellwig * as it requires pagecache updates. 1377bb46a67Snpiggin@suse.de * 1387bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 1397bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 1407bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 1417bb46a67Snpiggin@suse.de */ 1426a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds if (ia_valid & ATTR_UID) 1471da177e4SLinus Torvalds inode->i_uid = attr->ia_uid; 1481da177e4SLinus Torvalds if (ia_valid & ATTR_GID) 1491da177e4SLinus Torvalds inode->i_gid = attr->ia_gid; 1501da177e4SLinus Torvalds if (ia_valid & ATTR_ATIME) 1511da177e4SLinus Torvalds inode->i_atime = timespec_trunc(attr->ia_atime, 1521da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1531da177e4SLinus Torvalds if (ia_valid & ATTR_MTIME) 1541da177e4SLinus Torvalds inode->i_mtime = timespec_trunc(attr->ia_mtime, 1551da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1561da177e4SLinus Torvalds if (ia_valid & ATTR_CTIME) 1571da177e4SLinus Torvalds inode->i_ctime = timespec_trunc(attr->ia_ctime, 1581da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1591da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 1601da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 1611da177e4SLinus Torvalds 1627fa294c8SEric W. Biederman if (!in_group_p(inode->i_gid) && 1637fa294c8SEric W. Biederman !inode_capable(inode, CAP_FSETID)) 1641da177e4SLinus Torvalds mode &= ~S_ISGID; 1651da177e4SLinus Torvalds inode->i_mode = mode; 1661da177e4SLinus Torvalds } 1677bb46a67Snpiggin@suse.de } 1686a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 1697bb46a67Snpiggin@suse.de 170*27ac0ffeSJ. Bruce Fields /** 171*27ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 172*27ac0ffeSJ. Bruce Fields * @dentry: object affected 173*27ac0ffeSJ. Bruce Fields * @iattr: new attributes 174*27ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 175*27ac0ffeSJ. Bruce Fields * 176*27ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 177*27ac0ffeSJ. Bruce Fields * 178*27ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 179*27ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 180*27ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 181*27ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 182*27ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 183*27ac0ffeSJ. Bruce Fields * 184*27ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 185*27ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 186*27ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 187*27ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 188*27ac0ffeSJ. Bruce Fields * that case. 189*27ac0ffeSJ. Bruce Fields */ 190*27ac0ffeSJ. Bruce Fields int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode) 1911da177e4SLinus Torvalds { 1921da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 1938d334acdSAl Viro umode_t mode = inode->i_mode; 1941da177e4SLinus Torvalds int error; 1951da177e4SLinus Torvalds struct timespec now; 1961da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1971da177e4SLinus Torvalds 198c4107b30SAndrew Morton WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex)); 199c4107b30SAndrew Morton 200beb29e05SMiklos Szeredi if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 201beb29e05SMiklos Szeredi if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 202beb29e05SMiklos Szeredi return -EPERM; 203beb29e05SMiklos Szeredi } 204beb29e05SMiklos Szeredi 205799243a3SDmitry Kasatkin if ((ia_valid & ATTR_SIZE) && IS_I_VERSION(inode)) { 206799243a3SDmitry Kasatkin if (attr->ia_size != inode->i_size) 207799243a3SDmitry Kasatkin inode_inc_iversion(inode); 208799243a3SDmitry Kasatkin } 209799243a3SDmitry Kasatkin 21069b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 2118d334acdSAl Viro umode_t amode = attr->ia_mode; 21269b45732SAndi Kleen /* Flag setting protected by i_mutex */ 21369b45732SAndi Kleen if (is_sxid(amode)) 21469b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 21569b45732SAndi Kleen } 21669b45732SAndi Kleen 2171da177e4SLinus Torvalds now = current_fs_time(inode->i_sb); 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds attr->ia_ctime = now; 2201da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 2211da177e4SLinus Torvalds attr->ia_atime = now; 2221da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 2231da177e4SLinus Torvalds attr->ia_mtime = now; 224b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 225b5376771SSerge E. Hallyn attr->ia_valid &= ~ATTR_KILL_PRIV; 226b5376771SSerge E. Hallyn ia_valid &= ~ATTR_KILL_PRIV; 227b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 228b5376771SSerge E. Hallyn if (error > 0) 229b5376771SSerge E. Hallyn error = security_inode_killpriv(dentry); 230b5376771SSerge E. Hallyn if (error) 231b5376771SSerge E. Hallyn return error; 232b5376771SSerge E. Hallyn } 2336de0ec00SJeff Layton 2346de0ec00SJeff Layton /* 2356de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 2366de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 2376de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 2386de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 2396de0ec00SJeff Layton * ATTR_KILL_S*ID set. 2406de0ec00SJeff Layton */ 2416de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 2426de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 2436de0ec00SJeff Layton BUG(); 2446de0ec00SJeff Layton 2451da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 2461da177e4SLinus Torvalds if (mode & S_ISUID) { 2471da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2486de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 2521da177e4SLinus Torvalds if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 2531da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 2541da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2551da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds } 2606de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 2611da177e4SLinus Torvalds return 0; 2621da177e4SLinus Torvalds 263a77b72daSMiklos Szeredi error = security_inode_setattr(dentry, attr); 264a77b72daSMiklos Szeredi if (error) 265a77b72daSMiklos Szeredi return error; 266*27ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 267*27ac0ffeSJ. Bruce Fields if (error) 268*27ac0ffeSJ. Bruce Fields return error; 269a77b72daSMiklos Szeredi 270eef2380cSChristoph Hellwig if (inode->i_op->setattr) 2711da177e4SLinus Torvalds error = inode->i_op->setattr(dentry, attr); 272eef2380cSChristoph Hellwig else 273eef2380cSChristoph Hellwig error = simple_setattr(dentry, attr); 2741da177e4SLinus Torvalds 275975d2943SMimi Zohar if (!error) { 2760eeca283SRobert Love fsnotify_change(dentry, ia_valid); 2779957a504SMimi Zohar ima_inode_post_setattr(dentry); 278975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 279975d2943SMimi Zohar } 2800eeca283SRobert Love 2811da177e4SLinus Torvalds return error; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 284