1*b2441318SGreg 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 212c27c65eSChristoph Hellwig /** 2231051c85SJan Kara * setattr_prepare - check if attribute changes to a dentry are allowed 2331051c85SJan Kara * @dentry: dentry to check 242c27c65eSChristoph Hellwig * @attr: attributes to change 252c27c65eSChristoph Hellwig * 262c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr 2731051c85SJan Kara * in the given dentry. This includes the normal unix access permission 2831051c85SJan Kara * checks, as well as checks for rlimits and others. The function also clears 2931051c85SJan Kara * SGID bit from mode if user is not allowed to set it. Also file capabilities 3031051c85SJan Kara * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. 312c27c65eSChristoph Hellwig * 322c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations, 332c27c65eSChristoph Hellwig * possibly after taking additional locks. 342c27c65eSChristoph Hellwig */ 3531051c85SJan Kara int setattr_prepare(struct dentry *dentry, struct iattr *attr) 361da177e4SLinus Torvalds { 3731051c85SJan Kara struct inode *inode = d_inode(dentry); 381da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 391da177e4SLinus Torvalds 402c27c65eSChristoph Hellwig /* 412c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using 422c27c65eSChristoph Hellwig * ATTR_FORCE. 432c27c65eSChristoph Hellwig */ 442c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) { 452c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size); 462c27c65eSChristoph Hellwig if (error) 472c27c65eSChristoph Hellwig return error; 482c27c65eSChristoph Hellwig } 492c27c65eSChristoph Hellwig 501da177e4SLinus Torvalds /* If force is set do it anyway. */ 511da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE) 52030b533cSJan Kara goto kill_priv; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* Make sure a caller can chown. */ 551da177e4SLinus Torvalds if ((ia_valid & ATTR_UID) && 568e96e3b7SEric W. Biederman (!uid_eq(current_fsuid(), inode->i_uid) || 577fa294c8SEric W. Biederman !uid_eq(attr->ia_uid, inode->i_uid)) && 5823adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 592c27c65eSChristoph Hellwig return -EPERM; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds /* Make sure caller can chgrp. */ 621da177e4SLinus Torvalds if ((ia_valid & ATTR_GID) && 638e96e3b7SEric W. Biederman (!uid_eq(current_fsuid(), inode->i_uid) || 648e96e3b7SEric W. Biederman (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) && 6523adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 662c27c65eSChristoph Hellwig return -EPERM; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* Make sure a caller can chmod. */ 691da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 702e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 712c27c65eSChristoph Hellwig return -EPERM; 721da177e4SLinus Torvalds /* Also check the setgid bit! */ 731da177e4SLinus Torvalds if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid : 747fa294c8SEric W. Biederman inode->i_gid) && 7523adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_FSETID)) 761da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds /* Check for setting the inode time. */ 809767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { 812e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 822c27c65eSChristoph Hellwig return -EPERM; 831da177e4SLinus Torvalds } 842c27c65eSChristoph Hellwig 85030b533cSJan Kara kill_priv: 86030b533cSJan Kara /* User has permission for the change */ 87030b533cSJan Kara if (ia_valid & ATTR_KILL_PRIV) { 88030b533cSJan Kara int error; 89030b533cSJan Kara 90030b533cSJan Kara error = security_inode_killpriv(dentry); 91030b533cSJan Kara if (error) 92030b533cSJan Kara return error; 93030b533cSJan Kara } 94030b533cSJan Kara 952c27c65eSChristoph Hellwig return 0; 961da177e4SLinus Torvalds } 9731051c85SJan Kara EXPORT_SYMBOL(setattr_prepare); 981da177e4SLinus Torvalds 9925d9e2d1Snpiggin@suse.de /** 10025d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size 10125d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated 10225d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode 10325d9e2d1Snpiggin@suse.de * @Returns: 0 on success, -ve errno on failure 10425d9e2d1Snpiggin@suse.de * 1057bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 1067bb46a67Snpiggin@suse.de * 10725d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 10825d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 10925d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 11025d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 11125d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 11225d9e2d1Snpiggin@suse.de * conditions). 11325d9e2d1Snpiggin@suse.de */ 11425d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 11525d9e2d1Snpiggin@suse.de { 11625d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 11725d9e2d1Snpiggin@suse.de unsigned long limit; 11825d9e2d1Snpiggin@suse.de 119d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 12025d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 12125d9e2d1Snpiggin@suse.de goto out_sig; 12225d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 12325d9e2d1Snpiggin@suse.de goto out_big; 12425d9e2d1Snpiggin@suse.de } else { 12525d9e2d1Snpiggin@suse.de /* 12625d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 12725d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 12825d9e2d1Snpiggin@suse.de * blocks. 12925d9e2d1Snpiggin@suse.de */ 13025d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 13125d9e2d1Snpiggin@suse.de return -ETXTBSY; 13225d9e2d1Snpiggin@suse.de } 13325d9e2d1Snpiggin@suse.de 13425d9e2d1Snpiggin@suse.de return 0; 13525d9e2d1Snpiggin@suse.de out_sig: 13625d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 13725d9e2d1Snpiggin@suse.de out_big: 13825d9e2d1Snpiggin@suse.de return -EFBIG; 13925d9e2d1Snpiggin@suse.de } 14025d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 14125d9e2d1Snpiggin@suse.de 1427bb46a67Snpiggin@suse.de /** 1436a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 1447bb46a67Snpiggin@suse.de * @inode: the inode to be updated 1457bb46a67Snpiggin@suse.de * @attr: the new attributes 1467bb46a67Snpiggin@suse.de * 1476a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 1487bb46a67Snpiggin@suse.de * 1496a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 15025985edcSLucas De Marchi * in attr. Noticeably missing is inode size update, which is more complex 1512c27c65eSChristoph Hellwig * as it requires pagecache updates. 1527bb46a67Snpiggin@suse.de * 1537bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 1547bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 1557bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 1567bb46a67Snpiggin@suse.de */ 1576a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr) 1581da177e4SLinus Torvalds { 1591da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds if (ia_valid & ATTR_UID) 1621da177e4SLinus Torvalds inode->i_uid = attr->ia_uid; 1631da177e4SLinus Torvalds if (ia_valid & ATTR_GID) 1641da177e4SLinus Torvalds inode->i_gid = attr->ia_gid; 1651da177e4SLinus Torvalds if (ia_valid & ATTR_ATIME) 1661da177e4SLinus Torvalds inode->i_atime = timespec_trunc(attr->ia_atime, 1671da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1681da177e4SLinus Torvalds if (ia_valid & ATTR_MTIME) 1691da177e4SLinus Torvalds inode->i_mtime = timespec_trunc(attr->ia_mtime, 1701da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1711da177e4SLinus Torvalds if (ia_valid & ATTR_CTIME) 1721da177e4SLinus Torvalds inode->i_ctime = timespec_trunc(attr->ia_ctime, 1731da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1741da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 1751da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 1761da177e4SLinus Torvalds 1777fa294c8SEric W. Biederman if (!in_group_p(inode->i_gid) && 17823adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_FSETID)) 1791da177e4SLinus Torvalds mode &= ~S_ISGID; 1801da177e4SLinus Torvalds inode->i_mode = mode; 1811da177e4SLinus Torvalds } 1827bb46a67Snpiggin@suse.de } 1836a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 1847bb46a67Snpiggin@suse.de 18527ac0ffeSJ. Bruce Fields /** 18627ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 18727ac0ffeSJ. Bruce Fields * @dentry: object affected 18827ac0ffeSJ. Bruce Fields * @iattr: new attributes 18927ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 19027ac0ffeSJ. Bruce Fields * 19127ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 19227ac0ffeSJ. Bruce Fields * 19327ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 19427ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 19527ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 19627ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 19727ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 19827ac0ffeSJ. Bruce Fields * 19927ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 20027ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 20127ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 20227ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 20327ac0ffeSJ. Bruce Fields * that case. 20427ac0ffeSJ. Bruce Fields */ 20527ac0ffeSJ. Bruce Fields int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2088d334acdSAl Viro umode_t mode = inode->i_mode; 2091da177e4SLinus Torvalds int error; 2101da177e4SLinus Torvalds struct timespec now; 2111da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 2121da177e4SLinus Torvalds 2135955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode)); 214c4107b30SAndrew Morton 215beb29e05SMiklos Szeredi if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 216beb29e05SMiklos Szeredi if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 217beb29e05SMiklos Szeredi return -EPERM; 218beb29e05SMiklos Szeredi } 219beb29e05SMiklos Szeredi 220f2b20f6eSMiklos Szeredi /* 221f2b20f6eSMiklos Szeredi * If utimes(2) and friends are called with times == NULL (or both 222f2b20f6eSMiklos Szeredi * times are UTIME_NOW), then we need to check for write permission 223f2b20f6eSMiklos Szeredi */ 224f2b20f6eSMiklos Szeredi if (ia_valid & ATTR_TOUCH) { 225f2b20f6eSMiklos Szeredi if (IS_IMMUTABLE(inode)) 226f2b20f6eSMiklos Szeredi return -EPERM; 227f2b20f6eSMiklos Szeredi 228f2b20f6eSMiklos Szeredi if (!inode_owner_or_capable(inode)) { 229f2b20f6eSMiklos Szeredi error = inode_permission(inode, MAY_WRITE); 230f2b20f6eSMiklos Szeredi if (error) 231f2b20f6eSMiklos Szeredi return error; 232f2b20f6eSMiklos Szeredi } 233f2b20f6eSMiklos Szeredi } 234f2b20f6eSMiklos Szeredi 23569b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 2368d334acdSAl Viro umode_t amode = attr->ia_mode; 23769b45732SAndi Kleen /* Flag setting protected by i_mutex */ 23869b45732SAndi Kleen if (is_sxid(amode)) 23969b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 24069b45732SAndi Kleen } 24169b45732SAndi Kleen 242c2050a45SDeepa Dinamani now = current_time(inode); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds attr->ia_ctime = now; 2451da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 2461da177e4SLinus Torvalds attr->ia_atime = now; 2471da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 2481da177e4SLinus Torvalds attr->ia_mtime = now; 249b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 250b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 251030b533cSJan Kara if (error < 0) 252b5376771SSerge E. Hallyn return error; 253030b533cSJan Kara if (error == 0) 254030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; 255b5376771SSerge E. Hallyn } 2566de0ec00SJeff Layton 2576de0ec00SJeff Layton /* 2586de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 2596de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 2606de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 2616de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 2626de0ec00SJeff Layton * ATTR_KILL_S*ID set. 2636de0ec00SJeff Layton */ 2646de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 2656de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 2666de0ec00SJeff Layton BUG(); 2676de0ec00SJeff Layton 2681da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 2691da177e4SLinus Torvalds if (mode & S_ISUID) { 2701da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2716de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 2751da177e4SLinus Torvalds if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 2761da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 2771da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2781da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds } 2836de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds 286a475acf0SSeth Forshee /* 287a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target 288a475acf0SSeth Forshee * namespace of the superblock. 289a475acf0SSeth Forshee */ 290a475acf0SSeth Forshee if (ia_valid & ATTR_UID && 291a475acf0SSeth Forshee !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) 292a475acf0SSeth Forshee return -EOVERFLOW; 293a475acf0SSeth Forshee if (ia_valid & ATTR_GID && 294a475acf0SSeth Forshee !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) 295a475acf0SSeth Forshee return -EOVERFLOW; 296a475acf0SSeth Forshee 2970bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or 2980bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid. 2990bd23d09SEric W. Biederman */ 3000bd23d09SEric W. Biederman if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid)) 3010bd23d09SEric W. Biederman return -EOVERFLOW; 3020bd23d09SEric W. Biederman if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid)) 3030bd23d09SEric W. Biederman return -EOVERFLOW; 3040bd23d09SEric W. Biederman 305a77b72daSMiklos Szeredi error = security_inode_setattr(dentry, attr); 306a77b72daSMiklos Szeredi if (error) 307a77b72daSMiklos Szeredi return error; 30827ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 30927ac0ffeSJ. Bruce Fields if (error) 31027ac0ffeSJ. Bruce Fields return error; 311a77b72daSMiklos Szeredi 312eef2380cSChristoph Hellwig if (inode->i_op->setattr) 3131da177e4SLinus Torvalds error = inode->i_op->setattr(dentry, attr); 314eef2380cSChristoph Hellwig else 315eef2380cSChristoph Hellwig error = simple_setattr(dentry, attr); 3161da177e4SLinus Torvalds 317975d2943SMimi Zohar if (!error) { 3180eeca283SRobert Love fsnotify_change(dentry, ia_valid); 3199957a504SMimi Zohar ima_inode_post_setattr(dentry); 320975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 321975d2943SMimi Zohar } 3220eeca283SRobert Love 3231da177e4SLinus Torvalds return error; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 326