]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/attr.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | * changes by Thomas Schoebel-Theuer | |
6 | */ | |
7 | ||
630d9c47 | 8 | #include <linux/export.h> |
1da177e4 LT |
9 | #include <linux/time.h> |
10 | #include <linux/mm.h> | |
11 | #include <linux/string.h> | |
16f7e0fe | 12 | #include <linux/capability.h> |
0eeca283 | 13 | #include <linux/fsnotify.h> |
1da177e4 | 14 | #include <linux/fcntl.h> |
1da177e4 | 15 | #include <linux/security.h> |
975d2943 | 16 | #include <linux/evm.h> |
9957a504 | 17 | #include <linux/ima.h> |
1da177e4 | 18 | |
915c5a4e EB |
19 | static bool chown_ok(const struct inode *inode, kuid_t uid) |
20 | { | |
21 | if (uid_eq(current_fsuid(), inode->i_uid) && | |
22 | uid_eq(uid, inode->i_uid)) | |
23 | return true; | |
24 | if (capable_wrt_inode_uidgid(inode, CAP_CHOWN)) | |
25 | return true; | |
26 | if (ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) | |
27 | return true; | |
28 | return false; | |
29 | } | |
30 | ||
31 | static bool chgrp_ok(const struct inode *inode, kgid_t gid) | |
32 | { | |
33 | if (uid_eq(current_fsuid(), inode->i_uid) && | |
34 | (in_group_p(gid) || gid_eq(gid, inode->i_gid))) | |
35 | return true; | |
36 | if (capable_wrt_inode_uidgid(inode, CAP_CHOWN)) | |
37 | return true; | |
38 | if (ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) | |
39 | return true; | |
40 | return false; | |
41 | } | |
42 | ||
2c27c65e CH |
43 | /** |
44 | * inode_change_ok - check if attribute changes to an inode are allowed | |
45 | * @inode: inode to check | |
46 | * @attr: attributes to change | |
47 | * | |
48 | * Check if we are allowed to change the attributes contained in @attr | |
49 | * in the given inode. This includes the normal unix access permission | |
50 | * checks, as well as checks for rlimits and others. | |
51 | * | |
52 | * Should be called as the first thing in ->setattr implementations, | |
53 | * possibly after taking additional locks. | |
54 | */ | |
25d9e2d1 | 55 | int inode_change_ok(const struct inode *inode, struct iattr *attr) |
1da177e4 | 56 | { |
1da177e4 LT |
57 | unsigned int ia_valid = attr->ia_valid; |
58 | ||
2c27c65e CH |
59 | /* |
60 | * First check size constraints. These can't be overriden using | |
61 | * ATTR_FORCE. | |
62 | */ | |
63 | if (ia_valid & ATTR_SIZE) { | |
64 | int error = inode_newsize_ok(inode, attr->ia_size); | |
65 | if (error) | |
66 | return error; | |
67 | } | |
68 | ||
1da177e4 LT |
69 | /* If force is set do it anyway. */ |
70 | if (ia_valid & ATTR_FORCE) | |
2c27c65e | 71 | return 0; |
1da177e4 LT |
72 | |
73 | /* Make sure a caller can chown. */ | |
915c5a4e | 74 | if ((ia_valid & ATTR_UID) && !chown_ok(inode, attr->ia_uid)) |
2c27c65e | 75 | return -EPERM; |
1da177e4 LT |
76 | |
77 | /* Make sure caller can chgrp. */ | |
915c5a4e | 78 | if ((ia_valid & ATTR_GID) && !chgrp_ok(inode, attr->ia_gid)) |
2c27c65e | 79 | return -EPERM; |
1da177e4 LT |
80 | |
81 | /* Make sure a caller can chmod. */ | |
82 | if (ia_valid & ATTR_MODE) { | |
2e149670 | 83 | if (!inode_owner_or_capable(inode)) |
2c27c65e | 84 | return -EPERM; |
1da177e4 LT |
85 | /* Also check the setgid bit! */ |
86 | if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid : | |
7fa294c8 | 87 | inode->i_gid) && |
23adbe12 | 88 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) |
1da177e4 LT |
89 | attr->ia_mode &= ~S_ISGID; |
90 | } | |
91 | ||
92 | /* Check for setting the inode time. */ | |
9767d749 | 93 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { |
2e149670 | 94 | if (!inode_owner_or_capable(inode)) |
2c27c65e | 95 | return -EPERM; |
1da177e4 | 96 | } |
2c27c65e CH |
97 | |
98 | return 0; | |
1da177e4 | 99 | } |
1da177e4 LT |
100 | EXPORT_SYMBOL(inode_change_ok); |
101 | ||
25d9e2d1 | 102 | /** |
103 | * inode_newsize_ok - may this inode be truncated to a given size | |
104 | * @inode: the inode to be truncated | |
105 | * @offset: the new size to assign to the inode | |
106 | * @Returns: 0 on success, -ve errno on failure | |
107 | * | |
7bb46a67 | 108 | * inode_newsize_ok must be called with i_mutex held. |
109 | * | |
25d9e2d1 | 110 | * inode_newsize_ok will check filesystem limits and ulimits to check that the |
111 | * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ | |
112 | * when necessary. Caller must not proceed with inode size change if failure is | |
113 | * returned. @inode must be a file (not directory), with appropriate | |
114 | * permissions to allow truncate (inode_newsize_ok does NOT check these | |
115 | * conditions). | |
25d9e2d1 | 116 | */ |
117 | int inode_newsize_ok(const struct inode *inode, loff_t offset) | |
118 | { | |
119 | if (inode->i_size < offset) { | |
120 | unsigned long limit; | |
121 | ||
d554ed89 | 122 | limit = rlimit(RLIMIT_FSIZE); |
25d9e2d1 | 123 | if (limit != RLIM_INFINITY && offset > limit) |
124 | goto out_sig; | |
125 | if (offset > inode->i_sb->s_maxbytes) | |
126 | goto out_big; | |
127 | } else { | |
128 | /* | |
129 | * truncation of in-use swapfiles is disallowed - it would | |
130 | * cause subsequent swapout to scribble on the now-freed | |
131 | * blocks. | |
132 | */ | |
133 | if (IS_SWAPFILE(inode)) | |
134 | return -ETXTBSY; | |
135 | } | |
136 | ||
137 | return 0; | |
138 | out_sig: | |
139 | send_sig(SIGXFSZ, current, 0); | |
140 | out_big: | |
141 | return -EFBIG; | |
142 | } | |
143 | EXPORT_SYMBOL(inode_newsize_ok); | |
144 | ||
7bb46a67 | 145 | /** |
6a1a90ad | 146 | * setattr_copy - copy simple metadata updates into the generic inode |
7bb46a67 | 147 | * @inode: the inode to be updated |
148 | * @attr: the new attributes | |
149 | * | |
6a1a90ad | 150 | * setattr_copy must be called with i_mutex held. |
7bb46a67 | 151 | * |
6a1a90ad | 152 | * setattr_copy updates the inode's metadata with that specified |
25985edc | 153 | * in attr. Noticeably missing is inode size update, which is more complex |
2c27c65e | 154 | * as it requires pagecache updates. |
7bb46a67 | 155 | * |
156 | * The inode is not marked as dirty after this operation. The rationale is | |
157 | * that for "simple" filesystems, the struct inode is the inode storage. | |
158 | * The caller is free to mark the inode dirty afterwards if needed. | |
159 | */ | |
6a1a90ad | 160 | void setattr_copy(struct inode *inode, const struct iattr *attr) |
1da177e4 LT |
161 | { |
162 | unsigned int ia_valid = attr->ia_valid; | |
4a30131e | 163 | |
1da177e4 LT |
164 | if (ia_valid & ATTR_UID) |
165 | inode->i_uid = attr->ia_uid; | |
166 | if (ia_valid & ATTR_GID) | |
167 | inode->i_gid = attr->ia_gid; | |
168 | if (ia_valid & ATTR_ATIME) | |
169 | inode->i_atime = timespec_trunc(attr->ia_atime, | |
170 | inode->i_sb->s_time_gran); | |
171 | if (ia_valid & ATTR_MTIME) | |
172 | inode->i_mtime = timespec_trunc(attr->ia_mtime, | |
173 | inode->i_sb->s_time_gran); | |
174 | if (ia_valid & ATTR_CTIME) | |
175 | inode->i_ctime = timespec_trunc(attr->ia_ctime, | |
176 | inode->i_sb->s_time_gran); | |
177 | if (ia_valid & ATTR_MODE) { | |
178 | umode_t mode = attr->ia_mode; | |
179 | ||
7fa294c8 | 180 | if (!in_group_p(inode->i_gid) && |
23adbe12 | 181 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) |
1da177e4 LT |
182 | mode &= ~S_ISGID; |
183 | inode->i_mode = mode; | |
184 | } | |
7bb46a67 | 185 | } |
6a1a90ad | 186 | EXPORT_SYMBOL(setattr_copy); |
7bb46a67 | 187 | |
27ac0ffe BF |
188 | /** |
189 | * notify_change - modify attributes of a filesytem object | |
190 | * @dentry: object affected | |
191 | * @iattr: new attributes | |
192 | * @delegated_inode: returns inode, if the inode is delegated | |
193 | * | |
194 | * The caller must hold the i_mutex on the affected object. | |
195 | * | |
196 | * If notify_change discovers a delegation in need of breaking, | |
197 | * it will return -EWOULDBLOCK and return a reference to the inode in | |
198 | * delegated_inode. The caller should then break the delegation and | |
199 | * retry. Because breaking a delegation may take a long time, the | |
200 | * caller should drop the i_mutex before doing so. | |
201 | * | |
202 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
203 | * be appropriate for callers that expect the underlying filesystem not | |
204 | * to be NFS exported. Also, passing NULL is fine for callers holding | |
205 | * the file open for write, as there can be no conflicting delegation in | |
206 | * that case. | |
207 | */ | |
208 | int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode) | |
1da177e4 LT |
209 | { |
210 | struct inode *inode = dentry->d_inode; | |
8d334acd | 211 | umode_t mode = inode->i_mode; |
1da177e4 LT |
212 | int error; |
213 | struct timespec now; | |
214 | unsigned int ia_valid = attr->ia_valid; | |
215 | ||
c4107b30 AM |
216 | WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex)); |
217 | ||
beb29e05 MS |
218 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { |
219 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
220 | return -EPERM; | |
221 | } | |
222 | ||
f1e96ec9 MS |
223 | /* |
224 | * If utimes(2) and friends are called with times == NULL (or both | |
225 | * times are UTIME_NOW), then we need to check for write permission | |
226 | */ | |
227 | if (ia_valid & ATTR_TOUCH) { | |
228 | if (IS_IMMUTABLE(inode)) | |
229 | return -EPERM; | |
230 | ||
231 | if (!inode_owner_or_capable(inode)) { | |
232 | error = inode_permission(inode, MAY_WRITE); | |
233 | if (error) | |
234 | return error; | |
235 | } | |
236 | } | |
237 | ||
69b45732 | 238 | if ((ia_valid & ATTR_MODE)) { |
8d334acd | 239 | umode_t amode = attr->ia_mode; |
69b45732 AK |
240 | /* Flag setting protected by i_mutex */ |
241 | if (is_sxid(amode)) | |
242 | inode->i_flags &= ~S_NOSEC; | |
243 | } | |
244 | ||
1da177e4 LT |
245 | now = current_fs_time(inode->i_sb); |
246 | ||
247 | attr->ia_ctime = now; | |
248 | if (!(ia_valid & ATTR_ATIME_SET)) | |
249 | attr->ia_atime = now; | |
250 | if (!(ia_valid & ATTR_MTIME_SET)) | |
251 | attr->ia_mtime = now; | |
b5376771 SH |
252 | if (ia_valid & ATTR_KILL_PRIV) { |
253 | attr->ia_valid &= ~ATTR_KILL_PRIV; | |
254 | ia_valid &= ~ATTR_KILL_PRIV; | |
255 | error = security_inode_need_killpriv(dentry); | |
256 | if (error > 0) | |
257 | error = security_inode_killpriv(dentry); | |
258 | if (error) | |
259 | return error; | |
260 | } | |
6de0ec00 JL |
261 | |
262 | /* | |
263 | * We now pass ATTR_KILL_S*ID to the lower level setattr function so | |
264 | * that the function has the ability to reinterpret a mode change | |
265 | * that's due to these bits. This adds an implicit restriction that | |
266 | * no function will ever call notify_change with both ATTR_MODE and | |
267 | * ATTR_KILL_S*ID set. | |
268 | */ | |
269 | if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && | |
270 | (ia_valid & ATTR_MODE)) | |
271 | BUG(); | |
272 | ||
1da177e4 | 273 | if (ia_valid & ATTR_KILL_SUID) { |
1da177e4 | 274 | if (mode & S_ISUID) { |
6de0ec00 JL |
275 | ia_valid = attr->ia_valid |= ATTR_MODE; |
276 | attr->ia_mode = (inode->i_mode & ~S_ISUID); | |
1da177e4 LT |
277 | } |
278 | } | |
279 | if (ia_valid & ATTR_KILL_SGID) { | |
1da177e4 LT |
280 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
281 | if (!(ia_valid & ATTR_MODE)) { | |
282 | ia_valid = attr->ia_valid |= ATTR_MODE; | |
283 | attr->ia_mode = inode->i_mode; | |
284 | } | |
285 | attr->ia_mode &= ~S_ISGID; | |
286 | } | |
287 | } | |
6de0ec00 | 288 | if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) |
1da177e4 LT |
289 | return 0; |
290 | ||
7ccd1a40 SF |
291 | /* |
292 | * Verify that uid/gid changes are valid in the target | |
293 | * namespace of the superblock. | |
294 | */ | |
295 | if (ia_valid & ATTR_UID && | |
296 | !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) | |
297 | return -EOVERFLOW; | |
298 | if (ia_valid & ATTR_GID && | |
299 | !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) | |
300 | return -EOVERFLOW; | |
301 | ||
be494dcc EB |
302 | /* Don't allow modifications of files with invalid uids or |
303 | * gids unless those uids & gids are being made valid. | |
304 | */ | |
305 | if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid)) | |
306 | return -EOVERFLOW; | |
307 | if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid)) | |
308 | return -EOVERFLOW; | |
309 | ||
a77b72da | 310 | error = security_inode_setattr(dentry, attr); |
27ac0ffe BF |
311 | if (error) |
312 | return error; | |
313 | error = try_break_deleg(inode, delegated_inode); | |
a77b72da MS |
314 | if (error) |
315 | return error; | |
316 | ||
eef2380c | 317 | if (inode->i_op->setattr) |
a77b72da | 318 | error = inode->i_op->setattr(dentry, attr); |
eef2380c CH |
319 | else |
320 | error = simple_setattr(dentry, attr); | |
1da177e4 | 321 | |
975d2943 | 322 | if (!error) { |
0eeca283 | 323 | fsnotify_change(dentry, ia_valid); |
9957a504 | 324 | ima_inode_post_setattr(dentry); |
975d2943 MZ |
325 | evm_inode_post_setattr(dentry, ia_valid); |
326 | } | |
0eeca283 | 327 | |
1da177e4 LT |
328 | return error; |
329 | } | |
1da177e4 | 330 | EXPORT_SYMBOL(notify_change); |