1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/attr.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds
61da177e4SLinus Torvalds * changes by Thomas Schoebel-Theuer
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
9630d9c47SPaul Gortmaker #include <linux/export.h>
101da177e4SLinus Torvalds #include <linux/time.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/string.h>
133f07c014SIngo Molnar #include <linux/sched/signal.h>
1416f7e0feSRandy Dunlap #include <linux/capability.h>
150eeca283SRobert Love #include <linux/fsnotify.h>
161da177e4SLinus Torvalds #include <linux/fcntl.h>
175970e15dSJeff Layton #include <linux/filelock.h>
181da177e4SLinus Torvalds #include <linux/security.h>
19975d2943SMimi Zohar #include <linux/evm.h>
209957a504SMimi Zohar #include <linux/ima.h>
211da177e4SLinus Torvalds
2211c2a870SChristian Brauner #include "internal.h"
2311c2a870SChristian Brauner
2472ae017cSChristian Brauner /**
2572ae017cSChristian Brauner * setattr_should_drop_sgid - determine whether the setgid bit needs to be
2672ae017cSChristian Brauner * removed
279452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from
2872ae017cSChristian Brauner * @inode: inode to check
2972ae017cSChristian Brauner *
3072ae017cSChristian Brauner * This function determines whether the setgid bit needs to be removed.
3172ae017cSChristian Brauner * We retain backwards compatibility and require setgid bit to be removed
3272ae017cSChristian Brauner * unconditionally if S_IXGRP is set. Otherwise we have the exact same
3372ae017cSChristian Brauner * requirements as setattr_prepare() and setattr_copy().
3472ae017cSChristian Brauner *
3572ae017cSChristian Brauner * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
3672ae017cSChristian Brauner */
setattr_should_drop_sgid(struct mnt_idmap * idmap,const struct inode * inode)379452e93eSChristian Brauner int setattr_should_drop_sgid(struct mnt_idmap *idmap,
3872ae017cSChristian Brauner const struct inode *inode)
3972ae017cSChristian Brauner {
4072ae017cSChristian Brauner umode_t mode = inode->i_mode;
4172ae017cSChristian Brauner
4272ae017cSChristian Brauner if (!(mode & S_ISGID))
4372ae017cSChristian Brauner return 0;
4472ae017cSChristian Brauner if (mode & S_IXGRP)
4572ae017cSChristian Brauner return ATTR_KILL_SGID;
46e67fe633SChristian Brauner if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))
4772ae017cSChristian Brauner return ATTR_KILL_SGID;
4872ae017cSChristian Brauner return 0;
4972ae017cSChristian Brauner }
504f704d9aSChristian Brauner EXPORT_SYMBOL(setattr_should_drop_sgid);
5172ae017cSChristian Brauner
52ed5a7047SChristian Brauner /**
53ed5a7047SChristian Brauner * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to
54ed5a7047SChristian Brauner * be dropped
559452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from
56ed5a7047SChristian Brauner * @inode: inode to check
57e243e3f9SChristian Brauner *
58ed5a7047SChristian Brauner * This function determines whether the set{g,u}id bits need to be removed.
59ed5a7047SChristian Brauner * If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the
60ed5a7047SChristian Brauner * setgid bit needs to be removed ATTR_KILL_SGID is returned. If both
61ed5a7047SChristian Brauner * set{g,u}id bits need to be removed the corresponding mask of both flags is
62ed5a7047SChristian Brauner * returned.
63ed5a7047SChristian Brauner *
64ed5a7047SChristian Brauner * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits
65ed5a7047SChristian Brauner * to remove, 0 otherwise.
66e243e3f9SChristian Brauner */
setattr_should_drop_suidgid(struct mnt_idmap * idmap,struct inode * inode)679452e93eSChristian Brauner int setattr_should_drop_suidgid(struct mnt_idmap *idmap,
68ed5a7047SChristian Brauner struct inode *inode)
69e243e3f9SChristian Brauner {
70ed5a7047SChristian Brauner umode_t mode = inode->i_mode;
71e243e3f9SChristian Brauner int kill = 0;
72e243e3f9SChristian Brauner
73e243e3f9SChristian Brauner /* suid always must be killed */
74e243e3f9SChristian Brauner if (unlikely(mode & S_ISUID))
75e243e3f9SChristian Brauner kill = ATTR_KILL_SUID;
76e243e3f9SChristian Brauner
779452e93eSChristian Brauner kill |= setattr_should_drop_sgid(idmap, inode);
78e243e3f9SChristian Brauner
79e243e3f9SChristian Brauner if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
80e243e3f9SChristian Brauner return kill;
81e243e3f9SChristian Brauner
82e243e3f9SChristian Brauner return 0;
83e243e3f9SChristian Brauner }
84ed5a7047SChristian Brauner EXPORT_SYMBOL(setattr_should_drop_suidgid);
85e243e3f9SChristian Brauner
862f221d6fSChristian Brauner /**
872f221d6fSChristian Brauner * chown_ok - verify permissions to chown inode
889452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from
892f221d6fSChristian Brauner * @inode: inode to check permissions on
9081a1807dSChristian Brauner * @ia_vfsuid: uid to chown @inode to
912f221d6fSChristian Brauner *
929452e93eSChristian Brauner * If the inode has been found through an idmapped mount the idmap of
939452e93eSChristian Brauner * the vfsmount must be passed through @idmap. This function will then
949452e93eSChristian Brauner * take care to map the inode according to @idmap before checking
952f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be
969452e93eSChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap.
972f221d6fSChristian Brauner */
chown_ok(struct mnt_idmap * idmap,const struct inode * inode,vfsuid_t ia_vfsuid)989452e93eSChristian Brauner static bool chown_ok(struct mnt_idmap *idmap,
99b27c82e1SChristian Brauner const struct inode *inode, vfsuid_t ia_vfsuid)
1000031181cSEric W. Biederman {
101e67fe633SChristian Brauner vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
102b27c82e1SChristian Brauner if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
103b27c82e1SChristian Brauner vfsuid_eq(ia_vfsuid, vfsuid))
1040031181cSEric W. Biederman return true;
1059452e93eSChristian Brauner if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
1060031181cSEric W. Biederman return true;
107b27c82e1SChristian Brauner if (!vfsuid_valid(vfsuid) &&
1080031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
1090031181cSEric W. Biederman return true;
1100031181cSEric W. Biederman return false;
1110031181cSEric W. Biederman }
1120031181cSEric W. Biederman
1132f221d6fSChristian Brauner /**
1142f221d6fSChristian Brauner * chgrp_ok - verify permissions to chgrp inode
1159452e93eSChristian Brauner * @idmap: idmap of the mount @inode was found from
1162f221d6fSChristian Brauner * @inode: inode to check permissions on
11781a1807dSChristian Brauner * @ia_vfsgid: gid to chown @inode to
1182f221d6fSChristian Brauner *
1199452e93eSChristian Brauner * If the inode has been found through an idmapped mount the idmap of
1209452e93eSChristian Brauner * the vfsmount must be passed through @idmap. This function will then
1219452e93eSChristian Brauner * take care to map the inode according to @idmap before checking
1222f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be
1239452e93eSChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap.
1242f221d6fSChristian Brauner */
chgrp_ok(struct mnt_idmap * idmap,const struct inode * inode,vfsgid_t ia_vfsgid)1259452e93eSChristian Brauner static bool chgrp_ok(struct mnt_idmap *idmap,
126b27c82e1SChristian Brauner const struct inode *inode, vfsgid_t ia_vfsgid)
1270031181cSEric W. Biederman {
128e67fe633SChristian Brauner vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
129e67fe633SChristian Brauner vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
130b27c82e1SChristian Brauner if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
131b27c82e1SChristian Brauner if (vfsgid_eq(ia_vfsgid, vfsgid))
1320031181cSEric W. Biederman return true;
133b27c82e1SChristian Brauner if (vfsgid_in_group_p(ia_vfsgid))
134168f9128SChristian Brauner return true;
135168f9128SChristian Brauner }
1369452e93eSChristian Brauner if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
1370031181cSEric W. Biederman return true;
138b27c82e1SChristian Brauner if (!vfsgid_valid(vfsgid) &&
1390031181cSEric W. Biederman ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
1400031181cSEric W. Biederman return true;
1410031181cSEric W. Biederman return false;
1420031181cSEric W. Biederman }
1430031181cSEric W. Biederman
1442c27c65eSChristoph Hellwig /**
14531051c85SJan Kara * setattr_prepare - check if attribute changes to a dentry are allowed
146c1632a0fSChristian Brauner * @idmap: idmap of the mount the inode was found from
14731051c85SJan Kara * @dentry: dentry to check
1482c27c65eSChristoph Hellwig * @attr: attributes to change
1492c27c65eSChristoph Hellwig *
1502c27c65eSChristoph Hellwig * Check if we are allowed to change the attributes contained in @attr
15131051c85SJan Kara * in the given dentry. This includes the normal unix access permission
15231051c85SJan Kara * checks, as well as checks for rlimits and others. The function also clears
15331051c85SJan Kara * SGID bit from mode if user is not allowed to set it. Also file capabilities
15431051c85SJan Kara * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
1552c27c65eSChristoph Hellwig *
156c1632a0fSChristian Brauner * If the inode has been found through an idmapped mount the idmap of
157c1632a0fSChristian Brauner * the vfsmount must be passed through @idmap. This function will then
158c1632a0fSChristian Brauner * take care to map the inode according to @idmap before checking
1592f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be
160c1632a0fSChristian Brauner * performed on the raw inode simply passs @nop_mnt_idmap.
1612f221d6fSChristian Brauner *
1622c27c65eSChristoph Hellwig * Should be called as the first thing in ->setattr implementations,
1632c27c65eSChristoph Hellwig * possibly after taking additional locks.
1642c27c65eSChristoph Hellwig */
setattr_prepare(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)165c1632a0fSChristian Brauner int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,
1662f221d6fSChristian Brauner struct iattr *attr)
1671da177e4SLinus Torvalds {
16831051c85SJan Kara struct inode *inode = d_inode(dentry);
1691da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid;
1701da177e4SLinus Torvalds
1712c27c65eSChristoph Hellwig /*
1722c27c65eSChristoph Hellwig * First check size constraints. These can't be overriden using
1732c27c65eSChristoph Hellwig * ATTR_FORCE.
1742c27c65eSChristoph Hellwig */
1752c27c65eSChristoph Hellwig if (ia_valid & ATTR_SIZE) {
1762c27c65eSChristoph Hellwig int error = inode_newsize_ok(inode, attr->ia_size);
1772c27c65eSChristoph Hellwig if (error)
1782c27c65eSChristoph Hellwig return error;
1792c27c65eSChristoph Hellwig }
1802c27c65eSChristoph Hellwig
1811da177e4SLinus Torvalds /* If force is set do it anyway. */
1821da177e4SLinus Torvalds if (ia_valid & ATTR_FORCE)
183030b533cSJan Kara goto kill_priv;
1841da177e4SLinus Torvalds
1851da177e4SLinus Torvalds /* Make sure a caller can chown. */
186b27c82e1SChristian Brauner if ((ia_valid & ATTR_UID) &&
1879452e93eSChristian Brauner !chown_ok(idmap, inode, attr->ia_vfsuid))
1882c27c65eSChristoph Hellwig return -EPERM;
1891da177e4SLinus Torvalds
1901da177e4SLinus Torvalds /* Make sure caller can chgrp. */
191b27c82e1SChristian Brauner if ((ia_valid & ATTR_GID) &&
1929452e93eSChristian Brauner !chgrp_ok(idmap, inode, attr->ia_vfsgid))
1932c27c65eSChristoph Hellwig return -EPERM;
1941da177e4SLinus Torvalds
1951da177e4SLinus Torvalds /* Make sure a caller can chmod. */
1961da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) {
197b27c82e1SChristian Brauner vfsgid_t vfsgid;
198168f9128SChristian Brauner
19901beba79SChristian Brauner if (!inode_owner_or_capable(idmap, inode))
2002c27c65eSChristoph Hellwig return -EPERM;
201168f9128SChristian Brauner
202168f9128SChristian Brauner if (ia_valid & ATTR_GID)
203b27c82e1SChristian Brauner vfsgid = attr->ia_vfsgid;
204168f9128SChristian Brauner else
205e67fe633SChristian Brauner vfsgid = i_gid_into_vfsgid(idmap, inode);
206168f9128SChristian Brauner
2071da177e4SLinus Torvalds /* Also check the setgid bit! */
2089452e93eSChristian Brauner if (!in_group_or_capable(idmap, inode, vfsgid))
2091da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds
2121da177e4SLinus Torvalds /* Check for setting the inode time. */
2139767d749SMiklos Szeredi if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
21401beba79SChristian Brauner if (!inode_owner_or_capable(idmap, inode))
2152c27c65eSChristoph Hellwig return -EPERM;
2161da177e4SLinus Torvalds }
2172c27c65eSChristoph Hellwig
218030b533cSJan Kara kill_priv:
219030b533cSJan Kara /* User has permission for the change */
220030b533cSJan Kara if (ia_valid & ATTR_KILL_PRIV) {
221030b533cSJan Kara int error;
222030b533cSJan Kara
22339f60c1cSChristian Brauner error = security_inode_killpriv(idmap, dentry);
224030b533cSJan Kara if (error)
225030b533cSJan Kara return error;
226030b533cSJan Kara }
227030b533cSJan Kara
2282c27c65eSChristoph Hellwig return 0;
2291da177e4SLinus Torvalds }
23031051c85SJan Kara EXPORT_SYMBOL(setattr_prepare);
2311da177e4SLinus Torvalds
23225d9e2d1Snpiggin@suse.de /**
23325d9e2d1Snpiggin@suse.de * inode_newsize_ok - may this inode be truncated to a given size
23425d9e2d1Snpiggin@suse.de * @inode: the inode to be truncated
23525d9e2d1Snpiggin@suse.de * @offset: the new size to assign to the inode
23625d9e2d1Snpiggin@suse.de *
2377bb46a67Snpiggin@suse.de * inode_newsize_ok must be called with i_mutex held.
2387bb46a67Snpiggin@suse.de *
23925d9e2d1Snpiggin@suse.de * inode_newsize_ok will check filesystem limits and ulimits to check that the
24025d9e2d1Snpiggin@suse.de * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
24125d9e2d1Snpiggin@suse.de * when necessary. Caller must not proceed with inode size change if failure is
24225d9e2d1Snpiggin@suse.de * returned. @inode must be a file (not directory), with appropriate
24325d9e2d1Snpiggin@suse.de * permissions to allow truncate (inode_newsize_ok does NOT check these
24425d9e2d1Snpiggin@suse.de * conditions).
2453fae1746SMatthew Wilcox *
2463fae1746SMatthew Wilcox * Return: 0 on success, -ve errno on failure
24725d9e2d1Snpiggin@suse.de */
inode_newsize_ok(const struct inode * inode,loff_t offset)24825d9e2d1Snpiggin@suse.de int inode_newsize_ok(const struct inode *inode, loff_t offset)
24925d9e2d1Snpiggin@suse.de {
250e2ebff9cSDavid Howells if (offset < 0)
251e2ebff9cSDavid Howells return -EINVAL;
25225d9e2d1Snpiggin@suse.de if (inode->i_size < offset) {
25325d9e2d1Snpiggin@suse.de unsigned long limit;
25425d9e2d1Snpiggin@suse.de
255d554ed89SJiri Slaby limit = rlimit(RLIMIT_FSIZE);
25625d9e2d1Snpiggin@suse.de if (limit != RLIM_INFINITY && offset > limit)
25725d9e2d1Snpiggin@suse.de goto out_sig;
25825d9e2d1Snpiggin@suse.de if (offset > inode->i_sb->s_maxbytes)
25925d9e2d1Snpiggin@suse.de goto out_big;
26025d9e2d1Snpiggin@suse.de } else {
26125d9e2d1Snpiggin@suse.de /*
26225d9e2d1Snpiggin@suse.de * truncation of in-use swapfiles is disallowed - it would
26325d9e2d1Snpiggin@suse.de * cause subsequent swapout to scribble on the now-freed
26425d9e2d1Snpiggin@suse.de * blocks.
26525d9e2d1Snpiggin@suse.de */
26625d9e2d1Snpiggin@suse.de if (IS_SWAPFILE(inode))
26725d9e2d1Snpiggin@suse.de return -ETXTBSY;
26825d9e2d1Snpiggin@suse.de }
26925d9e2d1Snpiggin@suse.de
27025d9e2d1Snpiggin@suse.de return 0;
27125d9e2d1Snpiggin@suse.de out_sig:
27225d9e2d1Snpiggin@suse.de send_sig(SIGXFSZ, current, 0);
27325d9e2d1Snpiggin@suse.de out_big:
27425d9e2d1Snpiggin@suse.de return -EFBIG;
27525d9e2d1Snpiggin@suse.de }
27625d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(inode_newsize_ok);
27725d9e2d1Snpiggin@suse.de
2787bb46a67Snpiggin@suse.de /**
2796a1a90adSChristoph Hellwig * setattr_copy - copy simple metadata updates into the generic inode
280c1632a0fSChristian Brauner * @idmap: idmap of the mount the inode was found from
2817bb46a67Snpiggin@suse.de * @inode: the inode to be updated
2827bb46a67Snpiggin@suse.de * @attr: the new attributes
2837bb46a67Snpiggin@suse.de *
2846a1a90adSChristoph Hellwig * setattr_copy must be called with i_mutex held.
2857bb46a67Snpiggin@suse.de *
2866a1a90adSChristoph Hellwig * setattr_copy updates the inode's metadata with that specified
287b27c82e1SChristian Brauner * in attr on idmapped mounts. Necessary permission checks to determine
2882f221d6fSChristian Brauner * whether or not the S_ISGID property needs to be removed are performed with
2892f221d6fSChristian Brauner * the correct idmapped mount permission helpers.
2902f221d6fSChristian Brauner * Noticeably missing is inode size update, which is more complex
2912c27c65eSChristoph Hellwig * as it requires pagecache updates.
2927bb46a67Snpiggin@suse.de *
293c1632a0fSChristian Brauner * If the inode has been found through an idmapped mount the idmap of
294c1632a0fSChristian Brauner * the vfsmount must be passed through @idmap. This function will then
295c1632a0fSChristian Brauner * take care to map the inode according to @idmap before checking
2962f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be
297c1632a0fSChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap.
2982f221d6fSChristian Brauner *
2997bb46a67Snpiggin@suse.de * The inode is not marked as dirty after this operation. The rationale is
3007bb46a67Snpiggin@suse.de * that for "simple" filesystems, the struct inode is the inode storage.
3017bb46a67Snpiggin@suse.de * The caller is free to mark the inode dirty afterwards if needed.
3027bb46a67Snpiggin@suse.de */
setattr_copy(struct mnt_idmap * idmap,struct inode * inode,const struct iattr * attr)303c1632a0fSChristian Brauner void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,
3042f221d6fSChristian Brauner const struct iattr *attr)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid;
3071da177e4SLinus Torvalds
3080dbe12f2SChristian Brauner i_uid_update(idmap, attr, inode);
3090dbe12f2SChristian Brauner i_gid_update(idmap, attr, inode);
310eb31e2f6SAmir Goldstein if (ia_valid & ATTR_ATIME)
311eb31e2f6SAmir Goldstein inode->i_atime = attr->ia_atime;
312eb31e2f6SAmir Goldstein if (ia_valid & ATTR_MTIME)
313eb31e2f6SAmir Goldstein inode->i_mtime = attr->ia_mtime;
314eb31e2f6SAmir Goldstein if (ia_valid & ATTR_CTIME)
3152276e5baSJeff Layton inode_set_ctime_to_ts(inode, attr->ia_ctime);
3161da177e4SLinus Torvalds if (ia_valid & ATTR_MODE) {
3171da177e4SLinus Torvalds umode_t mode = attr->ia_mode;
3189452e93eSChristian Brauner if (!in_group_or_capable(idmap, inode,
319e67fe633SChristian Brauner i_gid_into_vfsgid(idmap, inode)))
3201da177e4SLinus Torvalds mode &= ~S_ISGID;
3211da177e4SLinus Torvalds inode->i_mode = mode;
3221da177e4SLinus Torvalds }
3237bb46a67Snpiggin@suse.de }
3246a1a90adSChristoph Hellwig EXPORT_SYMBOL(setattr_copy);
3257bb46a67Snpiggin@suse.de
may_setattr(struct mnt_idmap * idmap,struct inode * inode,unsigned int ia_valid)3264609e1f1SChristian Brauner int may_setattr(struct mnt_idmap *idmap, struct inode *inode,
3277bb698f0SAndreas Gruenbacher unsigned int ia_valid)
3287bb698f0SAndreas Gruenbacher {
3297bb698f0SAndreas Gruenbacher int error;
3307bb698f0SAndreas Gruenbacher
3317bb698f0SAndreas Gruenbacher if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
3327bb698f0SAndreas Gruenbacher if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
3337bb698f0SAndreas Gruenbacher return -EPERM;
3347bb698f0SAndreas Gruenbacher }
3357bb698f0SAndreas Gruenbacher
3367bb698f0SAndreas Gruenbacher /*
3377bb698f0SAndreas Gruenbacher * If utimes(2) and friends are called with times == NULL (or both
3387bb698f0SAndreas Gruenbacher * times are UTIME_NOW), then we need to check for write permission
3397bb698f0SAndreas Gruenbacher */
3407bb698f0SAndreas Gruenbacher if (ia_valid & ATTR_TOUCH) {
3417bb698f0SAndreas Gruenbacher if (IS_IMMUTABLE(inode))
3427bb698f0SAndreas Gruenbacher return -EPERM;
3437bb698f0SAndreas Gruenbacher
34401beba79SChristian Brauner if (!inode_owner_or_capable(idmap, inode)) {
3454609e1f1SChristian Brauner error = inode_permission(idmap, inode, MAY_WRITE);
3467bb698f0SAndreas Gruenbacher if (error)
3477bb698f0SAndreas Gruenbacher return error;
3487bb698f0SAndreas Gruenbacher }
3497bb698f0SAndreas Gruenbacher }
3507bb698f0SAndreas Gruenbacher return 0;
3517bb698f0SAndreas Gruenbacher }
3527bb698f0SAndreas Gruenbacher EXPORT_SYMBOL(may_setattr);
3537bb698f0SAndreas Gruenbacher
35427ac0ffeSJ. Bruce Fields /**
35527ac0ffeSJ. Bruce Fields * notify_change - modify attributes of a filesytem object
356abf08576SChristian Brauner * @idmap: idmap of the mount the inode was found from
35727ac0ffeSJ. Bruce Fields * @dentry: object affected
3583fae1746SMatthew Wilcox * @attr: new attributes
35927ac0ffeSJ. Bruce Fields * @delegated_inode: returns inode, if the inode is delegated
36027ac0ffeSJ. Bruce Fields *
36127ac0ffeSJ. Bruce Fields * The caller must hold the i_mutex on the affected object.
36227ac0ffeSJ. Bruce Fields *
36327ac0ffeSJ. Bruce Fields * If notify_change discovers a delegation in need of breaking,
36427ac0ffeSJ. Bruce Fields * it will return -EWOULDBLOCK and return a reference to the inode in
36527ac0ffeSJ. Bruce Fields * delegated_inode. The caller should then break the delegation and
36627ac0ffeSJ. Bruce Fields * retry. Because breaking a delegation may take a long time, the
36727ac0ffeSJ. Bruce Fields * caller should drop the i_mutex before doing so.
36827ac0ffeSJ. Bruce Fields *
36927ac0ffeSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may
37027ac0ffeSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not
37127ac0ffeSJ. Bruce Fields * to be NFS exported. Also, passing NULL is fine for callers holding
37227ac0ffeSJ. Bruce Fields * the file open for write, as there can be no conflicting delegation in
37327ac0ffeSJ. Bruce Fields * that case.
3742f221d6fSChristian Brauner *
375abf08576SChristian Brauner * If the inode has been found through an idmapped mount the idmap of
376abf08576SChristian Brauner * the vfsmount must be passed through @idmap. This function will then
377abf08576SChristian Brauner * take care to map the inode according to @idmap before checking
3782f221d6fSChristian Brauner * permissions. On non-idmapped mounts or if permission checking is to be
379abf08576SChristian Brauner * performed on the raw inode simply pass @nop_mnt_idmap.
38027ac0ffeSJ. Bruce Fields */
notify_change(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr,struct inode ** delegated_inode)381abf08576SChristian Brauner int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,
3822f221d6fSChristian Brauner struct iattr *attr, struct inode **delegated_inode)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds struct inode *inode = dentry->d_inode;
3858d334acdSAl Viro umode_t mode = inode->i_mode;
3861da177e4SLinus Torvalds int error;
38795582b00SDeepa Dinamani struct timespec64 now;
3881da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid;
3891da177e4SLinus Torvalds
3905955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode));
391c4107b30SAndrew Morton
3924609e1f1SChristian Brauner error = may_setattr(idmap, inode, ia_valid);
393f2b20f6eSMiklos Szeredi if (error)
394f2b20f6eSMiklos Szeredi return error;
395f2b20f6eSMiklos Szeredi
39669b45732SAndi Kleen if ((ia_valid & ATTR_MODE)) {
397*5d1f903fSChristian Brauner /*
398*5d1f903fSChristian Brauner * Don't allow changing the mode of symlinks:
399*5d1f903fSChristian Brauner *
400*5d1f903fSChristian Brauner * (1) The vfs doesn't take the mode of symlinks into account
401*5d1f903fSChristian Brauner * during permission checking.
402*5d1f903fSChristian Brauner * (2) This has never worked correctly. Most major filesystems
403*5d1f903fSChristian Brauner * did return EOPNOTSUPP due to interactions with POSIX ACLs
404*5d1f903fSChristian Brauner * but did still updated the mode of the symlink.
405*5d1f903fSChristian Brauner * This inconsistency led system call wrapper providers such
406*5d1f903fSChristian Brauner * as libc to block changing the mode of symlinks with
407*5d1f903fSChristian Brauner * EOPNOTSUPP already.
408*5d1f903fSChristian Brauner * (3) To even do this in the first place one would have to use
409*5d1f903fSChristian Brauner * specific file descriptors and quite some effort.
410*5d1f903fSChristian Brauner */
411*5d1f903fSChristian Brauner if (S_ISLNK(inode->i_mode))
412*5d1f903fSChristian Brauner return -EOPNOTSUPP;
413*5d1f903fSChristian Brauner
41469b45732SAndi Kleen /* Flag setting protected by i_mutex */
415*5d1f903fSChristian Brauner if (is_sxid(attr->ia_mode))
41669b45732SAndi Kleen inode->i_flags &= ~S_NOSEC;
41769b45732SAndi Kleen }
41869b45732SAndi Kleen
419c2050a45SDeepa Dinamani now = current_time(inode);
4201da177e4SLinus Torvalds
4211da177e4SLinus Torvalds attr->ia_ctime = now;
4221da177e4SLinus Torvalds if (!(ia_valid & ATTR_ATIME_SET))
4231da177e4SLinus Torvalds attr->ia_atime = now;
424eb31e2f6SAmir Goldstein else
425eb31e2f6SAmir Goldstein attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
4261da177e4SLinus Torvalds if (!(ia_valid & ATTR_MTIME_SET))
4271da177e4SLinus Torvalds attr->ia_mtime = now;
428eb31e2f6SAmir Goldstein else
429eb31e2f6SAmir Goldstein attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
430eb31e2f6SAmir Goldstein
431b5376771SSerge E. Hallyn if (ia_valid & ATTR_KILL_PRIV) {
432b5376771SSerge E. Hallyn error = security_inode_need_killpriv(dentry);
433030b533cSJan Kara if (error < 0)
434b5376771SSerge E. Hallyn return error;
435030b533cSJan Kara if (error == 0)
436030b533cSJan Kara ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
437b5376771SSerge E. Hallyn }
4386de0ec00SJeff Layton
4396de0ec00SJeff Layton /*
4406de0ec00SJeff Layton * We now pass ATTR_KILL_S*ID to the lower level setattr function so
4416de0ec00SJeff Layton * that the function has the ability to reinterpret a mode change
4426de0ec00SJeff Layton * that's due to these bits. This adds an implicit restriction that
4436de0ec00SJeff Layton * no function will ever call notify_change with both ATTR_MODE and
4446de0ec00SJeff Layton * ATTR_KILL_S*ID set.
4456de0ec00SJeff Layton */
4466de0ec00SJeff Layton if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
4476de0ec00SJeff Layton (ia_valid & ATTR_MODE))
4486de0ec00SJeff Layton BUG();
4496de0ec00SJeff Layton
4501da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SUID) {
4511da177e4SLinus Torvalds if (mode & S_ISUID) {
4521da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE;
4536de0ec00SJeff Layton attr->ia_mode = (inode->i_mode & ~S_ISUID);
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds if (ia_valid & ATTR_KILL_SGID) {
457ed5a7047SChristian Brauner if (mode & S_ISGID) {
4581da177e4SLinus Torvalds if (!(ia_valid & ATTR_MODE)) {
4591da177e4SLinus Torvalds ia_valid = attr->ia_valid |= ATTR_MODE;
4601da177e4SLinus Torvalds attr->ia_mode = inode->i_mode;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds attr->ia_mode &= ~S_ISGID;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds }
4656de0ec00SJeff Layton if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
4661da177e4SLinus Torvalds return 0;
4671da177e4SLinus Torvalds
468a475acf0SSeth Forshee /*
469a475acf0SSeth Forshee * Verify that uid/gid changes are valid in the target
470a475acf0SSeth Forshee * namespace of the superblock.
471a475acf0SSeth Forshee */
472a475acf0SSeth Forshee if (ia_valid & ATTR_UID &&
4734d7ca409SChristian Brauner !vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
474b27c82e1SChristian Brauner attr->ia_vfsuid))
475a475acf0SSeth Forshee return -EOVERFLOW;
476a475acf0SSeth Forshee if (ia_valid & ATTR_GID &&
4774d7ca409SChristian Brauner !vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
478b27c82e1SChristian Brauner attr->ia_vfsgid))
479a475acf0SSeth Forshee return -EOVERFLOW;
480a475acf0SSeth Forshee
4810bd23d09SEric W. Biederman /* Don't allow modifications of files with invalid uids or
4820bd23d09SEric W. Biederman * gids unless those uids & gids are being made valid.
4830bd23d09SEric W. Biederman */
4842f221d6fSChristian Brauner if (!(ia_valid & ATTR_UID) &&
485e67fe633SChristian Brauner !vfsuid_valid(i_uid_into_vfsuid(idmap, inode)))
4860bd23d09SEric W. Biederman return -EOVERFLOW;
4872f221d6fSChristian Brauner if (!(ia_valid & ATTR_GID) &&
488e67fe633SChristian Brauner !vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))
4890bd23d09SEric W. Biederman return -EOVERFLOW;
4900bd23d09SEric W. Biederman
491c1632a0fSChristian Brauner error = security_inode_setattr(idmap, dentry, attr);
492a77b72daSMiklos Szeredi if (error)
493a77b72daSMiklos Szeredi return error;
49427ac0ffeSJ. Bruce Fields error = try_break_deleg(inode, delegated_inode);
49527ac0ffeSJ. Bruce Fields if (error)
49627ac0ffeSJ. Bruce Fields return error;
497a77b72daSMiklos Szeredi
498eef2380cSChristoph Hellwig if (inode->i_op->setattr)
499c1632a0fSChristian Brauner error = inode->i_op->setattr(idmap, dentry, attr);
500eef2380cSChristoph Hellwig else
501c1632a0fSChristian Brauner error = simple_setattr(idmap, dentry, attr);
5021da177e4SLinus Torvalds
503975d2943SMimi Zohar if (!error) {
5040eeca283SRobert Love fsnotify_change(dentry, ia_valid);
50539f60c1cSChristian Brauner ima_inode_post_setattr(idmap, dentry);
506975d2943SMimi Zohar evm_inode_post_setattr(dentry, ia_valid);
507975d2943SMimi Zohar }
5080eeca283SRobert Love
5091da177e4SLinus Torvalds return error;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds EXPORT_SYMBOL(notify_change);
512