linux/fs/attr.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/attr.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 *  changes by Thomas Schoebel-Theuer
   6 */
   7
   8#include <linux/module.h>
   9#include <linux/time.h>
  10#include <linux/mm.h>
  11#include <linux/string.h>
  12#include <linux/capability.h>
  13#include <linux/fsnotify.h>
  14#include <linux/fcntl.h>
  15#include <linux/quotaops.h>
  16#include <linux/security.h>
  17
  18/* Taken over from the old code... */
  19
  20/* POSIX UID/GID verification for setting inode attributes. */
  21int inode_change_ok(const struct inode *inode, struct iattr *attr)
  22{
  23        int retval = -EPERM;
  24        unsigned int ia_valid = attr->ia_valid;
  25
  26        /* If force is set do it anyway. */
  27        if (ia_valid & ATTR_FORCE)
  28                goto fine;
  29
  30        /* Make sure a caller can chown. */
  31        if ((ia_valid & ATTR_UID) &&
  32            (current_fsuid() != inode->i_uid ||
  33             attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
  34                goto error;
  35
  36        /* Make sure caller can chgrp. */
  37        if ((ia_valid & ATTR_GID) &&
  38            (current_fsuid() != inode->i_uid ||
  39            (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
  40            !capable(CAP_CHOWN))
  41                goto error;
  42
  43        /* Make sure a caller can chmod. */
  44        if (ia_valid & ATTR_MODE) {
  45                if (!is_owner_or_cap(inode))
  46                        goto error;
  47                /* Also check the setgid bit! */
  48                if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
  49                                inode->i_gid) && !capable(CAP_FSETID))
  50                        attr->ia_mode &= ~S_ISGID;
  51        }
  52
  53        /* Check for setting the inode time. */
  54        if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
  55                if (!is_owner_or_cap(inode))
  56                        goto error;
  57        }
  58fine:
  59        retval = 0;
  60error:
  61        return retval;
  62}
  63EXPORT_SYMBOL(inode_change_ok);
  64
  65/**
  66 * inode_newsize_ok - may this inode be truncated to a given size
  67 * @inode:      the inode to be truncated
  68 * @offset:     the new size to assign to the inode
  69 * @Returns:    0 on success, -ve errno on failure
  70 *
  71 * inode_newsize_ok will check filesystem limits and ulimits to check that the
  72 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
  73 * when necessary. Caller must not proceed with inode size change if failure is
  74 * returned. @inode must be a file (not directory), with appropriate
  75 * permissions to allow truncate (inode_newsize_ok does NOT check these
  76 * conditions).
  77 *
  78 * inode_newsize_ok must be called with i_mutex held.
  79 */
  80int inode_newsize_ok(const struct inode *inode, loff_t offset)
  81{
  82        if (inode->i_size < offset) {
  83                unsigned long limit;
  84
  85                limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  86                if (limit != RLIM_INFINITY && offset > limit)
  87                        goto out_sig;
  88                if (offset > inode->i_sb->s_maxbytes)
  89                        goto out_big;
  90        } else {
  91                /*
  92                 * truncation of in-use swapfiles is disallowed - it would
  93                 * cause subsequent swapout to scribble on the now-freed
  94                 * blocks.
  95                 */
  96                if (IS_SWAPFILE(inode))
  97                        return -ETXTBSY;
  98        }
  99
 100        return 0;
 101out_sig:
 102        send_sig(SIGXFSZ, current, 0);
 103out_big:
 104        return -EFBIG;
 105}
 106EXPORT_SYMBOL(inode_newsize_ok);
 107
 108int inode_setattr(struct inode * inode, struct iattr * attr)
 109{
 110        unsigned int ia_valid = attr->ia_valid;
 111
 112        if (ia_valid & ATTR_SIZE &&
 113            attr->ia_size != i_size_read(inode)) {
 114                int error = vmtruncate(inode, attr->ia_size);
 115                if (error)
 116                        return error;
 117        }
 118
 119        if (ia_valid & ATTR_UID)
 120                inode->i_uid = attr->ia_uid;
 121        if (ia_valid & ATTR_GID)
 122                inode->i_gid = attr->ia_gid;
 123        if (ia_valid & ATTR_ATIME)
 124                inode->i_atime = timespec_trunc(attr->ia_atime,
 125                                                inode->i_sb->s_time_gran);
 126        if (ia_valid & ATTR_MTIME)
 127                inode->i_mtime = timespec_trunc(attr->ia_mtime,
 128                                                inode->i_sb->s_time_gran);
 129        if (ia_valid & ATTR_CTIME)
 130                inode->i_ctime = timespec_trunc(attr->ia_ctime,
 131                                                inode->i_sb->s_time_gran);
 132        if (ia_valid & ATTR_MODE) {
 133                umode_t mode = attr->ia_mode;
 134
 135                if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
 136                        mode &= ~S_ISGID;
 137                inode->i_mode = mode;
 138        }
 139        mark_inode_dirty(inode);
 140
 141        return 0;
 142}
 143EXPORT_SYMBOL(inode_setattr);
 144
 145int notify_change(struct dentry * dentry, struct iattr * attr)
 146{
 147        struct inode *inode = dentry->d_inode;
 148        mode_t mode = inode->i_mode;
 149        int error;
 150        struct timespec now;
 151        unsigned int ia_valid = attr->ia_valid;
 152
 153        if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
 154                if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
 155                        return -EPERM;
 156        }
 157
 158        now = current_fs_time(inode->i_sb);
 159
 160        attr->ia_ctime = now;
 161        if (!(ia_valid & ATTR_ATIME_SET))
 162                attr->ia_atime = now;
 163        if (!(ia_valid & ATTR_MTIME_SET))
 164                attr->ia_mtime = now;
 165        if (ia_valid & ATTR_KILL_PRIV) {
 166                attr->ia_valid &= ~ATTR_KILL_PRIV;
 167                ia_valid &= ~ATTR_KILL_PRIV;
 168                error = security_inode_need_killpriv(dentry);
 169                if (error > 0)
 170                        error = security_inode_killpriv(dentry);
 171                if (error)
 172                        return error;
 173        }
 174
 175        /*
 176         * We now pass ATTR_KILL_S*ID to the lower level setattr function so
 177         * that the function has the ability to reinterpret a mode change
 178         * that's due to these bits. This adds an implicit restriction that
 179         * no function will ever call notify_change with both ATTR_MODE and
 180         * ATTR_KILL_S*ID set.
 181         */
 182        if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
 183            (ia_valid & ATTR_MODE))
 184                BUG();
 185
 186        if (ia_valid & ATTR_KILL_SUID) {
 187                if (mode & S_ISUID) {
 188                        ia_valid = attr->ia_valid |= ATTR_MODE;
 189                        attr->ia_mode = (inode->i_mode & ~S_ISUID);
 190                }
 191        }
 192        if (ia_valid & ATTR_KILL_SGID) {
 193                if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
 194                        if (!(ia_valid & ATTR_MODE)) {
 195                                ia_valid = attr->ia_valid |= ATTR_MODE;
 196                                attr->ia_mode = inode->i_mode;
 197                        }
 198                        attr->ia_mode &= ~S_ISGID;
 199                }
 200        }
 201        if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
 202                return 0;
 203
 204        error = security_inode_setattr(dentry, attr);
 205        if (error)
 206                return error;
 207
 208        if (ia_valid & ATTR_SIZE)
 209                down_write(&dentry->d_inode->i_alloc_sem);
 210
 211        if (inode->i_op && inode->i_op->setattr) {
 212                error = inode->i_op->setattr(dentry, attr);
 213        } else {
 214                error = inode_change_ok(inode, attr);
 215                if (!error) {
 216                        if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
 217                            (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
 218                                error = vfs_dq_transfer(inode, attr) ?
 219                                        -EDQUOT : 0;
 220                        if (!error)
 221                                error = inode_setattr(inode, attr);
 222                }
 223        }
 224
 225        if (ia_valid & ATTR_SIZE)
 226                up_write(&dentry->d_inode->i_alloc_sem);
 227
 228        if (!error)
 229                fsnotify_change(dentry, ia_valid);
 230
 231        return error;
 232}
 233
 234EXPORT_SYMBOL(notify_change);
 235