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 * @inode: inode to check
28 * This function determines whether the setgid bit needs to be removed.
29 * We retain backwards compatibility and require setgid bit to be removed
30 * unconditionally if S_IXGRP is set. Otherwise we have the exact same
31 * requirements as setattr_prepare() and setattr_copy().
33 * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
35 int setattr_should_drop_sgid(const struct inode *inode)
37 umode_t mode = inode->i_mode;
39 if (!(mode & S_ISGID))
42 return ATTR_KILL_SGID;
43 if (!in_group_or_capable(inode, inode->i_gid))
44 return ATTR_KILL_SGID;
49 * setattr_should_drop_suidgid - determine whether the set{g,u}id bit needs to
51 * @inode: inode to check
53 * This function determines whether the set{g,u}id bits need to be removed.
54 * If the setuid bit needs to be removed ATTR_KILL_SUID is returned. If the
55 * setgid bit needs to be removed ATTR_KILL_SGID is returned. If both
56 * set{g,u}id bits need to be removed the corresponding mask of both flags is
59 * Return: A mask of ATTR_KILL_S{G,U}ID indicating which - if any - setid bits
60 * to remove, 0 otherwise.
62 int setattr_should_drop_suidgid(struct inode *inode)
64 umode_t mode = inode->i_mode;
67 /* suid always must be killed */
68 if (unlikely(mode & S_ISUID))
69 kill = ATTR_KILL_SUID;
71 kill |= setattr_should_drop_sgid(inode);
73 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
78 EXPORT_SYMBOL(setattr_should_drop_suidgid);
80 static bool chown_ok(const struct inode *inode, kuid_t uid)
82 if (uid_eq(current_fsuid(), inode->i_uid) &&
83 uid_eq(uid, inode->i_uid))
85 if (capable_wrt_inode_uidgid(inode, CAP_CHOWN))
87 if (uid_eq(inode->i_uid, INVALID_UID) &&
88 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
93 static bool chgrp_ok(const struct inode *inode, kgid_t gid)
95 if (uid_eq(current_fsuid(), inode->i_uid) &&
96 (in_group_p(gid) || gid_eq(gid, inode->i_gid)))
98 if (capable_wrt_inode_uidgid(inode, CAP_CHOWN))
100 if (gid_eq(inode->i_gid, INVALID_GID) &&
101 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
107 * setattr_prepare - check if attribute changes to a dentry are allowed
108 * @dentry: dentry to check
109 * @attr: attributes to change
111 * Check if we are allowed to change the attributes contained in @attr
112 * in the given dentry. This includes the normal unix access permission
113 * checks, as well as checks for rlimits and others. The function also clears
114 * SGID bit from mode if user is not allowed to set it. Also file capabilities
115 * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
117 * Should be called as the first thing in ->setattr implementations,
118 * possibly after taking additional locks.
120 int setattr_prepare(struct dentry *dentry, struct iattr *attr)
122 struct inode *inode = d_inode(dentry);
123 unsigned int ia_valid = attr->ia_valid;
126 * First check size constraints. These can't be overriden using
129 if (ia_valid & ATTR_SIZE) {
130 int error = inode_newsize_ok(inode, attr->ia_size);
135 /* If force is set do it anyway. */
136 if (ia_valid & ATTR_FORCE)
139 /* Make sure a caller can chown. */
140 if ((ia_valid & ATTR_UID) && !chown_ok(inode, attr->ia_uid))
143 /* Make sure caller can chgrp. */
144 if ((ia_valid & ATTR_GID) && !chgrp_ok(inode, attr->ia_gid))
147 /* Make sure a caller can chmod. */
148 if (ia_valid & ATTR_MODE) {
149 if (!inode_owner_or_capable(inode))
151 /* Also check the setgid bit! */
152 if (!in_group_or_capable(inode, (ia_valid & ATTR_GID) ?
153 attr->ia_gid : inode->i_gid))
154 attr->ia_mode &= ~S_ISGID;
157 /* Check for setting the inode time. */
158 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
159 if (!inode_owner_or_capable(inode))
164 /* User has permission for the change */
165 if (ia_valid & ATTR_KILL_PRIV) {
168 error = security_inode_killpriv(dentry);
175 EXPORT_SYMBOL(setattr_prepare);
178 * inode_newsize_ok - may this inode be truncated to a given size
179 * @inode: the inode to be truncated
180 * @offset: the new size to assign to the inode
182 * inode_newsize_ok must be called with i_mutex held.
184 * inode_newsize_ok will check filesystem limits and ulimits to check that the
185 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
186 * when necessary. Caller must not proceed with inode size change if failure is
187 * returned. @inode must be a file (not directory), with appropriate
188 * permissions to allow truncate (inode_newsize_ok does NOT check these
191 * Return: 0 on success, -ve errno on failure
193 int inode_newsize_ok(const struct inode *inode, loff_t offset)
197 if (inode->i_size < offset) {
200 limit = rlimit(RLIMIT_FSIZE);
201 if (limit != RLIM_INFINITY && offset > limit)
203 if (offset > inode->i_sb->s_maxbytes)
207 * truncation of in-use swapfiles is disallowed - it would
208 * cause subsequent swapout to scribble on the now-freed
211 if (IS_SWAPFILE(inode))
217 send_sig(SIGXFSZ, current, 0);
221 EXPORT_SYMBOL(inode_newsize_ok);
224 * setattr_copy - copy simple metadata updates into the generic inode
225 * @inode: the inode to be updated
226 * @attr: the new attributes
228 * setattr_copy must be called with i_mutex held.
230 * setattr_copy updates the inode's metadata with that specified
231 * in attr. Noticeably missing is inode size update, which is more complex
232 * as it requires pagecache updates.
234 * The inode is not marked as dirty after this operation. The rationale is
235 * that for "simple" filesystems, the struct inode is the inode storage.
236 * The caller is free to mark the inode dirty afterwards if needed.
238 void setattr_copy(struct inode *inode, const struct iattr *attr)
240 unsigned int ia_valid = attr->ia_valid;
242 if (ia_valid & ATTR_UID)
243 inode->i_uid = attr->ia_uid;
244 if (ia_valid & ATTR_GID)
245 inode->i_gid = attr->ia_gid;
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 if (!in_group_or_capable(inode, inode->i_gid))
256 inode->i_mode = mode;
259 EXPORT_SYMBOL(setattr_copy);
262 * notify_change - modify attributes of a filesytem object
263 * @dentry: object affected
264 * @attr: new attributes
265 * @delegated_inode: returns inode, if the inode is delegated
267 * The caller must hold the i_mutex on the affected object.
269 * If notify_change discovers a delegation in need of breaking,
270 * it will return -EWOULDBLOCK and return a reference to the inode in
271 * delegated_inode. The caller should then break the delegation and
272 * retry. Because breaking a delegation may take a long time, the
273 * caller should drop the i_mutex before doing so.
275 * Alternatively, a caller may pass NULL for delegated_inode. This may
276 * be appropriate for callers that expect the underlying filesystem not
277 * to be NFS exported. Also, passing NULL is fine for callers holding
278 * the file open for write, as there can be no conflicting delegation in
281 int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
283 struct inode *inode = dentry->d_inode;
284 umode_t mode = inode->i_mode;
286 struct timespec64 now;
287 unsigned int ia_valid = attr->ia_valid;
289 WARN_ON_ONCE(!inode_is_locked(inode));
291 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
292 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
297 * If utimes(2) and friends are called with times == NULL (or both
298 * times are UTIME_NOW), then we need to check for write permission
300 if (ia_valid & ATTR_TOUCH) {
301 if (IS_IMMUTABLE(inode))
304 if (!inode_owner_or_capable(inode)) {
305 error = inode_permission(inode, MAY_WRITE);
311 if ((ia_valid & ATTR_MODE)) {
313 * Don't allow changing the mode of symlinks:
315 * (1) The vfs doesn't take the mode of symlinks into account
316 * during permission checking.
317 * (2) This has never worked correctly. Most major filesystems
318 * did return EOPNOTSUPP due to interactions with POSIX ACLs
319 * but did still updated the mode of the symlink.
320 * This inconsistency led system call wrapper providers such
321 * as libc to block changing the mode of symlinks with
322 * EOPNOTSUPP already.
323 * (3) To even do this in the first place one would have to use
324 * specific file descriptors and quite some effort.
326 if (S_ISLNK(inode->i_mode))
329 /* Flag setting protected by i_mutex */
330 if (is_sxid(attr->ia_mode))
331 inode->i_flags &= ~S_NOSEC;
334 now = current_time(inode);
336 attr->ia_ctime = now;
337 if (!(ia_valid & ATTR_ATIME_SET))
338 attr->ia_atime = now;
340 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
341 if (!(ia_valid & ATTR_MTIME_SET))
342 attr->ia_mtime = now;
344 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
346 if (ia_valid & ATTR_KILL_PRIV) {
347 error = security_inode_need_killpriv(dentry);
351 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
355 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
356 * that the function has the ability to reinterpret a mode change
357 * that's due to these bits. This adds an implicit restriction that
358 * no function will ever call notify_change with both ATTR_MODE and
359 * ATTR_KILL_S*ID set.
361 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
362 (ia_valid & ATTR_MODE))
365 if (ia_valid & ATTR_KILL_SUID) {
366 if (mode & S_ISUID) {
367 ia_valid = attr->ia_valid |= ATTR_MODE;
368 attr->ia_mode = (inode->i_mode & ~S_ISUID);
371 if (ia_valid & ATTR_KILL_SGID) {
372 if (mode & S_ISGID) {
373 if (!(ia_valid & ATTR_MODE)) {
374 ia_valid = attr->ia_valid |= ATTR_MODE;
375 attr->ia_mode = inode->i_mode;
377 attr->ia_mode &= ~S_ISGID;
380 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
384 * Verify that uid/gid changes are valid in the target
385 * namespace of the superblock.
387 if (ia_valid & ATTR_UID &&
388 !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
390 if (ia_valid & ATTR_GID &&
391 !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
394 /* Don't allow modifications of files with invalid uids or
395 * gids unless those uids & gids are being made valid.
397 if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
399 if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
402 error = security_inode_setattr(dentry, attr);
405 error = try_break_deleg(inode, delegated_inode);
409 if (inode->i_op->setattr)
410 error = inode->i_op->setattr(dentry, attr);
412 error = simple_setattr(dentry, attr);
415 fsnotify_change(dentry, ia_valid);
416 ima_inode_post_setattr(dentry);
417 evm_inode_post_setattr(dentry, ia_valid);
422 EXPORT_SYMBOL(notify_change);