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 * @mnt_userns: user namespace 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 user_namespace *mnt_userns,
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(mnt_userns, inode,
46 i_gid_into_vfsgid(mnt_userns, inode)))
47 return ATTR_KILL_SGID;
52 * The logic we want is
54 * if suid or (sgid and xgrp)
57 int should_remove_suid(struct dentry *dentry)
59 umode_t mode = d_inode(dentry)->i_mode;
62 /* suid always must be killed */
63 if (unlikely(mode & S_ISUID))
64 kill = ATTR_KILL_SUID;
67 * sgid without any exec bits is just a mandatory locking mark; leave
68 * it alone. If some exec bits are set, it's a real sgid; kill it.
70 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
71 kill |= ATTR_KILL_SGID;
73 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
78 EXPORT_SYMBOL(should_remove_suid);
81 * chown_ok - verify permissions to chown inode
82 * @mnt_userns: user namespace of the mount @inode was found from
83 * @inode: inode to check permissions on
84 * @ia_vfsuid: uid to chown @inode to
86 * If the inode has been found through an idmapped mount the user namespace of
87 * the vfsmount must be passed through @mnt_userns. This function will then
88 * take care to map the inode according to @mnt_userns before checking
89 * permissions. On non-idmapped mounts or if permission checking is to be
90 * performed on the raw inode simply passs init_user_ns.
92 static bool chown_ok(struct user_namespace *mnt_userns,
93 const struct inode *inode, vfsuid_t ia_vfsuid)
95 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
96 if (vfsuid_eq_kuid(vfsuid, current_fsuid()) &&
97 vfsuid_eq(ia_vfsuid, vfsuid))
99 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
101 if (!vfsuid_valid(vfsuid) &&
102 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
108 * chgrp_ok - verify permissions to chgrp inode
109 * @mnt_userns: user namespace of the mount @inode was found from
110 * @inode: inode to check permissions on
111 * @ia_vfsgid: gid to chown @inode to
113 * If the inode has been found through an idmapped mount the user namespace of
114 * the vfsmount must be passed through @mnt_userns. This function will then
115 * take care to map the inode according to @mnt_userns before checking
116 * permissions. On non-idmapped mounts or if permission checking is to be
117 * performed on the raw inode simply passs init_user_ns.
119 static bool chgrp_ok(struct user_namespace *mnt_userns,
120 const struct inode *inode, vfsgid_t ia_vfsgid)
122 vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
123 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
124 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) {
125 if (vfsgid_eq(ia_vfsgid, vfsgid))
127 if (vfsgid_in_group_p(ia_vfsgid))
130 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
132 if (!vfsgid_valid(vfsgid) &&
133 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
139 * setattr_prepare - check if attribute changes to a dentry are allowed
140 * @mnt_userns: user namespace of the mount the inode was found from
141 * @dentry: dentry to check
142 * @attr: attributes to change
144 * Check if we are allowed to change the attributes contained in @attr
145 * in the given dentry. This includes the normal unix access permission
146 * checks, as well as checks for rlimits and others. The function also clears
147 * SGID bit from mode if user is not allowed to set it. Also file capabilities
148 * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
150 * If the inode has been found through an idmapped mount the user namespace of
151 * the vfsmount must be passed through @mnt_userns. This function will then
152 * take care to map the inode according to @mnt_userns before checking
153 * permissions. On non-idmapped mounts or if permission checking is to be
154 * performed on the raw inode simply passs init_user_ns.
156 * Should be called as the first thing in ->setattr implementations,
157 * possibly after taking additional locks.
159 int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
162 struct inode *inode = d_inode(dentry);
163 unsigned int ia_valid = attr->ia_valid;
166 * First check size constraints. These can't be overriden using
169 if (ia_valid & ATTR_SIZE) {
170 int error = inode_newsize_ok(inode, attr->ia_size);
175 /* If force is set do it anyway. */
176 if (ia_valid & ATTR_FORCE)
179 /* Make sure a caller can chown. */
180 if ((ia_valid & ATTR_UID) &&
181 !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
184 /* Make sure caller can chgrp. */
185 if ((ia_valid & ATTR_GID) &&
186 !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
189 /* Make sure a caller can chmod. */
190 if (ia_valid & ATTR_MODE) {
193 if (!inode_owner_or_capable(mnt_userns, inode))
196 if (ia_valid & ATTR_GID)
197 vfsgid = attr->ia_vfsgid;
199 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
201 /* Also check the setgid bit! */
202 if (!in_group_or_capable(mnt_userns, inode, vfsgid))
203 attr->ia_mode &= ~S_ISGID;
206 /* Check for setting the inode time. */
207 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
208 if (!inode_owner_or_capable(mnt_userns, inode))
213 /* User has permission for the change */
214 if (ia_valid & ATTR_KILL_PRIV) {
217 error = security_inode_killpriv(mnt_userns, dentry);
224 EXPORT_SYMBOL(setattr_prepare);
227 * inode_newsize_ok - may this inode be truncated to a given size
228 * @inode: the inode to be truncated
229 * @offset: the new size to assign to the inode
231 * inode_newsize_ok must be called with i_mutex held.
233 * inode_newsize_ok will check filesystem limits and ulimits to check that the
234 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
235 * when necessary. Caller must not proceed with inode size change if failure is
236 * returned. @inode must be a file (not directory), with appropriate
237 * permissions to allow truncate (inode_newsize_ok does NOT check these
240 * Return: 0 on success, -ve errno on failure
242 int inode_newsize_ok(const struct inode *inode, loff_t offset)
246 if (inode->i_size < offset) {
249 limit = rlimit(RLIMIT_FSIZE);
250 if (limit != RLIM_INFINITY && offset > limit)
252 if (offset > inode->i_sb->s_maxbytes)
256 * truncation of in-use swapfiles is disallowed - it would
257 * cause subsequent swapout to scribble on the now-freed
260 if (IS_SWAPFILE(inode))
266 send_sig(SIGXFSZ, current, 0);
270 EXPORT_SYMBOL(inode_newsize_ok);
273 * setattr_copy - copy simple metadata updates into the generic inode
274 * @mnt_userns: user namespace of the mount the inode was found from
275 * @inode: the inode to be updated
276 * @attr: the new attributes
278 * setattr_copy must be called with i_mutex held.
280 * setattr_copy updates the inode's metadata with that specified
281 * in attr on idmapped mounts. Necessary permission checks to determine
282 * whether or not the S_ISGID property needs to be removed are performed with
283 * the correct idmapped mount permission helpers.
284 * Noticeably missing is inode size update, which is more complex
285 * as it requires pagecache updates.
287 * If the inode has been found through an idmapped mount the user namespace of
288 * the vfsmount must be passed through @mnt_userns. This function will then
289 * take care to map the inode according to @mnt_userns before checking
290 * permissions. On non-idmapped mounts or if permission checking is to be
291 * performed on the raw inode simply passs init_user_ns.
293 * The inode is not marked as dirty after this operation. The rationale is
294 * that for "simple" filesystems, the struct inode is the inode storage.
295 * The caller is free to mark the inode dirty afterwards if needed.
297 void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
298 const struct iattr *attr)
300 unsigned int ia_valid = attr->ia_valid;
302 i_uid_update(mnt_userns, attr, inode);
303 i_gid_update(mnt_userns, attr, inode);
304 if (ia_valid & ATTR_ATIME)
305 inode->i_atime = attr->ia_atime;
306 if (ia_valid & ATTR_MTIME)
307 inode->i_mtime = attr->ia_mtime;
308 if (ia_valid & ATTR_CTIME)
309 inode->i_ctime = attr->ia_ctime;
310 if (ia_valid & ATTR_MODE) {
311 umode_t mode = attr->ia_mode;
312 if (!in_group_or_capable(mnt_userns, inode,
313 i_gid_into_vfsgid(mnt_userns, inode)))
315 inode->i_mode = mode;
318 EXPORT_SYMBOL(setattr_copy);
320 int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
321 unsigned int ia_valid)
325 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
326 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
331 * If utimes(2) and friends are called with times == NULL (or both
332 * times are UTIME_NOW), then we need to check for write permission
334 if (ia_valid & ATTR_TOUCH) {
335 if (IS_IMMUTABLE(inode))
338 if (!inode_owner_or_capable(mnt_userns, inode)) {
339 error = inode_permission(mnt_userns, inode, MAY_WRITE);
346 EXPORT_SYMBOL(may_setattr);
349 * notify_change - modify attributes of a filesytem object
350 * @mnt_userns: user namespace of the mount the inode was found from
351 * @dentry: object affected
352 * @attr: new attributes
353 * @delegated_inode: returns inode, if the inode is delegated
355 * The caller must hold the i_mutex on the affected object.
357 * If notify_change discovers a delegation in need of breaking,
358 * it will return -EWOULDBLOCK and return a reference to the inode in
359 * delegated_inode. The caller should then break the delegation and
360 * retry. Because breaking a delegation may take a long time, the
361 * caller should drop the i_mutex before doing so.
363 * Alternatively, a caller may pass NULL for delegated_inode. This may
364 * be appropriate for callers that expect the underlying filesystem not
365 * to be NFS exported. Also, passing NULL is fine for callers holding
366 * the file open for write, as there can be no conflicting delegation in
369 * If the inode has been found through an idmapped mount the user namespace of
370 * the vfsmount must be passed through @mnt_userns. This function will then
371 * take care to map the inode according to @mnt_userns before checking
372 * permissions. On non-idmapped mounts or if permission checking is to be
373 * performed on the raw inode simply passs init_user_ns.
375 int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
376 struct iattr *attr, struct inode **delegated_inode)
378 struct inode *inode = dentry->d_inode;
379 umode_t mode = inode->i_mode;
381 struct timespec64 now;
382 unsigned int ia_valid = attr->ia_valid;
384 WARN_ON_ONCE(!inode_is_locked(inode));
386 error = may_setattr(mnt_userns, inode, ia_valid);
390 if ((ia_valid & ATTR_MODE)) {
391 umode_t amode = attr->ia_mode;
392 /* Flag setting protected by i_mutex */
394 inode->i_flags &= ~S_NOSEC;
397 now = current_time(inode);
399 attr->ia_ctime = now;
400 if (!(ia_valid & ATTR_ATIME_SET))
401 attr->ia_atime = now;
403 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
404 if (!(ia_valid & ATTR_MTIME_SET))
405 attr->ia_mtime = now;
407 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
409 if (ia_valid & ATTR_KILL_PRIV) {
410 error = security_inode_need_killpriv(dentry);
414 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
418 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
419 * that the function has the ability to reinterpret a mode change
420 * that's due to these bits. This adds an implicit restriction that
421 * no function will ever call notify_change with both ATTR_MODE and
422 * ATTR_KILL_S*ID set.
424 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
425 (ia_valid & ATTR_MODE))
428 if (ia_valid & ATTR_KILL_SUID) {
429 if (mode & S_ISUID) {
430 ia_valid = attr->ia_valid |= ATTR_MODE;
431 attr->ia_mode = (inode->i_mode & ~S_ISUID);
434 if (ia_valid & ATTR_KILL_SGID) {
435 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
436 if (!(ia_valid & ATTR_MODE)) {
437 ia_valid = attr->ia_valid |= ATTR_MODE;
438 attr->ia_mode = inode->i_mode;
440 attr->ia_mode &= ~S_ISGID;
443 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
447 * Verify that uid/gid changes are valid in the target
448 * namespace of the superblock.
450 if (ia_valid & ATTR_UID &&
451 !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
454 if (ia_valid & ATTR_GID &&
455 !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
459 /* Don't allow modifications of files with invalid uids or
460 * gids unless those uids & gids are being made valid.
462 if (!(ia_valid & ATTR_UID) &&
463 !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
465 if (!(ia_valid & ATTR_GID) &&
466 !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
469 error = security_inode_setattr(mnt_userns, dentry, attr);
472 error = try_break_deleg(inode, delegated_inode);
476 if (inode->i_op->setattr)
477 error = inode->i_op->setattr(mnt_userns, dentry, attr);
479 error = simple_setattr(mnt_userns, dentry, attr);
482 fsnotify_change(dentry, ia_valid);
483 ima_inode_post_setattr(mnt_userns, dentry);
484 evm_inode_post_setattr(dentry, ia_valid);
489 EXPORT_SYMBOL(notify_change);