08532253612781b5c1f33124add37121db614157
[platform/kernel/linux-starfive.git] / fs / attr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/attr.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  *  changes by Thomas Schoebel-Theuer
7  */
8
9 #include <linux/export.h>
10 #include <linux/time.h>
11 #include <linux/mm.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>
20
21 #include "internal.h"
22
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  */
36 int 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
51 /*
52  * The logic we want is
53  *
54  *      if suid or (sgid and xgrp)
55  *              remove privs
56  */
57 int 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 }
78 EXPORT_SYMBOL(should_remove_suid);
79
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
84  * @ia_vfsuid:  uid to chown @inode to
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  */
92 static bool chown_ok(struct user_namespace *mnt_userns,
93                      const struct inode *inode, vfsuid_t ia_vfsuid)
94 {
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))
98                 return true;
99         if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
100                 return true;
101         if (!vfsuid_valid(vfsuid) &&
102             ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
103                 return true;
104         return false;
105 }
106
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
111  * @ia_vfsgid:  gid to chown @inode to
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  */
119 static bool chgrp_ok(struct user_namespace *mnt_userns,
120                      const struct inode *inode, vfsgid_t ia_vfsgid)
121 {
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))
126                         return true;
127                 if (vfsgid_in_group_p(ia_vfsgid))
128                         return true;
129         }
130         if (capable_wrt_inode_uidgid(mnt_userns, inode, CAP_CHOWN))
131                 return true;
132         if (!vfsgid_valid(vfsgid) &&
133             ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
134                 return true;
135         return false;
136 }
137
138 /**
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
143  *
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.
149  *
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  *
156  * Should be called as the first thing in ->setattr implementations,
157  * possibly after taking additional locks.
158  */
159 int setattr_prepare(struct user_namespace *mnt_userns, struct dentry *dentry,
160                     struct iattr *attr)
161 {
162         struct inode *inode = d_inode(dentry);
163         unsigned int ia_valid = attr->ia_valid;
164
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
175         /* If force is set do it anyway. */
176         if (ia_valid & ATTR_FORCE)
177                 goto kill_priv;
178
179         /* Make sure a caller can chown. */
180         if ((ia_valid & ATTR_UID) &&
181             !chown_ok(mnt_userns, inode, attr->ia_vfsuid))
182                 return -EPERM;
183
184         /* Make sure caller can chgrp. */
185         if ((ia_valid & ATTR_GID) &&
186             !chgrp_ok(mnt_userns, inode, attr->ia_vfsgid))
187                 return -EPERM;
188
189         /* Make sure a caller can chmod. */
190         if (ia_valid & ATTR_MODE) {
191                 vfsgid_t vfsgid;
192
193                 if (!inode_owner_or_capable(mnt_userns, inode))
194                         return -EPERM;
195
196                 if (ia_valid & ATTR_GID)
197                         vfsgid = attr->ia_vfsgid;
198                 else
199                         vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
200
201                 /* Also check the setgid bit! */
202                 if (!in_group_or_capable(mnt_userns, inode, vfsgid))
203                         attr->ia_mode &= ~S_ISGID;
204         }
205
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))
209                         return -EPERM;
210         }
211
212 kill_priv:
213         /* User has permission for the change */
214         if (ia_valid & ATTR_KILL_PRIV) {
215                 int error;
216
217                 error = security_inode_killpriv(mnt_userns, dentry);
218                 if (error)
219                         return error;
220         }
221
222         return 0;
223 }
224 EXPORT_SYMBOL(setattr_prepare);
225
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
230  *
231  * inode_newsize_ok must be called with i_mutex held.
232  *
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).
239  *
240  * Return: 0 on success, -ve errno on failure
241  */
242 int inode_newsize_ok(const struct inode *inode, loff_t offset)
243 {
244         if (offset < 0)
245                 return -EINVAL;
246         if (inode->i_size < offset) {
247                 unsigned long limit;
248
249                 limit = rlimit(RLIMIT_FSIZE);
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;
265 out_sig:
266         send_sig(SIGXFSZ, current, 0);
267 out_big:
268         return -EFBIG;
269 }
270 EXPORT_SYMBOL(inode_newsize_ok);
271
272 /**
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
277  *
278  * setattr_copy must be called with i_mutex held.
279  *
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.
286  *
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  *
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  */
297 void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
298                   const struct iattr *attr)
299 {
300         unsigned int ia_valid = attr->ia_valid;
301
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)))
314                         mode &= ~S_ISGID;
315                 inode->i_mode = mode;
316         }
317 }
318 EXPORT_SYMBOL(setattr_copy);
319
320 int 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 }
346 EXPORT_SYMBOL(may_setattr);
347
348 /**
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
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.
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.
374  */
375 int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
376                   struct iattr *attr, struct inode **delegated_inode)
377 {
378         struct inode *inode = dentry->d_inode;
379         umode_t mode = inode->i_mode;
380         int error;
381         struct timespec64 now;
382         unsigned int ia_valid = attr->ia_valid;
383
384         WARN_ON_ONCE(!inode_is_locked(inode));
385
386         error = may_setattr(mnt_userns, inode, ia_valid);
387         if (error)
388                 return error;
389
390         if ((ia_valid & ATTR_MODE)) {
391                 umode_t amode = attr->ia_mode;
392                 /* Flag setting protected by i_mutex */
393                 if (is_sxid(amode))
394                         inode->i_flags &= ~S_NOSEC;
395         }
396
397         now = current_time(inode);
398
399         attr->ia_ctime = now;
400         if (!(ia_valid & ATTR_ATIME_SET))
401                 attr->ia_atime = now;
402         else
403                 attr->ia_atime = timestamp_truncate(attr->ia_atime, inode);
404         if (!(ia_valid & ATTR_MTIME_SET))
405                 attr->ia_mtime = now;
406         else
407                 attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode);
408
409         if (ia_valid & ATTR_KILL_PRIV) {
410                 error = security_inode_need_killpriv(dentry);
411                 if (error < 0)
412                         return error;
413                 if (error == 0)
414                         ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
415         }
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
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);
432                 }
433         }
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;
439                         }
440                         attr->ia_mode &= ~S_ISGID;
441                 }
442         }
443         if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
444                 return 0;
445
446         /*
447          * Verify that uid/gid changes are valid in the target
448          * namespace of the superblock.
449          */
450         if (ia_valid & ATTR_UID &&
451             !vfsuid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
452                                   attr->ia_vfsuid))
453                 return -EOVERFLOW;
454         if (ia_valid & ATTR_GID &&
455             !vfsgid_has_fsmapping(mnt_userns, inode->i_sb->s_user_ns,
456                                   attr->ia_vfsgid))
457                 return -EOVERFLOW;
458
459         /* Don't allow modifications of files with invalid uids or
460          * gids unless those uids & gids are being made valid.
461          */
462         if (!(ia_valid & ATTR_UID) &&
463             !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)))
464                 return -EOVERFLOW;
465         if (!(ia_valid & ATTR_GID) &&
466             !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode)))
467                 return -EOVERFLOW;
468
469         error = security_inode_setattr(mnt_userns, dentry, attr);
470         if (error)
471                 return error;
472         error = try_break_deleg(inode, delegated_inode);
473         if (error)
474                 return error;
475
476         if (inode->i_op->setattr)
477                 error = inode->i_op->setattr(mnt_userns, dentry, attr);
478         else
479                 error = simple_setattr(mnt_userns, dentry, attr);
480
481         if (!error) {
482                 fsnotify_change(dentry, ia_valid);
483                 ima_inode_post_setattr(mnt_userns, dentry);
484                 evm_inode_post_setattr(dentry, ia_valid);
485         }
486
487         return error;
488 }
489 EXPORT_SYMBOL(notify_change);