1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * changes by Thomas Schoebel-Theuer
9 #include <linux/export.h>
10 #include <linux/time.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>
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 * @ia_vfsuid: uid to chown @inode to
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.
33 static bool chown_ok(struct user_namespace *mnt_userns,
34 const struct inode *inode, vfsuid_t ia_vfsuid)
36 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
37 if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
38 vfsuid_eq(ia_vfsuid, vfsuid))
40 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
42 if (!vfsuid_valid(vfsuid) &&
43 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
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 * @ia_vfsgid: gid to chown @inode to
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.
60 static bool chgrp_ok(struct user_namespace *mnt_userns,
61 const struct inode *inode, vfsgid_t ia_vfsgid)
63 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
64 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
65 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
66 if (vfsgid_eq(ia_vfsgid, vfsgid))
68 if (vfsgid_in_group_p(ia_vfsgid))
71 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
73 if (!vfsgid_valid(vfsgid) &&
74 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
80 * setattr_prepare - check if attribute changes to a dentry are allowed
81 * @mnt_userns: user namespace of the mount the inode was found from
82 * @dentry: dentry to check
83 * @attr: attributes to change
85 * Check if we are allowed to change the attributes contained in @attr
86 * in the given dentry. This includes the normal unix access permission
87 * checks, as well as checks for rlimits and others. The function also clears
88 * SGID bit from mode if user is not allowed to set it. Also file capabilities
89 * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
91 * If the inode has been found through an idmapped mount the user namespace of
92 * the vfsmount must be passed through @mnt_userns. This function will then
93 * take care to map the inode according to @mnt_userns before checking
94 * permissions. On non-idmapped mounts or if permission checking is to be
95 * performed on the raw inode simply passs init_user_ns.
97 * Should be called as the first thing in ->setattr implementations,
98 * possibly after taking additional locks.
100 int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
103 struct inode *inode = d_inode(dentry);
104 unsigned int ia_valid = attr->ia_valid;
107 * First check size constraints. These can't be overriden using
110 if (ia_valid & ATTR_SIZE) {
111 int error = inode_newsize_ok(inode, attr->ia_size);
116 /* If force is set do it anyway. */
117 if (ia_valid & ATTR_FORCE)
120 /* Make sure a caller can chown. */
121 if ((ia_valid & ATTR_UID) &&
122 !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
125 /* Make sure caller can chgrp. */
126 if ((ia_valid & ATTR_GID) &&
127 !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
130 /* Make sure a caller can chmod. */
131 if (ia_valid & ATTR_MODE) {
134 if (!inode_owner_or_capable(mnt_userns, inode))
137 if (ia_valid & ATTR_GID)
138 vfsgid = attr->ia_vfsgid;
140 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
142 /* Also check the setgid bit! */
143 if (!vfsgid_in_group_p(vfsgid) &&
144 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
145 attr->ia_mode &= ~S_ISGID;
148 /* Check for setting the inode time. */
149 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
150 if (!inode_owner_or_capable(mnt_userns, inode))
155 /* User has permission for the change */
156 if (ia_valid & ATTR_KILL_PRIV) {
159 error = security_inode_killpriv(mnt_userns, dentry);
166 EXPORT_SYMBOL(setattr_prepare);
169 * inode_newsize_ok - may this inode be truncated to a given size
170 * @inode: the inode to be truncated
171 * @offset: the new size to assign to the inode
173 * inode_newsize_ok must be called with i_mutex held.
175 * inode_newsize_ok will check filesystem limits and ulimits to check that the
176 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
177 * when necessary. Caller must not proceed with inode size change if failure is
178 * returned. @inode must be a file (not directory), with appropriate
179 * permissions to allow truncate (inode_newsize_ok does NOT check these
182 * Return: 0 on success, -ve errno on failure
184 int inode_newsize_ok(const struct inode *inode, loff_t offset)
188 if (inode->i_size < offset) {
191 limit = rlimit(RLIMIT_FSIZE);
192 if (limit != RLIM_INFINITY && offset > limit)
194 if (offset > inode->i_sb->s_maxbytes)
198 * truncation of in-use swapfiles is disallowed - it would
199 * cause subsequent swapout to scribble on the now-freed
202 if (IS_SWAPFILE(inode))
208 send_sig(SIGXFSZ, current, 0);
212 EXPORT_SYMBOL(inode_newsize_ok);
215 * setattr_copy - copy simple metadata updates into the generic inode
216 * @mnt_userns: user namespace of the mount the inode was found from
217 * @inode: the inode to be updated
218 * @attr: the new attributes
220 * setattr_copy must be called with i_mutex held.
222 * setattr_copy updates the inode's metadata with that specified
223 * in attr on idmapped mounts. Necessary permission checks to determine
224 * whether or not the S_ISGID property needs to be removed are performed with
225 * the correct idmapped mount permission helpers.
226 * Noticeably missing is inode size update, which is more complex
227 * as it requires pagecache updates.
229 * If the inode has been found through an idmapped mount the user namespace of
230 * the vfsmount must be passed through @mnt_userns. This function will then
231 * take care to map the inode according to @mnt_userns before checking
232 * permissions. On non-idmapped mounts or if permission checking is to be
233 * performed on the raw inode simply passs init_user_ns.
235 * The inode is not marked as dirty after this operation. The rationale is
236 * that for "simple" filesystems, the struct inode is the inode storage.
237 * The caller is free to mark the inode dirty afterwards if needed.
239 void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
240 const struct iattr *attr)
242 unsigned int ia_valid = attr->ia_valid;
244 i_uid_update(mnt_userns, attr, inode);
245 i_gid_update(mnt_userns, attr, inode);
246 if (ia_valid & ATTR_ATIME)
247 inode->i_atime = attr->ia_atime;
248 if (ia_valid & ATTR_MTIME)
249 inode->i_mtime = attr->ia_mtime;
250 if (ia_valid & ATTR_CTIME)
251 inode->i_ctime = attr->ia_ctime;
252 if (ia_valid & ATTR_MODE) {
253 umode_t mode = attr->ia_mode;
254 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
255 if (!vfsgid_in_group_p(vfsgid) &&
256 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
258 inode->i_mode = mode;
261 EXPORT_SYMBOL(setattr_copy);
263 int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
264 unsigned int ia_valid)
268 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
269 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
274 * If utimes(2) and friends are called with times == NULL (or both
275 * times are UTIME_NOW), then we need to check for write permission
277 if (ia_valid & ATTR_TOUCH) {
278 if (IS_IMMUTABLE(inode))
281 if (!inode_owner_or_capable(mnt_userns, inode)) {
282 error = inode_permission(mnt_userns, inode, MAY_WRITE);
289 EXPORT_SYMBOL(may_setattr);
292 * notify_change - modify attributes of a filesytem object
293 * @mnt_userns: user namespace of the mount the inode was found from
294 * @dentry: object affected
295 * @attr: new attributes
296 * @delegated_inode: returns inode, if the inode is delegated
298 * The caller must hold the i_mutex on the affected object.
300 * If notify_change discovers a delegation in need of breaking,
301 * it will return -EWOULDBLOCK and return a reference to the inode in
302 * delegated_inode. The caller should then break the delegation and
303 * retry. Because breaking a delegation may take a long time, the
304 * caller should drop the i_mutex before doing so.
306 * Alternatively, a caller may pass NULL for delegated_inode. This may
307 * be appropriate for callers that expect the underlying filesystem not
308 * to be NFS exported. Also, passing NULL is fine for callers holding
309 * the file open for write, as there can be no conflicting delegation in
312 * If the inode has been found through an idmapped mount the user namespace of
313 * the vfsmount must be passed through @mnt_userns. This function will then
314 * take care to map the inode according to @mnt_userns before checking
315 * permissions. On non-idmapped mounts or if permission checking is to be
316 * performed on the raw inode simply passs init_user_ns.
318 int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
319 struct iattr *attr, struct inode **delegated_inode)
321 struct inode *inode = dentry->d_inode;
322 umode_t mode = inode->i_mode;
324 struct timespec64 now;
325 unsigned int ia_valid = attr->ia_valid;
327 WARN_ON_ONCE(!inode_is_locked(inode));
329 error = may_setattr(mnt_userns, inode, ia_valid);
333 if ((ia_valid & ATTR_MODE)) {
334 umode_t amode = attr->ia_mode;
335 /* Flag setting protected by i_mutex */
337 inode->i_flags &= ~S_NOSEC;
340 now = current_time(inode);
342 attr->ia_ctime = now;
343 if (!(ia_valid & ATTR_ATIME_SET))
344 attr->ia_atime = now;
346 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
347 if (!(ia_valid & ATTR_MTIME_SET))
348 attr->ia_mtime = now;
350 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
352 if (ia_valid & ATTR_KILL_PRIV) {
353 error = security_inode_need_killpriv(dentry);
357 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
361 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
362 * that the function has the ability to reinterpret a mode change
363 * that's due to these bits. This adds an implicit restriction that
364 * no function will ever call notify_change with both ATTR_MODE and
365 * ATTR_KILL_S*ID set.
367 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
368 (ia_valid & ATTR_MODE))
371 if (ia_valid & ATTR_KILL_SUID) {
372 if (mode & S_ISUID) {
373 ia_valid = attr->ia_valid |= ATTR_MODE;
374 attr->ia_mode = (inode->i_mode & ~S_ISUID);
377 if (ia_valid & ATTR_KILL_SGID) {
378 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
379 if (!(ia_valid & ATTR_MODE)) {
380 ia_valid = attr->ia_valid |= ATTR_MODE;
381 attr->ia_mode = inode->i_mode;
383 attr->ia_mode &= ~S_ISGID;
386 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
390 * Verify that uid/gid changes are valid in the target
391 * namespace of the superblock.
393 if (ia_valid & ATTR_UID &&
394 !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
397 if (ia_valid & ATTR_GID &&
398 !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
402 /* Don't allow modifications of files with invalid uids or
403 * gids unless those uids & gids are being made valid.
405 if (!(ia_valid & ATTR_UID) &&
406 !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
408 if (!(ia_valid & ATTR_GID) &&
409 !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
412 error = security_inode_setattr(mnt_userns, dentry, attr);
415 error = try_break_deleg(inode, delegated_inode);
419 if (inode->i_op->setattr)
420 error = inode->i_op->setattr(mnt_userns, dentry, attr);
422 error = simple_setattr(mnt_userns, dentry, attr);
425 fsnotify_change(dentry, ia_valid);
426 ima_inode_post_setattr(mnt_userns, dentry);
427 evm_inode_post_setattr(dentry, ia_valid);
432 EXPORT_SYMBOL(notify_change);