blob: 87ef39db1c34d5a379cb61790b0f8a0a1719ad2f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/attr.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * changes by Thomas Schoebel-Theuer
7 */
8
Paul Gortmaker630d9c42011-11-16 23:57:37 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/time.h>
11#include <linux/mm.h>
12#include <linux/string.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010013#include <linux/sched/signal.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080014#include <linux/capability.h>
Robert Love0eeca282005-07-12 17:06:03 -040015#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/security.h>
Mimi Zohar975d2942011-03-09 14:39:57 -050018#include <linux/evm.h>
Mimi Zohar9957a502011-03-09 22:57:53 -050019#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Christian Brauner2f221d62021-01-21 14:19:26 +010021/**
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 */
33static bool chown_ok(struct user_namespace *mnt_userns,
34 const struct inode *inode,
35 kuid_t uid)
Eric W. Biederman00311812016-10-15 18:36:59 -050036{
Christian Brauner2f221d62021-01-21 14:19:26 +010037 kuid_t kuid = i_uid_into_mnt(mnt_userns, inode);
38 if (uid_eq(current_fsuid(), kuid) && uid_eq(uid, kuid))
Eric W. Biederman00311812016-10-15 18:36:59 -050039 return true;
Christian Brauner2f221d62021-01-21 14:19:26 +010040 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
Eric W. Biederman00311812016-10-15 18:36:59 -050041 return true;
Christian Brauner2f221d62021-01-21 14:19:26 +010042 if (uid_eq(kuid, INVALID_UID) &&
Eric W. Biederman00311812016-10-15 18:36:59 -050043 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
44 return true;
45 return false;
46}
47
Christian Brauner2f221d62021-01-21 14:19:26 +010048/**
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 */
60static bool chgrp_ok(struct user_namespace *mnt_userns,
61 const struct inode *inode, kgid_t gid)
Eric W. Biederman00311812016-10-15 18:36:59 -050062{
Christian Brauner2f221d62021-01-21 14:19:26 +010063 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
64 if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) &&
65 (in_group_p(gid) || gid_eq(gid, kgid)))
Eric W. Biederman00311812016-10-15 18:36:59 -050066 return true;
Christian Brauner2f221d62021-01-21 14:19:26 +010067 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
Eric W. Biederman00311812016-10-15 18:36:59 -050068 return true;
Christian Brauner2f221d62021-01-21 14:19:26 +010069 if (gid_eq(kgid, INVALID_GID) &&
Eric W. Biederman00311812016-10-15 18:36:59 -050070 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
71 return true;
72 return false;
73}
74
Christoph Hellwig2c27c652010-06-04 11:30:04 +020075/**
Jan Kara31051c82016-05-26 16:55:18 +020076 * setattr_prepare - check if attribute changes to a dentry are allowed
Christian Brauner2f221d62021-01-21 14:19:26 +010077 * @mnt_userns: user namespace of the mount the inode was found from
Jan Kara31051c82016-05-26 16:55:18 +020078 * @dentry: dentry to check
Christoph Hellwig2c27c652010-06-04 11:30:04 +020079 * @attr: attributes to change
80 *
81 * Check if we are allowed to change the attributes contained in @attr
Jan Kara31051c82016-05-26 16:55:18 +020082 * in the given dentry. This includes the normal unix access permission
83 * checks, as well as checks for rlimits and others. The function also clears
84 * SGID bit from mode if user is not allowed to set it. Also file capabilities
85 * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
Christoph Hellwig2c27c652010-06-04 11:30:04 +020086 *
Christian Brauner2f221d62021-01-21 14:19:26 +010087 * If the inode has been found through an idmapped mount the user namespace of
88 * the vfsmount must be passed through @mnt_userns. This function will then
89 * take care to map the inode according to @mnt_userns before checking
90 * permissions. On non-idmapped mounts or if permission checking is to be
91 * performed on the raw inode simply passs init_user_ns.
92 *
Christoph Hellwig2c27c652010-06-04 11:30:04 +020093 * Should be called as the first thing in ->setattr implementations,
94 * possibly after taking additional locks.
95 */
Christian Brauner2f221d62021-01-21 14:19:26 +010096int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
97 struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Jan Kara31051c82016-05-26 16:55:18 +020099 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned int ia_valid = attr->ia_valid;
101
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200102 /*
103 * First check size constraints. These can't be overriden using
104 * ATTR_FORCE.
105 */
106 if (ia_valid & ATTR_SIZE) {
107 int error = inode_newsize_ok(inode, attr->ia_size);
108 if (error)
109 return error;
110 }
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* If force is set do it anyway. */
113 if (ia_valid & ATTR_FORCE)
Jan Kara030b5332016-05-26 17:21:32 +0200114 goto kill_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /* Make sure a caller can chown. */
Christian Brauner2f221d62021-01-21 14:19:26 +0100117 if ((ia_valid & ATTR_UID) && !chown_ok(mnt_userns, inode, attr->ia_uid))
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200118 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* Make sure caller can chgrp. */
Christian Brauner2f221d62021-01-21 14:19:26 +0100121 if ((ia_valid & ATTR_GID) && !chgrp_ok(mnt_userns, inode, attr->ia_gid))
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200122 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* Make sure a caller can chmod. */
125 if (ia_valid & ATTR_MODE) {
Christian Brauner2f221d62021-01-21 14:19:26 +0100126 if (!inode_owner_or_capable(mnt_userns, inode))
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200127 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* Also check the setgid bit! */
Christian Brauner2f221d62021-01-21 14:19:26 +0100129 if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
130 i_gid_into_mnt(mnt_userns, inode)) &&
131 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 attr->ia_mode &= ~S_ISGID;
133 }
134
135 /* Check for setting the inode time. */
Miklos Szeredi9767d742008-07-01 15:01:26 +0200136 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
Christian Brauner2f221d62021-01-21 14:19:26 +0100137 if (!inode_owner_or_capable(mnt_userns, inode))
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200138 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200140
Jan Kara030b5332016-05-26 17:21:32 +0200141kill_priv:
142 /* User has permission for the change */
143 if (ia_valid & ATTR_KILL_PRIV) {
144 int error;
145
Christian Brauner71bc3562021-01-21 14:19:29 +0100146 error = security_inode_killpriv(mnt_userns, dentry);
Jan Kara030b5332016-05-26 17:21:32 +0200147 if (error)
148 return error;
149 }
150
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
Jan Kara31051c82016-05-26 16:55:18 +0200153EXPORT_SYMBOL(setattr_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000155/**
156 * inode_newsize_ok - may this inode be truncated to a given size
157 * @inode: the inode to be truncated
158 * @offset: the new size to assign to the inode
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000159 *
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000160 * inode_newsize_ok must be called with i_mutex held.
161 *
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000162 * inode_newsize_ok will check filesystem limits and ulimits to check that the
163 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
164 * when necessary. Caller must not proceed with inode size change if failure is
165 * returned. @inode must be a file (not directory), with appropriate
166 * permissions to allow truncate (inode_newsize_ok does NOT check these
167 * conditions).
Matthew Wilcox3fae1742018-07-03 08:08:34 -0700168 *
169 * Return: 0 on success, -ve errno on failure
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000170 */
171int inode_newsize_ok(const struct inode *inode, loff_t offset)
172{
173 if (inode->i_size < offset) {
174 unsigned long limit;
175
Jiri Slabyd554ed892010-03-05 13:42:42 -0800176 limit = rlimit(RLIMIT_FSIZE);
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000177 if (limit != RLIM_INFINITY && offset > limit)
178 goto out_sig;
179 if (offset > inode->i_sb->s_maxbytes)
180 goto out_big;
181 } else {
182 /*
183 * truncation of in-use swapfiles is disallowed - it would
184 * cause subsequent swapout to scribble on the now-freed
185 * blocks.
186 */
187 if (IS_SWAPFILE(inode))
188 return -ETXTBSY;
189 }
190
191 return 0;
192out_sig:
193 send_sig(SIGXFSZ, current, 0);
194out_big:
195 return -EFBIG;
196}
197EXPORT_SYMBOL(inode_newsize_ok);
198
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000199/**
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200200 * setattr_copy - copy simple metadata updates into the generic inode
Christian Brauner2f221d62021-01-21 14:19:26 +0100201 * @mnt_userns: user namespace of the mount the inode was found from
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000202 * @inode: the inode to be updated
203 * @attr: the new attributes
204 *
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200205 * setattr_copy must be called with i_mutex held.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000206 *
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200207 * setattr_copy updates the inode's metadata with that specified
Christian Brauner2f221d62021-01-21 14:19:26 +0100208 * in attr on idmapped mounts. If file ownership is changed setattr_copy
209 * doesn't map ia_uid and ia_gid. It will asssume the caller has already
210 * provided the intended values. Necessary permission checks to determine
211 * whether or not the S_ISGID property needs to be removed are performed with
212 * the correct idmapped mount permission helpers.
213 * Noticeably missing is inode size update, which is more complex
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200214 * as it requires pagecache updates.
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000215 *
Christian Brauner2f221d62021-01-21 14:19:26 +0100216 * If the inode has been found through an idmapped mount the user namespace of
217 * the vfsmount must be passed through @mnt_userns. This function will then
218 * take care to map the inode according to @mnt_userns before checking
219 * permissions. On non-idmapped mounts or if permission checking is to be
220 * performed on the raw inode simply passs init_user_ns.
221 *
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000222 * The inode is not marked as dirty after this operation. The rationale is
223 * that for "simple" filesystems, the struct inode is the inode storage.
224 * The caller is free to mark the inode dirty afterwards if needed.
225 */
Christian Brauner2f221d62021-01-21 14:19:26 +0100226void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
227 const struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 unsigned int ia_valid = attr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (ia_valid & ATTR_UID)
232 inode->i_uid = attr->ia_uid;
233 if (ia_valid & ATTR_GID)
234 inode->i_gid = attr->ia_gid;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200235 if (ia_valid & ATTR_ATIME)
236 inode->i_atime = attr->ia_atime;
237 if (ia_valid & ATTR_MTIME)
238 inode->i_mtime = attr->ia_mtime;
239 if (ia_valid & ATTR_CTIME)
240 inode->i_ctime = attr->ia_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (ia_valid & ATTR_MODE) {
242 umode_t mode = attr->ia_mode;
Christian Brauner2f221d62021-01-21 14:19:26 +0100243 kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
244 if (!in_group_p(kgid) &&
245 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 mode &= ~S_ISGID;
247 inode->i_mode = mode;
248 }
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000249}
Christoph Hellwig6a1a90a2010-06-04 11:30:00 +0200250EXPORT_SYMBOL(setattr_copy);
npiggin@suse.de7bb46a62010-05-27 01:05:33 +1000251
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400252/**
253 * notify_change - modify attributes of a filesytem object
Christian Brauner2f221d62021-01-21 14:19:26 +0100254 * @mnt_userns: user namespace of the mount the inode was found from
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400255 * @dentry: object affected
Matthew Wilcox3fae1742018-07-03 08:08:34 -0700256 * @attr: new attributes
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400257 * @delegated_inode: returns inode, if the inode is delegated
258 *
259 * The caller must hold the i_mutex on the affected object.
260 *
261 * If notify_change discovers a delegation in need of breaking,
262 * it will return -EWOULDBLOCK and return a reference to the inode in
263 * delegated_inode. The caller should then break the delegation and
264 * retry. Because breaking a delegation may take a long time, the
265 * caller should drop the i_mutex before doing so.
266 *
Christian Brauner2f221d62021-01-21 14:19:26 +0100267 * If file ownership is changed notify_change() doesn't map ia_uid and
268 * ia_gid. It will asssume the caller has already provided the intended values.
269 *
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400270 * Alternatively, a caller may pass NULL for delegated_inode. This may
271 * be appropriate for callers that expect the underlying filesystem not
272 * to be NFS exported. Also, passing NULL is fine for callers holding
273 * the file open for write, as there can be no conflicting delegation in
274 * that case.
Christian Brauner2f221d62021-01-21 14:19:26 +0100275 *
276 * If the inode has been found through an idmapped mount the user namespace of
277 * the vfsmount must be passed through @mnt_userns. This function will then
278 * take care to map the inode according to @mnt_userns before checking
279 * permissions. On non-idmapped mounts or if permission checking is to be
280 * performed on the raw inode simply passs init_user_ns.
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400281 */
Christian Brauner2f221d62021-01-21 14:19:26 +0100282int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
283 struct iattr *attr, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct inode *inode = dentry->d_inode;
Al Viro8d334ac2011-07-24 23:21:59 -0400286 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int error;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700288 struct timespec64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned int ia_valid = attr->ia_valid;
290
Al Viro59551022016-01-22 15:40:57 -0500291 WARN_ON_ONCE(!inode_is_locked(inode));
Andrew Mortonc4107b32012-06-20 09:55:58 +1000292
Miklos Szeredibeb29e02008-07-01 15:01:29 +0200293 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
294 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
295 return -EPERM;
296 }
297
Miklos Szeredif2b20f62016-09-16 12:44:20 +0200298 /*
299 * If utimes(2) and friends are called with times == NULL (or both
300 * times are UTIME_NOW), then we need to check for write permission
301 */
302 if (ia_valid & ATTR_TOUCH) {
303 if (IS_IMMUTABLE(inode))
304 return -EPERM;
305
Christian Brauner2f221d62021-01-21 14:19:26 +0100306 if (!inode_owner_or_capable(mnt_userns, inode)) {
307 error = inode_permission(mnt_userns, inode, MAY_WRITE);
Miklos Szeredif2b20f62016-09-16 12:44:20 +0200308 if (error)
309 return error;
310 }
311 }
312
Andi Kleen69b45732011-05-28 08:25:51 -0700313 if ((ia_valid & ATTR_MODE)) {
Al Viro8d334ac2011-07-24 23:21:59 -0400314 umode_t amode = attr->ia_mode;
Andi Kleen69b45732011-05-28 08:25:51 -0700315 /* Flag setting protected by i_mutex */
316 if (is_sxid(amode))
317 inode->i_flags &= ~S_NOSEC;
318 }
319
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700320 now = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 attr->ia_ctime = now;
323 if (!(ia_valid & ATTR_ATIME_SET))
324 attr->ia_atime = now;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200325 else
326 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!(ia_valid & ATTR_MTIME_SET))
328 attr->ia_mtime = now;
Amir Goldsteineb31e2f2019-11-24 21:31:45 +0200329 else
330 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
331
Serge E. Hallynb5376772007-10-16 23:31:36 -0700332 if (ia_valid & ATTR_KILL_PRIV) {
Serge E. Hallynb5376772007-10-16 23:31:36 -0700333 error = security_inode_need_killpriv(dentry);
Jan Kara030b5332016-05-26 17:21:32 +0200334 if (error < 0)
Serge E. Hallynb5376772007-10-16 23:31:36 -0700335 return error;
Jan Kara030b5332016-05-26 17:21:32 +0200336 if (error == 0)
337 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700338 }
Jeff Layton6de0ec02007-10-18 03:05:20 -0700339
340 /*
341 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
342 * that the function has the ability to reinterpret a mode change
343 * that's due to these bits. This adds an implicit restriction that
344 * no function will ever call notify_change with both ATTR_MODE and
345 * ATTR_KILL_S*ID set.
346 */
347 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
348 (ia_valid & ATTR_MODE))
349 BUG();
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (ia_valid & ATTR_KILL_SUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (mode & S_ISUID) {
Jeff Layton6de0ec02007-10-18 03:05:20 -0700353 ia_valid = attr->ia_valid |= ATTR_MODE;
354 attr->ia_mode = (inode->i_mode & ~S_ISUID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 }
357 if (ia_valid & ATTR_KILL_SGID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
359 if (!(ia_valid & ATTR_MODE)) {
360 ia_valid = attr->ia_valid |= ATTR_MODE;
361 attr->ia_mode = inode->i_mode;
362 }
363 attr->ia_mode &= ~S_ISGID;
364 }
365 }
Jeff Layton6de0ec02007-10-18 03:05:20 -0700366 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368
Seth Forsheea475acf2016-04-26 14:36:25 -0500369 /*
370 * Verify that uid/gid changes are valid in the target
371 * namespace of the superblock.
372 */
373 if (ia_valid & ATTR_UID &&
374 !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
375 return -EOVERFLOW;
376 if (ia_valid & ATTR_GID &&
377 !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
378 return -EOVERFLOW;
379
Eric W. Biederman0bd23d092016-06-29 14:54:46 -0500380 /* Don't allow modifications of files with invalid uids or
381 * gids unless those uids & gids are being made valid.
382 */
Christian Brauner2f221d62021-01-21 14:19:26 +0100383 if (!(ia_valid & ATTR_UID) &&
384 !uid_valid(i_uid_into_mnt(mnt_userns, inode)))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -0500385 return -EOVERFLOW;
Christian Brauner2f221d62021-01-21 14:19:26 +0100386 if (!(ia_valid & ATTR_GID) &&
387 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -0500388 return -EOVERFLOW;
389
Miklos Szeredia77b72d2008-07-30 14:06:22 +0200390 error = security_inode_setattr(dentry, attr);
391 if (error)
392 return error;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400393 error = try_break_deleg(inode, delegated_inode);
394 if (error)
395 return error;
Miklos Szeredia77b72d2008-07-30 14:06:22 +0200396
Christoph Hellwigeef23802010-06-04 11:30:01 +0200397 if (inode->i_op->setattr)
Christian Brauner549c7292021-01-21 14:19:43 +0100398 error = inode->i_op->setattr(mnt_userns, dentry, attr);
Christoph Hellwigeef23802010-06-04 11:30:01 +0200399 else
Christian Brauner549c7292021-01-21 14:19:43 +0100400 error = simple_setattr(mnt_userns, dentry, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Mimi Zohar975d2942011-03-09 14:39:57 -0500402 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -0400403 fsnotify_change(dentry, ia_valid);
Christian Braunera2d23292021-01-21 14:19:45 +0100404 ima_inode_post_setattr(mnt_userns, dentry);
Mimi Zohar975d2942011-03-09 14:39:57 -0500405 evm_inode_post_setattr(dentry, ia_valid);
406 }
Robert Love0eeca282005-07-12 17:06:03 -0400407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return error;
409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410EXPORT_SYMBOL(notify_change);