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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* Taken over from the old code... */ |
| 18 | |
| 19 | /* POSIX UID/GID verification for setting inode attributes. */ |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 20 | int inode_change_ok(const struct inode *inode, struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | int retval = -EPERM; |
| 23 | unsigned int ia_valid = attr->ia_valid; |
| 24 | |
| 25 | /* If force is set do it anyway. */ |
| 26 | if (ia_valid & ATTR_FORCE) |
| 27 | goto fine; |
| 28 | |
| 29 | /* Make sure a caller can chown. */ |
| 30 | if ((ia_valid & ATTR_UID) && |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 31 | (current_fsuid() != inode->i_uid || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN)) |
| 33 | goto error; |
| 34 | |
| 35 | /* Make sure caller can chgrp. */ |
| 36 | if ((ia_valid & ATTR_GID) && |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 37 | (current_fsuid() != inode->i_uid || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) && |
| 39 | !capable(CAP_CHOWN)) |
| 40 | goto error; |
| 41 | |
| 42 | /* Make sure a caller can chmod. */ |
| 43 | if (ia_valid & ATTR_MODE) { |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 44 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | goto error; |
| 46 | /* Also check the setgid bit! */ |
| 47 | if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid : |
| 48 | inode->i_gid) && !capable(CAP_FSETID)) |
| 49 | attr->ia_mode &= ~S_ISGID; |
| 50 | } |
| 51 | |
| 52 | /* Check for setting the inode time. */ |
Miklos Szeredi | 9767d74 | 2008-07-01 15:01:26 +0200 | [diff] [blame] | 53 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 54 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | goto error; |
| 56 | } |
| 57 | fine: |
| 58 | retval = 0; |
| 59 | error: |
| 60 | return retval; |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | EXPORT_SYMBOL(inode_change_ok); |
| 63 | |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 64 | /** |
| 65 | * inode_newsize_ok - may this inode be truncated to a given size |
| 66 | * @inode: the inode to be truncated |
| 67 | * @offset: the new size to assign to the inode |
| 68 | * @Returns: 0 on success, -ve errno on failure |
| 69 | * |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame^] | 70 | * inode_newsize_ok must be called with i_mutex held. |
| 71 | * |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 72 | * inode_newsize_ok will check filesystem limits and ulimits to check that the |
| 73 | * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ |
| 74 | * when necessary. Caller must not proceed with inode size change if failure is |
| 75 | * returned. @inode must be a file (not directory), with appropriate |
| 76 | * permissions to allow truncate (inode_newsize_ok does NOT check these |
| 77 | * conditions). |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 78 | */ |
| 79 | int inode_newsize_ok(const struct inode *inode, loff_t offset) |
| 80 | { |
| 81 | if (inode->i_size < offset) { |
| 82 | unsigned long limit; |
| 83 | |
Jiri Slaby | d554ed89 | 2010-03-05 13:42:42 -0800 | [diff] [blame] | 84 | limit = rlimit(RLIMIT_FSIZE); |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 85 | if (limit != RLIM_INFINITY && offset > limit) |
| 86 | goto out_sig; |
| 87 | if (offset > inode->i_sb->s_maxbytes) |
| 88 | goto out_big; |
| 89 | } else { |
| 90 | /* |
| 91 | * truncation of in-use swapfiles is disallowed - it would |
| 92 | * cause subsequent swapout to scribble on the now-freed |
| 93 | * blocks. |
| 94 | */ |
| 95 | if (IS_SWAPFILE(inode)) |
| 96 | return -ETXTBSY; |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | out_sig: |
| 101 | send_sig(SIGXFSZ, current, 0); |
| 102 | out_big: |
| 103 | return -EFBIG; |
| 104 | } |
| 105 | EXPORT_SYMBOL(inode_newsize_ok); |
| 106 | |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame^] | 107 | /** |
| 108 | * generic_setattr - copy simple metadata updates into the generic inode |
| 109 | * @inode: the inode to be updated |
| 110 | * @attr: the new attributes |
| 111 | * |
| 112 | * generic_setattr must be called with i_mutex held. |
| 113 | * |
| 114 | * generic_setattr updates the inode's metadata with that specified |
| 115 | * in attr. Noticably missing is inode size update, which is more complex |
| 116 | * as it requires pagecache updates. See simple_setsize. |
| 117 | * |
| 118 | * The inode is not marked as dirty after this operation. The rationale is |
| 119 | * that for "simple" filesystems, the struct inode is the inode storage. |
| 120 | * The caller is free to mark the inode dirty afterwards if needed. |
| 121 | */ |
| 122 | void generic_setattr(struct inode *inode, const struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | unsigned int ia_valid = attr->ia_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if (ia_valid & ATTR_UID) |
| 127 | inode->i_uid = attr->ia_uid; |
| 128 | if (ia_valid & ATTR_GID) |
| 129 | inode->i_gid = attr->ia_gid; |
| 130 | if (ia_valid & ATTR_ATIME) |
| 131 | inode->i_atime = timespec_trunc(attr->ia_atime, |
| 132 | inode->i_sb->s_time_gran); |
| 133 | if (ia_valid & ATTR_MTIME) |
| 134 | inode->i_mtime = timespec_trunc(attr->ia_mtime, |
| 135 | inode->i_sb->s_time_gran); |
| 136 | if (ia_valid & ATTR_CTIME) |
| 137 | inode->i_ctime = timespec_trunc(attr->ia_ctime, |
| 138 | inode->i_sb->s_time_gran); |
| 139 | if (ia_valid & ATTR_MODE) { |
| 140 | umode_t mode = attr->ia_mode; |
| 141 | |
| 142 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 143 | mode &= ~S_ISGID; |
| 144 | inode->i_mode = mode; |
| 145 | } |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame^] | 146 | } |
| 147 | EXPORT_SYMBOL(generic_setattr); |
| 148 | |
| 149 | /* |
| 150 | * note this function is deprecated, the new truncate sequence should be |
| 151 | * used instead -- see eg. simple_setsize, generic_setattr. |
| 152 | */ |
| 153 | int inode_setattr(struct inode *inode, const struct iattr *attr) |
| 154 | { |
| 155 | unsigned int ia_valid = attr->ia_valid; |
| 156 | |
| 157 | if (ia_valid & ATTR_SIZE && |
| 158 | attr->ia_size != i_size_read(inode)) { |
| 159 | int error; |
| 160 | |
| 161 | error = vmtruncate(inode, attr->ia_size); |
| 162 | if (error) |
| 163 | return error; |
| 164 | } |
| 165 | |
| 166 | generic_setattr(inode, attr); |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | mark_inode_dirty(inode); |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 169 | |
| 170 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | EXPORT_SYMBOL(inode_setattr); |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | int notify_change(struct dentry * dentry, struct iattr * attr) |
| 175 | { |
| 176 | struct inode *inode = dentry->d_inode; |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 177 | mode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | int error; |
| 179 | struct timespec now; |
| 180 | unsigned int ia_valid = attr->ia_valid; |
| 181 | |
Miklos Szeredi | beb29e0 | 2008-07-01 15:01:29 +0200 | [diff] [blame] | 182 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { |
| 183 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 184 | return -EPERM; |
| 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | now = current_fs_time(inode->i_sb); |
| 188 | |
| 189 | attr->ia_ctime = now; |
| 190 | if (!(ia_valid & ATTR_ATIME_SET)) |
| 191 | attr->ia_atime = now; |
| 192 | if (!(ia_valid & ATTR_MTIME_SET)) |
| 193 | attr->ia_mtime = now; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 194 | if (ia_valid & ATTR_KILL_PRIV) { |
| 195 | attr->ia_valid &= ~ATTR_KILL_PRIV; |
| 196 | ia_valid &= ~ATTR_KILL_PRIV; |
| 197 | error = security_inode_need_killpriv(dentry); |
| 198 | if (error > 0) |
| 199 | error = security_inode_killpriv(dentry); |
| 200 | if (error) |
| 201 | return error; |
| 202 | } |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * We now pass ATTR_KILL_S*ID to the lower level setattr function so |
| 206 | * that the function has the ability to reinterpret a mode change |
| 207 | * that's due to these bits. This adds an implicit restriction that |
| 208 | * no function will ever call notify_change with both ATTR_MODE and |
| 209 | * ATTR_KILL_S*ID set. |
| 210 | */ |
| 211 | if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && |
| 212 | (ia_valid & ATTR_MODE)) |
| 213 | BUG(); |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (ia_valid & ATTR_KILL_SUID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (mode & S_ISUID) { |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 217 | ia_valid = attr->ia_valid |= ATTR_MODE; |
| 218 | attr->ia_mode = (inode->i_mode & ~S_ISUID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | if (ia_valid & ATTR_KILL_SGID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
| 223 | if (!(ia_valid & ATTR_MODE)) { |
| 224 | ia_valid = attr->ia_valid |= ATTR_MODE; |
| 225 | attr->ia_mode = inode->i_mode; |
| 226 | } |
| 227 | attr->ia_mode &= ~S_ISGID; |
| 228 | } |
| 229 | } |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 230 | if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return 0; |
| 232 | |
Miklos Szeredi | a77b72d | 2008-07-30 14:06:22 +0200 | [diff] [blame] | 233 | error = security_inode_setattr(dentry, attr); |
| 234 | if (error) |
| 235 | return error; |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (ia_valid & ATTR_SIZE) |
| 238 | down_write(&dentry->d_inode->i_alloc_sem); |
| 239 | |
| 240 | if (inode->i_op && inode->i_op->setattr) { |
Miklos Szeredi | a77b72d | 2008-07-30 14:06:22 +0200 | [diff] [blame] | 241 | error = inode->i_op->setattr(dentry, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } else { |
| 243 | error = inode_change_ok(inode, attr); |
Christoph Hellwig | 759bfee | 2010-03-03 09:05:02 -0500 | [diff] [blame] | 244 | if (!error) |
| 245 | error = inode_setattr(inode, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (ia_valid & ATTR_SIZE) |
| 249 | up_write(&dentry->d_inode->i_alloc_sem); |
| 250 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 251 | if (!error) |
| 252 | fsnotify_change(dentry, ia_valid); |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return error; |
| 255 | } |
| 256 | |
| 257 | EXPORT_SYMBOL(notify_change); |