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