attr: add setattr_should_drop_sgid()
[linux-block.git] / fs / attr.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/attr.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * changes by Thomas Schoebel-Theuer
7 */
8
630d9c47 9#include <linux/export.h>
1da177e4
LT
10#include <linux/time.h>
11#include <linux/mm.h>
12#include <linux/string.h>
3f07c014 13#include <linux/sched/signal.h>
16f7e0fe 14#include <linux/capability.h>
0eeca283 15#include <linux/fsnotify.h>
1da177e4 16#include <linux/fcntl.h>
1da177e4 17#include <linux/security.h>
975d2943 18#include <linux/evm.h>
9957a504 19#include <linux/ima.h>
1da177e4 20
11c2a870
CB
21#include "internal.h"
22
72ae017c
CB
23/**
24 * setattr_should_drop_sgid - determine whether the setgid bit needs to be
25 * removed
26 * @mnt_userns: user namespace of the mount @inode was found from
27 * @inode: inode to check
28 *
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().
33 *
34 * Return: ATTR_KILL_SGID if setgid bit needs to be removed, 0 otherwise.
35 */
36int setattr_should_drop_sgid(struct user_namespace *mnt_userns,
37 const struct inode *inode)
38{
39 umode_t mode = inode->i_mode;
40
41 if (!(mode & S_ISGID))
42 return 0;
43 if (mode & S_IXGRP)
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;
48 return 0;
49}
50
e243e3f9
CB
51/*
52 * The logic we want is
53 *
54 * if suid or (sgid and xgrp)
55 * remove privs
56 */
57int should_remove_suid(struct dentry *dentry)
58{
59 umode_t mode = d_inode(dentry)->i_mode;
60 int kill = 0;
61
62 /* suid always must be killed */
63 if (unlikely(mode & S_ISUID))
64 kill = ATTR_KILL_SUID;
65
66 /*
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.
69 */
70 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
71 kill |= ATTR_KILL_SGID;
72
73 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
74 return kill;
75
76 return 0;
77}
78EXPORT_SYMBOL(should_remove_suid);
79
2f221d6f
CB
80/**
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
81a1807d 84 * @ia_vfsuid: uid to chown @inode to
2f221d6f
CB
85 *
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.
91 */
92static bool chown_ok(struct user_namespace *mnt_userns,
b27c82e1 93 const struct inode *inode, vfsuid_t ia_vfsuid)
0031181c 94{
b27c82e1
CB
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))
0031181c 98 return true;
2f221d6f 99 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
0031181c 100 return true;
b27c82e1 101 if (!vfsuid_valid(vfsuid) &&
0031181c
EB
102 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
103 return true;
104 return false;
105}
106
2f221d6f
CB
107/**
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
81a1807d 111 * @ia_vfsgid: gid to chown @inode to
2f221d6f
CB
112 *
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.
118 */
119static bool chgrp_ok(struct user_namespace *mnt_userns,
b27c82e1 120 const struct inode *inode, vfsgid_t ia_vfsgid)
0031181c 121{
b27c82e1
CB
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))
168f9128 126 return true;
b27c82e1 127 if (vfsgid_in_group_p(ia_vfsgid))
168f9128
CB
128 return true;
129 }
2f221d6f 130 if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
0031181c 131 return true;
b27c82e1 132 if (!vfsgid_valid(vfsgid) &&
0031181c
EB
133 ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
134 return true;
135 return false;
136}
137
2c27c65e 138/**
31051c85 139 * setattr_prepare - check if attribute changes to a dentry are allowed
2f221d6f 140 * @mnt_userns: user namespace of the mount the inode was found from
31051c85 141 * @dentry: dentry to check
2c27c65e
CH
142 * @attr: attributes to change
143 *
144 * Check if we are allowed to change the attributes contained in @attr
31051c85
JK
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.
2c27c65e 149 *
2f221d6f
CB
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.
155 *
2c27c65e
CH
156 * Should be called as the first thing in ->setattr implementations,
157 * possibly after taking additional locks.
158 */
2f221d6f
CB
159int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
160 struct iattr *attr)
1da177e4 161{
31051c85 162 struct inode *inode = d_inode(dentry);
1da177e4
LT
163 unsigned int ia_valid = attr->ia_valid;
164
2c27c65e
CH
165 /*
166 * First check size constraints. These can't be overriden using
167 * ATTR_FORCE.
168 */
169 if (ia_valid & ATTR_SIZE) {
170 int error = inode_newsize_ok(inode, attr->ia_size);
171 if (error)
172 return error;
173 }
174
1da177e4
LT
175 /* If force is set do it anyway. */
176 if (ia_valid & ATTR_FORCE)
030b533c 177 goto kill_priv;
1da177e4
LT
178
179 /* Make sure a caller can chown. */
b27c82e1
CB
180 if ((ia_valid & ATTR_UID) &&
181 !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
2c27c65e 182 return -EPERM;
1da177e4
LT
183
184 /* Make sure caller can chgrp. */
b27c82e1
CB
185 if ((ia_valid & ATTR_GID) &&
186 !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
2c27c65e 187 return -EPERM;
1da177e4
LT
188
189 /* Make sure a caller can chmod. */
190 if (ia_valid & ATTR_MODE) {
b27c82e1 191 vfsgid_t vfsgid;
168f9128 192
2f221d6f 193 if (!inode_owner_or_capable(mnt_userns, inode))
2c27c65e 194 return -EPERM;
168f9128
CB
195
196 if (ia_valid & ATTR_GID)
b27c82e1 197 vfsgid = attr->ia_vfsgid;
168f9128 198 else
b27c82e1 199 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
168f9128 200
1da177e4 201 /* Also check the setgid bit! */
11c2a870 202 if (!in_group_or_capable(mnt_userns, inode, vfsgid))
1da177e4
LT
203 attr->ia_mode &= ~S_ISGID;
204 }
205
206 /* Check for setting the inode time. */
9767d749 207 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
2f221d6f 208 if (!inode_owner_or_capable(mnt_userns, inode))
2c27c65e 209 return -EPERM;
1da177e4 210 }
2c27c65e 211
030b533c
JK
212kill_priv:
213 /* User has permission for the change */
214 if (ia_valid & ATTR_KILL_PRIV) {
215 int error;
216
71bc356f 217 error = security_inode_killpriv(mnt_userns, dentry);
030b533c
JK
218 if (error)
219 return error;
220 }
221
2c27c65e 222 return 0;
1da177e4 223}
31051c85 224EXPORT_SYMBOL(setattr_prepare);
1da177e4 225
25d9e2d1 226/**
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
25d9e2d1 230 *
7bb46a67 231 * inode_newsize_ok must be called with i_mutex held.
232 *
25d9e2d1 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
238 * conditions).
3fae1746
MW
239 *
240 * Return: 0 on success, -ve errno on failure
25d9e2d1 241 */
242int inode_newsize_ok(const struct inode *inode, loff_t offset)
243{
e2ebff9c
DH
244 if (offset < 0)
245 return -EINVAL;
25d9e2d1 246 if (inode->i_size < offset) {
247 unsigned long limit;
248
d554ed89 249 limit = rlimit(RLIMIT_FSIZE);
25d9e2d1 250 if (limit != RLIM_INFINITY && offset > limit)
251 goto out_sig;
252 if (offset > inode->i_sb->s_maxbytes)
253 goto out_big;
254 } else {
255 /*
256 * truncation of in-use swapfiles is disallowed - it would
257 * cause subsequent swapout to scribble on the now-freed
258 * blocks.
259 */
260 if (IS_SWAPFILE(inode))
261 return -ETXTBSY;
262 }
263
264 return 0;
265out_sig:
266 send_sig(SIGXFSZ, current, 0);
267out_big:
268 return -EFBIG;
269}
270EXPORT_SYMBOL(inode_newsize_ok);
271
7bb46a67 272/**
6a1a90ad 273 * setattr_copy - copy simple metadata updates into the generic inode
2f221d6f 274 * @mnt_userns: user namespace of the mount the inode was found from
7bb46a67 275 * @inode: the inode to be updated
276 * @attr: the new attributes
277 *
6a1a90ad 278 * setattr_copy must be called with i_mutex held.
7bb46a67 279 *
6a1a90ad 280 * setattr_copy updates the inode's metadata with that specified
b27c82e1 281 * in attr on idmapped mounts. Necessary permission checks to determine
2f221d6f
CB
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
2c27c65e 285 * as it requires pagecache updates.
7bb46a67 286 *
2f221d6f
CB
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.
292 *
7bb46a67 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.
296 */
2f221d6f
CB
297void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
298 const struct iattr *attr)
1da177e4
LT
299{
300 unsigned int ia_valid = attr->ia_valid;
4a30131e 301
b27c82e1
CB
302 i_uid_update(mnt_userns, attr, inode);
303 i_gid_update(mnt_userns, attr, inode);
eb31e2f6
AG
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;
1da177e4
LT
310 if (ia_valid & ATTR_MODE) {
311 umode_t mode = attr->ia_mode;
11c2a870
CB
312 if (!in_group_or_capable(mnt_userns, inode,
313 i_gid_into_vfsgid(mnt_userns, inode)))
1da177e4
LT
314 mode &= ~S_ISGID;
315 inode->i_mode = mode;
316 }
7bb46a67 317}
6a1a90ad 318EXPORT_SYMBOL(setattr_copy);
7bb46a67 319
7bb698f0
AG
320int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
321 unsigned int ia_valid)
322{
323 int error;
324
325 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
326 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
327 return -EPERM;
328 }
329
330 /*
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
333 */
334 if (ia_valid & ATTR_TOUCH) {
335 if (IS_IMMUTABLE(inode))
336 return -EPERM;
337
338 if (!inode_owner_or_capable(mnt_userns, inode)) {
339 error = inode_permission(mnt_userns, inode, MAY_WRITE);
340 if (error)
341 return error;
342 }
343 }
344 return 0;
345}
346EXPORT_SYMBOL(may_setattr);
347
27ac0ffe
BF
348/**
349 * notify_change - modify attributes of a filesytem object
2f221d6f 350 * @mnt_userns: user namespace of the mount the inode was found from
27ac0ffe 351 * @dentry: object affected
3fae1746 352 * @attr: new attributes
27ac0ffe
BF
353 * @delegated_inode: returns inode, if the inode is delegated
354 *
355 * The caller must hold the i_mutex on the affected object.
356 *
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.
362 *
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
367 * that case.
2f221d6f
CB
368 *
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.
27ac0ffe 374 */
2f221d6f
CB
375int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
376 struct iattr *attr, struct inode **delegated_inode)
1da177e4
LT
377{
378 struct inode *inode = dentry->d_inode;
8d334acd 379 umode_t mode = inode->i_mode;
1da177e4 380 int error;
95582b00 381 struct timespec64 now;
1da177e4
LT
382 unsigned int ia_valid = attr->ia_valid;
383
5955102c 384 WARN_ON_ONCE(!inode_is_locked(inode));
c4107b30 385
7bb698f0
AG
386 error = may_setattr(mnt_userns, inode, ia_valid);
387 if (error)
388 return error;
f2b20f6e 389
69b45732 390 if ((ia_valid & ATTR_MODE)) {
8d334acd 391 umode_t amode = attr->ia_mode;
69b45732
AK
392 /* Flag setting protected by i_mutex */
393 if (is_sxid(amode))
394 inode->i_flags &= ~S_NOSEC;
395 }
396
c2050a45 397 now = current_time(inode);
1da177e4
LT
398
399 attr->ia_ctime = now;
400 if (!(ia_valid & ATTR_ATIME_SET))
401 attr->ia_atime = now;
eb31e2f6
AG
402 else
403 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
1da177e4
LT
404 if (!(ia_valid & ATTR_MTIME_SET))
405 attr->ia_mtime = now;
eb31e2f6
AG
406 else
407 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
408
b5376771 409 if (ia_valid & ATTR_KILL_PRIV) {
b5376771 410 error = security_inode_need_killpriv(dentry);
030b533c 411 if (error < 0)
b5376771 412 return error;
030b533c
JK
413 if (error == 0)
414 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
b5376771 415 }
6de0ec00
JL
416
417 /*
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.
423 */
424 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
425 (ia_valid & ATTR_MODE))
426 BUG();
427
1da177e4 428 if (ia_valid & ATTR_KILL_SUID) {
1da177e4 429 if (mode & S_ISUID) {
6de0ec00
JL
430 ia_valid = attr->ia_valid |= ATTR_MODE;
431 attr->ia_mode = (inode->i_mode & ~S_ISUID);
1da177e4
LT
432 }
433 }
434 if (ia_valid & ATTR_KILL_SGID) {
1da177e4
LT
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;
439 }
440 attr->ia_mode &= ~S_ISGID;
441 }
442 }
6de0ec00 443 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
1da177e4
LT
444 return 0;
445
a475acf0
SF
446 /*
447 * Verify that uid/gid changes are valid in the target
448 * namespace of the superblock.
449 */
450 if (ia_valid & ATTR_UID &&
b27c82e1
CB
451 !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
452 attr->ia_vfsuid))
a475acf0
SF
453 return -EOVERFLOW;
454 if (ia_valid & ATTR_GID &&
b27c82e1
CB
455 !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
456 attr->ia_vfsgid))
a475acf0
SF
457 return -EOVERFLOW;
458
0bd23d09
EB
459 /* Don't allow modifications of files with invalid uids or
460 * gids unless those uids & gids are being made valid.
461 */
2f221d6f 462 if (!(ia_valid & ATTR_UID) &&
b27c82e1 463 !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
0bd23d09 464 return -EOVERFLOW;
2f221d6f 465 if (!(ia_valid & ATTR_GID) &&
b27c82e1 466 !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
0bd23d09
EB
467 return -EOVERFLOW;
468
b27c82e1 469 error = security_inode_setattr(mnt_userns, dentry, attr);
27ac0ffe
BF
470 if (error)
471 return error;
472 error = try_break_deleg(inode, delegated_inode);
a77b72da
MS
473 if (error)
474 return error;
475
eef2380c 476 if (inode->i_op->setattr)
549c7297 477 error = inode->i_op->setattr(mnt_userns, dentry, attr);
eef2380c 478 else
549c7297 479 error = simple_setattr(mnt_userns, dentry, attr);
1da177e4 480
975d2943 481 if (!error) {
0eeca283 482 fsnotify_change(dentry, ia_valid);
a2d2329e 483 ima_inode_post_setattr(mnt_userns, dentry);
975d2943
MZ
484 evm_inode_post_setattr(dentry, ia_valid);
485 }
0eeca283 486
1da177e4
LT
487 return error;
488}
1da177e4 489EXPORT_SYMBOL(notify_change);