]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/attr.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | * changes by Thomas Schoebel-Theuer | |
7 | */ | |
8 | ||
630d9c47 | 9 | #include <linux/export.h> |
1da177e4 LT |
10 | #include <linux/time.h> |
11 | #include <linux/mm.h> | |
12 | #include <linux/string.h> | |
3f07c014 | 13 | #include <linux/sched/signal.h> |
16f7e0fe | 14 | #include <linux/capability.h> |
0eeca283 | 15 | #include <linux/fsnotify.h> |
1da177e4 | 16 | #include <linux/fcntl.h> |
5970e15d | 17 | #include <linux/filelock.h> |
1da177e4 | 18 | #include <linux/security.h> |
1da177e4 | 19 | |
11c2a870 CB |
20 | #include "internal.h" |
21 | ||
72ae017c CB |
22 | /** |
23 | * setattr_should_drop_sgid - determine whether the setgid bit needs to be | |
24 | * removed | |
9452e93e | 25 | * @idmap: idmap of the mount @inode was found from |
72ae017c CB |
26 | * @inode: inode to check |
27 | * | |
28 | * This function determines whether the setgid bit needs to be removed. | |
29 | * We retain backwards compatibility and require setgid bit to be removed | |
30 | * unconditionally if S_IXGRP is set. Otherwise we have the exact same | |
31 | * requirements as setattr_prepare() and setattr_copy(). | |
32 | * | |
33 | * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise. | |
34 | */ | |
9452e93e | 35 | int setattr_should_drop_sgid(struct mnt_idmap *idmap, |
72ae017c CB |
36 | const struct inode *inode) |
37 | { | |
38 | umode_t mode = inode->i_mode; | |
39 | ||
40 | if (!(mode & S_ISGID)) | |
41 | return 0; | |
42 | if (mode & S_IXGRP) | |
43 | return ATTR_KILL_SGID; | |
e67fe633 | 44 | if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode))) |
72ae017c CB |
45 | return ATTR_KILL_SGID; |
46 | return 0; | |
47 | } | |
4f704d9a | 48 | EXPORT_SYMBOL(setattr_should_drop_sgid); |
72ae017c | 49 | |
ed5a7047 CB |
50 | /** |
51 | * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to | |
52 | * be dropped | |
9452e93e | 53 | * @idmap: idmap of the mount @inode was found from |
ed5a7047 | 54 | * @inode: inode to check |
e243e3f9 | 55 | * |
ed5a7047 CB |
56 | * This function determines whether the set{g,u}id bits need to be removed. |
57 | * If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the | |
58 | * setgid bit needs to be removed ATTR_KILL_SGID is returned. If both | |
59 | * set{g,u}id bits need to be removed the corresponding mask of both flags is | |
60 | * returned. | |
61 | * | |
62 | * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits | |
63 | * to remove, 0 otherwise. | |
e243e3f9 | 64 | */ |
9452e93e | 65 | int setattr_should_drop_suidgid(struct mnt_idmap *idmap, |
ed5a7047 | 66 | struct inode *inode) |
e243e3f9 | 67 | { |
ed5a7047 | 68 | umode_t mode = inode->i_mode; |
e243e3f9 CB |
69 | int kill = 0; |
70 | ||
71 | /* suid always must be killed */ | |
72 | if (unlikely(mode & S_ISUID)) | |
73 | kill = ATTR_KILL_SUID; | |
74 | ||
9452e93e | 75 | kill |= setattr_should_drop_sgid(idmap, inode); |
e243e3f9 CB |
76 | |
77 | if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode))) | |
78 | return kill; | |
79 | ||
80 | return 0; | |
81 | } | |
ed5a7047 | 82 | EXPORT_SYMBOL(setattr_should_drop_suidgid); |
e243e3f9 | 83 | |
2f221d6f CB |
84 | /** |
85 | * chown_ok - verify permissions to chown inode | |
9452e93e | 86 | * @idmap: idmap of the mount @inode was found from |
2f221d6f | 87 | * @inode: inode to check permissions on |
81a1807d | 88 | * @ia_vfsuid: uid to chown @inode to |
2f221d6f | 89 | * |
9452e93e CB |
90 | * If the inode has been found through an idmapped mount the idmap of |
91 | * the vfsmount must be passed through @idmap. This function will then | |
92 | * take care to map the inode according to @idmap before checking | |
2f221d6f | 93 | * permissions. On non-idmapped mounts or if permission checking is to be |
9452e93e | 94 | * performed on the raw inode simply pass @nop_mnt_idmap. |
2f221d6f | 95 | */ |
9452e93e | 96 | static bool chown_ok(struct mnt_idmap *idmap, |
b27c82e1 | 97 | const struct inode *inode, vfsuid_t ia_vfsuid) |
0031181c | 98 | { |
e67fe633 | 99 | vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); |
b27c82e1 CB |
100 | if (vfsuid_eq_kuid(vfsuid, current_fsuid()) && |
101 | vfsuid_eq(ia_vfsuid, vfsuid)) | |
0031181c | 102 | return true; |
9452e93e | 103 | if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN)) |
0031181c | 104 | return true; |
b27c82e1 | 105 | if (!vfsuid_valid(vfsuid) && |
0031181c EB |
106 | ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) |
107 | return true; | |
108 | return false; | |
109 | } | |
110 | ||
2f221d6f CB |
111 | /** |
112 | * chgrp_ok - verify permissions to chgrp inode | |
9452e93e | 113 | * @idmap: idmap of the mount @inode was found from |
2f221d6f | 114 | * @inode: inode to check permissions on |
81a1807d | 115 | * @ia_vfsgid: gid to chown @inode to |
2f221d6f | 116 | * |
9452e93e CB |
117 | * If the inode has been found through an idmapped mount the idmap of |
118 | * the vfsmount must be passed through @idmap. This function will then | |
119 | * take care to map the inode according to @idmap before checking | |
2f221d6f | 120 | * permissions. On non-idmapped mounts or if permission checking is to be |
9452e93e | 121 | * performed on the raw inode simply pass @nop_mnt_idmap. |
2f221d6f | 122 | */ |
9452e93e | 123 | static bool chgrp_ok(struct mnt_idmap *idmap, |
b27c82e1 | 124 | const struct inode *inode, vfsgid_t ia_vfsgid) |
0031181c | 125 | { |
e67fe633 CB |
126 | vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); |
127 | vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); | |
b27c82e1 CB |
128 | if (vfsuid_eq_kuid(vfsuid, current_fsuid())) { |
129 | if (vfsgid_eq(ia_vfsgid, vfsgid)) | |
168f9128 | 130 | return true; |
b27c82e1 | 131 | if (vfsgid_in_group_p(ia_vfsgid)) |
168f9128 CB |
132 | return true; |
133 | } | |
9452e93e | 134 | if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN)) |
0031181c | 135 | return true; |
b27c82e1 | 136 | if (!vfsgid_valid(vfsgid) && |
0031181c EB |
137 | ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) |
138 | return true; | |
139 | return false; | |
140 | } | |
141 | ||
2c27c65e | 142 | /** |
31051c85 | 143 | * setattr_prepare - check if attribute changes to a dentry are allowed |
c1632a0f | 144 | * @idmap: idmap of the mount the inode was found from |
31051c85 | 145 | * @dentry: dentry to check |
2c27c65e CH |
146 | * @attr: attributes to change |
147 | * | |
148 | * Check if we are allowed to change the attributes contained in @attr | |
31051c85 JK |
149 | * in the given dentry. This includes the normal unix access permission |
150 | * checks, as well as checks for rlimits and others. The function also clears | |
151 | * SGID bit from mode if user is not allowed to set it. Also file capabilities | |
152 | * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. | |
2c27c65e | 153 | * |
c1632a0f CB |
154 | * If the inode has been found through an idmapped mount the idmap of |
155 | * the vfsmount must be passed through @idmap. This function will then | |
156 | * take care to map the inode according to @idmap before checking | |
2f221d6f | 157 | * permissions. On non-idmapped mounts or if permission checking is to be |
376870aa | 158 | * performed on the raw inode simply pass @nop_mnt_idmap. |
2f221d6f | 159 | * |
2c27c65e CH |
160 | * Should be called as the first thing in ->setattr implementations, |
161 | * possibly after taking additional locks. | |
162 | */ | |
c1632a0f | 163 | int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry, |
2f221d6f | 164 | struct iattr *attr) |
1da177e4 | 165 | { |
31051c85 | 166 | struct inode *inode = d_inode(dentry); |
1da177e4 LT |
167 | unsigned int ia_valid = attr->ia_valid; |
168 | ||
2c27c65e CH |
169 | /* |
170 | * First check size constraints. These can't be overriden using | |
171 | * ATTR_FORCE. | |
172 | */ | |
173 | if (ia_valid & ATTR_SIZE) { | |
174 | int error = inode_newsize_ok(inode, attr->ia_size); | |
175 | if (error) | |
176 | return error; | |
177 | } | |
178 | ||
1da177e4 LT |
179 | /* If force is set do it anyway. */ |
180 | if (ia_valid & ATTR_FORCE) | |
030b533c | 181 | goto kill_priv; |
1da177e4 LT |
182 | |
183 | /* Make sure a caller can chown. */ | |
b27c82e1 | 184 | if ((ia_valid & ATTR_UID) && |
9452e93e | 185 | !chown_ok(idmap, inode, attr->ia_vfsuid)) |
2c27c65e | 186 | return -EPERM; |
1da177e4 LT |
187 | |
188 | /* Make sure caller can chgrp. */ | |
b27c82e1 | 189 | if ((ia_valid & ATTR_GID) && |
9452e93e | 190 | !chgrp_ok(idmap, inode, attr->ia_vfsgid)) |
2c27c65e | 191 | return -EPERM; |
1da177e4 LT |
192 | |
193 | /* Make sure a caller can chmod. */ | |
194 | if (ia_valid & ATTR_MODE) { | |
b27c82e1 | 195 | vfsgid_t vfsgid; |
168f9128 | 196 | |
01beba79 | 197 | if (!inode_owner_or_capable(idmap, inode)) |
2c27c65e | 198 | return -EPERM; |
168f9128 CB |
199 | |
200 | if (ia_valid & ATTR_GID) | |
b27c82e1 | 201 | vfsgid = attr->ia_vfsgid; |
168f9128 | 202 | else |
e67fe633 | 203 | vfsgid = i_gid_into_vfsgid(idmap, inode); |
168f9128 | 204 | |
1da177e4 | 205 | /* Also check the setgid bit! */ |
9452e93e | 206 | if (!in_group_or_capable(idmap, inode, vfsgid)) |
1da177e4 LT |
207 | attr->ia_mode &= ~S_ISGID; |
208 | } | |
209 | ||
210 | /* Check for setting the inode time. */ | |
9767d749 | 211 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { |
01beba79 | 212 | if (!inode_owner_or_capable(idmap, inode)) |
2c27c65e | 213 | return -EPERM; |
1da177e4 | 214 | } |
2c27c65e | 215 | |
030b533c JK |
216 | kill_priv: |
217 | /* User has permission for the change */ | |
218 | if (ia_valid & ATTR_KILL_PRIV) { | |
219 | int error; | |
220 | ||
39f60c1c | 221 | error = security_inode_killpriv(idmap, dentry); |
030b533c JK |
222 | if (error) |
223 | return error; | |
224 | } | |
225 | ||
2c27c65e | 226 | return 0; |
1da177e4 | 227 | } |
31051c85 | 228 | EXPORT_SYMBOL(setattr_prepare); |
1da177e4 | 229 | |
25d9e2d1 NP |
230 | /** |
231 | * inode_newsize_ok - may this inode be truncated to a given size | |
232 | * @inode: the inode to be truncated | |
233 | * @offset: the new size to assign to the inode | |
25d9e2d1 | 234 | * |
7bb46a67 NP |
235 | * inode_newsize_ok must be called with i_mutex held. |
236 | * | |
25d9e2d1 NP |
237 | * inode_newsize_ok will check filesystem limits and ulimits to check that the |
238 | * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ | |
239 | * when necessary. Caller must not proceed with inode size change if failure is | |
240 | * returned. @inode must be a file (not directory), with appropriate | |
241 | * permissions to allow truncate (inode_newsize_ok does NOT check these | |
242 | * conditions). | |
3fae1746 MW |
243 | * |
244 | * Return: 0 on success, -ve errno on failure | |
25d9e2d1 NP |
245 | */ |
246 | int inode_newsize_ok(const struct inode *inode, loff_t offset) | |
247 | { | |
e2ebff9c DH |
248 | if (offset < 0) |
249 | return -EINVAL; | |
25d9e2d1 NP |
250 | if (inode->i_size < offset) { |
251 | unsigned long limit; | |
252 | ||
d554ed89 | 253 | limit = rlimit(RLIMIT_FSIZE); |
25d9e2d1 NP |
254 | if (limit != RLIM_INFINITY && offset > limit) |
255 | goto out_sig; | |
256 | if (offset > inode->i_sb->s_maxbytes) | |
257 | goto out_big; | |
258 | } else { | |
259 | /* | |
260 | * truncation of in-use swapfiles is disallowed - it would | |
261 | * cause subsequent swapout to scribble on the now-freed | |
262 | * blocks. | |
263 | */ | |
264 | if (IS_SWAPFILE(inode)) | |
265 | return -ETXTBSY; | |
266 | } | |
267 | ||
268 | return 0; | |
269 | out_sig: | |
270 | send_sig(SIGXFSZ, current, 0); | |
271 | out_big: | |
272 | return -EFBIG; | |
273 | } | |
274 | EXPORT_SYMBOL(inode_newsize_ok); | |
275 | ||
7bb46a67 | 276 | /** |
6a1a90ad | 277 | * setattr_copy - copy simple metadata updates into the generic inode |
c1632a0f | 278 | * @idmap: idmap of the mount the inode was found from |
7bb46a67 NP |
279 | * @inode: the inode to be updated |
280 | * @attr: the new attributes | |
281 | * | |
6a1a90ad | 282 | * setattr_copy must be called with i_mutex held. |
7bb46a67 | 283 | * |
6a1a90ad | 284 | * setattr_copy updates the inode's metadata with that specified |
b27c82e1 | 285 | * in attr on idmapped mounts. Necessary permission checks to determine |
2f221d6f CB |
286 | * whether or not the S_ISGID property needs to be removed are performed with |
287 | * the correct idmapped mount permission helpers. | |
288 | * Noticeably missing is inode size update, which is more complex | |
2c27c65e | 289 | * as it requires pagecache updates. |
7bb46a67 | 290 | * |
c1632a0f CB |
291 | * If the inode has been found through an idmapped mount the idmap of |
292 | * the vfsmount must be passed through @idmap. This function will then | |
293 | * take care to map the inode according to @idmap before checking | |
2f221d6f | 294 | * permissions. On non-idmapped mounts or if permission checking is to be |
c1632a0f | 295 | * performed on the raw inode simply pass @nop_mnt_idmap. |
2f221d6f | 296 | * |
7bb46a67 NP |
297 | * The inode is not marked as dirty after this operation. The rationale is |
298 | * that for "simple" filesystems, the struct inode is the inode storage. | |
299 | * The caller is free to mark the inode dirty afterwards if needed. | |
300 | */ | |
c1632a0f | 301 | void setattr_copy(struct mnt_idmap *idmap, struct inode *inode, |
2f221d6f | 302 | const struct iattr *attr) |
1da177e4 LT |
303 | { |
304 | unsigned int ia_valid = attr->ia_valid; | |
4a30131e | 305 | |
0dbe12f2 CB |
306 | i_uid_update(idmap, attr, inode); |
307 | i_gid_update(idmap, attr, inode); | |
eb31e2f6 | 308 | if (ia_valid & ATTR_ATIME) |
16a94965 | 309 | inode_set_atime_to_ts(inode, attr->ia_atime); |
eb31e2f6 | 310 | if (ia_valid & ATTR_MTIME) |
16a94965 | 311 | inode_set_mtime_to_ts(inode, attr->ia_mtime); |
eb31e2f6 | 312 | if (ia_valid & ATTR_CTIME) |
2276e5ba | 313 | inode_set_ctime_to_ts(inode, attr->ia_ctime); |
1da177e4 LT |
314 | if (ia_valid & ATTR_MODE) { |
315 | umode_t mode = attr->ia_mode; | |
9452e93e | 316 | if (!in_group_or_capable(idmap, inode, |
e67fe633 | 317 | i_gid_into_vfsgid(idmap, inode))) |
1da177e4 LT |
318 | mode &= ~S_ISGID; |
319 | inode->i_mode = mode; | |
320 | } | |
7bb46a67 | 321 | } |
6a1a90ad | 322 | EXPORT_SYMBOL(setattr_copy); |
7bb46a67 | 323 | |
4609e1f1 | 324 | int may_setattr(struct mnt_idmap *idmap, struct inode *inode, |
7bb698f0 AG |
325 | unsigned int ia_valid) |
326 | { | |
327 | int error; | |
328 | ||
329 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { | |
330 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
331 | return -EPERM; | |
332 | } | |
333 | ||
334 | /* | |
335 | * If utimes(2) and friends are called with times == NULL (or both | |
336 | * times are UTIME_NOW), then we need to check for write permission | |
337 | */ | |
338 | if (ia_valid & ATTR_TOUCH) { | |
339 | if (IS_IMMUTABLE(inode)) | |
340 | return -EPERM; | |
341 | ||
01beba79 | 342 | if (!inode_owner_or_capable(idmap, inode)) { |
4609e1f1 | 343 | error = inode_permission(idmap, inode, MAY_WRITE); |
7bb698f0 AG |
344 | if (error) |
345 | return error; | |
346 | } | |
347 | } | |
348 | return 0; | |
349 | } | |
350 | EXPORT_SYMBOL(may_setattr); | |
351 | ||
27ac0ffe | 352 | /** |
fe12cfc1 | 353 | * notify_change - modify attributes of a filesystem object |
abf08576 | 354 | * @idmap: idmap of the mount the inode was found from |
27ac0ffe | 355 | * @dentry: object affected |
3fae1746 | 356 | * @attr: new attributes |
27ac0ffe BF |
357 | * @delegated_inode: returns inode, if the inode is delegated |
358 | * | |
359 | * The caller must hold the i_mutex on the affected object. | |
360 | * | |
361 | * If notify_change discovers a delegation in need of breaking, | |
362 | * it will return -EWOULDBLOCK and return a reference to the inode in | |
363 | * delegated_inode. The caller should then break the delegation and | |
364 | * retry. Because breaking a delegation may take a long time, the | |
365 | * caller should drop the i_mutex before doing so. | |
366 | * | |
367 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
368 | * be appropriate for callers that expect the underlying filesystem not | |
369 | * to be NFS exported. Also, passing NULL is fine for callers holding | |
370 | * the file open for write, as there can be no conflicting delegation in | |
371 | * that case. | |
2f221d6f | 372 | * |
abf08576 CB |
373 | * If the inode has been found through an idmapped mount the idmap of |
374 | * the vfsmount must be passed through @idmap. This function will then | |
375 | * take care to map the inode according to @idmap before checking | |
2f221d6f | 376 | * permissions. On non-idmapped mounts or if permission checking is to be |
abf08576 | 377 | * performed on the raw inode simply pass @nop_mnt_idmap. |
27ac0ffe | 378 | */ |
abf08576 | 379 | int notify_change(struct mnt_idmap *idmap, struct dentry *dentry, |
2f221d6f | 380 | struct iattr *attr, struct inode **delegated_inode) |
1da177e4 LT |
381 | { |
382 | struct inode *inode = dentry->d_inode; | |
8d334acd | 383 | umode_t mode = inode->i_mode; |
1da177e4 | 384 | int error; |
95582b00 | 385 | struct timespec64 now; |
1da177e4 LT |
386 | unsigned int ia_valid = attr->ia_valid; |
387 | ||
5955102c | 388 | WARN_ON_ONCE(!inode_is_locked(inode)); |
c4107b30 | 389 | |
4609e1f1 | 390 | error = may_setattr(idmap, inode, ia_valid); |
7bb698f0 AG |
391 | if (error) |
392 | return error; | |
f2b20f6e | 393 | |
69b45732 | 394 | if ((ia_valid & ATTR_MODE)) { |
5d1f903f CB |
395 | /* |
396 | * Don't allow changing the mode of symlinks: | |
397 | * | |
398 | * (1) The vfs doesn't take the mode of symlinks into account | |
399 | * during permission checking. | |
400 | * (2) This has never worked correctly. Most major filesystems | |
401 | * did return EOPNOTSUPP due to interactions with POSIX ACLs | |
402 | * but did still updated the mode of the symlink. | |
403 | * This inconsistency led system call wrapper providers such | |
404 | * as libc to block changing the mode of symlinks with | |
405 | * EOPNOTSUPP already. | |
406 | * (3) To even do this in the first place one would have to use | |
407 | * specific file descriptors and quite some effort. | |
408 | */ | |
409 | if (S_ISLNK(inode->i_mode)) | |
410 | return -EOPNOTSUPP; | |
411 | ||
69b45732 | 412 | /* Flag setting protected by i_mutex */ |
5d1f903f | 413 | if (is_sxid(attr->ia_mode)) |
69b45732 AK |
414 | inode->i_flags &= ~S_NOSEC; |
415 | } | |
416 | ||
c2050a45 | 417 | now = current_time(inode); |
1da177e4 LT |
418 | |
419 | attr->ia_ctime = now; | |
420 | if (!(ia_valid & ATTR_ATIME_SET)) | |
421 | attr->ia_atime = now; | |
eb31e2f6 AG |
422 | else |
423 | attr->ia_atime = timestamp_truncate(attr->ia_atime, inode); | |
1da177e4 LT |
424 | if (!(ia_valid & ATTR_MTIME_SET)) |
425 | attr->ia_mtime = now; | |
eb31e2f6 AG |
426 | else |
427 | attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); | |
428 | ||
b5376771 | 429 | if (ia_valid & ATTR_KILL_PRIV) { |
b5376771 | 430 | error = security_inode_need_killpriv(dentry); |
030b533c | 431 | if (error < 0) |
b5376771 | 432 | return error; |
030b533c JK |
433 | if (error == 0) |
434 | ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; | |
b5376771 | 435 | } |
6de0ec00 JL |
436 | |
437 | /* | |
438 | * We now pass ATTR_KILL_S*ID to the lower level setattr function so | |
439 | * that the function has the ability to reinterpret a mode change | |
440 | * that's due to these bits. This adds an implicit restriction that | |
441 | * no function will ever call notify_change with both ATTR_MODE and | |
442 | * ATTR_KILL_S*ID set. | |
443 | */ | |
444 | if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && | |
445 | (ia_valid & ATTR_MODE)) | |
446 | BUG(); | |
447 | ||
1da177e4 | 448 | if (ia_valid & ATTR_KILL_SUID) { |
1da177e4 | 449 | if (mode & S_ISUID) { |
6de0ec00 JL |
450 | ia_valid = attr->ia_valid |= ATTR_MODE; |
451 | attr->ia_mode = (inode->i_mode & ~S_ISUID); | |
1da177e4 LT |
452 | } |
453 | } | |
454 | if (ia_valid & ATTR_KILL_SGID) { | |
ed5a7047 | 455 | if (mode & S_ISGID) { |
1da177e4 LT |
456 | if (!(ia_valid & ATTR_MODE)) { |
457 | ia_valid = attr->ia_valid |= ATTR_MODE; | |
458 | attr->ia_mode = inode->i_mode; | |
459 | } | |
460 | attr->ia_mode &= ~S_ISGID; | |
461 | } | |
462 | } | |
6de0ec00 | 463 | if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) |
1da177e4 LT |
464 | return 0; |
465 | ||
a475acf0 SF |
466 | /* |
467 | * Verify that uid/gid changes are valid in the target | |
468 | * namespace of the superblock. | |
469 | */ | |
470 | if (ia_valid & ATTR_UID && | |
4d7ca409 | 471 | !vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns, |
b27c82e1 | 472 | attr->ia_vfsuid)) |
a475acf0 SF |
473 | return -EOVERFLOW; |
474 | if (ia_valid & ATTR_GID && | |
4d7ca409 | 475 | !vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns, |
b27c82e1 | 476 | attr->ia_vfsgid)) |
a475acf0 SF |
477 | return -EOVERFLOW; |
478 | ||
0bd23d09 EB |
479 | /* Don't allow modifications of files with invalid uids or |
480 | * gids unless those uids & gids are being made valid. | |
481 | */ | |
2f221d6f | 482 | if (!(ia_valid & ATTR_UID) && |
e67fe633 | 483 | !vfsuid_valid(i_uid_into_vfsuid(idmap, inode))) |
0bd23d09 | 484 | return -EOVERFLOW; |
2f221d6f | 485 | if (!(ia_valid & ATTR_GID) && |
e67fe633 | 486 | !vfsgid_valid(i_gid_into_vfsgid(idmap, inode))) |
0bd23d09 EB |
487 | return -EOVERFLOW; |
488 | ||
c1632a0f | 489 | error = security_inode_setattr(idmap, dentry, attr); |
27ac0ffe BF |
490 | if (error) |
491 | return error; | |
492 | error = try_break_deleg(inode, delegated_inode); | |
a77b72da MS |
493 | if (error) |
494 | return error; | |
495 | ||
eef2380c | 496 | if (inode->i_op->setattr) |
c1632a0f | 497 | error = inode->i_op->setattr(idmap, dentry, attr); |
eef2380c | 498 | else |
c1632a0f | 499 | error = simple_setattr(idmap, dentry, attr); |
1da177e4 | 500 | |
975d2943 | 501 | if (!error) { |
0eeca283 | 502 | fsnotify_change(dentry, ia_valid); |
77fa6f31 | 503 | security_inode_post_setattr(idmap, dentry, ia_valid); |
975d2943 | 504 | } |
0eeca283 | 505 | |
1da177e4 LT |
506 | return error; |
507 | } | |
1da177e4 | 508 | EXPORT_SYMBOL(notify_change); |