Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 12 | #include <linux/capability.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 13 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/fcntl.h> |
| 15 | #include <linux/quotaops.h> |
| 16 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /* Taken over from the old code... */ |
| 19 | |
| 20 | /* POSIX UID/GID verification for setting inode attributes. */ |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 21 | int inode_change_ok(const struct inode *inode, struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) && |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 32 | (current_fsuid() != inode->i_uid || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) && |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 38 | (current_fsuid() != inode->i_uid || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 45 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Miklos Szeredi | 9767d74 | 2008-07-01 15:01:26 +0200 | [diff] [blame] | 54 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 55 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | goto error; |
| 57 | } |
| 58 | fine: |
| 59 | retval = 0; |
| 60 | error: |
| 61 | return retval; |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | EXPORT_SYMBOL(inode_change_ok); |
| 64 | |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 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 | */ |
| 80 | int 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; |
| 101 | out_sig: |
| 102 | send_sig(SIGXFSZ, current, 0); |
| 103 | out_big: |
| 104 | return -EFBIG; |
| 105 | } |
| 106 | EXPORT_SYMBOL(inode_newsize_ok); |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | int inode_setattr(struct inode * inode, struct iattr * attr) |
| 109 | { |
| 110 | unsigned int ia_valid = attr->ia_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 140 | |
| 141 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | EXPORT_SYMBOL(inode_setattr); |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | int notify_change(struct dentry * dentry, struct iattr * attr) |
| 146 | { |
| 147 | struct inode *inode = dentry->d_inode; |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 148 | mode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | int error; |
| 150 | struct timespec now; |
| 151 | unsigned int ia_valid = attr->ia_valid; |
| 152 | |
Miklos Szeredi | beb29e0 | 2008-07-01 15:01:29 +0200 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 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 | } |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | if (ia_valid & ATTR_KILL_SUID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if (mode & S_ISUID) { |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 188 | ia_valid = attr->ia_valid |= ATTR_MODE; |
| 189 | attr->ia_mode = (inode->i_mode & ~S_ISUID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | if (ia_valid & ATTR_KILL_SGID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 201 | if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | return 0; |
| 203 | |
Miklos Szeredi | a77b72d | 2008-07-30 14:06:22 +0200 | [diff] [blame] | 204 | error = security_inode_setattr(dentry, attr); |
| 205 | if (error) |
| 206 | return error; |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Miklos Szeredi | a77b72d | 2008-07-30 14:06:22 +0200 | [diff] [blame] | 212 | error = inode->i_op->setattr(dentry, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } else { |
| 214 | error = inode_change_ok(inode, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Jan Kara | 9e3509e | 2009-01-26 16:45:12 +0100 | [diff] [blame] | 218 | error = vfs_dq_transfer(inode, attr) ? |
| 219 | -EDQUOT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 228 | if (!error) |
| 229 | fsnotify_change(dentry, ia_valid); |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return error; |
| 232 | } |
| 233 | |
| 234 | EXPORT_SYMBOL(notify_change); |