Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/fs/attr.c
0003  *
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  *  changes by Thomas Schoebel-Theuer
0006  */
0007 
0008 #include <linux/export.h>
0009 #include <linux/time.h>
0010 #include <linux/mm.h>
0011 #include <linux/string.h>
0012 #include <linux/capability.h>
0013 #include <linux/fsnotify.h>
0014 #include <linux/fcntl.h>
0015 #include <linux/security.h>
0016 #include <linux/evm.h>
0017 #include <linux/ima.h>
0018 
0019 /**
0020  * setattr_prepare - check if attribute changes to a dentry are allowed
0021  * @dentry: dentry to check
0022  * @attr:   attributes to change
0023  *
0024  * Check if we are allowed to change the attributes contained in @attr
0025  * in the given dentry.  This includes the normal unix access permission
0026  * checks, as well as checks for rlimits and others. The function also clears
0027  * SGID bit from mode if user is not allowed to set it. Also file capabilities
0028  * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
0029  *
0030  * Should be called as the first thing in ->setattr implementations,
0031  * possibly after taking additional locks.
0032  */
0033 int setattr_prepare(struct dentry *dentry, struct iattr *attr)
0034 {
0035     struct inode *inode = d_inode(dentry);
0036     unsigned int ia_valid = attr->ia_valid;
0037 
0038     /*
0039      * First check size constraints.  These can't be overriden using
0040      * ATTR_FORCE.
0041      */
0042     if (ia_valid & ATTR_SIZE) {
0043         int error = inode_newsize_ok(inode, attr->ia_size);
0044         if (error)
0045             return error;
0046     }
0047 
0048     /* If force is set do it anyway. */
0049     if (ia_valid & ATTR_FORCE)
0050         goto kill_priv;
0051 
0052     /* Make sure a caller can chown. */
0053     if ((ia_valid & ATTR_UID) &&
0054         (!uid_eq(current_fsuid(), inode->i_uid) ||
0055          !uid_eq(attr->ia_uid, inode->i_uid)) &&
0056         !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
0057         return -EPERM;
0058 
0059     /* Make sure caller can chgrp. */
0060     if ((ia_valid & ATTR_GID) &&
0061         (!uid_eq(current_fsuid(), inode->i_uid) ||
0062         (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
0063         !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
0064         return -EPERM;
0065 
0066     /* Make sure a caller can chmod. */
0067     if (ia_valid & ATTR_MODE) {
0068         if (!inode_owner_or_capable(inode))
0069             return -EPERM;
0070         /* Also check the setgid bit! */
0071         if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
0072                 inode->i_gid) &&
0073             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
0074             attr->ia_mode &= ~S_ISGID;
0075     }
0076 
0077     /* Check for setting the inode time. */
0078     if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
0079         if (!inode_owner_or_capable(inode))
0080             return -EPERM;
0081     }
0082 
0083 kill_priv:
0084     /* User has permission for the change */
0085     if (ia_valid & ATTR_KILL_PRIV) {
0086         int error;
0087 
0088         error = security_inode_killpriv(dentry);
0089         if (error)
0090             return error;
0091     }
0092 
0093     return 0;
0094 }
0095 EXPORT_SYMBOL(setattr_prepare);
0096 
0097 /**
0098  * inode_newsize_ok - may this inode be truncated to a given size
0099  * @inode:  the inode to be truncated
0100  * @offset: the new size to assign to the inode
0101  * @Returns:    0 on success, -ve errno on failure
0102  *
0103  * inode_newsize_ok must be called with i_mutex held.
0104  *
0105  * inode_newsize_ok will check filesystem limits and ulimits to check that the
0106  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
0107  * when necessary. Caller must not proceed with inode size change if failure is
0108  * returned. @inode must be a file (not directory), with appropriate
0109  * permissions to allow truncate (inode_newsize_ok does NOT check these
0110  * conditions).
0111  */
0112 int inode_newsize_ok(const struct inode *inode, loff_t offset)
0113 {
0114     if (inode->i_size < offset) {
0115         unsigned long limit;
0116 
0117         limit = rlimit(RLIMIT_FSIZE);
0118         if (limit != RLIM_INFINITY && offset > limit)
0119             goto out_sig;
0120         if (offset > inode->i_sb->s_maxbytes)
0121             goto out_big;
0122     } else {
0123         /*
0124          * truncation of in-use swapfiles is disallowed - it would
0125          * cause subsequent swapout to scribble on the now-freed
0126          * blocks.
0127          */
0128         if (IS_SWAPFILE(inode))
0129             return -ETXTBSY;
0130     }
0131 
0132     return 0;
0133 out_sig:
0134     send_sig(SIGXFSZ, current, 0);
0135 out_big:
0136     return -EFBIG;
0137 }
0138 EXPORT_SYMBOL(inode_newsize_ok);
0139 
0140 /**
0141  * setattr_copy - copy simple metadata updates into the generic inode
0142  * @inode:  the inode to be updated
0143  * @attr:   the new attributes
0144  *
0145  * setattr_copy must be called with i_mutex held.
0146  *
0147  * setattr_copy updates the inode's metadata with that specified
0148  * in attr. Noticeably missing is inode size update, which is more complex
0149  * as it requires pagecache updates.
0150  *
0151  * The inode is not marked as dirty after this operation. The rationale is
0152  * that for "simple" filesystems, the struct inode is the inode storage.
0153  * The caller is free to mark the inode dirty afterwards if needed.
0154  */
0155 void setattr_copy(struct inode *inode, const struct iattr *attr)
0156 {
0157     unsigned int ia_valid = attr->ia_valid;
0158 
0159     if (ia_valid & ATTR_UID)
0160         inode->i_uid = attr->ia_uid;
0161     if (ia_valid & ATTR_GID)
0162         inode->i_gid = attr->ia_gid;
0163     if (ia_valid & ATTR_ATIME)
0164         inode->i_atime = timespec_trunc(attr->ia_atime,
0165                         inode->i_sb->s_time_gran);
0166     if (ia_valid & ATTR_MTIME)
0167         inode->i_mtime = timespec_trunc(attr->ia_mtime,
0168                         inode->i_sb->s_time_gran);
0169     if (ia_valid & ATTR_CTIME)
0170         inode->i_ctime = timespec_trunc(attr->ia_ctime,
0171                         inode->i_sb->s_time_gran);
0172     if (ia_valid & ATTR_MODE) {
0173         umode_t mode = attr->ia_mode;
0174 
0175         if (!in_group_p(inode->i_gid) &&
0176             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
0177             mode &= ~S_ISGID;
0178         inode->i_mode = mode;
0179     }
0180 }
0181 EXPORT_SYMBOL(setattr_copy);
0182 
0183 /**
0184  * notify_change - modify attributes of a filesytem object
0185  * @dentry: object affected
0186  * @iattr:  new attributes
0187  * @delegated_inode: returns inode, if the inode is delegated
0188  *
0189  * The caller must hold the i_mutex on the affected object.
0190  *
0191  * If notify_change discovers a delegation in need of breaking,
0192  * it will return -EWOULDBLOCK and return a reference to the inode in
0193  * delegated_inode.  The caller should then break the delegation and
0194  * retry.  Because breaking a delegation may take a long time, the
0195  * caller should drop the i_mutex before doing so.
0196  *
0197  * Alternatively, a caller may pass NULL for delegated_inode.  This may
0198  * be appropriate for callers that expect the underlying filesystem not
0199  * to be NFS exported.  Also, passing NULL is fine for callers holding
0200  * the file open for write, as there can be no conflicting delegation in
0201  * that case.
0202  */
0203 int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
0204 {
0205     struct inode *inode = dentry->d_inode;
0206     umode_t mode = inode->i_mode;
0207     int error;
0208     struct timespec now;
0209     unsigned int ia_valid = attr->ia_valid;
0210 
0211     WARN_ON_ONCE(!inode_is_locked(inode));
0212 
0213     if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
0214         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
0215             return -EPERM;
0216     }
0217 
0218     /*
0219      * If utimes(2) and friends are called with times == NULL (or both
0220      * times are UTIME_NOW), then we need to check for write permission
0221      */
0222     if (ia_valid & ATTR_TOUCH) {
0223         if (IS_IMMUTABLE(inode))
0224             return -EPERM;
0225 
0226         if (!inode_owner_or_capable(inode)) {
0227             error = inode_permission(inode, MAY_WRITE);
0228             if (error)
0229                 return error;
0230         }
0231     }
0232 
0233     if ((ia_valid & ATTR_MODE)) {
0234         umode_t amode = attr->ia_mode;
0235         /* Flag setting protected by i_mutex */
0236         if (is_sxid(amode))
0237             inode->i_flags &= ~S_NOSEC;
0238     }
0239 
0240     now = current_time(inode);
0241 
0242     attr->ia_ctime = now;
0243     if (!(ia_valid & ATTR_ATIME_SET))
0244         attr->ia_atime = now;
0245     if (!(ia_valid & ATTR_MTIME_SET))
0246         attr->ia_mtime = now;
0247     if (ia_valid & ATTR_KILL_PRIV) {
0248         error = security_inode_need_killpriv(dentry);
0249         if (error < 0)
0250             return error;
0251         if (error == 0)
0252             ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
0253     }
0254 
0255     /*
0256      * We now pass ATTR_KILL_S*ID to the lower level setattr function so
0257      * that the function has the ability to reinterpret a mode change
0258      * that's due to these bits. This adds an implicit restriction that
0259      * no function will ever call notify_change with both ATTR_MODE and
0260      * ATTR_KILL_S*ID set.
0261      */
0262     if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
0263         (ia_valid & ATTR_MODE))
0264         BUG();
0265 
0266     if (ia_valid & ATTR_KILL_SUID) {
0267         if (mode & S_ISUID) {
0268             ia_valid = attr->ia_valid |= ATTR_MODE;
0269             attr->ia_mode = (inode->i_mode & ~S_ISUID);
0270         }
0271     }
0272     if (ia_valid & ATTR_KILL_SGID) {
0273         if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
0274             if (!(ia_valid & ATTR_MODE)) {
0275                 ia_valid = attr->ia_valid |= ATTR_MODE;
0276                 attr->ia_mode = inode->i_mode;
0277             }
0278             attr->ia_mode &= ~S_ISGID;
0279         }
0280     }
0281     if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
0282         return 0;
0283 
0284     /*
0285      * Verify that uid/gid changes are valid in the target
0286      * namespace of the superblock.
0287      */
0288     if (ia_valid & ATTR_UID &&
0289         !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
0290         return -EOVERFLOW;
0291     if (ia_valid & ATTR_GID &&
0292         !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
0293         return -EOVERFLOW;
0294 
0295     /* Don't allow modifications of files with invalid uids or
0296      * gids unless those uids & gids are being made valid.
0297      */
0298     if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
0299         return -EOVERFLOW;
0300     if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
0301         return -EOVERFLOW;
0302 
0303     error = security_inode_setattr(dentry, attr);
0304     if (error)
0305         return error;
0306     error = try_break_deleg(inode, delegated_inode);
0307     if (error)
0308         return error;
0309 
0310     if (inode->i_op->setattr)
0311         error = inode->i_op->setattr(dentry, attr);
0312     else
0313         error = simple_setattr(dentry, attr);
0314 
0315     if (!error) {
0316         fsnotify_change(dentry, ia_valid);
0317         ima_inode_post_setattr(dentry);
0318         evm_inode_post_setattr(dentry, ia_valid);
0319     }
0320 
0321     return error;
0322 }
0323 EXPORT_SYMBOL(notify_change);