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