Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/attr.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | * changes by Thomas Schoebel-Theuer |
| 7 | */ |
| 8 | |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/time.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/string.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 13 | #include <linux/sched/signal.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 14 | #include <linux/capability.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 15 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/security.h> |
Mimi Zohar | 975d294 | 2011-03-09 14:39:57 -0500 | [diff] [blame] | 18 | #include <linux/evm.h> |
Mimi Zohar | 9957a50 | 2011-03-09 22:57:53 -0500 | [diff] [blame] | 19 | #include <linux/ima.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 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) |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 36 | { |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 37 | kuid_t kuid = i_uid_into_mnt(mnt_userns, inode); |
| 38 | if (uid_eq(current_fsuid(), kuid) && uid_eq(uid, kuid)) |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 39 | return true; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 40 | if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN)) |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 41 | return true; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 42 | if (uid_eq(kuid, INVALID_UID) && |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 43 | ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) |
| 44 | return true; |
| 45 | return false; |
| 46 | } |
| 47 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 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) |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 62 | { |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 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 | (in_group_p(gid) || gid_eq(gid, kgid))) |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 66 | return true; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 67 | if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN)) |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 68 | return true; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 69 | if (gid_eq(kgid, INVALID_GID) && |
Eric W. Biederman | 0031181 | 2016-10-15 18:36:59 -0500 | [diff] [blame] | 70 | ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN)) |
| 71 | return true; |
| 72 | return false; |
| 73 | } |
| 74 | |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 75 | /** |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 76 | * setattr_prepare - check if attribute changes to a dentry are allowed |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 77 | * @mnt_userns: user namespace of the mount the inode was found from |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 78 | * @dentry: dentry to check |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 79 | * @attr: attributes to change |
| 80 | * |
| 81 | * Check if we are allowed to change the attributes contained in @attr |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 82 | * 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 Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 86 | * |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 87 | * 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 Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 93 | * Should be called as the first thing in ->setattr implementations, |
| 94 | * possibly after taking additional locks. |
| 95 | */ |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 96 | int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 97 | struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 99 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | unsigned int ia_valid = attr->ia_valid; |
| 101 | |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 102 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* If force is set do it anyway. */ |
| 113 | if (ia_valid & ATTR_FORCE) |
Jan Kara | 030b533 | 2016-05-26 17:21:32 +0200 | [diff] [blame] | 114 | goto kill_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* Make sure a caller can chown. */ |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 117 | if ((ia_valid & ATTR_UID) && !chown_ok(mnt_userns, inode, attr->ia_uid)) |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 118 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | /* Make sure caller can chgrp. */ |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 121 | if ((ia_valid & ATTR_GID) && !chgrp_ok(mnt_userns, inode, attr->ia_gid)) |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 122 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | /* Make sure a caller can chmod. */ |
| 125 | if (ia_valid & ATTR_MODE) { |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 126 | if (!inode_owner_or_capable(mnt_userns, inode)) |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 127 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* Also check the setgid bit! */ |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 129 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | attr->ia_mode &= ~S_ISGID; |
| 133 | } |
| 134 | |
| 135 | /* Check for setting the inode time. */ |
Miklos Szeredi | 9767d74 | 2008-07-01 15:01:26 +0200 | [diff] [blame] | 136 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) { |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 137 | if (!inode_owner_or_capable(mnt_userns, inode)) |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 138 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 140 | |
Jan Kara | 030b533 | 2016-05-26 17:21:32 +0200 | [diff] [blame] | 141 | kill_priv: |
| 142 | /* User has permission for the change */ |
| 143 | if (ia_valid & ATTR_KILL_PRIV) { |
| 144 | int error; |
| 145 | |
Christian Brauner | 71bc356 | 2021-01-21 14:19:29 +0100 | [diff] [blame] | 146 | error = security_inode_killpriv(mnt_userns, dentry); |
Jan Kara | 030b533 | 2016-05-26 17:21:32 +0200 | [diff] [blame] | 147 | if (error) |
| 148 | return error; |
| 149 | } |
| 150 | |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 151 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 153 | EXPORT_SYMBOL(setattr_prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 155 | /** |
| 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.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 159 | * |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 160 | * inode_newsize_ok must be called with i_mutex held. |
| 161 | * |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 162 | * 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 Wilcox | 3fae174 | 2018-07-03 08:08:34 -0700 | [diff] [blame] | 168 | * |
| 169 | * Return: 0 on success, -ve errno on failure |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 170 | */ |
| 171 | int inode_newsize_ok(const struct inode *inode, loff_t offset) |
| 172 | { |
| 173 | if (inode->i_size < offset) { |
| 174 | unsigned long limit; |
| 175 | |
Jiri Slaby | d554ed89 | 2010-03-05 13:42:42 -0800 | [diff] [blame] | 176 | limit = rlimit(RLIMIT_FSIZE); |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 177 | 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; |
| 192 | out_sig: |
| 193 | send_sig(SIGXFSZ, current, 0); |
| 194 | out_big: |
| 195 | return -EFBIG; |
| 196 | } |
| 197 | EXPORT_SYMBOL(inode_newsize_ok); |
| 198 | |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 199 | /** |
Christoph Hellwig | 6a1a90a | 2010-06-04 11:30:00 +0200 | [diff] [blame] | 200 | * setattr_copy - copy simple metadata updates into the generic inode |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 201 | * @mnt_userns: user namespace of the mount the inode was found from |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 202 | * @inode: the inode to be updated |
| 203 | * @attr: the new attributes |
| 204 | * |
Christoph Hellwig | 6a1a90a | 2010-06-04 11:30:00 +0200 | [diff] [blame] | 205 | * setattr_copy must be called with i_mutex held. |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 206 | * |
Christoph Hellwig | 6a1a90a | 2010-06-04 11:30:00 +0200 | [diff] [blame] | 207 | * setattr_copy updates the inode's metadata with that specified |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 208 | * 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 Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 214 | * as it requires pagecache updates. |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 215 | * |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 216 | * 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.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 222 | * 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 Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 226 | void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode, |
| 227 | const struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | unsigned int ia_valid = attr->ia_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | 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 Goldstein | eb31e2f | 2019-11-24 21:31:45 +0200 | [diff] [blame] | 235 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (ia_valid & ATTR_MODE) { |
| 242 | umode_t mode = attr->ia_mode; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 243 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | mode &= ~S_ISGID; |
| 247 | inode->i_mode = mode; |
| 248 | } |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 249 | } |
Christoph Hellwig | 6a1a90a | 2010-06-04 11:30:00 +0200 | [diff] [blame] | 250 | EXPORT_SYMBOL(setattr_copy); |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 251 | |
Andreas Gruenbacher | 7bb698f | 2021-07-28 07:47:33 -0500 | [diff] [blame] | 252 | int may_setattr(struct user_namespace *mnt_userns, struct inode *inode, |
| 253 | unsigned int ia_valid) |
| 254 | { |
| 255 | int error; |
| 256 | |
| 257 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) { |
| 258 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 259 | return -EPERM; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * If utimes(2) and friends are called with times == NULL (or both |
| 264 | * times are UTIME_NOW), then we need to check for write permission |
| 265 | */ |
| 266 | if (ia_valid & ATTR_TOUCH) { |
| 267 | if (IS_IMMUTABLE(inode)) |
| 268 | return -EPERM; |
| 269 | |
| 270 | if (!inode_owner_or_capable(mnt_userns, inode)) { |
| 271 | error = inode_permission(mnt_userns, inode, MAY_WRITE); |
| 272 | if (error) |
| 273 | return error; |
| 274 | } |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | EXPORT_SYMBOL(may_setattr); |
| 279 | |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 280 | /** |
| 281 | * notify_change - modify attributes of a filesytem object |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 282 | * @mnt_userns: user namespace of the mount the inode was found from |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 283 | * @dentry: object affected |
Matthew Wilcox | 3fae174 | 2018-07-03 08:08:34 -0700 | [diff] [blame] | 284 | * @attr: new attributes |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 285 | * @delegated_inode: returns inode, if the inode is delegated |
| 286 | * |
| 287 | * The caller must hold the i_mutex on the affected object. |
| 288 | * |
| 289 | * If notify_change discovers a delegation in need of breaking, |
| 290 | * it will return -EWOULDBLOCK and return a reference to the inode in |
| 291 | * delegated_inode. The caller should then break the delegation and |
| 292 | * retry. Because breaking a delegation may take a long time, the |
| 293 | * caller should drop the i_mutex before doing so. |
| 294 | * |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 295 | * If file ownership is changed notify_change() doesn't map ia_uid and |
| 296 | * ia_gid. It will asssume the caller has already provided the intended values. |
| 297 | * |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 298 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 299 | * be appropriate for callers that expect the underlying filesystem not |
| 300 | * to be NFS exported. Also, passing NULL is fine for callers holding |
| 301 | * the file open for write, as there can be no conflicting delegation in |
| 302 | * that case. |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 303 | * |
| 304 | * If the inode has been found through an idmapped mount the user namespace of |
| 305 | * the vfsmount must be passed through @mnt_userns. This function will then |
| 306 | * take care to map the inode according to @mnt_userns before checking |
| 307 | * permissions. On non-idmapped mounts or if permission checking is to be |
| 308 | * performed on the raw inode simply passs init_user_ns. |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 309 | */ |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 310 | int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 311 | struct iattr *attr, struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | struct inode *inode = dentry->d_inode; |
Al Viro | 8d334ac | 2011-07-24 23:21:59 -0400 | [diff] [blame] | 314 | umode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | int error; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 316 | struct timespec64 now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | unsigned int ia_valid = attr->ia_valid; |
| 318 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 319 | WARN_ON_ONCE(!inode_is_locked(inode)); |
Andrew Morton | c4107b3 | 2012-06-20 09:55:58 +1000 | [diff] [blame] | 320 | |
Andreas Gruenbacher | 7bb698f | 2021-07-28 07:47:33 -0500 | [diff] [blame] | 321 | error = may_setattr(mnt_userns, inode, ia_valid); |
| 322 | if (error) |
| 323 | return error; |
Miklos Szeredi | f2b20f6 | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 324 | |
Andi Kleen | 69b4573 | 2011-05-28 08:25:51 -0700 | [diff] [blame] | 325 | if ((ia_valid & ATTR_MODE)) { |
Al Viro | 8d334ac | 2011-07-24 23:21:59 -0400 | [diff] [blame] | 326 | umode_t amode = attr->ia_mode; |
Andi Kleen | 69b4573 | 2011-05-28 08:25:51 -0700 | [diff] [blame] | 327 | /* Flag setting protected by i_mutex */ |
| 328 | if (is_sxid(amode)) |
| 329 | inode->i_flags &= ~S_NOSEC; |
| 330 | } |
| 331 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 332 | now = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | attr->ia_ctime = now; |
| 335 | if (!(ia_valid & ATTR_ATIME_SET)) |
| 336 | attr->ia_atime = now; |
Amir Goldstein | eb31e2f | 2019-11-24 21:31:45 +0200 | [diff] [blame] | 337 | else |
| 338 | attr->ia_atime = timestamp_truncate(attr->ia_atime, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | if (!(ia_valid & ATTR_MTIME_SET)) |
| 340 | attr->ia_mtime = now; |
Amir Goldstein | eb31e2f | 2019-11-24 21:31:45 +0200 | [diff] [blame] | 341 | else |
| 342 | attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); |
| 343 | |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 344 | if (ia_valid & ATTR_KILL_PRIV) { |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 345 | error = security_inode_need_killpriv(dentry); |
Jan Kara | 030b533 | 2016-05-26 17:21:32 +0200 | [diff] [blame] | 346 | if (error < 0) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 347 | return error; |
Jan Kara | 030b533 | 2016-05-26 17:21:32 +0200 | [diff] [blame] | 348 | if (error == 0) |
| 349 | ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 350 | } |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * We now pass ATTR_KILL_S*ID to the lower level setattr function so |
| 354 | * that the function has the ability to reinterpret a mode change |
| 355 | * that's due to these bits. This adds an implicit restriction that |
| 356 | * no function will ever call notify_change with both ATTR_MODE and |
| 357 | * ATTR_KILL_S*ID set. |
| 358 | */ |
| 359 | if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) && |
| 360 | (ia_valid & ATTR_MODE)) |
| 361 | BUG(); |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (ia_valid & ATTR_KILL_SUID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (mode & S_ISUID) { |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 365 | ia_valid = attr->ia_valid |= ATTR_MODE; |
| 366 | attr->ia_mode = (inode->i_mode & ~S_ISUID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | if (ia_valid & ATTR_KILL_SGID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
| 371 | if (!(ia_valid & ATTR_MODE)) { |
| 372 | ia_valid = attr->ia_valid |= ATTR_MODE; |
| 373 | attr->ia_mode = inode->i_mode; |
| 374 | } |
| 375 | attr->ia_mode &= ~S_ISGID; |
| 376 | } |
| 377 | } |
Jeff Layton | 6de0ec0 | 2007-10-18 03:05:20 -0700 | [diff] [blame] | 378 | if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | return 0; |
| 380 | |
Seth Forshee | a475acf | 2016-04-26 14:36:25 -0500 | [diff] [blame] | 381 | /* |
| 382 | * Verify that uid/gid changes are valid in the target |
| 383 | * namespace of the superblock. |
| 384 | */ |
| 385 | if (ia_valid & ATTR_UID && |
| 386 | !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid)) |
| 387 | return -EOVERFLOW; |
| 388 | if (ia_valid & ATTR_GID && |
| 389 | !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid)) |
| 390 | return -EOVERFLOW; |
| 391 | |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 392 | /* Don't allow modifications of files with invalid uids or |
| 393 | * gids unless those uids & gids are being made valid. |
| 394 | */ |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 395 | if (!(ia_valid & ATTR_UID) && |
| 396 | !uid_valid(i_uid_into_mnt(mnt_userns, inode))) |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 397 | return -EOVERFLOW; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 398 | if (!(ia_valid & ATTR_GID) && |
| 399 | !gid_valid(i_gid_into_mnt(mnt_userns, inode))) |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 400 | return -EOVERFLOW; |
| 401 | |
Miklos Szeredi | a77b72d | 2008-07-30 14:06:22 +0200 | [diff] [blame] | 402 | error = security_inode_setattr(dentry, attr); |
| 403 | if (error) |
| 404 | return error; |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 405 | error = try_break_deleg(inode, delegated_inode); |
| 406 | if (error) |
| 407 | return error; |
Miklos Szeredi | a77b72d | 2008-07-30 14:06:22 +0200 | [diff] [blame] | 408 | |
Christoph Hellwig | eef2380 | 2010-06-04 11:30:01 +0200 | [diff] [blame] | 409 | if (inode->i_op->setattr) |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 410 | error = inode->i_op->setattr(mnt_userns, dentry, attr); |
Christoph Hellwig | eef2380 | 2010-06-04 11:30:01 +0200 | [diff] [blame] | 411 | else |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 412 | error = simple_setattr(mnt_userns, dentry, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Mimi Zohar | 975d294 | 2011-03-09 14:39:57 -0500 | [diff] [blame] | 414 | if (!error) { |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 415 | fsnotify_change(dentry, ia_valid); |
Christian Brauner | a2d2329 | 2021-01-21 14:19:45 +0100 | [diff] [blame] | 416 | ima_inode_post_setattr(mnt_userns, dentry); |
Mimi Zohar | 975d294 | 2011-03-09 14:39:57 -0500 | [diff] [blame] | 417 | evm_inode_post_setattr(dentry, ia_valid); |
| 418 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return error; |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | EXPORT_SYMBOL(notify_change); |