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> 12*3f07c014SIngo Molnar #include <linux/sched/signal.h> 1316f7e0feSRandy Dunlap #include <linux/capability.h> 140eeca283SRobert Love #include <linux/fsnotify.h> 151da177e4SLinus Torvalds #include <linux/fcntl.h> 161da177e4SLinus Torvalds #include <linux/security.h> 17975d2943SMimi Zohar #include <linux/evm.h> 189957a504SMimi Zohar #include <linux/ima.h> 191da177e4SLinus Torvalds 202c27c65eSChristoph Hellwig /** 2131051c85SJan Kara * setattr_prepare - check if attribute changes to a dentry are allowed 2231051c85SJan Kara * @dentry: dentry to check 232c27c65eSChristoph Hellwig * @attr: attributes to change 242c27c65eSChristoph Hellwig * 252c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr 2631051c85SJan Kara * in the given dentry. This includes the normal unix access permission 2731051c85SJan Kara * checks, as well as checks for rlimits and others. The function also clears 2831051c85SJan Kara * SGID bit from mode if user is not allowed to set it. Also file capabilities 2931051c85SJan Kara * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. 302c27c65eSChristoph Hellwig * 312c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations, 322c27c65eSChristoph Hellwig * possibly after taking additional locks. 332c27c65eSChristoph Hellwig */ 3431051c85SJan Kara int setattr_prepare(struct dentry *dentry, struct iattr *attr) 351da177e4SLinus Torvalds { 3631051c85SJan Kara struct inode *inode = d_inode(dentry); 371da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 381da177e4SLinus Torvalds 392c27c65eSChristoph Hellwig /* 402c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using 412c27c65eSChristoph Hellwig * ATTR_FORCE. 422c27c65eSChristoph Hellwig */ 432c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) { 442c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size); 452c27c65eSChristoph Hellwig if (error) 462c27c65eSChristoph Hellwig return error; 472c27c65eSChristoph Hellwig } 482c27c65eSChristoph Hellwig 491da177e4SLinus Torvalds /* If force is set do it anyway. */ 501da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE) 51030b533cSJan Kara goto kill_priv; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* Make sure a caller can chown. */ 541da177e4SLinus Torvalds if ((ia_valid & ATTR_UID) && 558e96e3b7SEric W. Biederman (!uid_eq(current_fsuid(), inode->i_uid) || 567fa294c8SEric W. Biederman !uid_eq(attr->ia_uid, inode->i_uid)) && 5723adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 582c27c65eSChristoph Hellwig return -EPERM; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* Make sure caller can chgrp. */ 611da177e4SLinus Torvalds if ((ia_valid & ATTR_GID) && 628e96e3b7SEric W. Biederman (!uid_eq(current_fsuid(), inode->i_uid) || 638e96e3b7SEric W. Biederman (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) && 6423adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_CHOWN)) 652c27c65eSChristoph Hellwig return -EPERM; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* Make sure a caller can chmod. */ 681da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 692e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 702c27c65eSChristoph Hellwig return -EPERM; 711da177e4SLinus Torvalds /* Also check the setgid bit! */ 721da177e4SLinus Torvalds if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid : 737fa294c8SEric W. Biederman inode->i_gid) && 7423adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_FSETID)) 751da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* Check for setting the inode time. */ 799767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { 802e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 812c27c65eSChristoph Hellwig return -EPERM; 821da177e4SLinus Torvalds } 832c27c65eSChristoph Hellwig 84030b533cSJan Kara kill_priv: 85030b533cSJan Kara /* User has permission for the change */ 86030b533cSJan Kara if (ia_valid & ATTR_KILL_PRIV) { 87030b533cSJan Kara int error; 88030b533cSJan Kara 89030b533cSJan Kara error = security_inode_killpriv(dentry); 90030b533cSJan Kara if (error) 91030b533cSJan Kara return error; 92030b533cSJan Kara } 93030b533cSJan Kara 942c27c65eSChristoph Hellwig return 0; 951da177e4SLinus Torvalds } 9631051c85SJan Kara EXPORT_SYMBOL(setattr_prepare); 971da177e4SLinus Torvalds 9825d9e2d1Snpiggin@suse.de /** 9925d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size 10025d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated 10125d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode 10225d9e2d1Snpiggin@suse.de * @Returns: 0 on success, -ve errno on failure 10325d9e2d1Snpiggin@suse.de * 1047bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held. 1057bb46a67Snpiggin@suse.de * 10625d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the 10725d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ 10825d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is 10925d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate 11025d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these 11125d9e2d1Snpiggin@suse.de * conditions). 11225d9e2d1Snpiggin@suse.de */ 11325d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset) 11425d9e2d1Snpiggin@suse.de { 11525d9e2d1Snpiggin@suse.de if (inode->i_size < offset) { 11625d9e2d1Snpiggin@suse.de unsigned long limit; 11725d9e2d1Snpiggin@suse.de 118d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE); 11925d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit) 12025d9e2d1Snpiggin@suse.de goto out_sig; 12125d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes) 12225d9e2d1Snpiggin@suse.de goto out_big; 12325d9e2d1Snpiggin@suse.de } else { 12425d9e2d1Snpiggin@suse.de /* 12525d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would 12625d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed 12725d9e2d1Snpiggin@suse.de * blocks. 12825d9e2d1Snpiggin@suse.de */ 12925d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode)) 13025d9e2d1Snpiggin@suse.de return -ETXTBSY; 13125d9e2d1Snpiggin@suse.de } 13225d9e2d1Snpiggin@suse.de 13325d9e2d1Snpiggin@suse.de return 0; 13425d9e2d1Snpiggin@suse.de out_sig: 13525d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0); 13625d9e2d1Snpiggin@suse.de out_big: 13725d9e2d1Snpiggin@suse.de return -EFBIG; 13825d9e2d1Snpiggin@suse.de } 13925d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok); 14025d9e2d1Snpiggin@suse.de 1417bb46a67Snpiggin@suse.de /** 1426a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode 1437bb46a67Snpiggin@suse.de * @inode: the inode to be updated 1447bb46a67Snpiggin@suse.de * @attr: the new attributes 1457bb46a67Snpiggin@suse.de * 1466a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held. 1477bb46a67Snpiggin@suse.de * 1486a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified 14925985edcSLucas De Marchi * in attr. Noticeably missing is inode size update, which is more complex 1502c27c65eSChristoph Hellwig * as it requires pagecache updates. 1517bb46a67Snpiggin@suse.de * 1527bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is 1537bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage. 1547bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed. 1557bb46a67Snpiggin@suse.de */ 1566a1a90adSChristoph Hellwig void setattr_copy(struct inode *inode, const struct iattr *attr) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds if (ia_valid & ATTR_UID) 1611da177e4SLinus Torvalds inode->i_uid = attr->ia_uid; 1621da177e4SLinus Torvalds if (ia_valid & ATTR_GID) 1631da177e4SLinus Torvalds inode->i_gid = attr->ia_gid; 1641da177e4SLinus Torvalds if (ia_valid & ATTR_ATIME) 1651da177e4SLinus Torvalds inode->i_atime = timespec_trunc(attr->ia_atime, 1661da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1671da177e4SLinus Torvalds if (ia_valid & ATTR_MTIME) 1681da177e4SLinus Torvalds inode->i_mtime = timespec_trunc(attr->ia_mtime, 1691da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1701da177e4SLinus Torvalds if (ia_valid & ATTR_CTIME) 1711da177e4SLinus Torvalds inode->i_ctime = timespec_trunc(attr->ia_ctime, 1721da177e4SLinus Torvalds inode->i_sb->s_time_gran); 1731da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) { 1741da177e4SLinus Torvalds umode_t mode = attr->ia_mode; 1751da177e4SLinus Torvalds 1767fa294c8SEric W. Biederman if (!in_group_p(inode->i_gid) && 17723adbe12SAndy Lutomirski !capable_wrt_inode_uidgid(inode, CAP_FSETID)) 1781da177e4SLinus Torvalds mode &= ~S_ISGID; 1791da177e4SLinus Torvalds inode->i_mode = mode; 1801da177e4SLinus Torvalds } 1817bb46a67Snpiggin@suse.de } 1826a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy); 1837bb46a67Snpiggin@suse.de 18427ac0ffeSJ. Bruce Fields /** 18527ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object 18627ac0ffeSJ. Bruce Fields * @dentry: object affected 18727ac0ffeSJ. Bruce Fields * @iattr: new attributes 18827ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated 18927ac0ffeSJ. Bruce Fields * 19027ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object. 19127ac0ffeSJ. Bruce Fields * 19227ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking, 19327ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in 19427ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and 19527ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the 19627ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so. 19727ac0ffeSJ. Bruce Fields * 19827ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 19927ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 20027ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding 20127ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in 20227ac0ffeSJ. Bruce Fields * that case. 20327ac0ffeSJ. Bruce Fields */ 20427ac0ffeSJ. Bruce Fields int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2078d334acdSAl Viro umode_t mode = inode->i_mode; 2081da177e4SLinus Torvalds int error; 2091da177e4SLinus Torvalds struct timespec now; 2101da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 2111da177e4SLinus Torvalds 2125955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode)); 213c4107b30SAndrew Morton 214beb29e05SMiklos Szeredi if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { 215beb29e05SMiklos Szeredi if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 216beb29e05SMiklos Szeredi return -EPERM; 217beb29e05SMiklos Szeredi } 218beb29e05SMiklos Szeredi 219f2b20f6eSMiklos Szeredi /* 220f2b20f6eSMiklos Szeredi * If utimes(2) and friends are called with times == NULL (or both 221f2b20f6eSMiklos Szeredi * times are UTIME_NOW), then we need to check for write permission 222f2b20f6eSMiklos Szeredi */ 223f2b20f6eSMiklos Szeredi if (ia_valid & ATTR_TOUCH) { 224f2b20f6eSMiklos Szeredi if (IS_IMMUTABLE(inode)) 225f2b20f6eSMiklos Szeredi return -EPERM; 226f2b20f6eSMiklos Szeredi 227f2b20f6eSMiklos Szeredi if (!inode_owner_or_capable(inode)) { 228f2b20f6eSMiklos Szeredi error = inode_permission(inode, MAY_WRITE); 229f2b20f6eSMiklos Szeredi if (error) 230f2b20f6eSMiklos Szeredi return error; 231f2b20f6eSMiklos Szeredi } 232f2b20f6eSMiklos Szeredi } 233f2b20f6eSMiklos Szeredi 23469b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) { 2358d334acdSAl Viro umode_t amode = attr->ia_mode; 23669b45732SAndi Kleen /* Flag setting protected by i_mutex */ 23769b45732SAndi Kleen if (is_sxid(amode)) 23869b45732SAndi Kleen inode->i_flags &= ~S_NOSEC; 23969b45732SAndi Kleen } 24069b45732SAndi Kleen 241c2050a45SDeepa Dinamani now = current_time(inode); 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds attr->ia_ctime = now; 2441da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET)) 2451da177e4SLinus Torvalds attr->ia_atime = now; 2461da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET)) 2471da177e4SLinus Torvalds attr->ia_mtime = now; 248b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) { 249b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry); 250030b533cSJan Kara if (error < 0) 251b5376771SSerge E. Hallyn return error; 252030b533cSJan Kara if (error == 0) 253030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; 254b5376771SSerge E. Hallyn } 2556de0ec00SJeff Layton 2566de0ec00SJeff Layton /* 2576de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so 2586de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change 2596de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that 2606de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and 2616de0ec00SJeff Layton * ATTR_KILL_S*ID set. 2626de0ec00SJeff Layton */ 2636de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && 2646de0ec00SJeff Layton (ia_valid & ATTR_MODE)) 2656de0ec00SJeff Layton BUG(); 2666de0ec00SJeff Layton 2671da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) { 2681da177e4SLinus Torvalds if (mode & S_ISUID) { 2691da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2706de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) { 2741da177e4SLinus Torvalds if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 2751da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) { 2761da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE; 2771da177e4SLinus Torvalds attr->ia_mode = inode->i_mode; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds } 2826de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) 2831da177e4SLinus Torvalds return 0; 2841da177e4SLinus Torvalds 285a475acf0SSeth Forshee /* 286a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target 287a475acf0SSeth Forshee * namespace of the superblock. 288a475acf0SSeth Forshee */ 289a475acf0SSeth Forshee if (ia_valid & ATTR_UID && 290a475acf0SSeth Forshee !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) 291a475acf0SSeth Forshee return -EOVERFLOW; 292a475acf0SSeth Forshee if (ia_valid & ATTR_GID && 293a475acf0SSeth Forshee !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) 294a475acf0SSeth Forshee return -EOVERFLOW; 295a475acf0SSeth Forshee 2960bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or 2970bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid. 2980bd23d09SEric W. Biederman */ 2990bd23d09SEric W. Biederman if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid)) 3000bd23d09SEric W. Biederman return -EOVERFLOW; 3010bd23d09SEric W. Biederman if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid)) 3020bd23d09SEric W. Biederman return -EOVERFLOW; 3030bd23d09SEric W. Biederman 304a77b72daSMiklos Szeredi error = security_inode_setattr(dentry, attr); 305a77b72daSMiklos Szeredi if (error) 306a77b72daSMiklos Szeredi return error; 30727ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 30827ac0ffeSJ. Bruce Fields if (error) 30927ac0ffeSJ. Bruce Fields return error; 310a77b72daSMiklos Szeredi 311eef2380cSChristoph Hellwig if (inode->i_op->setattr) 3121da177e4SLinus Torvalds error = inode->i_op->setattr(dentry, attr); 313eef2380cSChristoph Hellwig else 314eef2380cSChristoph Hellwig error = simple_setattr(dentry, attr); 3151da177e4SLinus Torvalds 316975d2943SMimi Zohar if (!error) { 3170eeca283SRobert Love fsnotify_change(dentry, ia_valid); 3189957a504SMimi Zohar ima_inode_post_setattr(dentry); 319975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid); 320975d2943SMimi Zohar } 3210eeca283SRobert Love 3221da177e4SLinus Torvalds return error; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change); 325