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> |
1da177e4 | 17 | #include <linux/security.h> |
975d2943 | 18 | #include <linux/evm.h> |
9957a504 | 19 | #include <linux/ima.h> |
1da177e4 | 20 | |
11c2a870 CB |
21 | #include "internal.h" |
22 | ||
2f221d6f CB |
23 | /** |
24 | * chown_ok - verify permissions to chown inode | |
25 | * @mnt_userns: user namespace of the mount @inode was found from | |
26 | * @inode: inode to check permissions on | |
81a1807d | 27 | * @ia_vfsuid: uid to chown @inode to |
2f221d6f CB |
28 | * |
29 | * If the inode has been found through an idmapped mount the user namespace of | |
30 | * the vfsmount must be passed through @mnt_userns. This function will then | |
31 | * take care to map the inode according to @mnt_userns before checking | |
32 | * permissions. On non-idmapped mounts or if permission checking is to be | |
33 | * performed on the raw inode simply passs init_user_ns. | |
34 | */ | |
35 | static bool chown_ok(struct user_namespace *mnt_userns, | |
b27c82e1 | 36 | const struct inode *inode, vfsuid_t ia_vfsuid) |
0031181c | 37 | { |
b27c82e1 CB |
38 | vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode); |
39 | if (vfsuid_eq_kuid(vfsuid, current_fsuid()) && | |
40 | vfsuid_eq(ia_vfsuid, vfsuid)) | |
0031181c | 41 | return true; |
2f221d6f | 42 | if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN)) |
0031181c | 43 | return true; |
b27c82e1 | 44 | if (!vfsuid_valid(vfsuid) && |
0031181c EB |
45 | ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) |
46 | return true; | |
47 | return false; | |
48 | } | |
49 | ||
2f221d6f CB |
50 | /** |
51 | * chgrp_ok - verify permissions to chgrp inode | |
52 | * @mnt_userns: user namespace of the mount @inode was found from | |
53 | * @inode: inode to check permissions on | |
81a1807d | 54 | * @ia_vfsgid: gid to chown @inode to |
2f221d6f CB |
55 | * |
56 | * If the inode has been found through an idmapped mount the user namespace of | |
57 | * the vfsmount must be passed through @mnt_userns. This function will then | |
58 | * take care to map the inode according to @mnt_userns before checking | |
59 | * permissions. On non-idmapped mounts or if permission checking is to be | |
60 | * performed on the raw inode simply passs init_user_ns. | |
61 | */ | |
62 | static bool chgrp_ok(struct user_namespace *mnt_userns, | |
b27c82e1 | 63 | const struct inode *inode, vfsgid_t ia_vfsgid) |
0031181c | 64 | { |
b27c82e1 CB |
65 | vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode); |
66 | vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode); | |
67 | if (vfsuid_eq_kuid(vfsuid, current_fsuid())) { | |
68 | if (vfsgid_eq(ia_vfsgid, vfsgid)) | |
168f9128 | 69 | return true; |
b27c82e1 | 70 | if (vfsgid_in_group_p(ia_vfsgid)) |
168f9128 CB |
71 | return true; |
72 | } | |
2f221d6f | 73 | if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN)) |
0031181c | 74 | return true; |
b27c82e1 | 75 | if (!vfsgid_valid(vfsgid) && |
0031181c EB |
76 | ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) |
77 | return true; | |
78 | return false; | |
79 | } | |
80 | ||
2c27c65e | 81 | /** |
31051c85 | 82 | * setattr_prepare - check if attribute changes to a dentry are allowed |
2f221d6f | 83 | * @mnt_userns: user namespace of the mount the inode was found from |
31051c85 | 84 | * @dentry: dentry to check |
2c27c65e CH |
85 | * @attr: attributes to change |
86 | * | |
87 | * Check if we are allowed to change the attributes contained in @attr | |
31051c85 JK |
88 | * in the given dentry. This includes the normal unix access permission |
89 | * checks, as well as checks for rlimits and others. The function also clears | |
90 | * SGID bit from mode if user is not allowed to set it. Also file capabilities | |
91 | * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set. | |
2c27c65e | 92 | * |
2f221d6f CB |
93 | * If the inode has been found through an idmapped mount the user namespace of |
94 | * the vfsmount must be passed through @mnt_userns. This function will then | |
95 | * take care to map the inode according to @mnt_userns before checking | |
96 | * permissions. On non-idmapped mounts or if permission checking is to be | |
97 | * performed on the raw inode simply passs init_user_ns. | |
98 | * | |
2c27c65e CH |
99 | * Should be called as the first thing in ->setattr implementations, |
100 | * possibly after taking additional locks. | |
101 | */ | |
2f221d6f CB |
102 | int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry, |
103 | struct iattr *attr) | |
1da177e4 | 104 | { |
31051c85 | 105 | struct inode *inode = d_inode(dentry); |
1da177e4 LT |
106 | unsigned int ia_valid = attr->ia_valid; |
107 | ||
2c27c65e CH |
108 | /* |
109 | * First check size constraints. These can't be overriden using | |
110 | * ATTR_FORCE. | |
111 | */ | |
112 | if (ia_valid & ATTR_SIZE) { | |
113 | int error = inode_newsize_ok(inode, attr->ia_size); | |
114 | if (error) | |
115 | return error; | |
116 | } | |
117 | ||
1da177e4 LT |
118 | /* If force is set do it anyway. */ |
119 | if (ia_valid & ATTR_FORCE) | |
030b533c | 120 | goto kill_priv; |
1da177e4 LT |
121 | |
122 | /* Make sure a caller can chown. */ | |
b27c82e1 CB |
123 | if ((ia_valid & ATTR_UID) && |
124 | !chown_ok(mnt_userns, inode, attr->ia_vfsuid)) | |
2c27c65e | 125 | return -EPERM; |
1da177e4 LT |
126 | |
127 | /* Make sure caller can chgrp. */ | |
b27c82e1 CB |
128 | if ((ia_valid & ATTR_GID) && |
129 | !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid)) | |
2c27c65e | 130 | return -EPERM; |
1da177e4 LT |
131 | |
132 | /* Make sure a caller can chmod. */ | |
133 | if (ia_valid & ATTR_MODE) { | |
b27c82e1 | 134 | vfsgid_t vfsgid; |
168f9128 | 135 | |
2f221d6f | 136 | if (!inode_owner_or_capable(mnt_userns, inode)) |
2c27c65e | 137 | return -EPERM; |
168f9128 CB |
138 | |
139 | if (ia_valid & ATTR_GID) | |
b27c82e1 | 140 | vfsgid = attr->ia_vfsgid; |
168f9128 | 141 | else |
b27c82e1 | 142 | vfsgid = i_gid_into_vfsgid(mnt_userns, inode); |
168f9128 | 143 | |
1da177e4 | 144 | /* Also check the setgid bit! */ |
11c2a870 | 145 | if (!in_group_or_capable(mnt_userns, inode, vfsgid)) |
1da177e4 LT |
146 | attr->ia_mode &= ~S_ISGID; |
147 | } | |
148 | ||
149 | /* Check for setting the inode time. */ | |
9767d749 | 150 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { |
2f221d6f | 151 | if (!inode_owner_or_capable(mnt_userns, inode)) |
2c27c65e | 152 | return -EPERM; |
1da177e4 | 153 | } |
2c27c65e | 154 | |
030b533c JK |
155 | kill_priv: |
156 | /* User has permission for the change */ | |
157 | if (ia_valid & ATTR_KILL_PRIV) { | |
158 | int error; | |
159 | ||
71bc356f | 160 | error = security_inode_killpriv(mnt_userns, dentry); |
030b533c JK |
161 | if (error) |
162 | return error; | |
163 | } | |
164 | ||
2c27c65e | 165 | return 0; |
1da177e4 | 166 | } |
31051c85 | 167 | EXPORT_SYMBOL(setattr_prepare); |
1da177e4 | 168 | |
25d9e2d1 | 169 | /** |
170 | * inode_newsize_ok - may this inode be truncated to a given size | |
171 | * @inode: the inode to be truncated | |
172 | * @offset: the new size to assign to the inode | |
25d9e2d1 | 173 | * |
7bb46a67 | 174 | * inode_newsize_ok must be called with i_mutex held. |
175 | * | |
25d9e2d1 | 176 | * inode_newsize_ok will check filesystem limits and ulimits to check that the |
177 | * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ | |
178 | * when necessary. Caller must not proceed with inode size change if failure is | |
179 | * returned. @inode must be a file (not directory), with appropriate | |
180 | * permissions to allow truncate (inode_newsize_ok does NOT check these | |
181 | * conditions). | |
3fae1746 MW |
182 | * |
183 | * Return: 0 on success, -ve errno on failure | |
25d9e2d1 | 184 | */ |
185 | int inode_newsize_ok(const struct inode *inode, loff_t offset) | |
186 | { | |
e2ebff9c DH |
187 | if (offset < 0) |
188 | return -EINVAL; | |
25d9e2d1 | 189 | if (inode->i_size < offset) { |
190 | unsigned long limit; | |
191 | ||
d554ed89 | 192 | limit = rlimit(RLIMIT_FSIZE); |
25d9e2d1 | 193 | if (limit != RLIM_INFINITY && offset > limit) |
194 | goto out_sig; | |
195 | if (offset > inode->i_sb->s_maxbytes) | |
196 | goto out_big; | |
197 | } else { | |
198 | /* | |
199 | * truncation of in-use swapfiles is disallowed - it would | |
200 | * cause subsequent swapout to scribble on the now-freed | |
201 | * blocks. | |
202 | */ | |
203 | if (IS_SWAPFILE(inode)) | |
204 | return -ETXTBSY; | |
205 | } | |
206 | ||
207 | return 0; | |
208 | out_sig: | |
209 | send_sig(SIGXFSZ, current, 0); | |
210 | out_big: | |
211 | return -EFBIG; | |
212 | } | |
213 | EXPORT_SYMBOL(inode_newsize_ok); | |
214 | ||
7bb46a67 | 215 | /** |
6a1a90ad | 216 | * setattr_copy - copy simple metadata updates into the generic inode |
2f221d6f | 217 | * @mnt_userns: user namespace of the mount the inode was found from |
7bb46a67 | 218 | * @inode: the inode to be updated |
219 | * @attr: the new attributes | |
220 | * | |
6a1a90ad | 221 | * setattr_copy must be called with i_mutex held. |
7bb46a67 | 222 | * |
6a1a90ad | 223 | * setattr_copy updates the inode's metadata with that specified |
b27c82e1 | 224 | * in attr on idmapped mounts. Necessary permission checks to determine |
2f221d6f CB |
225 | * whether or not the S_ISGID property needs to be removed are performed with |
226 | * the correct idmapped mount permission helpers. | |
227 | * Noticeably missing is inode size update, which is more complex | |
2c27c65e | 228 | * as it requires pagecache updates. |
7bb46a67 | 229 | * |
2f221d6f CB |
230 | * If the inode has been found through an idmapped mount the user namespace of |
231 | * the vfsmount must be passed through @mnt_userns. This function will then | |
232 | * take care to map the inode according to @mnt_userns before checking | |
233 | * permissions. On non-idmapped mounts or if permission checking is to be | |
234 | * performed on the raw inode simply passs init_user_ns. | |
235 | * | |
7bb46a67 | 236 | * The inode is not marked as dirty after this operation. The rationale is |
237 | * that for "simple" filesystems, the struct inode is the inode storage. | |
238 | * The caller is free to mark the inode dirty afterwards if needed. | |
239 | */ | |
2f221d6f CB |
240 | void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode, |
241 | const struct iattr *attr) | |
1da177e4 LT |
242 | { |
243 | unsigned int ia_valid = attr->ia_valid; | |
4a30131e | 244 | |
b27c82e1 CB |
245 | i_uid_update(mnt_userns, attr, inode); |
246 | i_gid_update(mnt_userns, attr, inode); | |
eb31e2f6 AG |
247 | if (ia_valid & ATTR_ATIME) |
248 | inode->i_atime = attr->ia_atime; | |
249 | if (ia_valid & ATTR_MTIME) | |
250 | inode->i_mtime = attr->ia_mtime; | |
251 | if (ia_valid & ATTR_CTIME) | |
252 | inode->i_ctime = attr->ia_ctime; | |
1da177e4 LT |
253 | if (ia_valid & ATTR_MODE) { |
254 | umode_t mode = attr->ia_mode; | |
11c2a870 CB |
255 | if (!in_group_or_capable(mnt_userns, inode, |
256 | i_gid_into_vfsgid(mnt_userns, inode))) | |
1da177e4 LT |
257 | mode &= ~S_ISGID; |
258 | inode->i_mode = mode; | |
259 | } | |
7bb46a67 | 260 | } |
6a1a90ad | 261 | EXPORT_SYMBOL(setattr_copy); |
7bb46a67 | 262 | |
7bb698f0 AG |
263 | int may_setattr(struct user_namespace *mnt_userns, struct inode *inode, |
264 | unsigned int ia_valid) | |
265 | { | |
266 | int error; | |
267 | ||
268 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { | |
269 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
270 | return -EPERM; | |
271 | } | |
272 | ||
273 | /* | |
274 | * If utimes(2) and friends are called with times == NULL (or both | |
275 | * times are UTIME_NOW), then we need to check for write permission | |
276 | */ | |
277 | if (ia_valid & ATTR_TOUCH) { | |
278 | if (IS_IMMUTABLE(inode)) | |
279 | return -EPERM; | |
280 | ||
281 | if (!inode_owner_or_capable(mnt_userns, inode)) { | |
282 | error = inode_permission(mnt_userns, inode, MAY_WRITE); | |
283 | if (error) | |
284 | return error; | |
285 | } | |
286 | } | |
287 | return 0; | |
288 | } | |
289 | EXPORT_SYMBOL(may_setattr); | |
290 | ||
27ac0ffe BF |
291 | /** |
292 | * notify_change - modify attributes of a filesytem object | |
2f221d6f | 293 | * @mnt_userns: user namespace of the mount the inode was found from |
27ac0ffe | 294 | * @dentry: object affected |
3fae1746 | 295 | * @attr: new attributes |
27ac0ffe BF |
296 | * @delegated_inode: returns inode, if the inode is delegated |
297 | * | |
298 | * The caller must hold the i_mutex on the affected object. | |
299 | * | |
300 | * If notify_change discovers a delegation in need of breaking, | |
301 | * it will return -EWOULDBLOCK and return a reference to the inode in | |
302 | * delegated_inode. The caller should then break the delegation and | |
303 | * retry. Because breaking a delegation may take a long time, the | |
304 | * caller should drop the i_mutex before doing so. | |
305 | * | |
306 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
307 | * be appropriate for callers that expect the underlying filesystem not | |
308 | * to be NFS exported. Also, passing NULL is fine for callers holding | |
309 | * the file open for write, as there can be no conflicting delegation in | |
310 | * that case. | |
2f221d6f CB |
311 | * |
312 | * If the inode has been found through an idmapped mount the user namespace of | |
313 | * the vfsmount must be passed through @mnt_userns. This function will then | |
314 | * take care to map the inode according to @mnt_userns before checking | |
315 | * permissions. On non-idmapped mounts or if permission checking is to be | |
316 | * performed on the raw inode simply passs init_user_ns. | |
27ac0ffe | 317 | */ |
2f221d6f CB |
318 | int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry, |
319 | struct iattr *attr, struct inode **delegated_inode) | |
1da177e4 LT |
320 | { |
321 | struct inode *inode = dentry->d_inode; | |
8d334acd | 322 | umode_t mode = inode->i_mode; |
1da177e4 | 323 | int error; |
95582b00 | 324 | struct timespec64 now; |
1da177e4 LT |
325 | unsigned int ia_valid = attr->ia_valid; |
326 | ||
5955102c | 327 | WARN_ON_ONCE(!inode_is_locked(inode)); |
c4107b30 | 328 | |
7bb698f0 AG |
329 | error = may_setattr(mnt_userns, inode, ia_valid); |
330 | if (error) | |
331 | return error; | |
f2b20f6e | 332 | |
69b45732 | 333 | if ((ia_valid & ATTR_MODE)) { |
8d334acd | 334 | umode_t amode = attr->ia_mode; |
69b45732 AK |
335 | /* Flag setting protected by i_mutex */ |
336 | if (is_sxid(amode)) | |
337 | inode->i_flags &= ~S_NOSEC; | |
338 | } | |
339 | ||
c2050a45 | 340 | now = current_time(inode); |
1da177e4 LT |
341 | |
342 | attr->ia_ctime = now; | |
343 | if (!(ia_valid & ATTR_ATIME_SET)) | |
344 | attr->ia_atime = now; | |
eb31e2f6 AG |
345 | else |
346 | attr->ia_atime = timestamp_truncate(attr->ia_atime, inode); | |
1da177e4 LT |
347 | if (!(ia_valid & ATTR_MTIME_SET)) |
348 | attr->ia_mtime = now; | |
eb31e2f6 AG |
349 | else |
350 | attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); | |
351 | ||
b5376771 | 352 | if (ia_valid & ATTR_KILL_PRIV) { |
b5376771 | 353 | error = security_inode_need_killpriv(dentry); |
030b533c | 354 | if (error < 0) |
b5376771 | 355 | return error; |
030b533c JK |
356 | if (error == 0) |
357 | ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; | |
b5376771 | 358 | } |
6de0ec00 JL |
359 | |
360 | /* | |
361 | * We now pass ATTR_KILL_S*ID to the lower level setattr function so | |
362 | * that the function has the ability to reinterpret a mode change | |
363 | * that's due to these bits. This adds an implicit restriction that | |
364 | * no function will ever call notify_change with both ATTR_MODE and | |
365 | * ATTR_KILL_S*ID set. | |
366 | */ | |
367 | if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && | |
368 | (ia_valid & ATTR_MODE)) | |
369 | BUG(); | |
370 | ||
1da177e4 | 371 | if (ia_valid & ATTR_KILL_SUID) { |
1da177e4 | 372 | if (mode & S_ISUID) { |
6de0ec00 JL |
373 | ia_valid = attr->ia_valid |= ATTR_MODE; |
374 | attr->ia_mode = (inode->i_mode & ~S_ISUID); | |
1da177e4 LT |
375 | } |
376 | } | |
377 | if (ia_valid & ATTR_KILL_SGID) { | |
1da177e4 LT |
378 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
379 | if (!(ia_valid & ATTR_MODE)) { | |
380 | ia_valid = attr->ia_valid |= ATTR_MODE; | |
381 | attr->ia_mode = inode->i_mode; | |
382 | } | |
383 | attr->ia_mode &= ~S_ISGID; | |
384 | } | |
385 | } | |
6de0ec00 | 386 | if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) |
1da177e4 LT |
387 | return 0; |
388 | ||
a475acf0 SF |
389 | /* |
390 | * Verify that uid/gid changes are valid in the target | |
391 | * namespace of the superblock. | |
392 | */ | |
393 | if (ia_valid & ATTR_UID && | |
b27c82e1 CB |
394 | !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns, |
395 | attr->ia_vfsuid)) | |
a475acf0 SF |
396 | return -EOVERFLOW; |
397 | if (ia_valid & ATTR_GID && | |
b27c82e1 CB |
398 | !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns, |
399 | attr->ia_vfsgid)) | |
a475acf0 SF |
400 | return -EOVERFLOW; |
401 | ||
0bd23d09 EB |
402 | /* Don't allow modifications of files with invalid uids or |
403 | * gids unless those uids & gids are being made valid. | |
404 | */ | |
2f221d6f | 405 | if (!(ia_valid & ATTR_UID) && |
b27c82e1 | 406 | !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode))) |
0bd23d09 | 407 | return -EOVERFLOW; |
2f221d6f | 408 | if (!(ia_valid & ATTR_GID) && |
b27c82e1 | 409 | !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode))) |
0bd23d09 EB |
410 | return -EOVERFLOW; |
411 | ||
b27c82e1 | 412 | error = security_inode_setattr(mnt_userns, dentry, attr); |
27ac0ffe BF |
413 | if (error) |
414 | return error; | |
415 | error = try_break_deleg(inode, delegated_inode); | |
a77b72da MS |
416 | if (error) |
417 | return error; | |
418 | ||
eef2380c | 419 | if (inode->i_op->setattr) |
549c7297 | 420 | error = inode->i_op->setattr(mnt_userns, dentry, attr); |
eef2380c | 421 | else |
549c7297 | 422 | error = simple_setattr(mnt_userns, dentry, attr); |
1da177e4 | 423 | |
975d2943 | 424 | if (!error) { |
0eeca283 | 425 | fsnotify_change(dentry, ia_valid); |
a2d2329e | 426 | ima_inode_post_setattr(mnt_userns, dentry); |
975d2943 MZ |
427 | evm_inode_post_setattr(dentry, ia_valid); |
428 | } | |
0eeca283 | 429 | |
1da177e4 LT |
430 | return error; |
431 | } | |
1da177e4 | 432 | EXPORT_SYMBOL(notify_change); |