]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/attr.c
dpaa2-switch: Fix memory leak in dpaa2_switch_acl_entry_add() and dpaa2_switch_acl_en...
[mirror_ubuntu-jammy-kernel.git] / fs / attr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/attr.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * changes by Thomas Schoebel-Theuer
7 */
8
9 #include <linux/export.h>
10 #include <linux/time.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/sched/signal.h>
14 #include <linux/capability.h>
15 #include <linux/fsnotify.h>
16 #include <linux/fcntl.h>
17 #include <linux/security.h>
18 #include <linux/evm.h>
19 #include <linux/ima.h>
20
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
25 * @uid: uid to chown @inode to
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,
34 const struct inode *inode,
35 kuid_t uid)
36 {
37 kuid_t kuid = i_uid_into_mnt(mnt_userns, inode);
38 if (uid_eq(current_fsuid(), kuid) && uid_eq(uid, inode->i_uid))
39 return true;
40 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
41 return true;
42 if (uid_eq(kuid, INVALID_UID) &&
43 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
44 return true;
45 return false;
46 }
47
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
52 * @gid: gid to chown @inode to
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,
61 const struct inode *inode, kgid_t gid)
62 {
63 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
64 if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) {
65 kgid_t mapped_gid;
66
67 if (gid_eq(gid, inode->i_gid))
68 return true;
69 mapped_gid = mapped_kgid_fs(mnt_userns, i_user_ns(inode), gid);
70 if (in_group_p(mapped_gid))
71 return true;
72 }
73 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
74 return true;
75 if (gid_eq(kgid, INVALID_GID) &&
76 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
77 return true;
78 return false;
79 }
80
81 /**
82 * setattr_prepare - check if attribute changes to a dentry are allowed
83 * @mnt_userns: user namespace of the mount the inode was found from
84 * @dentry: dentry to check
85 * @attr: attributes to change
86 *
87 * Check if we are allowed to change the attributes contained in @attr
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.
92 *
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 *
99 * Should be called as the first thing in ->setattr implementations,
100 * possibly after taking additional locks.
101 */
102 int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
103 struct iattr *attr)
104 {
105 struct inode *inode = d_inode(dentry);
106 unsigned int ia_valid = attr->ia_valid;
107
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
118 /* If force is set do it anyway. */
119 if (ia_valid & ATTR_FORCE)
120 goto kill_priv;
121
122 /* Make sure a caller can chown. */
123 if ((ia_valid & ATTR_UID) && !chown_ok(mnt_userns, inode, attr->ia_uid))
124 return -EPERM;
125
126 /* Make sure caller can chgrp. */
127 if ((ia_valid & ATTR_GID) && !chgrp_ok(mnt_userns, inode, attr->ia_gid))
128 return -EPERM;
129
130 /* Make sure a caller can chmod. */
131 if (ia_valid & ATTR_MODE) {
132 kgid_t mapped_gid;
133
134 if (!inode_owner_or_capable(mnt_userns, inode))
135 return -EPERM;
136
137 if (ia_valid & ATTR_GID)
138 mapped_gid = mapped_kgid_fs(mnt_userns,
139 i_user_ns(inode), attr->ia_gid);
140 else
141 mapped_gid = i_gid_into_mnt(mnt_userns, inode);
142
143 /* Also check the setgid bit! */
144 if (!in_group_p(mapped_gid) &&
145 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
146 attr->ia_mode &= ~S_ISGID;
147 }
148
149 /* Check for setting the inode time. */
150 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
151 if (!inode_owner_or_capable(mnt_userns, inode))
152 return -EPERM;
153 }
154
155 kill_priv:
156 /* User has permission for the change */
157 if (ia_valid & ATTR_KILL_PRIV) {
158 int error;
159
160 error = security_inode_killpriv(mnt_userns, dentry);
161 if (error)
162 return error;
163 }
164
165 return 0;
166 }
167 EXPORT_SYMBOL(setattr_prepare);
168
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
173 *
174 * inode_newsize_ok must be called with i_mutex held.
175 *
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).
182 *
183 * Return: 0 on success, -ve errno on failure
184 */
185 int inode_newsize_ok(const struct inode *inode, loff_t offset)
186 {
187 if (offset < 0)
188 return -EINVAL;
189 if (inode->i_size < offset) {
190 unsigned long limit;
191
192 limit = rlimit(RLIMIT_FSIZE);
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
215 /**
216 * setattr_copy - copy simple metadata updates into the generic inode
217 * @mnt_userns: user namespace of the mount the inode was found from
218 * @inode: the inode to be updated
219 * @attr: the new attributes
220 *
221 * setattr_copy must be called with i_mutex held.
222 *
223 * setattr_copy updates the inode's metadata with that specified
224 * in attr on idmapped mounts. If file ownership is changed setattr_copy
225 * doesn't map ia_uid and ia_gid. It will asssume the caller has already
226 * provided the intended values. Necessary permission checks to determine
227 * whether or not the S_ISGID property needs to be removed are performed with
228 * the correct idmapped mount permission helpers.
229 * Noticeably missing is inode size update, which is more complex
230 * as it requires pagecache updates.
231 *
232 * If the inode has been found through an idmapped mount the user namespace of
233 * the vfsmount must be passed through @mnt_userns. This function will then
234 * take care to map the inode according to @mnt_userns before checking
235 * permissions. On non-idmapped mounts or if permission checking is to be
236 * performed on the raw inode simply passs init_user_ns.
237 *
238 * The inode is not marked as dirty after this operation. The rationale is
239 * that for "simple" filesystems, the struct inode is the inode storage.
240 * The caller is free to mark the inode dirty afterwards if needed.
241 */
242 void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
243 const struct iattr *attr)
244 {
245 unsigned int ia_valid = attr->ia_valid;
246
247 if (ia_valid & ATTR_UID)
248 inode->i_uid = attr->ia_uid;
249 if (ia_valid & ATTR_GID)
250 inode->i_gid = attr->ia_gid;
251 if (ia_valid & ATTR_ATIME)
252 inode->i_atime = attr->ia_atime;
253 if (ia_valid & ATTR_MTIME)
254 inode->i_mtime = attr->ia_mtime;
255 if (ia_valid & ATTR_CTIME)
256 inode->i_ctime = attr->ia_ctime;
257 if (ia_valid & ATTR_MODE) {
258 umode_t mode = attr->ia_mode;
259 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
260 if (!in_group_p(kgid) &&
261 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
262 mode &= ~S_ISGID;
263 inode->i_mode = mode;
264 }
265 }
266 EXPORT_SYMBOL(setattr_copy);
267
268 int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
269 unsigned int ia_valid)
270 {
271 int error;
272
273 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
274 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
275 return -EPERM;
276 }
277
278 /*
279 * If utimes(2) and friends are called with times == NULL (or both
280 * times are UTIME_NOW), then we need to check for write permission
281 */
282 if (ia_valid & ATTR_TOUCH) {
283 if (IS_IMMUTABLE(inode))
284 return -EPERM;
285
286 if (!inode_owner_or_capable(mnt_userns, inode)) {
287 error = inode_permission(mnt_userns, inode, MAY_WRITE);
288 if (error)
289 return error;
290 }
291 }
292 return 0;
293 }
294 EXPORT_SYMBOL(may_setattr);
295
296 /**
297 * notify_change - modify attributes of a filesytem object
298 * @mnt_userns: user namespace of the mount the inode was found from
299 * @dentry: object affected
300 * @attr: new attributes
301 * @delegated_inode: returns inode, if the inode is delegated
302 *
303 * The caller must hold the i_mutex on the affected object.
304 *
305 * If notify_change discovers a delegation in need of breaking,
306 * it will return -EWOULDBLOCK and return a reference to the inode in
307 * delegated_inode. The caller should then break the delegation and
308 * retry. Because breaking a delegation may take a long time, the
309 * caller should drop the i_mutex before doing so.
310 *
311 * If file ownership is changed notify_change() doesn't map ia_uid and
312 * ia_gid. It will asssume the caller has already provided the intended values.
313 *
314 * Alternatively, a caller may pass NULL for delegated_inode. This may
315 * be appropriate for callers that expect the underlying filesystem not
316 * to be NFS exported. Also, passing NULL is fine for callers holding
317 * the file open for write, as there can be no conflicting delegation in
318 * that case.
319 *
320 * If the inode has been found through an idmapped mount the user namespace of
321 * the vfsmount must be passed through @mnt_userns. This function will then
322 * take care to map the inode according to @mnt_userns before checking
323 * permissions. On non-idmapped mounts or if permission checking is to be
324 * performed on the raw inode simply passs init_user_ns.
325 */
326 int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
327 struct iattr *attr, struct inode **delegated_inode)
328 {
329 struct inode *inode = dentry->d_inode;
330 umode_t mode = inode->i_mode;
331 int error;
332 struct timespec64 now;
333 unsigned int ia_valid = attr->ia_valid;
334
335 WARN_ON_ONCE(!inode_is_locked(inode));
336
337 error = may_setattr(mnt_userns, inode, ia_valid);
338 if (error)
339 return error;
340
341 if ((ia_valid & ATTR_MODE)) {
342 umode_t amode = attr->ia_mode;
343 /* Flag setting protected by i_mutex */
344 if (is_sxid(amode))
345 inode->i_flags &= ~S_NOSEC;
346 }
347
348 now = current_time(inode);
349
350 attr->ia_ctime = now;
351 if (!(ia_valid & ATTR_ATIME_SET))
352 attr->ia_atime = now;
353 else
354 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
355 if (!(ia_valid & ATTR_MTIME_SET))
356 attr->ia_mtime = now;
357 else
358 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
359
360 if (ia_valid & ATTR_KILL_PRIV) {
361 error = security_inode_need_killpriv(dentry);
362 if (error < 0)
363 return error;
364 if (error == 0)
365 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
366 }
367
368 /*
369 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
370 * that the function has the ability to reinterpret a mode change
371 * that's due to these bits. This adds an implicit restriction that
372 * no function will ever call notify_change with both ATTR_MODE and
373 * ATTR_KILL_S*ID set.
374 */
375 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
376 (ia_valid & ATTR_MODE))
377 BUG();
378
379 if (ia_valid & ATTR_KILL_SUID) {
380 if (mode & S_ISUID) {
381 ia_valid = attr->ia_valid |= ATTR_MODE;
382 attr->ia_mode = (inode->i_mode & ~S_ISUID);
383 }
384 }
385 if (ia_valid & ATTR_KILL_SGID) {
386 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
387 if (!(ia_valid & ATTR_MODE)) {
388 ia_valid = attr->ia_valid |= ATTR_MODE;
389 attr->ia_mode = inode->i_mode;
390 }
391 attr->ia_mode &= ~S_ISGID;
392 }
393 }
394 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
395 return 0;
396
397 /*
398 * Verify that uid/gid changes are valid in the target
399 * namespace of the superblock.
400 */
401 if (ia_valid & ATTR_UID &&
402 !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
403 return -EOVERFLOW;
404 if (ia_valid & ATTR_GID &&
405 !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
406 return -EOVERFLOW;
407
408 /* Don't allow modifications of files with invalid uids or
409 * gids unless those uids & gids are being made valid.
410 */
411 if (!(ia_valid & ATTR_UID) &&
412 !uid_valid(i_uid_into_mnt(mnt_userns, inode)))
413 return -EOVERFLOW;
414 if (!(ia_valid & ATTR_GID) &&
415 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
416 return -EOVERFLOW;
417
418 error = security_inode_setattr(dentry, attr);
419 if (error)
420 return error;
421 error = try_break_deleg(inode, delegated_inode);
422 if (error)
423 return error;
424
425 if (inode->i_op->setattr)
426 error = inode->i_op->setattr(mnt_userns, dentry, attr);
427 else
428 error = simple_setattr(mnt_userns, dentry, attr);
429
430 if (!error) {
431 fsnotify_change(dentry, ia_valid);
432 ima_inode_post_setattr(mnt_userns, dentry);
433 evm_inode_post_setattr(dentry, ia_valid);
434 }
435
436 return error;
437 }
438 EXPORT_SYMBOL(notify_change);