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>
24 * setattr_should_drop_sgid - determine whether the setgid bit needs to be
26 * @idmap: idmap of the mount @inode was found from
27 * @inode: inode to check
29 * This function determines whether the setgid bit needs to be removed.
30 * We retain backwards compatibility and require setgid bit to be removed
31 * unconditionally if S_IXGRP is set. Otherwise we have the exact same
32 * requirements as setattr_prepare() and setattr_copy().
34 * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
36 int setattr_should_drop_sgid(struct mnt_idmap *idmap,
37 const struct inode *inode)
39 umode_t mode = inode->i_mode;
41 if (!(mode & S_ISGID))
44 return ATTR_KILL_SGID;
45 if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))
46 return ATTR_KILL_SGID;
51 * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to
53 * @idmap: idmap of the mount @inode was found from
54 * @inode: inode to check
56 * This function determines whether the set{g,u}id bits need to be removed.
57 * If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the
58 * setgid bit needs to be removed ATTR_KILL_SGID is returned. If both
59 * set{g,u}id bits need to be removed the corresponding mask of both flags is
62 * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits
63 * to remove, 0 otherwise.
65 int setattr_should_drop_suidgid(struct mnt_idmap *idmap,
68 umode_t mode = inode->i_mode;
71 /* suid always must be killed */
72 if (unlikely(mode & S_ISUID))
73 kill = ATTR_KILL_SUID;
75 kill |= setattr_should_drop_sgid(idmap, inode);
77 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
82 EXPORT_SYMBOL(setattr_should_drop_suidgid);
85 * chown_ok - verify permissions to chown inode
86 * @idmap: idmap of the mount @inode was found from
87 * @inode: inode to check permissions on
88 * @ia_vfsuid: uid to chown @inode to
90 * If the inode has been found through an idmapped mount the idmap of
91 * the vfsmount must be passed through @idmap. This function will then
92 * take care to map the inode according to @idmap before checking
93 * permissions. On non-idmapped mounts or if permission checking is to be
94 * performed on the raw inode simply pass @nop_mnt_idmap.
96 static bool chown_ok(struct mnt_idmap *idmap,
97 const struct inode *inode, vfsuid_t ia_vfsuid)
99 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
100 if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
101 vfsuid_eq(ia_vfsuid, vfsuid))
103 if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
105 if (!vfsuid_valid(vfsuid) &&
106 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
112 * chgrp_ok - verify permissions to chgrp inode
113 * @idmap: idmap of the mount @inode was found from
114 * @inode: inode to check permissions on
115 * @ia_vfsgid: gid to chown @inode to
117 * If the inode has been found through an idmapped mount the idmap of
118 * the vfsmount must be passed through @idmap. This function will then
119 * take care to map the inode according to @idmap before checking
120 * permissions. On non-idmapped mounts or if permission checking is to be
121 * performed on the raw inode simply pass @nop_mnt_idmap.
123 static bool chgrp_ok(struct mnt_idmap *idmap,
124 const struct inode *inode, vfsgid_t ia_vfsgid)
126 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
127 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
128 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
129 if (vfsgid_eq(ia_vfsgid, vfsgid))
131 if (vfsgid_in_group_p(ia_vfsgid))
134 if (capable_wrt_inode_uidgid(idmap, inode, CAP_CHOWN))
136 if (!vfsgid_valid(vfsgid) &&
137 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
143 * setattr_prepare - check if attribute changes to a dentry are allowed
144 * @idmap: idmap of the mount the inode was found from
145 * @dentry: dentry to check
146 * @attr: attributes to change
148 * Check if we are allowed to change the attributes contained in @attr
149 * in the given dentry. This includes the normal unix access permission
150 * checks, as well as checks for rlimits and others. The function also clears
151 * SGID bit from mode if user is not allowed to set it. Also file capabilities
152 * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
154 * If the inode has been found through an idmapped mount the idmap of
155 * the vfsmount must be passed through @idmap. This function will then
156 * take care to map the inode according to @idmap before checking
157 * permissions. On non-idmapped mounts or if permission checking is to be
158 * performed on the raw inode simply passs @nop_mnt_idmap.
160 * Should be called as the first thing in ->setattr implementations,
161 * possibly after taking additional locks.
163 int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,
166 struct inode *inode = d_inode(dentry);
167 unsigned int ia_valid = attr->ia_valid;
170 * First check size constraints. These can't be overriden using
173 if (ia_valid & ATTR_SIZE) {
174 int error = inode_newsize_ok(inode, attr->ia_size);
179 /* If force is set do it anyway. */
180 if (ia_valid & ATTR_FORCE)
183 /* Make sure a caller can chown. */
184 if ((ia_valid & ATTR_UID) &&
185 !chown_ok(idmap, inode, attr->ia_vfsuid))
188 /* Make sure caller can chgrp. */
189 if ((ia_valid & ATTR_GID) &&
190 !chgrp_ok(idmap, inode, attr->ia_vfsgid))
193 /* Make sure a caller can chmod. */
194 if (ia_valid & ATTR_MODE) {
197 if (!inode_owner_or_capable(idmap, inode))
200 if (ia_valid & ATTR_GID)
201 vfsgid = attr->ia_vfsgid;
203 vfsgid = i_gid_into_vfsgid(idmap, inode);
205 /* Also check the setgid bit! */
206 if (!in_group_or_capable(idmap, inode, vfsgid))
207 attr->ia_mode &= ~S_ISGID;
210 /* Check for setting the inode time. */
211 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
212 if (!inode_owner_or_capable(idmap, inode))
217 /* User has permission for the change */
218 if (ia_valid & ATTR_KILL_PRIV) {
221 error = security_inode_killpriv(idmap, dentry);
228 EXPORT_SYMBOL(setattr_prepare);
231 * inode_newsize_ok - may this inode be truncated to a given size
232 * @inode: the inode to be truncated
233 * @offset: the new size to assign to the inode
235 * inode_newsize_ok must be called with i_mutex held.
237 * inode_newsize_ok will check filesystem limits and ulimits to check that the
238 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
239 * when necessary. Caller must not proceed with inode size change if failure is
240 * returned. @inode must be a file (not directory), with appropriate
241 * permissions to allow truncate (inode_newsize_ok does NOT check these
244 * Return: 0 on success, -ve errno on failure
246 int inode_newsize_ok(const struct inode *inode, loff_t offset)
250 if (inode->i_size < offset) {
253 limit = rlimit(RLIMIT_FSIZE);
254 if (limit != RLIM_INFINITY && offset > limit)
256 if (offset > inode->i_sb->s_maxbytes)
260 * truncation of in-use swapfiles is disallowed - it would
261 * cause subsequent swapout to scribble on the now-freed
264 if (IS_SWAPFILE(inode))
270 send_sig(SIGXFSZ, current, 0);
274 EXPORT_SYMBOL(inode_newsize_ok);
277 * setattr_copy - copy simple metadata updates into the generic inode
278 * @idmap: idmap of the mount the inode was found from
279 * @inode: the inode to be updated
280 * @attr: the new attributes
282 * setattr_copy must be called with i_mutex held.
284 * setattr_copy updates the inode's metadata with that specified
285 * in attr on idmapped mounts. Necessary permission checks to determine
286 * whether or not the S_ISGID property needs to be removed are performed with
287 * the correct idmapped mount permission helpers.
288 * Noticeably missing is inode size update, which is more complex
289 * as it requires pagecache updates.
291 * If the inode has been found through an idmapped mount the idmap of
292 * the vfsmount must be passed through @idmap. This function will then
293 * take care to map the inode according to @idmap before checking
294 * permissions. On non-idmapped mounts or if permission checking is to be
295 * performed on the raw inode simply pass @nop_mnt_idmap.
297 * The inode is not marked as dirty after this operation. The rationale is
298 * that for "simple" filesystems, the struct inode is the inode storage.
299 * The caller is free to mark the inode dirty afterwards if needed.
301 void setattr_copy(struct mnt_idmap *idmap, struct inode *inode,
302 const struct iattr *attr)
304 unsigned int ia_valid = attr->ia_valid;
306 i_uid_update(idmap, attr, inode);
307 i_gid_update(idmap, attr, inode);
308 if (ia_valid & ATTR_ATIME)
309 inode->i_atime = attr->ia_atime;
310 if (ia_valid & ATTR_MTIME)
311 inode->i_mtime = attr->ia_mtime;
312 if (ia_valid & ATTR_CTIME)
313 inode->i_ctime = attr->ia_ctime;
314 if (ia_valid & ATTR_MODE) {
315 umode_t mode = attr->ia_mode;
316 if (!in_group_or_capable(idmap, inode,
317 i_gid_into_vfsgid(idmap, inode)))
319 inode->i_mode = mode;
322 EXPORT_SYMBOL(setattr_copy);
324 int may_setattr(struct mnt_idmap *idmap, struct inode *inode,
325 unsigned int ia_valid)
329 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
330 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
335 * If utimes(2) and friends are called with times == NULL (or both
336 * times are UTIME_NOW), then we need to check for write permission
338 if (ia_valid & ATTR_TOUCH) {
339 if (IS_IMMUTABLE(inode))
342 if (!inode_owner_or_capable(idmap, inode)) {
343 error = inode_permission(idmap, inode, MAY_WRITE);
350 EXPORT_SYMBOL(may_setattr);
353 * notify_change - modify attributes of a filesytem object
354 * @idmap: idmap of the mount the inode was found from
355 * @dentry: object affected
356 * @attr: new attributes
357 * @delegated_inode: returns inode, if the inode is delegated
359 * The caller must hold the i_mutex on the affected object.
361 * If notify_change discovers a delegation in need of breaking,
362 * it will return -EWOULDBLOCK and return a reference to the inode in
363 * delegated_inode. The caller should then break the delegation and
364 * retry. Because breaking a delegation may take a long time, the
365 * caller should drop the i_mutex before doing so.
367 * Alternatively, a caller may pass NULL for delegated_inode. This may
368 * be appropriate for callers that expect the underlying filesystem not
369 * to be NFS exported. Also, passing NULL is fine for callers holding
370 * the file open for write, as there can be no conflicting delegation in
373 * If the inode has been found through an idmapped mount the idmap of
374 * the vfsmount must be passed through @idmap. This function will then
375 * take care to map the inode according to @idmap before checking
376 * permissions. On non-idmapped mounts or if permission checking is to be
377 * performed on the raw inode simply pass @nop_mnt_idmap.
379 int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,
380 struct iattr *attr, struct inode **delegated_inode)
382 struct inode *inode = dentry->d_inode;
383 umode_t mode = inode->i_mode;
385 struct timespec64 now;
386 unsigned int ia_valid = attr->ia_valid;
388 WARN_ON_ONCE(!inode_is_locked(inode));
390 error = may_setattr(idmap, inode, ia_valid);
394 if ((ia_valid & ATTR_MODE)) {
395 umode_t amode = attr->ia_mode;
396 /* Flag setting protected by i_mutex */
398 inode->i_flags &= ~S_NOSEC;
401 now = current_time(inode);
403 attr->ia_ctime = now;
404 if (!(ia_valid & ATTR_ATIME_SET))
405 attr->ia_atime = now;
407 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
408 if (!(ia_valid & ATTR_MTIME_SET))
409 attr->ia_mtime = now;
411 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
413 if (ia_valid & ATTR_KILL_PRIV) {
414 error = security_inode_need_killpriv(dentry);
418 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
422 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
423 * that the function has the ability to reinterpret a mode change
424 * that's due to these bits. This adds an implicit restriction that
425 * no function will ever call notify_change with both ATTR_MODE and
426 * ATTR_KILL_S*ID set.
428 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
429 (ia_valid & ATTR_MODE))
432 if (ia_valid & ATTR_KILL_SUID) {
433 if (mode & S_ISUID) {
434 ia_valid = attr->ia_valid |= ATTR_MODE;
435 attr->ia_mode = (inode->i_mode & ~S_ISUID);
438 if (ia_valid & ATTR_KILL_SGID) {
439 if (mode & S_ISGID) {
440 if (!(ia_valid & ATTR_MODE)) {
441 ia_valid = attr->ia_valid |= ATTR_MODE;
442 attr->ia_mode = inode->i_mode;
444 attr->ia_mode &= ~S_ISGID;
447 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
451 * Verify that uid/gid changes are valid in the target
452 * namespace of the superblock.
454 if (ia_valid & ATTR_UID &&
455 !vfsuid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
458 if (ia_valid & ATTR_GID &&
459 !vfsgid_has_fsmapping(idmap, inode->i_sb->s_user_ns,
463 /* Don't allow modifications of files with invalid uids or
464 * gids unless those uids & gids are being made valid.
466 if (!(ia_valid & ATTR_UID) &&
467 !vfsuid_valid(i_uid_into_vfsuid(idmap, inode)))
469 if (!(ia_valid & ATTR_GID) &&
470 !vfsgid_valid(i_gid_into_vfsgid(idmap, inode)))
473 error = security_inode_setattr(idmap, dentry, attr);
476 error = try_break_deleg(inode, delegated_inode);
480 if (inode->i_op->setattr)
481 error = inode->i_op->setattr(idmap, dentry, attr);
483 error = simple_setattr(idmap, dentry, attr);
486 fsnotify_change(dentry, ia_valid);
487 ima_inode_post_setattr(idmap, dentry);
488 evm_inode_post_setattr(dentry, ia_valid);
493 EXPORT_SYMBOL(notify_change);